예제 #1
0
        public static VariableInfoAttribute GetVariableInfo(System.Type variableType)
        {
            object[] attributes = variableType.GetCustomAttributes(typeof(VariableInfoAttribute), false);
            foreach (object obj in attributes)
            {
                VariableInfoAttribute variableInfoAttr = obj as VariableInfoAttribute;
                if (variableInfoAttr != null)
                {
                    return(variableInfoAttr);
                }
            }

            return(null);
        }
예제 #2
0
        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();
        }
예제 #3
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));
        }
        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();
        }
예제 #6
0
        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();
        }