diff --git a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs index 9bac8a57..e60027b9 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs @@ -18,6 +18,11 @@ namespace Fungus.Script variablesProperty = serializedObject.FindProperty("variables"); } + public void OnInspectorUpdate() + { + Repaint(); + } + public override void OnInspectorGUI() { serializedObject.Update(); @@ -69,6 +74,7 @@ namespace Fungus.Script GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); + if (GUILayout.Button("Add Variable")) { GenericMenu menu = new GenericMenu (); @@ -93,9 +99,9 @@ namespace Fungus.Script return; } - Variable variable = new Variable(); + Undo.RecordObject(fungusScript, "Add Boolean"); + BooleanVariable variable = fungusScript.gameObject.AddComponent(); variable.key = MakeUniqueKey(fungusScript); - variable.type = VariableType.Boolean; fungusScript.variables.Add(variable); } @@ -107,9 +113,9 @@ namespace Fungus.Script return; } - Variable variable = new Variable(); + Undo.RecordObject(fungusScript, "Add Integer"); + IntegerVariable variable = fungusScript.gameObject.AddComponent(); variable.key = MakeUniqueKey(fungusScript); - variable.type = VariableType.Integer; fungusScript.variables.Add(variable); } @@ -121,9 +127,9 @@ namespace Fungus.Script return; } - Variable variable = new Variable(); + Undo.RecordObject(fungusScript, "Add Float"); + FloatVariable variable = fungusScript.gameObject.AddComponent(); variable.key = MakeUniqueKey(fungusScript); - variable.type = VariableType.Float; fungusScript.variables.Add(variable); } @@ -135,9 +141,9 @@ namespace Fungus.Script return; } - Variable variable = new Variable(); + Undo.RecordObject(fungusScript, "Add String"); + StringVariable variable = fungusScript.gameObject.AddComponent(); variable.key = MakeUniqueKey(fungusScript); - variable.type = VariableType.String; fungusScript.variables.Add(variable); } @@ -149,7 +155,7 @@ namespace Fungus.Script string key = "Var" + index; bool found = false; - foreach(Variable variable in fungusScript.variables) + foreach(FungusVariable variable in fungusScript.GetComponents()) { if (variable.key == key) { diff --git a/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs b/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs index c39a830a..d1194efb 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs @@ -8,105 +8,13 @@ using System.Linq; namespace Fungus.Script { - [CustomPropertyDrawer (typeof(Variable))] - public class VariableDrawer : PropertyDrawer - { - public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) - { - EditorGUI.BeginProperty(position, label, property); - - SerializedProperty keyProp = property.FindPropertyRelative("key"); - SerializedProperty typeProp = property.FindPropertyRelative("type"); - SerializedProperty scopeProp = property.FindPropertyRelative("scope"); - - // Draw the text field control GUI. - EditorGUI.BeginChangeCheck(); - - float width2 = 60; - float width3 = 50; - float width1 = position.width - width2 - width3; - - Rect keyRect = position; - keyRect.width = width1; - - Rect typeRect = position; - typeRect.x += width1; - typeRect.width = width2; - - Rect scopeRect = position; - scopeRect.x += width1 + width2; - scopeRect.width = width3; - - string keyValue = EditorGUI.TextField(keyRect, label, keyProp.stringValue); - - string typeLabel = ""; - switch ((VariableType)typeProp.enumValueIndex) - { - case VariableType.Boolean: - typeLabel = "Boolean"; - break; - case VariableType.Integer: - typeLabel = "Integer"; - break; - case VariableType.Float: - typeLabel = "Float"; - break; - case VariableType.String: - typeLabel = "String"; - break; - } - GUI.Label(typeRect, typeLabel); - - int scopeValue = (int)(VariableScope)EditorGUI.EnumPopup(scopeRect, (VariableScope)scopeProp.enumValueIndex); - - if (EditorGUI.EndChangeCheck ()) - { - char[] arr = keyValue.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray(); - - keyValue = new string(arr); - - keyProp.stringValue = keyValue; - scopeProp.enumValueIndex = scopeValue; - } - - EditorGUI.EndProperty(); - } - } - [CustomEditor (typeof(FungusVariable), true)] public class FungusVariableEditor : FungusCommandEditor { void OnEnable() - { - // Uncomment to hide variable components in inspector - //FungusVariable t = target as FungusVariable; - //t.hideFlags = HideFlags.HideInInspector; - } - - public override void OnInspectorGUI() { FungusVariable t = target as FungusVariable; - - EditorGUI.BeginChangeCheck(); - - string key = EditorGUILayout.TextField(new GUIContent("Key", "Name to use for this variable"), t.key); - VariableScope scope = (VariableScope)EditorGUILayout.EnumPopup(new GUIContent("Scope", "Local or global access to variable value"), t.scope); - - if (EditorGUI.EndChangeCheck()) - { - Undo.RecordObject(t, "Set Variable"); - t.key = key; - t.scope = scope; - } - - GUILayout.BeginHorizontal(); - GUILayout.FlexibleSpace(); - if (GUILayout.Button("Delete Variable")) - { - Undo.RecordObject(t, "Delete Variable"); - DestroyImmediate(t); - } - GUILayout.EndHorizontal(); + t.hideFlags = HideFlags.HideInInspector; } static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func filter = null) @@ -117,7 +25,7 @@ namespace Fungus.Script variableKeys.Add(""); variableObjects.Add(null); - FungusVariable[] variables = fungusScript.GetComponents(); + List variables = fungusScript.variables; int index = 0; int selectedIndex = 0; foreach (FungusVariable v in variables) diff --git a/Assets/Fungus/Editor/FungusScript/VariableList.cs b/Assets/Fungus/Editor/FungusScript/VariableList.cs index 970b39f8..8809a865 100644 --- a/Assets/Fungus/Editor/FungusScript/VariableList.cs +++ b/Assets/Fungus/Editor/FungusScript/VariableList.cs @@ -4,9 +4,8 @@ using UnityEngine; using UnityEditor; - using System; - +using System.Linq; using Rotorz.ReorderableList; namespace Fungus.Script @@ -14,7 +13,6 @@ namespace Fungus.Script public class VariableListAdaptor : IReorderableListAdaptor { - private SerializedProperty _arrayProperty; public float fixedItemHeight; @@ -41,8 +39,7 @@ namespace Fungus.Script } public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) - { - } + {} public int Count { @@ -77,11 +74,15 @@ namespace Fungus.Script _arrayProperty.InsertArrayElementAtIndex(index); } - public void Remove(int index) { + public void Remove(int index) + { + FungusVariable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as FungusVariable; + Undo.DestroyObjectImmediate(variable); + + _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null; _arrayProperty.DeleteArrayElementAtIndex(index); } - public void Move(int sourceIndex, int destIndex) { if (destIndex > sourceIndex) @@ -95,7 +96,59 @@ namespace Fungus.Script public virtual void DrawItem(Rect position, int index) { - EditorGUI.PropertyField(position, this[index], GUIContent.none, false); + FungusVariable variable = this[index].objectReferenceValue as FungusVariable; + + float width1 = 60; + float width3 = 50; + float width2 = position.width - width1 - width3; + + Rect typeRect = position; + typeRect.width = width1; + + Rect keyRect = position; + keyRect.x += width1; + keyRect.width = width2; + + Rect scopeRect = position; + scopeRect.x += width1 + width2; + scopeRect.width = width3; + + string type = ""; + if (variable.GetType() == typeof(BooleanVariable)) + { + type = "Boolean"; + } + else if (variable.GetType() == typeof(IntegerVariable)) + { + type = "Integer"; + } + else if (variable.GetType() == typeof(FloatVariable)) + { + type = "Float"; + } + else if (variable.GetType() == typeof(StringVariable)) + { + type = "String"; + } + + GUI.Label(typeRect, type); + + EditorGUI.BeginChangeCheck(); + + string key = EditorGUI.TextField(keyRect, variable.key); + + VariableScope scope = (VariableScope)EditorGUI.EnumPopup(scopeRect, variable.scope); + + if (EditorGUI.EndChangeCheck ()) + { + Undo.RecordObject(variable, "Set Variable"); + + char[] arr = key.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray(); + key = new string(arr); + + variable.key = key; + variable.scope = scope; + } } public virtual float GetItemHeight(int index) diff --git a/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs b/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs index 1ca3e099..589df5f6 100644 --- a/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs +++ b/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs @@ -72,7 +72,7 @@ namespace Fungus.Script boxStyle.margin.top = 0; boxStyle.margin.bottom = 0; - FungusVariable[] fsVariables = fungusScript.GetComponents(); + List fsVariables = fungusScript.variables; foreach (FungusVariable variable in fsVariables) { GUILayout.BeginHorizontal(boxStyle); diff --git a/Assets/Fungus/VisualScripting/FungusScript.cs b/Assets/Fungus/VisualScripting/FungusScript.cs index f52c858b..690beaeb 100644 --- a/Assets/Fungus/VisualScripting/FungusScript.cs +++ b/Assets/Fungus/VisualScripting/FungusScript.cs @@ -20,7 +20,7 @@ namespace Fungus.Script public bool startAutomatically = false; - public List variables = new List(); + public List variables = new List(); void Start() { diff --git a/Assets/Fungus/VisualScripting/FungusVariable.cs b/Assets/Fungus/VisualScripting/FungusVariable.cs index 82b982a5..b1989a82 100644 --- a/Assets/Fungus/VisualScripting/FungusVariable.cs +++ b/Assets/Fungus/VisualScripting/FungusVariable.cs @@ -18,18 +18,6 @@ namespace Fungus.Script Global } - [System.Serializable] - public class Variable - { - public string key; - public VariableType type; - public VariableScope scope; - public BooleanData booleanData; - public IntegerData integerData; - public FloatData floatData; - public StringData stringData; - } - public class FungusVariable : MonoBehaviour { public VariableScope scope; diff --git a/Assets/Shuttle/ShuttleGame.unity b/Assets/Shuttle/ShuttleGame.unity index 66535a2c..ad18bb6c 100644 Binary files a/Assets/Shuttle/ShuttleGame.unity and b/Assets/Shuttle/ShuttleGame.unity differ