/// <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; }
/// <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; } } }
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> /// When a font changes from the default, longest width of all /// menu items must be calculated again so that all menu items /// share the same width. /// </summary> private void CalculateGreatestWidth() { _longestWidth = 0; for (var i = 1; i < Items.Count; i++) { Items[i].Width += (int)Items[i].Padding.Left + (int)Items[i].Padding.Right + _checkMarkWidth; // adding space for the arrow to denote that a submenu is available. if (AllSubMenuTitles.Contains(Items[i].Header)) { Items[i].Width += 20; } if (_longestWidth <= Items[i].Width) { _longestWidth = Items[i].Width; } } }
/// <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(); } }