Library for creating menu systems in displays.
Menu.h@10:af54763d3d21, 2014-07-20 (annotated)
- Committer:
- ollie8
- Date:
- Sun Jul 20 12:41:33 2014 +0000
- Revision:
- 10:af54763d3d21
- Parent:
- 9:7828182dbc9f
Updated docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ollie8 | 0:3f4d33765f10 | 1 | #ifndef MENU_H |
ollie8 | 0:3f4d33765f10 | 2 | #define MENU_H |
ollie8 | 0:3f4d33765f10 | 3 | |
ollie8 | 6:1297ffffb202 | 4 | #include <mbed.h> |
ollie8 | 2:feaf1471cda0 | 5 | |
ollie8 | 7:5bbf520bc14d | 6 | class Menu; |
ollie8 | 7:5bbf520bc14d | 7 | |
ollie8 | 7:5bbf520bc14d | 8 | /** MenuNode represents an entry in a menu. Each menu node is instantiated with |
ollie8 | 7:5bbf520bc14d | 9 | * two callbacks, one for selecting the node and one for entering the node. |
ollie8 | 7:5bbf520bc14d | 10 | * Methods are provided for determining whether the node is selected and |
ollie8 | 2:feaf1471cda0 | 11 | * retrieving the row number set by the containing menu. |
ollie8 | 1:2084d862196c | 12 | */ |
ollie8 | 7:5bbf520bc14d | 13 | class MenuNode { |
ollie8 | 0:3f4d33765f10 | 14 | |
ollie8 | 0:3f4d33765f10 | 15 | private: |
ollie8 | 7:5bbf520bc14d | 16 | typedef void (*EnterMenuNode)(MenuNode*); |
ollie8 | 7:5bbf520bc14d | 17 | void (*entered)(MenuNode*); |
ollie8 | 0:3f4d33765f10 | 18 | bool selected; |
ollie8 | 0:3f4d33765f10 | 19 | int row; |
ollie8 | 7:5bbf520bc14d | 20 | friend class Menu; |
ollie8 | 0:3f4d33765f10 | 21 | public: |
ollie8 | 7:5bbf520bc14d | 22 | |
ollie8 | 7:5bbf520bc14d | 23 | /** Defines a pointer to a functions that is called when the node is selected. |
ollie8 | 7:5bbf520bc14d | 24 | */ |
ollie8 | 7:5bbf520bc14d | 25 | typedef void (*SelectionChange)(MenuNode*); |
ollie8 | 3:ece3d3346330 | 26 | |
ollie8 | 7:5bbf520bc14d | 27 | /** Constructs a new MenuNode object. The constructor takes two function pointers as |
ollie8 | 7:5bbf520bc14d | 28 | * arguments both of which must take a single argument of a MenuNode pointer. |
ollie8 | 6:1297ffffb202 | 29 | * |
ollie8 | 6:1297ffffb202 | 30 | * @code |
ollie8 | 6:1297ffffb202 | 31 | * |
ollie8 | 7:5bbf520bc14d | 32 | * void enterNode(MenuNode *node) { |
ollie8 | 6:1297ffffb202 | 33 | * |
ollie8 | 6:1297ffffb202 | 34 | * } |
ollie8 | 3:ece3d3346330 | 35 | * |
ollie8 | 7:5bbf520bc14d | 36 | * void selectNode(MenuNode *node) { |
ollie8 | 6:1297ffffb202 | 37 | * |
ollie8 | 6:1297ffffb202 | 38 | * } |
ollie8 | 6:1297ffffb202 | 39 | * |
ollie8 | 6:1297ffffb202 | 40 | * main() { |
ollie8 | 7:5bbf520bc14d | 41 | * MenuNode *node = new MenuNode(&enterNode, &selectNode, "name"); |
ollie8 | 6:1297ffffb202 | 42 | * } |
ollie8 | 6:1297ffffb202 | 43 | * |
ollie8 | 6:1297ffffb202 | 44 | * @endcode |
ollie8 | 3:ece3d3346330 | 45 | * |
ollie8 | 7:5bbf520bc14d | 46 | * @param EnterMenuNode A pointer to a callback fuction called when the node is entered. |
ollie8 | 7:5bbf520bc14d | 47 | * @param SelectionChange A pointer to a callback fuction called when the node is selected. |
ollie8 | 7:5bbf520bc14d | 48 | * @param name The name of the MenuNode. |
ollie8 | 3:ece3d3346330 | 49 | */ |
ollie8 | 7:5bbf520bc14d | 50 | MenuNode(EnterMenuNode, SelectionChange, char*); |
ollie8 | 3:ece3d3346330 | 51 | |
ollie8 | 7:5bbf520bc14d | 52 | /** Gets the name of the node. |
ollie8 | 7:5bbf520bc14d | 53 | * @return A char array containing the node name. |
ollie8 | 3:ece3d3346330 | 54 | */ |
ollie8 | 0:3f4d33765f10 | 55 | char* getName(); |
ollie8 | 3:ece3d3346330 | 56 | |
ollie8 | 7:5bbf520bc14d | 57 | /** Enters this instance of MenuNode and ultimatley calls the entered callback function. |
ollie8 | 3:ece3d3346330 | 58 | */ |
ollie8 | 0:3f4d33765f10 | 59 | virtual void enter(); |
ollie8 | 3:ece3d3346330 | 60 | |
ollie8 | 6:1297ffffb202 | 61 | /** Determines wheter tgis instance is selected. |
ollie8 | 3:ece3d3346330 | 62 | * @return true if selected. |
ollie8 | 3:ece3d3346330 | 63 | */ |
ollie8 | 0:3f4d33765f10 | 64 | bool isSelected(); |
ollie8 | 3:ece3d3346330 | 65 | |
ollie8 | 7:5bbf520bc14d | 66 | /** Selects this instance of MenuNode in its containing Menu and called the selected call back function. |
ollie8 | 3:ece3d3346330 | 67 | */ |
ollie8 | 0:3f4d33765f10 | 68 | void select(); |
ollie8 | 3:ece3d3346330 | 69 | |
ollie8 | 7:5bbf520bc14d | 70 | |
ollie8 | 0:3f4d33765f10 | 71 | protected: |
ollie8 | 0:3f4d33765f10 | 72 | char* name; |
ollie8 | 7:5bbf520bc14d | 73 | void (*selection)(MenuNode*); |
ollie8 | 7:5bbf520bc14d | 74 | MenuNode(); |
ollie8 | 0:3f4d33765f10 | 75 | }; |
ollie8 | 0:3f4d33765f10 | 76 | |
ollie8 | 7:5bbf520bc14d | 77 | /** Menu represents a container for a set of MenuNodes. Menu is itself |
ollie8 | 10:af54763d3d21 | 78 | * a menu node meaning that Menu objects can be used as sub menus. |
ollie8 | 2:feaf1471cda0 | 79 | */ |
ollie8 | 7:5bbf520bc14d | 80 | class Menu : public MenuNode { |
ollie8 | 0:3f4d33765f10 | 81 | |
ollie8 | 0:3f4d33765f10 | 82 | private: |
ollie8 | 0:3f4d33765f10 | 83 | typedef void (*EnterMenu)(Menu*); |
ollie8 | 7:5bbf520bc14d | 84 | MenuNode **nodes; |
ollie8 | 0:3f4d33765f10 | 85 | void (*entered)(Menu*); |
ollie8 | 7:5bbf520bc14d | 86 | int selectedNode; |
ollie8 | 7:5bbf520bc14d | 87 | int nodeCount; |
ollie8 | 0:3f4d33765f10 | 88 | public: |
ollie8 | 7:5bbf520bc14d | 89 | |
ollie8 | 7:5bbf520bc14d | 90 | /** Constructs a new menu objects. |
ollie8 | 7:5bbf520bc14d | 91 | * |
ollie8 | 7:5bbf520bc14d | 92 | * @code |
ollie8 | 7:5bbf520bc14d | 93 | * void enterMenu(Menu *menu) { |
ollie8 | 7:5bbf520bc14d | 94 | * |
ollie8 | 7:5bbf520bc14d | 95 | * } |
ollie8 | 7:5bbf520bc14d | 96 | * |
ollie8 | 7:5bbf520bc14d | 97 | * void selectNode(MenuNode *node) { |
ollie8 | 7:5bbf520bc14d | 98 | * |
ollie8 | 7:5bbf520bc14d | 99 | * } |
ollie8 | 7:5bbf520bc14d | 100 | * |
ollie8 | 7:5bbf520bc14d | 101 | * Menu root(&enterMenu, &selectNode, "root", 5); |
ollie8 | 7:5bbf520bc14d | 102 | * |
ollie8 | 7:5bbf520bc14d | 103 | * @endcode |
ollie8 | 7:5bbf520bc14d | 104 | * |
ollie8 | 7:5bbf520bc14d | 105 | * @param EnterMenu A pointer to a functions to be called when the Menu is entered. |
ollie8 | 7:5bbf520bc14d | 106 | * @param SelectionChange A pointer to a function to be called when the Menu is selected. |
ollie8 | 7:5bbf520bc14d | 107 | * @param char* A char array containing the name of the Menu. |
ollie8 | 7:5bbf520bc14d | 108 | * @param int The number of node that this Menu will contain. |
ollie8 | 7:5bbf520bc14d | 109 | */ |
ollie8 | 0:3f4d33765f10 | 110 | Menu(EnterMenu, SelectionChange, char*, int); |
ollie8 | 7:5bbf520bc14d | 111 | |
ollie8 | 9:7828182dbc9f | 112 | /** Clears down all contained MenuNodes*/ |
ollie8 | 9:7828182dbc9f | 113 | ~Menu(); |
ollie8 | 9:7828182dbc9f | 114 | |
ollie8 | 7:5bbf520bc14d | 115 | /** Adds the given node to the Menu. MenuNodes are ordered by the order in which they are added. |
ollie8 | 7:5bbf520bc14d | 116 | * Note that a Menu is also a MenuNode meaning that a menu can bee added as a MenuNode to create a sub Menu. |
ollie8 | 7:5bbf520bc14d | 117 | * |
ollie8 | 7:5bbf520bc14d | 118 | * @code |
ollie8 | 7:5bbf520bc14d | 119 | * Menu root(&enterMenu, &selectNode, "root", 5); |
ollie8 | 7:5bbf520bc14d | 120 | * |
ollie8 | 7:5bbf520bc14d | 121 | * void main() { |
ollie8 | 7:5bbf520bc14d | 122 | * Menu *sub1 = new Menu(&enterMenu, &selectNode, "Settings", 5); |
ollie8 | 7:5bbf520bc14d | 123 | * MenuNode *ipnode = new MenuNode(&enterNode, &setip, "IP Address"); |
ollie8 | 7:5bbf520bc14d | 124 | * MenuNode *netmasknode = new MenuNode(&enterNode, &setmask, "Net mask"); |
ollie8 | 7:5bbf520bc14d | 125 | * sub1->addMenuNode(*ipnode); |
ollie8 | 7:5bbf520bc14d | 126 | * sub1->addMenuNode(*netmasknode); |
ollie8 | 7:5bbf520bc14d | 127 | * root.addMenuNode(*sub1); |
ollie8 | 7:5bbf520bc14d | 128 | * root.enter(); |
ollie8 | 7:5bbf520bc14d | 129 | * } |
ollie8 | 8:4a276e420f73 | 130 | * @endcode |
ollie8 | 7:5bbf520bc14d | 131 | * |
ollie8 | 7:5bbf520bc14d | 132 | * @para MenuNode A reference to the MenuNode to add. |
ollie8 | 7:5bbf520bc14d | 133 | */ |
ollie8 | 7:5bbf520bc14d | 134 | void addMenuNode(MenuNode&); |
ollie8 | 7:5bbf520bc14d | 135 | |
ollie8 | 7:5bbf520bc14d | 136 | /** Gets a pointer to the selected MenuNode. |
ollie8 | 7:5bbf520bc14d | 137 | * @return A pointer to the selected MenuNode. |
ollie8 | 7:5bbf520bc14d | 138 | */ |
ollie8 | 7:5bbf520bc14d | 139 | MenuNode & getSelectedNode(); |
ollie8 | 7:5bbf520bc14d | 140 | |
ollie8 | 7:5bbf520bc14d | 141 | /** Gets the number of MenuNodes contained by this Menu. |
ollie8 | 7:5bbf520bc14d | 142 | * @return The MenuNode count. |
ollie8 | 7:5bbf520bc14d | 143 | */ |
ollie8 | 0:3f4d33765f10 | 144 | int size(); |
ollie8 | 7:5bbf520bc14d | 145 | |
ollie8 | 7:5bbf520bc14d | 146 | /** Enters this implementation of Menu. */ |
ollie8 | 0:3f4d33765f10 | 147 | virtual void enter(); |
ollie8 | 7:5bbf520bc14d | 148 | |
ollie8 | 7:5bbf520bc14d | 149 | /** Moves the selection up to the previous MenuNode if there is one and de-selects the current. */ |
ollie8 | 0:3f4d33765f10 | 150 | void up(); |
ollie8 | 7:5bbf520bc14d | 151 | |
ollie8 | 7:5bbf520bc14d | 152 | /** Moves the selection down to the next node if there is one and de-selects the current. */ |
ollie8 | 0:3f4d33765f10 | 153 | void down(); |
ollie8 | 0:3f4d33765f10 | 154 | }; |
ollie8 | 0:3f4d33765f10 | 155 | |
ollie8 | 0:3f4d33765f10 | 156 | #endif |