/// <summary> /// Gets the command from menu. /// </summary> /// <returns></returns> private string GetCommandFromMenu() { try { // open multiplayer window SingleMenuModel model = new SingleMenuModel(); SingleMenuVM vm = new SingleMenuVM(model); SingleMenu mnu = new SingleMenu(vm); string cmd; mnu.ShowDialog(out cmd); return(cmd); }catch (Exception e) { MessageBox.Show("Error connecting to the server!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } }
/// <summary>Processes a mouse move event</summary> /// <param name="x">The screen-relative x coordinate of the move event</param> /// <param name="y">The screen-relative y coordinate of the move event</param> internal bool ProcessMouseMove(int x, int y) { // if (CurrMenu < 0) { return(false); } // if not in menu or during control customisation or down outside menu area, do nothing if (Game.CurrentInterface != Game.InterfaceType.Menu || isCustomisingControl) { return(false); } // Load the current menu SingleMenu menu = Menus[CurrMenu]; if (menu.TopItem > 1 && y < topItemY && y > menuYmin) { //Item is the scroll up ellipsis menu.Selection = menu.TopItem - 1; return(true); } if (x < topItemY || x > menuXmax || y < menuYmin || y > menuYmax) { return(false); } int item = (y - topItemY) / lineHeight + menu.TopItem; // if the mouse is above a command item, select it if (item >= 0 && item < menu.Items.Length && menu.Items[item] is MenuCommand) { if (item < visibleItems + menu.TopItem + 1) { //Item is a standard menu entry or the scroll down elipsis menu.Selection = item; return(true); } } return(false); }
// // POSITION MENU // /// <summary>Computes the position in the screen of the current menu. /// Also sets the menu size</summary> private void PositionMenu() { // int i; if (CurrMenu < 0 || CurrMenu >= Menus.Length) { return; } SingleMenu menu = Menus[CurrMenu]; // HORIZONTAL PLACEMENT: centre the menu in the main window menuXmin = (LibRender.Screen.Width - menu.Width) / 2; // menu left edge (border excluded) menuXmax = menuXmin + menu.Width; // menu right edge (border excluded) // VERTICAL PLACEMENT: centre the menu in the main window menuYmin = (LibRender.Screen.Height - menu.Height) / 2; // menu top edge (border excluded) menuYmax = menuYmin + menu.Height; // menu bottom edge (border excluded) topItemY = menuYmin; // top edge of top item // assume all items fit in the screen visibleItems = menu.Items.Length; // if there are more items than can fit in the screen height, // (there should be at least room for the menu top border) if (menuYmin < MenuBorderY) { // the number of lines which fit in the screen int numOfLines = (LibRender.Screen.Height - MenuBorderY * 2) / lineHeight; visibleItems = numOfLines - 2; // at least an empty line at the top and at the bottom // split the menu in chunks of 'visibleItems' items // and display the chunk which contains the currently selected item menu.TopItem = menu.Selection - (menu.Selection % visibleItems); visibleItems = menu.Items.Length - menu.TopItem < visibleItems ? // in the last chunk, menu.Items.Length - menu.TopItem : visibleItems; // display remaining items only menuYmin = (LibRender.Screen.Height - numOfLines * lineHeight) / 2; menuYmax = menuYmin + numOfLines * lineHeight; // first menu item is drawn on second line (first line is empty // on first screen and contains an ellipsis on following screens topItemY = menuYmin + lineHeight; } }
// // PROCESS MOUSE EVENTS // /// <summary>Processes a scroll wheel event</summary> /// <param name="Scroll">The delta</param> internal void ProcessMouseScroll(int Scroll) { // Load the current menu SingleMenu menu = Menus[CurrMenu]; if (Math.Abs(Scroll) == Scroll) { //Negative if (menu.TopItem > 0) { menu.TopItem--; } } else { //Positive if (menu.Items.Length - menu.TopItem > visibleItems) { menu.TopItem++; } } }
// // PROCESS MOUSE MOVE EVENTS // /// <summary>Processes a mouse move event</summary> /// <param name="x">The screen-relative x coordinate of the move event</param> /// <param name="y">The screen-relative y coordinate of the move event</param> internal bool ProcessMouseMove(int x, int y) { // if not in menu or during control customisation or down outside menu area, do nothing if (Game.CurrentInterface != Game.InterfaceType.Menu || isCustomisingControl || (x < topItemY || x > menuXmax || y < menuYmin || y > menuYmax)) { return(false); } // locate the menu item under the mouse SingleMenu menu = Menus[CurrMenu]; int item = (y - topItemY) / lineHeight + menu.TopItem; // if the mouse is above a command item, select it if (item >= 0 && item < visibleItems && menu.Items[item] is MenuCommand) { menu.Selection = item; return(true); } return(false); }
// // DRAW MENU // /// <summary>Draws the current menu as a screen overlay</summary> internal void Draw() { int i; if (CurrMenu < 0 || CurrMenu >= Menus.Length) { return; } SingleMenu menu = Menus[CurrMenu]; // overlay background GL.Color4(overlayColor.R, overlayColor.G, overlayColor.B, overlayColor.A); LibRender.Renderer.RenderOverlaySolid(0.0, 0.0, (double)LibRender.Screen.Width, (double)LibRender.Screen.Height); GL.Color4(1.0f, 1.0f, 1.0f, 1.0f); // HORIZONTAL PLACEMENT: centre the menu in the main window int itemLeft = (LibRender.Screen.Width - menu.ItemWidth) / 2; // item left edge // if menu alignment is left, left-align items, otherwise centre them in the screen int itemX = (menu.Align & TextAlignment.Left) != 0 ? itemLeft : LibRender.Screen.Width / 2; int menuBottomItem = menu.TopItem + visibleItems - 1; // draw the menu background GL.Color4(backgroundColor.R, backgroundColor.G, backgroundColor.B, backgroundColor.A); LibRender.Renderer.RenderOverlaySolid(menuXmin - MenuBorderX, menuYmin - MenuBorderY, menuXmax + MenuBorderX, menuYmax + MenuBorderY); // if not starting from the top of the menu, draw a dimmed ellipsis item if (menu.Selection == menu.TopItem - 1 && !isCustomisingControl) { GL.Color4(highlightColor.R, highlightColor.G, highlightColor.B, highlightColor.A); LibRender.Renderer.RenderOverlaySolid(itemLeft - MenuItemBorderX, menuYmin /*-MenuItemBorderY*/, itemLeft + menu.ItemWidth + MenuItemBorderX, menuYmin + em + MenuItemBorderY * 2); } if (menu.TopItem > 0) { LibRender.Renderer.DrawString(MenuFont, "...", new Point(itemX, menuYmin), menu.Align, ColourDimmed, false); } // draw the items int itemY = topItemY; for (i = menu.TopItem; i <= menuBottomItem && i < menu.Items.Length; i++) { if (menu.Items[i] == null) { continue; } if (i == menu.Selection) { // draw a solid highlight rectangle under the text // HACK! the highlight rectangle has to be shifted a little down to match // the text body. OpenGL 'feature'? GL.Color4(highlightColor.R, highlightColor.G, highlightColor.B, highlightColor.A); LibRender.Renderer.RenderOverlaySolid(itemLeft - MenuItemBorderX, itemY /*-MenuItemBorderY*/, itemLeft + menu.ItemWidth + MenuItemBorderX, itemY + em + MenuItemBorderY * 2); // draw the text LibRender.Renderer.DrawString(MenuFont, menu.Items[i].Text, new Point(itemX, itemY), menu.Align, ColourHighlight, false); } else if (menu.Items[i] is MenuCaption) { LibRender.Renderer.DrawString(MenuFont, menu.Items[i].Text, new Point(itemX, itemY), menu.Align, ColourCaption, false); } else { LibRender.Renderer.DrawString(MenuFont, menu.Items[i].Text, new Point(itemX, itemY), menu.Align, ColourNormal, false); } itemY += lineHeight; } if (menu.Selection == menu.TopItem + visibleItems) { GL.Color4(highlightColor.R, highlightColor.G, highlightColor.B, highlightColor.A); LibRender.Renderer.RenderOverlaySolid(itemLeft - MenuItemBorderX, itemY /*-MenuItemBorderY*/, itemLeft + menu.ItemWidth + MenuItemBorderX, itemY + em + MenuItemBorderY * 2); } // if not at the end of the menu, draw a dimmed ellipsis item at the bottom if (i < menu.Items.Length - 1) { LibRender.Renderer.DrawString(MenuFont, "...", new Point(itemX, itemY), menu.Align, ColourDimmed, false); } }
// // PROCESS MENU COMMAND // /// <summary>Processes a user command for the current menu</summary> /// <param name="cmd">The command to apply to the current menu</param> /// <param name="timeElapsed">The time elapsed since previous frame</param> internal void ProcessCommand(Translations.Command cmd, double timeElapsed) { if (CurrMenu < 0) { return; } // MenuBack is managed independently from single menu data if (cmd == Translations.Command.MenuBack) { PopMenu(); return; } SingleMenu menu = Menus[CurrMenu]; if (menu.Selection == SelectionNone) // if menu has no selection, do nothing { return; } switch (cmd) { case Translations.Command.MenuUp: // UP if (menu.Selection > 0 && !(menu.Items[menu.Selection - 1] is MenuCaption)) { menu.Selection--; PositionMenu(); } break; case Translations.Command.MenuDown: // DOWN if (menu.Selection < menu.Items.Length - 1) { menu.Selection++; PositionMenu(); } break; // case Translations.Command.MenuBack: // ESC: managed above // break; case Translations.Command.MenuEnter: // ENTER if (menu.Items[menu.Selection] is MenuCommand) { MenuCommand menuItem = (MenuCommand)menu.Items[menu.Selection]; switch (menuItem.Tag) { // menu management commands case MenuTag.MenuBack: // BACK TO PREVIOUS MENU Menu.instance.PopMenu(); break; case MenuTag.MenuJumpToStation: // TO STATIONS MENU Menu.instance.PushMenu(MenuType.JumpToStation); break; case MenuTag.MenuExitToMainMenu: // TO EXIT MENU Menu.instance.PushMenu(MenuType.ExitToMainMenu); break; case MenuTag.MenuQuit: // TO QUIT MENU Menu.instance.PushMenu(MenuType.Quit); break; case MenuTag.MenuControls: // TO CONTROLS MENU Menu.instance.PushMenu(MenuType.Controls); break; case MenuTag.BackToSim: // OUT OF MENU BACK TO SIMULATION Reset(); Game.PreviousInterface = Game.InterfaceType.Menu; Game.CurrentInterface = Game.InterfaceType.Normal; break; // simulation commands case MenuTag.JumpToStation: // JUMP TO STATION Reset(); TrainManager.JumpTrain(TrainManager.PlayerTrain, menuItem.Data); TrainManager.JumpTFO(); break; case MenuTag.ExitToMainMenu: // BACK TO MAIN MENU Reset(); Program.RestartArguments = Interface.CurrentOptions.GameMode == GameMode.Arcade ? "/review" : ""; MainLoop.Quit = MainLoop.QuitMode.ExitToMenu; break; case MenuTag.Control: // CONTROL CUSTOMIZATION PushMenu(MenuType.Control, ((MenuCommand)menu.Items[menu.Selection]).Data); isCustomisingControl = true; CustomControlIdx = ((MenuCommand)menu.Items[menu.Selection]).Data; break; case MenuTag.Quit: // QUIT PROGRAMME Reset(); MainLoop.Quit = MainLoop.QuitMode.QuitProgram; break; } } break; case Translations.Command.MiscFullscreen: // fullscreen Screen.ToggleFullscreen(); break; case Translations.Command.MiscMute: // mute Program.Sounds.GlobalMute = !Program.Sounds.GlobalMute; Program.Sounds.Update(timeElapsed, Interface.CurrentOptions.SoundModel); break; } }
// // DRAW MENU // /// <summary>Draws the current menu as a screen overlay</summary> internal void Draw() { int i; if (CurrMenu < 0 || CurrMenu >= Menus.Length) { return; } SingleMenu menu = Menus[CurrMenu]; // overlay background Program.Renderer.Rectangle.Draw(null, Vector2.Null, new Vector2(Program.Renderer.Screen.Width, Program.Renderer.Screen.Height), overlayColor); // HORIZONTAL PLACEMENT: centre the menu in the main window int itemLeft = (Program.Renderer.Screen.Width - menu.ItemWidth) / 2; // item left edge // if menu alignment is left, left-align items, otherwise centre them in the screen int itemX = (menu.Align & TextAlignment.Left) != 0 ? itemLeft : Program.Renderer.Screen.Width / 2; int menuBottomItem = menu.TopItem + visibleItems - 1; // draw the menu background Program.Renderer.Rectangle.Draw(null, new Vector2(menuXmin - MenuBorderX, menuYmin - MenuBorderY), new Vector2(menuXmax - menuXmin + 2.0f * MenuBorderX, menuYmax - menuYmin + 2.0f * MenuBorderY), backgroundColor); // if not starting from the top of the menu, draw a dimmed ellipsis item if (menu.Selection == menu.TopItem - 1 && !isCustomisingControl) { Program.Renderer.Rectangle.Draw(null, new Vector2(itemLeft - MenuItemBorderX, menuYmin /*-MenuItemBorderY*/), new Vector2(menu.ItemWidth + MenuItemBorderX, em + MenuItemBorderY * 2), highlightColor); } if (menu.TopItem > 0) { Program.Renderer.OpenGlString.Draw(MenuFont, "...", new Point(itemX, menuYmin), menu.Align, ColourDimmed, false); } // draw the items int itemY = topItemY; for (i = menu.TopItem; i <= menuBottomItem && i < menu.Items.Length; i++) { if (menu.Items[i] == null) { continue; } if (i == menu.Selection) { // draw a solid highlight rectangle under the text // HACK! the highlight rectangle has to be shifted a little down to match // the text body. OpenGL 'feature'? Program.Renderer.Rectangle.Draw(null, new Vector2(itemLeft - MenuItemBorderX, itemY /*-MenuItemBorderY*/), new Vector2(menu.ItemWidth + 2.0f * MenuItemBorderX, em + MenuItemBorderY * 2), highlightColor); // draw the text Program.Renderer.OpenGlString.Draw(MenuFont, menu.Items[i].Text, new Point(itemX, itemY), menu.Align, ColourHighlight, false); } else if (menu.Items[i] is MenuCaption) { Program.Renderer.OpenGlString.Draw(MenuFont, menu.Items[i].Text, new Point(itemX, itemY), menu.Align, ColourCaption, false); } else { Program.Renderer.OpenGlString.Draw(MenuFont, menu.Items[i].Text, new Point(itemX, itemY), menu.Align, ColourNormal, false); } itemY += lineHeight; } if (menu.Selection == menu.TopItem + visibleItems) { Program.Renderer.Rectangle.Draw(null, new Vector2(itemLeft - MenuItemBorderX, itemY /*-MenuItemBorderY*/), new Vector2(menu.ItemWidth + 2.0f * MenuItemBorderX, em + MenuItemBorderY * 2), highlightColor); } // if not at the end of the menu, draw a dimmed ellipsis item at the bottom if (i < menu.Items.Length - 1) { Program.Renderer.OpenGlString.Draw(MenuFont, "...", new Point(itemX, itemY), menu.Align, ColourDimmed, false); } }