From 14226652dc28df73582dbb19104e5b00c6a4653c Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Tue, 4 Aug 2015 12:40:27 +0100 Subject: [PATCH] Invoke command - call methods with parameters. --- .../Fungus/Flowchart/Editor/InvokeEditor.cs | 71 ++++++++++ .../InvokeEditor.cs.meta} | 4 +- .../Flowchart/Scripts/Commands/Invoke.cs | 133 ++++++++++++++++++ .../{SetText.cs.meta => Invoke.cs.meta} | 4 +- 4 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 Assets/Fungus/Flowchart/Editor/InvokeEditor.cs rename Assets/Fungus/Flowchart/{Scripts/Commands/GetText.cs.meta => Editor/InvokeEditor.cs.meta} (76%) create mode 100644 Assets/Fungus/Flowchart/Scripts/Commands/Invoke.cs rename Assets/Fungus/Flowchart/Scripts/Commands/{SetText.cs.meta => Invoke.cs.meta} (76%) diff --git a/Assets/Fungus/Flowchart/Editor/InvokeEditor.cs b/Assets/Fungus/Flowchart/Editor/InvokeEditor.cs new file mode 100644 index 00000000..702cefbb --- /dev/null +++ b/Assets/Fungus/Flowchart/Editor/InvokeEditor.cs @@ -0,0 +1,71 @@ +using UnityEditor; +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +namespace Fungus +{ + [CustomEditor (typeof(Invoke))] + public class InvokeEditor : CommandEditor + { + protected SerializedProperty delayProp; + protected SerializedProperty invokeTypeProp; + protected SerializedProperty staticEventProp; + protected SerializedProperty booleanParameterProp; + protected SerializedProperty booleanEventProp; + protected SerializedProperty integerParameterProp; + protected SerializedProperty integerEventProp; + protected SerializedProperty floatParameterProp; + protected SerializedProperty floatEventProp; + protected SerializedProperty stringParameterProp; + protected SerializedProperty stringEventProp; + + protected virtual void OnEnable() + { + delayProp = serializedObject.FindProperty("delay"); + invokeTypeProp = serializedObject.FindProperty("invokeType"); + staticEventProp = serializedObject.FindProperty("staticEvent"); + booleanParameterProp = serializedObject.FindProperty("booleanParameter"); + booleanEventProp = serializedObject.FindProperty("booleanEvent"); + integerParameterProp = serializedObject.FindProperty("integerParameter"); + integerEventProp = serializedObject.FindProperty("integerEvent"); + floatParameterProp = serializedObject.FindProperty("floatParameter"); + floatEventProp = serializedObject.FindProperty("floatEvent"); + stringParameterProp = serializedObject.FindProperty("stringParameter"); + stringEventProp = serializedObject.FindProperty("stringEvent"); + } + + public override void DrawCommandGUI() + { + serializedObject.Update(); + + EditorGUILayout.PropertyField(delayProp); + EditorGUILayout.PropertyField(invokeTypeProp); + + switch ((Invoke.InvokeType)invokeTypeProp.enumValueIndex) + { + case Invoke.InvokeType.Static: + EditorGUILayout.PropertyField(staticEventProp); + break; + case Invoke.InvokeType.DynamicBoolean: + EditorGUILayout.PropertyField(booleanEventProp); + EditorGUILayout.PropertyField(booleanParameterProp); + break; + case Invoke.InvokeType.DynamicInteger: + EditorGUILayout.PropertyField(integerEventProp); + EditorGUILayout.PropertyField(integerParameterProp); + break; + case Invoke.InvokeType.DynamicFloat: + EditorGUILayout.PropertyField(floatEventProp); + EditorGUILayout.PropertyField(floatParameterProp); + break; + case Invoke.InvokeType.DynamicString: + EditorGUILayout.PropertyField(stringEventProp); + EditorGUILayout.PropertyField(stringParameterProp); + break; + } + + serializedObject.ApplyModifiedProperties(); + } + } +} diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs.meta b/Assets/Fungus/Flowchart/Editor/InvokeEditor.cs.meta similarity index 76% rename from Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs.meta rename to Assets/Fungus/Flowchart/Editor/InvokeEditor.cs.meta index 2d8509b5..24e0fa99 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/GetText.cs.meta +++ b/Assets/Fungus/Flowchart/Editor/InvokeEditor.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 54baf7aee38a7425ca4bd2addbfcfdcf -timeCreated: 1431530474 +guid: 1de123a9a8da54ff49b112d39101366b +timeCreated: 1437051529 licenseType: Free MonoImporter: serializedVersion: 2 diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/Invoke.cs b/Assets/Fungus/Flowchart/Scripts/Commands/Invoke.cs new file mode 100644 index 00000000..f3f0b16a --- /dev/null +++ b/Assets/Fungus/Flowchart/Scripts/Commands/Invoke.cs @@ -0,0 +1,133 @@ +using UnityEngine; +using System.Collections; +using System; +using UnityEngine.Events; + +namespace Fungus +{ + [CommandInfo("Scripting", + "Invoke", + "Calls a list of methods via the Unity UI EventSystem.")] + [AddComponentMenu("")] + + // This command uses the UnityEvent system to call methods in script. + // http://docs.unity3d.com/Manual/UnityEvents.html + public class Invoke : Command + { + [Serializable] public class BooleanEvent : UnityEvent {} + [Serializable] public class IntegerEvent : UnityEvent {} + [Serializable] public class FloatEvent : UnityEvent {} + [Serializable] public class StringEvent : UnityEvent {} + + public enum InvokeType + { + Static, // Call a method with an optional constant value parameter + DynamicBoolean, // Call a method with an optional boolean constant / variable parameter + DynamicInteger, // Call a method with an optional integer constant / variable parameter + DynamicFloat, // Call a method with an optional float constant / variable parameter + DynamicString // Call a method with an optional string constant / variable parameter + } + + [Tooltip("Delay (in seconds) before the methods will be called")] + public float delay; + + public InvokeType invokeType; + + [Tooltip("List of methods to call. Supports methods with no parameters or exactly one string, int, float or object parameter.")] + public UnityEvent staticEvent = new UnityEvent(); + + [Tooltip("Boolean parameter to pass to the invoked methods.")] + public BooleanData booleanParameter; + + [Tooltip("List of methods to call. Supports methods with one boolean parameter.")] + public BooleanEvent booleanEvent = new BooleanEvent(); + + [Tooltip("Integer parameter to pass to the invoked methods.")] + public IntegerData integerParameter; + + [Tooltip("List of methods to call. Supports methods with one integer parameter.")] + public IntegerEvent integerEvent = new IntegerEvent(); + + [Tooltip("Float parameter to pass to the invoked methods.")] + public FloatData floatParameter; + + [Tooltip("List of methods to call. Supports methods with one float parameter.")] + public FloatEvent floatEvent = new FloatEvent(); + + [Tooltip("String parameter to pass to the invoked methods.")] + public StringData stringParameter; + + [Tooltip("List of methods to call. Supports methods with one string parameter.")] + public StringEvent stringEvent = new StringEvent(); + + public override void OnEnter() + { + if (delay == 0f) + { + DoInvoke(); + } + else + { + Invoke("DoInvoke", delay); + } + + Continue(); + } + + protected virtual void DoInvoke() + { + switch (invokeType) + { + default: + case InvokeType.Static: + staticEvent.Invoke(); + break; + case InvokeType.DynamicBoolean: + booleanEvent.Invoke(booleanParameter); + break; + case InvokeType.DynamicInteger: + integerEvent.Invoke(integerParameter); + break; + case InvokeType.DynamicFloat: + floatEvent.Invoke(floatParameter); + break; + case InvokeType.DynamicString: + stringEvent.Invoke(stringParameter); + break; + } + } + + public override string GetSummary() + { + string summary = invokeType.ToString() + " "; + + switch (invokeType) + { + default: + case InvokeType.Static: + summary += staticEvent.GetPersistentEventCount(); + break; + case InvokeType.DynamicBoolean: + summary += booleanEvent.GetPersistentEventCount(); + break; + case InvokeType.DynamicInteger: + summary += integerEvent.GetPersistentEventCount(); + break; + case InvokeType.DynamicFloat: + summary += floatEvent.GetPersistentEventCount(); + break; + case InvokeType.DynamicString: + summary += stringEvent.GetPersistentEventCount(); + break; + } + + return summary + " methods"; + } + + public override Color GetButtonColor() + { + return new Color32(235, 191, 217, 255); + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs.meta b/Assets/Fungus/Flowchart/Scripts/Commands/Invoke.cs.meta similarity index 76% rename from Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs.meta rename to Assets/Fungus/Flowchart/Scripts/Commands/Invoke.cs.meta index a3987454..c9967c8e 100644 --- a/Assets/Fungus/Flowchart/Scripts/Commands/SetText.cs.meta +++ b/Assets/Fungus/Flowchart/Scripts/Commands/Invoke.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 7ec5abc702c3f400b91bbeb2b8328d02 -timeCreated: 1431529735 +guid: 95d9ff288f3904c05ada7ac9c9a075bb +timeCreated: 1436969477 licenseType: Free MonoImporter: serializedVersion: 2