/// <summary> /// call this to set the parent to some value (for when the user presses escape /// </summary> /// <param name="p">the parent of this menu choice</param> public void SetParent(MenuChoice p) { m_parent = p; }
/// <summary> /// Constructor. Your graphics device, please. /// </summary> public Menu(Game game) : base(game) { if (game.GraphicsDevice == null) { throw new ArgumentNullException("graphicsDevice"); } // SET THE DEVICE FIRST m_device = game.GraphicsDevice; m_device.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset); showBackChoice = true; backChoiceString = "Return"; m_fontTimes = new BitmapFont("times.xml"); textColor = Color.White; selectColor = Color.Green; verticalSpacing = 10; horizontalSpacing = 10; m_root = new MenuChoice("Menu"); // create a default back node. MenuChoice back = CreateBackNode(); back.SetParent(m_root); m_root.GetChoices().SetLastChoice(back); // set the position last, it relies on some of the above being initialized position = new Vector2(-86, -86); LoadResources(); }
/// <summary> /// Call this function to make sure the menu updates itself. It will /// do nothing if the menu is not visible /// </summary> /// <param name="gameTime">the current game time</param> public override void Update(GameTime gameTime) { if(!visible) return; // check mouse & keyboard KeyboardState kbState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); // Priorities: // // For starters, mouse comes before keyboard. And if a selection event occurs, then // stop processing selection input. This will make the most recent interaction // take priority over others (for example, a mouse sitting on top of a menu choice // when you're trying to use the keyboard) int currentSelection = m_currentNode.GetChoices().GetSelectedIndex(); bool selectionHandled = false; // SELECTION CODE // mouse selection if (mouseState.X != m_prevMouseState.X || mouseState.Y != m_prevMouseState.Y) { // we're in here because the mouse has moved // check for a collision Rectangle mouseRect = new Rectangle(mouseState.X, mouseState.Y, 1, 1); for (int i = 0; i < m_currentNode.count; ++i) { if (mouseRect.Intersects(m_currentNode.GetChoice(i).rect)) { m_currentMouseIntersection = i; // deselection event OnChoiceDeselected(); m_currentNode.GetChoices().SetSelectedIndex(i); selectionHandled = true; // don't let the keyboard do anything for this Update call // kick off selection changed event! OnChoiceSelected(); } if (!selectionHandled) { m_currentMouseIntersection = -1; } } } // keyboard up button if (kbState.IsKeyDown(Keys.Up) && !m_prevKbState.IsKeyDown(Keys.Up) && !selectionHandled) { // deselection event OnChoiceDeselected(); // at the top of the list if (currentSelection - 1 < 0) m_currentNode.GetChoices().SetSelectedIndex(m_currentNode.count - 1); // loop around else m_currentNode.GetChoices().SetSelectedIndex(currentSelection - 1); selectionHandled = true; // set this to true for sanity. // kick off selection changed event! OnChoiceSelected(); } // keyboard down button else if (kbState.IsKeyDown(Keys.Down) && !m_prevKbState.IsKeyDown(Keys.Down)) { // deselection event OnChoiceDeselected(); // at the bottom of the list if (currentSelection == m_currentNode.count - 1) m_currentNode.GetChoices().SetSelectedIndex(0); // loop around else m_currentNode.GetChoices().SetSelectedIndex(currentSelection + 1); selectionHandled = true; // set this to true for sanity. // kick off selection changed event! OnChoiceSelected(); } // keyboard left button if (kbState.IsKeyDown(Keys.Left) && !m_prevKbState.IsKeyDown(Keys.Left) && !selectionHandled) { OnChoiceDeselected(); m_currentNode.GetChoice(currentSelection).MoveSelectionLeft(); OnChoiceSelected(); } // keyboard right button if (kbState.IsKeyDown(Keys.Right) && !m_prevKbState.IsKeyDown(Keys.Right) && !selectionHandled) { OnChoiceDeselected(); m_currentNode.GetChoice(currentSelection).MoveSelectionRight(); OnChoiceSelected(); } // end SELECTION CODE // handle execution if (kbState.IsKeyDown(Keys.Enter) && !m_prevKbState.IsKeyDown(Keys.Enter)) { if (m_currentNode.GetChoice(currentSelection).GetChoiceType() == ChoiceType.Normal) { // handle "back" case if (m_currentNode.GetChoice(currentSelection).text == backChoiceString && m_currentNode != m_root) { m_currentNode = m_currentNode.GetChoice(currentSelection).GetParent(); } else //else, we're at the root, so act normally { // execution event OnChoiceExecuted(); if (m_currentNode.GetChoice(currentSelection).count > 0) { m_currentNode.GetChoice(currentSelection).SetParent(m_currentNode); MenuChoice back = CreateBackNode(); if (showBackChoice) { // give it information to go backwards back.SetParent(m_currentNode); } m_currentNode = m_currentNode.GetChoice(currentSelection); if(showBackChoice) m_currentNode.GetChoices().SetLastChoice(back); m_currentNode.GetChoices().SetSelectedIndex(0); CalculatePositioning(); } } } } // mouse button and we are hovering above something if (mouseState.LeftButton == ButtonState.Pressed && m_currentMouseIntersection != -1) { if (m_currentNode.GetChoice(currentSelection).GetChoiceType() == ChoiceType.Normal) { // handle "back" case if (m_currentNode.GetChoice(currentSelection).text == backChoiceString && m_currentNode != m_root) { m_currentNode = m_currentNode.GetChoice(currentSelection).GetParent(); } else //else, we're at the root, so act normally { // execution event OnChoiceExecuted(); if (m_currentNode.GetChoice(currentSelection).count > 0) { m_currentNode.GetChoice(currentSelection).SetParent(m_currentNode); MenuChoice back = CreateBackNode(); if (showBackChoice) { // give it information to go backwards back.SetParent(m_currentNode); } m_currentNode = m_currentNode.GetChoice(currentSelection); if (showBackChoice) m_currentNode.GetChoices().SetLastChoice(back); m_currentNode.GetChoices().SetSelectedIndex(0); CalculatePositioning(); } } m_currentMouseIntersection = -1; } } m_prevKbState = kbState; m_prevMouseState = mouseState; }
MenuChoice CreateBackNode() { MenuChoice ret = new MenuChoice(backChoiceString); ret.textColor = textColor; ret.selectColor = selectColor; return ret; }
public static int GetDefaultLRChoice(MenuChoice choice) { // in the future, get config data from some config file // for now, default to 0 return 0; }