tcMenu
GfxMenuConfig.h
Go to the documentation of this file.
1 #ifndef _GFX_MENU_CONFIG_H_
2 #define _GFX_MENU_CONFIG_H_
3 
4 #include <tcUtil.h>
5 #include <SimpleCollections.h>
6 #include "MenuItems.h"
7 #include "DrawingPrimitives.h"
8 
9 
18 namespace tcgfx {
19 
20 #define SPECIAL_ID_EDIT_ICON 0xfffe
21 #define SPECIAL_ID_ACTIVE_ICON 0xfffd
22 
27  template<typename FONTPTR> struct ColorGfxMenuConfig {
28  color_t bgTitleColor;
29  color_t fgTitleColor;
30  MenuPadding titlePadding;
31  FONTPTR titleFont;
32 
33  color_t bgItemColor;
34  color_t fgItemColor;
35  MenuPadding itemPadding;
36  FONTPTR itemFont;
37 
38  color_t bgSelectColor;
39  color_t fgSelectColor;
40  color_t widgetColor;
41  MenuPadding widgetPadding;
42 
43  const uint8_t* activeIcon;
44  const uint8_t* editIcon;
45  uint8_t editIconWidth;
46  uint8_t editIconHeight;
47 
48  uint8_t titleBottomMargin;
49  uint8_t titleFontMagnification;
50  uint8_t itemFontMagnification;
51  };
52 
57  class GridPosition {
58  public:
62  enum GridDrawingMode : byte {
74  DRAW_TITLE_ITEM
75  };
76 
77  enum GridJustification : byte {
78  JUSTIFY_TITLE_LEFT_VALUE_RIGHT,
79  JUSTIFY_TITLE_LEFT_WITH_VALUE,
80  JUSTIFY_CENTER_WITH_VALUE,
81  JUSTIFY_RIGHT_WITH_VALUE,
82  JUSTIFY_LEFT_NO_VALUE,
83  JUSTIFY_CENTER_NO_VALUE,
84  JUSTIFY_RIGHT_NO_VALUE
85  };
86  private:
88  uint32_t gridSize: 3;
90  uint32_t gridPosition: 3;
92  uint32_t gridRowSpan: 3;
94  uint32_t gridHeight: 9;
96  uint32_t rowPosition: 7;
98  uint32_t drawingMode: 4;
100  uint32_t justification: 3;
101  public:
102  GridPosition() : gridSize(0), gridPosition(0), gridHeight(0), rowPosition(0), drawingMode(0),
103  justification(JUSTIFY_TITLE_LEFT_VALUE_RIGHT) {}
104 
105  GridPosition(const GridPosition &other) = default;
106 
112  GridPosition(GridDrawingMode mode, GridJustification justification, int row, int height = 0)
113  : gridSize(1), gridPosition(1), gridHeight(height),rowPosition(row), drawingMode(mode), justification(justification) { }
114 
123  GridPosition(GridDrawingMode mode, GridJustification just, int size, int pos, int row, int hei)
124  : gridSize(size), gridPosition(pos), gridHeight(hei), rowPosition(row), drawingMode(mode), justification(just) { }
125 
126  GridDrawingMode getDrawingMode() const { return static_cast<GridDrawingMode>(drawingMode); }
127 
128  GridJustification getJustification() const { return static_cast<GridJustification>(justification); }
129 
130  int getGridSize() const { return gridSize; }
131 
132  int getGridHeight() const { return gridHeight; }
133 
134  int getGridPosition() const { return gridPosition; }
135 
136  int getRow() const { return rowPosition; }
137  };
138 
139  inline bool itemNeedsValue(GridPosition::GridJustification justification) {
140  return (justification == GridPosition::JUSTIFY_TITLE_LEFT_WITH_VALUE ||
141  justification == GridPosition::JUSTIFY_CENTER_WITH_VALUE ||
142  justification == GridPosition::JUSTIFY_RIGHT_WITH_VALUE);
143  }
144 
152  private:
153  uint16_t menuId;
154  GridPosition thePosition;
155  public:
156  GridPositionWithId() : menuId(0xffff), thePosition(GridPosition::DRAW_TEXTUAL_ITEM,
157  GridPosition::JUSTIFY_TITLE_LEFT_VALUE_RIGHT, 0) {}
158 
159  GridPositionWithId(const GridPositionWithId &other) = default;
160 
161  GridPositionWithId(uint16_t itemId, const GridPosition &pos) : menuId(itemId), thePosition(pos) {}
162 
163  const GridPosition &getPosition() { return thePosition; }
164 
165  uint16_t getKey() const { return menuId; }
166 
167  void setNewPosition(GridPosition newPosition) {
168  thePosition = newPosition;
169  }
170  };
171 
178  public:
182  enum ColorType: uint8_t {
183  TEXT,
184  BACKGROUND,
185  HIGHLIGHT1,
186  HIGHLIGHT2,
187  SIZEOF_COLOR_ARRAY
188  };
193  COMPTYPE_TITLE,
194  COMPTYPE_ITEM,
195  COMPTYPE_ACTION,
196  };
197  private:
198  uint32_t propsKey;
199  color_t colors[SIZEOF_COLOR_ARRAY];
200  MenuPadding padding;
201  const void* fontData;
202  uint8_t fontMagnification: 4;
203  uint8_t defaultJustification: 4;
204  uint8_t spaceAfter;
205  uint8_t requiredHeight;
206  public:
207  ItemDisplayProperties() : propsKey(0), colors{}, padding(), fontData(nullptr), fontMagnification(1), defaultJustification(0), spaceAfter(0), requiredHeight(0) {}
208  ItemDisplayProperties(uint32_t key, color_t* palette, const MenuPadding& pad, const void* font, uint8_t mag, uint8_t spacing,
209  uint8_t height, GridPosition::GridJustification defaultJustification)
210  : propsKey(key), padding{pad}, fontData(font), fontMagnification(mag), defaultJustification(defaultJustification),
211  spaceAfter(spacing), requiredHeight(height) {
212  memcpy(colors, palette, sizeof colors);
213  }
214  ItemDisplayProperties(const ItemDisplayProperties& other) : padding{other.padding}, fontData(other.fontData),
215  fontMagnification(other.fontMagnification), defaultJustification(other.defaultJustification),
216  spaceAfter(other.spaceAfter), requiredHeight(other.requiredHeight) {
217  memcpy(colors, other.colors, sizeof colors);
218  }
219 
220  uint32_t getKey() const { return propsKey; }
221 
222  GridPosition::GridJustification getDefaultJustification() { return (GridPosition::GridJustification)defaultJustification; }
223 
224  void setDefaultJustification(GridPosition::GridJustification justification) { defaultJustification = justification; }
225 
226  uint8_t getSpaceAfter() const {return spaceAfter; }
227 
228  void setSpaceAfter(uint8_t space) { spaceAfter = space; }
229 
230  uint8_t getRequiredHeight() const { return requiredHeight; }
231 
232  void setRequiredHeight(uint8_t newHeight) { requiredHeight = newHeight; }
233 
234  color_t getColor(ColorType color) const {
235  return (color < SIZEOF_COLOR_ARRAY) ? colors[color] : RGB(0,0,0);
236  }
237 
238  void setColor(ColorType color, color_t value) {
239  if(color >= SIZEOF_COLOR_ARRAY) return;
240  colors[color] = value;
241  }
242 
243  void setColors(color_t* palette) {
244  memcpy(colors, palette, sizeof colors);
245  }
246 
247  const MenuPadding& getPadding() const {
248  return padding;
249  }
250 
251  void setPadding(const MenuPadding& pad) {
252  padding = pad;
253  }
254 
255  uint8_t getFontMagnification() const {
256  return fontMagnification;
257  }
258 
259  void setFontInfo(const void* font, uint8_t mag) {
260  fontMagnification = mag;
261  fontData = font;
262  }
263 
264  const void* getFont() {
265  return fontData;
266  }
267 
268  color_t* getPalette() {
269  return colors;
270  }
271  };
272 
282  public:
283  virtual ItemDisplayProperties* configFor(MenuItem* pItem, ItemDisplayProperties::ComponentType compType) = 0;
284  virtual DrawableIcon* iconForMenuItem(uint16_t id) = 0;
285  virtual GridPositionWithId* gridPositionForItem(MenuItem* pItem) = 0;
286  virtual color_t getSelectedColor(ItemDisplayProperties::ColorType colorType) = 0;
287  };
288 
297  private:
298  ItemDisplayProperties props = ItemDisplayProperties(0, {}, MenuPadding(0), nullptr, 1, 0, 1, GridPosition::JUSTIFY_TITLE_LEFT_VALUE_RIGHT);
299  BtreeList<uint16_t, GridPositionWithId> gridByItem;
300  public:
301  NullItemDisplayPropertiesFactory() : gridByItem(4) {}
302 
303  ItemDisplayProperties* configFor(MenuItem* pItem, ItemDisplayProperties::ComponentType compType) override {
304  return &props;
305  }
306 
307  DrawableIcon* iconForMenuItem(uint16_t id) override{
308  return nullptr;
309  }
310 
311  color_t getSelectedColor(ItemDisplayProperties::ColorType colorType) override {
312  return 0;
313  }
314 
315  GridPositionWithId* gridPositionForItem(MenuItem* pItem) override {
316  if(!pItem) return nullptr;
317  return gridByItem.getByKey(pItem->getId());
318  }
319 
320  void addGridPosition(MenuItem* pItem, const GridPosition& position) {
321  if(!pItem) return;
322  auto* grid = gridByItem.getByKey(pItem->getId());
323  if(grid) {
324  grid->setNewPosition(position);
325  }
326  else {
327  gridByItem.add(GridPositionWithId(pItem->getId(), position));
328  }
329  }
330  };
331 
332 #define MENUID_NOTSET 0xffff
333 
334  inline uint32_t MakePropsKey(uint16_t menuId, bool parentKey, ItemDisplayProperties::ComponentType ty) {
335  return (uint32_t)menuId | (parentKey ? 0x10000UL : 0UL) | ((uint32_t)ty << 18UL);
336  }
337 
346  private:
347  BtreeList<uint32_t, ItemDisplayProperties> displayProperties;
348  BtreeList<uint16_t, DrawableIcon> iconsByItem;
349  BtreeList<uint16_t, GridPositionWithId> gridByItem;
350  color_t selectedTextColor = RGB(255,255,255);
351  color_t selectedBackgroundColor = RGB(0, 0, 255);
352  public:
354  : displayProperties(5, GROW_BY_5),
355  iconsByItem(6, GROW_BY_5)
356  { }
357  DrawableIcon* iconForMenuItem(uint16_t id) override {
358  return iconsByItem.getByKey(id);
359  }
360 
361  GridPositionWithId* gridPositionForItem(MenuItem* pItem) override {
362  if(!pItem) return nullptr;
363  return gridByItem.getByKey(pItem->getId());
364  }
365 
366  ItemDisplayProperties* configFor(MenuItem* pItem, ItemDisplayProperties::ComponentType compType) override;
367 
368  color_t getSelectedColor(ItemDisplayProperties::ColorType colorType) override {
369  return colorType == ItemDisplayProperties::BACKGROUND ? selectedBackgroundColor : selectedTextColor;
370  }
371 
372  void addGridPosition(MenuItem* pItem, const GridPosition& position) {
373  if(!pItem) return;
374  auto* grid = gridByItem.getByKey(pItem->getId());
375  if(grid) {
376  grid->setNewPosition(position);
377  }
378  else {
379  gridByItem.add(GridPositionWithId(pItem->getId(), position));
380  }
381  }
382 
383  void addImageToCache(const DrawableIcon& toAdd) {
384  auto* current = iconsByItem.getByKey(toAdd.getKey());
385  if(current) {
386  current->setFromValues(toAdd.getDimensions(), toAdd.getIconType(), toAdd.getIcon(false), toAdd.getIcon(true));
387  }
388  else {
389  iconsByItem.add(toAdd);
390  }
391  }
392 
393  void setDrawingPropertiesDefault(ItemDisplayProperties::ComponentType drawing, color_t* palette, MenuPadding pad, const void *font,
394  uint8_t mag,uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification) {
395  setDrawingProperties(MakePropsKey(MENUID_NOTSET, false, drawing), palette, pad, font, mag, spacing, requiredHeight, defaultJustification);
396  }
397 
398  void setDrawingPropertiesForItem(ItemDisplayProperties::ComponentType drawing, uint16_t id, color_t* palette, MenuPadding pad, const void *font,
399  uint8_t mag,uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification) {
400  setDrawingProperties(MakePropsKey(id, false, drawing), palette, pad, font, mag, spacing, requiredHeight, defaultJustification);
401  }
402 
403  void setDrawingPropertiesAllInSub(ItemDisplayProperties::ComponentType drawing, uint16_t id, color_t* palette, MenuPadding pad, const void *font,
404  uint8_t mag, uint8_t spacing, uint8_t requiredHeight, GridPosition::GridJustification defaultJustification) {
405  setDrawingProperties(MakePropsKey(id, true, drawing), palette, pad, font, mag, spacing, requiredHeight, defaultJustification);
406  }
407 
408  void setDrawingProperties(uint32_t key, color_t* palette, MenuPadding pad, const void* font, uint8_t mag, uint8_t spacing,
409  uint8_t requiredHeight, GridPosition::GridJustification defaultJustification);
410 
411  void setSelectedColors(color_t background, color_t text) {
412  selectedBackgroundColor = background;
413  selectedTextColor = text;
414  }
415  };
416 
420  extern const unsigned char PROGMEM loResEditingIcon[];
421 
425  extern const unsigned char PROGMEM loResActiveIcon[];
426 
430  extern const unsigned char PROGMEM defActiveIcon[];
431 
435  extern const unsigned char PROGMEM defEditingIcon[];
436 
437 } // namespace tcgfx
438 
439 #endif // _GFX_MENU_CONFIG_H_
Definition: MenuItems.h:316
Definition: GfxMenuConfig.h:281
Definition: DrawingPrimitives.h:126
const unsigned char PROGMEM loResActiveIcon[]
Definition: GfxMenuConfig.cpp:34
ComponentType
Definition: GfxMenuConfig.h:192
Definition: GfxMenuConfig.h:70
const uint8_t iconWifiNotConnected [] PROGMEM
Definition: wifiAndConnectionIcons16x12.h:23
Definition: BaseGraphicalRenderer.cpp:9
const unsigned char PROGMEM defActiveIcon[]
Definition: GfxMenuConfig.cpp:20
const uint8_t * getIcon(bool selected) const
Definition: DrawingPrimitives.h:187
Definition: GfxMenuConfig.h:296
IconType getIconType() const
Definition: DrawingPrimitives.h:201
Definition: GfxMenuConfig.h:64
Definition: DrawingPrimitives.h:35
uint16_t getId()
Definition: MenuItems.cpp:69
GridDrawingMode
Definition: GfxMenuConfig.h:62
Definition: GfxMenuConfig.h:177
Definition: GfxMenuConfig.h:151
const unsigned char PROGMEM loResEditingIcon[]
Definition: GfxMenuConfig.cpp:28
ColorType
Definition: GfxMenuConfig.h:182
Definition: GfxMenuConfig.h:66
Definition: GfxMenuConfig.h:72
GridPosition(GridDrawingMode mode, GridJustification just, int size, int pos, int row, int hei)
Definition: GfxMenuConfig.h:123
GridPosition(GridDrawingMode mode, GridJustification justification, int row, int height=0)
Definition: GfxMenuConfig.h:112
Definition: GfxMenuConfig.h:57
uint16_t color_t
Definition: DrawingPrimitives.h:29
Definition: GfxMenuConfig.h:27
const unsigned char PROGMEM defEditingIcon[]
Definition: GfxMenuConfig.h:68
Coord getDimensions() const
Definition: DrawingPrimitives.h:194