A primitive menu system for touchscreen on the RA8875. This menu is not "finger friendly" and reminds one of the 90's using a Palm.

Dependents:   FRDM_RA8875_mPaint PUB_RA8875_mPaint

Committer:
WiredHome
Date:
Sat Jan 03 17:24:55 2015 +0000
Revision:
3:b59d6f7c1e68
Parent:
2:944069915001
Tiny documentation fix.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:905736afd672 1
WiredHome 0:905736afd672 2 #ifndef MENU_H
WiredHome 0:905736afd672 3 #define MENU_H
WiredHome 0:905736afd672 4
WiredHome 0:905736afd672 5 #include "RA8875.h"
WiredHome 0:905736afd672 6
WiredHome 0:905736afd672 7 #define CANVAS 0
WiredHome 0:905736afd672 8 #define MENUS 1
WiredHome 0:905736afd672 9
WiredHome 0:905736afd672 10 /// A very simple menu system, for use with the RA8875 display library.
WiredHome 0:905736afd672 11 ///
WiredHome 0:905736afd672 12 /// This will seem like a "throwback" to the 90's. As designed, it is
WiredHome 0:905736afd672 13 /// not for "finger" but for stylus, since menu picks are the size of
WiredHome 0:905736afd672 14 /// the selected font. The user could change the font size, and the
WiredHome 0:905736afd672 15 /// menu should resize accordingly.
WiredHome 0:905736afd672 16 ///
WiredHome 0:905736afd672 17 /// The user defines some simple arrays of structures which define
WiredHome 0:905736afd672 18 /// menu picks.
WiredHome 0:905736afd672 19 ///
WiredHome 0:905736afd672 20 /// The menu is created horizontally on the screen, starting in the top-
WiredHome 0:905736afd672 21 /// left.
WiredHome 0:905736afd672 22 ///
WiredHome 0:905736afd672 23 /// Submenus, of which only 1 submenu level is supported, are also
WiredHome 0:905736afd672 24 /// shown horizontally starting directly beneath the top-level (and
WiredHome 0:905736afd672 25 /// 1 char to the right). Care should be taken if it may cause wordwrap,
WiredHome 0:905736afd672 26 /// as this is not supported (and the behavior has not been tested).
WiredHome 0:905736afd672 27 ///
WiredHome 0:905736afd672 28 /// @note When using this menu system, it uses the two-layer mode of the
WiredHome 0:905736afd672 29 /// RA8875 display, which limits the display to those with not more
WiredHome 0:905736afd672 30 /// than 480x272 pixels in size.
WiredHome 0:905736afd672 31 ///
WiredHome 0:905736afd672 32 /// @code
WiredHome 0:905736afd672 33 /// // +----------------------------------------------------+
WiredHome 0:905736afd672 34 /// // | File | Pen | Tools |
WiredHome 0:905736afd672 35 /// // +----------------------------------------------------+ fontheight()
WiredHome 0:905736afd672 36 /// // | |
WiredHome 0:905736afd672 37 /// // | |
WiredHome 0:905736afd672 38 /// // | |
WiredHome 0:905736afd672 39 /// // | |
WiredHome 0:905736afd672 40 /// // | |
WiredHome 0:905736afd672 41 /// // | |
WiredHome 0:905736afd672 42 /// // | |
WiredHome 0:905736afd672 43 /// // | |
WiredHome 0:905736afd672 44 /// // | |
WiredHome 0:905736afd672 45 /// // | |
WiredHome 0:905736afd672 46 /// // | |
WiredHome 0:905736afd672 47 /// // +----------------------------------------------------+
WiredHome 0:905736afd672 48 ///
WiredHome 0:905736afd672 49 /// // +----------------------------------------------------+
WiredHome 0:905736afd672 50 /// // | File | Pen | Tools |
WiredHome 0:905736afd672 51 /// // +----------------------------------------------------+
WiredHome 0:905736afd672 52 /// // || New... | Save... | Save all | Calibrate | Reset | |
WiredHome 0:905736afd672 53 /// // | |
WiredHome 0:905736afd672 54 ///
WiredHome 0:905736afd672 55 /// // +----------------------------------------------------+
WiredHome 0:905736afd672 56 /// // | File | Pen | Tools |
WiredHome 0:905736afd672 57 /// // +----------------------------------------------------+
WiredHome 0:905736afd672 58 /// // | | 1 pix | 2 pix | 4 pix | ... |
WiredHome 2:944069915001 59 /// //
WiredHome 0:905736afd672 60 /// @endcode
WiredHome 0:905736afd672 61 ///
WiredHome 0:905736afd672 62 class Menu
WiredHome 0:905736afd672 63 {
WiredHome 0:905736afd672 64 public:
WiredHome 0:905736afd672 65 /// When menu functions are called, they can return a command
WiredHome 0:905736afd672 66 /// to the menu system to determine what it should do - if
WiredHome 0:905736afd672 67 /// anything.
WiredHome 0:905736afd672 68 ///
WiredHome 0:905736afd672 69 typedef enum
WiredHome 0:905736afd672 70 {
WiredHome 0:905736afd672 71 no_action, ///< take no action on return.
WiredHome 0:905736afd672 72 close_menu, ///< close the menu immediately.
WiredHome 0:905736afd672 73 } post_fnc_action_t;
WiredHome 0:905736afd672 74
WiredHome 0:905736afd672 75 /// Each menu item is a structure as defined here. An array
WiredHome 0:905736afd672 76 /// of these create multple entries on that menu level.
WiredHome 0:905736afd672 77 /// The array must be terminated by a NULL entry.
WiredHome 0:905736afd672 78 ///
WiredHome 0:905736afd672 79 typedef struct menu_item_t
WiredHome 0:905736afd672 80 {
WiredHome 0:905736afd672 81 char * menuText; ///< text to show (keep it brief!)
WiredHome 0:905736afd672 82 post_fnc_action_t (* fncPress)(uint32_t); ///< function to call "on touch", or NULL
WiredHome 0:905736afd672 83 post_fnc_action_t (* fncHeld)(uint32_t); ///< function to call "on held", or NULL
WiredHome 0:905736afd672 84 post_fnc_action_t (* fncRelease)(uint32_t); ///< function to call "on release", or NULL
WiredHome 0:905736afd672 85 uint32_t param; ///< parameter to pass to the function.
WiredHome 0:905736afd672 86 menu_item_t * child; ///< child menu, if this is an upper menu.
WiredHome 0:905736afd672 87 } menu_item_t;
WiredHome 0:905736afd672 88
WiredHome 0:905736afd672 89 /// The constructor for the menu class.
WiredHome 0:905736afd672 90 ///
WiredHome 2:944069915001 91 /// This creates the instance of the menu. The initialization is done
WiredHome 2:944069915001 92 /// later.
WiredHome 2:944069915001 93 ///
WiredHome 2:944069915001 94 /// @code
WiredHome 2:944069915001 95 /// #include "menu.h"
WiredHome 2:944069915001 96 /// ...
WiredHome 2:944069915001 97 /// Menu::menu_item_t menudata[] = {
WiredHome 2:944069915001 98 /// // Text touch held release param childmenu
WiredHome 2:944069915001 99 /// { "File", File, NULL, NULL, 0, file_menu },
WiredHome 2:944069915001 100 /// { "Pen", NULL, NULL, NULL, 0, pen_menu },
WiredHome 2:944069915001 101 /// { "Tools", NULL, NULL, NULL, 0, tools_menu },
WiredHome 2:944069915001 102 /// { "Hide", NULL, NULL, HideMenu, 0, NULL },
WiredHome 2:944069915001 103 /// { NULL, NULL, NULL, NULL, 0, NULL }, // NULL terminator
WiredHome 2:944069915001 104 /// };
WiredHome 2:944069915001 105 ///
WiredHome 2:944069915001 106 /// Menu menu(lcd, menudata);
WiredHome 2:944069915001 107 ///
WiredHome 2:944069915001 108 /// void main() {
WiredHome 2:944069915001 109 /// menu.init();
WiredHome 2:944069915001 110 /// for (;;) {
WiredHome 2:944069915001 111 /// point_t p;
WiredHome 2:944069915001 112 /// TouchCode_t touchcode = lcd.TouchPanelReadable(&p);
WiredHome 2:944069915001 113 ///
WiredHome 2:944069915001 114 /// if (touchcode != no_touch) {
WiredHome 2:944069915001 115 /// bool menuHandledIt = menu.HandledTouch(p, touchcode);
WiredHome 2:944069915001 116 /// if (menuHandledIt) {
WiredHome 2:944069915001 117 /// // menu handled it
WiredHome 2:944069915001 118 /// } else {
WiredHome 2:944069915001 119 /// // user code here for touch in non-menu areas.
WiredHome 2:944069915001 120 /// }
WiredHome 2:944069915001 121 /// }
WiredHome 2:944069915001 122 /// }
WiredHome 3:b59d6f7c1e68 123 /// }
WiredHome 2:944069915001 124 /// @endcode
WiredHome 0:905736afd672 125 ///
WiredHome 0:905736afd672 126 /// @param lcd is a reference to the RA8875 display object. @see RA8875.
WiredHome 0:905736afd672 127 /// @param menu is a pointer to the top-level menu list. @see menu_item_t.
WiredHome 0:905736afd672 128 /// @param fg is the foreground color for the menu.
WiredHome 0:905736afd672 129 /// @param bg is the background color for the menu.
WiredHome 0:905736afd672 130 /// @param hl is the highlight color for the menu.
WiredHome 0:905736afd672 131 ///
WiredHome 0:905736afd672 132 Menu(RA8875 & lcd, menu_item_t * menu, color_t fg = BrightBlue, color_t bg = Black, color_t hl = Red);
WiredHome 0:905736afd672 133
WiredHome 0:905736afd672 134 /// Destructure for the menu class.
WiredHome 0:905736afd672 135 ~Menu();
WiredHome 0:905736afd672 136
WiredHome 0:905736afd672 137 /// initialize the menu system to get it started.
WiredHome 0:905736afd672 138 ///
WiredHome 0:905736afd672 139 /// This sets up the RA8875 display and layering for the menu system.
WiredHome 0:905736afd672 140 ///
WiredHome 0:905736afd672 141 void init(void);
WiredHome 0:905736afd672 142
WiredHome 0:905736afd672 143 /// This is the main "run-time" entry point.
WiredHome 0:905736afd672 144 ///
WiredHome 0:905736afd672 145 /// Every touch should call this and pass in the point and touchcode.
WiredHome 0:905736afd672 146 /// If the menu can handle this event, it will indicate so the
WiredHome 0:905736afd672 147 /// return code.
WiredHome 0:905736afd672 148 ///
WiredHome 0:905736afd672 149 /// @param p is the point provided to this method indicating where
WiredHome 0:905736afd672 150 /// the last touch was detected.
WiredHome 0:905736afd672 151 /// @param touchcode indicates the type of touch, and permits easy
WiredHome 0:905736afd672 152 /// handling of touch, held, release events.
WiredHome 0:905736afd672 153 /// @returns true if the touch was handled by the menu system.
WiredHome 0:905736afd672 154 ///
WiredHome 0:905736afd672 155 bool HandledTouch(point_t p, TouchCode_t touchcode);
WiredHome 0:905736afd672 156
WiredHome 0:905736afd672 157 /// Force show the menu.
WiredHome 0:905736afd672 158 ///
WiredHome 0:905736afd672 159 void Show();
WiredHome 0:905736afd672 160
WiredHome 0:905736afd672 161 /// Force hide the menu.
WiredHome 0:905736afd672 162 ///
WiredHome 0:905736afd672 163 void Hide();
WiredHome 0:905736afd672 164
WiredHome 0:905736afd672 165 /// Indicates if the menu is currently visible.
WiredHome 0:905736afd672 166 ///
WiredHome 0:905736afd672 167 /// @returns true if the menu is visible.
WiredHome 0:905736afd672 168 ///
WiredHome 0:905736afd672 169 bool isVisible() { return isShowing; }
WiredHome 0:905736afd672 170
WiredHome 0:905736afd672 171 private:
WiredHome 0:905736afd672 172 /// This method expands a top-level menu to show the lower.
WiredHome 0:905736afd672 173 ///
WiredHome 0:905736afd672 174 /// It also will first high any previously open submenu.
WiredHome 0:905736afd672 175 ///
WiredHome 0:905736afd672 176 /// @param menu is a pointer to the menu to expand if it has child menus.
WiredHome 0:905736afd672 177 ///
WiredHome 0:905736afd672 178 void Expand(menu_item_t * menu);
WiredHome 0:905736afd672 179 void SetColors(color_t fg, color_t bg, color_t hl);
WiredHome 0:905736afd672 180 bool _isTouched(menu_item_t * pTraverse, rect_t r, point_t p, menu_item_t ** menuTouched);
WiredHome 0:905736afd672 181 bool _GetMenuRect(menu_item_t * pMenu, menu_item_t * needle, rect_t * pRect);
WiredHome 0:905736afd672 182 void _ShowMenu_Helper(menu_item_t * pMenu, rect_t r, bool show = true);
WiredHome 0:905736afd672 183 void _Rect(rect_t r, color_t c, fill_t fill = NOFILL);
WiredHome 0:905736afd672 184 RA8875 & lcd;
WiredHome 0:905736afd672 185 menu_item_t * pTopMenu;
WiredHome 0:905736afd672 186 menu_item_t * pExpanded; // points to an expanded menu
WiredHome 0:905736afd672 187 bool isShowing;
WiredHome 0:905736afd672 188 color_t menuForeground;
WiredHome 0:905736afd672 189 color_t menuBackground;
WiredHome 0:905736afd672 190 color_t menuHighlight;
WiredHome 0:905736afd672 191 };
WiredHome 0:905736afd672 192
WiredHome 0:905736afd672 193 #endif // MENU_H