/// <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; }
private void MouseDownUpEvent() { // set bool to toggle check marks if IsCheckable on mouse down. if (IsCheckable) { IsChecked = !IsChecked; } // causes the menu to close when a menu item is clicked, and it is not the head // of the menu. if (!AllSubMenuTitles.Contains(Header) && !AllMenuTitles.Contains(Header)) { MenuVisibilityCount = 0; } }
/// <summary> /// Draws the Menu Item. /// </summary> /// <param name="gameTime"></param> public override void Draw(GameTime gameTime) { base.Draw(gameTime); if (Visible == Visibility.Visible) { if (Items.Count > 0 && !_menuItemVariablesFinalized) { _menuItemVariablesFinalized = true; CalculateGreatestWidth(); SetWidthAndHeight(); } SpriteBatch.Begin(); if (!_transparent) { if (AllMenuTitles.Contains(Header)) { if (MouseEntered) { SpriteBatch.Draw(Pixel, Panel, Color.LightGray * 0.25f); } else { SpriteBatch.Draw(Pixel, Panel, _backgroundColor); } } if (!AllMenuTitles.Contains(Header)) { var opacity = (float)Opacity * 0.45f; // drawing the drop shadow. if (!_dropShadowRectMeasured) { MeasureGhostRect(); } SpriteBatch.Draw(Pixel, _dropShadowRect, Color.Black * opacity); // highlights the hovered menu item. if (MouseEntered || (_subMenuPanel.Contains(MsRect) && Items[0].Visible == Visibility.Visible)) { SpriteBatch.Draw(Pixel, Panel, _backgroundColor); SpriteBatch.Draw(Pixel, Panel, Color.LightGray * 0.35f); // borders the top of the menu item SpriteBatch.Draw(Pixel, _strokePanelTop, _stroke * (float)Opacity); // borders the bottom of the menu item. SpriteBatch.Draw(Pixel, _strokePanelBottom, _stroke * (float)Opacity); // borders the left side of the menu item. SpriteBatch.Draw(Pixel, _strokePanelLeft, _stroke * (float)Opacity); // borders the right side of the menu item. SpriteBatch.Draw(Pixel, _strokePanelRight, _stroke * (float)Opacity); } else { if (!AllMenuTitles.Contains(Header)) { SpriteBatch.Draw(Pixel, Panel, _backgroundColor); SpriteBatch.Draw(Pixel, _strokePanelLeft, _stroke * (float)Opacity); SpriteBatch.Draw(Pixel, _strokePanelRight, _stroke * (float)Opacity); } } } if (AllSubMenuTitles.Contains(Header)) { ArrowRect.X = Panel.X + Panel.Width - (Arrow.Width + 5); ArrowRect.Y = (Panel.Y + (Height / 2)) - (ArrowRect.Height / 2); SpriteBatch.Draw(Arrow, ArrowRect, Color.White * (float)Opacity); } } if (SubMenuOpened) { SpriteBatch.Draw(Pixel, _subMenuPanel, Color.Black); } if (IsEnabled) { SpriteBatch.DrawString(SpriteFont, Text, _textPos, ForegroundColor * (float)Opacity); } else { SpriteBatch.DrawString(SpriteFont, Text, _textPos, ForegroundColor * 0.3f); } if (IsChecked) { MeasureCheckMark(); SpriteBatch.Draw(CheckMark, CheckMarkRect, Color.White * (float)Opacity); } SpriteBatch.End(); } }
/// <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); } } } } }