/// Credit AriathTheWise
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2#post-1796783

using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace UnityEngine.UI.Extensions
{
    /// <summary>
    /// UIButton
    /// </summary>
    [AddComponentMenu("UI/Extensions/UI Button")]
    public class UIButton : Button, IPointerDownHandler, IPointerUpHandler
    {
        #region Sub-Classes
        [System.Serializable]
        public class UIButtonEvent : UnityEvent<PointerEventData.InputButton> { }
        #endregion

        #region Events
        public UIButtonEvent OnButtonClick;
        public UIButtonEvent OnButtonPress;
        public UIButtonEvent OnButtonRelease;
        #endregion

        public override void OnPointerClick(PointerEventData eventData)
        {
            base.OnSubmit(eventData);

            if (OnButtonClick != null)
            {
                OnButtonClick.Invoke(eventData.button);
            }
        }


        void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
        {
            DoStateTransition(SelectionState.Pressed, false);

            if (OnButtonPress != null)
            {
                OnButtonPress.Invoke(eventData.button);
            }
        }


        void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
        {
            DoStateTransition(SelectionState.Normal, false);

            if (OnButtonRelease != null)
            {
                OnButtonRelease.Invoke(eventData.button);
            }
        }
    }
}