From f617b72dee1b169ccdcb69c1db2c882265668f97 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Thu, 31 Jul 2014 12:25:30 +0100 Subject: [PATCH] Refactored variables --- .../Fungus/Editor/SequenceControllerEditor.cs | 9 ++- .../Fungus/Editor/SetVariableCommandEditor.cs | 55 ++++++++---------- .../Fungus/Tests/Sequence/SequenceTest.unity | Bin 72964 -> 73028 bytes .../VisualScripting/SetVariableCommand.cs | 37 ++++-------- Assets/Fungus/VisualScripting/Variable.cs | 52 ++++++++++++++++- 5 files changed, 89 insertions(+), 64 deletions(-) diff --git a/Assets/Fungus/Editor/SequenceControllerEditor.cs b/Assets/Fungus/Editor/SequenceControllerEditor.cs index f4253dd6..53b77ef4 100644 --- a/Assets/Fungus/Editor/SequenceControllerEditor.cs +++ b/Assets/Fungus/Editor/SequenceControllerEditor.cs @@ -34,7 +34,7 @@ public class VariableDrawer : PropertyDrawer keyValue = new string(arr); keyProp.stringValue = keyValue; - typeProp.enumValueIndex = selectedEnum; + typeProp.enumValueIndex = selectedEnum; } } } @@ -42,12 +42,11 @@ public class VariableDrawer : PropertyDrawer [CustomEditor (typeof(SequenceController))] public class SequenceControllerEditor : Editor { - SerializedProperty globalVariablesProperty; - SerializedProperty localVariablesProperty; + SerializedProperty variablesProperty; void OnEnable() { - localVariablesProperty = serializedObject.FindProperty("variables"); + variablesProperty = serializedObject.FindProperty("variables"); } public override void OnInspectorGUI() @@ -92,7 +91,7 @@ public class SequenceControllerEditor : Editor } ReorderableListGUI.Title("Variables"); - ReorderableListGUI.ListField(localVariablesProperty); + ReorderableListGUI.ListField(variablesProperty); serializedObject.ApplyModifiedProperties(); } diff --git a/Assets/Fungus/Editor/SetVariableCommandEditor.cs b/Assets/Fungus/Editor/SetVariableCommandEditor.cs index 2bed13e0..f5e7bae0 100644 --- a/Assets/Fungus/Editor/SetVariableCommandEditor.cs +++ b/Assets/Fungus/Editor/SetVariableCommandEditor.cs @@ -26,78 +26,71 @@ namespace Fungus { Variable v = sc.variables[i]; keys.Add(v.key); - if (v.key == t.variableKey && + if (v.key == t.variable.key && index == 0) { index = i + 1; } } - - int newIndex = EditorGUILayout.Popup("Variable", index, keys.ToArray()); - if (newIndex != index) + bool keyChanged = (t.variable.key != keys[newIndex]); + + if (keyChanged) { Undo.RecordObject(t, "Select variable"); + t.variable.key = keys[newIndex]; } - if (newIndex == 0) + if (t.variable.key == "") { - t.variableKey = ""; return; } - Variable variable = sc.variables[newIndex - 1]; - - if (t.variableKey != variable.key) - { - Undo.RecordObject(t, "Select variable"); - t.variableKey = variable.key; - } + VariableType variableType = sc.variables[newIndex - 1].type; - bool booleanValue = t.booleanValue; - int integerValue = t.integerValue; - float floatValue = t.floatValue; - string stringValue = t.stringValue; + bool booleanValue = t.variable.booleanValue; + int integerValue = t.variable.integerValue; + float floatValue = t.variable.floatValue; + string stringValue = t.variable.stringValue; - switch (variable.type) + switch (variableType) { case VariableType.Boolean: - booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), t.booleanValue); + booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); break; case VariableType.Integer: - integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), t.integerValue); + integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); break; case VariableType.Float: - floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), t.floatValue); + floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); break; case VariableType.String: - stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), t.stringValue); + stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); break; } - if (booleanValue != t.booleanValue) + if (booleanValue != t.variable.booleanValue) { Undo.RecordObject(t, "Set boolean value"); - t.booleanValue = booleanValue; + t.variable.booleanValue = booleanValue; } - else if (integerValue != t.integerValue) + else if (integerValue != t.variable.integerValue) { Undo.RecordObject(t, "Set integer value"); - t.integerValue = integerValue; + t.variable.integerValue = integerValue; } - else if (floatValue != t.floatValue) + else if (floatValue != t.variable.floatValue) { Undo.RecordObject(t, "Set float value"); - t.floatValue = floatValue; + t.variable.floatValue = floatValue; } - else if (stringValue != t.stringValue) + else if (stringValue != t.variable.stringValue) { Undo.RecordObject(t, "Set string value"); - t.stringValue = stringValue; + t.variable.stringValue = stringValue; } - } } diff --git a/Assets/Fungus/Tests/Sequence/SequenceTest.unity b/Assets/Fungus/Tests/Sequence/SequenceTest.unity index 7f8cc4a8206bcbd91e141875630bb1afa6cbcbfd..1a1ca9072999eeaf5ca79e23f9d1df88d27ec4fe 100644 GIT binary patch delta 512 zcmYk0%_~G<6voeMX6~H{xX^K!jl4fCKg$7|I) zTtzH2Wvd1gDGSNMQa-XUKAwBs=&+mPn&RICJo*r2hqTDNqie@5m5Pd4U%Di3M ztDm;bp26hM52>ojM*1>!#Q3KeE z$u_`x!Dhg+W^ICHzy_eLW?d|ob<}x?`k=9+%~*5^xCq$&&zJ>!1WSNDn@b`+v60pi zqWRyjk&S|7z=pK=JKMDT+*NVDzRZGZA5C&P=aKcfdwOgoAq!8TO&24nWD-3O-)@I) oZyB3PS)jZJwTjrca3*Kx#V@Ophd-iNO!o}K155dVPZbaLAJo^7TL1t6 delta 540 zcmX@Ii=|~3ivR<|pN$NRvMdY?44gprg^dD@9E^gS4UfHN+-%Pz$uha$j(zfd6#+)I z$p@5`C(9Y}OqO96ooo-JC)jgLzQDuBs5#kjz4+uV6~W1F{34U@@`>|l15E<~MiAjS zS({&)QD^eQaAC$zlP`*DZ59`B5t$stFV3h6G81UBAiKikXWk+p(_#lC#WTw;SL!NVhjw85Y1q{ zT0pTAK(U<7{og0EZkGRhQ%H~*D9;YWA*sbBVTnZyOp_b`3JdT6MS}BlQ~gUaQj37X zKtYk|2lyCScpdZdL1O-SsSFIvKw;_aB?63Fn5Rz=VdRnE0BQ?L%}vcsN-Y9~2oyK~ N)yZs65Mg}60RVs^i*o<~ diff --git a/Assets/Fungus/VisualScripting/SetVariableCommand.cs b/Assets/Fungus/VisualScripting/SetVariableCommand.cs index e845cc49..3818d8fc 100644 --- a/Assets/Fungus/VisualScripting/SetVariableCommand.cs +++ b/Assets/Fungus/VisualScripting/SetVariableCommand.cs @@ -4,45 +4,30 @@ using Fungus; public class SetVariableCommand : FungusCommand { - public string variableKey; - - public string stringValue; - - public int integerValue; - - public bool booleanValue; - - public float floatValue; - + public Variable variable; + public override void OnEnter() { - if (variableKey.Length == 0) + if (variable.key.Length == 0) { Finish(); return; } - Variable v = parentSequenceController.GetVariable(variableKey); + Variable v = parentSequenceController.GetVariable(variable.key); if (v == null) { - Debug.LogError("Variable " + variableKey + " not defined."); + Debug.LogError("Variable " + variable.key + " not defined."); } else { - switch (v.type) + if (v.IsSameType(variable)) + { + v.Assign(variable); + } + else { - case VariableType.String: - v.stringValue = stringValue; - break; - case VariableType.Integer: - v.integerValue = integerValue; - break; - case VariableType.Float: - v.floatValue = floatValue; - break; - case VariableType.Boolean: - v.booleanValue = booleanValue; - break; + Debug.LogError("Variables " + variable.key + " and " + v.key + " are not of the same type."); } } diff --git a/Assets/Fungus/VisualScripting/Variable.cs b/Assets/Fungus/VisualScripting/Variable.cs index 059056fa..8f881c24 100644 --- a/Assets/Fungus/VisualScripting/Variable.cs +++ b/Assets/Fungus/VisualScripting/Variable.cs @@ -19,10 +19,58 @@ namespace Fungus public string key; public VariableType type; - public string stringValue; + public bool booleanValue; public int integerValue; public float floatValue; - public bool booleanValue; + public string stringValue; + + public bool IsSameType(Variable other) + { + return (this.type == other.type); + } + + public bool Assign(Variable other) + { + if (!IsSameType(other)) + { + return false; + } + + booleanValue = other.booleanValue; + integerValue = other.integerValue; + floatValue = other.floatValue; + stringValue = other.stringValue; + + return true; + } + } + + [Serializable] + public class VariableData + { + } + + [Serializable] + public class BooleanData : VariableData + { + public bool value; + } + + [Serializable] + public class IntegerData : VariableData + { + public int value; } + [Serializable] + public class FloatData : VariableData + { + public float value; + } + + [Serializable] + public class StringData : VariableData + { + public string value; + } } \ No newline at end of file