public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { VariablePropertyAttribute variableProperty = attribute as VariablePropertyAttribute; EditorGUI.BeginProperty(position, label, property); // Filter the variables by the types listed in the VariableProperty attribute Func <Variable, bool> compare = v => { if (variableProperty.VariableTypes.Length == 0) { return(true); } DebugLog.print(variableProperty.ToString()); return(variableProperty.VariableTypes.Contains <System.Type>(v.GetType())); }; VariableEditor.VariableField(property, label, FungusScriptWindow.GetFungusScript(), compare, (s, t, u) => (EditorGUI.Popup(position, s, t, u))); EditorGUI.EndProperty(); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); // The variable reference and data properties must follow the naming convention 'typeRef', 'typeVal' VariableInfoAttribute typeInfo = VariableEditor.GetVariableInfo(typeof(T)); if (typeInfo == null) { return; } string propNameBase = typeInfo.VariableType; propNameBase = Char.ToLowerInvariant(propNameBase[0]) + propNameBase.Substring(1); SerializedProperty referenceProp = property.FindPropertyRelative(propNameBase + "Ref"); SerializedProperty valueProp = property.FindPropertyRelative(propNameBase + "Val"); if (referenceProp == null || valueProp == null) { return; } Command command = property.serializedObject.targetObject as Command; if (command == null) { return; } Flowchart flowchart = command.GetFlowchart() as Flowchart; if (flowchart == null) { return; } if (EditorGUI.GetPropertyHeight(valueProp, label) > EditorGUIUtility.singleLineHeight) { DrawMultiLineProperty(position, label, referenceProp, valueProp, flowchart); } else { DrawSingleLineProperty(position, label, referenceProp, valueProp, flowchart); } EditorGUI.EndProperty(); }
public override void DrawCommandGUI() { serializedObject.Update(); RandomInteger t = target as RandomInteger; FungusScript fungusScript = t.GetFungusScript(); if (fungusScript == null) { return; } VariableEditor.VariableField(variableProp, new GUIContent("Variable", "Variable to use in operation"), t.GetFungusScript(), (v) => (v.GetType() == typeof(IntegerVariable))); EditorGUILayout.PropertyField(minValueProp); EditorGUILayout.PropertyField(maxValueProp); serializedObject.ApplyModifiedProperties(); }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { VariableInfoAttribute typeInfo = VariableEditor.GetVariableInfo(typeof(T)); if (typeInfo == null) { return(EditorGUIUtility.singleLineHeight); } string propNameBase = typeInfo.VariableType; propNameBase = Char.ToLowerInvariant(propNameBase[0]) + propNameBase.Substring(1); SerializedProperty referenceProp = property.FindPropertyRelative(propNameBase + "Ref"); if (referenceProp.objectReferenceValue != null) { return(EditorGUIUtility.singleLineHeight); } SerializedProperty valueProp = property.FindPropertyRelative(propNameBase + "Val"); return(EditorGUI.GetPropertyHeight(valueProp, label)); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { VariablePropertyAttribute variableProperty = attribute as VariablePropertyAttribute; if (variableProperty == null) { return; } EditorGUI.BeginProperty(position, label, property); // Filter the variables by the types listed in the VariableProperty attribute Func <Variable, bool> compare = v => { if (v == null) { return(false); } if (variableProperty.VariableTypes.Length == 0) { return(true); } return(variableProperty.VariableTypes.Contains <System.Type>(v.GetType())); }; VariableEditor.VariableField(property, label, FlowchartWindow.GetFlowchart(), variableProperty.defaultText, compare, (s, t, u) => (EditorGUI.Popup(position, s, t, u))); EditorGUI.EndProperty(); }
public void DrawItem(Rect position, int index) { Variable variable = this[index].objectReferenceValue as Variable; if (variable == null) { return; } float[] widths = { 80, 100, 140, 60 }; Rect[] rects = new Rect[4]; for (int i = 0; i < 4; ++i) { rects[i] = position; rects[i].width = widths[i] - 5; for (int j = 0; j < i; ++j) { rects[i].x += widths[j]; } } VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(variable.GetType()); if (variableInfo == null) { return; } Flowchart flowchart = FlowchartWindow.GetFlowchart(); if (flowchart == null) { return; } // Highlight if an active or selected command is referencing this variable bool highlight = false; if (flowchart.selectedBlock != null) { if (Application.isPlaying && flowchart.selectedBlock.IsExecuting()) { highlight = flowchart.selectedBlock.activeCommand.HasReference(variable); } else if (!Application.isPlaying && flowchart.selectedCommands.Count > 0) { foreach (Command selectedCommand in flowchart.selectedCommands) { if (selectedCommand == null) { continue; } if (selectedCommand.HasReference(variable)) { highlight = true; break; } } } } if (highlight) { GUI.backgroundColor = Color.green; GUI.Box(position, ""); } string key = variable.key; VariableScope scope = variable.scope; // To access properties in a monobehavior, you have to new a SerializedObject // http://answers.unity3d.com/questions/629803/findrelativeproperty-never-worked-for-me-how-does.html SerializedObject variableObject = new SerializedObject(this[index].objectReferenceValue); variableObject.Update(); GUI.Label(rects[0], variableInfo.VariableType); key = EditorGUI.TextField(rects[1], variable.key); SerializedProperty keyProp = variableObject.FindProperty("key"); keyProp.stringValue = flowchart.GetUniqueVariableKey(key, variable); SerializedProperty defaultProp = variableObject.FindProperty("value"); EditorGUI.PropertyField(rects[2], defaultProp, new GUIContent("")); SerializedProperty scopeProp = variableObject.FindProperty("scope"); scope = (VariableScope)EditorGUI.EnumPopup(rects[3], variable.scope); scopeProp.enumValueIndex = (int)scope; variableObject.ApplyModifiedProperties(); GUI.backgroundColor = Color.white; }
public virtual void DrawVariablesGUI() { serializedObject.Update(); Flowchart t = target as Flowchart; if (t.variables.Count == 0) { t.variablesExpanded = true; } if (!t.variablesExpanded) { if (GUILayout.Button("Variables (" + t.variables.Count + ")", GUILayout.Height(24))) { t.variablesExpanded = true; } } else { Rect listRect = new Rect(); if (t.variables.Count > 0) { // Remove any null variables from the list // Can sometimes happen when upgrading to a new version of Fungus (if .meta GUID changes for a variable class) for (int i = t.variables.Count - 1; i >= 0; i--) { if (t.variables[i] == null) { t.variables.RemoveAt(i); } } ReorderableListGUI.Title("Variables"); VariableListAdaptor adaptor = new VariableListAdaptor(variablesProp, 0); ReorderableListFlags flags = ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton; ReorderableListControl.DrawControlFromState(adaptor, null, flags); listRect = GUILayoutUtility.GetLastRect(); } else { GUILayoutUtility.GetRect(300, 24); listRect = GUILayoutUtility.GetLastRect(); listRect.y += 20; } float plusWidth = 32; float plusHeight = 24; Rect buttonRect = listRect; float buttonHeight = 24; buttonRect.x = 4; buttonRect.y -= buttonHeight - 1; buttonRect.height = buttonHeight; if (!Application.isPlaying) { buttonRect.width -= 30; } if (GUI.Button(buttonRect, "Variables")) { t.variablesExpanded = false; } Rect plusRect = listRect; plusRect.x += plusRect.width - plusWidth; plusRect.y -= plusHeight - 1; plusRect.width = plusWidth; plusRect.height = plusHeight; if (!Application.isPlaying && GUI.Button(plusRect, FungusEditorResources.texAddButton)) { GenericMenu menu = new GenericMenu(); List <System.Type> types = FindAllDerivedTypes <Variable>(); // Add variable types without a category foreach (System.Type type in types) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); if (variableInfo == null || variableInfo.Category != "") { continue; } AddVariableInfo addVariableInfo = new AddVariableInfo(); addVariableInfo.flowchart = t; addVariableInfo.variableType = type; GUIContent typeName = new GUIContent(variableInfo.VariableType); menu.AddItem(typeName, false, AddVariable, addVariableInfo); } // Add types with a category foreach (System.Type type in types) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); if (variableInfo == null || variableInfo.Category == "") { continue; } AddVariableInfo info = new AddVariableInfo(); info.flowchart = t; info.variableType = type; GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType); menu.AddItem(typeName, false, AddVariable, info); } menu.ShowAsContext(); } } serializedObject.ApplyModifiedProperties(); }
public override void DrawCommandGUI() { serializedObject.Update(); SetVariable t = target as SetVariable; FungusScript fungusScript = t.GetFungusScript(); if (fungusScript == null) { return; } VariableEditor.VariableField(variableProp, new GUIContent("Variable", "Variable to set"), fungusScript); if (variableProp.objectReferenceValue == null) { serializedObject.ApplyModifiedProperties(); return; } Variable selectedVariable = variableProp.objectReferenceValue as Variable; System.Type variableType = selectedVariable.GetType(); List <GUIContent> operatorsList = new List <GUIContent>(); operatorsList.Add(new GUIContent("=")); if (variableType == typeof(BooleanVariable)) { operatorsList.Add(new GUIContent("!")); } else if (variableType == typeof(IntegerVariable) || variableType == typeof(FloatVariable)) { operatorsList.Add(new GUIContent("+=")); operatorsList.Add(new GUIContent("-=")); operatorsList.Add(new GUIContent("*=")); operatorsList.Add(new GUIContent("/=")); } int selectedIndex = 0; switch (t.setOperator) { default: case SetVariable.SetOperator.Assign: selectedIndex = 0; break; case SetVariable.SetOperator.Negate: selectedIndex = 1; break; case SetVariable.SetOperator.Add: selectedIndex = 1; break; case SetVariable.SetOperator.Subtract: selectedIndex = 2; break; case SetVariable.SetOperator.Multiply: selectedIndex = 3; break; case SetVariable.SetOperator.Divide: selectedIndex = 4; break; } selectedIndex = EditorGUILayout.Popup(new GUIContent("Operator", "Arithmetic operator to use"), selectedIndex, operatorsList.ToArray()); SetVariable.SetOperator setOperator = SetVariable.SetOperator.Assign; if (variableType == typeof(BooleanVariable) || variableType == typeof(StringVariable)) { switch (selectedIndex) { default: case 0: setOperator = SetVariable.SetOperator.Assign; break; case 1: setOperator = SetVariable.SetOperator.Negate; break; } } else if (variableType == typeof(IntegerVariable) || variableType == typeof(FloatVariable)) { switch (selectedIndex) { default: case 0: setOperator = SetVariable.SetOperator.Assign; break; case 1: setOperator = SetVariable.SetOperator.Add; break; case 2: setOperator = SetVariable.SetOperator.Subtract; break; case 3: setOperator = SetVariable.SetOperator.Multiply; break; case 4: setOperator = SetVariable.SetOperator.Divide; break; } } setOperatorProp.enumValueIndex = (int)setOperator; if (variableType == typeof(BooleanVariable)) { EditorGUILayout.PropertyField(booleanDataProp); } else if (variableType == typeof(IntegerVariable)) { EditorGUILayout.PropertyField(integerDataProp); } else if (variableType == typeof(FloatVariable)) { EditorGUILayout.PropertyField(floatDataProp); } else if (variableType == typeof(StringVariable)) { EditorGUILayout.PropertyField(stringDataProp); } serializedObject.ApplyModifiedProperties(); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); // The variable reference and data properties must follow the naming convention 'typeRef', 'typeVal' VariableInfoAttribute typeInfo = VariableEditor.GetVariableInfo(typeof(T)); if (typeInfo == null) { return; } string propNameBase = typeInfo.VariableType; propNameBase = Char.ToLowerInvariant(propNameBase[0]) + propNameBase.Substring(1); SerializedProperty referenceProp = property.FindPropertyRelative(propNameBase + "Ref"); SerializedProperty valueProp = property.FindPropertyRelative(propNameBase + "Val"); if (referenceProp == null || valueProp == null) { return; } const int popupWidth = 65; Rect controlRect = EditorGUI.PrefixLabel(position, label); Rect valueRect = controlRect; valueRect.width = controlRect.width - popupWidth - 5; Rect popupRect = controlRect; if (referenceProp.objectReferenceValue == null) { EditorGUI.PropertyField(valueRect, valueProp, new GUIContent("")); popupRect.x += valueRect.width + 5; popupRect.width = popupWidth; } FungusScript fungusScript = property.serializedObject.targetObject as FungusScript; if (fungusScript == null) { Command command = property.serializedObject.targetObject as Command; if (command != null) { fungusScript = command.GetFungusScript(); } } if (fungusScript != null) { T selectedVariable = referenceProp.objectReferenceValue as T; List <string> variableKeys = new List <string>(); List <Variable> variableObjects = new List <Variable>(); variableKeys.Add("<Value>"); variableObjects.Add(null); int index = 0; int selectedIndex = 0; foreach (Variable v in fungusScript.variables) { if (v.GetType() != typeof(T)) { continue; } variableKeys.Add(v.key); variableObjects.Add(v); index++; if (v == selectedVariable) { selectedIndex = index; } } selectedIndex = EditorGUI.Popup(popupRect, selectedIndex, variableKeys.ToArray()); referenceProp.objectReferenceValue = variableObjects[selectedIndex]; } EditorGUI.EndProperty(); }
public override void DrawCommandGUI() { serializedObject.Update(); If t = target as If; FungusScript fungusScript = t.GetFungusScript(); if (fungusScript == null) { return; } VariableEditor.VariableField(variableProp, new GUIContent("Variable", "Variable to use in operation"), t.GetFungusScript(), null); if (variableProp.objectReferenceValue == null) { serializedObject.ApplyModifiedProperties(); return; } Variable selectedVariable = variableProp.objectReferenceValue as Variable; System.Type variableType = selectedVariable.GetType(); List <GUIContent> operatorList = new List <GUIContent>(); operatorList.Add(new GUIContent("==")); operatorList.Add(new GUIContent("!=")); if (variableType == typeof(IntegerVariable) || variableType == typeof(FloatVariable)) { operatorList.Add(new GUIContent("<")); operatorList.Add(new GUIContent(">")); operatorList.Add(new GUIContent("<=")); operatorList.Add(new GUIContent(">=")); } compareOperatorProp.enumValueIndex = EditorGUILayout.Popup(new GUIContent("Compare", "The comparison operator to use when comparing values"), compareOperatorProp.enumValueIndex, operatorList.ToArray()); if (variableType == typeof(BooleanVariable)) { EditorGUILayout.PropertyField(booleanValueProp); } else if (variableType == typeof(IntegerVariable)) { EditorGUILayout.PropertyField(integerValueProp); } else if (variableType == typeof(FloatVariable)) { EditorGUILayout.PropertyField(floatValueProp); } else if (variableType == typeof(StringVariable)) { EditorGUILayout.PropertyField(stringValueProp); } serializedObject.ApplyModifiedProperties(); }