LCDGFX LCD display driver  2.0.1
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-2019, 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>
46 
50 template<class T>
51 class NanoObject: public NanoEngineObject<T>
52 {
53 public:
54  template<class N> friend class NanoObjectList;
60  NanoObject(const NanoPoint &pos)
61  : m_rect{pos, pos}
62  {
63  }
64 
71  NanoObject(const NanoPoint &pos, const NanoPoint &size)
72  : m_rect{pos, pos + size - (NanoPoint){1,1}}
73  {
74  }
75 
79  void draw() override { };
80 
84  void refresh() override
85  {
86  if (this->hasTiler())
87  {
88  this->getTiler().refreshWorld( m_rect );
89  }
90  }
91 
95  void update() override { }
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){ p,
162  (NanoPoint){ (lcdint_t)(p.x + m_rect.p2.x - m_rect.p1.x),
163  (lcdint_t)(p.y + m_rect.p2.y - m_rect.p1.y) } };
164  }
165 
169  const NanoPoint bottom() const
170  {
171  return { (lcdint_t)((m_rect.p1.x + m_rect.p2.x) >> 1), m_rect.p2.y };
172  }
173 
177  const NanoPoint top() const
178  {
179  return { (lcdint_t)((m_rect.p1.x + m_rect.p2.x) >> 1), m_rect.p1.y };
180  }
181 
185  const NanoPoint left() const
186  {
187  return { m_rect.p1.x, (lcdint_t)((m_rect.p1.y + m_rect.p2.y) >> 1) };
188  }
189 
193  const NanoPoint right() const
194  {
195  return { m_rect.p2.x, (lcdint_t)((m_rect.p1.y + m_rect.p2.y) >> 1) };
196  }
197 
201  const NanoPoint center() const
202  {
203  return { (lcdint_t)((m_rect.p1.x + m_rect.p2.x) >> 1),
204  (lcdint_t)((m_rect.p1.y + m_rect.p2.y) >> 1) };
205  }
206 
210  lcdint_t x( ) const { return m_rect.p1.x; }
211 
215  lcdint_t y( ) const { return m_rect.p1.y; }
216 
220  const NanoPoint & getPosition() const { return m_rect.p1; }
221 
225  const NanoRect & getRect() const { return m_rect; }
226 
227 protected:
230 };
231 
232 template<class T>
233 class NanoObjectList: public NanoObject<T>
234 {
235 public:
237 
245  {
246  return static_cast<NanoObject<T>*>(prev ? prev->m_next : m_first);
247  }
248 
256  {
257  NanoObject<T> *p = m_first;
258  while (p)
259  {
260  if (p->m_next == curr)
261  {
262  break;
263  }
264  p = static_cast<NanoObject<T>*>(p->m_next);
265  }
266  return p;
267  }
268 
272  void update() override
273  {
274  NanoObject<T> *p = getNext();
275  while (p)
276  {
277  p->update();
278  p = getNext( p );
279  }
280  }
281 
285  void refresh() override
286  {
287  NanoObject<T> *p = getNext();
288  while (p)
289  {
290  p->setTiler( this->m_tiler );
291  p->refresh();
292  p = getNext( p );
293  }
294  }
295 
299  void draw() override
300  {
301  NanoObject<T> *p = getNext();
302  while (p)
303  {
304  p->draw();
305  p = getNext(p);
306  }
307  }
308 
314  bool has(NanoObject<T> &object)
315  {
316  NanoObject<T> *p = getNext();
317  while (p && p != &object)
318  {
319  p = getNext(p);
320  }
321  return p != nullptr;
322  }
323 
329  void add(NanoObject<T> &object)
330  {
331  if ( has( object ) )
332  {
333  return;
334  }
335  object.m_next = nullptr;
336  object.setTiler( this->m_tiler );
337  if ( !m_first )
338  {
339  m_first = &object;
340  }
341  else
342  {
343  getPrev()->m_next = &object;
344  }
345  object.refresh();
346  }
347 
353  void insert(NanoObject<T> &object)
354  {
355  if ( has( object ) )
356  {
357  return;
358  }
359  object.m_next = m_first;
360  object.m_tiler = this->m_tiler;
361  m_first = &object;
362  object.refresh();
363  }
364 
370  void remove(NanoObject<T> &object)
371  {
372  if ( m_first == nullptr )
373  {
374  }
375  else if ( &object == m_first )
376  {
377  object.refresh();
378  m_first = object.m_next;
379  object.m_next = nullptr;
380  object.m_tiler = nullptr;
381  }
382  else
383  {
384  NanoObject<T> *p = m_first;
385  while ( p->m_next )
386  {
387  if ( p->m_next == &object )
388  {
389  object.refresh();
390  p->m_next = object.m_next;
391  object.m_next = nullptr;
392  object.m_tiler = nullptr;
393  break;
394  }
395  p = p->m_next;
396  }
397  }
398  }
399 
400 private:
401  NanoObject<T> *m_first = nullptr;
402 };
403 
408 #endif
409 
NanoObject< T > * getNext(NanoObject< T > *prev=nullptr)
Definition: object.h:244
bool has(NanoObject< T > &object)
Definition: object.h:314
void moveBy(const NanoPoint &p)
Definition: object.h:126
struct _NanoPoint NanoPoint
void refresh() override
Definition: object.h:84
lcdint_t height() const
Definition: object.h:108
T * m_tiler
Active tiler, assigned to the NanoEngineObject.
Definition: tiler.h:150
Definition: rect.h:42
void setSize(const NanoPoint &size)
Definition: object.h:150
lcdint_t y() const
Definition: object.h:215
const NanoPoint bottom() const
Definition: object.h:169
lcdint_t height() const
Definition: rect.h:63
NanoPoint p2
Definition: rect.h:48
int8_t lcdint_t
Definition: canvas_types.h:79
NanoObject(const NanoPoint &pos, const NanoPoint &size)
Definition: object.h:71
const NanoRect & getRect() const
Definition: object.h:225
const NanoPoint right() const
Definition: object.h:193
lcdint_t y
Definition: point.h:45
const NanoPoint left() const
Definition: object.h:185
const NanoPoint & getPosition() const
Definition: object.h:220
const NanoPoint top() const
Definition: object.h:177
void insert(NanoObject< T > &object)
Definition: object.h:353
void resize(const NanoPoint &size)
Definition: object.h:138
void draw() override
Definition: object.h:79
void refresh() override
Definition: object.h:285
struct _NanoRect NanoRect
void moveTo(const NanoPoint &p)
Definition: object.h:116
NanoObject< T > * getPrev(NanoObject< T > *curr=nullptr)
Definition: object.h:255
void update() override
Definition: object.h:272
bool hasTiler()
Definition: tiler.h:141
NanoObject(const NanoPoint &pos)
Definition: object.h:60
T & getTiler()
Definition: tiler.h:147
NanoRect m_rect
Definition: object.h:229
lcdint_t width() const
Definition: object.h:100
void update() override
Definition: object.h:95
void add(NanoObject< T > &object)
Definition: object.h:329
void setTiler(T *tiler)
Definition: tiler.h:158
lcdint_t x() const
Definition: object.h:210
void draw() override
Definition: object.h:299
lcdint_t width() const
Definition: rect.h:51
NanoEngineObject< T > * m_next
Next NanoEngineObject in the list.
Definition: tiler.h:151
const NanoPoint center() const
Definition: object.h:201
NanoPoint p1
Definition: rect.h:45
lcdint_t x
Definition: point.h:43
void setPos(const NanoPoint &p)
Definition: object.h:159