LCDGFX LCD display driver  1.1.5
This library is developed to control SSD1306/SSD1325/SSD1327/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
object.h
1 /*
2  MIT License
3 
4  Copyright (c) 2018-2020, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
28 #ifndef _NANO_OBJECT_H_
29 #define _NANO_OBJECT_H_
30 
31 #include "canvas/point.h"
32 #include "canvas/rect.h"
33 #include "lcd_hal/io.h"
34 #include "tiler.h"
35 
44 template <class T> class NanoObjectList;
45 
49 template <class T> class NanoObject: public NanoEngineObject<T>
50 {
51 public:
52  template <class N> friend class NanoObjectList;
58  explicit NanoObject(const NanoPoint &pos)
59  : m_rect{pos, pos}
60  {
61  }
62 
69  NanoObject(const NanoPoint &pos, const NanoPoint &size)
70  : m_rect{pos, pos + size - (NanoPoint){1, 1}}
71  {
72  }
73 
77  void draw() override{};
78 
82  void refresh() override
83  {
84  if ( this->hasTiler() )
85  {
86  this->getTiler().refreshWorld(m_rect);
87  }
88  }
89 
93  void update() override
94  {
95  }
96 
100  lcdint_t width() const
101  {
102  return m_rect.width();
103  }
104 
108  lcdint_t height() const
109  {
110  return m_rect.height();
111  }
112 
116  void moveTo(const NanoPoint &p)
117  {
118  refresh();
119  setPos(p);
120  refresh();
121  }
122 
126  void moveBy(const NanoPoint &p)
127  {
128  refresh();
129  setPos(m_rect.p1 + p);
130  refresh();
131  }
132 
138  void resize(const NanoPoint &size)
139  {
140  refresh();
141  setSize(size);
142  refresh();
143  }
144 
150  void setSize(const NanoPoint &size)
151  {
152  m_rect.p2.x = m_rect.p1.x + size.x - 1;
153  m_rect.p2.y = m_rect.p1.y + size.y - 1;
154  }
155 
159  void setPos(const NanoPoint &p)
160  {
161  m_rect = (NanoRect){
162  p, (NanoPoint){(lcdint_t)(p.x + m_rect.p2.x - m_rect.p1.x), (lcdint_t)(p.y + m_rect.p2.y - m_rect.p1.y)}};
163  }
164 
168  const NanoPoint bottom() const
169  {
170  return {(lcdint_t)((m_rect.p1.x + m_rect.p2.x) >> 1), m_rect.p2.y};
171  }
172 
176  const NanoPoint top() const
177  {
178  return {(lcdint_t)((m_rect.p1.x + m_rect.p2.x) >> 1), m_rect.p1.y};
179  }
180 
184  const NanoPoint left() const
185  {
186  return {m_rect.p1.x, (lcdint_t)((m_rect.p1.y + m_rect.p2.y) >> 1)};
187  }
188 
192  const NanoPoint right() const
193  {
194  return {m_rect.p2.x, (lcdint_t)((m_rect.p1.y + m_rect.p2.y) >> 1)};
195  }
196 
200  const NanoPoint center() const
201  {
202  return {(lcdint_t)((m_rect.p1.x + m_rect.p2.x) >> 1), (lcdint_t)((m_rect.p1.y + m_rect.p2.y) >> 1)};
203  }
204 
208  lcdint_t x() const
209  {
210  return m_rect.p1.x;
211  }
212 
216  lcdint_t y() const
217  {
218  return m_rect.p1.y;
219  }
220 
224  const NanoPoint &getPosition() const
225  {
226  return m_rect.p1;
227  }
228 
232  const NanoRect &getRect() const
233  {
234  return m_rect;
235  }
236 
237 protected:
240 };
241 
242 template <class T> class NanoObjectList: public NanoObject<T>
243 {
244 public:
246 
254  {
255  return static_cast<NanoObject<T> *>(prev ? prev->m_next : m_first);
256  }
257 
265  {
266  NanoObject<T> *p = m_first;
267  while ( p )
268  {
269  if ( p->m_next == curr )
270  {
271  break;
272  }
273  p = static_cast<NanoObject<T> *>(p->m_next);
274  }
275  return p;
276  }
277 
281  void update() override
282  {
283  NanoObject<T> *p = getNext();
284  while ( p )
285  {
286  p->update();
287  p = getNext(p);
288  }
289  }
290 
294  void refresh() override
295  {
296  NanoObject<T> *p = getNext();
297  while ( p )
298  {
299  p->setTiler(this->m_tiler);
300  p->refresh();
301  p = getNext(p);
302  }
303  }
304 
308  void draw() override
309  {
310  NanoObject<T> *p = getNext();
311  while ( p )
312  {
313  p->draw();
314  p = getNext(p);
315  }
316  }
317 
323  bool has(NanoObject<T> &object)
324  {
325  NanoObject<T> *p = getNext();
326  while ( p && p != &object )
327  {
328  p = getNext(p);
329  }
330  return p != nullptr;
331  }
332 
338  void add(NanoObject<T> &object)
339  {
340  if ( has(object) )
341  {
342  return;
343  }
344  object.m_next = nullptr;
345  object.setTiler(this->m_tiler);
346  if ( !m_first )
347  {
348  m_first = &object;
349  }
350  else
351  {
352  getPrev()->m_next = &object;
353  }
354  object.refresh();
355  }
356 
362  void insert(NanoObject<T> &object)
363  {
364  if ( has(object) )
365  {
366  return;
367  }
368  object.m_next = m_first;
369  object.m_tiler = this->m_tiler;
370  m_first = &object;
371  object.refresh();
372  }
373 
379  void remove(NanoObject<T> &object)
380  {
381  if ( m_first == nullptr )
382  {
383  }
384  else if ( &object == m_first )
385  {
386  object.refresh();
387  m_first = object.m_next;
388  object.m_next = nullptr;
389  object.m_tiler = nullptr;
390  }
391  else
392  {
393  NanoObject<T> *p = m_first;
394  while ( p->m_next )
395  {
396  if ( p->m_next == &object )
397  {
398  object.refresh();
399  p->m_next = object.m_next;
400  object.m_next = nullptr;
401  object.m_tiler = nullptr;
402  break;
403  }
404  p = p->m_next;
405  }
406  }
407  }
408 
409 private:
410  NanoObject<T> *m_first = nullptr;
411 };
412 
417 #endif
NanoObject< T > * getNext(NanoObject< T > *prev=nullptr)
Definition: object.h:253
bool has(NanoObject< T > &object)
Definition: object.h:323
void moveBy(const NanoPoint &p)
Definition: object.h:126
struct _NanoPoint NanoPoint
void refresh() override
Definition: object.h:82
lcdint_t height() const
Definition: object.h:108
T * m_tiler
Active tiler, assigned to the NanoEngineObject.
Definition: tiler.h:154
Definition: rect.h:42
void setSize(const NanoPoint &size)
Definition: object.h:150
lcdint_t y() const
Definition: object.h:216
const NanoPoint bottom() const
Definition: object.h:168
lcdint_t height() const
Definition: rect.h:69
NanoPoint p2
Definition: rect.h:48
int8_t lcdint_t
Definition: canvas_types.h:77
NanoObject(const NanoPoint &pos, const NanoPoint &size)
Definition: object.h:69
const NanoRect & getRect() const
Definition: object.h:232
const NanoPoint right() const
Definition: object.h:192
lcdint_t y
Definition: point.h:44
const NanoPoint left() const
Definition: object.h:184
const NanoPoint & getPosition() const
Definition: object.h:224
const NanoPoint top() const
Definition: object.h:176
void insert(NanoObject< T > &object)
Definition: object.h:362
void resize(const NanoPoint &size)
Definition: object.h:138
void draw() override
Definition: object.h:77
void refresh() override
Definition: object.h:294
struct _NanoRect NanoRect
void moveTo(const NanoPoint &p)
Definition: object.h:116
NanoObject< T > * getPrev(NanoObject< T > *curr=nullptr)
Definition: object.h:264
void update() override
Definition: object.h:281
bool hasTiler()
Definition: tiler.h:139
NanoObject(const NanoPoint &pos)
Definition: object.h:58
T & getTiler()
Definition: tiler.h:148
NanoRect m_rect
Definition: object.h:239
lcdint_t width() const
Definition: object.h:100
void update() override
Definition: object.h:93
void add(NanoObject< T > &object)
Definition: object.h:338
void setTiler(T *tiler)
Definition: tiler.h:162
lcdint_t x() const
Definition: object.h:208
void draw() override
Definition: object.h:308
lcdint_t width() const
Definition: rect.h:51
NanoEngineObject< T > * m_next
Next NanoEngineObject in the list.
Definition: tiler.h:155
const NanoPoint center() const
Definition: object.h:200
NanoPoint p1
Definition: rect.h:45
lcdint_t x
Definition: point.h:42
void setPos(const NanoPoint &p)
Definition: object.h:159