/// <summary> /// Called when "SubMenu" state is entered. /// </summary> private void OnEnterSubMenuScreen(object sender, StateEventArgs eventArgs) { // Similar to OnEnterMenuScreen. _subMenuWindow = new Window { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, }; _uiScreen.Children.Add(_subMenuWindow); var stackPanel = new StackPanel { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Bottom, Margin = new Vector4F(150, 0, 0, 200) }; _subMenuWindow.Content = stackPanel; var button1 = new Button { Name = "Item1Button", Content = new TextBlock { Text = "Item 1" }, FocusWhenMouseOver = true, }; var button2 = new Button { Name = "Item2Button", Content = new TextBlock { Text = "Item 2" }, FocusWhenMouseOver = true, }; var button3 = new Button { Name = "Item3Button", Content = new TextBlock { Text = "Item 3" }, FocusWhenMouseOver = true, }; var backButton = new Button { Name = "BackButton", Content = new TextBlock { Text = "Back" }, FocusWhenMouseOver = true, }; backButton.Click += OnBackButtonClicked; stackPanel.Children.Add(button1); stackPanel.Children.Add(button2); stackPanel.Children.Add(button3); stackPanel.Children.Add(backButton); button1.Focus(); // Fade-in the buttons from the right. AnimateFrom(stackPanel.Children, 0, new Vector2F(300, 0)); }
private void AddWindowComponents() { var menuWindow = new Window { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, }; _screen.Children.Add(menuWindow); var stackPanel = new StackPanel { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, Margin = new Vector4F(150, 0, 0, 0), }; menuWindow.Content = stackPanel; var startButton = new Button { Name = "StartButton", Content = new TextBlock { Text = "Start" }, FocusWhenMouseOver = true, }; startButton.Click += OnStartButtonClicked; stackPanel.Children.Add(startButton); startButton.Focus(); _game.ResetElapsedTime(); }
/// <summary> /// Called when "Menu" state is entered. /// </summary> private void OnEnterMenuScreen(object sender, StateEventArgs eventArgs) { // Show a main menu consisting of several buttons. // The user should be able to select individual buttons by using the // D-pad on the gamepad or the arrow keys. Therefore we need to create // a Window. A Window manages the currently selected ("focused") control // and automatically handles focus movement. // In this example the Window is invisible (no chrome) and stretches across // the entire screen. _menuWindow = new Window { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, }; _uiScreen.Children.Add(_menuWindow); // The content of the Window is a vertical StackPanel containing several buttons. var stackPanel = new StackPanel { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Bottom, Margin = new Vector4F(150, 0, 0, 200) }; _menuWindow.Content = stackPanel; // The "Start" button starts the "Game" state. var startButton = new Button { Name = "StartButton", Content = new TextBlock { Text = "Start" }, FocusWhenMouseOver = true, }; startButton.Click += OnStartButtonClicked; // The buttons "Sub menu 1" and "Sub menu 2" show a dummy sub-menu. var subMenu1Button = new Button { Name = "SubMenu1Button", Content = new TextBlock { Text = "Sub-menu 1" }, FocusWhenMouseOver = true, }; subMenu1Button.Click += OnSubMenuButtonClicked; var subMenu2Button = new Button { Name = "SubMenu2Button", Content = new TextBlock { Text = "Sub-menu 2" }, FocusWhenMouseOver = true, }; subMenu2Button.Click += OnSubMenuButtonClicked; // The "Exit" button closes the application. var exitButton = new Button { Name = "ExitButton", Content = new TextBlock { Text = "Exit" }, FocusWhenMouseOver = true, }; exitButton.Click += OnExitButtonClicked; stackPanel.Children.Add(startButton); stackPanel.Children.Add(subMenu1Button); stackPanel.Children.Add(subMenu2Button); stackPanel.Children.Add(exitButton); // By default, the first button should be selected. startButton.Focus(); // Slide the buttons in from the left (off screen) to make things more dynamic. AnimateFrom(stackPanel.Children, 0, new Vector2F(-300, 0)); // The first time initialization of the GUI can take a short time. If we reset the elapsed // time of the XNA game timer, the animation will start a lot smoother. // (This works only if the XNA game uses a variable time step.) Game.ResetElapsedTime(); }