Пример #1
0
        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();
        }
Пример #2
0
        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++;
            }
        }
Пример #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var l        = EditorGUI.BeginProperty(position, label, property);
            var startPos = position;

            position        = EditorGUI.PrefixLabel(position, l);
            position.height = EditorGUIUtility.singleLineHeight;
            var variable = property.FindPropertyRelative("variable");

            Fungus.Variable v = variable.objectReferenceValue as Fungus.Variable;

            if (variable.objectReferenceValue != null && lastFlowchart == null)
            {
                if (v != null)
                {
                    lastFlowchart = v.GetComponent <Flowchart>();
                }
            }

            lastFlowchart = EditorGUI.ObjectField(position, lastFlowchart, typeof(Fungus.Flowchart), true) as Fungus.Flowchart;
            position.y   += EditorGUIUtility.singleLineHeight;
            if (lastFlowchart != null)
            {
                var ourPos = startPos;
                ourPos.y = position.y;
                var prefixLabel = new GUIContent(v != null ? v.GetType().Name : "No Var Selected");
                EditorGUI.indentLevel++;
                VariableEditor.VariableField(variable,
                                             prefixLabel,
                                             lastFlowchart,
                                             "<None>",
                                             null,
                                             //lable, index, elements
                                             (s, t, u) => (EditorGUI.Popup(ourPos, s, t, u)));


                EditorGUI.indentLevel--;
            }
            else
            {
                EditorGUI.PrefixLabel(position, new GUIContent("Flowchart Required"));
            }

            variable.serializedObject.ApplyModifiedProperties();
            property.serializedObject.ApplyModifiedProperties();
            EditorGUI.EndProperty();
        }
        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();
        }
Пример #5
0
        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)
                {
                    var compatChecker = property.serializedObject.targetObject as ICollectionCompatible;
                    if (compatChecker != null)
                    {
                        return(compatChecker.IsVarCompatibleWithCollection(v, variableProperty.compatibleVariableName));
                    }
                    else
                    {
                        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();
        }
        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++;
            }
        }
Пример #7
0
        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();
        }
Пример #8
0
        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));
        }
Пример #9
0
        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;
        }
Пример #10
0
        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();
        }