Пример #1
0
        /** <summary> Draws the debug menu. </summary> */
        private void DrawMenu(Graphics2D g, DebugMenuItem item, int pathIndex, Point2I position)
        {
            if (pathIndex >= currentPath.Count)
            {
                return;
            }

            int itemWidth         = 32;
            int itemHeight        = 28;
            int offset            = 32;
            int textOffset        = 0;
            int hotkeyOffset      = 0;
            int hotkeyColumnWidth = 0;
            int textColumnWidth   = 0;
            int rightPading       = 24;
            //int padding = 20;
            int hotkeyColumnPadding = 20;

            if (pathIndex == 0)
            {
                offset              = 6;
                rightPading         = 8;
                hotkeyColumnPadding = 0;
            }

            // Measure the width to draw the menu at.
            for (int i = 0; i < item.Items.Count; ++i)
            {
                DebugMenuItem subItem = item.Items[i];

                Rectangle2I r1 = (Rectangle2I)debugMenuFont.MeasureStringBounds(subItem.Text, Align.Left);
                Rectangle2I r2 = (Rectangle2I)debugMenuFont.MeasureStringBounds(subItem.HotKey.Name, Align.Left);
                hotkeyOffset      = GMath.Max(hotkeyOffset, r1.Width + offset + 10);
                itemWidth         = GMath.Max(itemWidth, r1.Width + r2.Width + offset + rightPading);
                textColumnWidth   = GMath.Max(textColumnWidth, r1.Width);
                hotkeyColumnWidth = GMath.Max(hotkeyColumnWidth, r2.Width);
            }
            hotkeyOffset = offset + textColumnWidth + hotkeyColumnPadding;
            textOffset   = offset;
            itemWidth    = offset + textColumnWidth + hotkeyColumnPadding + hotkeyColumnWidth + rightPading;


            // Draw outline.
            Rectangle2I menuRect = new Rectangle2I(position.X, position.Y, itemWidth, itemHeight * item.Items.Count);

            if (pathIndex == 0)
            {
                menuRect.Width  = itemWidth * item.Items.Count;
                menuRect.Height = itemHeight;
            }
            g.DrawRectangle(menuRect, 1.0f, colorOutline);

            // Draw background.
            menuRect.Inflate(-1, -1);
            g.FillRectangle(menuRect, colorBackground);

            // Draw item list.
            for (int i = 0; i < item.Items.Count; ++i)
            {
                Rectangle2I r = new Rectangle2I(position.X, position.Y, itemWidth, itemHeight);

                if (pathIndex == 0)
                {
                    r.Inflate(0, -1);
                    if (i == 0)
                    {
                        r.X     += 1;
                        r.Width -= 1;
                    }
                    if (i == item.Items.Count - 1)
                    {
                        r.Width -= 1;
                    }
                }
                else
                {
                    r.Inflate(-1, 0);
                    if (i == 0)
                    {
                        r.Y      += 1;
                        r.Height -= 1;
                    }
                    if (i == item.Items.Count - 1)
                    {
                        r.Height -= 1;
                    }
                }

                DebugMenuItem subItem = item.Items[i];

                // Draw highlight.
                if (currentPath[pathIndex] == i)
                {
                    Rectangle2F sr = (Rectangle2I)r;
                    sr.Inflate(-2, -2);
                    if (controlMode == MenuControlMode.Keyboard || controlMode == MenuControlMode.GamePad || pathIndex < currentPath.Count - 1)
                    {
                        g.FillRectangle(sr, colorBackgroundHighlight);
                    }
                }
                Point2I ms = (Point2I)Mouse.GetPosition();
                if (r.Contains(ms))
                {
                    mouseHover     = true;
                    mouseHoverItem = subItem;
                    Rectangle2F sr = (Rectangle2I)r;
                    sr.Inflate(-2, -2);
                    if (controlMode == MenuControlMode.Mouse)
                    {
                        g.FillRectangle(sr, colorBackgroundHighlight);
                    }
                }

                // Draw text label.
                string text   = subItem.Text;
                string hotkey = subItem.HotKey.Name;
                g.DrawRealString(debugMenuFont, text, new Point2I(r.Min.X + textOffset, (int)r.Center.Y), Align.Left | Align.Int, colorText);
                g.DrawRealString(debugMenuFont, hotkey, new Point2I(r.Min.X + hotkeyOffset, (int)r.Center.Y), Align.Left | Align.Int, colorHotkey);

                // Draw toggle check.
                if (subItem is ToggleMenuItem)
                {
                    bool     enabled = ((ToggleMenuItem)subItem).IsEnabled;
                    SpriteEx spr     = debugMenuSprites["checkbox_disabled"];
                    if (enabled)
                    {
                        spr = debugMenuSprites["checkbox_enabled"];
                    }
                    if (subItem is RadioButtonMenuItem)
                    {
                        spr = debugMenuSprites["radiobutton_disabled"];
                        if (enabled)
                        {
                            spr = debugMenuSprites["radiobutton_enabled"];
                        }
                    }
                    g.DrawSpriteEx(spr, new Vector2F(r.Min.X + 6, r.Min.Y + 6), colorText);
                }

                // Draw submenu arrow.
                if (item != menu && subItem.Items.Count > 0)
                {
                    g.DrawSpriteEx(debugMenuSprites["submenu_arrow"], new Vector2F(r.Max.X - 18, r.Min.Y + 6), colorArrow);
                }

                // Draw nested menu.
                if (currentPath[pathIndex] == i)
                {
                    Point2I p = position;
                    if (pathIndex == 0)
                    {
                        p.Y += itemHeight - 1;
                    }
                    else
                    {
                        p.X += itemWidth - 1;
                    }

                    DrawMenu(g, subItem, pathIndex + 1, p);
                }

                // Move current position.
                if (pathIndex == 0)
                {
                    position.X += itemWidth;
                }
                else
                {
                    position.Y += itemHeight;
                }
            }
        }