From 239b9e7416068d9be23d5f2059231cacc70e11c5 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 8 Aug 2014 10:56:45 +0100 Subject: [PATCH] Pick variable or constant value --- .../Editor/FungusScript/AddOptionEditor.cs | 8 +- .../FungusScript/FungusVariableEditor.cs | 38 ++++++++ .../Fungus/Editor/FungusScript/JumpEditor.cs | 94 +++++++++++-------- .../Fungus/Editor/FungusScript/SayEditor.cs | 8 +- .../Editor/FungusScript/SequenceEditor.cs | 38 -------- .../Fungus/Editor/FungusScript/SetEditor.cs | 85 +++++++++-------- Assets/Fungus/VisualScripting/Jump.cs | 54 +++++++---- Assets/Fungus/VisualScripting/Set.cs | 54 ++++++----- Assets/Fungus/VisualScripting/Variable.cs | 35 ------- .../Fungus/VisualScripting/Variable.cs.meta | 8 -- 10 files changed, 210 insertions(+), 212 deletions(-) delete mode 100644 Assets/Fungus/VisualScripting/Variable.cs delete mode 100644 Assets/Fungus/VisualScripting/Variable.cs.meta diff --git a/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs b/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs index 3a4fb362..d187f2f6 100644 --- a/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs @@ -25,10 +25,10 @@ namespace Fungus.Script if (showCondition == AddOption.ShowCondition.BooleanIsFalse || showCondition == AddOption.ShowCondition.BooleanIsTrue) { - booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), - t.GetFungusScript (), - t.booleanVariable, - v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; + booleanVariable = FungusVariableEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), + t.GetFungusScript (), + t.booleanVariable, + v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; } if (EditorGUI.EndChangeCheck()) diff --git a/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs b/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs index d1c99a7f..2ba74e49 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs @@ -1,6 +1,7 @@ using UnityEditor; using UnityEditorInternal; using UnityEngine; +using System; using System.Collections; using System.Collections.Generic; @@ -26,6 +27,43 @@ namespace Fungus.Script t.scope = scope; } } + + static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func filter = null) + { + List variableKeys = new List(); + List variableObjects = new List(); + + variableKeys.Add(""); + variableObjects.Add(null); + + FungusVariable[] variables = fungusScript.GetComponents(); + int index = 0; + int selectedIndex = 0; + foreach (FungusVariable v in variables) + { + if (filter != null) + { + if (!filter(v)) + { + continue; + } + } + + variableKeys.Add(v.key); + variableObjects.Add(v); + + index++; + + if (v == variable) + { + selectedIndex = index; + } + } + + selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray()); + + return variableObjects[selectedIndex]; + } } } \ No newline at end of file diff --git a/Assets/Fungus/Editor/FungusScript/JumpEditor.cs b/Assets/Fungus/Editor/FungusScript/JumpEditor.cs index 70cee6c2..c2928e6b 100644 --- a/Assets/Fungus/Editor/FungusScript/JumpEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/JumpEditor.cs @@ -37,10 +37,10 @@ namespace Fungus.Script return; } - FungusVariable fungusVariable = SequenceEditor.VariableField(new GUIContent("Compare Variable", "Variable to use in compare operation"), - t.GetFungusScript(), - t.variable, - null); + FungusVariable fungusVariable = FungusVariableEditor.VariableField(new GUIContent("Compare Variable", "Variable to use in compare operation"), + t.GetFungusScript(), + t.variable, + null); if (fungusVariable != t.variable) { @@ -75,47 +75,61 @@ namespace Fungus.Script t.compareOperator = compareOperator; } - bool booleanValue = t.booleanData.value; - int integerValue = t.integerData.value; - float floatValue = t.floatData.value; - string stringValue = t.stringData.value; + FungusVariable compareVariable = FungusVariableEditor.VariableField(new GUIContent("Compare Variable", "Variable to compare with"), + t.GetFungusScript(), + t.compareVariable, + v => v.GetType() == fungusVariable.GetType()); - if (t.variable.GetType() == typeof(BooleanVariable)) + if (compareVariable != t.compareVariable) { - booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); - } - else if (t.variable.GetType() == typeof(IntegerVariable)) - { - integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); - } - else if (t.variable.GetType() == typeof(FloatVariable)) - { - floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); - } - else if (t.variable.GetType() == typeof(StringVariable)) - { - stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); + Undo.RecordObject(t, "Select Compare Variable"); + t.compareVariable = compareVariable; } - if (booleanValue != t.booleanData.value) - { - Undo.RecordObject(t, "Set boolean value"); - t.booleanData.value = booleanValue; - } - else if (integerValue != t.integerData.value) + if (compareVariable == null) { - Undo.RecordObject(t, "Set integer value"); - t.integerData.value = integerValue; - } - else if (floatValue != t.floatData.value) - { - Undo.RecordObject(t, "Set float value"); - t.floatData.value = floatValue; - } - else if (stringValue != t.stringData.value) - { - Undo.RecordObject(t, "Set string value"); - t.stringData.value = stringValue; + bool booleanValue = t.booleanValue; + int integerValue = t.integerValue; + float floatValue = t.floatValue; + string stringValue = t.stringValue; + + if (t.variable.GetType() == typeof(BooleanVariable)) + { + booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); + } + else if (t.variable.GetType() == typeof(IntegerVariable)) + { + integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); + } + else if (t.variable.GetType() == typeof(FloatVariable)) + { + floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); + } + else if (t.variable.GetType() == typeof(StringVariable)) + { + stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); + } + + if (booleanValue != t.booleanValue) + { + Undo.RecordObject(t, "Set boolean value"); + t.booleanValue = booleanValue; + } + else if (integerValue != t.integerValue) + { + Undo.RecordObject(t, "Set integer value"); + t.integerValue = integerValue; + } + else if (floatValue != t.floatValue) + { + Undo.RecordObject(t, "Set float value"); + t.floatValue = floatValue; + } + else if (stringValue != t.stringValue) + { + Undo.RecordObject(t, "Set string value"); + t.stringValue = stringValue; + } } Sequence onTrue = SequenceEditor.SequenceField(new GUIContent("On True Sequence", "Sequence to execute if comparision is true"), diff --git a/Assets/Fungus/Editor/FungusScript/SayEditor.cs b/Assets/Fungus/Editor/FungusScript/SayEditor.cs index 408b8def..71cb5290 100644 --- a/Assets/Fungus/Editor/FungusScript/SayEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/SayEditor.cs @@ -29,10 +29,10 @@ namespace Fungus.Script if (showCondition == Say.ShowCondition.BooleanIsFalse || showCondition == Say.ShowCondition.BooleanIsTrue) { - booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), - t.GetFungusScript (), - t.booleanVariable, - v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; + booleanVariable = FungusVariableEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), + t.GetFungusScript (), + t.booleanVariable, + v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; } if (EditorGUI.EndChangeCheck()) diff --git a/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs b/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs index 8cb74e4e..ba2918b9 100644 --- a/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs @@ -3,7 +3,6 @@ using UnityEngine; using System; using System.Collections; using System.Collections.Generic; -using System.Linq; namespace Fungus.Script { @@ -48,43 +47,6 @@ namespace Fungus.Script return result; } - - static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func filter = null) - { - List variableKeys = new List(); - List variableObjects = new List(); - - variableKeys.Add(""); - variableObjects.Add(null); - - FungusVariable[] variables = fungusScript.GetComponents(); - int index = 0; - int selectedIndex = 0; - foreach (FungusVariable v in variables) - { - if (filter != null) - { - if (!filter(v)) - { - continue; - } - } - - variableKeys.Add(v.key); - variableObjects.Add(v); - - index++; - - if (v == variable) - { - selectedIndex = index; - } - } - - selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray()); - - return variableObjects[selectedIndex]; - } } } \ No newline at end of file diff --git a/Assets/Fungus/Editor/FungusScript/SetEditor.cs b/Assets/Fungus/Editor/FungusScript/SetEditor.cs index a6529b88..801d06a6 100644 --- a/Assets/Fungus/Editor/FungusScript/SetEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/SetEditor.cs @@ -19,9 +19,9 @@ namespace Fungus.Script return; } - FungusVariable variable = SequenceEditor.VariableField(new GUIContent("Variable", "Variable to set"), - fungusScript, - t.variable); + FungusVariable variable = FungusVariableEditor.VariableField(new GUIContent("Variable", "Variable to set"), + fungusScript, + t.variable); if (variable != t.variable) { @@ -119,48 +119,53 @@ namespace Fungus.Script Undo.RecordObject(t, "Set Operator"); t.setOperator = setOperator; } - - bool booleanValue = t.booleanData.value; - int integerValue = t.integerData.value; - float floatValue = t.floatData.value; - string stringValue = t.stringData.value; - if (variable.GetType() == typeof(BooleanVariable)) - { - booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); - } - else if (variable.GetType() == typeof(IntegerVariable)) - { - integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); - } - else if (variable.GetType() == typeof(FloatVariable)) - { - floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); - } - else if (variable.GetType() == typeof(StringVariable)) - { - stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); - } + FungusVariable setVariable = FungusVariableEditor.VariableField(new GUIContent("Other Variable", "The variable to read the assigned value from."), + t.GetFungusScript(), + t.setVariable, + v => v.GetType() == variable.GetType ()); - if (booleanValue != t.booleanData.value) - { - Undo.RecordObject(t, "Set boolean value"); - t.booleanData.value = booleanValue; - } - else if (integerValue != t.integerData.value) + if (setVariable != t.setVariable) { - Undo.RecordObject(t, "Set integer value"); - t.integerData.value = integerValue; + Undo.RecordObject(t, "Select Set Variable"); + t.setVariable = setVariable; } - else if (floatValue != t.floatData.value) - { - Undo.RecordObject(t, "Set float value"); - t.floatData.value = floatValue; - } - else if (stringValue != t.stringData.value) + + if (setVariable == null) { - Undo.RecordObject(t, "Set string value"); - t.stringData.value = stringValue; + EditorGUI.BeginChangeCheck(); + + bool booleanValue = t.booleanValue; + int integerValue = t.integerValue; + float floatValue = t.floatValue; + string stringValue = t.stringValue; + + if (variable.GetType() == typeof(BooleanVariable)) + { + booleanValue = EditorGUILayout.Toggle (new GUIContent("Boolean Value", "A constant boolean value to set the variable with"), booleanValue); + } + else if (variable.GetType() == typeof(IntegerVariable)) + { + integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "A constant integer value to set the variable with"), integerValue); + } + else if (variable.GetType() == typeof(FloatVariable)) + { + floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "A constant float value to set the variable with"), floatValue); + } + else if (variable.GetType() == typeof(StringVariable)) + { + stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "A constant string value to set the variable with"), stringValue); + } + + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(t, "Set Variable Data"); + + t.booleanValue = booleanValue; + t.integerValue = integerValue; + t.floatValue = floatValue; + t.stringValue = stringValue; + } } } } diff --git a/Assets/Fungus/VisualScripting/Jump.cs b/Assets/Fungus/VisualScripting/Jump.cs index f6ae3b85..3164b2df 100644 --- a/Assets/Fungus/VisualScripting/Jump.cs +++ b/Assets/Fungus/VisualScripting/Jump.cs @@ -30,13 +30,15 @@ namespace Fungus.Script public CompareOperator compareOperator; - public BooleanData booleanData; + public FungusVariable compareVariable; - public IntegerData integerData; + public bool booleanValue; - public FloatData floatData; + public int integerValue; - public StringData stringData; + public float floatValue; + + public string stringValue; public Sequence onTrueSequence; @@ -61,75 +63,87 @@ namespace Fungus.Script if (variable.GetType() == typeof(BooleanVariable)) { + bool lhs = (variable as BooleanVariable).Value; + bool rhs = (compareVariable as BooleanVariable) == null ? booleanValue : (compareVariable as BooleanVariable).Value; + switch (compareOperator) { case CompareOperator.Equals: - condition = (variable as BooleanVariable).Value == booleanData.value; + condition = lhs == rhs; break; case CompareOperator.NotEquals: default: - condition = (variable as BooleanVariable).Value != booleanData.value; + condition = lhs != rhs; break; } } else if (variable.GetType() == typeof(IntegerVariable)) { + int lhs = (variable as IntegerVariable).Value; + int rhs = (compareVariable as IntegerVariable) == null ? integerValue : (compareVariable as IntegerVariable).Value; + switch (compareOperator) { case CompareOperator.Equals: - condition = (variable as IntegerVariable).Value == integerData.value; + condition = lhs == rhs; break; case CompareOperator.NotEquals: - condition = (variable as IntegerVariable).Value != integerData.value; + condition = lhs != rhs; break; case CompareOperator.LessThan: - condition = (variable as IntegerVariable).Value < integerData.value; + condition = lhs < rhs; break; case CompareOperator.GreaterThan: - condition = (variable as IntegerVariable).Value > integerData.value; + condition = lhs > rhs; break; case CompareOperator.LessThanOrEquals: - condition = (variable as IntegerVariable).Value <= integerData.value; + condition = lhs <= rhs; break; case CompareOperator.GreaterThanOrEquals: - condition = (variable as IntegerVariable).Value >= integerData.value; + condition = lhs >= rhs; break; } } else if (variable.GetType() == typeof(FloatVariable)) { + float lhs = (variable as FloatVariable).Value; + float rhs = (compareVariable as FloatVariable) == null ? floatValue : (compareVariable as FloatVariable).Value; + switch (compareOperator) { case CompareOperator.Equals: - condition = (variable as FloatVariable).Value == floatData.value; + condition = lhs == rhs; break; case CompareOperator.NotEquals: - condition = (variable as FloatVariable).Value != floatData.value; + condition = lhs != rhs; break; case CompareOperator.LessThan: - condition = (variable as FloatVariable).Value < floatData.value; + condition = lhs < rhs; break; case CompareOperator.GreaterThan: - condition = (variable as FloatVariable).Value > floatData.value; + condition = lhs > rhs; break; case CompareOperator.LessThanOrEquals: - condition = (variable as FloatVariable).Value <= floatData.value; + condition = lhs <= rhs; break; case CompareOperator.GreaterThanOrEquals: - condition = (variable as FloatVariable).Value >= floatData.value; + condition = lhs >= rhs; break; } } else if (variable.GetType() == typeof(StringVariable)) { + string lhs = (variable as StringVariable).Value; + string rhs = (compareVariable as StringVariable) == null ? stringValue : (compareVariable as StringVariable).Value; + switch (compareOperator) { case CompareOperator.Equals: - condition = (variable as StringVariable).Value == stringData.value; + condition = lhs == rhs; break; case CompareOperator.NotEquals: default: - condition = (variable as StringVariable).Value != stringData.value; + condition = lhs != rhs; break; } } diff --git a/Assets/Fungus/VisualScripting/Set.cs b/Assets/Fungus/VisualScripting/Set.cs index 8f0f90ae..cf308c1a 100644 --- a/Assets/Fungus/VisualScripting/Set.cs +++ b/Assets/Fungus/VisualScripting/Set.cs @@ -20,13 +20,15 @@ namespace Fungus.Script public SetOperator setOperator; - public BooleanData booleanData; + public FungusVariable setVariable; - public IntegerData integerData; + public bool booleanValue; - public FloatData floatData; + public int integerValue; - public StringData stringData; + public float floatValue; + + public string stringValue; public override void OnEnter() { @@ -38,74 +40,80 @@ namespace Fungus.Script if (variable.GetType() == typeof(BooleanVariable)) { + BooleanVariable lhs = (variable as BooleanVariable); + bool rhs = (setVariable as BooleanVariable) == null ? booleanValue : (setVariable as BooleanVariable).Value; + switch (setOperator) { default: case SetOperator.Assign: - (variable as BooleanVariable).Value = booleanData.value; + lhs.Value = rhs; break; case SetOperator.Negate: - (variable as BooleanVariable).Value = !booleanData.value; + lhs.Value = !rhs; break; } } else if (variable.GetType() == typeof(IntegerVariable)) { + IntegerVariable lhs = (variable as IntegerVariable); + int rhs = (setVariable as IntegerVariable) == null ? integerValue : (setVariable as IntegerVariable).Value; + switch (setOperator) { default: case SetOperator.Assign: - (variable as IntegerVariable).Value = integerData.value; - break; - case SetOperator.Negate: - (variable as IntegerVariable).Value = -integerData.value; + lhs.Value = rhs; break; case SetOperator.Add: - (variable as IntegerVariable).Value += integerData.value; + lhs.Value += rhs; break; case SetOperator.Subtract: - (variable as IntegerVariable).Value -= integerData.value; + lhs.Value -= rhs; break; case SetOperator.Multiply: - (variable as IntegerVariable).Value *= integerData.value; + lhs.Value *= rhs; break; case SetOperator.Divide: - (variable as IntegerVariable).Value /= integerData.value; + lhs.Value /= rhs; break; } } else if (variable.GetType() == typeof(FloatVariable)) { + FloatVariable lhs = (variable as FloatVariable); + float rhs = (setVariable as FloatVariable) == null ? floatValue : (setVariable as FloatVariable).Value; + switch (setOperator) { default: case SetOperator.Assign: - (variable as FloatVariable).Value = floatData.value; - break; - case SetOperator.Negate: - (variable as FloatVariable).Value = -floatData.value; + lhs.Value = rhs; break; case SetOperator.Add: - (variable as FloatVariable).Value += floatData.value; + lhs.Value += rhs; break; case SetOperator.Subtract: - (variable as FloatVariable).Value -= floatData.value; + lhs.Value -= rhs; break; case SetOperator.Multiply: - (variable as FloatVariable).Value *= floatData.value; + lhs.Value *= rhs; break; case SetOperator.Divide: - (variable as FloatVariable).Value /= floatData.value; + lhs.Value /= rhs; break; } } else if (variable.GetType() == typeof(StringVariable)) { + StringVariable lhs = (variable as StringVariable); + string rhs = (setVariable as StringVariable) == null ? stringValue : (setVariable as StringVariable).Value; + switch (setOperator) { default: case SetOperator.Assign: - (variable as StringVariable).Value = stringData.value; + lhs.Value = rhs; break; } } diff --git a/Assets/Fungus/VisualScripting/Variable.cs b/Assets/Fungus/VisualScripting/Variable.cs deleted file mode 100644 index 40abf58b..00000000 --- a/Assets/Fungus/VisualScripting/Variable.cs +++ /dev/null @@ -1,35 +0,0 @@ -using UnityEngine; -using System; -using System.Collections; - -namespace Fungus.Script -{ - [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 diff --git a/Assets/Fungus/VisualScripting/Variable.cs.meta b/Assets/Fungus/VisualScripting/Variable.cs.meta deleted file mode 100644 index c02059a0..00000000 --- a/Assets/Fungus/VisualScripting/Variable.cs.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 2f3853d313cd94fe184e3478cefc11f2 -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: