ProjectDDD/Packages/SLUnity/SLUI/SLUIHover.cs

173 lines
5.4 KiB
C#
Raw Normal View History

using UnityEngine;
using UnityEngine.EventSystems;
namespace Superlazy.UI
{
public class SLUIHover : SLUIComponent, IPointerEnterHandler, IPointerExitHandler, IPointerMoveHandler
{
public string enterCommand;
public string exitCommand;
public bool useLongHover;
public string longHoverCommand;
public SLValueComparison comparison;
public string hoverSound;
private Canvas canvas;
private bool hovered = false;
private GameObject hoverRing;
protected override void Validate()
{
canvas = GetComponentInParent<Canvas>();
}
protected override void Init()
{
}
protected override void Enable()
{
if (useLongHover)
{
if (hoverRing == null)
{
hoverRing = SLResources.CreatePrefabInstance("UIComponents/HoverRing", gameObject.transform);
}
hoverRing.SetActive(false);
}
if (comparison.useCheckValue) comparison.OnEnable(bindParent.BindPath, OnChange);
}
protected override void Disable()
{
if (comparison.useCheckValue) comparison.OnDisable();
if (useLongHover)
{
if (hoverRing)
{
hoverRing.SetActive(false);
}
}
if (hovered)
{
EndHover();
hovered = false;
}
}
public void Update()
{
if (comparison.useCheckValue && comparison.Result == false) return;
if (useLongHover && hovered)
{
if (SLGame.Session["Hover"]["HoverTime"])
{
if (SLGame.Session["Hover"]["HoverDelay"])
{
SLGame.Session["Hover"]["HoverDelay"] -= Time.deltaTime;
if (SLGame.Session["Hover"]["HoverDelay"] <= 0)
{
SLGame.Session["Hover"]["HoverDelay"] = false;
}
return;
}
SLGame.Session["Hover"]["HoverTime"] += Time.deltaTime;
if (SLGame.Session["Hover"]["HoverTime"] > SLGame.Session["Hover"]["HoverTimeMax"])
{
SLGame.Session["Hover"]["HoverTime"] = false;
SLGame.Session["Hover"]["HoverTimeMax"] = false;
hoverRing.gameObject.SetActive(false);
LongHover();
}
}
}
}
private void OnChange()
{
if (bindParent.Active == false) return;
if (hovered && comparison.useCheckValue && comparison.Result == false)
{
EndHover();
}
}
public void OnPointerEnter(PointerEventData eventData)
{
hovered = true;
if (useLongHover)
{
hoverRing.gameObject.SetActive(true);
SLGame.Session["Hover"]["HoverTimeMax"] = SLSystem.Data["Default"]["LongHoverDuration"];
SLGame.Session["Hover"]["HoverTime"] = 0;
}
BeginHover();
}
public void OnPointerExit(PointerEventData eventData)
{
hovered = false;
if (useLongHover)
{
hoverRing.gameObject.SetActive(false);
SLGame.Session["Hover"]["HoverTimeMax"] = false;
SLGame.Session["Hover"]["HoverTime"] = false;
}
EndHover();
}
public void OnPointerMove(PointerEventData eventData)
{
if (useLongHover && SLGame.Session["Hover"]["HoverTime"])
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(gameObject.GetComponent<RectTransform>(), eventData.position, canvas.worldCamera, out var localPoint);
hoverRing.GetComponent<RectTransform>().localPosition = localPoint;
SLGame.Session["Hover"]["HoverTime"] = 0;
SLGame.Session["Hover"]["HoverDelay"] = SLSystem.Data["Default"]["LongHoverDelay"];
}
}
private void BeginHover()
{
if (comparison.useCheckValue && comparison.Result == false) return;
if (enterCommand == string.Empty) return;
if (string.IsNullOrEmpty(hoverSound) == false)
{
SLSound.PlaySound(hoverSound, "UI", false, false, true, 0.5f);
}
SLGame.Command(enterCommand, SLGame.SessionGet(bindParent.BindPath));
}
private void EndHover()
{
if (comparison.useCheckValue && comparison.Result == false) return;
if (exitCommand == string.Empty) return;
SLGame.Command(exitCommand, SLGame.SessionGet(bindParent.BindPath));
}
private void LongHover()
{
if (comparison.useCheckValue && comparison.Result == false) return;
if (longHoverCommand == string.Empty) return;
if (string.IsNullOrEmpty(hoverSound) == false)
{
SLSound.PlaySound(hoverSound, "UI", false, false, true, 0.5f);
}
SLGame.Command(longHoverCommand, SLGame.SessionGet(bindParent.BindPath));
}
}
}