LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
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)
Returns next object in the list.
Definition: object.h:253
bool has(NanoObject< T > &object)
Returns true if NanoObjectList contains specified object.
Definition: object.h:323
void moveBy(const NanoPoint &p)
Moves sprite to new position by specified offset.
Definition: object.h:126
struct _NanoPoint NanoPoint
Describes point.
void refresh() override
Marks nano object location for refreshing on the new frame.
Definition: object.h:82
lcdint_t height() const
Returns height of NanoObject.
Definition: object.h:108
T * m_tiler
Active tiler, assigned to the NanoEngineObject.
Definition: tiler.h:154
Describes point.
Definition: point.h:39
NanoRect structure describes rectangle area.
Definition: rect.h:42
void setSize(const NanoPoint &size)
Sets new size of NanoObject.
Definition: object.h:150
lcdint_t y() const
Returns sprite y position.
Definition: object.h:216
const NanoPoint bottom() const
Returns bottom-center point of the sprite.
Definition: object.h:168
Template class for NanoEngine objects lists.
Definition: object.h:44
lcdint_t height() const
returns height of NanoRect
Definition: rect.h:69
Point class.
NanoPoint p2
right-bottom point of the rectangle area
Definition: rect.h:48
int8_t lcdint_t
internal int type, used by the library.
Definition: canvas_types.h:77
NanoObject(const NanoPoint &pos, const NanoPoint &size)
Creates basic object with arbitrary size.
Definition: object.h:69
const NanoRect & getRect() const
Returns rectangle area, occupied by the NanoObject.
Definition: object.h:232
const NanoPoint right() const
Returns right-center point of the sprite.
Definition: object.h:192
SSD1306 HAL IO communication functions.
lcdint_t y
y position in pixels
Definition: point.h:44
const NanoPoint left() const
Returns left-center point of the sprite.
Definition: object.h:184
const NanoPoint & getPosition() const
Returns current sprite position (top-left corner)
Definition: object.h:224
const NanoPoint top() const
Returns top-center point of the sprite.
Definition: object.h:176
Rectangle class.
void insert(NanoObject< T > &object)
Adds new NanoObject to the beginning of the list and marks it for refresh.
Definition: object.h:362
void resize(const NanoPoint &size)
Resizes NanoObject and marks screen area for refresh.
Definition: object.h:138
void draw() override
Draws nano object Engine canvas.
Definition: object.h:77
void refresh() override
Refreshes all objects in the list.
Definition: object.h:294
struct _NanoRect NanoRect
NanoRect structure describes rectangle area.
void moveTo(const NanoPoint &p)
Moves sprite to new position and marks screen area for refresh.
Definition: object.h:116
This is base class for all NanoObjects.
Definition: object.h:49
NanoObject< T > * getPrev(NanoObject< T > *curr=nullptr)
Returns previous object in the list.
Definition: object.h:264
void update() override
Updates all objects in the list.
Definition: object.h:281
bool hasTiler()
Returns true if NanoEngineObject is bound to NanoEngine instance.
Definition: tiler.h:139
NanoObject(const NanoPoint &pos)
Creates basic object with the size [1,1].
Definition: object.h:58
T & getTiler()
Returns reference to NanoEngine.
Definition: tiler.h:148
NanoRect m_rect
Rectangle area occupied by the object.
Definition: object.h:239
lcdint_t width() const
Returns width of NanoObject.
Definition: object.h:100
void update() override
Updates NanoObject.
Definition: object.h:93
void add(NanoObject< T > &object)
Adds new NanoObject to the end of the list and marks it for refresh.
Definition: object.h:338
void setTiler(T *tiler)
Bind NanoEngineObject to specific NanoEngine.
Definition: tiler.h:162
lcdint_t x() const
Returns sprite x position.
Definition: object.h:208
void draw() override
Draw all objects from the list in the buffer.
Definition: object.h:308
lcdint_t width() const
returns width of NanoRect
Definition: rect.h:51
NanoEngineObject< T > * m_next
Next NanoEngineObject in the list.
Definition: tiler.h:155
const NanoPoint center() const
Returns center point of the sprite.
Definition: object.h:200
NanoPoint p1
top-left point of the rectangle area
Definition: rect.h:45
lcdint_t x
x position in pixels
Definition: point.h:42
Tiler helper for graphics processing.
Template class for all NanoEngine objects.
Definition: tiler.h:84
void setPos(const NanoPoint &p)
Sets position of NanoObject, doesn&#39;t mark for update content on the screen.
Definition: object.h:159