diff --git a/Assets/Fungus/Scripts/Commands/UiButtonCursorState.cs b/Assets/Fungus/Scripts/Commands/UiButtonCursorState.cs new file mode 100644 index 000000000..f58246ba1 --- /dev/null +++ b/Assets/Fungus/Scripts/Commands/UiButtonCursorState.cs @@ -0,0 +1,147 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.EventSystems; + +public enum UIbuttonEnableDisable +{ + Enable, + Disable +} + +namespace Fungus +{ + /// + /// Mouse cursor state when hovering ui buttons. + /// + [CommandInfo("UI", + "OnMouseCursorHoverButton", + "Change cursor state when entering a button. NOTE: Default Mouse Cursor is OPTIONAL for when the game already has custom cursor")] + [AddComponentMenu("")] + public class UiButtonCursorState : Command + { + [System.Serializable] + public class UiButtonMouseChange + { + public UIbuttonEnableDisable enable = UIbuttonEnableDisable.Disable; + public Button UiButton; + public Texture2D mouseTexture; + public Vector2 hotSpot = Vector2.zero; + public Flowchart flowchart; + public string ExecBlock; + [HideInInspector] public bool eventAdded = false; + } + [Tooltip("Assign UI buttons.")] + [SerializeField] private UiButtonMouseChange[] buttons; + [Tooltip("Optional, if the game already has a custom cursor set. If null, the default system cursor will be used.")] + [SerializeField] private Texture2D defaultMouseCursor; + [SerializeField] private CursorMode cursorMode = CursorMode.Auto; + public virtual bool EnableCursorStateOverButton{get;set;} + private bool custMouse = false; + + private void MouseActions(UiButtonMouseChange uis) + { + if (uis != null && uis.enable == UIbuttonEnableDisable.Enable) + { + if (uis.mouseTexture == null) + return; + + custMouse = true; + Cursor.SetCursor(uis.mouseTexture, uis.hotSpot, cursorMode); + + if(uis.flowchart != null) + { + if (uis.ExecBlock != string.Empty && uis.flowchart.HasBlock(uis.ExecBlock)) + { + if(uis.eventAdded == false) + { + uis.UiButton.onClick.AddListener(() => uis.flowchart.ExecuteBlock(uis.ExecBlock)); + uis.eventAdded = true; + } + } + } + } + } + public virtual void SetDefaultMouseCursor() + { + if (custMouse) + { + if(defaultMouseCursor == null) + Cursor.SetCursor(null, Vector2.zero, cursorMode); + else + Cursor.SetCursor(defaultMouseCursor, Vector2.zero, cursorMode); + + custMouse = false; + } + } + public virtual void DisableUiButtonCustomState() + { + EnableCursorStateOverButton = false; + } + // Update is called once per frame + void Update() + { + if(EnableCursorStateOverButton) + { + if (EventSystem.current.IsPointerOverGameObject()) + { + void EventLoops() + { + PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); + eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y); + List results = new List(); + EventSystem.current.RaycastAll(eventDataCurrentPosition, results); + + for (int h = 0; h < results.Count; h++) + { + for (int j = 0; j < buttons.Length; j++) + { + if (results[h].gameObject.GetInstanceID() == buttons[j].UiButton.gameObject.GetInstanceID()) + { + MouseActions(buttons[j]); + return; + } + } + } + } + EventLoops(); + } + else + { + if (custMouse) + SetDefaultMouseCursor(); + } + } + } + public override string GetSummary() + { + string btn = string.Empty; + string txtr = string.Empty; + + for(int i = 0; i < buttons.Length; i++) + { + if(buttons[i].enable == UIbuttonEnableDisable.Enable && buttons[i].UiButton == null) + { + btn = "UI button slot can't be empty"; + } + + if(buttons[i].enable == UIbuttonEnableDisable.Enable && buttons[i].mouseTexture == null) + { + txtr = "Mouse Texture can't slot can't be empty"; + } + } + + return btn + " : " + txtr; + } + public override void OnEnter() + { + EnableCursorStateOverButton = true; + Continue(); + } + public override Color GetButtonColor() + { + return new Color32(200, 163, 180, 255); + } + } +}