Browse Source

Simplify edit menu items

The `edit` part of menu items displaying values is not needed. Menu edit types can be modeled on sub-menus.
Scott Lahteine 4 years ago
parent
commit
719615a6b6
2 changed files with 48 additions and 39 deletions
  1. 13
    12
      Marlin/src/lcd/menu/menu.cpp
  2. 35
    27
      Marlin/src/lcd/menu/menu.h

+ 13
- 12
Marlin/src/lcd/menu/menu.cpp View File

@@ -68,11 +68,11 @@ uint8_t screen_history_depth = 0;
68 68
 bool screen_changed;
69 69
 
70 70
 // Value Editing
71
-PGM_P MenuItemBase::editLabel;
72
-void* MenuItemBase::editValue;
73
-int32_t MenuItemBase::minEditValue, MenuItemBase::maxEditValue;
74
-screenFunc_t MenuItemBase::callbackFunc;
75
-bool MenuItemBase::liveEdit;
71
+PGM_P MenuEditItemBase::editLabel;
72
+void* MenuEditItemBase::editValue;
73
+int32_t MenuEditItemBase::minEditValue, MenuEditItemBase::maxEditValue;
74
+screenFunc_t MenuEditItemBase::callbackFunc;
75
+bool MenuEditItemBase::liveEdit;
76 76
 
77 77
 // Prevent recursion into screen handlers
78 78
 bool no_reentry = false;
@@ -131,18 +131,19 @@ void MenuItem_gcode::action(PGM_P const, PGM_P const pgcode) { queue.inject_P(pg
131 131
  *
132 132
  *   bool MenuItem_int3::_edit();
133 133
  *   void MenuItem_int3::edit(); // edit int16_t (interactively)
134
- *   void MenuItem_int3::action_edit(PGM_P const pstr, int16_t * const ptr, const int16_t minValue, const int16_t maxValue, const screenFunc_t callback = null, const bool live = false);
134
+ *   void MenuItem_int3::action(PGM_P const pstr, int16_t * const ptr, const int16_t minValue, const int16_t maxValue, const screenFunc_t callback = null, const bool live = false);
135 135
  *
136 136
  * You can then use one of the menu macros to present the edit interface:
137 137
  *   EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
138 138
  *
139 139
  * This expands into a more primitive menu item:
140
- *   MENU_ITEM_VARIANT(int3, _edit, MSG_SPEED, PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
140
+ *  _MENU_ITEM_P(int3, false, PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
141 141
  *
142 142
  * ...which calls:
143
- *       MenuItem_int3::action_edit(PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
143
+ *       MenuItem_int3::action(plabel, &feedrate_percentage, 10, 999)
144
+ *       draw_menu_item_int3(encoderLine == _thisItemNr, _lcdLineNr, plabel, &feedrate_percentage, 10, 999)
144 145
  */
145
-void MenuItemBase::edit(strfunc_t strfunc, loadfunc_t loadfunc) {
146
+void MenuEditItemBase::edit(strfunc_t strfunc, loadfunc_t loadfunc) {
146 147
   #if ENABLED(TOUCH_BUTTONS)
147 148
     ui.repeat_delay = BUTTON_DELAY_EDIT;
148 149
   #endif
@@ -157,7 +158,7 @@ void MenuItemBase::edit(strfunc_t strfunc, loadfunc_t loadfunc) {
157 158
   }
158 159
 }
159 160
 
160
-void MenuItemBase::init(PGM_P const el, void * const ev, const int32_t minv, const int32_t maxv, const uint16_t ep, const screenFunc_t cs, const screenFunc_t cb, const bool le) {
161
+void MenuEditItemBase::init(PGM_P const el, void * const ev, const int32_t minv, const int32_t maxv, const uint16_t ep, const screenFunc_t cs, const screenFunc_t cb, const bool le) {
161 162
   ui.save_previous_screen();
162 163
   ui.refresh();
163 164
   editLabel = el;
@@ -170,7 +171,7 @@ void MenuItemBase::init(PGM_P const el, void * const ev, const int32_t minv, con
170 171
   liveEdit = le;
171 172
 }
172 173
 
173
-#define DEFINE_MENU_EDIT_ITEM(NAME) template class TMenuItem<MenuItemInfo_##NAME>
174
+#define DEFINE_MENU_EDIT_ITEM(NAME) template class TMenuEditItem<MenuEditItemInfo_##NAME>
174 175
 
175 176
 DEFINE_MENU_EDIT_ITEM(percent);     // 100%       right-justified
176 177
 DEFINE_MENU_EDIT_ITEM(int3);        // 123, -12   right-justified
@@ -191,7 +192,7 @@ DEFINE_MENU_EDIT_ITEM(float52sign); // +123.45
191 192
 DEFINE_MENU_EDIT_ITEM(long5);       // 12345      right-justified
192 193
 DEFINE_MENU_EDIT_ITEM(long5_25);    // 12345      right-justified (25 increment)
193 194
 
194
-void MenuItem_bool::action_edit(PGM_P pstr, bool *ptr, screenFunc_t callback) {
195
+void MenuItem_bool::action(PGM_P pstr, bool *ptr, screenFunc_t callback) {
195 196
   UNUSED(pstr); *ptr ^= true; ui.refresh();
196 197
   if (callback) (*callback)();
197 198
 }

+ 35
- 27
Marlin/src/lcd/menu/menu.h View File

@@ -42,7 +42,7 @@ bool printer_busy();
42 42
 ////////////////////////////////////////////
43 43
 
44 44
 #define DECLARE_MENU_EDIT_TYPE(TYPE, NAME, STRFUNC, SCALE) \
45
-  struct MenuItemInfo_##NAME { \
45
+  struct MenuEditItemInfo_##NAME { \
46 46
     typedef TYPE type_t; \
47 47
     static constexpr float scale = SCALE; \
48 48
     static inline char* strfunc(const float value) { return STRFUNC((TYPE) value); } \
@@ -111,14 +111,14 @@ FORCE_INLINE void draw_menu_item_edit_P(const bool sel, const uint8_t row, PGM_P
111 111
 ////////////////////////////////////////////
112 112
 
113 113
 #define _DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(TYPE, NAME, STRFUNC) \
114
-  FORCE_INLINE void draw_menu_item_edit_##NAME (const bool sel, const uint8_t row, PGM_P const pstr, TYPE * const data, ...) { \
114
+  FORCE_INLINE void draw_menu_item_##NAME (const bool sel, const uint8_t row, PGM_P const pstr, TYPE * const data, ...) { \
115 115
     DRAW_MENU_ITEM_SETTING_EDIT_GENERIC(STRFUNC(*(data))); \
116 116
   } \
117
-  FORCE_INLINE void draw_menu_item_edit_accessor_##NAME (const bool sel, const uint8_t row, PGM_P const pstr, PGM_P const, TYPE (*pget)(), void (*)(TYPE), ...) { \
117
+  FORCE_INLINE void draw_menu_item_accessor_##NAME (const bool sel, const uint8_t row, PGM_P const pstr, PGM_P const, TYPE (*pget)(), void (*)(TYPE), ...) { \
118 118
     DRAW_MENU_ITEM_SETTING_EDIT_GENERIC(STRFUNC(pget())); \
119 119
   } \
120 120
   typedef void NAME##_void
121
-#define DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(NAME) _DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(MenuItemInfo_##NAME::type_t, NAME, MenuItemInfo_##NAME::strfunc)
121
+#define DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(NAME) _DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(MenuEditItemInfo_##NAME::type_t, NAME, MenuEditItemInfo_##NAME::strfunc)
122 122
 
123 123
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(percent);          // 100%       right-justified
124 124
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(int3);             // 123, -12   right-justified
@@ -139,8 +139,8 @@ DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(float52sign);      // +123.45
139 139
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(long5);            // 12345      right-justified
140 140
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(long5_25);         // 12345      right-justified (25 increment)
141 141
 
142
-#define draw_menu_item_edit_bool(sel, row, pstr, data, ...)           DRAW_BOOL_SETTING(sel, row, pstr, data)
143
-#define draw_menu_item_edit_accessor_bool(sel, row, pstr, pget, pset) DRAW_BOOL_SETTING(sel, row, pstr, data)
142
+#define draw_menu_item_bool(sel, row, pstr, data, ...)           DRAW_BOOL_SETTING(sel, row, pstr, data)
143
+#define draw_menu_item_accessor_bool(sel, row, pstr, pget, pset) DRAW_BOOL_SETTING(sel, row, pstr, data)
144 144
 
145 145
 ////////////////////////////////////////////
146 146
 /////////////// Menu Actions ///////////////
@@ -176,11 +176,12 @@ class MenuItem_function {
176 176
 /////////// Menu Editing Actions ///////////
177 177
 ////////////////////////////////////////////
178 178
 
179
-class MenuItemBase {
179
+// Edit items use long integer encoder units
180
+class MenuEditItemBase {
180 181
   private:
181 182
     static PGM_P editLabel;
182 183
     static void *editValue;
183
-    static int32_t minEditValue, maxEditValue;
184
+    static int32_t minEditValue, maxEditValue;  // Encoder value range
184 185
     static screenFunc_t callbackFunc;
185 186
     static bool liveEdit;
186 187
   protected:
@@ -191,7 +192,7 @@ class MenuItemBase {
191 192
 };
192 193
 
193 194
 template<typename NAME>
194
-class TMenuItem : MenuItemBase {
195
+class TMenuEditItem : MenuEditItemBase {
195 196
   private:
196 197
     typedef typename NAME::type_t type_t;
197 198
     static inline float unscale(const float value)    { return value * (1.0f / NAME::scale);  }
@@ -199,16 +200,23 @@ class TMenuItem : MenuItemBase {
199 200
     static void load(void *ptr, const int32_t value)  { *((type_t*)ptr) = unscale(value);     }
200 201
     static char* to_string(const int32_t value)       { return NAME::strfunc(unscale(value)); }
201 202
   public:
202
-    static void action_edit(PGM_P const pstr, type_t * const ptr, const type_t minValue, const type_t maxValue, const screenFunc_t callback=nullptr, const bool live=false) {
203
-      // Make sure minv and maxv fit within int16_t
204
-      const int32_t minv = _MAX(scale(minValue), INT16_MIN),
205
-                    maxv = _MIN(scale(maxValue), INT16_MAX);
203
+    static void action(
204
+      PGM_P const pstr,                     // Edit label
205
+      type_t * const ptr,                   // Value pointer
206
+      const type_t minValue,                // Value range
207
+      const type_t maxValue,
208
+      const screenFunc_t callback=nullptr,  // Value update callback
209
+      const bool live=false                 // Callback during editing
210
+    ) {
211
+      // Make sure minv and maxv fit within int32_t
212
+      const int32_t minv = _MAX(scale(minValue), INT32_MIN),
213
+                    maxv = _MIN(scale(maxValue), INT32_MAX);
206 214
       init(pstr, ptr, minv, maxv - minv, scale(*ptr) - minv, edit, callback, live);
207 215
     }
208
-    static void edit() { MenuItemBase::edit(to_string, load); }
216
+    static void edit() { MenuEditItemBase::edit(to_string, load); }
209 217
 };
210 218
 
211
-#define DECLARE_MENU_EDIT_ITEM(NAME) typedef TMenuItem<MenuItemInfo_##NAME> MenuItem_##NAME;
219
+#define DECLARE_MENU_EDIT_ITEM(NAME) typedef TMenuEditItem<MenuEditItemInfo_##NAME> MenuItem_##NAME;
212 220
 
213 221
 DECLARE_MENU_EDIT_ITEM(percent);
214 222
 DECLARE_MENU_EDIT_ITEM(int3);
@@ -231,7 +239,7 @@ DECLARE_MENU_EDIT_ITEM(long5_25);
231 239
 
232 240
 class MenuItem_bool {
233 241
   public:
234
-    static void action_edit(PGM_P const pstr, bool* ptr, const screenFunc_t callbackFunc=nullptr);
242
+    static void action(PGM_P const pstr, bool* ptr, const screenFunc_t callbackFunc=nullptr);
235 243
 };
236 244
 
237 245
 ////////////////////////////////////////////
@@ -289,8 +297,8 @@ class MenuItem_bool {
289 297
 /**
290 298
  * MENU_ITEM generates draw & handler code for a menu item, potentially calling:
291 299
  *
292
- *   draw_menu_item_<type>[_variant](sel, row, label, arg3...)
293
- *   MenuItem_<type>::action[_variant](arg3...)
300
+ *   draw_menu_item_<type>(sel, row, label, arg3...)
301
+ *   MenuItem_<type>::action(arg3...)
294 302
  *
295 303
  * Examples:
296 304
  *   BACK_ITEM(MSG_WATCH)
@@ -304,21 +312,21 @@ class MenuItem_bool {
304 312
  *     MenuItem_function::action(lcd_sdcard_pause)
305 313
  *
306 314
  *   EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
307
- *     draw_menu_item_edit_int3(sel, row, PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
308
- *     MenuItem_int3::action_edit(PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
315
+ *     draw_menu_item_int3(sel, row, PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
316
+ *     MenuItem_int3::action(PSTR(MSG_SPEED), &feedrate_percentage, 10, 999)
309 317
  *
310 318
  */
311
-#define _MENU_ITEM_VARIANT_P(TYPE, VARIANT, USE_MULTIPLIER, PLABEL, V...) do {  \
319
+#define _MENU_ITEM_P(TYPE, USE_MULTIPLIER, PLABEL, V...) do {  \
312 320
     _skipStatic = false;                                          \
313 321
     if (_menuLineNr == _thisItemNr) {                             \
314 322
       PGM_P const plabel = PLABEL;                                \
315 323
       if (encoderLine == _thisItemNr && ui.use_click()) {         \
316 324
         _MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER);              \
317
-        MenuItem_##TYPE ::action ## VARIANT(plabel, ##V);         \
325
+        MenuItem_##TYPE ::action(plabel, ##V);                    \
318 326
         if (screen_changed) return;                               \
319 327
       }                                                           \
320 328
       if (ui.should_draw())                                       \
321
-        draw_menu_item ## VARIANT ## _ ## TYPE                    \
329
+        draw_menu_item_ ## TYPE                                   \
322 330
           (encoderLine == _thisItemNr, _lcdLineNr, plabel, ##V);  \
323 331
     }                                                             \
324 332
   ++_thisItemNr;                                                  \
@@ -346,10 +354,10 @@ class MenuItem_bool {
346 354
 
347 355
 #define STATIC_ITEM(LABEL, V...) STATIC_ITEM_P(PSTR(LABEL), ##V)
348 356
 
349
-#define MENU_ITEM_P(TYPE, PLABEL, V...)   _MENU_ITEM_VARIANT_P(TYPE,      , false, PLABEL,      ##V)
350
-#define MENU_ITEM(TYPE, LABEL, V...)      _MENU_ITEM_VARIANT_P(TYPE,      , false, PSTR(LABEL), ##V)
351
-#define EDIT_ITEM(TYPE, LABEL, V...)      _MENU_ITEM_VARIANT_P(TYPE, _edit, false, PSTR(LABEL), ##V)
352
-#define EDIT_ITEM_FAST(TYPE, LABEL, V...) _MENU_ITEM_VARIANT_P(TYPE, _edit,  true, PSTR(LABEL), ##V)
357
+#define MENU_ITEM_P(TYPE, PLABEL, V...)   _MENU_ITEM_P(TYPE, false, PLABEL,      ##V)
358
+#define MENU_ITEM(TYPE, LABEL, V...)      _MENU_ITEM_P(TYPE, false, PSTR(LABEL), ##V)
359
+#define EDIT_ITEM(TYPE, LABEL, V...)      _MENU_ITEM_P(TYPE, false, PSTR(LABEL), ##V)
360
+#define EDIT_ITEM_FAST(TYPE, LABEL, V...) _MENU_ITEM_P(TYPE,  true, PSTR(LABEL), ##V)
353 361
 
354 362
 #define SKIP_ITEM()                 (_thisItemNr++)
355 363
 #define BACK_ITEM(LABEL)            MENU_ITEM(back,LABEL)

Loading…
Cancel
Save