// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using UnityEngine.Events; using System.Collections.Generic; using UnityEngine.EventSystems; namespace PixelCrushers.QuestMachine { /// /// Unity UI template for a button. /// public class bUnityUIButtonTemplate : UnityUIIconTemplate, IPointerEnterHandler { public UnityEvent onHoverEvent; private List m_actions; protected List actions { get { return m_actions; } set { m_actions = value; } } private int m_groupNumber = ButtonQuestContent.NoGroup; /// /// Group number this button belongs to, or -1 if none. When one button in the group is clicked, the other buttons become non-interactable. /// 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 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 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(); } } }