Browse Source

Refactored variables

master
chrisgregan 11 years ago
parent
commit
f617b72dee
  1. 7
      Assets/Fungus/Editor/SequenceControllerEditor.cs
  2. 55
      Assets/Fungus/Editor/SetVariableCommandEditor.cs
  3. BIN
      Assets/Fungus/Tests/Sequence/SequenceTest.unity
  4. 35
      Assets/Fungus/VisualScripting/SetVariableCommand.cs
  5. 52
      Assets/Fungus/VisualScripting/Variable.cs

7
Assets/Fungus/Editor/SequenceControllerEditor.cs

@ -42,12 +42,11 @@ public class VariableDrawer : PropertyDrawer
[CustomEditor (typeof(SequenceController))] [CustomEditor (typeof(SequenceController))]
public class SequenceControllerEditor : Editor public class SequenceControllerEditor : Editor
{ {
SerializedProperty globalVariablesProperty; SerializedProperty variablesProperty;
SerializedProperty localVariablesProperty;
void OnEnable() void OnEnable()
{ {
localVariablesProperty = serializedObject.FindProperty("variables"); variablesProperty = serializedObject.FindProperty("variables");
} }
public override void OnInspectorGUI() public override void OnInspectorGUI()
@ -92,7 +91,7 @@ public class SequenceControllerEditor : Editor
} }
ReorderableListGUI.Title("Variables"); ReorderableListGUI.Title("Variables");
ReorderableListGUI.ListField(localVariablesProperty); ReorderableListGUI.ListField(variablesProperty);
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
} }

55
Assets/Fungus/Editor/SetVariableCommandEditor.cs

@ -26,78 +26,71 @@ namespace Fungus
{ {
Variable v = sc.variables[i]; Variable v = sc.variables[i];
keys.Add(v.key); keys.Add(v.key);
if (v.key == t.variableKey && if (v.key == t.variable.key &&
index == 0) index == 0)
{ {
index = i + 1; index = i + 1;
} }
} }
int newIndex = EditorGUILayout.Popup("Variable", index, keys.ToArray()); int newIndex = EditorGUILayout.Popup("Variable", index, keys.ToArray());
if (newIndex != index) bool keyChanged = (t.variable.key != keys[newIndex]);
if (keyChanged)
{ {
Undo.RecordObject(t, "Select variable"); Undo.RecordObject(t, "Select variable");
t.variable.key = keys[newIndex];
} }
if (newIndex == 0) if (t.variable.key == "<None>")
{ {
t.variableKey = "";
return; return;
} }
Variable variable = sc.variables[newIndex - 1]; VariableType variableType = sc.variables[newIndex - 1].type;
if (t.variableKey != variable.key)
{
Undo.RecordObject(t, "Select variable");
t.variableKey = variable.key;
}
bool booleanValue = t.booleanValue; bool booleanValue = t.variable.booleanValue;
int integerValue = t.integerValue; int integerValue = t.variable.integerValue;
float floatValue = t.floatValue; float floatValue = t.variable.floatValue;
string stringValue = t.stringValue; string stringValue = t.variable.stringValue;
switch (variable.type) switch (variableType)
{ {
case VariableType.Boolean: case VariableType.Boolean:
booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), t.booleanValue); booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue);
break; break;
case VariableType.Integer: case VariableType.Integer:
integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), t.integerValue); integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue);
break; break;
case VariableType.Float: case VariableType.Float:
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), t.floatValue); floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue);
break; break;
case VariableType.String: case VariableType.String:
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), t.stringValue); stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue);
break; break;
} }
if (booleanValue != t.booleanValue) if (booleanValue != t.variable.booleanValue)
{ {
Undo.RecordObject(t, "Set boolean value"); Undo.RecordObject(t, "Set boolean value");
t.booleanValue = booleanValue; t.variable.booleanValue = booleanValue;
} }
else if (integerValue != t.integerValue) else if (integerValue != t.variable.integerValue)
{ {
Undo.RecordObject(t, "Set integer value"); Undo.RecordObject(t, "Set integer value");
t.integerValue = integerValue; t.variable.integerValue = integerValue;
} }
else if (floatValue != t.floatValue) else if (floatValue != t.variable.floatValue)
{ {
Undo.RecordObject(t, "Set float value"); Undo.RecordObject(t, "Set float value");
t.floatValue = floatValue; t.variable.floatValue = floatValue;
} }
else if (stringValue != t.stringValue) else if (stringValue != t.variable.stringValue)
{ {
Undo.RecordObject(t, "Set string value"); Undo.RecordObject(t, "Set string value");
t.stringValue = stringValue; t.variable.stringValue = stringValue;
} }
} }
} }

BIN
Assets/Fungus/Tests/Sequence/SequenceTest.unity

Binary file not shown.

35
Assets/Fungus/VisualScripting/SetVariableCommand.cs

@ -4,45 +4,30 @@ using Fungus;
public class SetVariableCommand : FungusCommand public class SetVariableCommand : FungusCommand
{ {
public string variableKey; public Variable variable;
public string stringValue;
public int integerValue;
public bool booleanValue;
public float floatValue;
public override void OnEnter() public override void OnEnter()
{ {
if (variableKey.Length == 0) if (variable.key.Length == 0)
{ {
Finish(); Finish();
return; return;
} }
Variable v = parentSequenceController.GetVariable(variableKey); Variable v = parentSequenceController.GetVariable(variable.key);
if (v == null) if (v == null)
{ {
Debug.LogError("Variable " + variableKey + " not defined."); Debug.LogError("Variable " + variable.key + " not defined.");
} }
else else
{ {
switch (v.type) if (v.IsSameType(variable))
{
v.Assign(variable);
}
else
{ {
case VariableType.String: Debug.LogError("Variables " + variable.key + " and " + v.key + " are not of the same type.");
v.stringValue = stringValue;
break;
case VariableType.Integer:
v.integerValue = integerValue;
break;
case VariableType.Float:
v.floatValue = floatValue;
break;
case VariableType.Boolean:
v.booleanValue = booleanValue;
break;
} }
} }

52
Assets/Fungus/VisualScripting/Variable.cs

@ -19,10 +19,58 @@ namespace Fungus
public string key; public string key;
public VariableType type; public VariableType type;
public string stringValue; public bool booleanValue;
public int integerValue; public int integerValue;
public float floatValue; public float floatValue;
public bool booleanValue; public string stringValue;
public bool IsSameType(Variable other)
{
return (this.type == other.type);
} }
public bool Assign(Variable other)
{
if (!IsSameType(other))
{
return false;
}
booleanValue = other.booleanValue;
integerValue = other.integerValue;
floatValue = other.floatValue;
stringValue = other.stringValue;
return true;
}
}
[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;
}
} }
Loading…
Cancel
Save