/// <summary> /// Load a menu into memory by instantiating an instance of it's view /// and populating it with the data from the model passed in. /// </summary> /// <typeparam name="T">The type of menu to load.</typeparam> /// <param name="menu">The menu's model.</param> public void LoadMenu <T>(T menu) where T : class, IMenu { if (menuPresenter != null) { menuPresenter.Unload(); } MenuPresenterAttribute presenterAttribute = typeof(T).GetCustomAttribute <MenuPresenterAttribute>(); //Easy. We know what kind of presenter to use. if (presenterAttribute != null) { //Is the current one what we need? if ((menuPresenter?.GetType() ?? null) != presenterAttribute.Type) { menuPresenter = Activator.CreateInstance(presenterAttribute.Type, this, commandConsole, configContainer) as IMenuPresenter; } } else { throw new Exception(string.Format("No menu presenter attribute found on menu of type: {0}", typeof(T))); } menuPresenter.Load(menuContainer, menu); }
/// <summary> /// Handle console to choose from menuitems and call the right method Play, Load, Quit /// from menuPresenter /// </summary> private void HandleMenuItems() { int optionsPerLine = menuItems.Length; ConsoleKey key; do { Show(); key = Console.ReadKey(true).Key; switch (key) { case ConsoleKey.LeftArrow: { if (currentSelection % optionsPerLine > 0) { currentSelection--; } break; } case ConsoleKey.RightArrow: { if (currentSelection % optionsPerLine < optionsPerLine - 1) { currentSelection++; } break; } case ConsoleKey.UpArrow: { if (currentSelection >= optionsPerLine) { currentSelection -= optionsPerLine; } break; } case ConsoleKey.DownArrow: { if (currentSelection + optionsPerLine < menuItems.Length) { currentSelection += optionsPerLine; } break; } case ConsoleKey.Escape: { menuPresenter.Quit(); break; } } } while (key != ConsoleKey.Enter); Console.CursorVisible = true; switch (currentSelection) { case 0: menuPresenter.Play(new GameView()); break; case 1: menuPresenter.Load(new GameView(), new LoadView()); break; case 2: menuPresenter.Quit(); break; default: break; } }