Advertisement
Volatile_Pulse

menu2.h

Jun 18th, 2012
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.39 KB | None | 0 0
  1. /******************************************************************************
  2.  *                                                                            *
  3.  *                            Volatile Programming                            *
  4.  *                                   Menu2.h                                  *
  5.  *                                                                            *
  6.  ******************************************************************************
  7.  *                                                                            *
  8.  * By: Volatile Pulse                                                         *
  9.  * What: Menu Class v2                                                        *
  10.  * Date: 06.18.2012                                                           *
  11.  *                                                                            *
  12.  ******************************************************************************
  13.  * Use:                                                                       *
  14.  *    // Define Empty Menu with defaults                                      *
  15.  *      // No Title                                                           *
  16.  *      // No Selectors                                                       *
  17.  *      // Menu in the Top Left Corner                                        *
  18.  *    Menu MenuExample                                                        *
  19.  *                                                                            *
  20.  *    // Define Menu with Title FooBar and .. Left and Right Selectors        *
  21.  *    // and a location of the Bottom Right Corner                            *
  22.  *    Menu MenuExample("FooBar", "..", "..", BOTTOM, RIGHT)                   *
  23.  *                                                                            *
  24.  *    // Set Title as "MenuExample Title"                                     *
  25.  *    MenuExample.SetTitle("MenuExample Title")                               *
  26.  *                                                                            *
  27.  *    // Set Left Selector to == and Right Selector to !!                     *
  28.  *    MenuExample.SetSelectors("==", "!!")                                    *
  29.  *                                                                            *
  30.  *    // Set vertical location to center, and horizontal location to center   *
  31.  *    MenuExample.SetLocation(CENTER, CENTER)                                 *
  32.  *                                                                            *
  33.  *    // Set Wrap Around Option (Default is true)                             *
  34.  *    MenuExample.Wrap(false)                                                 *
  35.  *                                                                            *
  36.  *    // Add Option My Option to the end of the menu                          *
  37.  *    MenuExample.AddOption("My Option")                                      *
  38.  *                                                                            *
  39.  *    // Add Option My Option to the end of the menu                          *
  40.  *    // and calls myFunc when selected                                       *
  41.  *    MenuExample.AddOption("My Option", myFunc)                              *
  42.  *                                                                            *
  43.  *    // Set Option number 1 to Hello World                                   *
  44.  *    MenuExample.SetOption(1, "Hello World")                                 *
  45.  *                                                                            *
  46.  *    // Set Option number 1 to Hello World                                   *
  47.  *    // and sets the menu option call FooBar                                 *
  48.  *    MenuExample.SetOption(1, "Hello World", FooBar)                         *
  49.  *                                                                            *
  50.  *    // Returns the user's last selection                                    *
  51.  *    MenuExample.LastSelection()                                             *
  52.  *                                                                            *
  53.  *    // Returns the size of the menu from how many options are in it         *
  54.  *    MenuExample.Size()                                                      *
  55.  *                                                                            *
  56.  *    // Play MenuExample                                                     *
  57.  *    // Use the arrow keys on the keyboard and enter to navigate the menu    *
  58.  *    MenuExample.Play()                                                      *
  59.  *                                                                            *
  60.  ******************************************************************************/
  61. // TODO (Volatile Pulse#9#): Allow Options with a value, On/Off, 1-10, any strings.
  62. // TODO (Volatile Pulse#9#): Allow multiple colors within each option.
  63.  
  64. #ifndef _VP_MENU_H_
  65. #define _VP_MENU_H_
  66.  
  67. #ifdef _WIN32
  68.  
  69. #include <functional>
  70. #include <iostream>
  71. #include <vector>
  72. #include <string>
  73.  
  74. #include "header.h"
  75.  
  76. // Define Menu Positions
  77. enum position {LEFT, CENTER, RIGHT, TOP, BOTTOM};
  78.  
  79. class Menu {
  80.    public:
  81.       // Define Empty Menu with a Title and set Left and Right Selectors
  82.       Menu (std::string title = "",
  83.             std::string leftSelector = "",
  84.             std::string rightSelector = "",
  85.             position verticalPosition = TOP,
  86.             position horizontalPosition = LEFT) {
  87.          SetTitle (title);
  88.          SetSelectors (leftSelector, rightSelector);
  89.          SetLocation (verticalPosition, horizontalPosition);
  90.          Wrap (true);
  91.          iLastSelection = 1;
  92.       }
  93.  
  94.       // Set Title for the Menu
  95.       void SetTitle (std::string title) {
  96.          strMenuTitle = title;
  97.       }
  98.  
  99.       // Set Left and Right Selectors
  100.       void SetSelectors (std::string leftSelector, std::string rightSelector) {
  101.          strLeftSelector = leftSelector;
  102.          strRightSelector = rightSelector;
  103.       }
  104.  
  105.       // Set Menu Location
  106.       void SetLocation (position verticalPosition = TOP, position horizontalPosition = LEFT) {
  107.          posVPosition = verticalPosition;
  108.          posHPosition = horizontalPosition;
  109.       }
  110.  
  111.       // Allow Option Selection to return from bottom/top to top/bottom
  112.       void Wrap (bool wrapAround = true) {
  113.          bWrapAround = wrapAround;
  114.       }
  115.  
  116.       // Add new Option
  117.       void AddOption (std::string option) {
  118.          vMenuOptions.push_back (option);
  119.          vMenuFunctions.resize (vMenuOptions.size());
  120.       }
  121.  
  122.       // Add new Option with a function
  123.       void AddOption (std::string option, std::function<void ()> func) {
  124.          vMenuOptions.push_back (option);
  125.          vMenuFunctions.push_back (func);
  126.       }
  127.  
  128.       // Set Option x to "text"
  129.       void SetOption (unsigned int number, std::string option);
  130.  
  131.       // Set Option x to "text" with a function
  132.       void SetOption (unsigned int number, std::string option, std::function<void ()> func);
  133.  
  134.       // Allow Option Selection to return from bottom/top to top/bottom
  135.       unsigned int LastSelection() {
  136.          return iLastSelection;
  137.       }
  138.  
  139.       // Allow Option Selection to return from bottom/top to top/bottom
  140.       int Size() {
  141.          return vMenuOptions.size();
  142.       }
  143.  
  144.       // Display menu with an option already highlighted
  145.       void Play ();
  146.    private:
  147.       // Private function to Draw the menu
  148.       void Draw();
  149.       // Define a string to hold the left selector
  150.       std::string strMenuTitle;
  151.       // Define a string to hold the left selector
  152.       std::string strLeftSelector;
  153.       // Define a string to hold the right selector
  154.       std::string strRightSelector;
  155.       // Define a position to hold the vertical position
  156.       position posVPosition;
  157.       // Define a position to hold the horizontal position
  158.       position posHPosition;
  159.       // Define a vactor for storing the option strings
  160.       std::vector<std::string> vMenuOptions;
  161.       // Define a vector for storing the option functions
  162.       std::vector<std::function <void ()>> vMenuFunctions;
  163.       // Define an unsigned int to hold the last selected option
  164.       unsigned int iLastSelection;
  165.       // Define wrap around option
  166.       bool bWrapAround;
  167. };
  168.  
  169. void Menu::SetOption (unsigned int number, std::string option) {
  170.    // Resize vMenuOptions to allow option at position
  171.    if (vMenuOptions.size() < number) {
  172.       vMenuOptions.resize (number, "");
  173.       vMenuFunctions.resize (number);
  174.    }
  175.  
  176.    // Assign the new option
  177.    vMenuOptions[ (number - 1)] = option;
  178. }
  179.  
  180. void Menu::SetOption (unsigned int number, std::string option, std::function<void ()> func) {
  181.    // Resize vMenuOptions to allow option at position
  182.    if (vMenuOptions.size() < number) {
  183.       vMenuOptions.resize (number, "");
  184.       vMenuFunctions.resize (number);
  185.    }
  186.  
  187.    // Assign the new option
  188.    vMenuOptions[ (number - 1)] = option;
  189.    // Assign the new function
  190.    vMenuFunctions[(number - 1)] = func;
  191. }
  192.  
  193. void Menu::Draw () {
  194.    // Clear the screen
  195.    VP_ClearScreen();
  196.  
  197.    // Draw Title
  198.    if (!strMenuTitle.empty() ) {
  199.  
  200.       // Print Title
  201.       VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - strMenuTitle.length() ) :
  202.                     (posHPosition == CENTER) ? ( (79 - strMenuTitle.length() ) / 2) :
  203.                     /*(posHPosition == LEFT) ?*/ strLeftSelector.length() ),
  204.                   ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  205.                     (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  206.                     /*(posVPosition == TOP) ?*/ 0) );
  207.       std::cout << strMenuTitle;
  208.  
  209.       // Print Seperator
  210.       VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - strMenuTitle.length() ) :
  211.                     (posHPosition == CENTER) ? ( (79 - strMenuTitle.length() ) / 2) :
  212.                     /*(posHPosition == LEFT) ?*/ strLeftSelector.length() ),
  213.                   ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  214.                     (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  215.                     /*(posVPosition == TOP) ?*/ 0) + 1);
  216.       for (unsigned int i = 0; i < strMenuTitle.length(); i ++)
  217.          std::cout << "-";
  218.    }
  219.  
  220.    // Draw Options
  221.    for (unsigned int i = 0; i < vMenuOptions.size(); i ++) {
  222.       // Output Menu Option
  223.       VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[i].length() ) :
  224.                     (posHPosition == CENTER) ? ( (79 - vMenuOptions[i].length() ) / 2) :
  225.                     /*(posHPosition == LEFT) ?*/ strLeftSelector.length() ),
  226.                   ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  227.                     (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  228.                     /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? i : (i + 2) ) );
  229.       std::cout << vMenuOptions[i];
  230.    }
  231. }
  232.  
  233. void Menu::Play () {
  234.    // Draw the menu
  235.    Draw();
  236.  
  237.    // Define Variables
  238.    int iKeyPress;
  239.  
  240.    // Handle User Input until ENTER is Pressed
  241.    do {
  242.       // Draw Left Selector
  243.       VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[ (iLastSelection - 1)].length() - strLeftSelector.length() ) :
  244.                     (posHPosition == CENTER) ? ( (79 - vMenuOptions[ (iLastSelection - 1)].length() ) / 2 - strLeftSelector.length() ) :
  245.                     /*(posHPosition == LEFT) ?*/ 0),
  246.                   ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  247.                     (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  248.                     /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
  249.       std::cout << strLeftSelector;
  250.  
  251.       // Draw Right Selector
  252.       VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() ) :
  253.                     (posHPosition == CENTER) ? ( (79 + vMenuOptions[ (iLastSelection - 1)].length() ) / 2) :
  254.                     /*(posHPosition == LEFT) ?*/ strLeftSelector.length() + vMenuOptions[ (iLastSelection - 1)].length() ),
  255.                   ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  256.                     (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  257.                     /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
  258.       std::cout << strRightSelector;
  259.  
  260.       iKeyPress = VP_GetCh();
  261.  
  262.       // Handle User Input
  263.       switch (iKeyPress) {
  264.             // User Pressed Up
  265.          case VK_UP:
  266.             // Clear Left Selector
  267.             VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[ (iLastSelection - 1)].length() - strLeftSelector.length() ) :
  268.                           (posHPosition == CENTER) ? ( (79 - vMenuOptions[ (iLastSelection - 1)].length() ) / 2 - strLeftSelector.length() ) :
  269.                           /*(posHPosition == LEFT) ?*/ 0),
  270.                         ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  271.                           (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  272.                           /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
  273.             for (unsigned int i = 0; i < strLeftSelector.length(); i ++)
  274.                std::cout << " ";
  275.  
  276.             // Clear Right Selector
  277.             VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() ) :
  278.                           (posHPosition == CENTER) ? ( (79 + vMenuOptions[ (iLastSelection - 1)].length() ) / 2) :
  279.                           /*(posHPosition == LEFT) ?*/ strLeftSelector.length() + vMenuOptions[ (iLastSelection - 1)].length() ),
  280.                         ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  281.                           (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  282.                           /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
  283.             for (unsigned int i = 0; i < strRightSelector.length(); i ++)
  284.                std::cout << " ";
  285.  
  286.             // Set iLastSelection and allow for loop around
  287.             iLastSelection = ( (iLastSelection == 1) ? ( (bWrapAround) ? vMenuOptions.size() : 1) : (iLastSelection - 1) );
  288.             break;
  289.  
  290.             // User Pressed Down
  291.          case VK_DOWN:
  292.             // Clear Left Selector
  293.             VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() - vMenuOptions[ (iLastSelection - 1)].length() - strLeftSelector.length() ) :
  294.                           (posHPosition == CENTER) ? ( (79 - vMenuOptions[ (iLastSelection - 1)].length() ) / 2 - strLeftSelector.length() ) :
  295.                           /*(posHPosition == LEFT) ?*/ 0),
  296.                         ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  297.                           (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  298.                           /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
  299.             for (unsigned int i = 0; i < strLeftSelector.length(); i ++)
  300.                std::cout << " ";
  301.  
  302.             // Clear Right Selector
  303.             VP_GoToXY ( ( (posHPosition == RIGHT) ? (79 - strRightSelector.length() ) :
  304.                           (posHPosition == CENTER) ? ( (79 + vMenuOptions[ (iLastSelection - 1)].length() ) / 2) :
  305.                           /*(posHPosition == LEFT) ?*/ strLeftSelector.length() + vMenuOptions[ (iLastSelection - 1)].length() ),
  306.                         ( (posVPosition == BOTTOM) ? (25 - Size() - 2) :
  307.                           (posVPosition == CENTER) ? ( (25 - Size() - 2) / 2) :
  308.                           /*(posVPosition == TOP) ?*/ 0) + ( (strMenuTitle.empty() ) ? (iLastSelection - 1) : (iLastSelection + 1) ) );
  309.             for (unsigned int i = 0; i < strRightSelector.length(); i ++)
  310.                std::cout << " ";
  311.  
  312.             // Set iLastSelection and allow for loop around
  313.             iLastSelection = ( (iLastSelection == vMenuOptions.size() ) ? ( (bWrapAround) ? 1 : vMenuOptions.size() ) : (iLastSelection + 1) );
  314.             break;
  315.  
  316.             // User Pressed Enter
  317.          case VK_RETURN:
  318.             if(vMenuFunctions[(iLastSelection - 1)]) {
  319.                VP_ClearScreen();
  320.                vMenuFunctions[(iLastSelection - 1)]();
  321.             }
  322.             Draw();
  323.       }
  324.    } while (vMenuFunctions[(iLastSelection - 1)] || (iKeyPress != VK_RETURN));
  325. }
  326.  
  327. #endif
  328.  
  329. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement