tcMenu
DrawingPrimitives.h
Go to the documentation of this file.
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 
8 #ifndef TCMENU_DRAWING_PRIMITIVES_H
9 #define TCMENU_DRAWING_PRIMITIVES_H
10 
11 #include <PlatformDetermination.h>
12 
13 namespace tcgfx {
14 
18 #define DRAW_NO_MASK 0xffff
19 
20 
21 #ifdef NEED_32BIT_COLOR_T_ALPHA
22 
26  typedef uint32_t color_t;
28 #define RGB(r, g, b) (uint32_t)( 0xff000000UL | ((r)<<16UL) | ((g)<<8UL) | (b) )
29 #else
30 
34  typedef uint16_t color_t;
36 #define RGB(r, g, b) (uint16_t)( (((r)>>3)<<11) | (((g)>>2)<<5) | ((b)>>3) )
37 #endif
38 
42  struct MenuFontDef {
43  const void* fontData;
44  const uint8_t fontMag;
45 
46  MenuFontDef(const void* data, uint8_t mag) : fontData(data), fontMag(mag) { }
47  };
48 
53  struct MenuPadding {
54  uint16_t top: 4;
55  uint16_t right: 4;
56  uint16_t bottom: 4;
57  uint16_t left: 4;
58 
59  MenuPadding(int top_, int right_, int bottom_, int left_) {
60  top = top_;
61  bottom = bottom_;
62  right = right_;
63  left = left_;
64  }
65 
66  explicit MenuPadding(int equalAll = 0) {
67  top = bottom = right = left = equalAll;
68  }
69  };
70 
79  inline void makePadding(MenuPadding& padding, int top, int right, int bottom, int left) {
80  padding.top = top;
81  padding.right = right;
82  padding.bottom = bottom;
83  padding.left = left;
84  }
85 
89  struct MenuBorder {
90  uint8_t top:2;
91  uint8_t left:2;
92  uint8_t bottom:2;
93  uint8_t right:2;
94 
95  MenuBorder() = default;
96 
97  explicit MenuBorder(uint8_t equalSides) {
98  top = left = bottom = right = equalSides;
99  }
100 
101  MenuBorder(uint8_t top_, uint8_t right_, uint8_t bottom_, uint8_t left_) {
102  top = top_;
103  right = right_;
104  left = left_;
105  bottom = bottom_;
106  }
107 
108  bool areAllBordersEqual() const {
109  return (top == left) && (left == right) && (right == bottom);
110  }
111 
112  bool isBorderOff() const {
113  return areAllBordersEqual() && top == 0;
114  }
115  };
116 
117 
119  struct Coord {
121  Coord() {
122  x = y = 0;
123  }
124 
130  Coord(int x, int y) {
131  this->x = x;
132  this->y = y;
133  }
134 
135  Coord(const Coord &other) {
136  this->x = other.x;
137  this->y = other.y;
138  }
139 
140  Coord& operator = (const Coord& other) {
141  this->x = other.x;
142  this->y = other.y;
143  return *this;
144  }
145 
146  int32_t x: 16;
147  int32_t y: 16;
148  };
149 
158  public:
159  const tcgfx::color_t *palette;
160  const uint8_t* data;
161  tcgfx::color_t maskColor;
162  uint8_t bitDepth;
163 
164  PaletteDrawingData(const tcgfx::color_t *palette, const uint8_t *data, tcgfx::color_t maskColor, uint8_t bitDepth)
165  : palette(palette), data(data), maskColor(maskColor), bitDepth(bitDepth) {}
166 
167  PaletteDrawingData(const tcgfx::color_t *palette, const uint8_t *data, uint8_t bitDepth)
168  : palette(palette), data(data), maskColor(DRAW_NO_MASK), bitDepth(bitDepth) {}
169 
170  const color_t* getPalette() const { return palette; }
171  const uint8_t* getData() const { return data; }
172  bool hasMask() const { return maskColor != DRAW_NO_MASK; }
173  color_t getMaskColor() const { return maskColor; }
174  uint8_t getBitDepth() const { return bitDepth; }
175  };
176 
181  class DrawableIcon {
182  public:
183  enum IconType : uint8_t {
191  ICON_NATIVE
192  };
193  enum MemoryLocation : uint8_t {
199  LOAD_FROM_STORAGE
200  };
201  private:
202  uint16_t menuId;
203  Coord dimensions;
204  IconType iconType;
205  MemoryLocation location;
206  const uint8_t *normalIcon;
207  const uint8_t *selectedIcon;
208 
209  public:
213  DrawableIcon() : menuId(0), dimensions(0, 0), iconType(ICON_XBITMAP), location(STORED_IN_ROM),
214  normalIcon(nullptr), selectedIcon(nullptr) {}
215 
219  DrawableIcon(const DrawableIcon &other) : menuId(other.menuId), dimensions(other.dimensions),
220  iconType(other.iconType),
221  location(other.location), normalIcon(other.normalIcon),
222  selectedIcon(other.selectedIcon) {}
223 
224  DrawableIcon& operator=(const DrawableIcon& other) {
225  if(&other == this) return *this;
226  menuId = other.menuId;
227  dimensions = other.dimensions;
228  iconType = other.iconType;
229  location = other.location;
230  normalIcon = other.normalIcon;
231  selectedIcon = other.selectedIcon;
232  return *this;
233  }
242  DrawableIcon(uint16_t id, const Coord &size, IconType ty, const uint8_t *normal,
243  const uint8_t *selected = nullptr)
244  : menuId(id), dimensions(size), iconType(ty), location(STORED_IN_ROM), normalIcon(normal),
245  selectedIcon(selected) {}
246 
252  const uint8_t *getIcon(bool selected) const {
253  return (selected && selectedIcon != nullptr) ? selectedIcon : normalIcon;
254  }
255 
260  return dimensions;
261  };
262 
267  return iconType;
268  }
269 
270  uint16_t getKey() const {
271  return menuId;
272  }
273 
274  void setFromValues(const Coord &size, IconType ty, const uint8_t *normal, const uint8_t *selected = nullptr) {
275  this->dimensions = size;
276  this->iconType = ty;
277  this->normalIcon = normal;
278  this->selectedIcon = selected;
279  }
280  };
281 
282 } // namespace tcgfx
283 
284 #endif //TCMENU_DRAWING_PRIMITIVES_H
Coord(int x, int y)
Definition: DrawingPrimitives.h:130
MemoryLocation
Definition: DrawingPrimitives.h:193
Definition: DrawingPrimitives.h:181
void makePadding(MenuPadding &padding, int top, int right, int bottom, int left)
Definition: DrawingPrimitives.h:79
Definition: DrawingPrimitives.h:189
Definition: DrawingPrimitives.h:195
Definition: BaseGraphicalRenderer.cpp:10
Coord()
Definition: DrawingPrimitives.h:121
const uint8_t * getIcon(bool selected) const
Definition: DrawingPrimitives.h:252
Definition: DrawingPrimitives.h:185
IconType getIconType() const
Definition: DrawingPrimitives.h:266
DrawableIcon()
Definition: DrawingPrimitives.h:213
Definition: DrawingPrimitives.h:53
DrawableIcon(uint16_t id, const Coord &size, IconType ty, const uint8_t *normal, const uint8_t *selected=nullptr)
Definition: DrawingPrimitives.h:242
Definition: DrawingPrimitives.h:157
IconType
Definition: DrawingPrimitives.h:183
DrawableIcon(const DrawableIcon &other)
Definition: DrawingPrimitives.h:219
Definition: DrawingPrimitives.h:42
Definition: DrawingPrimitives.h:89
Definition: DrawingPrimitives.h:119
Definition: DrawingPrimitives.h:187
uint32_t color_t
Definition: DrawingPrimitives.h:26
Definition: DrawingPrimitives.h:197
Coord getDimensions() const
Definition: DrawingPrimitives.h:259