tcMenu
GfxMenuConfig.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 
13 #ifndef _GFX_MENU_CONFIG_H_
14 #define _GFX_MENU_CONFIG_H_
15 
16 #include <tcUtil.h>
17 #include <SimpleCollections.h>
18 #include "MenuItems.h"
19 #include "DrawingPrimitives.h"
20 #include <IoLogging.h>
21 
22 
23 namespace tcgfx {
24 
25 #define SPECIAL_ID_EDIT_ICON 0xfffe
26 #define SPECIAL_ID_ACTIVE_ICON 0xfffd
27 
32  template<typename FONTPTR> struct ColorGfxMenuConfig {
33  color_t bgTitleColor;
34  color_t fgTitleColor;
35  MenuPadding titlePadding;
36  FONTPTR titleFont;
37 
38  color_t bgItemColor;
39  color_t fgItemColor;
40  MenuPadding itemPadding;
41  FONTPTR itemFont;
42 
43  color_t bgSelectColor;
44  color_t fgSelectColor;
45  color_t widgetColor;
46  MenuPadding widgetPadding;
47 
48  const uint8_t* activeIcon;
49  const uint8_t* editIcon;
50  uint8_t editIconWidth;
51  uint8_t editIconHeight;
52 
53  uint8_t titleBottomMargin;
54  uint8_t titleFontMagnification;
55  uint8_t itemFontMagnification;
56  };
57 
62  void prepareDefaultGfxConfig(ColorGfxMenuConfig<void*>* config);
63 
64 
69  class GridPosition {
70  public:
74  enum GridDrawingMode : uint8_t {
87  };
88 
93  enum GridJustification : uint8_t {
100 
105 
126  };
127  private:
129  uint32_t gridSize: 3;
131  uint32_t gridPosition: 3;
133  uint32_t gridRowSpan: 2;
135  uint32_t gridHeight: 9;
137  uint32_t rowPosition: 7;
139  uint32_t drawingMode: 4;
141  uint32_t justification: 4;
142  public:
143  GridPosition() : gridSize(0), gridPosition(0), gridHeight(0), rowPosition(0), drawingMode(0),
144  justification(JUSTIFY_TITLE_LEFT_VALUE_RIGHT) {}
145 
146  GridPosition(const GridPosition &other) = default;
147  GridPosition& operator=(const GridPosition &other) = default;
148 
156  GridPosition(GridDrawingMode mode, GridJustification justification, int row, int height = 0)
157  : gridSize(1), gridPosition(1), gridHeight(height),rowPosition(row), drawingMode(mode), justification(justification) { }
158 
169  GridPosition(GridDrawingMode mode, GridJustification just, int size, int pos, int row, int hei)
170  : gridSize(size), gridPosition(pos), gridHeight(hei), rowPosition(row), drawingMode(mode), justification(just) { }
171 
172  GridDrawingMode getDrawingMode() const { return static_cast<GridDrawingMode>(drawingMode); }
173 
174  GridJustification getJustification() const { return static_cast<GridJustification>(justification); }
175 
176  int getGridSize() const { return gridSize; }
177 
178  int getGridHeight() const { return gridHeight; }
179 
180  int getGridPosition() const { return gridPosition; }
181 
182  int getRow() const { return rowPosition; }
183  };
184 
190  inline bool itemNeedsValue(GridPosition::GridJustification justification) {
191  return justification == GridPosition::JUSTIFY_TITLE_LEFT_VALUE_RIGHT || (justification & GridPosition::CORE_JUSTIFY_VALUE_REQUIRED) != 0;
192  }
193 
199  inline bool itemNeedsName(GridPosition::GridJustification justification) {
200  return (justification & GridPosition::CORE_JUSTIFY_NAME_REQUIRED) != 0;
201  }
202 
209  return static_cast<GridPosition::GridJustification>(j & 0b11);
210  }
211 
219  private:
220  uint16_t menuId;
221  GridPosition thePosition;
222  public:
223  GridPositionWithId() : menuId(0xffff), thePosition(GridPosition::DRAW_TEXTUAL_ITEM,
225 
226  GridPositionWithId(const GridPositionWithId &other) = default;
227  GridPositionWithId& operator=(const GridPositionWithId &other) = default;
228 
229  GridPositionWithId(uint16_t itemId, const GridPosition &pos) : menuId(itemId), thePosition(pos) {}
230 
231  const GridPosition &getPosition() { return thePosition; }
232 
233  uint16_t getKey() const { return menuId; }
234 
235  void setNewPosition(GridPosition newPosition) {
236  thePosition = newPosition;
237  }
238  };
239 
246  public:
250  enum ColorType: uint8_t {
251  TEXT,
252  BACKGROUND,
253  HIGHLIGHT1,
254  HIGHLIGHT2,
255  SIZEOF_COLOR_ARRAY
256  };
261  COMPTYPE_TITLE,
262  COMPTYPE_ITEM,
263  COMPTYPE_ACTION,
264  };
265  private:
266  uint32_t propsKey;
267  color_t colors[SIZEOF_COLOR_ARRAY];
268  MenuPadding padding;
269  const void* fontData;
270  MenuBorder borderWidths;
271  uint8_t fontMagnification;
272  GridPosition::GridJustification defaultJustification;
273  uint8_t spaceAfter;
274  uint8_t requiredHeight;
275  public:
276  ItemDisplayProperties() : propsKey(0), colors{}, padding(), fontData(nullptr), borderWidths(), fontMagnification(1),
277  defaultJustification(GridPosition::JUSTIFY_TITLE_LEFT_VALUE_RIGHT), spaceAfter(0), requiredHeight(0) {}
278  ItemDisplayProperties(uint32_t key, const color_t* palette, const MenuPadding& pad, const void* font, uint8_t mag, uint8_t spacing,
279  uint8_t height, GridPosition::GridJustification defaultJustification, MenuBorder borderWidths)
280  : propsKey(key), padding{pad}, fontData(font), borderWidths(borderWidths), fontMagnification(mag), defaultJustification(defaultJustification),
281  spaceAfter(spacing), requiredHeight(height) {
282  memcpy(colors, palette, sizeof colors);
283  }
284  ItemDisplayProperties(const ItemDisplayProperties& other) = default;
285  ItemDisplayProperties& operator=(const ItemDisplayProperties& other) = default;
286 
287  uint32_t getKey() const { return propsKey; }
288 
289  GridPosition::GridJustification getDefaultJustification() const {
290  return defaultJustification;
291  }
292 
293  void setDefaultJustification(GridPosition::GridJustification justification) { defaultJustification = justification; }
294 
295  uint8_t getSpaceAfter() const {return spaceAfter; }
296 
297  void setSpaceAfter(uint8_t space) { spaceAfter = space; }
298 
299  uint8_t getRequiredHeight() const { return requiredHeight; }
300 
301  void setRequiredHeight(uint8_t newHeight) { requiredHeight = newHeight; }
302 
303  color_t getColor(ColorType color) const {
304  return (color < SIZEOF_COLOR_ARRAY) ? colors[color] : RGB(0,0,0);
305  }
306 
307  void setColor(ColorType color, color_t value) {
308  if(color >= SIZEOF_COLOR_ARRAY) return;
309  colors[color] = value;
310  }
311 
312  void setColors(const color_t* palette) {
313  memcpy(colors, palette, sizeof colors);
314  }
315 
316  const MenuPadding& getPadding() const {
317  return padding;
318  }
319 
320  void setPadding(const MenuPadding& pad) {
321  padding = pad;
322  }
323 
324  MenuBorder getBorder() const {
325  return borderWidths;
326  }
327 
328  void setBorder(MenuBorder border) {
329  borderWidths = border;
330  }
331 
332  uint8_t getFontMagnification() const {
333  return fontMagnification;
334  }
335 
336  void setFontInfo(const void* font, uint8_t mag) {
337  fontMagnification = mag;
338  fontData = font;
339  }
340 
341  const void* getFont() {
342  return fontData;
343  }
344 
345  color_t* getPalette() {
346  return colors;
347  }
348  };
349 
359  public:
367 
376 
382  virtual DrawableIcon* iconForMenuItem(uint16_t id) = 0;
383 
390 
396  virtual color_t getSelectedColor(ItemDisplayProperties::ColorType colorType, bool isUnderCursor = false) = 0;
397 
403  virtual void addGridPosition(MenuItem* item, const GridPosition& position) = 0;
404  };
405 
414  private:
415  color_t anEmptyPalette[4] = {0};
417  BtreeList<uint16_t, GridPositionWithId> gridByItem;
418  public:
419  NullItemDisplayPropertiesFactory() : gridByItem(4) {}
420 
422  return &props;
423  }
424 
426  return &props;
427  }
428 
429  DrawableIcon* iconForMenuItem(uint16_t /*id*/) override{
430  return nullptr;
431  }
432 
433  color_t getSelectedColor(ItemDisplayProperties::ColorType colorType, bool /*isUnderCursor*/ = false) override {
434  return 0;
435  }
436 
438  if(!pItem) return nullptr;
439  return gridByItem.getByKey(pItem->getId());
440  }
441 
442  void addGridPosition(MenuItem* pItem, const GridPosition& position) override {
443  if(!pItem) return;
444  auto* grid = gridByItem.getByKey(pItem->getId());
445  if(grid) {
446  grid->setNewPosition(position);
447  }
448  else {
449  serlogF2(SER_TCMENU_DEBUG, "Adding grid ", pItem->getId());
450  gridByItem.add(GridPositionWithId(pItem->getId(), position));
451  }
452  }
453  };
454 
455 #define MENUID_NOTSET 0xffff
456 
457  inline uint32_t MakePropsKey(uint16_t menuId, bool parentKey, ItemDisplayProperties::ComponentType ty) {
458  return ((uint32_t)menuId) | (parentKey ? 0x10000UL : 0UL) | ((uint32_t)ty << 18UL);
459  }
460 
469  };
470 
480  private:
481  BtreeList<uint32_t, ItemDisplayProperties> displayProperties;
482  BtreeList<uint16_t, DrawableIcon> iconsByItem;
483  BtreeList<uint16_t, GridPositionWithId> gridByItem;
484  color_t selectedTextColor = RGB(0,0,0);
485  color_t selectedBackgroundColor = RGB(0, 100, 255);
486  color_t editCursorColor = RGB(255, 255, 255);
487  color_t editCursorTextColor = RGB(0, 0, 0);
488  EditCursorMode editCursorMode = CURSOR_MODE_UNDERLINE;
489  public:
491  : displayProperties(5, GROW_BY_5),
492  iconsByItem(6, GROW_BY_5) { }
493 
499  DrawableIcon* iconForMenuItem(uint16_t id) override {
500  return iconsByItem.getByKey(id);
501  }
502 
509  if(!pItem) return nullptr;
510  return gridByItem.getByKey(pItem->getId());
511  }
512 
522 
531 
537  color_t getSelectedColor(ItemDisplayProperties::ColorType colorType, bool isUnderCursor = false) override {
538  if(isUnderCursor) {
539  return colorType == ItemDisplayProperties::BACKGROUND ? editCursorColor : editCursorTextColor;
540  }
541  return colorType == ItemDisplayProperties::BACKGROUND ? selectedBackgroundColor : selectedTextColor;
542  }
543 
550  void addGridPosition(MenuItem* pItem, const GridPosition& position) override {
551  if(!pItem) return;
552  auto* grid = gridByItem.getByKey(pItem->getId());
553  if(grid) {
554  grid->setNewPosition(position);
555  }
556  else {
557  gridByItem.add(GridPositionWithId(pItem->getId(), position));
558  }
559  }
560 
566  void addImageToCache(const DrawableIcon& toAdd) {
567  auto* current = iconsByItem.getByKey(toAdd.getKey());
568  if(current) {
569  current->setFromValues(toAdd.getDimensions(), toAdd.getIconType(), toAdd.getIcon(false), toAdd.getIcon(true));
570  }
571  else {
572  iconsByItem.add(toAdd);
573  }
574  }
575 
588  void setDrawingPropertiesDefault(ItemDisplayProperties::ComponentType drawing, const color_t* palette, MenuPadding pad, const void *font, uint8_t mag,
589  uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border) {
590 
591  auto key = MakePropsKey(MENUID_NOTSET, false, drawing);
592  serlogF2(SER_TCMENU_DEBUG, "Add def prop: ", key);
593  setDrawingProperties(key, palette, pad, font, mag, spacing, requiredHeight, defaultJustification, border);
594  }
595 
609  void setDrawingPropertiesForItem(ItemDisplayProperties::ComponentType drawing, uint16_t id, const color_t* palette, MenuPadding pad, const void *font, uint8_t mag,
610  uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border) {
611  auto key = MakePropsKey(id, false, drawing);
612  serlogF3(SER_TCMENU_DEBUG, "Add item prop: ", key, id);
613  setDrawingProperties(key, palette, pad, font, mag, spacing, requiredHeight, defaultJustification, border);
614  }
615 
629  void setDrawingPropertiesAllInSub(ItemDisplayProperties::ComponentType drawing, uint16_t id, const color_t* palette, MenuPadding pad, const void *font, uint8_t mag,
630  uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border) {
631  auto key = MakePropsKey(id, true, drawing);
632  serlogF3(SER_TCMENU_DEBUG, "Add sub prop: ", key, id);
633  setDrawingProperties(key, palette, pad, font, mag, spacing, requiredHeight, defaultJustification, border);
634  }
635 
641  void setSelectedColors(color_t background, color_t text) {
642  selectedBackgroundColor = background;
643  selectedTextColor = text;
644  }
645 
652  void setEditCursorMode(EditCursorMode mode, color_t cursorCol, color_t textCol) {
653  editCursorMode = mode;
654  editCursorColor = cursorCol;
655  editCursorTextColor = textCol;
656  }
657 
662  static void refreshCache();
663 
664  private:
665  void setDrawingProperties(uint32_t key, const color_t* palette, MenuPadding pad, const void* font, uint8_t mag, uint8_t spacing,
666  uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border);
667 
668  };
669 
670 } // namespace tcgfx
671 
675 extern const uint8_t PROGMEM loResEditingIcon[];
676 
680 extern const uint8_t PROGMEM loResActiveIcon[];
681 
685 extern const uint8_t PROGMEM defActiveIcon[];
686 
690 extern const uint8_t PROGMEM defEditingIcon[];
691 
692 #endif // _GFX_MENU_CONFIG_H_
contains a series of core components needed by all graphical renderers
uint32_t color_t
Definition: DrawingPrimitives.h:29
In TcMenu, MenuItem storage is shared between program memory and RAM. Usually each MenuItem has assoc...
Definition: MenuItems.h:329
menuid_t getId() const
Definition: MenuItems.cpp:81
void setDrawingPropertiesAllInSub(ItemDisplayProperties::ComponentType drawing, uint16_t id, const color_t *palette, MenuPadding pad, const void *font, uint8_t mag, uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border)
Definition: GfxMenuConfig.h:629
static void refreshCache()
Definition: GfxMenuConfig.cpp:128
ItemDisplayProperties * configFor(MenuItem *pItem, ItemDisplayProperties::ComponentType compType) override
Definition: GfxMenuConfig.cpp:66
DrawableIcon * iconForMenuItem(uint16_t id) override
Definition: GfxMenuConfig.h:499
void setEditCursorMode(EditCursorMode mode, color_t cursorCol, color_t textCol)
Definition: GfxMenuConfig.h:652
color_t getSelectedColor(ItemDisplayProperties::ColorType colorType, bool isUnderCursor=false) override
Definition: GfxMenuConfig.h:537
void addImageToCache(const DrawableIcon &toAdd)
Definition: GfxMenuConfig.h:566
void addGridPosition(MenuItem *pItem, const GridPosition &position) override
Definition: GfxMenuConfig.h:550
GridPositionWithId * gridPositionForItem(MenuItem *pItem) override
Definition: GfxMenuConfig.h:508
void setDrawingPropertiesForItem(ItemDisplayProperties::ComponentType drawing, uint16_t id, const color_t *palette, MenuPadding pad, const void *font, uint8_t mag, uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border)
Definition: GfxMenuConfig.h:609
void setSelectedColors(color_t background, color_t text)
Definition: GfxMenuConfig.h:641
void setDrawingPropertiesDefault(ItemDisplayProperties::ComponentType drawing, const color_t *palette, MenuPadding pad, const void *font, uint8_t mag, uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border)
Definition: GfxMenuConfig.h:588
ItemDisplayProperties * configForCurrentSub(ItemDisplayProperties::ComponentType compType) override
Definition: GfxMenuConfig.cpp:85
Definition: DrawingPrimitives.h:152
const uint8_t * getIcon(bool selected) const
Definition: DrawingPrimitives.h:228
Coord getDimensions() const
Definition: DrawingPrimitives.h:243
IconType getIconType() const
Definition: DrawingPrimitives.h:250
Definition: GfxMenuConfig.h:69
GridJustification
Definition: GfxMenuConfig.h:93
@ JUSTIFY_TITLE_LEFT_VALUE_RIGHT
Definition: GfxMenuConfig.h:107
@ CORE_JUSTIFY_VALUE_REQUIRED
Definition: GfxMenuConfig.h:102
@ JUSTIFY_LEFT_NO_VALUE
Definition: GfxMenuConfig.h:115
@ JUSTIFY_CENTER_WITH_VALUE
Definition: GfxMenuConfig.h:111
@ CORE_JUSTIFY_RIGHT
Definition: GfxMenuConfig.h:97
@ JUSTIFY_RIGHT_WITH_VALUE
Definition: GfxMenuConfig.h:113
@ JUSTIFY_RIGHT_NO_VALUE
Definition: GfxMenuConfig.h:119
@ CORE_JUSTIFY_CENTER
Definition: GfxMenuConfig.h:99
@ JUSTIFY_LEFT_VALUE_ONLY
Definition: GfxMenuConfig.h:121
@ JUSTIFY_CENTER_VALUE_ONLY
Definition: GfxMenuConfig.h:123
@ JUSTIFY_CENTER_NO_VALUE
Definition: GfxMenuConfig.h:117
@ CORE_JUSTIFY_NAME_REQUIRED
Definition: GfxMenuConfig.h:104
@ CORE_JUSTIFY_LEFT
Definition: GfxMenuConfig.h:95
@ JUSTIFY_TITLE_LEFT_WITH_VALUE
Definition: GfxMenuConfig.h:109
@ JUSTIFY_RIGHT_VALUE_ONLY
Definition: GfxMenuConfig.h:125
GridPosition(GridDrawingMode mode, GridJustification justification, int row, int height=0)
Definition: GfxMenuConfig.h:156
GridDrawingMode
Definition: GfxMenuConfig.h:74
@ DRAW_TEXTUAL_ITEM
Definition: GfxMenuConfig.h:76
@ DRAW_AS_ICON_TEXT
Definition: GfxMenuConfig.h:84
@ DRAW_TITLE_ITEM
Definition: GfxMenuConfig.h:86
@ DRAW_INTEGER_AS_UP_DOWN
Definition: GfxMenuConfig.h:78
@ DRAW_AS_ICON_ONLY
Definition: GfxMenuConfig.h:82
@ DRAW_INTEGER_AS_SCROLL
Definition: GfxMenuConfig.h:80
GridPosition(GridDrawingMode mode, GridJustification just, int size, int pos, int row, int hei)
Definition: GfxMenuConfig.h:169
Definition: GfxMenuConfig.h:218
Definition: GfxMenuConfig.h:358
virtual DrawableIcon * iconForMenuItem(uint16_t id)=0
virtual color_t getSelectedColor(ItemDisplayProperties::ColorType colorType, bool isUnderCursor=false)=0
virtual GridPositionWithId * gridPositionForItem(MenuItem *pItem)=0
virtual ItemDisplayProperties * configForCurrentSub(ItemDisplayProperties::ComponentType compType)=0
virtual ItemDisplayProperties * configFor(MenuItem *pItem, ItemDisplayProperties::ComponentType compType)=0
virtual void addGridPosition(MenuItem *item, const GridPosition &position)=0
Definition: GfxMenuConfig.h:245
ComponentType
Definition: GfxMenuConfig.h:260
ColorType
Definition: GfxMenuConfig.h:250
Definition: GfxMenuConfig.h:413
void addGridPosition(MenuItem *pItem, const GridPosition &position) override
Definition: GfxMenuConfig.h:442
ItemDisplayProperties * configForCurrentSub(ItemDisplayProperties::ComponentType) override
Definition: GfxMenuConfig.h:425
DrawableIcon * iconForMenuItem(uint16_t) override
Definition: GfxMenuConfig.h:429
color_t getSelectedColor(ItemDisplayProperties::ColorType colorType, bool=false) override
Definition: GfxMenuConfig.h:433
GridPositionWithId * gridPositionForItem(MenuItem *pItem) override
Definition: GfxMenuConfig.h:437
ItemDisplayProperties * configFor(MenuItem *pItem, ItemDisplayProperties::ComponentType) override
Definition: GfxMenuConfig.h:421
const uint8_t ArrowHoriz11x22BitmapLeft[] PROGMEM
Definition: directionalIcons.h:21
GridPosition::GridJustification coreJustification(GridPosition::GridJustification j)
Definition: GfxMenuConfig.h:208
const uint8_t PROGMEM loResActiveIcon[]
Definition: GfxMenuConfig.cpp:36
const uint8_t PROGMEM loResEditingIcon[]
Definition: GfxMenuConfig.cpp:31
const uint8_t PROGMEM defEditingIcon[]
bool itemNeedsValue(GridPosition::GridJustification justification)
Definition: GfxMenuConfig.h:190
EditCursorMode
Definition: GfxMenuConfig.h:464
@ CURSOR_MODE_BACKGROUND_BOX
Definition: GfxMenuConfig.h:468
@ CURSOR_MODE_UNDERLINE
Definition: GfxMenuConfig.h:466
bool itemNeedsName(GridPosition::GridJustification justification)
Definition: GfxMenuConfig.h:199
const uint8_t PROGMEM defActiveIcon[]
Definition: GfxMenuConfig.cpp:23
Definition: GfxMenuConfig.h:32
Definition: DrawingPrimitives.h:92
Definition: DrawingPrimitives.h:56
A series of utilities that used throughout tcMenu.