diff --git a/Assets/Fungus/Editor/FungusScript/JumpEditor.cs b/Assets/Fungus/Editor/FungusScript/JumpEditor.cs new file mode 100644 index 00000000..6c8afbd6 --- /dev/null +++ b/Assets/Fungus/Editor/FungusScript/JumpEditor.cs @@ -0,0 +1,156 @@ +using UnityEditor; +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +namespace Fungus.Script +{ + + [CustomEditor (typeof(Jump))] + public class JumpEditor : FungusCommandEditor + { + public override void DrawCommandInspectorGUI() + { + Jump t = target as Jump; + + FungusScript sc = t.GetFungusScript(); + if (sc == null) + { + return; + } + + JumpCondition jumpCondition = (JumpCondition)EditorGUILayout.EnumPopup(new GUIContent("Jump Condition", "Condition when jump will occur"), t.jumpCondition); + if (jumpCondition != t.jumpCondition) + { + Undo.RecordObject(t, "Set Jump Condition"); + t.jumpCondition = jumpCondition; + } + + if (t.jumpCondition == JumpCondition.JumpAlways) + { + Sequence targetSequence = SequenceEditor.SequenceField(new GUIContent("Target Sequence", "Sequence to jump to"), t.GetFungusScript(), t.targetSequence); + if (targetSequence != t.targetSequence) + { + Undo.RecordObject(t, "Set Target Sequence"); + t.targetSequence = targetSequence; + } + return; + } + + List keys = new List(); + keys.Add(""); + int index = 0; + for (int i = 0; i < sc.variables.Count; ++i) + { + Variable v = sc.variables[i]; + keys.Add(v.key); + if (v.key == t.variableKey && + index == 0) + { + index = i + 1; + } + } + + int newIndex = EditorGUILayout.Popup("Compare Variable", index, keys.ToArray()); + + bool keyChanged = (t.variableKey != keys[newIndex]); + + if (keyChanged) + { + Undo.RecordObject(t, "Select variable"); + t.variableKey = keys[newIndex]; + } + + if (t.variableKey == "") + { + return; + } + + VariableType variableType = sc.variables[newIndex - 1].type; + + List operatorList = new List(); + operatorList.Add(new GUIContent("==")); + operatorList.Add(new GUIContent("!=")); + if (variableType == VariableType.Integer || + variableType == VariableType.Float) + { + operatorList.Add(new GUIContent("<")); + operatorList.Add(new GUIContent(">")); + operatorList.Add(new GUIContent("<=")); + operatorList.Add(new GUIContent(">=")); + } + + CompareOperator compareOperator = (CompareOperator)EditorGUILayout.Popup(new GUIContent("Operator", + "The comparison operator to use when comparing values"), + (int)t.compareOperator, + operatorList.ToArray()); + if (compareOperator != t.compareOperator) + { + Undo.RecordObject(t, "Select compare operator"); + t.compareOperator = compareOperator; + } + + bool booleanValue = t.booleanData.value; + int integerValue = t.integerData.value; + float floatValue = t.floatData.value; + string stringValue = t.stringData.value; + + switch (variableType) + { + case VariableType.Boolean: + booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); + break; + case VariableType.Integer: + integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); + break; + case VariableType.Float: + floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); + break; + case VariableType.String: + stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); + break; + } + + if (booleanValue != t.booleanData.value) + { + Undo.RecordObject(t, "Set boolean value"); + t.booleanData.value = booleanValue; + } + else if (integerValue != t.integerData.value) + { + Undo.RecordObject(t, "Set integer value"); + t.integerData.value = integerValue; + } + else if (floatValue != t.floatData.value) + { + Undo.RecordObject(t, "Set float value"); + t.floatData.value = floatValue; + } + else if (stringValue != t.stringData.value) + { + Undo.RecordObject(t, "Set string value"); + t.stringData.value = stringValue; + } + + Sequence onTrue = SequenceEditor.SequenceField(new GUIContent("On True Sequence", "Sequence to execute if comparision is true"), + t.GetFungusScript(), + t.onTrueSequence); + + Sequence onFalse = SequenceEditor.SequenceField(new GUIContent("On False Sequence", "Sequence to execute if comparision is false"), + t.GetFungusScript(), + t.onFalseSequence); + + if (onTrue != t.onTrueSequence) + { + Undo.RecordObject(t, "Set On True Sequence"); + t.onTrueSequence = onTrue; + } + if (onFalse != t.onFalseSequence) + { + Undo.RecordObject(t, "Set On False Sequence"); + t.onFalseSequence = onFalse; + } + } + } + +} diff --git a/Assets/Fungus/VisualScripting/Jump.cs b/Assets/Fungus/VisualScripting/Jump.cs new file mode 100644 index 00000000..23980db4 --- /dev/null +++ b/Assets/Fungus/VisualScripting/Jump.cs @@ -0,0 +1,182 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +namespace Fungus.Script +{ + public enum JumpCondition + { + JumpAlways, + JumpOnCompare + } + + public enum CompareOperator + { + Equals, // == + NotEquals, // != + LessThan, // < + GreaterThan, // > + LessThanOrEquals, // <= + GreaterThanOrEquals // >= + } + + public class Jump : FungusCommand + { + public JumpCondition jumpCondition; + + public Sequence targetSequence; // Only used for Always condition + + public string variableKey; + + public CompareOperator compareOperator; + + public BooleanData booleanData; + + public IntegerData integerData; + + public FloatData floatData; + + public StringData stringData; + + public Sequence onTrueSequence; + + public Sequence onFalseSequence; + + public override void OnEnter() + { + if (jumpCondition == JumpCondition.JumpAlways && + targetSequence != null) + { + ExecuteSequence(targetSequence); + return; + } + + Variable v = parentFungusScript.GetVariable(variableKey); + + bool condition = false; + + switch (v.type) + { + case VariableType.Boolean: + switch (compareOperator) + { + case CompareOperator.Equals: + condition = (v.booleanValue == booleanData.value); + break; + case CompareOperator.NotEquals: + default: + condition = (v.booleanValue != booleanData.value); + break; + } + break; + case VariableType.Integer: + switch (compareOperator) + { + case CompareOperator.Equals: + condition = (v.integerValue == integerData.value); + break; + case CompareOperator.NotEquals: + condition = (v.integerValue != integerData.value); + break; + case CompareOperator.LessThan: + condition = (v.integerValue < integerData.value); + break; + case CompareOperator.GreaterThan: + condition = (v.integerValue > integerData.value); + break; + case CompareOperator.LessThanOrEquals: + condition = (v.integerValue <= integerData.value); + break; + case CompareOperator.GreaterThanOrEquals: + condition = (v.integerValue >= integerData.value); + break; + } + break; + case VariableType.Float: + switch (compareOperator) + { + case CompareOperator.Equals: + condition = (v.floatValue == floatData.value); + break; + case CompareOperator.NotEquals: + condition = (v.floatValue != floatData.value); + break; + case CompareOperator.LessThan: + condition = (v.floatValue < floatData.value); + break; + case CompareOperator.GreaterThan: + condition = (v.floatValue > floatData.value); + break; + case CompareOperator.LessThanOrEquals: + condition = (v.floatValue <= floatData.value); + break; + case CompareOperator.GreaterThanOrEquals: + condition = (v.floatValue >= floatData.value); + break; + } + break; + case VariableType.String: + switch (compareOperator) + { + case CompareOperator.Equals: + condition = (v.stringValue == stringData.value); + break; + case CompareOperator.NotEquals: + default: + condition = (v.stringValue != stringData.value); + break; + } + break; + } + + if (condition) + { + if (onTrueSequence != null) + { + ExecuteSequence(onTrueSequence); + return; + } + } + else + { + if (onFalseSequence != null) + { + ExecuteSequence(onFalseSequence); + return; + } + } + + Continue(); + } + + public override void GetConnectedSequences(ref List connectedSequences) + { + if (jumpCondition == JumpCondition.JumpAlways && + targetSequence != null) + { + connectedSequences.Add(targetSequence); + return; + } + + if (onTrueSequence != null) + { + connectedSequences.Add(onTrueSequence); + } + if (onFalseSequence != null) + { + connectedSequences.Add(onFalseSequence); + } + } + + public override string GetCommandName() + { + if (jumpCondition == JumpCondition.JumpAlways) + { + return GetType().Name; + } + + return GetType().Name + "?"; + } + } + +} \ No newline at end of file