Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * *
- * Volatile Programming *
- * Menu2.h *
- * *
- ******************************************************************************
- * *
- * By: Volatile Pulse *
- * What: Menu Class v2 *
- * Date: 06.18.2012 *
- * *
- ******************************************************************************
- * Use: *
- * // Define Empty Menu with defaults *
- * // No Title *
- * // No Selectors *
- * // Menu in the Top Left Corner *
- * Menu MenuExample *
- * *
- * // Define Menu with Title FooBar and .. Left and Right Selectors *
- * // and a location of the Bottom Right Corner *
- * Menu MenuExample("FooBar", "..", "..", BOTTOM, RIGHT) *
- * *
- * // Set Title as "MenuExample Title" *
- * MenuExample.SetTitle("MenuExample Title") *
- * *
- * // Set Left Selector to == and Right Selector to !! *
- * MenuExample.SetSelectors("==", "!!") *
- * *
- * // Set vertical location to center, and horizontal location to center *
- * MenuExample.SetLocation(CENTER, CENTER) *
- * *
- * // Set Wrap Around Option (Default is true) *
- * MenuExample.Wrap(false) *
- * *
- * // Add Option My Option to the end of the menu *
- * MenuExample.AddOption("My Option") *
- * *
- * // Add Option My Option to the end of the menu *
- * // and calls myFunc when selected *
- * MenuExample.AddOption("My Option", myFunc) *
- * *
- * // Set Option number 1 to Hello World *
- * MenuExample.SetOption(1, "Hello World") *
- * *
- * // Set Option number 1 to Hello World *
- * // and sets the menu option call FooBar *
- * MenuExample.SetOption(1, "Hello World", FooBar) *
- * *
- * // Returns the user's last selection *
- * MenuExample.LastSelection() *
- * *
- * // Returns the size of the menu from how many options are in it *
- * MenuExample.Size() *
- * *
- * // Play MenuExample *
- * // Use the arrow keys on the keyboard and enter to navigate the menu *
- * MenuExample.Play() *
- * *
- ******************************************************************************/
- // TODO (Volatile Pulse#9#): Allow Options with a value, On/Off, 1-10, any strings.
- // TODO (Volatile Pulse#9#): Allow multiple colors within each option.
- #ifndef _VP_MENU_H_
- #define _VP_MENU_H_
- #ifdef _WIN32
- #include <functional>
- #include <iostream>
- #include <vector>
- #include <string>
- #include "header.h"
- // Define Menu Positions
- enum position {LEFT, CENTER, RIGHT, TOP, BOTTOM};
- class Menu {
- public:
- // Define Empty Menu with a Title and set Left and Right Selectors
- Menu (std::string title = "",
- std::string leftSelector = "",
- std::string rightSelector = "",
- position verticalPosition = TOP,
- position horizontalPosition = LEFT) {
- SetTitle (title);
- SetSelectors (leftSelector, rightSelector);
- SetLocation (verticalPosition, horizontalPosition);
- Wrap (true);
- iLastSelection = 1;
- }
- // Set Title for the Menu
- void SetTitle (std::string title) {
- strMenuTitle = title;
- }
- // Set Left and Right Selectors
- void SetSelectors (std::string leftSelector, std::string rightSelector) {
- strLeftSelector = leftSelector;
- strRightSelector = rightSelector;
- }
- // Set Menu Location
- void SetLocation (position verticalPosition = TOP, position horizontalPosition = LEFT) {
- posVPosition = verticalPosition;
- posHPosition = horizontalPosition;
- }
- // Allow Option Selection to return from bottom/top to top/bottom
- void Wrap (bool wrapAround = true) {
- bWrapAround = wrapAround;
- }
- // Add new Option
- void AddOption (std::string option) {
- vMenuOptions.push_back (option);
- vMenuFunctions.resize (vMenuOptions.size());
- }
- // Add new Option with a function
- void AddOption (std::string option, std::function<void ()> func) {
- vMenuOptions.push_back (option);
- vMenuFunctions.push_back (func);
- }
- // Set Option x to "text"
- void SetOption (unsigned int number, std::string option);
- // Set Option x to "text" with a function
- void SetOption (unsigned int number, std::string option, std::function<void ()> func);
- // Allow Option Selection to return from bottom/top to top/bottom
- unsigned int LastSelection() {
- return iLastSelection;
- }
- // Allow Option Selection to return from bottom/top to top/bottom
- int Size() {
- return vMenuOptions.size();
- }
- // Display menu with an option already highlighted
- void Play ();
- private:
- // Private function to Draw the menu
- void Draw();
- // Define a string to hold the left selector
- std::string strMenuTitle;
- // Define a string to hold the left selector
- std::string strLeftSelector;
- // Define a string to hold the right selector
- std::string strRightSelector;
- // Define a position to hold the vertical position
- position posVPosition;
- // Define a position to hold the horizontal position
- position posHPosition;
- // Define a vactor for storing the option strings
- std::vector<std::string> vMenuOptions;
- // Define a vector for storing the option functions
- std::vector<std::function <void ()>> vMenuFunctions;
- // Define an unsigned int to hold the last selected option
- unsigned int iLastSelection;
- // Define wrap around option
- bool bWrapAround;
- };
- void Menu::SetOption (unsigned int number, std::string option) {
- // Resize vMenuOptions to allow option at position
- if (vMenuOptions.size() < number) {
- vMenuOptions.resize (number, "");
- vMenuFunctions.resize (number);
- }
- // Assign the new option
- vMenuOptions[ (number - 1)] = option;
- }
- void Menu::SetOption (unsigned int number, std::string option, std::function<void ()> func) {
- // Resize vMenuOptions to allow option at position
- if (vMenuOptions.size() < number) {
- vMenuOptions.resize (number, "");
- vMenuFunctions.resize (number);
- }
- // Assign the new option
- vMenuOptions[ (number - 1)] = option;
- // Assign the new function
- vMenuFunctions[(number - 1)] = func;
- }
- void Menu::Draw () {
- // Clear the screen
- VP_ClearScreen();
- // Draw Title
- if (!strMenuTitle.empty() ) {
- // Print Title
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - strMenuTitle.length() ) :
- (posHPosition == CENTER) ? ( (79 - strMenuTitle.length() ) / 2) :
- /*(posHPosition == LEFT) ?*/ strLeftSelector.length() ),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) );
- std::cout << strMenuTitle;
- // Print Seperator
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - strMenuTitle.length() ) :
- (posHPosition == CENTER) ? ( (79 - strMenuTitle.length() ) / 2) :
- /*(posHPosition == LEFT) ?*/ strLeftSelector.length() ),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + 1);
- for (unsigned int i = 0; i < strMenuTitle.length(); i ++)
- std::cout << "-";
- }
- // Draw Options
- for (unsigned int i = 0; i < vMenuOptions.size(); i ++) {
- // Output Menu Option
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[i].length() ) :
- (posHPosition == CENTER) ? ( (79 - vMenuOptions[i].length() ) / 2) :
- /*(posHPosition == LEFT) ?*/ strLeftSelector.length() ),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? i : (i + 2) ) );
- std::cout << vMenuOptions[i];
- }
- }
- void Menu::Play () {
- // Draw the menu
- Draw();
- // Define Variables
- int iKeyPress;
- // Handle User Input until ENTER is Pressed
- do {
- // Draw Left Selector
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[ (iLastSelection - 1)].length() - strLeftSelector.length() ) :
- (posHPosition == CENTER) ? ( (79 - vMenuOptions[ (iLastSelection - 1)].length() ) / 2 - strLeftSelector.length() ) :
- /*(posHPosition == LEFT) ?*/ 0),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
- std::cout << strLeftSelector;
- // Draw Right Selector
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() ) :
- (posHPosition == CENTER) ? ( (79 + vMenuOptions[ (iLastSelection - 1)].length() ) / 2) :
- /*(posHPosition == LEFT) ?*/ strLeftSelector.length() + vMenuOptions[ (iLastSelection - 1)].length() ),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
- std::cout << strRightSelector;
- iKeyPress = VP_GetCh();
- // Handle User Input
- switch (iKeyPress) {
- // User Pressed Up
- case VK_UP:
- // Clear Left Selector
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[ (iLastSelection - 1)].length() - strLeftSelector.length() ) :
- (posHPosition == CENTER) ? ( (79 - vMenuOptions[ (iLastSelection - 1)].length() ) / 2 - strLeftSelector.length() ) :
- /*(posHPosition == LEFT) ?*/ 0),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
- for (unsigned int i = 0; i < strLeftSelector.length(); i ++)
- std::cout << " ";
- // Clear Right Selector
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() ) :
- (posHPosition == CENTER) ? ( (79 + vMenuOptions[ (iLastSelection - 1)].length() ) / 2) :
- /*(posHPosition == LEFT) ?*/ strLeftSelector.length() + vMenuOptions[ (iLastSelection - 1)].length() ),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
- for (unsigned int i = 0; i < strRightSelector.length(); i ++)
- std::cout << " ";
- // Set iLastSelection and allow for loop around
- iLastSelection = ( (iLastSelection == 1) ? ( (bWrapAround) ? vMenuOptions.size() : 1) : (iLastSelection - 1) );
- break;
- // User Pressed Down
- case VK_DOWN:
- // Clear Left Selector
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[ (iLastSelection - 1)].length() - strLeftSelector.length() ) :
- (posHPosition == CENTER) ? ( (79 - vMenuOptions[ (iLastSelection - 1)].length() ) / 2 - strLeftSelector.length() ) :
- /*(posHPosition == LEFT) ?*/ 0),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
- for (unsigned int i = 0; i < strLeftSelector.length(); i ++)
- std::cout << " ";
- // Clear Right Selector
- VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() ) :
- (posHPosition == CENTER) ? ( (79 + vMenuOptions[ (iLastSelection - 1)].length() ) / 2) :
- /*(posHPosition == LEFT) ?*/ strLeftSelector.length() + vMenuOptions[ (iLastSelection - 1)].length() ),
- ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
- (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
- /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
- for (unsigned int i = 0; i < strRightSelector.length(); i ++)
- std::cout << " ";
- // Set iLastSelection and allow for loop around
- iLastSelection = ( (iLastSelection == vMenuOptions.size() ) ? ( (bWrapAround) ? 1 : vMenuOptions.size() ) : (iLastSelection + 1) );
- break;
- // User Pressed Enter
- case VK_RETURN:
- if(vMenuFunctions[(iLastSelection - 1)]) {
- VP_ClearScreen();
- vMenuFunctions[(iLastSelection - 1)]();
- }
- Draw();
- }
- } while (vMenuFunctions[(iLastSelection - 1)] || (iKeyPress != VK_RETURN));
- }
- #endif
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement