tcMenu
tcMenu.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #ifndef TCMENU_MANAGER_H
7 #define TCMENU_MANAGER_H
8 
9 #include <IoAbstraction.h>
10 #include "tcUtil.h"
11 #include "MenuItems.h"
12 #include "MenuHistoryNavigator.h"
13 #include "RuntimeMenuItem.h"
14 #include "BaseRenderers.h"
15 #include "RemoteAuthentication.h"
16 #include "EepromItemStorage.h"
17 
18 // forward reference
19 class MenuRenderer;
20 
28 class SecuredMenuPopup;
29 
35 public:
40  virtual void structureHasChanged()=0;
41 
47  virtual bool menuEditStarting(MenuItem* item)=0;
48 
55  virtual void menuEditEnded(MenuItem* item)=0;
56 
62  virtual void activeItemHasChanged(MenuItem* /*newActive*/) {}
63 };
64 
69 private:
70  MenuCallbackFn commitCb;
71 public:
72  explicit CommitCallbackObserver(MenuCallbackFn callbackFn) {
73  commitCb = callbackFn;
74  }
75 
76  void structureHasChanged() override {}
77  bool menuEditStarting(MenuItem*) override { return true; }
78  void menuEditEnded(MenuItem* item) override {
79  commitCb(item->getId());
80  }
81 };
82 
83 #ifndef MAX_MENU_NOTIFIERS
84 #define MAX_MENU_NOTIFIERS 4
85 #endif
86 
91 private:
92  menuid_t itemId;
93  bool overrideValue;
94 public:
95  EncoderWrapOverride() = default;
96  EncoderWrapOverride(const EncoderWrapOverride &other) = default;
97  EncoderWrapOverride &operator=(const EncoderWrapOverride &other) = default;
98  EncoderWrapOverride(menuid_t itemId, bool overrideValue) : itemId(itemId), overrideValue(overrideValue) {}
99 
100  menuid_t getKey() const { return itemId; }
101 
102  menuid_t getMenuId() const { return itemId; }
103  bool getOverrideValue() const { return overrideValue; }
104 };
105 
112 public:
117  EDITOR_REGULAR = 0,
118  EDITOR_WHOLE_ONLY = 0x0001, EDITOR_FRACTION_ONLY = 0x0002,
119  EDITOR_RUNTIME_TEXT = 0x0004,
120  EDITOR_OVERRIDE_LOCK = 0x8000
121  };
122 private:
123  EditorRenderingType renderingType = EDITOR_REGULAR;
124  int editStart = 0;
125  int editEnd = 0;
126 public:
127  CurrentEditorRenderingHints() = default;
128  void changeEditingParams(EditorRenderingType ty, int startOffset, int endOffset);
129  void lockEditor(bool lock) { renderingType = lock ? EDITOR_OVERRIDE_LOCK : EDITOR_REGULAR; }
130  EditorRenderingType getEditorRenderingType() const { return renderingType; }
131  int getStartIndex() const { return editStart; }
132  int getEndIndex() const { return editEnd; }
133 };
134 
139 class MenuManager {
140 private:
141  tcnav::MenuNavigationStore navigator{};
142  MenuItem* currentEditor;
143  MenuRenderer* renderer;
144  SecuredMenuPopup* securedMenuPopup;
145  AuthenticationManager *authenticationManager;
146  EepromAbstraction* eepromRef;
147  MenuManagerObserver* structureNotifier[MAX_MENU_NOTIFIERS];
148  bool useWrapAroundByDefault = false;
149  BtreeList<menuid_t, EncoderWrapOverride> encoderWrapOverrides;
150  CurrentEditorRenderingHints renderingHints;
151 public:
152  static SubMenuItem ROOT;
153  MenuManager();
154 
160  void setUseWrapAroundEncoder(bool wrapAround) {
161  useWrapAroundByDefault = wrapAround;
162  }
163 
174  void addEncoderWrapOverride(MenuItem& item, bool override);
175 
183  bool isWrapAroundEncoder(MenuItem* item);
184 
194  void initForEncoder(MenuRenderer* renderer, MenuItem* root, pinid_t encoderPinA, pinid_t encoderPinB, pinid_t encoderButton, EncoderType type = FULL_CYCLE);
195 
205  void initForUpDownOk(MenuRenderer* renderer, MenuItem* root, pinid_t downPin, pinid_t upPin, pinid_t okPin, int speed=20);
206 
219  void initFor4WayJoystick(MenuRenderer* renderer, MenuItem* root, pinid_t downPin, pinid_t upPin, pinid_t leftPin,
220  pinid_t rightPin, pinid_t okPin, int speed=20);
221 
229  void initForTwoButton(MenuRenderer* renderer, MenuItem* root, pinid_t upPin, pinid_t downPin);
230 
243  void initWithoutInput(MenuRenderer* renderer, MenuItem* root);
244 
249  void setBackButton(pinid_t backButtonPin);
250 
255  void setNextButton(pinid_t nextButtonPin);
256 
261  void setRootMenu(MenuItem* menuItem) {
262  navigator.setRootItem(menuItem);
263  }
264 
271  void setAuthenticator(AuthenticationManager* manager) { authenticationManager = manager; }
272 
277  if(authenticationManager == nullptr) authenticationManager = new NoAuthenticationManager();
278  return authenticationManager;
279  }
280 
287  void setItemCommittedHook(MenuCallbackFn commitCallback) {
288  addChangeNotification(new CommitCallbackObserver(commitCallback));
289  }
290 
295  void valueChanged(int value);
296 
301  void onMenuSelect(bool held);
302 
308  void performDirectionMove(bool dirIsBack);
309 
315  void setItemsInCurrentMenu(int size, int offs = 0);
316 
317  EepromAbstraction* getEepromAbstraction() { return eepromRef; }
318 
323  void setEepromRef(EepromAbstraction* globalRom) {
324  eepromRef = globalRom;
325  }
326 
331  void load(EepromAbstraction& eeprom, uint16_t magicKey = 0xfade, TimerFn onEepromEmpty = nullptr);
332 
333 
338  void load(uint16_t magicKey = 0xfade, TimerFn onEepromEmpty = nullptr);
339 
340 
348  void save(uint16_t magicKey = 0xfade) { if(eepromRef) saveMenuStructure(eepromRef, magicKey); }
349 
355  void save(EepromAbstraction& eeprom, uint16_t magicKey = 0xfade) { saveMenuStructure(&eeprom, magicKey); }
356 
361 
365  MenuItem* getRoot() { return ROOT.getChild(); }
366 
370  MenuRenderer* getRenderer() { return renderer; }
371 
375  MenuItem* getCurrentEditor() { return currentEditor; }
376 
381  void setCurrentEditor(MenuItem* editor);
382 
388  void setItemActive(MenuItem* item);
389 
394  void changeMenu(MenuItem* possibleActive=nullptr);
395 
409  void navigateToMenu(MenuItem* theNewItem, MenuItem* possibleActive = nullptr, bool skipHistory = false);
410 
414  void resetMenu(bool completeReset);
415 
419  MenuItem* getCurrentMenu() { return navigator.getCurrentRoot(); }
420 
424  MenuItem* getCurrentSubMenu() { return navigator.getCurrentSubMenu(); }
425 
430 
435 
441  return switches.getEncoder() != nullptr ? switches.getEncoder()->getCurrentReading() : 0;
442  }
443 
450 
456  void stopEditingCurrentItem(bool doMultiPartNext);
457 
465  void addMenuAfter(MenuItem* existing, MenuItem* toAdd, bool silent = false);
466 
471  void notifyStructureChanged();
472 
478 
484  void recalculateListIfOnDisplay(RuntimeMenuItem* runtimeItem);
485 
493  void setEditorHints(CurrentEditorRenderingHints::EditorRenderingType hint, size_t start=0, size_t end=0);
494  const CurrentEditorRenderingHints& getEditorHints() { return renderingHints; }
495 
501  void setEditorHintsLocked(bool locked);
502 
506  void resetObservers();
507 
512  void setupForEditing(MenuItem* item);
513 protected:
514  void actionOnCurrentItem(MenuItem * toEdit);
515  void actionOnSubMenu(MenuItem* nextSub);
516 
517  void notifyEditEnd(MenuItem *pItem);
518  bool notifyEditStarting(MenuItem *pItem);
519 
520  void setRootItem(MenuItem *pItem);
521 };
522 
523 inline bool editorHintNeedsCursor(CurrentEditorRenderingHints::EditorRenderingType ty) {
524  return ty != CurrentEditorRenderingHints::EDITOR_REGULAR && ty != CurrentEditorRenderingHints::EDITOR_OVERRIDE_LOCK;
525 }
526 
530 extern MenuManager menuMgr;
531 
532 #endif // TCMENU_MANAGER_H
The base definitions and classes shared by all TcMenu renderers.
this file contains a series of helper methods for loading and saving menu item to eeprom.
void saveMenuStructure(EepromAbstraction *eeprom, uint16_t magicKey=0xfade)
Definition: EepromItemStorage.cpp:82
contains the functionality to do with menu item navigation, including a stack of previous navigations
In TcMenu, MenuItem storage is shared between program memory and RAM. Usually each MenuItem has assoc...
void(* MenuCallbackFn)(int id)
Definition: MenuItems.h:45
Contains the base functionality for communication between the menu library and remote APIs.
Contains definitions of menu items that can be fully defined at runtime with no need for prog mem str...
Definition: RemoteAuthentication.h:38
Definition: tcMenu.h:68
void menuEditEnded(MenuItem *item) override
Definition: tcMenu.h:78
void structureHasChanged() override
Definition: tcMenu.h:76
bool menuEditStarting(MenuItem *) override
Definition: tcMenu.h:77
Definition: tcMenu.h:111
EditorRenderingType
Definition: tcMenu.h:116
Definition: tcMenu.h:90
Definition: MenuItems.h:329
menuid_t getId() const
Definition: MenuItems.cpp:81
Definition: tcMenu.h:139
void addEncoderWrapOverride(MenuItem &item, bool override)
Definition: tcMenu.cpp:509
void performDirectionMove(bool dirIsBack)
Definition: tcMenu.cpp:106
void notifyStructureChanged()
Definition: tcMenu.cpp:469
void addMenuAfter(MenuItem *existing, MenuItem *toAdd, bool silent=false)
Definition: tcMenu.cpp:404
void setBackButton(pinid_t backButtonPin)
Definition: tcMenu.cpp:94
void initForTwoButton(MenuRenderer *renderer, MenuItem *root, pinid_t upPin, pinid_t downPin)
Definition: tcMenu.cpp:66
MenuItem * getCurrentMenu()
Definition: tcMenu.h:419
void recalculateListIfOnDisplay(RuntimeMenuItem *runtimeItem)
Definition: tcMenu.cpp:520
AuthenticationManager * getAuthenticator()
Definition: tcMenu.h:276
void setCurrentEditor(MenuItem *editor)
Definition: tcMenu.cpp:354
void addChangeNotification(MenuManagerObserver *observer)
Definition: tcMenu.cpp:414
void navigateToMenu(MenuItem *theNewItem, MenuItem *possibleActive=nullptr, bool skipHistory=false)
Definition: tcMenu.cpp:504
void setUseWrapAroundEncoder(bool wrapAround)
Definition: tcMenu.h:160
void save(EepromAbstraction &eeprom, uint16_t magicKey=0xfade)
Definition: tcMenu.h:355
tcnav::MenuNavigationStore & getNavigationStore()
Definition: tcMenu.h:429
MenuItem * getRoot()
Definition: tcMenu.h:365
void initFor4WayJoystick(MenuRenderer *renderer, MenuItem *root, pinid_t downPin, pinid_t upPin, pinid_t leftPin, pinid_t rightPin, pinid_t okPin, int speed=20)
Definition: tcMenu.cpp:50
void setItemActive(MenuItem *item)
Definition: tcMenu.cpp:180
void load(EepromAbstraction &eeprom, uint16_t magicKey=0xfade, TimerFn onEepromEmpty=nullptr)
Definition: tcMenu.cpp:436
void stopEditingCurrentItem(bool doMultiPartNext)
Definition: tcMenu.cpp:281
void setupForEditing(MenuItem *item)
Definition: tcMenu.cpp:319
SecuredMenuPopup * secureMenuInstance()
Definition: tcMenu.cpp:391
void resetMenu(bool completeReset)
Definition: tcMenu.cpp:490
void setAuthenticator(AuthenticationManager *manager)
Definition: tcMenu.h:271
MenuRenderer * getRenderer()
Definition: tcMenu.h:370
void initWithoutInput(MenuRenderer *renderer, MenuItem *root)
Definition: tcMenu.cpp:134
int getCurrentRangeValue()
Definition: tcMenu.h:440
void changeMenu(MenuItem *possibleActive=nullptr)
Definition: tcMenu.cpp:367
void onMenuSelect(bool held)
Definition: tcMenu.cpp:201
MenuItem * findCurrentActive()
Definition: tcMenu.cpp:541
bool isWrapAroundEncoder(MenuItem *item)
Definition: tcMenu.cpp:513
void setEditorHintsLocked(bool locked)
Definition: tcMenu.cpp:536
void save(uint16_t magicKey=0xfade)
Definition: tcMenu.h:348
MenuItem * getCurrentEditor()
Definition: tcMenu.h:375
void setEditorHints(CurrentEditorRenderingHints::EditorRenderingType hint, size_t start=0, size_t end=0)
Definition: tcMenu.cpp:531
void initForUpDownOk(MenuRenderer *renderer, MenuItem *root, pinid_t downPin, pinid_t upPin, pinid_t okPin, int speed=20)
Definition: tcMenu.cpp:73
void initForEncoder(MenuRenderer *renderer, MenuItem *root, pinid_t encoderPinA, pinid_t encoderPinB, pinid_t encoderButton, EncoderType type=FULL_CYCLE)
Definition: tcMenu.cpp:83
void resetObservers()
Definition: tcMenu.cpp:423
void setEepromRef(EepromAbstraction *globalRom)
Definition: tcMenu.h:323
void setItemCommittedHook(MenuCallbackFn commitCallback)
Definition: tcMenu.h:287
MenuItem * getParentAndReset()
Definition: tcMenu.cpp:308
void setItemsInCurrentMenu(int size, int offs=0)
Definition: tcMenu.cpp:482
MenuItem * getCurrentSubMenu()
Definition: tcMenu.h:424
void valueChanged(int value)
Definition: tcMenu.cpp:153
void setNextButton(pinid_t nextButtonPin)
Definition: tcMenu.cpp:100
void setRootMenu(MenuItem *menuItem)
Definition: tcMenu.h:261
Definition: tcMenu.h:34
virtual void menuEditEnded(MenuItem *item)=0
virtual void structureHasChanged()=0
virtual void activeItemHasChanged(MenuItem *)
Definition: tcMenu.h:62
virtual bool menuEditStarting(MenuItem *item)=0
Definition: BaseRenderers.h:198
Definition: RemoteAuthentication.h:171
Definition: RuntimeMenuItem.h:75
Definition: SecuredMenuPopup.h:40
Definition: RuntimeMenuItem.h:149
MenuItem * getChild() const
Definition: RuntimeMenuItem.h:183
Definition: MenuHistoryNavigator.h:30
MenuItem * getCurrentSubMenu()
Definition: MenuHistoryNavigator.h:60
void setRootItem(MenuItem *item)
Definition: MenuHistoryNavigator.cpp:12
MenuItem * getCurrentRoot()
Definition: MenuHistoryNavigator.h:55
MenuManager menuMgr
Definition: tcMenu.cpp:16
A series of utilities that used throughout tcMenu.