public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Q: EditorGUI.BeginProperty ?? EditorGUI.BeginProperty(position, label, property); // The variable reference and data properties must follow the naming convention 'typeRef', 'typeVal' // 获得上面的VariableInfoAttribute标签 VariableInfoAttribute typeInfo = VariableEditor.GetVariableInfo(typeof(T)); if (typeInfo == null) { return; } string propNameBase = typeInfo.VariableType; propNameBase = Char.ToLowerInvariant(propNameBase[0]) + propNameBase.Substring(1); // 例如:获取 // BooleanVariable booleanRef; SerializedProperty referenceProp = property.FindPropertyRelative(propNameBase + "Ref"); // bool booleanVal; SerializedProperty valueProp = property.FindPropertyRelative(propNameBase + "Val"); if (referenceProp == null || valueProp == null) { return; } Command command = property.serializedObject.targetObject as Command; if (command == null) { return; } var flowchart = command.GetFlowchart() as Flowchart; if (flowchart == null) { return; } var origLabel = new GUIContent(label); if (EditorGUI.GetPropertyHeight(valueProp, label) > EditorGUIUtility.singleLineHeight) { DrawMultiLineProperty(position, origLabel, referenceProp, valueProp, flowchart); } else { DrawSingleLineProperty(position, origLabel, referenceProp, valueProp, flowchart); } EditorGUI.EndProperty(); }
protected override void PrepareAllItems() { int i = 0; foreach (var item in VariableTypes) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(item); if (variableInfo != null) { allItems.Add(new FilteredListItem(i, (variableInfo.Category.Length > 0 ? variableInfo.Category + CATEGORY_CHAR : "") + variableInfo.VariableType)); } i++; } }
private void AddButton(ReorderableList list) { GenericMenu menu = new GenericMenu(); List <System.Type> types = FlowchartEditor.FindAllDerivedTypes <Variable>(); // Add variable types without a category foreach (var type in types) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); if (variableInfo == null || variableInfo.Category != "") { continue; } AddVariableInfo addVariableInfo = new AddVariableInfo(); addVariableInfo.flowchart = TargetFlowchart; addVariableInfo.variableType = type; GUIContent typeName = new GUIContent(variableInfo.VariableType); menu.AddItem(typeName, false, AddVariable, addVariableInfo); } // Add types with a category foreach (var type in types) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); if (variableInfo == null || variableInfo.Category == "") { continue; } AddVariableInfo info = new AddVariableInfo(); info.flowchart = TargetFlowchart; info.variableType = type; GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType); menu.AddItem(typeName, false, AddVariable, info); } menu.ShowAsContext(); }
protected override void PrepareAllItems() { int i = 0; foreach (var item in VariableTypes) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(item); if (variableInfo != null) { var obsAttr = item.GetCustomAttribute <System.ObsoleteAttribute>(); var fliStr = (variableInfo.Category.Length > 0 ? variableInfo.Category + CATEGORY_CHAR : "") + (obsAttr != null ? FungusConstants.UIPrefixForDeprecated_RichText : "") + variableInfo.VariableType; allItems.Add(new FilteredListItem(i, fliStr)); } i++; } }
static protected void DoOlderMenu(Flowchart flowchart) { GenericMenu menu = new GenericMenu(); // Add variable types without a category foreach (var type in VariableTypes) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); if (variableInfo == null || variableInfo.Category != "") { continue; } GUIContent typeName = new GUIContent(variableInfo.VariableType); menu.AddItem(typeName, false, AddVariable, type); } // Add types with a category foreach (var type in VariableTypes) { VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); if (variableInfo == null || variableInfo.Category == "") { continue; } GUIContent typeName = new GUIContent(variableInfo.Category + CATEGORY_CHAR + variableInfo.VariableType); menu.AddItem(typeName, false, AddVariable, type); } menu.ShowAsContext(); }
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 void DrawItem(Rect position, int index, bool selected, bool focused) { Variable variable = GetVarAt(index);// this[index].objectReferenceValue as Variable; if (variable == null) { return; } if (Event.current.type == EventType.ContextClick && position.Contains(Event.current.mousePosition)) { DoRightClickMenu(index); } for (int i = 0; i < 4; ++i) { itemRects[i] = position; itemRects[i].width = itemWidths[i] - 5; for (int j = 0; j < i; ++j) { itemRects[i].x += itemWidths[j]; } } var varType = variable.GetType(); VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(varType); if (variableInfo == null) { return; } var flowchart = TargetFlowchart; 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.IsVariableReferenced(variable); } else if (!Application.isPlaying && flowchart.SelectedCommands.Count > 0) { foreach (Command selectedCommand in flowchart.SelectedCommands) { if (selectedCommand == null) { continue; } if (selectedCommand.IsVariableReferenced(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(variable); variableObject.Update(); var obsAttr = varType.GetCustomAttribute <System.ObsoleteAttribute>(); if (obsAttr != null) { var existingGUICol = GUI.color; GUI.color = Color.yellow; GUI.Label(itemRects[0], FungusConstants.UIPrefixForDeprecated + variableInfo.VariableType); GUI.color = existingGUICol; } else { GUI.Label(itemRects[0], variableInfo.VariableType); } SerializedProperty keyProp = variableObject.FindProperty("key"); SerializedProperty defaultProp = variableObject.FindProperty("value"); SerializedProperty scopeProp = variableObject.FindProperty("scope"); EditorGUI.BeginChangeCheck(); key = EditorGUI.TextField(itemRects[1], variable.Key); if (EditorGUI.EndChangeCheck()) { keyProp.stringValue = flowchart.GetUniqueVariableKey(key, variable); } bool isGlobal = scopeProp.enumValueIndex == (int)VariableScope.Global; var prevEnabled = GUI.enabled; if (isGlobal && Application.isPlaying) { var res = FungusManager.Instance.GlobalVariables.GetVariable(keyProp.stringValue); if (res != null) { SerializedObject globalValue = new SerializedObject(res); var globalValProp = globalValue.FindProperty("value"); GUI.enabled = false; defaultProp = globalValProp; } } //variable.DrawProperty(rects[2], defaultProp, variableInfo); VariableDrawProperty(variable, itemRects[2], defaultProp, variableInfo); GUI.enabled = prevEnabled; scope = (VariableScope)EditorGUI.EnumPopup(itemRects[3], variable.Scope); scopeProp.enumValueIndex = (int)scope; variableObject.ApplyModifiedProperties(); GUI.backgroundColor = Color.white; }
public void DrawItem(Rect position, int index) { Variable variable = this[index].objectReferenceValue as Variable; if (variable == null) { return; } int width = widthOfList; int totalRatio = DefaultWidth; float[] widths = { (80.0f / totalRatio) * width, (100.0f / totalRatio) * width, (140.0f / totalRatio) * width, (60.0f / totalRatio) * width }; 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; } var 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"); SerializedProperty defaultProp = variableObject.FindProperty("value"); SerializedProperty scopeProp = variableObject.FindProperty("scope"); keyProp.stringValue = flowchart.GetUniqueVariableKey(key, variable); bool isGlobal = scopeProp.enumValueIndex == (int)VariableScope.Global; if (isGlobal && Application.isPlaying) { var res = FungusManager.Instance.GlobalVariables.GetVariable(keyProp.stringValue); if (res != null) { SerializedObject globalValue = new SerializedObject(res); var globalValProp = globalValue.FindProperty("value"); var prevEnabled = GUI.enabled; GUI.enabled = false; EditorGUI.PropertyField(rects[2], globalValProp, new GUIContent("")); GUI.enabled = prevEnabled; } } else { EditorGUI.PropertyField(rects[2], defaultProp, new GUIContent("")); } scope = (VariableScope)EditorGUI.EnumPopup(rects[3], variable.Scope); scopeProp.enumValueIndex = (int)scope; variableObject.ApplyModifiedProperties(); GUI.backgroundColor = Color.white; }
public virtual void DrawVariablesGUI() { serializedObject.Update(); var 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; } // Draw disclosure triangle Rect lastRect = GUILayoutUtility.GetLastRect(); lastRect.x += 5; lastRect.y += 5; EditorGUI.Foldout(lastRect, false, ""); } 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; } // Draw disclosure triangle Rect lastRect = buttonRect; lastRect.x += 5; lastRect.y += 5; EditorGUI.Foldout(lastRect, true, ""); Rect plusRect = listRect; plusRect.x += plusRect.width - plusWidth; plusRect.y -= plusHeight - 1; plusRect.width = plusWidth; plusRect.height = plusHeight; if (!Application.isPlaying && GUI.Button(plusRect, addTexture)) { GenericMenu menu = new GenericMenu(); List <System.Type> types = FindAllDerivedTypes <Variable>(); // Add variable types without a category foreach (var 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 (var 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(); }