tcMenu
BaseGraphicalRenderer.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 
11 #ifndef TCMENU_BASEGRAPHICALRENDERER_H
12 #define TCMENU_BASEGRAPHICALRENDERER_H
13 
14 #include <BaseRenderers.h>
15 #include "GfxMenuConfig.h"
16 #include "RuntimeTitleMenuItem.h"
17 
18 #define GFX_LAST_ROW_FIT_FLAG 0
19 #define GFX_USING_RAW_TOUCH 1
20 #define GFX_USING_TOUCH_INTERFACE 2
21 #define GFX_SLIDER_FOR_ANALOG 3
22 #define GFX_TITLE_ON_DISPLAY 4
23 #define GFX_EDIT_STATUS_ICONS_ENABLED 5
24 
25 namespace tcgfx {
26 
33  inline uint16_t rowCol(int row, int col) {
34  return (row * 100) + col;
35  }
36 
37  class DrawingFlags {
38  private:
39  uint16_t flags;
40  public:
41  DrawingFlags(bool drawAll, bool active, bool editing) : flags(0) {
42  bitWrite(flags, 0, drawAll);
43  bitWrite(flags, 1, active);
44  bitWrite(flags, 2, editing);
45  }
46  DrawingFlags(const DrawingFlags& other) = default;
47  DrawingFlags& operator=(const DrawingFlags& other) = default;
48 
49  bool isDrawingAll() const { return bitRead(flags, 0); }
50  bool isActive() const { return bitRead(flags, 1); }
51  bool isEditing() const { return bitRead(flags, 2); }
52  };
53 
61  private:
62  MenuItem *menuItem;
63  GridPosition thePosition;
64  ItemDisplayProperties *properties;
65  public:
66  GridPositionRowCacheEntry() : menuItem(nullptr), thePosition(GridPosition::DRAW_TEXTUAL_ITEM, GridPosition::JUSTIFY_TITLE_LEFT_VALUE_RIGHT, 0), properties(nullptr) {}
67 
69  GridPositionRowCacheEntry& operator=(const GridPositionRowCacheEntry &other) = default;
70 
71  GridPositionRowCacheEntry(MenuItem *item, const GridPosition &pos, ItemDisplayProperties *props) : menuItem(item), thePosition(pos), properties(props) {}
72 
73  const GridPosition &getPosition() { return thePosition; }
74 
75  ItemDisplayProperties *getDisplayProperties() { return properties; }
76 
77  uint16_t getKey() const { return rowCol(thePosition.getRow(), thePosition.getGridPosition()); }
78 
79  uint16_t getHeight() {
80  return thePosition.getGridHeight() != 0 ? thePosition.getGridHeight() : properties->getRequiredHeight();
81  }
82 
83  MenuItem *getMenuItem() { return menuItem; }
84  };
85 
89  enum LayoutMode: uint8_t {
94  };
95 
96  class BaseGraphicalRenderer;
97 
100  private:
101  BaseGraphicalRenderer* renderer;
102  public:
104  void navigationHasChanged(MenuItem *newItem, bool completelyReset) override;
105  };
106 
108  private:
109  uint16_t startY;
110  uint8_t currentOffset;
111  public:
112  CachedDrawingLocation() = default;
113  CachedDrawingLocation(uint16_t startY, uint8_t currentOffset) : startY(startY), currentOffset(currentOffset) {}
114  CachedDrawingLocation(const CachedDrawingLocation& other) = default;
115  CachedDrawingLocation& operator=(const CachedDrawingLocation& other) = default;
116 
117  uint16_t getStartY() const {
118  return startY;
119  }
120 
121  uint8_t getCurrentOffset() const {
122  return currentOffset;
123  }
124  };
125 
132  public:
136  enum TitleMode : uint8_t {
143  };
154  };
155 
160  MenuItem *getCurrentRendererRoot() { return currentRootMenu; }
161 
162  private:
163  RenderingNavigationListener navigationListener;
164  MenuItem *currentRootMenu;
165  const char *pgmTitle;
166  GridPositionRowCacheEntry cachedEntryItem;
167  protected:
168  BtreeList<uint16_t, GridPositionRowCacheEntry> itemOrderByRow;
169  TitleMode titleMode = TITLE_FIRST_ROW;
170  uint16_t width, height;
171  CachedDrawingLocation drawingLocation;
172  uint8_t flags;
173  public:
174  BaseGraphicalRenderer(int bufferSize, int wid, int hei, bool lastRowExact, const char *appTitle);
175  void initialise() override;
176 
177  void setTitleMode(TitleMode mode);
178 
184  void setDisplayNumber(uint8_t displayNum) { this->displayNumber = displayNum; }
185 
189  uint8_t getDisplayNumber() { return this->displayNumber; }
190 
195  void setUseSliderForAnalog(bool useSlider) { bitWrite(flags, GFX_SLIDER_FOR_ANALOG, useSlider); }
200  void setHasTouchInterface(bool hasTouch) { bitWrite(flags, GFX_USING_TOUCH_INTERFACE, hasTouch); }
205  void setTitleOnDisplay(bool titleOn) { bitWrite(flags, GFX_TITLE_ON_DISPLAY, titleOn); }
211  void setLastRowExactFit(bool exact) { bitWrite(flags, GFX_LAST_ROW_FIT_FLAG, exact); }
216  void setRawTouchMode(bool rawTouch) { bitWrite(flags, GFX_USING_RAW_TOUCH, rawTouch); }
223  void setEditStatusIconsEnabled(bool ena) { bitWrite(flags, GFX_EDIT_STATUS_ICONS_ENABLED, ena); }
227  bool isUseSliderForAnalog() const { return bitRead(flags, GFX_SLIDER_FOR_ANALOG); }
231  bool isHasTouchInterface() const { return bitRead(flags, GFX_USING_TOUCH_INTERFACE); }
235  bool isTitleOnDisplay() const { return bitRead(flags, GFX_TITLE_ON_DISPLAY); }
239  bool isLastRowExactFit() const { return bitRead(flags, GFX_LAST_ROW_FIT_FLAG); }
243  bool isRawTouchMode() const { return bitRead(flags, GFX_USING_RAW_TOUCH); }
247  bool isEditStatusIconEnabled() const { return bitRead(flags, GFX_EDIT_STATUS_ICONS_ENABLED); }
248 
249  void render() override;
250 
256  void setDisplayDimensions(int w, int h) {
257  serlogF3(SER_TCMENU_INFO, "Set dimensions: ", w, h);
258  width = w;
259  height = h;
260  }
261 
266 
275  virtual void drawWidget(Coord where, TitleWidget *widget, color_t colorFg, color_t colorBg) = 0;
276 
284  virtual void drawMenuItem(GridPositionRowCacheEntry *entry, Coord where, Coord areaSize, const DrawingFlags& drawFlags) = 0;
285 
289  virtual void drawingCommand(RenderDrawingCommand command) = 0;
290 
296  virtual void fillWithBackgroundTo(int endPoint) = 0;
297 
305 
312  int findItemIndex(MenuItem *root, MenuItem *toFind) override;
313 
317  uint8_t itemCount(MenuItem* root, bool) override;
318 
327  GridPositionRowCacheEntry* findMenuEntryAndDimensions(const Coord &screenPos, Coord &localStart, Coord &localSize);
328 
332  MenuItem *getMenuItemAtIndex(MenuItem* currentRoot, uint8_t idx) override;
333 
339  BaseDialog *getDialog() override;
340 
344  int getWidth() const { return width; }
345 
349  int getHeight() const { return height; }
350 
355 
361  void rootHasChanged(MenuItem* newItem);
362 
369  uint8_t setActiveItem(MenuItem *item) override;
370  protected:
379  virtual void subMenuRender(MenuItem* rootItem, uint8_t& locRedrawMode, bool& forceDrawWidgets);
380  int heightOfRow(int row, bool includeSpace=false);
381  private:
382  bool drawTheMenuItems(int startRow, int startY, bool drawEveryLine);
383 
384  void renderList();
385 
386  void recalculateDisplayOrder(MenuItem *pItem, bool safeMode);
387 
388  void redrawAllWidgets(bool forceRedraw);
389 
390  bool areRowsOutOfOrder();
391 
392  int calculateHeightTo(int index, MenuItem *pItem);
393 
395 
396  GridPositionRowCacheEntry *firstItemOnRow(int row);
397 
398  int skipRow(int row);
399  };
400 
408  inline int analogRangeToScreen(AnalogMenuItem *item, int screenWidth) {
409  float ratio = (float) screenWidth / (float) item->getMaximumValue();
410  return int((float) item->getCurrentValue() * ratio);
411  }
412 
421  void preparePropertiesFromConfig(ConfigurableItemDisplayPropertiesFactory &factory,
422  const ColorGfxMenuConfig<const void *> *gfxConfig, int titleHeight,
423  int itemHeight);
424 
431  GridPosition::GridDrawingMode modeFromItem(MenuItem* item, bool useSlider);
432 
433 } // namespace tcgfx
434 
435 #endif //TCMENU_BASEGRAPHICALRENDERER_H
uint16_t rowCol(int row, int col)
Definition: BaseGraphicalRenderer.h:33
int analogRangeToScreen(AnalogMenuItem *item, int screenWidth)
Definition: BaseGraphicalRenderer.h:408
LayoutMode
Definition: BaseGraphicalRenderer.h:89
@ LAYOUT_CARD_SIDEWAYS
Definition: BaseGraphicalRenderer.h:93
@ LAYOUT_VERTICAL_DEFAULT
Definition: BaseGraphicalRenderer.h:91
The base definitions and classes shared by all TcMenu renderers.
uint32_t color_t
Definition: DrawingPrimitives.h:29
the menu item that is presented for the title
Definition: MenuItems.h:476
Definition: BaseDialog.h:102
Definition: BaseRenderers.h:282
Definition: MenuItems.h:329
uint16_t getMaximumValue() const
Definition: MenuItems.cpp:90
Definition: BaseRenderers.h:126
uint16_t getCurrentValue() const
Definition: MenuItems.h:438
Definition: BaseGraphicalRenderer.h:131
bool isUseSliderForAnalog() const
Definition: BaseGraphicalRenderer.h:227
int findItemIndex(MenuItem *root, MenuItem *toFind) override
Definition: BaseGraphicalRenderer.cpp:467
virtual void drawMenuItem(GridPositionRowCacheEntry *entry, Coord where, Coord areaSize, const DrawingFlags &drawFlags)=0
void setUseSliderForAnalog(bool useSlider)
Definition: BaseGraphicalRenderer.h:195
TitleMode
Definition: BaseGraphicalRenderer.h:136
@ TITLE_FIRST_ROW
Definition: BaseGraphicalRenderer.h:140
@ NO_TITLE
Definition: BaseGraphicalRenderer.h:138
@ TITLE_ALWAYS
Definition: BaseGraphicalRenderer.h:142
void setEditStatusIconsEnabled(bool ena)
Definition: BaseGraphicalRenderer.h:223
int getWidth() const
Definition: BaseGraphicalRenderer.h:344
int getHeight() const
Definition: BaseGraphicalRenderer.h:349
virtual ItemDisplayPropertiesFactory & getDisplayPropertiesFactory()=0
void rootHasChanged(MenuItem *newItem)
Definition: BaseGraphicalRenderer.cpp:291
RenderDrawingCommand
Definition: BaseGraphicalRenderer.h:147
@ DRAW_COMMAND_ENDED
Definition: BaseGraphicalRenderer.h:153
@ DRAW_COMMAND_START
Definition: BaseGraphicalRenderer.h:151
@ DRAW_COMMAND_CLEAR
Definition: BaseGraphicalRenderer.h:149
void setRawTouchMode(bool rawTouch)
Definition: BaseGraphicalRenderer.h:216
void initialise() override
Definition: BaseGraphicalRenderer.cpp:524
bool isLastRowExactFit() const
Definition: BaseGraphicalRenderer.h:239
uint8_t itemCount(MenuItem *root, bool) override
Definition: BaseGraphicalRenderer.cpp:457
void displayPropertiesHaveChanged()
Definition: BaseGraphicalRenderer.cpp:529
void render() override
Definition: BaseGraphicalRenderer.cpp:12
MenuItem * getCurrentRendererRoot()
Definition: BaseGraphicalRenderer.h:160
virtual void drawingCommand(RenderDrawingCommand command)=0
GridPositionRowCacheEntry * findMenuEntryAndDimensions(const Coord &screenPos, Coord &localStart, Coord &localSize)
Definition: BaseGraphicalRenderer.cpp:114
BaseDialog * getDialog() override
Definition: BaseGraphicalRenderer.cpp:498
void setDisplayDimensions(int w, int h)
Definition: BaseGraphicalRenderer.h:256
void setLastRowExactFit(bool exact)
Definition: BaseGraphicalRenderer.h:211
bool isRawTouchMode() const
Definition: BaseGraphicalRenderer.h:243
virtual void drawWidget(Coord where, TitleWidget *widget, color_t colorFg, color_t colorBg)=0
uint8_t getDisplayNumber()
Definition: BaseGraphicalRenderer.h:189
void setHasTouchInterface(bool hasTouch)
Definition: BaseGraphicalRenderer.h:200
virtual LayoutMode getLayoutMode(MenuItem *rootItem)
Definition: BaseGraphicalRenderer.h:265
MenuItem * getMenuItemAtIndex(MenuItem *currentRoot, uint8_t idx) override
Definition: BaseGraphicalRenderer.cpp:475
virtual void subMenuRender(MenuItem *rootItem, uint8_t &locRedrawMode, bool &forceDrawWidgets)
Definition: BaseGraphicalRenderer.cpp:92
uint8_t setActiveItem(MenuItem *item) override
Definition: BaseGraphicalRenderer.cpp:48
bool isHasTouchInterface() const
Definition: BaseGraphicalRenderer.h:231
bool isTitleOnDisplay() const
Definition: BaseGraphicalRenderer.h:235
void setTitleOnDisplay(bool titleOn)
Definition: BaseGraphicalRenderer.h:205
virtual void fillWithBackgroundTo(int endPoint)=0
void setDisplayNumber(uint8_t displayNum)
Definition: BaseGraphicalRenderer.h:184
bool isEditStatusIconEnabled() const
Definition: BaseGraphicalRenderer.h:247
Definition: BaseGraphicalRenderer.h:107
Definition: BaseGraphicalRenderer.h:37
Definition: GfxMenuConfig.h:69
@ JUSTIFY_TITLE_LEFT_VALUE_RIGHT
Definition: GfxMenuConfig.h:107
GridDrawingMode
Definition: GfxMenuConfig.h:74
@ DRAW_TEXTUAL_ITEM
Definition: GfxMenuConfig.h:76
Definition: BaseGraphicalRenderer.h:60
Definition: GfxMenuConfig.h:358
Definition: GfxMenuConfig.h:245
ComponentType
Definition: GfxMenuConfig.h:260
Definition: BaseGraphicalRenderer.h:99
Definition: MenuHistoryNavigator.h:22
Definition: DrawingPrimitives.h:123