Browse Source

Variables are now displayed in Reorderable List

master
chrisgregan 11 years ago
parent
commit
9f06860183
  1. 24
      Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs
  2. 96
      Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs
  3. 69
      Assets/Fungus/Editor/FungusScript/VariableList.cs
  4. 2
      Assets/Fungus/Editor/FungusScript/VariablesWindow.cs
  5. 2
      Assets/Fungus/VisualScripting/FungusScript.cs
  6. 12
      Assets/Fungus/VisualScripting/FungusVariable.cs
  7. BIN
      Assets/Shuttle/ShuttleGame.unity

24
Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs

@ -18,6 +18,11 @@ namespace Fungus.Script
variablesProperty = serializedObject.FindProperty("variables"); variablesProperty = serializedObject.FindProperty("variables");
} }
public void OnInspectorUpdate()
{
Repaint();
}
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
serializedObject.Update(); serializedObject.Update();
@ -69,6 +74,7 @@ namespace Fungus.Script
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
if (GUILayout.Button("Add Variable")) if (GUILayout.Button("Add Variable"))
{ {
GenericMenu menu = new GenericMenu (); GenericMenu menu = new GenericMenu ();
@ -93,9 +99,9 @@ namespace Fungus.Script
return; return;
} }
Variable variable = new Variable(); Undo.RecordObject(fungusScript, "Add Boolean");
BooleanVariable variable = fungusScript.gameObject.AddComponent<BooleanVariable>();
variable.key = MakeUniqueKey(fungusScript); variable.key = MakeUniqueKey(fungusScript);
variable.type = VariableType.Boolean;
fungusScript.variables.Add(variable); fungusScript.variables.Add(variable);
} }
@ -107,9 +113,9 @@ namespace Fungus.Script
return; return;
} }
Variable variable = new Variable(); Undo.RecordObject(fungusScript, "Add Integer");
IntegerVariable variable = fungusScript.gameObject.AddComponent<IntegerVariable>();
variable.key = MakeUniqueKey(fungusScript); variable.key = MakeUniqueKey(fungusScript);
variable.type = VariableType.Integer;
fungusScript.variables.Add(variable); fungusScript.variables.Add(variable);
} }
@ -121,9 +127,9 @@ namespace Fungus.Script
return; return;
} }
Variable variable = new Variable(); Undo.RecordObject(fungusScript, "Add Float");
FloatVariable variable = fungusScript.gameObject.AddComponent<FloatVariable>();
variable.key = MakeUniqueKey(fungusScript); variable.key = MakeUniqueKey(fungusScript);
variable.type = VariableType.Float;
fungusScript.variables.Add(variable); fungusScript.variables.Add(variable);
} }
@ -135,9 +141,9 @@ namespace Fungus.Script
return; return;
} }
Variable variable = new Variable(); Undo.RecordObject(fungusScript, "Add String");
StringVariable variable = fungusScript.gameObject.AddComponent<StringVariable>();
variable.key = MakeUniqueKey(fungusScript); variable.key = MakeUniqueKey(fungusScript);
variable.type = VariableType.String;
fungusScript.variables.Add(variable); fungusScript.variables.Add(variable);
} }
@ -149,7 +155,7 @@ namespace Fungus.Script
string key = "Var" + index; string key = "Var" + index;
bool found = false; bool found = false;
foreach(Variable variable in fungusScript.variables) foreach(FungusVariable variable in fungusScript.GetComponents<FungusVariable>())
{ {
if (variable.key == key) if (variable.key == key)
{ {

96
Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs

@ -8,105 +8,13 @@ using System.Linq;
namespace Fungus.Script 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)] [CustomEditor (typeof(FungusVariable), true)]
public class FungusVariableEditor : FungusCommandEditor public class FungusVariableEditor : FungusCommandEditor
{ {
void OnEnable() 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; FungusVariable t = target as FungusVariable;
t.hideFlags = HideFlags.HideInInspector;
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();
} }
static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func<FungusVariable, bool> filter = null) static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func<FungusVariable, bool> filter = null)
@ -117,7 +25,7 @@ namespace Fungus.Script
variableKeys.Add("<None>"); variableKeys.Add("<None>");
variableObjects.Add(null); variableObjects.Add(null);
FungusVariable[] variables = fungusScript.GetComponents<FungusVariable>(); List<FungusVariable> variables = fungusScript.variables;
int index = 0; int index = 0;
int selectedIndex = 0; int selectedIndex = 0;
foreach (FungusVariable v in variables) foreach (FungusVariable v in variables)

69
Assets/Fungus/Editor/FungusScript/VariableList.cs

@ -4,9 +4,8 @@
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System; using System;
using System.Linq;
using Rotorz.ReorderableList; using Rotorz.ReorderableList;
namespace Fungus.Script namespace Fungus.Script
@ -14,7 +13,6 @@ namespace Fungus.Script
public class VariableListAdaptor : IReorderableListAdaptor public class VariableListAdaptor : IReorderableListAdaptor
{ {
private SerializedProperty _arrayProperty; private SerializedProperty _arrayProperty;
public float fixedItemHeight; public float fixedItemHeight;
@ -41,8 +39,7 @@ namespace Fungus.Script
} }
public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f)
{ {}
}
public int Count public int Count
{ {
@ -77,11 +74,15 @@ namespace Fungus.Script
_arrayProperty.InsertArrayElementAtIndex(index); _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); _arrayProperty.DeleteArrayElementAtIndex(index);
} }
public void Move(int sourceIndex, int destIndex) public void Move(int sourceIndex, int destIndex)
{ {
if (destIndex > sourceIndex) if (destIndex > sourceIndex)
@ -95,7 +96,59 @@ namespace Fungus.Script
public virtual void DrawItem(Rect position, int index) 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) public virtual float GetItemHeight(int index)

2
Assets/Fungus/Editor/FungusScript/VariablesWindow.cs

@ -72,7 +72,7 @@ namespace Fungus.Script
boxStyle.margin.top = 0; boxStyle.margin.top = 0;
boxStyle.margin.bottom = 0; boxStyle.margin.bottom = 0;
FungusVariable[] fsVariables = fungusScript.GetComponents<FungusVariable>(); List<FungusVariable> fsVariables = fungusScript.variables;
foreach (FungusVariable variable in fsVariables) foreach (FungusVariable variable in fsVariables)
{ {
GUILayout.BeginHorizontal(boxStyle); GUILayout.BeginHorizontal(boxStyle);

2
Assets/Fungus/VisualScripting/FungusScript.cs

@ -20,7 +20,7 @@ namespace Fungus.Script
public bool startAutomatically = false; public bool startAutomatically = false;
public List<Variable> variables = new List<Variable>(); public List<FungusVariable> variables = new List<FungusVariable>();
void Start() void Start()
{ {

12
Assets/Fungus/VisualScripting/FungusVariable.cs

@ -18,18 +18,6 @@ namespace Fungus.Script
Global 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 class FungusVariable : MonoBehaviour
{ {
public VariableScope scope; public VariableScope scope;

BIN
Assets/Shuttle/ShuttleGame.unity

Binary file not shown.
Loading…
Cancel
Save