From 9f06860183710c2fdb88ed8a7ef332963ab41b12 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Mon, 11 Aug 2014 15:40:28 +0100 Subject: [PATCH] Variables are now displayed in Reorderable List --- .../Editor/FungusScript/FungusScriptEditor.cs | 24 +++-- .../FungusScript/FungusVariableEditor.cs | 96 +----------------- .../Editor/FungusScript/VariableList.cs | 69 +++++++++++-- .../Editor/FungusScript/VariablesWindow.cs | 2 +- Assets/Fungus/VisualScripting/FungusScript.cs | 2 +- .../Fungus/VisualScripting/FungusVariable.cs | 12 --- Assets/Shuttle/ShuttleGame.unity | Bin 82132 -> 81388 bytes 7 files changed, 80 insertions(+), 125 deletions(-) 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 66535a2c848d9e41d6696c6430e7c865a85f6855..ad18bb6c656899961a4da225b6359924d8c5eb2d 100644 GIT binary patch delta 1270 zcmZA0Pe@cj90%~<*lKIJxr>WzV5IH3ntz{vuq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