Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On UI Button Hover New Command #1049

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions Assets/Fungus/Scripts/Commands/UiButtonCursorState.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Mouse cursor state when hovering ui buttons.
/// </summary>
[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<RaycastResult> results = new List<RaycastResult>();
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);
}
}
}