Browse Source

Refactored EventHandler to use IEventHandler

master
Christopher 9 years ago
parent
commit
dc0ea43558
  1. 19
      Assets/Fungus/Flowchart/Scripts/EventHandler.cs
  2. 2
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs
  3. 1
      Assets/Fungus/Flowchart/Scripts/IBlock.cs
  4. 30
      Assets/Fungus/Flowchart/Scripts/IEventHandler.cs
  5. 12
      Assets/Fungus/Flowchart/Scripts/IEventHandler.cs.meta
  6. 2
      Assets/Fungus/Scripts/Editor/FlowchartMenuItems.cs

19
Assets/Fungus/Flowchart/Scripts/EventHandler.cs

@ -32,23 +32,19 @@ namespace Fungus
/// Add an EventHandlerInfo attibute and your new EventHandler class will automatically appear in the
/// 'Execute On Event' dropdown menu when a block is selected.
/// </summary>
[RequireComponent(typeof(IBlock))]
[RequireComponent(typeof(Block))]
[RequireComponent(typeof(Flowchart))]
[AddComponentMenu("")]
public class EventHandler : MonoBehaviour
public class EventHandler : MonoBehaviour, IEventHandler
{
/// <summary>
/// Returns the parent Block which owns this Event Handler.
/// </summary>
/// <value>The parent block.</value>
[HideInInspector]
[FormerlySerializedAs("parentSequence")]
[SerializeField] protected Block parentBlock;
#region IEventHandler
public virtual IBlock ParentBlock { get { return parentBlock; } set { parentBlock = (Block)value; } }
/// <summary>
/// The Event Handler should call this method when the event is detected.
/// </summary>
public virtual bool ExecuteBlock()
{
if (parentBlock == null)
@ -72,12 +68,11 @@ namespace Fungus
return flowchart.ExecuteBlock(parentBlock);
}
/// <summary>
/// Returns custom summary text for the event handler.
/// </summary>
public virtual string GetSummary()
{
return "";
}
#endregion
}
}

2
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -890,7 +890,7 @@ namespace Fungus
}
EventHandler[] eventHandlers = GetComponents<EventHandler>();
foreach (EventHandler eventHandler in eventHandlers)
foreach (var eventHandler in eventHandlers)
{
eventHandler.hideFlags = HideFlags.HideInInspector;
}

1
Assets/Fungus/Flowchart/Scripts/IBlock.cs

@ -40,6 +40,7 @@ namespace Fungus
/// <summary>
/// An optional Event Handler which can execute the block when an event occurs.
/// Note: Using the concrete class instead of the interface here because of weird editor behaviour.
/// </summary>
EventHandler _EventHandler { get; set; }

30
Assets/Fungus/Flowchart/Scripts/IEventHandler.cs

@ -0,0 +1,30 @@
using UnityEngine;
namespace Fungus
{
/// <summary>
/// A Block may have an associated Event Handler which starts executing commands when
/// a specific event occurs.
/// To create a custom Event Handler, simply subclass EventHandler and call the ExecuteBlock() method
/// when the event occurs.
/// Add an EventHandlerInfo attibute and your new EventHandler class will automatically appear in the
/// 'Execute On Event' dropdown menu when a block is selected.
/// </summary>
public interface IEventHandler
{
/// <summary>
/// The parent Block which owns this Event Handler.
/// </summary>
IBlock ParentBlock { get; set; }
/// <summary>
/// The Event Handler should call this method when the event is detected to start executing the Block.
/// </summary>
bool ExecuteBlock();
/// <summary>
/// Returns custom summary text for the event handler.
/// </summary>
string GetSummary();
}
}

12
Assets/Fungus/Flowchart/Scripts/IEventHandler.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5ae3dddb3147c4a07a851843721affe8
timeCreated: 1473856414
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

2
Assets/Fungus/Scripts/Editor/FlowchartMenuItems.cs

@ -25,8 +25,8 @@ namespace Fungus
if (GameObject.FindObjectsOfType<Flowchart>().Length > 1)
{
IBlock block = go.GetComponent<IBlock>();
block._EventHandler = null;
GameObject.DestroyImmediate(block._EventHandler);
block._EventHandler = null;
}
}

Loading…
Cancel
Save