public override void Update(GameTime gameTime) { if (Game1.GetState == GameState.Menu) { if (KeyboardComponent.KeyPressed(Keys.S)) { NextMenuChoice(); } if (KeyboardComponent.KeyPressed(Keys.W)) { PreviousMenuChoice(); } if (KeyboardComponent.KeyPressed(Keys.Enter)) { try { var selectedChoice = choices.First(c => c.Selected); selectedChoice.ClickAction.Invoke(); } catch { } } int choicesGap = 70; float startY = Game1.window.ClientBounds.Height / 4f; foreach (var choice in choices) { Vector2 size = AssetManager.normalFont.MeasureString(choice.Text); choice.posY = startY; choice.posX = Game1.window.ClientBounds.Width / 2 - size.X / 2; choice.HitBox = new Rectangle((int)choice.posX, (int)choice.posY, (int)size.X, (int)size.Y); startY += choicesGap; } var mouseState = Mouse.GetState(); foreach (var choice in choices) { if (choice.HitBox.Contains(mouseState.X, mouseState.Y)) { choices.ForEach(c => c.Selected = false); choice.Selected = true; if (previousMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed) { choice.ClickAction.Invoke(); } } } previousMouseState = mouseState; base.Update(gameTime); } }
protected override void Initialize() { window = Window; menuComponent = new MenuComponent(this); keyboardComponent = new KeyboardComponent(this); Components.Add(menuComponent); Components.Add(keyboardComponent); base.Initialize(); }