Updated Cooldown button to work with Keyboard input

Resolves: https://github.com/Unity-UI-Extensions/com.unity.uiextensions/issues/171
pull/413/head
Simon Jackson 2022-12-18 19:29:40 +00:00
parent 00b67b7046
commit 85ee380ee9
1 changed files with 24 additions and 15 deletions

View File

@ -7,15 +7,14 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[AddComponentMenu("UI/Extensions/Cooldown Button")] [AddComponentMenu("UI/Extensions/Cooldown Button")]
public class CooldownButton : MonoBehaviour, IPointerDownHandler public class CooldownButton : MonoBehaviour, IPointerDownHandler, ISubmitHandler
{ {
#region Sub-Classes #region Sub-Classes
[System.Serializable] [System.Serializable]
public class CooldownButtonEvent : UnityEvent<PointerEventData.InputButton> { } public class CooldownButtonEvent : UnityEvent<GameObject> { }
#endregion #endregion
#region Private variables #region Private variables
[SerializeField] [SerializeField]
private float cooldownTimeout; private float cooldownTimeout;
[SerializeField] [SerializeField]
@ -33,7 +32,7 @@ namespace UnityEngine.UI.Extensions
[SerializeField][ReadOnly] [SerializeField][ReadOnly]
private int cooldownPercentComplete; private int cooldownPercentComplete;
PointerEventData buttonSource; BaseEventData buttonSource;
#endregion #endregion
#region Public Properties #region Public Properties
@ -116,7 +115,6 @@ namespace UnityEngine.UI.Extensions
#endregion #endregion
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Pause Cooldown without resetting values, allows Restarting of cooldown /// Pause Cooldown without resetting values, allows Restarting of cooldown
/// </summary> /// </summary>
@ -144,9 +142,9 @@ namespace UnityEngine.UI.Extensions
/// </summary> /// </summary>
public void StartCooldown() public void StartCooldown()
{ {
PointerEventData emptySource = new PointerEventData(EventSystem.current); BaseEventData emptySource = new BaseEventData(EventSystem.current);
buttonSource = emptySource; buttonSource = emptySource;
OnCooldownStart.Invoke(emptySource.button); OnCooldownStart.Invoke(emptySource.selectedObject);
cooldownTimeRemaining = cooldownTimeout; cooldownTimeRemaining = cooldownTimeout;
CooldownActive = cooldownInEffect = true; CooldownActive = cooldownInEffect = true;
} }
@ -161,7 +159,7 @@ namespace UnityEngine.UI.Extensions
cooldownPercentRemaining = 0; cooldownPercentRemaining = 0;
cooldownPercentComplete = 100; cooldownPercentComplete = 100;
cooldownActive = cooldownInEffect = false; cooldownActive = cooldownInEffect = false;
if (OnCoolDownFinish != null) OnCoolDownFinish.Invoke(buttonSource.button); OnCoolDownFinish?.Invoke(buttonSource.selectedObject);
} }
/// <summary> /// <summary>
@ -171,27 +169,38 @@ namespace UnityEngine.UI.Extensions
{ {
cooldownActive = cooldownInEffect = false; cooldownActive = cooldownInEffect = false;
} }
#endregion #endregion
#region IPointerDownHandler #region IPointerDownHandler
void IPointerDownHandler.OnPointerDown(PointerEventData eventData) void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
{
HandleButtonClick(eventData);
}
#endregion
#region ISubmitHandler
public void OnSubmit(BaseEventData eventData)
{
HandleButtonClick(eventData);
}
#endregion ISubmitHandler
#region Private Methods
public void HandleButtonClick(BaseEventData eventData)
{ {
buttonSource = eventData; buttonSource = eventData;
if (CooldownInEffect) if (CooldownInEffect)
{ {
if (OnButtonClickDuringCooldown != null) OnButtonClickDuringCooldown.Invoke(eventData.button); OnButtonClickDuringCooldown?.Invoke(buttonSource.selectedObject);
} }
if (!CooldownInEffect) if (!CooldownInEffect)
{ {
if(OnCooldownStart != null) OnCooldownStart.Invoke(eventData.button); OnCooldownStart?.Invoke(buttonSource.selectedObject);
cooldownTimeRemaining = cooldownTimeout; cooldownTimeRemaining = cooldownTimeout;
cooldownActive = cooldownInEffect = true; cooldownActive = cooldownInEffect = true;
} }
} }
#endregion Private Methods
#endregion
} }
} }