Browse Source

Fixed delete variable errors

master
chrisgregan 11 years ago
parent
commit
6115eaa8c7
  1. 94
      Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs
  2. 145
      Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs

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

@ -12,7 +12,7 @@ namespace Fungus.Script
public class FungusScriptEditor : Editor public class FungusScriptEditor : Editor
{ {
SerializedProperty variablesProperty; SerializedProperty variablesProperty;
void OnEnable() void OnEnable()
{ {
if (serializedObject != null) if (serializedObject != null)
@ -20,18 +20,18 @@ namespace Fungus.Script
variablesProperty = serializedObject.FindProperty("variables"); variablesProperty = serializedObject.FindProperty("variables");
} }
} }
public void OnInspectorUpdate() public void OnInspectorUpdate()
{ {
Repaint(); Repaint();
} }
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
serializedObject.Update(); serializedObject.Update();
FungusScript t = target as FungusScript; FungusScript t = target as FungusScript;
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
if (GUILayout.Button("Open Fungus Editor")) if (GUILayout.Button("Open Fungus Editor"))
@ -50,33 +50,33 @@ namespace Fungus.Script
Undo.RegisterCreatedObjectUndo(go, "Sequence"); Undo.RegisterCreatedObjectUndo(go, "Sequence");
Selection.activeGameObject = go; Selection.activeGameObject = go;
} }
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
GUIContent stepTimeLabel = new GUIContent("Step Time", "Minimum time to execute each step"); GUIContent stepTimeLabel = new GUIContent("Step Time", "Minimum time to execute each step");
t.stepTime = EditorGUILayout.FloatField(stepTimeLabel, t.stepTime); t.stepTime = EditorGUILayout.FloatField(stepTimeLabel, t.stepTime);
GUIContent startSequenceLabel = new GUIContent("Start Sequence", "Sequence to be executed when controller starts."); GUIContent startSequenceLabel = new GUIContent("Start Sequence", "Sequence to be executed when controller starts.");
t.startSequence = SequenceEditor.SequenceField(startSequenceLabel, t, t.startSequence); t.startSequence = SequenceEditor.SequenceField(startSequenceLabel, t, t.startSequence);
GUIContent startAutomaticallyLabel = new GUIContent("Start Automatically", "Start this Fungus Script when the scene starts."); GUIContent startAutomaticallyLabel = new GUIContent("Start Automatically", "Start this Fungus Script when the scene starts.");
t.startAutomatically = EditorGUILayout.Toggle(startAutomaticallyLabel, t.startAutomatically); t.startAutomatically = EditorGUILayout.Toggle(startAutomaticallyLabel, t.startAutomatically);
if (t.startSequence == null) if (t.startSequence == null)
{ {
GUIStyle style = new GUIStyle(GUI.skin.label); GUIStyle style = new GUIStyle(GUI.skin.label);
style.normal.textColor = new Color(1,0,0); style.normal.textColor = new Color(1,0,0);
EditorGUILayout.LabelField(new GUIContent("Error: Please select a Start Sequence"), style); EditorGUILayout.LabelField(new GUIContent("Error: Please select a Start Sequence"), style);
} }
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }
public void DrawVariablesGUI() public void DrawVariablesGUI()
{ {
serializedObject.Update(); serializedObject.Update();
FungusScript t = target as FungusScript; FungusScript t = target as FungusScript;
ReorderableListGUI.Title("Variables"); ReorderableListGUI.Title("Variables");
@ -86,66 +86,24 @@ namespace Fungus.Script
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
if (!Application.isPlaying && GUILayout.Button("Add Variable")) if (!Application.isPlaying && GUILayout.Button("Add Variable"))
{ {
GenericMenu menu = new GenericMenu (); GenericMenu menu = new GenericMenu ();
menu.AddItem(new GUIContent ("Boolean"), false, AddBooleanVariable, t); menu.AddItem(new GUIContent ("Boolean"), false, AddVariable<BooleanVariable>, t);
menu.AddItem (new GUIContent ("Integer"), false, AddIntegerVariable, t); menu.AddItem (new GUIContent ("Integer"), false, AddVariable<IntegerVariable>, t);
menu.AddItem (new GUIContent ("Float"), false, AddFloatVariable, t); menu.AddItem (new GUIContent ("Float"), false, AddVariable<FloatVariable>, t);
menu.AddItem (new GUIContent ("String"), false, AddStringVariable, t); menu.AddItem (new GUIContent ("String"), false, AddVariable<StringVariable>, t);
menu.ShowAsContext (); menu.ShowAsContext ();
} }
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
serializedObject.ApplyModifiedProperties();
}
void AddBooleanVariable(object obj)
{
FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null)
{
return;
}
Undo.RecordObject(fungusScript, "Add Boolean");
BooleanVariable variable = fungusScript.gameObject.AddComponent<BooleanVariable>();
variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable);
}
void AddIntegerVariable(object obj)
{
FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null)
{
return;
}
Undo.RecordObject(fungusScript, "Add Integer"); serializedObject.ApplyModifiedProperties();
IntegerVariable variable = fungusScript.gameObject.AddComponent<IntegerVariable>();
variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable);
}
void AddFloatVariable(object obj)
{
FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null)
{
return;
}
Undo.RecordObject(fungusScript, "Add Float");
FloatVariable variable = fungusScript.gameObject.AddComponent<FloatVariable>();
variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable);
} }
void AddStringVariable(object obj) void AddVariable<T>(object obj) where T : FungusVariable
{ {
FungusScript fungusScript = obj as FungusScript; FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null) if (fungusScript == null)
@ -153,8 +111,8 @@ namespace Fungus.Script
return; return;
} }
Undo.RecordObject(fungusScript, "Add String"); Undo.RecordObject(fungusScript, "Add Variable");
StringVariable variable = fungusScript.gameObject.AddComponent<StringVariable>(); T variable = fungusScript.gameObject.AddComponent<T>();
variable.key = MakeUniqueKey(fungusScript); variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable); fungusScript.variables.Add(variable);
} }
@ -165,7 +123,7 @@ namespace Fungus.Script
while (true) while (true)
{ {
string key = "Var" + index; string key = "Var" + index;
bool found = false; bool found = false;
foreach(FungusVariable variable in fungusScript.GetComponents<FungusVariable>()) foreach(FungusVariable variable in fungusScript.GetComponents<FungusVariable>())
{ {
@ -175,7 +133,7 @@ namespace Fungus.Script
index++; index++;
} }
} }
if (!found) if (!found)
{ {
return key; return key;
@ -183,5 +141,5 @@ namespace Fungus.Script
} }
} }
} }
} }

145
Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs

@ -10,19 +10,83 @@ using Rotorz.ReorderableList;
namespace Fungus.Script namespace Fungus.Script
{ {
public class FungusVariableListAdaptor : IReorderableListAdaptor {
public class FungusVariableListAdaptor : SerializedPropertyAdaptor
{
public FungusVariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight) : base(arrayProperty, fixedItemHeight)
{}
public FungusVariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) private SerializedProperty _arrayProperty;
{}
public float fixedItemHeight;
public SerializedProperty this[int index] {
get { return _arrayProperty.GetArrayElementAtIndex(index); }
}
public SerializedProperty arrayProperty {
get { return _arrayProperty; }
}
public FungusVariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight) {
if (arrayProperty == null)
throw new ArgumentNullException("Array property was null.");
if (!arrayProperty.isArray)
throw new InvalidOperationException("Specified serialized propery is not an array.");
this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight;
}
public FungusVariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) {
}
public int Count {
get { return _arrayProperty.arraySize; }
}
public virtual bool CanDrag(int index) {
return true;
}
public virtual bool CanRemove(int index) {
return true;
}
public void Add() {
int newIndex = _arrayProperty.arraySize;
++_arrayProperty.arraySize;
ResetValue(_arrayProperty.GetArrayElementAtIndex(newIndex));
}
public void Insert(int index) {
_arrayProperty.InsertArrayElementAtIndex(index);
ResetValue(_arrayProperty.GetArrayElementAtIndex(index));
}
public void Duplicate(int index) {
_arrayProperty.InsertArrayElementAtIndex(index);
}
public void Remove(int index) {
// Remove the Fungus Variable component
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)
--destIndex;
_arrayProperty.MoveArrayElement(sourceIndex, destIndex);
}
public override void DrawItem(Rect position, int index) public void Clear() {
_arrayProperty.ClearArray();
}
public void DrawItem(Rect position, int index)
{ {
FungusVariable variable = this[index].objectReferenceValue as FungusVariable; FungusVariable variable = this[index].objectReferenceValue as FungusVariable;
float width1 = 60; float width1 = 60;
float width3 = 50; float width3 = 50;
float width2 = Mathf.Max(position.width - width1 - width3, 100); float width2 = Mathf.Max(position.width - width1 - width3, 100);
@ -37,7 +101,7 @@ namespace Fungus.Script
Rect scopeRect = position; Rect scopeRect = position;
scopeRect.x += width1 + width2; scopeRect.x += width1 + width2;
scopeRect.width = width3; scopeRect.width = width3;
string type = ""; string type = "";
if (variable.GetType() == typeof(BooleanVariable)) if (variable.GetType() == typeof(BooleanVariable))
{ {
@ -55,13 +119,13 @@ namespace Fungus.Script
{ {
type = "String"; type = "String";
} }
GUI.Label(typeRect, type); GUI.Label(typeRect, type);
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
string key = variable.key; string key = variable.key;
if (Application.isPlaying) if (Application.isPlaying)
{ {
const float w = 100; const float w = 100;
@ -96,21 +160,64 @@ namespace Fungus.Script
keyRect.width -= 5; keyRect.width -= 5;
key = EditorGUI.TextField(keyRect, variable.key); key = EditorGUI.TextField(keyRect, variable.key);
} }
VariableScope scope = (VariableScope)EditorGUI.EnumPopup(scopeRect, variable.scope); VariableScope scope = (VariableScope)EditorGUI.EnumPopup(scopeRect, variable.scope);
if (EditorGUI.EndChangeCheck ()) if (EditorGUI.EndChangeCheck ())
{ {
Undo.RecordObject(variable, "Set Variable"); Undo.RecordObject(variable, "Set Variable");
char[] arr = key.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray(); char[] arr = key.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray();
key = new string(arr); key = new string(arr);
variable.key = key; variable.key = key;
variable.scope = scope; variable.scope = scope;
} }
} }
public virtual float GetItemHeight(int index) {
return fixedItemHeight != 0f
? fixedItemHeight
: EditorGUI.GetPropertyHeight(this[index], GUIContent.none, false)
;
}
private void ResetValue(SerializedProperty element) {
switch (element.type) {
case "string":
element.stringValue = "";
break;
case "Vector2f":
element.vector2Value = Vector2.zero;
break;
case "Vector3f":
element.vector3Value = Vector3.zero;
break;
case "Rectf":
element.rectValue = new Rect();
break;
case "Quaternionf":
element.quaternionValue = Quaternion.identity;
break;
case "int":
element.intValue = 0;
break;
case "float":
element.floatValue = 0f;
break;
case "UInt8":
element.boolValue = false;
break;
case "ColorRGBA":
element.colorValue = Color.black;
break;
default:
if (element.type.StartsWith("PPtr"))
element.objectReferenceValue = null;
break;
}
}
} }
} }

Loading…
Cancel
Save