You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/3rd/StompyRobot/SRF/Scripts/UI/FlashGraphic.cs

72 lines
1.9 KiB
C#

namespace SRF.UI
{
using Internal;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// Instantly sets colour to FlashColor on pointer down, then fades back to DefaultColour once pointer is released.
/// </summary>
[AddComponentMenu(ComponentMenuPaths.FlashGraphic)]
[ExecuteInEditMode]
public class FlashGraphic : UIBehaviour, IPointerDownHandler, IPointerUpHandler
{
public float DecayTime = 0.15f;
public Color DefaultColor = new Color(1, 1, 1, 0);
public Color FlashColor = Color.white;
public Graphic Target;
private bool _isHoldingUntilNextPress;
public void OnPointerDown(PointerEventData eventData)
{
Target.CrossFadeColor(FlashColor, 0f, true, true);
_isHoldingUntilNextPress = false;
}
public void OnPointerUp(PointerEventData eventData)
{
if (!_isHoldingUntilNextPress)
{
Target.CrossFadeColor(DefaultColor, DecayTime, true, true);
}
}
protected override void OnEnable()
{
base.OnEnable();
if (!_isHoldingUntilNextPress)
{
Target.CrossFadeColor(DefaultColor, 0f, true, true);
}
}
#if UNITY_EDITOR
protected void Update()
{
if (!Application.isPlaying && Target != null)
{
Target.CrossFadeColor(DefaultColor, 0, true, true);
}
}
#endif
public void Flash()
{
Target.CrossFadeColor(FlashColor, 0f, true, true);
Target.CrossFadeColor(DefaultColor, DecayTime, true, true);
_isHoldingUntilNextPress = false;
}
public void FlashAndHoldUntilNextPress()
{
Target.CrossFadeColor(FlashColor, 0f, true, true);
_isHoldingUntilNextPress = true;
}
}
}