コード例 #1
0
        /// <summary>
        /// Initializes the menu components.
        /// </summary>
        public void LateInitialize()
        {
            // adding the head of the menu to the list of menus
            AllMenuTitles.Add(Items[0].Header);

            foreach (var t in Items)
            {
                Game.Components.Add(t);
                // Add the child component to the game with the modified parameters.
                if (t.Items.Count > 0)
                {
                    AllSubMenuTitles.Add(t.Header);
                    if (!OpenSubMenuDictionary.ContainsKey(t.Header))
                    {
                        OpenSubMenuDictionary.Add(t.Header, false);
                    }
                }
            }

            Items[0].Width  += (int)Items[0].Padding.Left + (int)Items[0].Padding.Right;
            Items[0].Height += (int)Items[0].Padding.Top + (int)Items[0].Padding.Bottom;
            Items[0].Margin  = new Thickness(Margin.Left, Margin.Top, Margin.Right, Margin.Bottom);

            CalculateGreatestWidth();

            SetWidthAndHeight();

            IsEnabled = true;

            _lateInitialize = true;
        }
コード例 #2
0
        /// <summary>
        /// Loads the children once the grid has been set up.
        /// </summary>
        private void LateInitialize()
        {
            _lateInitialize = true;

            if (Items.Count > 0)
            {
                foreach (var t in Items)
                {
                    Game.Components.Add(t);
                }

                // space for the arrow to draw.
                Width += 20;

                // Add the header to the list of submenus so that its parent
                // menus will not close.
                AllSubMenuTitles.Add(Header);
                if (!OpenSubMenuDictionary.ContainsKey(Header))
                {
                    OpenSubMenuDictionary.Add(Header, false);
                }

                CalculateGreatestWidth();

                SetWidthAndHeight();

                foreach (var t in Items)
                {
                    t.Visible             = Visibility.Hidden;
                    t.HorizontalAlignment = HorizontalAlignment;
                    t.VerticalAlignment   = VerticalAlignment;
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Closes a menu.
        /// </summary>
        private void CloseMenu()
        {
            if (!OpenSubMenuDictionary.ContainsValue(true))
            {
                for (var i = 1; i < Items.Count; i++)
                {
                    Items[i].Visible = Visibility.Hidden;
                }

                _fullMenuOpen = false;
            }
        }
コード例 #4
0
        /// <summary>
        /// Updates the menu.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (!IsEnabled)
            {
                // setting up the menu
                if (!_lateInitialize)
                {
                    LateInitialize();
                }
                else
                {
                    if (_headerVisibilityOn)
                    {
                        // If the menu was disabled while the application is running
                        // this will hide the menu.
                        CloseMenu();
                        Items[0].Visible       = Visibility.Hidden;
                        _headerVisibilityOn    = false;
                        _menuVisibilityCounted = false;
                        MenuVisibilityCount    = 0;
                    }
                }
            }
            else
            {
                // If the menu header was previously hidden because it was
                // disabled.
                if (!_headerVisibilityOn)
                {
                    _headerVisibilityOn = true;
                    Items[0].Visible    = Visibility.Visible;
                }

                // If neither the header nor an open menu contains the mouse
                // close the menu.
                if (!Panel.Contains(MsRect) && !_menuItemPanel.Contains(MsRect))
                {
                    CloseMenu();
                }

                // updating the menu visibility count.  If zero, the user must
                // make a mouse down event in order to reopen any menu.  If
                // greater than zero, the menus will open with a mouse enter
                // event.
                if (!_fullMenuOpen && _menuVisibilityCounted)
                {
                    _menuVisibilityCounted = false;
                    if (MenuVisibilityCount != 0)
                    {
                        MenuVisibilityCount--;
                    }
                }
                else if (_fullMenuOpen && !_menuVisibilityCounted)
                {
                    _menuVisibilityCounted = true;
                    MenuVisibilityCount++;
                }
                else if (MenuVisibilityCount == 0)
                {
                    _menuVisibilityCounted = false;
                    if (!OpenSubMenuDictionary.ContainsValue(true))
                    {
                        CloseMenu();
                        MenuShouldAutoOpen = false;
                    }
                }

                // let this continue to size itself until first mouse over.
                // This prevents problems that occur when the user starts
                // an application with their mouse already over the position
                // of a menu.
                if (!headerSizesSet)
                {
                    SetHeaderWidthAndHeight();
                }

                if (MouseEntered)
                {
                    Background = Brushes.LightGray;
                }
                else if (_fullMenuOpen)
                {
                    Background = Brushes.LightGray;

                    // this stops the sizing from perpetually updating.
                    if (!headerSizesSet)
                    {
                        headerSizesSet = true;
                    }
                }
                else
                {
                    Background          = Brushes.Transparent;
                    Items[0].Background = Brushes.Transparent;
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Updates the menu item.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (_fontFamilyChanged)
            {
                _fontFamilyChanged = false;
                UpdateFontFamily(_fontFamily);
                SpriteFont.Spacing = Spacing;
                RecalculateWidthAndHeight(Header);
                MarginChanged = true;
            }

            if (MarginChanged)
            {
                MarginChanged = false;
                Margin        = new Thickness(Margin.Left, Margin.Top, Margin.Right, Margin.Bottom);
                if (AllMenuTitles.Contains(Header))
                {
                    Panel    = new Rectangle((int)Position.X, (int)Position.Y, (int)SpriteFont.MeasureString(Text).X + (int)Padding.Left + (int)Padding.Right, Height + (int)Padding.Top + (int)Padding.Bottom);
                    _textPos = new Vector2(Position.X + (int)Padding.Left, Position.Y + (int)Padding.Top);
                }
                else
                {
                    Panel    = new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
                    _textPos = new Vector2(Position.X + (int)Padding.Left + _checkMarkWidth, Position.Y + (int)Padding.Top);
                }
            }

            if (!_lateInitialize)
            {
                LateInitialize();
            }

            if (!_headerHeightRecalculated)
            {
                _headerHeightRecalculated = true;
                if (AllMenuTitles.Contains(Header))
                {
                    Panel = new Rectangle((int)Margin.Left, (int)Margin.Top, Width + (int)Padding.Left + (int)Padding.Right, Height);
                }
            }

            if (IsEnabled)
            {
                // opens a sub-menu panel, if it exists.
                if (MouseEntered && Items.Count > 0 && Visible == Visibility.Visible)
                {
                    SubMenuOpened = true;

                    foreach (var t in Items)
                    {
                        t.Visible = Visibility.Visible;
                    }

                    if (!SubMenuKeyAdded)
                    {
                        SubMenuKeyAdded = true;
                        if (OpenSubMenuDictionary.ContainsKey(Header))
                        {
                            OpenSubMenuDictionary.Remove(Header);
                            OpenSubMenuDictionary.Add(Header, true);
                        }
                    }
                }
                else
                {
                    if (!SubMenuOpened)
                    {
                        foreach (var t in Items)
                        {
                            t.Visible = Visibility.Hidden;
                        }
                    }
                    else if (Items.Count > 0 && !_subMenuPanel.Contains(MsRect))
                    {
                        // Check whether the submenu has a submenu.  If so,
                        // don't close the menu.
                        foreach (var xamLiteMenuItem in Items)
                        {
                            if (xamLiteMenuItem.Items.Count > 0 && xamLiteMenuItem.Items[0].Visible == Visibility.Visible)
                            {
                                return;
                            }
                        }

                        SubMenuOpened   = false;
                        SubMenuKeyAdded = false;
                        if (OpenSubMenuDictionary.ContainsKey(Header))
                        {
                            OpenSubMenuDictionary.Remove(Header);
                            OpenSubMenuDictionary.Add(Header, false);
                        }
                    }
                }

                // closes sub-menu panel after a menu item has been selected.
                if (Ms.LeftButton == ButtonState.Pressed)
                {
                    if (Math.Abs(MousePressPosition.X - Ms.X) < 0.01 && Math.Abs(MousePressPosition.Y - Ms.Y) < 0.01)
                    {
                        if (_subMenuPanel.Contains(MsRect))
                        {
                            SubMenuOpened   = false;
                            SubMenuKeyAdded = false;
                            OpenSubMenuDictionary.Remove(Header);
                            OpenSubMenuDictionary.Add(Header, false);
                        }
                    }
                }
            }
        }