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
menu.h@0:905736afd672, 2015-01-03 (annotated)
- Committer:
- WiredHome
- Date:
- Sat Jan 03 15:35:20 2015 +0000
- Revision:
- 0:905736afd672
- Child:
- 2:944069915001
Fixed a warning - caused by code-blindness of knowing what it should do and not seeing what it was doing...
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:905736afd672 | 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 | 0:905736afd672 | 91 | /// This creates the instance of the menu, and provides appropriate initialization. |
WiredHome | 0:905736afd672 | 92 | /// |
WiredHome | 0:905736afd672 | 93 | /// @param lcd is a reference to the RA8875 display object. @see RA8875. |
WiredHome | 0:905736afd672 | 94 | /// @param menu is a pointer to the top-level menu list. @see menu_item_t. |
WiredHome | 0:905736afd672 | 95 | /// @param fg is the foreground color for the menu. |
WiredHome | 0:905736afd672 | 96 | /// @param bg is the background color for the menu. |
WiredHome | 0:905736afd672 | 97 | /// @param hl is the highlight color for the menu. |
WiredHome | 0:905736afd672 | 98 | /// |
WiredHome | 0:905736afd672 | 99 | Menu(RA8875 & lcd, menu_item_t * menu, color_t fg = BrightBlue, color_t bg = Black, color_t hl = Red); |
WiredHome | 0:905736afd672 | 100 | |
WiredHome | 0:905736afd672 | 101 | /// Destructure for the menu class. |
WiredHome | 0:905736afd672 | 102 | ~Menu(); |
WiredHome | 0:905736afd672 | 103 | |
WiredHome | 0:905736afd672 | 104 | /// initialize the menu system to get it started. |
WiredHome | 0:905736afd672 | 105 | /// |
WiredHome | 0:905736afd672 | 106 | /// This sets up the RA8875 display and layering for the menu system. |
WiredHome | 0:905736afd672 | 107 | /// |
WiredHome | 0:905736afd672 | 108 | void init(void); |
WiredHome | 0:905736afd672 | 109 | |
WiredHome | 0:905736afd672 | 110 | /// This is the main "run-time" entry point. |
WiredHome | 0:905736afd672 | 111 | /// |
WiredHome | 0:905736afd672 | 112 | /// Every touch should call this and pass in the point and touchcode. |
WiredHome | 0:905736afd672 | 113 | /// If the menu can handle this event, it will indicate so the |
WiredHome | 0:905736afd672 | 114 | /// return code. |
WiredHome | 0:905736afd672 | 115 | /// |
WiredHome | 0:905736afd672 | 116 | /// @param p is the point provided to this method indicating where |
WiredHome | 0:905736afd672 | 117 | /// the last touch was detected. |
WiredHome | 0:905736afd672 | 118 | /// @param touchcode indicates the type of touch, and permits easy |
WiredHome | 0:905736afd672 | 119 | /// handling of touch, held, release events. |
WiredHome | 0:905736afd672 | 120 | /// @returns true if the touch was handled by the menu system. |
WiredHome | 0:905736afd672 | 121 | /// |
WiredHome | 0:905736afd672 | 122 | bool HandledTouch(point_t p, TouchCode_t touchcode); |
WiredHome | 0:905736afd672 | 123 | |
WiredHome | 0:905736afd672 | 124 | /// Force show the menu. |
WiredHome | 0:905736afd672 | 125 | /// |
WiredHome | 0:905736afd672 | 126 | void Show(); |
WiredHome | 0:905736afd672 | 127 | |
WiredHome | 0:905736afd672 | 128 | /// Force hide the menu. |
WiredHome | 0:905736afd672 | 129 | /// |
WiredHome | 0:905736afd672 | 130 | void Hide(); |
WiredHome | 0:905736afd672 | 131 | |
WiredHome | 0:905736afd672 | 132 | /// Indicates if the menu is currently visible. |
WiredHome | 0:905736afd672 | 133 | /// |
WiredHome | 0:905736afd672 | 134 | /// @returns true if the menu is visible. |
WiredHome | 0:905736afd672 | 135 | /// |
WiredHome | 0:905736afd672 | 136 | bool isVisible() { return isShowing; } |
WiredHome | 0:905736afd672 | 137 | |
WiredHome | 0:905736afd672 | 138 | private: |
WiredHome | 0:905736afd672 | 139 | /// This method expands a top-level menu to show the lower. |
WiredHome | 0:905736afd672 | 140 | /// |
WiredHome | 0:905736afd672 | 141 | /// It also will first high any previously open submenu. |
WiredHome | 0:905736afd672 | 142 | /// |
WiredHome | 0:905736afd672 | 143 | /// @param menu is a pointer to the menu to expand if it has child menus. |
WiredHome | 0:905736afd672 | 144 | /// |
WiredHome | 0:905736afd672 | 145 | void Expand(menu_item_t * menu); |
WiredHome | 0:905736afd672 | 146 | void SetColors(color_t fg, color_t bg, color_t hl); |
WiredHome | 0:905736afd672 | 147 | bool _isTouched(menu_item_t * pTraverse, rect_t r, point_t p, menu_item_t ** menuTouched); |
WiredHome | 0:905736afd672 | 148 | bool _GetMenuRect(menu_item_t * pMenu, menu_item_t * needle, rect_t * pRect); |
WiredHome | 0:905736afd672 | 149 | void _ShowMenu_Helper(menu_item_t * pMenu, rect_t r, bool show = true); |
WiredHome | 0:905736afd672 | 150 | void _Rect(rect_t r, color_t c, fill_t fill = NOFILL); |
WiredHome | 0:905736afd672 | 151 | RA8875 & lcd; |
WiredHome | 0:905736afd672 | 152 | menu_item_t * pTopMenu; |
WiredHome | 0:905736afd672 | 153 | menu_item_t * pExpanded; // points to an expanded menu |
WiredHome | 0:905736afd672 | 154 | bool isShowing; |
WiredHome | 0:905736afd672 | 155 | color_t menuForeground; |
WiredHome | 0:905736afd672 | 156 | color_t menuBackground; |
WiredHome | 0:905736afd672 | 157 | color_t menuHighlight; |
WiredHome | 0:905736afd672 | 158 | }; |
WiredHome | 0:905736afd672 | 159 | |
WiredHome | 0:905736afd672 | 160 | #endif // MENU_H |