SHOW:
|
|
- or go back to the newest paste.
| 1 | /****************************************************************************** | |
| 2 | * * | |
| 3 | * Volatile Programming * | |
| 4 | - | * Menu2.h * |
| 4 | + | * Menu_Example2.cpp * |
| 5 | * * | |
| 6 | ****************************************************************************** | |
| 7 | * * | |
| 8 | * By: Volatile Pulse * | |
| 9 | - | * What: Menu Class v2 * |
| 9 | + | * What: An example to demonstrate the different features of the Menu2 class * |
| 10 | * Date: 06.18.2012 * | |
| 11 | * * | |
| 12 | ******************************************************************************/ | |
| 13 | - | * Use: * |
| 13 | + | |
| 14 | - | * // Define Empty Menu with defaults * |
| 14 | + | #include "\lib\menu2.h" |
| 15 | - | * // No Title * |
| 15 | + | #include "\lib\header.h" |
| 16 | - | * // No Selectors * |
| 16 | + | |
| 17 | - | * // Menu in the Top Left Corner * |
| 17 | + | // Define Calc Functions |
| 18 | - | * Menu MenuExample * |
| 18 | + | void AddMe(int x, int y) { std::cout << x << " + " << y; VP_GetCh(); }
|
| 19 | void SubtractMe(int x, int y) { std::cout << x << " - " << y; VP_GetCh(); }
| |
| 20 | - | * // Define Menu with Title FooBar and .. Left and Right Selectors * |
| 20 | + | void MultiplyMe() { std::cout << "You chose to do some multiplication"; VP_GetCh(); }
|
| 21 | - | * // and a location of the Bottom Right Corner * |
| 21 | + | void DivideMe() { std::cout << "You chose to do some division"; VP_GetCh(); }
|
| 22 | - | * Menu MenuExample("FooBar", "..", "..", BOTTOM, RIGHT) *
|
| 22 | + | |
| 23 | // Define Game Functions | |
| 24 | - | * // Set Title as "MenuExample Title" * |
| 24 | + | void Tetris() { std::cout << "You chose to play Tetris"; VP_GetCh(); }
|
| 25 | - | * MenuExample.SetTitle("MenuExample Title") *
|
| 25 | + | void TTT() { std::cout << "You chose to play Tic-Tac-Toe"; VP_GetCh(); }
|
| 26 | void Battleship() { std::cout << "You chose to play Battleship"; VP_GetCh(); }
| |
| 27 | - | * // Set Left Selector to == and Right Selector to !! * |
| 27 | + | void Minesweeper() { std::cout << "You chose to play Minesweeper"; VP_GetCh(); }
|
| 28 | - | * MenuExample.SetSelectors("==", "!!") *
|
| 28 | + | |
| 29 | // Define AboutMe Function | |
| 30 | - | * // Set vertical location to center, and horizontal location to center * |
| 30 | + | void AboutMe() {
|
| 31 | - | * MenuExample.SetLocation(CENTER, CENTER) * |
| 31 | + | std::cout << "This is the latest update in the Menu Class\n\n" |
| 32 | << "New features include passing functions to the menu options\n\n" | |
| 33 | - | * // Set Wrap Around Option (Default is true) * |
| 33 | + | << "Currently Requires some C++11 support, specifically:\n" |
| 34 | - | * MenuExample.Wrap(false) * |
| 34 | + | << "\t#include <functional>"; |
| 35 | VP_GetCh(); | |
| 36 | - | * // Add Option My Option to the end of the menu * |
| 36 | + | |
| 37 | - | * MenuExample.AddOption("My Option") *
|
| 37 | + | |
| 38 | int main() {
| |
| 39 | - | * // Add Option My Option to the end of the menu * |
| 39 | + | // Disable Console Cursor |
| 40 | - | * // and calls myFunc when selected * |
| 40 | + | VP_SetConsoleCursor(false); |
| 41 | - | * MenuExample.AddOption("My Option", myFunc) *
|
| 41 | + | |
| 42 | // Define Calculator Menu | |
| 43 | - | * // Set Option number 1 to Hello World * |
| 43 | + | Menu CalcMenu("Calculator", "-> ", " <-", CENTER, CENTER);
|
| 44 | - | * MenuExample.SetOption(1, "Hello World") * |
| 44 | + | // Using std::bind to pass values to AddMe |
| 45 | CalcMenu.AddOption("Addition", std::bind(AddMe, 4, 5));
| |
| 46 | - | * // Set Option number 1 to Hello World * |
| 46 | + | // Using a lambda function to call Subtract me with values |
| 47 | - | * // and sets the menu option call FooBar * |
| 47 | + | CalcMenu.AddOption("Subtraction", [] () { SubtractMe(9, 5); });
|
| 48 | - | * MenuExample.SetOption(1, "Hello World", FooBar) * |
| 48 | + | // Standard function call |
| 49 | CalcMenu.AddOption("Multiply", MultiplyMe);
| |
| 50 | - | * // Returns the user's last selection * |
| 50 | + | CalcMenu.AddOption("Divide", DivideMe);
|
| 51 | - | * MenuExample.LastSelection() * |
| 51 | + | CalcMenu.AddOption("Main Menu");
|
| 52 | ||
| 53 | - | * // Returns the size of the menu from how many options are in it * |
| 53 | + | // Define Games Menu |
| 54 | - | * MenuExample.Size() * |
| 54 | + | Menu GameMenu("Games", "-> ", " <-", CENTER, CENTER);
|
| 55 | GameMenu.AddOption("Tetris", Tetris);
| |
| 56 | - | * // Play MenuExample * |
| 56 | + | // Using Lambda to call TTT |
| 57 | - | * // Use the arrow keys on the keyboard and enter to navigate the menu * |
| 57 | + | GameMenu.AddOption("Tic-Tac-Toe", [] () { TTT(); } );
|
| 58 | - | * MenuExample.Play() * |
| 58 | + | GameMenu.AddOption("Battleship", Battleship);
|
| 59 | GameMenu.AddOption("Minesweeper", Minesweeper);
| |
| 60 | GameMenu.AddOption("Main Menu");
| |
| 61 | - | // TODO (Volatile Pulse#9#): Allow Options with a value, On/Off, 1-10, any strings. |
| 61 | + | |
| 62 | - | // TODO (Volatile Pulse#9#): Allow multiple colors within each option. |
| 62 | + | // Define Main Menu |
| 63 | Menu MainMenu("Menu v2 Example", "-> ", " <-", CENTER, CENTER);
| |
| 64 | - | #ifndef _VP_MENU_H_ |
| 64 | + | // Must use std::bind or lambda to "Play" another menu |
| 65 | - | #define _VP_MENU_H_ |
| 65 | + | MainMenu.AddOption("Calculator", std::bind(&Menu::Play, std::ref(CalcMenu)));
|
| 66 | // Notice the &: It is required to call class members | |
| 67 | - | #ifdef _WIN32 |
| 67 | + | MainMenu.AddOption("Games", [&] () { GameMenu.Play(); } );
|
| 68 | MainMenu.AddOption("About Me", AboutMe);
| |
| 69 | - | #include <functional> |
| 69 | + | MainMenu.AddOption("Exit");
|
| 70 | - | #include <iostream> |
| 70 | + | |
| 71 | - | #include <vector> |
| 71 | + | MainMenu.Play(); |
| 72 | - | #include <string> |
| 72 | + | |
| 73 | return 0; | |
| 74 | - | #include "header.h" |
| 74 | + | } |