Beispiel #1
0
        internal static bool FoldoutHeaderGroupInternal(Rect position, bool foldout, string label, Texture2D background, Color backgroundColor, string additionalLabel = "")
        {
            GUIStyle foldoutStyle = EditorStyles.titlebarFoldout;

            var offset = 18;

            position.x += offset;

            // add some spaces so that we could draw a colored texture before the label
            label = $"       {label}";
            GUIContent content = new GUIContent(label);

            // Removing the default margin for inspectors
            if (EditorGUIUtility.hierarchyMode)
            {
                position.xMin -= EditorStyles.inspectorDefaultMargins.padding.left - EditorStyles.inspectorDefaultMargins.padding.right;
                position.xMax += EditorStyles.inspectorDefaultMargins.padding.right;
            }

            var  labelSize = GUI.skin.label.CalcSize(new GUIContent(additionalLabel));
            Rect menuRect  = new Rect
            {
                x    = position.xMax - foldoutStyle.padding.right - labelSize.x - offset,
                y    = position.y + 2,
                size = labelSize
            };

            int id = GUIUtility.GetControlID(s_FoldoutHeaderHash, FocusType.Keyboard, position);

            if (Event.current.type == EventType.KeyDown && GUIUtility.keyboardControl == id)
            {
                KeyCode kc = Event.current.keyCode;
                if (kc == KeyCode.LeftArrow && foldout || (kc == KeyCode.RightArrow && foldout == false))
                {
                    foldout     = !foldout;
                    GUI.changed = true;
                    Event.current.Use();
                }
            }
            else
            {
                foldout = EditorGUIInternal.DoToggleForward(position, id, foldout, content, foldoutStyle);
            }

            if (additionalLabel != null && Event.current.type == EventType.Repaint && labelSize.x < Screen.width * 0.8f)
            {
                GUI.Label(menuRect, additionalLabel);
                menuRect.x     = 24 + offset;
                menuRect.y    += 1;
                menuRect.width = menuRect.height = 16;
                GUI.DrawTexture(menuRect, background, ScaleMode.ScaleToFit, true, 1, backgroundColor, Vector4.zero, Vector4.one * 2, true);
            }

            return(foldout);
        }
        public static bool BeginFoldoutHeaderGroup(Rect position, bool foldout, GUIContent content, [DefaultValue("EditorStyles.foldoutHeader")] GUIStyle style = null, Action <Rect> menuAction = null, GUIStyle menuIcon = null)
        {
            // Removing the default margin for inspectors
            if (EditorGUIUtility.hierarchyMode)
            {
                position.xMin -= EditorStyles.inspectorDefaultMargins.padding.left - EditorStyles.inspectorDefaultMargins.padding.right;
                position.xMax += EditorStyles.inspectorDefaultMargins.padding.right;
            }

            if (style == null)
            {
                style = EditorStyles.foldoutHeader;
            }
            if (s_FoldoutHeaderGroupActive)
            {
                EditorGUI.HelpBox(position, L10n.Tr("You can't nest Foldout Headers, end it with EndHeaderFoldoutGroup."), MessageType.Error);
                return(false);
            }
            s_FoldoutHeaderGroupActive = true;

            const float iconSize = 16;
            Rect        menuRect = new Rect
            {
                x    = position.xMax - style.padding.right - iconSize,
                y    = position.y + style.padding.top,
                size = Vector2.one * iconSize
            };

            bool menuIconHover  = menuRect.Contains(Event.current.mousePosition);
            bool menuIconActive = (menuIconHover && Event.current.type == EventType.MouseDown && Event.current.button == 0);

            if (menuAction != null && menuIconActive)
            {
                menuAction.Invoke(menuRect);
                Event.current.Use();
            }
            int id = GUIUtility.GetControlID(s_FoldoutHeaderHash, FocusType.Keyboard, position);

            if (Event.current.type == EventType.KeyDown && GUIUtility.keyboardControl == id)
            {
                KeyCode kc = Event.current.keyCode;
                if (kc == KeyCode.LeftArrow && foldout || (kc == KeyCode.RightArrow && foldout == false))
                {
                    foldout     = !foldout;
                    GUI.changed = true;
                    Event.current.Use();
                }
            }
            else
            {
                foldout = EditorGUIInternal.DoToggleForward(position, id, foldout, content, style);
            }

            // Menu icon
            if (menuAction != null && Event.current.type == EventType.Repaint)
            {
                if (menuIcon == null)
                {
                    menuIcon = EditorStyles.foldoutHeaderIcon;
                }

                menuIcon.Draw(menuRect, menuIconHover, menuIconActive, false, false);
            }

            return(foldout);
        }