Files
beyond/Assets/Scripts/QuestMachineDerivatives/bUnityUIButtonTemplate.cs
2024-11-20 15:21:28 +01:00

92 lines
2.7 KiB
C#

// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
using UnityEngine.Events;
using System.Collections.Generic;
using UnityEngine.EventSystems;
namespace PixelCrushers.QuestMachine
{
/// <summary>
/// Unity UI template for a button.
/// </summary>
public class bUnityUIButtonTemplate : UnityUIIconTemplate, IPointerEnterHandler
{
public UnityEvent onHoverEvent;
private List<QuestAction> m_actions;
protected List<QuestAction> actions
{
get { return m_actions; }
set { m_actions = value; }
}
private int m_groupNumber = ButtonQuestContent.NoGroup;
/// <summary>
/// Group number this button belongs to, or -1 if none. When one button in the group is clicked, the other buttons become non-interactable.
/// </summary>
public int groupNumber
{
get { return m_groupNumber; }
set { m_groupNumber = value; }
}
public override void Awake()
{
base.Awake();
// if (button == null && Debug.isDebugBuild) Debug.LogError("Quest Machine: UI Button is unassigned.", this);
}
public virtual void Assign(Sprite sprite, int count, string caption, List<QuestAction> actions)
{
base.Assign(sprite, count, caption);
this.actions = actions;
onHoverEvent.RemoveAllListeners();
if (actions != null) onHoverEvent.AddListener(ExecuteActions);
}
public virtual void Assign(Sprite sprite, string caption, List<QuestAction> actions)
{
Assign(sprite, 1, caption, actions);
}
public virtual void Assign(Sprite sprite, int count, string caption, UnityAction unityAction)
{
Assign(sprite, count, caption);
if (unityAction != null)
{
onHoverEvent.AddListener(unityAction);
}
else
{
}
}
public virtual void Assign(Sprite sprite, string caption, UnityAction unityAction)
{
Assign(sprite, 1, caption, unityAction);
}
protected virtual void ExecuteActions()
{
if (actions == null) return;
for (int i = 0; i < actions.Count; i++)
{
if (actions[i] != null) actions[i].Execute();
}
if (groupNumber != ButtonQuestContent.NoGroup)
{
MessageSystem.SendMessage(this, QuestMachineMessages.GroupButtonClickedMessage, string.Empty, groupNumber);
}
}
public void OnPointerEnter(PointerEventData eventData)
{
onHoverEvent?.Invoke();
}
}
}