tcMenu
GfxMenuConfig.h
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
14 #ifndef _GFX_MENU_CONFIG_H_
15 #define _GFX_MENU_CONFIG_H_
16 
17 #include <tcUtil.h>
18 #include <SimpleCollections.h>
19 #include "MenuItems.h"
20 #include "DrawingPrimitives.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 {
86  DRAW_TITLE_ITEM
87  };
88 
89  enum GridJustification : uint8_t {
90  CORE_JUSTIFY_LEFT = 1,
91  CORE_JUSTIFY_RIGHT = 2,
92  CORE_JUSTIFY_CENTER = 3,
93 
94  CORE_JUSTIFY_VALUE_REQUIRED = 0b1000,
95  CORE_JUSTIFY_NAME_REQUIRED = 0b0100,
96 
97  JUSTIFY_TITLE_LEFT_VALUE_RIGHT = 0,
98  JUSTIFY_TITLE_LEFT_WITH_VALUE = CORE_JUSTIFY_LEFT + CORE_JUSTIFY_NAME_REQUIRED + CORE_JUSTIFY_VALUE_REQUIRED,
99  JUSTIFY_CENTER_WITH_VALUE = CORE_JUSTIFY_CENTER + CORE_JUSTIFY_NAME_REQUIRED + CORE_JUSTIFY_VALUE_REQUIRED,
100  JUSTIFY_RIGHT_WITH_VALUE = CORE_JUSTIFY_RIGHT + CORE_JUSTIFY_NAME_REQUIRED + CORE_JUSTIFY_VALUE_REQUIRED,
101  JUSTIFY_LEFT_NO_VALUE = CORE_JUSTIFY_LEFT + CORE_JUSTIFY_NAME_REQUIRED,
102  JUSTIFY_CENTER_NO_VALUE = CORE_JUSTIFY_CENTER + CORE_JUSTIFY_NAME_REQUIRED,
103  JUSTIFY_RIGHT_NO_VALUE = CORE_JUSTIFY_RIGHT + CORE_JUSTIFY_NAME_REQUIRED,
104  JUSTIFY_LEFT_VALUE_ONLY= CORE_JUSTIFY_LEFT + CORE_JUSTIFY_VALUE_REQUIRED,
105  JUSTIFY_CENTER_VALUE_ONLY = CORE_JUSTIFY_CENTER + CORE_JUSTIFY_VALUE_REQUIRED,
106  JUSTIFY_RIGHT_VALUE_ONLY = CORE_JUSTIFY_RIGHT + CORE_JUSTIFY_VALUE_REQUIRED
107  };
108  private:
110  uint32_t gridSize: 3;
112  uint32_t gridPosition: 3;
114  uint32_t gridRowSpan: 2;
116  uint32_t gridHeight: 9;
118  uint32_t rowPosition: 7;
120  uint32_t drawingMode: 4;
122  uint32_t justification: 4;
123  public:
124  GridPosition() : gridSize(0), gridPosition(0), gridHeight(0), rowPosition(0), drawingMode(0),
125  justification(JUSTIFY_TITLE_LEFT_VALUE_RIGHT) {}
126 
127  GridPosition(const GridPosition &other) = default;
128  GridPosition& operator=(const GridPosition &other) = default;
129 
135  GridPosition(GridDrawingMode mode, GridJustification justification, int row, int height = 0)
136  : gridSize(1), gridPosition(1), gridHeight(height),rowPosition(row), drawingMode(mode), justification(justification) { }
137 
146  GridPosition(GridDrawingMode mode, GridJustification just, int size, int pos, int row, int hei)
147  : gridSize(size), gridPosition(pos), gridHeight(hei), rowPosition(row), drawingMode(mode), justification(just) { }
148 
149  GridDrawingMode getDrawingMode() const { return static_cast<GridDrawingMode>(drawingMode); }
150 
151  GridJustification getJustification() const { return static_cast<GridJustification>(justification); }
152 
153  int getGridSize() const { return gridSize; }
154 
155  int getGridHeight() const { return gridHeight; }
156 
157  int getGridPosition() const { return gridPosition; }
158 
159  int getRow() const { return rowPosition; }
160  };
161 
167  inline bool itemNeedsValue(GridPosition::GridJustification justification) {
168  return (justification & GridPosition::CORE_JUSTIFY_VALUE_REQUIRED) != 0;
169  }
170 
176  inline bool itemNeedsName(GridPosition::GridJustification justification) {
177  return (justification & GridPosition::CORE_JUSTIFY_NAME_REQUIRED) != 0;
178  }
179 
185  inline GridPosition::GridJustification coreJustification(GridPosition::GridJustification j) {
186  return static_cast<GridPosition::GridJustification>(j & 0b11);
187  }
188 
196  private:
197  uint16_t menuId;
198  GridPosition thePosition;
199  public:
200  GridPositionWithId() : menuId(0xffff), thePosition(GridPosition::DRAW_TEXTUAL_ITEM,
201  GridPosition::JUSTIFY_TITLE_LEFT_VALUE_RIGHT, 0) {}
202 
203  GridPositionWithId(const GridPositionWithId &other) = default;
204  GridPositionWithId& operator=(const GridPositionWithId &other) = default;
205 
206  GridPositionWithId(uint16_t itemId, const GridPosition &pos) : menuId(itemId), thePosition(pos) {}
207 
208  const GridPosition &getPosition() { return thePosition; }
209 
210  uint16_t getKey() const { return menuId; }
211 
212  void setNewPosition(GridPosition newPosition) {
213  thePosition = newPosition;
214  }
215  };
216 
223  public:
227  enum ColorType: uint8_t {
228  TEXT,
229  BACKGROUND,
230  HIGHLIGHT1,
231  HIGHLIGHT2,
232  SIZEOF_COLOR_ARRAY
233  };
238  COMPTYPE_TITLE,
239  COMPTYPE_ITEM,
240  COMPTYPE_ACTION,
241  };
242  private:
243  uint32_t propsKey;
244  color_t colors[SIZEOF_COLOR_ARRAY];
245  MenuPadding padding;
246  const void* fontData;
247  MenuBorder borderWidths;
248  uint8_t fontMagnification: 4;
249  uint8_t defaultJustification: 4;
250  uint8_t spaceAfter;
251  uint8_t requiredHeight;
252  public:
253  ItemDisplayProperties() : propsKey(0), colors{}, padding(), fontData(nullptr), borderWidths(), fontMagnification(1), defaultJustification(0), spaceAfter(0), requiredHeight(0) {}
254  ItemDisplayProperties(uint32_t key, const color_t* palette, const MenuPadding& pad, const void* font, uint8_t mag, uint8_t spacing,
255  uint8_t height, GridPosition::GridJustification defaultJustification, MenuBorder borderWidths)
256  : propsKey(key), padding{pad}, fontData(font), borderWidths(borderWidths), fontMagnification(mag), defaultJustification(defaultJustification),
257  spaceAfter(spacing), requiredHeight(height) {
258  memcpy(colors, palette, sizeof colors);
259  }
260  ItemDisplayProperties(const ItemDisplayProperties& other) : propsKey(other.propsKey), padding{other.padding}, fontData(other.fontData),
261  borderWidths(other.borderWidths), fontMagnification(other.fontMagnification), defaultJustification(other.defaultJustification),
262  spaceAfter(other.spaceAfter), requiredHeight(other.requiredHeight) {
263  memcpy(colors, other.colors, sizeof colors);
264  }
265  ItemDisplayProperties& operator=(const ItemDisplayProperties& other) {
266  if(&other == this) return *this;
267  propsKey = other.propsKey;
268  padding = other.padding;
269  fontData = other.fontData;
270  borderWidths = other.borderWidths;
271  fontMagnification = other.fontMagnification;
272  defaultJustification = other.defaultJustification;
273  spaceAfter = other.spaceAfter;
274  requiredHeight = other.requiredHeight;
275  memcpy(colors, other.colors, sizeof colors);
276  return *this;
277  }
278 
279  uint32_t getKey() const { return propsKey; }
280 
281  GridPosition::GridJustification getDefaultJustification() const { return (GridPosition::GridJustification)defaultJustification; }
282 
283  void setDefaultJustification(GridPosition::GridJustification justification) { defaultJustification = justification; }
284 
285  uint8_t getSpaceAfter() const {return spaceAfter; }
286 
287  void setSpaceAfter(uint8_t space) { spaceAfter = space; }
288 
289  uint8_t getRequiredHeight() const { return requiredHeight; }
290 
291  void setRequiredHeight(uint8_t newHeight) { requiredHeight = newHeight; }
292 
293  color_t getColor(ColorType color) const {
294  return (color < SIZEOF_COLOR_ARRAY) ? colors[color] : RGB(0,0,0);
295  }
296 
297  void setColor(ColorType color, color_t value) {
298  if(color >= SIZEOF_COLOR_ARRAY) return;
299  colors[color] = value;
300  }
301 
302  void setColors(const color_t* palette) {
303  memcpy(colors, palette, sizeof colors);
304  }
305 
306  const MenuPadding& getPadding() const {
307  return padding;
308  }
309 
310  void setPadding(const MenuPadding& pad) {
311  padding = pad;
312  }
313 
314  MenuBorder getBorder() const {
315  return borderWidths;
316  }
317 
318  void setBorder(MenuBorder border) {
319  borderWidths = border;
320  }
321 
322  uint8_t getFontMagnification() const {
323  return fontMagnification;
324  }
325 
326  void setFontInfo(const void* font, uint8_t mag) {
327  fontMagnification = mag;
328  fontData = font;
329  }
330 
331  const void* getFont() {
332  return fontData;
333  }
334 
335  color_t* getPalette() {
336  return colors;
337  }
338  };
339 
349  public:
356  virtual ItemDisplayProperties* configFor(MenuItem* pItem, ItemDisplayProperties::ComponentType compType) = 0;
357 
363  virtual DrawableIcon* iconForMenuItem(uint16_t id) = 0;
364 
370  virtual GridPositionWithId* gridPositionForItem(MenuItem* pItem) = 0;
371 
377  virtual color_t getSelectedColor(ItemDisplayProperties::ColorType colorType) = 0;
378 
384  virtual void addGridPosition(MenuItem* item, const GridPosition& position) = 0;
385  };
386 
395  private:
396  color_t anEmptyPalette[4] = {0};
397  ItemDisplayProperties props = ItemDisplayProperties(0, anEmptyPalette, MenuPadding(0), nullptr, 1, 0, 1, GridPosition::JUSTIFY_TITLE_LEFT_VALUE_RIGHT, MenuBorder());
398  BtreeList<uint16_t, GridPositionWithId> gridByItem;
399  public:
400  NullItemDisplayPropertiesFactory() : gridByItem(4) {}
401 
403  return &props;
404  }
405 
406  DrawableIcon* iconForMenuItem(uint16_t id) override{
407  return nullptr;
408  }
409 
411  return 0;
412  }
413 
415  if(!pItem) return nullptr;
416  return gridByItem.getByKey(pItem->getId());
417  }
418 
419  void addGridPosition(MenuItem* pItem, const GridPosition& position) override {
420  if(!pItem) return;
421  auto* grid = gridByItem.getByKey(pItem->getId());
422  if(grid) {
423  grid->setNewPosition(position);
424  }
425  else {
426  serdebugF2("Adding grid ", pItem->getId());
427  gridByItem.add(GridPositionWithId(pItem->getId(), position));
428  }
429  }
430  };
431 
432 #define MENUID_NOTSET 0xffff
433 
434  inline uint32_t MakePropsKey(uint16_t menuId, bool parentKey, ItemDisplayProperties::ComponentType ty) {
435  return (uint32_t)menuId | (parentKey ? 0x10000UL : 0UL) | ((uint32_t)ty << 18UL);
436  }
437 
447  private:
448  BtreeList<uint32_t, ItemDisplayProperties> displayProperties;
449  BtreeList<uint16_t, DrawableIcon> iconsByItem;
450  BtreeList<uint16_t, GridPositionWithId> gridByItem;
451  color_t selectedTextColor = RGB(0,0,0);
452  color_t selectedBackgroundColor = RGB(0, 100, 255);
453  public:
455  : displayProperties(5, GROW_BY_5),
456  iconsByItem(6, GROW_BY_5) { }
457 
463  DrawableIcon* iconForMenuItem(uint16_t id) override {
464  return iconsByItem.getByKey(id);
465  }
466 
473  if(!pItem) return nullptr;
474  return gridByItem.getByKey(pItem->getId());
475  }
476 
485  ItemDisplayProperties* configFor(MenuItem* pItem, ItemDisplayProperties::ComponentType compType) override;
486 
493  return colorType == ItemDisplayProperties::BACKGROUND ? selectedBackgroundColor : selectedTextColor;
494  }
495 
502  void addGridPosition(MenuItem* pItem, const GridPosition& position) override {
503  if(!pItem) return;
504  auto* grid = gridByItem.getByKey(pItem->getId());
505  if(grid) {
506  grid->setNewPosition(position);
507  }
508  else {
509  gridByItem.add(GridPositionWithId(pItem->getId(), position));
510  }
511  }
512 
518  void addImageToCache(const DrawableIcon& toAdd) {
519  auto* current = iconsByItem.getByKey(toAdd.getKey());
520  if(current) {
521  current->setFromValues(toAdd.getDimensions(), toAdd.getIconType(), toAdd.getIcon(false), toAdd.getIcon(true));
522  }
523  else {
524  iconsByItem.add(toAdd);
525  }
526  }
527 
540  void setDrawingPropertiesDefault(ItemDisplayProperties::ComponentType drawing, const color_t* palette, MenuPadding pad, const void *font, uint8_t mag,
541  uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border) {
542  setDrawingProperties(MakePropsKey(MENUID_NOTSET, false, drawing), palette, pad, font, mag, spacing, requiredHeight, defaultJustification, border);
543  }
544 
558  void setDrawingPropertiesForItem(ItemDisplayProperties::ComponentType drawing, uint16_t id, const color_t* palette, MenuPadding pad, const void *font, uint8_t mag,
559  uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border) {
560  setDrawingProperties(MakePropsKey(id, false, drawing), palette, pad, font, mag, spacing, requiredHeight, defaultJustification, border);
561  }
562 
576  void setDrawingPropertiesAllInSub(ItemDisplayProperties::ComponentType drawing, uint16_t id, const color_t* palette, MenuPadding pad, const void *font, uint8_t mag,
577  uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border) {
578  setDrawingProperties(MakePropsKey(id, true, drawing), palette, pad, font, mag, spacing, requiredHeight, defaultJustification, border);
579  }
580 
586  void setSelectedColors(color_t background, color_t text) {
587  selectedBackgroundColor = background;
588  selectedTextColor = text;
589  }
590 
595  static void refreshCache();
596  private:
597  void setDrawingProperties(uint32_t key, const color_t* palette, MenuPadding pad, const void* font, uint8_t mag, uint8_t spacing,
598  uint8_t requiredHeight, GridPosition::GridJustification defaultJustification, MenuBorder border);
599 
600  };
601 
602 } // namespace tcgfx
603 
607 extern const uint8_t PROGMEM loResEditingIcon[];
608 
612 extern const uint8_t PROGMEM loResActiveIcon[];
613 
617 extern const uint8_t PROGMEM defActiveIcon[];
618 
622 extern const uint8_t PROGMEM defEditingIcon[];
623 
624 #endif // _GFX_MENU_CONFIG_H_
GridPositionWithId * gridPositionForItem(MenuItem *pItem) override
Definition: GfxMenuConfig.h:472
Definition: MenuItems.h:329
void addGridPosition(MenuItem *pItem, const GridPosition &position) override
Definition: GfxMenuConfig.h:502
Definition: GfxMenuConfig.h:348
Definition: DrawingPrimitives.h:181
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:540
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:558
ComponentType
Definition: GfxMenuConfig.h:237
const uint8_t iconWifiNotConnected [] PROGMEM
Definition: wifiAndConnectionIcons16x12.h:23
color_t getSelectedColor(ItemDisplayProperties::ColorType colorType) override
Definition: GfxMenuConfig.h:410
color_t getSelectedColor(ItemDisplayProperties::ColorType colorType) override
Definition: GfxMenuConfig.h:492
Definition: GfxMenuConfig.h:76
Definition: GfxMenuConfig.h:82
menuid_t getId() const
Definition: MenuItems.cpp:74
Definition: BaseGraphicalRenderer.cpp:10
const uint8_t * getIcon(bool selected) const
Definition: DrawingPrimitives.h:252
Definition: GfxMenuConfig.h:394
void addGridPosition(MenuItem *pItem, const GridPosition &position) override
Definition: GfxMenuConfig.h:419
IconType getIconType() const
Definition: DrawingPrimitives.h:266
DrawableIcon * iconForMenuItem(uint16_t id) override
Definition: GfxMenuConfig.h:406
Definition: DrawingPrimitives.h:53
void addImageToCache(const DrawableIcon &toAdd)
Definition: GfxMenuConfig.h:518
Definition: GfxMenuConfig.h:222
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:576
Definition: GfxMenuConfig.h:195
ColorType
Definition: GfxMenuConfig.h:227
DrawableIcon * iconForMenuItem(uint16_t id) override
Definition: GfxMenuConfig.h:463
Definition: GfxMenuConfig.h:80
Definition: DrawingPrimitives.h:89
GridPosition(GridDrawingMode mode, GridJustification just, int size, int pos, int row, int hei)
Definition: GfxMenuConfig.h:146
GridDrawingMode
Definition: GfxMenuConfig.h:74
GridPosition(GridDrawingMode mode, GridJustification justification, int row, int height=0)
Definition: GfxMenuConfig.h:135
Definition: GfxMenuConfig.h:69
Definition: GfxMenuConfig.h:78
ItemDisplayProperties * configFor(MenuItem *pItem, ItemDisplayProperties::ComponentType compType) override
Definition: GfxMenuConfig.h:402
Definition: GfxMenuConfig.h:84
uint32_t color_t
Definition: DrawingPrimitives.h:26
Definition: GfxMenuConfig.h:32
GridPositionWithId * gridPositionForItem(MenuItem *pItem) override
Definition: GfxMenuConfig.h:414
Coord getDimensions() const
Definition: DrawingPrimitives.h:259
void setSelectedColors(color_t background, color_t text)
Definition: GfxMenuConfig.h:586