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
 bool screen_changed;
68
 bool screen_changed;
69
 
69
 
70
 // Value Editing
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
 // Prevent recursion into screen handlers
77
 // Prevent recursion into screen handlers
78
 bool no_reentry = false;
78
 bool no_reentry = false;
131
  *
131
  *
132
  *   bool MenuItem_int3::_edit();
132
  *   bool MenuItem_int3::_edit();
133
  *   void MenuItem_int3::edit(); // edit int16_t (interactively)
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
  * You can then use one of the menu macros to present the edit interface:
136
  * You can then use one of the menu macros to present the edit interface:
137
  *   EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
137
  *   EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
138
  *
138
  *
139
  * This expands into a more primitive menu item:
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
  * ...which calls:
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
   #if ENABLED(TOUCH_BUTTONS)
147
   #if ENABLED(TOUCH_BUTTONS)
147
     ui.repeat_delay = BUTTON_DELAY_EDIT;
148
     ui.repeat_delay = BUTTON_DELAY_EDIT;
148
   #endif
149
   #endif
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
   ui.save_previous_screen();
162
   ui.save_previous_screen();
162
   ui.refresh();
163
   ui.refresh();
163
   editLabel = el;
164
   editLabel = el;
170
   liveEdit = le;
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
 DEFINE_MENU_EDIT_ITEM(percent);     // 100%       right-justified
176
 DEFINE_MENU_EDIT_ITEM(percent);     // 100%       right-justified
176
 DEFINE_MENU_EDIT_ITEM(int3);        // 123, -12   right-justified
177
 DEFINE_MENU_EDIT_ITEM(int3);        // 123, -12   right-justified
191
 DEFINE_MENU_EDIT_ITEM(long5);       // 12345      right-justified
192
 DEFINE_MENU_EDIT_ITEM(long5);       // 12345      right-justified
192
 DEFINE_MENU_EDIT_ITEM(long5_25);    // 12345      right-justified (25 increment)
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
   UNUSED(pstr); *ptr ^= true; ui.refresh();
196
   UNUSED(pstr); *ptr ^= true; ui.refresh();
196
   if (callback) (*callback)();
197
   if (callback) (*callback)();
197
 }
198
 }

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

42
 ////////////////////////////////////////////
42
 ////////////////////////////////////////////
43
 
43
 
44
 #define DECLARE_MENU_EDIT_TYPE(TYPE, NAME, STRFUNC, SCALE) \
44
 #define DECLARE_MENU_EDIT_TYPE(TYPE, NAME, STRFUNC, SCALE) \
45
-  struct MenuItemInfo_##NAME { \
45
+  struct MenuEditItemInfo_##NAME { \
46
     typedef TYPE type_t; \
46
     typedef TYPE type_t; \
47
     static constexpr float scale = SCALE; \
47
     static constexpr float scale = SCALE; \
48
     static inline char* strfunc(const float value) { return STRFUNC((TYPE) value); } \
48
     static inline char* strfunc(const float value) { return STRFUNC((TYPE) value); } \
111
 ////////////////////////////////////////////
111
 ////////////////////////////////////////////
112
 
112
 
113
 #define _DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(TYPE, NAME, STRFUNC) \
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
     DRAW_MENU_ITEM_SETTING_EDIT_GENERIC(STRFUNC(*(data))); \
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
     DRAW_MENU_ITEM_SETTING_EDIT_GENERIC(STRFUNC(pget())); \
118
     DRAW_MENU_ITEM_SETTING_EDIT_GENERIC(STRFUNC(pget())); \
119
   } \
119
   } \
120
   typedef void NAME##_void
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
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(percent);          // 100%       right-justified
123
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(percent);          // 100%       right-justified
124
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(int3);             // 123, -12   right-justified
124
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(int3);             // 123, -12   right-justified
139
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(long5);            // 12345      right-justified
139
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(long5);            // 12345      right-justified
140
 DEFINE_DRAW_MENU_ITEM_SETTING_EDIT(long5_25);         // 12345      right-justified (25 increment)
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
 /////////////// Menu Actions ///////////////
146
 /////////////// Menu Actions ///////////////
176
 /////////// Menu Editing Actions ///////////
176
 /////////// Menu Editing Actions ///////////
177
 ////////////////////////////////////////////
177
 ////////////////////////////////////////////
178
 
178
 
179
-class MenuItemBase {
179
+// Edit items use long integer encoder units
180
+class MenuEditItemBase {
180
   private:
181
   private:
181
     static PGM_P editLabel;
182
     static PGM_P editLabel;
182
     static void *editValue;
183
     static void *editValue;
183
-    static int32_t minEditValue, maxEditValue;
184
+    static int32_t minEditValue, maxEditValue;  // Encoder value range
184
     static screenFunc_t callbackFunc;
185
     static screenFunc_t callbackFunc;
185
     static bool liveEdit;
186
     static bool liveEdit;
186
   protected:
187
   protected:
191
 };
192
 };
192
 
193
 
193
 template<typename NAME>
194
 template<typename NAME>
194
-class TMenuItem : MenuItemBase {
195
+class TMenuEditItem : MenuEditItemBase {
195
   private:
196
   private:
196
     typedef typename NAME::type_t type_t;
197
     typedef typename NAME::type_t type_t;
197
     static inline float unscale(const float value)    { return value * (1.0f / NAME::scale);  }
198
     static inline float unscale(const float value)    { return value * (1.0f / NAME::scale);  }
199
     static void load(void *ptr, const int32_t value)  { *((type_t*)ptr) = unscale(value);     }
200
     static void load(void *ptr, const int32_t value)  { *((type_t*)ptr) = unscale(value);     }
200
     static char* to_string(const int32_t value)       { return NAME::strfunc(unscale(value)); }
201
     static char* to_string(const int32_t value)       { return NAME::strfunc(unscale(value)); }
201
   public:
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
       init(pstr, ptr, minv, maxv - minv, scale(*ptr) - minv, edit, callback, live);
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
 DECLARE_MENU_EDIT_ITEM(percent);
221
 DECLARE_MENU_EDIT_ITEM(percent);
214
 DECLARE_MENU_EDIT_ITEM(int3);
222
 DECLARE_MENU_EDIT_ITEM(int3);
231
 
239
 
232
 class MenuItem_bool {
240
 class MenuItem_bool {
233
   public:
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
 /**
297
 /**
290
  * MENU_ITEM generates draw & handler code for a menu item, potentially calling:
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
  * Examples:
303
  * Examples:
296
  *   BACK_ITEM(MSG_WATCH)
304
  *   BACK_ITEM(MSG_WATCH)
304
  *     MenuItem_function::action(lcd_sdcard_pause)
312
  *     MenuItem_function::action(lcd_sdcard_pause)
305
  *
313
  *
306
  *   EDIT_ITEM(int3, MSG_SPEED, &feedrate_percentage, 10, 999)
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
     _skipStatic = false;                                          \
320
     _skipStatic = false;                                          \
313
     if (_menuLineNr == _thisItemNr) {                             \
321
     if (_menuLineNr == _thisItemNr) {                             \
314
       PGM_P const plabel = PLABEL;                                \
322
       PGM_P const plabel = PLABEL;                                \
315
       if (encoderLine == _thisItemNr && ui.use_click()) {         \
323
       if (encoderLine == _thisItemNr && ui.use_click()) {         \
316
         _MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER);              \
324
         _MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER);              \
317
-        MenuItem_##TYPE ::action ## VARIANT(plabel, ##V);         \
325
+        MenuItem_##TYPE ::action(plabel, ##V);                    \
318
         if (screen_changed) return;                               \
326
         if (screen_changed) return;                               \
319
       }                                                           \
327
       }                                                           \
320
       if (ui.should_draw())                                       \
328
       if (ui.should_draw())                                       \
321
-        draw_menu_item ## VARIANT ## _ ## TYPE                    \
329
+        draw_menu_item_ ## TYPE                                   \
322
           (encoderLine == _thisItemNr, _lcdLineNr, plabel, ##V);  \
330
           (encoderLine == _thisItemNr, _lcdLineNr, plabel, ##V);  \
323
     }                                                             \
331
     }                                                             \
324
   ++_thisItemNr;                                                  \
332
   ++_thisItemNr;                                                  \
346
 
354
 
347
 #define STATIC_ITEM(LABEL, V...) STATIC_ITEM_P(PSTR(LABEL), ##V)
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
 #define SKIP_ITEM()                 (_thisItemNr++)
362
 #define SKIP_ITEM()                 (_thisItemNr++)
355
 #define BACK_ITEM(LABEL)            MENU_ITEM(back,LABEL)
363
 #define BACK_ITEM(LABEL)            MENU_ITEM(back,LABEL)

Loading…
Cancel
Save