Skip to content

Commit 47801de

Browse files
author
dave
committed
#202 part 1 of refactor: remove all attempts to lazy recalculate
1 parent 40fe481 commit 47801de

File tree

5 files changed

+104
-32
lines changed

5 files changed

+104
-32
lines changed

src/MenuHistoryNavigator.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void tcnav::MenuNavigationStore::setRootItem(MenuItem *item) {
1414
currentSub = nullptr;
1515
navIdx = 0;
1616
currentIsCustom = false;
17+
triggerNavigationListener(false);
1718
}
1819

1920
void tcnav::MenuNavigationStore::navigateTo(MenuItem *activeItem, MenuItem *newRoot, bool custom) {
@@ -30,6 +31,7 @@ void tcnav::MenuNavigationStore::navigateTo(MenuItem *activeItem, MenuItem *newR
3031
}
3132
currentRoot = newRoot;
3233
currentSub = getSubMenuFor(newRoot);
34+
triggerNavigationListener(false);
3335
}
3436

3537
MenuItem *tcnav::MenuNavigationStore::popNavigationGetActive() {
@@ -38,12 +40,14 @@ MenuItem *tcnav::MenuNavigationStore::popNavigationGetActive() {
3840
serlogF(SER_TCMENU_INFO, "Nav pop root");
3941
currentSub = nullptr;
4042
currentRoot = root;
43+
triggerNavigationListener(false);
4144
return root;
4245
} else {
4346
navIdx--;
4447
currentRoot = navItems[navIdx];
4548
currentSub = getSubMenuFor(currentRoot);
4649
serlogF3(SER_TCMENU_INFO, "Nav pop ", navIdx, currentRoot->getId());
50+
triggerNavigationListener(false);
4751
return activeItems[navIdx];
4852
}
4953
}
@@ -54,4 +58,23 @@ bool tcnav::MenuNavigationStore::isShowingRoot() {
5458

5559
void tcnav::MenuNavigationStore::resetStack() {
5660
setRootItem(root);
61+
triggerNavigationListener(true);
62+
}
63+
64+
void tcnav::MenuNavigationStore::addNavigationListener(tcnav::NavigationListener *newListener) {
65+
if(this->navigationLister == nullptr) {
66+
this->navigationLister = newListener;
67+
} else {
68+
newListener->setNext(navigationLister);
69+
navigationLister = newListener;
70+
}
71+
newListener->navigationHasChanged(currentRoot, false);
72+
}
73+
74+
void tcnav::MenuNavigationStore::triggerNavigationListener(bool completeReset) {
75+
auto current = navigationLister;
76+
while(current) {
77+
current->navigationHasChanged(currentRoot, completeReset);
78+
current = navigationLister->getNext();
79+
}
5780
}

src/MenuHistoryNavigator.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
#endif
2020

2121
namespace tcnav {
22+
class NavigationListener {
23+
NavigationListener* next = nullptr;
24+
public:
25+
virtual void navigationHasChanged(MenuItem* newItem, bool completelyReset)=0;
26+
NavigationListener* getNext() { return next; }
27+
void setNext(NavigationListener* nxt) { next = nxt; }
28+
};
2229

2330
class MenuNavigationStore {
2431
private:
@@ -27,11 +34,16 @@ namespace tcnav {
2734
MenuItem* currentSub;
2835
MenuItem* navItems[NAV_ITEM_ARRAY_SIZE];
2936
MenuItem* activeItems[NAV_ITEM_ARRAY_SIZE];
37+
NavigationListener* navigationLister = nullptr;
3038
uint8_t navIdx;
3139
bool currentIsCustom;
3240
public:
3341
MenuNavigationStore() = default;
3442

43+
void addNavigationListener(NavigationListener* newListener);
44+
45+
void triggerNavigationListener(bool completeReset);
46+
3547
/**
3648
* @return the absolute root of all menu items
3749
*/

src/graphics/BaseGraphicalRenderer.cpp

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace tcgfx {
1111

12-
1312
void BaseGraphicalRenderer::render() {
14-
checkIfRootHasChanged();
13+
// do not attempt rendering before everything is initialised.
14+
if(currentRootMenu == nullptr) return;
1515

1616
uint8_t locRedrawMode = redrawMode;
1717
redrawMode = MENUDRAW_NO_CHANGE;
@@ -263,11 +263,7 @@ GridPosition::GridDrawingMode modeFromItem(MenuItem* item, bool useSlider) {
263263
}
264264
}
265265

266-
void BaseGraphicalRenderer::checkIfRootHasChanged() {
267-
auto* rootItem = menuMgr.getCurrentMenu();
268-
if(currentRootMenu != rootItem)
269-
{
270-
serlogF(SER_TCMENU_INFO, "root has changed");
266+
void BaseGraphicalRenderer::rootHasChanged(MenuItem* rootItem) {
271267
currentRootMenu = rootItem;
272268
redrawMode = MENUDRAW_COMPLETE_REDRAW;
273269
recalculateDisplayOrder(rootItem, false);
@@ -278,7 +274,6 @@ void BaseGraphicalRenderer::checkIfRootHasChanged() {
278274
serlogF3(SER_TCMENU_INFO, "Force encoder size: ", switches.getEncoder()->getMaximumValue(), expectedCount)
279275
menuMgr.setItemsInCurrentMenu(expectedCount, switches.getEncoder()->getCurrentReading());
280276
}
281-
}
282277
}
283278

284279
void BaseGraphicalRenderer::recalculateDisplayOrder(MenuItem *root, bool safeMode) {
@@ -394,7 +389,6 @@ int BaseGraphicalRenderer::calculateHeightTo(int index, MenuItem *pItem) {
394389
}
395390

396391
uint8_t BaseGraphicalRenderer::itemCount(MenuItem*, bool ) {
397-
checkIfRootHasChanged();
398392
if(currentRootMenu && currentRootMenu->getMenuType() == MENUTYPE_RUNTIME_LIST) {
399393
auto* listItem = reinterpret_cast<ListRuntimeMenuItem*>(currentRootMenu);
400394
return listItem->getNumberOfRows() + 1; // accounts for title.
@@ -405,7 +399,6 @@ uint8_t BaseGraphicalRenderer::itemCount(MenuItem*, bool ) {
405399
}
406400

407401
int BaseGraphicalRenderer::findItemIndex(MenuItem *root, MenuItem *toFind) {
408-
checkIfRootHasChanged();
409402
for(bsize_t i=0;i<itemOrderByRow.count();i++) {
410403
auto* possibleActive = itemOrderByRow.itemAtIndex(i);
411404
if(possibleActive->getMenuItem() == toFind) return i;
@@ -414,7 +407,6 @@ int BaseGraphicalRenderer::findItemIndex(MenuItem *root, MenuItem *toFind) {
414407
}
415408

416409
int BaseGraphicalRenderer::findActiveItem(MenuItem* root) {
417-
checkIfRootHasChanged();
418410
if(currentRootMenu && currentRootMenu->getMenuType() == MENUTYPE_RUNTIME_LIST) {
419411
auto* listItem = reinterpret_cast<ListRuntimeMenuItem*>(currentRootMenu);
420412
return listItem->getActiveIndex(); // accounts for title.
@@ -427,7 +419,6 @@ int BaseGraphicalRenderer::findActiveItem(MenuItem* root) {
427419
}
428420

429421
MenuItem *BaseGraphicalRenderer::getMenuItemAtIndex(MenuItem* item, uint8_t idx) {
430-
checkIfRootHasChanged();
431422
if(currentRootMenu && currentRootMenu->getMenuType() == MENUTYPE_RUNTIME_LIST) {
432423
return currentRootMenu;
433424
}
@@ -463,6 +454,30 @@ void BaseGraphicalRenderer::setTitleMode(BaseGraphicalRenderer::TitleMode mode)
463454
menuMgr.changeMenu(menuMgr.getCurrentMenu());
464455
}
465456

457+
BaseGraphicalRenderer::BaseGraphicalRenderer(int bufferSize, int wid, int hei, bool lastRowExact,const char *appTitle)
458+
: BaseMenuRenderer(bufferSize, RENDER_TYPE_CONFIGURABLE), navigationListener(this) {
459+
width = wid;
460+
height = hei;
461+
flags = 0;
462+
setTitleOnDisplay(true);
463+
setLastRowExactFit(lastRowExact);
464+
setUseSliderForAnalog(true);
465+
setEditStatusIconsEnabled(true);
466+
currentRootMenu = nullptr;
467+
pgmTitle = appTitle;
468+
}
469+
470+
void BaseGraphicalRenderer::initialise() {
471+
BaseMenuRenderer::initialise();
472+
menuMgr.getNavigationStore().addNavigationListener(&navigationListener);
473+
}
474+
475+
void BaseGraphicalRenderer::displayPropertiesHaveChanged() {
476+
currentRootMenu = nullptr;
477+
rootHasChanged(menuMgr.getCurrentMenu());
478+
redrawMode = MENUDRAW_COMPLETE_REDRAW;
479+
}
480+
466481
void preparePropertiesFromConfig(ConfigurableItemDisplayPropertiesFactory& factory, const ColorGfxMenuConfig<const void*>* gfxConfig, int titleHeight, int itemHeight) {
467482
// TEXT, BACKGROUND, HIGHLIGHT1, HIGHLIGHT2, SELECTED_FG, SELECTED_BG
468483
color_t paletteItems[] { gfxConfig->fgItemColor, gfxConfig->bgItemColor, gfxConfig->bgSelectColor, gfxConfig->fgSelectColor};
@@ -479,4 +494,15 @@ void preparePropertiesFromConfig(ConfigurableItemDisplayPropertiesFactory& facto
479494
ConfigurableItemDisplayPropertiesFactory::refreshCache();
480495
}
481496

497+
void RenderingNavigationListener::navigationHasChanged(MenuItem *newItem, bool completelyReset) {
498+
if(renderer->getCurrentRendererRoot() != newItem) {
499+
serlogF(SER_TCMENU_INFO, "Rendering root needs to change");
500+
renderer->rootHasChanged(newItem);
501+
}
502+
}
503+
504+
RenderingNavigationListener::RenderingNavigationListener(BaseGraphicalRenderer *r) {
505+
renderer = r;
506+
}
507+
482508
} // namespace tcgfx

src/graphics/BaseGraphicalRenderer.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ namespace tcgfx {
7676
LAYOUT_CARD_SIDEWAYS
7777
};
7878

79+
class BaseGraphicalRenderer;
80+
81+
/** Used to tie together the navigation change events with the renderer without multiple inheritance */
82+
class RenderingNavigationListener : public tcnav::NavigationListener {
83+
private:
84+
BaseGraphicalRenderer* renderer;
85+
public:
86+
explicit RenderingNavigationListener(BaseGraphicalRenderer* r);
87+
void navigationHasChanged(MenuItem *newItem, bool completelyReset) override;
88+
};
89+
7990
/**
8091
* This is the base class for all simpler renderer classes where the height of a row is equal for all entries,
8192
* and there is always exactly one item on a row. This takes away much of the work to row allocation for simple
@@ -105,7 +116,15 @@ namespace tcgfx {
105116
/** this indicates that the drawing is ending */
106117
DRAW_COMMAND_ENDED
107118
};
119+
120+
/**
121+
* The current menu from the renderers perspective
122+
* @return the current menu that the rendering layer is drawing for
123+
*/
124+
MenuItem *getCurrentRendererRoot() { return currentRootMenu; }
125+
108126
private:
127+
RenderingNavigationListener navigationListener;
109128
MenuItem *currentRootMenu;
110129
const char *pgmTitle;
111130
GridPositionRowCacheEntry cachedEntryItem;
@@ -115,18 +134,8 @@ namespace tcgfx {
115134
uint8_t flags;
116135
uint16_t width, height;
117136
public:
118-
BaseGraphicalRenderer(int bufferSize, int wid, int hei, bool lastRowExact, const char *appTitle)
119-
: BaseMenuRenderer(bufferSize, RENDER_TYPE_CONFIGURABLE) {
120-
width = wid;
121-
height = hei;
122-
flags = 0;
123-
setTitleOnDisplay(true);
124-
setLastRowExactFit(lastRowExact);
125-
setUseSliderForAnalog(true);
126-
setEditStatusIconsEnabled(true);
127-
currentRootMenu = nullptr;
128-
pgmTitle = appTitle;
129-
}
137+
BaseGraphicalRenderer(int bufferSize, int wid, int hei, bool lastRowExact, const char *appTitle);
138+
void initialise() override;
130139

131140
void setTitleMode(TitleMode mode);
132141

@@ -300,10 +309,14 @@ namespace tcgfx {
300309
/**
301310
* Force the renderer to completely recalculate the display parameters next time it's drawn.
302311
*/
303-
void displayPropertiesHaveChanged() {
304-
currentRootMenu = nullptr;
305-
redrawMode = MENUDRAW_COMPLETE_REDRAW;
306-
}
312+
void displayPropertiesHaveChanged();
313+
314+
/**
315+
* This is generally called by the navigation listener when the root item has changed due to a new menu being
316+
* displayed, or display reset event. It will force an immediate recalculation of all items.
317+
* @param newItem the new root item
318+
*/
319+
void rootHasChanged(MenuItem* newItem);
307320

308321
protected:
309322
/**
@@ -317,8 +330,6 @@ namespace tcgfx {
317330
virtual void subMenuRender(MenuItem* rootItem, uint8_t& locRedrawMode, bool& forceDrawWidgets);
318331
int heightOfRow(int row, bool includeSpace=false);
319332
private:
320-
void checkIfRootHasChanged();
321-
322333
bool drawTheMenuItems(int startRow, int startY, bool drawEveryLine);
323334

324335
void renderList();

src/tcMenu.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CommitCallbackObserver : public MenuManagerObserver {
6969
private:
7070
MenuCallbackFn commitCb;
7171
public:
72-
CommitCallbackObserver(MenuCallbackFn callbackFn) {
72+
explicit CommitCallbackObserver(MenuCallbackFn callbackFn) {
7373
commitCb = callbackFn;
7474
}
7575

@@ -534,4 +534,4 @@ inline bool editorHintNeedsCursor(CurrentEditorRenderingHints::EditorRenderingTy
534534
*/
535535
extern MenuManager menuMgr;
536536

537-
#endif // defined header file
537+
#endif // TCMENU_MANAGER_H

0 commit comments

Comments
 (0)