Browse Source

Refactored Clickable2D to use IClickable2D interface

master
Christopher 8 years ago
parent
commit
8dcb56a9e8
  1. 91
      Assets/Fungus/Sprite/Scripts/Clickable2D.cs
  2. 15
      Assets/Fungus/Sprite/Scripts/IClickable2D.cs
  3. 12
      Assets/Fungus/Sprite/Scripts/IClickable2D.cs.meta

91
Assets/Fungus/Sprite/Scripts/Clickable2D.cs

@ -15,7 +15,6 @@ namespace Fungus
{ {
[Tooltip("Is object clicking enabled")] [Tooltip("Is object clicking enabled")]
[SerializeField] protected bool clickEnabled = true; [SerializeField] protected bool clickEnabled = true;
public bool ClickEnabled { set { clickEnabled = value; } }
[Tooltip("Mouse texture to use when hovering mouse over object")] [Tooltip("Mouse texture to use when hovering mouse over object")]
[SerializeField] protected Texture2D hoverCursor; [SerializeField] protected Texture2D hoverCursor;
@ -23,7 +22,44 @@ namespace Fungus
[Tooltip("Use the UI Event System to check for clicks. Clicks that hit an overlapping UI object will be ignored. Camera must have a PhysicsRaycaster component, or a Physics2DRaycaster for 2D colliders.")] [Tooltip("Use the UI Event System to check for clicks. Clicks that hit an overlapping UI object will be ignored. Camera must have a PhysicsRaycaster component, or a Physics2DRaycaster for 2D colliders.")]
[SerializeField] protected bool useEventSystem; [SerializeField] protected bool useEventSystem;
protected virtual void ChangeCursor(Texture2D cursorTexture)
{
if (!clickEnabled)
{
return;
}
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto);
}
protected virtual void DoPointerClick()
{
if (!clickEnabled)
{
return;
}
// TODO: Cache these objects for faster lookup
ObjectClicked[] handlers = GameObject.FindObjectsOfType<ObjectClicked>();
foreach (ObjectClicked handler in handlers)
{
handler.OnObjectClicked(this);
}
}
protected virtual void DoPointerEnter()
{
ChangeCursor(hoverCursor);
}
protected virtual void DoPointerExit()
{
// Always reset the mouse cursor to be on the safe side
SetMouseCursor.ResetMouseCursor();
}
#region Legacy OnMouseX methods #region Legacy OnMouseX methods
protected virtual void OnMouseDown() protected virtual void OnMouseDown()
{ {
if (!useEventSystem) if (!useEventSystem)
@ -47,9 +83,17 @@ namespace Fungus
DoPointerExit(); DoPointerExit();
} }
} }
#endregion
#region IClickable2D implementation
public bool ClickEnabled { set { clickEnabled = value; } }
#endregion #endregion
#region IPointerXHandler implementations #region IPointerClickHandler implementation
public void OnPointerClick(PointerEventData eventData) public void OnPointerClick(PointerEventData eventData)
{ {
if (useEventSystem) if (useEventSystem)
@ -58,6 +102,10 @@ namespace Fungus
} }
} }
#endregion
#region IPointerEnterHandler implementation
public void OnPointerEnter(PointerEventData eventData) public void OnPointerEnter(PointerEventData eventData)
{ {
if (useEventSystem) if (useEventSystem)
@ -66,6 +114,10 @@ namespace Fungus
} }
} }
#endregion
#region IPointerExitHandler implementation
public void OnPointerExit(PointerEventData eventData) public void OnPointerExit(PointerEventData eventData)
{ {
if (useEventSystem) if (useEventSystem)
@ -74,41 +126,6 @@ namespace Fungus
} }
} }
protected virtual void DoPointerClick()
{
if (!clickEnabled)
{
return;
}
// TODO: Cache these objects for faster lookup
ObjectClicked[] handlers = GameObject.FindObjectsOfType<ObjectClicked>();
foreach (ObjectClicked handler in handlers)
{
handler.OnObjectClicked(this);
}
}
protected virtual void DoPointerEnter()
{
ChangeCursor(hoverCursor);
}
protected virtual void DoPointerExit()
{
// Always reset the mouse cursor to be on the safe side
SetMouseCursor.ResetMouseCursor();
}
#endregion #endregion
protected virtual void ChangeCursor(Texture2D cursorTexture)
{
if (!clickEnabled)
{
return;
}
Cursor.SetCursor(cursorTexture, Vector2.zero, CursorMode.Auto);
}
} }
} }

15
Assets/Fungus/Sprite/Scripts/IClickable2D.cs

@ -0,0 +1,15 @@
namespace Fungus
{
/// <summary>
/// Detects mouse clicks and touches on a Game Object, and sends an event to all Flowchart event handlers in the scene.
/// The Game Object must have a Collider or Collider2D component attached.
/// Use in conjunction with the ObjectClicked Flowchart event handler.
/// </summary>
public interface IClickable2D
{
/// <summary>
/// Is object clicking enabled.
/// </summary>
bool ClickEnabled { set; }
}
}

12
Assets/Fungus/Sprite/Scripts/IClickable2D.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 477f75d30919449dfa922f74038ceaa4
timeCreated: 1473690975
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save