summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/ControllerUIEffect.cs
blob: 695aa76caedb93fbf82fa0f01f59ea9456e98036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using UnityEngine;
using UnityEngine.UI;

namespace Rewired.Demos.GamepadTemplateUI;

[RequireComponent(typeof(Image))]
public class ControllerUIEffect : MonoBehaviour
{
	[SerializeField]
	private Color _highlightColor = Color.white;

	private Image _image;

	private Color _color;

	private Color _origColor;

	private bool _isActive;

	private float _highlightAmount;

	private void Awake()
	{
		_image = GetComponent<Image>();
		_origColor = _image.color;
		_color = _origColor;
	}

	public void Activate(float amount)
	{
		amount = Mathf.Clamp01(amount);
		if (!_isActive || amount != _highlightAmount)
		{
			_highlightAmount = amount;
			_color = Color.Lerp(_origColor, _highlightColor, _highlightAmount);
			_isActive = true;
			RedrawImage();
		}
	}

	public void Deactivate()
	{
		if (_isActive)
		{
			_color = _origColor;
			_highlightAmount = 0f;
			_isActive = false;
			RedrawImage();
		}
	}

	private void RedrawImage()
	{
		_image.color = _color;
		_image.enabled = _isActive;
	}
}