Exemple #1
0
        public void Info(string text, MessageType messageType = MessageType.Info)
        {
            GUIStyle style = new GUIStyle(GUI.skin.box);

            style.alignment = TextAnchor.MiddleCenter;
            style.wordWrap  = true;

            float height = 0;

            if (positionRect.width > 20f)
            {
                float      width   = positionRect.width;
                GUIContent content = new GUIContent(text);
                height            = style.CalcHeight(content, positionRect.width);
                style.fixedHeight = height;
                Rect infoRect = new Rect(positionRect.x, positionRect.y, positionRect.width, height);
                EditorGUI.HelpBox(infoRect, text, messageType);

                EditorCache.SetCachedValue("height", height,
                                           GenerateKey() + text.GetHashCode()); // not the most optimal
            }
            else
            {
                EditorCache.GetCachedValue("height", ref height, GenerateKey() + text.GetHashCode());
            }

            AdvancePosition(height + NUISettings.FIELD_SPACING);
        }
Exemple #2
0
        public float GetHeight(string key)
        {
            float height = 0;

            EditorCache.GetCachedValue("height", ref height, key);
            return(height);
        }
Exemple #3
0
        public void BeginProperty(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property == null)
            {
                Debug.LogError("Cannot draw property drawer for null property.");
                return;
            }

            _drawerType        = DrawerType.Property;
            serializedProperty = property;
            serializedObject   = property.serializedObject;
            _cachedKey         = GenerateKey();

            totalHeight  = 0;
            positionRect = position;

            bool guiWasEnabled = true;

            if (EditorCache.GetCachedValue("guiWasEnabled", ref guiWasEnabled, GenerateKey()))
            {
                GUI.enabled = guiWasEnabled;
            }

            InitializeGUIElements();

            Rect totalRect = new Rect(positionRect.x, positionRect.y, positionRect.width, GetHeight());

            DrawBackgroundRect(new Rect(positionRect.x, positionRect.y, positionRect.width, GetHeight()));
            //EditorGUI.BeginProperty(totalRect, label, property);

            EditorGUI.BeginChangeCheck();
        }
Exemple #4
0
        public void ReorderableList(string propertyName, string label = null, bool draggable = true,
                                    bool showAddRemoveButtons         = true,
                                    Type baseType = null, float elementSpacing = 0)
        {
            SerializedProperty listProperty = FindProperty(propertyName);

            if (listProperty == null)
            {
                Debug.LogError("Property is null.");
                return;
            }

            ReorderableList reorderableList = null;

            EditorCache.GetCachedValue("ReorderableList", ref reorderableList, GenerateKey(listProperty));
            if (reorderableList == null)
            {
                reorderableList = new ReorderableList(listProperty.serializedObject, listProperty, draggable,
                                                      true, true, true);
            }

            reorderableList.serializedProperty = listProperty;
            reorderableList.displayAdd         = showAddRemoveButtons;
            reorderableList.displayRemove      = showAddRemoveButtons;

            string headerLabel = string.IsNullOrEmpty(label) ? listProperty.displayName : label;

            reorderableList.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, new GUIContent(headerLabel)); };

            reorderableList.drawElementCallback = (rect, index, active, focused) =>
            {
                SerializedProperty p = listProperty.GetArrayElementAtIndex(index);
                EditorGUI.PropertyField(rect, p, true);
            };

            reorderableList.elementHeightCallback = index =>
            {
                SerializedProperty p = listProperty.GetArrayElementAtIndex(index);
                float height         = EditorGUI.GetPropertyHeight(p, true);
                return(height + elementSpacing);
            };

            float listHeight = reorderableList.GetHeight();
            Rect  listRect   = new Rect(positionRect.x, positionRect.y + 3f, positionRect.width, listHeight);

            reorderableList.DoList(listRect);

            AdvancePosition(listHeight + NUISettings.FIELD_SPACING);

            EditorCache.SetCachedValue("ReorderableList", reorderableList, GenerateKey(listProperty));
        }
Exemple #5
0
        public void EmbeddedObjectEditor(Object obj, Rect rect, float leftMargin = 0)
        {
            if (obj == null)
            {
                return;
            }

            Editor editor = null;
            string key    = GenerateKey(obj);

            EditorCache.GetCachedValue("NUIEditor", ref editor, key);
            NUIEditor nuiEditor = editor as NUIEditor;

            if (nuiEditor == null)
            {
                nuiEditor = Editor.CreateEditor(obj) as NUIEditor;

                if (nuiEditor == null)
                {
                    Debug.LogError("Failed to create scriptable object editor.");
                    return;
                }

                EditorCache.SetCachedValue("NUIEditor", nuiEditor, GenerateKey(obj));
            }

            rect.y += 8;

            if (nuiEditor != null)
            {
                Space();
                nuiEditor.drawer.customPositionRect = positionRect;
                nuiEditor.drawer.parentKey          = GenerateKey();
                nuiEditor.OnInspectorNUI();

                float editorHeight = GetHeight(key);
                GUILayout.Space(
                    -editorHeight); // Negate space that embedded editor has already added to prevent overly-large final editor
                AdvancePosition(editorHeight);
            }
            else
            {
                Debug.LogError("Cannot draw null editor.");
            }

            UpdateCachedKey();
        }
Exemple #6
0
        public Texture2D GetTexture(string resourcesPath)
        {
            Texture2D tex = null;

            EditorCache.GetCachedValue("Texture2D", ref tex, resourcesPath);
            if (tex == null)
            {
                tex = Resources.Load(resourcesPath) as Texture2D;
                if (tex == null)
                {
                    Debug.LogError($"{resourcesPath} not found or not Texture2D.");
                }
                else
                {
                    EditorCache.SetCachedValue("Texture2D", tex, resourcesPath);
                }
            }

            return(tex);
        }
Exemple #7
0
        public virtual void EndProperty()
        {
            if (EditorGUI.EndChangeCheck())
            {
                serializedObject.ApplyModifiedProperties();
            }

            if (totalHeight > 30f)
            {
                Space(6); // Add padding if expanded
            }

            bool wasEnabled = true;

            EditorCache.GetCachedValue("guiWasEnabled", ref wasEnabled, GenerateKey());
            if (!GUI.enabled && wasEnabled)
            {
                EditorGUI.EndDisabledGroup();
            }

            SetHeight(totalHeight);
            GUI.enabled = true;
        }
Exemple #8
0
 public void SetHeight(string key, float height)
 {
     EditorCache.SetCachedValue("height", height, key);
 }
Exemple #9
0
        public int HorizontalToolbar(string name, string[] texts, bool fillWidth = true, bool singleLine = false,
                                     float targetButtonWidth = 80f, float buttonHeight = 18f)
        {
            float toolbarHeight = 0;
            int   tabIndex      = GetTabIndex(name);

            if (positionRect.width > 20f)
            {
                Rect  initRect         = positionRect;
                float rowHeight        = buttonHeight;
                int   buttonCount      = texts.Length;
                float rowWidth         = positionRect.width;
                float singleLineWidth  = buttonCount * targetButtonWidth;
                int   rowCount         = singleLine ? 1 : rowWidth > 10 ? Mathf.CeilToInt(singleLineWidth / rowWidth) : 1;
                int   maxButtonsPerRow = Mathf.FloorToInt(rowWidth / targetButtonWidth);
                int   lastRowButtons   = buttonCount - maxButtonsPerRow * (rowCount - 1);
                toolbarHeight = rowCount * rowHeight;

                Rect bottomLine = new Rect(initRect.x, initRect.y + toolbarHeight, initRect.width, 1f);
                EditorGUI.DrawRect(bottomLine, new Color(0.4f, 0.4f, 0.5f, 1f));

                string[][] subTexts = new string[rowCount][];
                int        offset   = 0;
                for (int i = 0; i < rowCount; i++)
                {
                    if (i == rowCount - 1)
                    {
                        subTexts[i] = new string[lastRowButtons];
                        for (int j = 0; j < lastRowButtons; j++)
                        {
                            subTexts[i][j] = texts[offset++];
                        }
                    }
                    else
                    {
                        subTexts[i] = new string[maxButtonsPerRow];
                        for (int j = 0; j < maxButtonsPerRow; j++)
                        {
                            subTexts[i][j] = texts[offset++];
                        }
                    }
                }


                int counter = 0;
                for (int x = 0; x < rowCount; x++)
                {
                    float buttonWidth = fillWidth
                        ? x == rowCount - 1 ? rowWidth / lastRowButtons : rowWidth / maxButtonsPerRow
                        : targetButtonWidth;
                    for (int y = 0; y < subTexts[x].Length; y++)
                    {
                        GUIStyle buttonStyle = new GUIStyle(EditorStyles.miniButtonMid);
                        buttonStyle.fixedHeight = buttonHeight;

                        Color initColor = GUI.color;

                        Rect buttonRect = new Rect(initRect.x + y * buttonWidth, initRect.y, buttonWidth,
                                                   rowHeight + 1);

                        if (tabIndex == counter)
                        {
                            GUI.color = new Color(1f, 1f, 1f, 1f);
                        }
                        else
                        {
                            GUI.color = new Color(0.9f, 0.9f, 0.9f, 1f);
                        }

                        if (GUI.Button(buttonRect, subTexts[x][y], buttonStyle))
                        {
                            tabIndex = counter;
                        }

                        if (tabIndex == counter)
                        {
                            Rect highlightRect = new Rect(buttonRect.x, buttonRect.y + buttonRect.height - 3,
                                                          buttonRect.width,
                                                          3);
                            EditorGUI.DrawRect(highlightRect, NUISettings.lightBlueColor);
                        }

                        GUI.color = initColor;
                        counter++;
                    }

                    initRect.y += rowHeight;
                }

                SetTabIndex(name, tabIndex);
                EditorCache.SetCachedValue("height", toolbarHeight, GenerateKey() + name);
            }
            else
            {
                EditorCache.GetCachedValue("height", ref toolbarHeight, GenerateKey() + name);
            }

            AdvancePosition(toolbarHeight + 2);
            return(tabIndex);
        }
Exemple #10
0
        public int HorizontalToolbar(string name, string[] textureResourcePaths, float toolbarHeight)
        {
            int tabIndex = GetTabIndex(name);

            int n = textureResourcePaths.Length;

            if (n == 0)
            {
                return(-1);
            }

            if (positionRect.width > 20f)
            {
                Rect  initRect = positionRect;
                float rowWidth = positionRect.width;

                Rect bottomLine = new Rect(initRect.x, initRect.y + toolbarHeight, initRect.width, 1f);
                EditorGUI.DrawRect(bottomLine, new Color(0.4f, 0.4f, 0.5f, 1f));

                int   counter     = 0;
                float buttonWidth = rowWidth / n;

                Texture2D[] icons = new Texture2D[n];
                for (int i = 0; i < n; i++)
                {
                    icons[i] = GetTexture(textureResourcePaths[i]);
                }

                for (int i = 0; i < n; i++)
                {
                    GUIStyle buttonStyle = new GUIStyle(EditorStyles.miniButtonMid);
                    buttonStyle.fixedHeight = toolbarHeight;

                    Color initColor  = GUI.color;
                    Rect  buttonRect = new Rect(initRect.x + i * buttonWidth, initRect.y, buttonWidth, toolbarHeight);

                    if (tabIndex == counter)
                    {
                        GUI.color = new Color(1f, 1f, 1f, 1f);
                    }
                    else
                    {
                        GUI.color = new Color(0.9f, 0.9f, 0.9f, 1f);
                    }

                    if (GUI.Button(buttonRect, icons[i], buttonStyle))
                    {
                        tabIndex = counter;
                    }

                    if (tabIndex == counter)
                    {
                        Rect highlightRect = new Rect(buttonRect.x, buttonRect.y + buttonRect.height - 3,
                                                      buttonRect.width,
                                                      3);
                        EditorGUI.DrawRect(highlightRect, NUISettings.lightBlueColor);
                    }

                    GUI.color = initColor;
                    counter++;
                }

                SetTabIndex(name, tabIndex);
                EditorCache.SetCachedValue("height", toolbarHeight, GenerateKey() + name);
            }
            else
            {
                EditorCache.GetCachedValue("height", ref toolbarHeight, GenerateKey() + name);
            }

            AdvancePosition(toolbarHeight + 2);
            return(tabIndex);
        }