Advertisement
Volatile_Pulse

menu.h

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