void LoadMenuItems() { if (menuRectangle == null) { using (var stream = TitleContainer.OpenStream("Content/RoundedRectangle.png")) { menuRectangle = Texture2D.FromStream(GraphicsDevice, stream); } } if (menuRectangleHover == null) { using (var stream = TitleContainer.OpenStream("Content/RoundedRectangleDarkGreen.png")) { menuRectangleHover = Texture2D.FromStream(GraphicsDevice, stream); } } menuSound = Content.Load <SoundEffect>("killenemy"); GameOptionsFile.LoadOptions(); }
void optionsMenuUpdate(GameTime gameTime) { resetButtonHover(); MouseState mouseState = Mouse.GetState(); mouseXY = new Vector2(mouseState.X, mouseState.Y); TouchCollection touchCollection = TouchPanel.GetState(); Vector2 touchXY = new Vector2(0, 0); if (mouseState.LeftButton == ButtonState.Pressed) { touchXY = new Vector2(mouseState.X, mouseState.Y); } else if (touchCollection.Count > 0) { touchXY = new Vector2(touchCollection[0].Position.X, touchCollection[0].Position.Y); } // Convert position of mouse and touch input to rectangles to be able to use intersection methods. Rectangle mouseRectangle = new Rectangle((int)mouseXY.X, (int)mouseXY.Y, 1, 1); Rectangle touchRectangle = new Rectangle((int)touchXY.X, (int)touchXY.Y, 1, 1); // Set rectangles used to layout options menu. int elementPositionY = menuItemSpaceDistance; // Used to position the elements of the menu from top to bottom. Set first element position. Rectangle rectangleGoBack = new Rectangle(menuButtonXOffset, (int)(elementPositionY * menuScaleFactor), 600, 150); elementPositionY += menuRectangle.Height + menuItemSpaceDistance * (int)menuScaleFactor; Rectangle rectangleChangeName = new Rectangle(menuButtonXOffset, (int)(elementPositionY * menuScaleFactor), 600, 150); elementPositionY += menuRectangle.Height + menuItemSpaceDistance * (int)menuScaleFactor; Vector2 stringSize = font.MeasureString("STRING"); // Used to get the height of a string rendered with this font. Used here to match layout in draw functions. elementPositionY += (int)stringSize.Y * (int)menuScaleFactor; Rectangle rectangleSetDifficulty = new Rectangle(menuButtonXOffset, (int)(elementPositionY * menuScaleFactor), 600, 150); elementPositionY += menuRectangle.Height + (int)stringSize.Y; Rectangle rectangleSetTouchControl = new Rectangle(menuButtonXOffset, (int)(elementPositionY * menuScaleFactor), 600, 150); // Logic for UI elements if (bHasEnteredName == false) { #if ANDROID if (androidNameTask == null && !KeyboardInput.IsVisible) { androidNameTask = KeyboardInput.Show("Name", "What's your name?", "Player"); } if (androidNameTask != null && androidNameTask.IsCompleted) { if (androidNameTask.Result != null) { playerName = androidNameTask.Result; bHasEnteredName = true; } } #endif #if !ANDROID if (touchXY.X > 0 || touchXY.Y > 0) // If the user is clicking or tapping anywhere if they are supposed to be typing their name, flash text to prompt user to type name. { bFlashPlayerNamePrompt = true; menuButtonTimer = DateTime.Now.AddSeconds(menuButtonTimeSeconds); // Flash text for this long. } if (kbName == "") { kbNameHandler.Update(); } else { playerName = kbName; bHasEnteredName = true; } bHasTappedButtonLastUpdate = false; #endif } else // Don't let them click/tap anything if they haven't entered their name { if (touchXY.X != 0 && touchXY.Y != 0) // Touch control { if (rectangleGoBack.Intersects(touchRectangle)) { try { GameOptionsFile.SaveOptions(); } catch (Exception ex) { Debug.WriteLine("Unable to save, Error: " + ex.Message); } if (bPlayerRequestedGame) { gameState = GameState.inGame; } else { gameState = GameState.menu; } LittleTiggy.menuSound.Play(); } if (rectangleChangeName.Intersects(touchRectangle) && !bHasTappedButtonLastUpdate && menuButtonTimer.CompareTo(DateTime.Now) < 0) { menuButtonTimer = DateTime.Now.AddSeconds(menuButtonTimeSeconds); LittleTiggy.menuSound.Play(); bHasTappedButtonLastUpdate = true; // Used as sometimes a touch is held for multiple updates but we only want the button to be activated once. bHasEnteredName = false; androidNameTask = null; kbName = ""; kbInput = ""; } if (rectangleSetDifficulty.Intersects(touchRectangle) && !bHasTappedButtonLastUpdate && menuButtonTimer.CompareTo(DateTime.Now) < 0) { menuButtonTimer = DateTime.Now.AddSeconds(menuButtonTimeSeconds); LittleTiggy.menuSound.Play(); bHasTappedButtonLastUpdate = true; // Used as sometimes a touch is held for multiple updates but we only want the button to be activated once. if (gameDifficulty == GameDifficulty.Hard) { gameDifficulty = GameDifficulty.Easy; } else { gameDifficulty = gameDifficulty + 1; } bChangedDifficulty = true; // Used to reset the game if the difficulty has been changed to stop leaderboard cheating. } if (rectangleSetTouchControl.Intersects(touchRectangle) && !bHasTappedButtonLastUpdate && menuButtonTimer.CompareTo(DateTime.Now) < 0) { menuButtonTimer = DateTime.Now.AddSeconds(menuButtonTimeSeconds); LittleTiggy.menuSound.Play(); bHasTappedButtonLastUpdate = true; // Used as sometimes a touch is held for multiple updates but we only want the button to be activated once. if (gameTouchControlMethod == GameTouchControlMethod.Joystick) { gameTouchControlMethod = GameTouchControlMethod.ScreenTap; } else { gameTouchControlMethod = GameTouchControlMethod.Joystick; } } } else { bHasTappedButtonLastUpdate = false; } if (mouseXY.X != 0 && mouseXY.Y != 0) // Mouse control { if (rectangleGoBack.Intersects(mouseRectangle)) { menuButtonHover[0] = true; } if (rectangleChangeName.Intersects(mouseRectangle)) { menuButtonHover[1] = true; } if (rectangleSetDifficulty.Intersects(mouseRectangle)) { menuButtonHover[2] = true; } if (rectangleSetTouchControl.Intersects(mouseRectangle)) { menuButtonHover[3] = true; } } } }