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
menu.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2019,2021, 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_MENU_H_
29 #define _NANO_MENU_H_
30 
31 #include "canvas/point.h"
32 #include "canvas/rect.h"
33 #include "object.h"
34 #include "lcd_hal/io.h"
35 
41 template <class T> class NanoMenuItem: public NanoObject<T>
42 {
43 public:
45 };
46 
51 template <class T> class NanoMenu: public NanoObjectList<T>
52 {
53 public:
59  explicit NanoMenu(const NanoPoint &pos)
60  : NanoObjectList<T>(pos)
61  {
62  }
63 
68  : NanoObjectList<T>({0, 0})
69  {
70  }
71 
77  void add(NanoObject<T> &item)
78  {
80  item.update(); // update item to init all params
81  updateMenuItemsPosition();
82  if ( !m_selected )
83  {
84  m_selected = &item;
85  item.focus();
86  }
87  }
88 
94  void insert(NanoObject<T> &item)
95  {
97  item.update(); // update item to init all params
98  updateMenuItemsPosition();
99  if ( !m_selected )
100  {
101  m_selected = &item;
102  item.focus();
103  }
104  }
105 
111  {
112  return m_selected;
113  }
114 
120  void down()
121  {
122  if ( m_selected )
123  {
124  m_selected->defocus();
125  }
126  m_selected = this->getNext(m_selected);
127  if ( !m_selected )
128  {
129  m_selected = this->getNext();
130  }
131  if ( m_selected )
132  {
133  m_selected->focus();
134  }
135  }
136 
142  void up()
143  {
144  if ( m_selected )
145  {
146  m_selected->defocus();
147  }
148  m_selected = getPrev(m_selected);
149  if ( !m_selected )
150  {
151  m_selected = this->getPrev();
152  }
153  if ( m_selected )
154  {
155  m_selected->focus();
156  }
157  }
158 
159 protected:
164  virtual void updateMenuItemsPosition() = 0;
165 
166 private:
167  NanoObject<T> *m_selected = nullptr;
168 };
169 
174 template <class T> class NanoListMenu: public NanoMenu<T>
175 {
176 public:
177  using NanoMenu<T>::NanoMenu;
178 
182  void draw() override
183  {
184  this->getTiler().getCanvas().setColor(0xFFFF);
185  this->getTiler().getCanvas().drawRect(
186  {this->m_rect.p1 + (NanoPoint){2, 2}, this->m_rect.p2 - (NanoPoint){2, 2}});
188  }
189 
190 private:
191  void updateMenuItemsPosition() override
192  {
193  NanoObject<T> *p = this->getNext();
194  lcdint_t y_pos = this->m_rect.p1.y + 4;
195  while ( p )
196  {
197  p->setPos({(lcdint_t)(this->m_rect.p1.x + 4), y_pos});
198  y_pos += p->height() + 1;
199  p = this->getNext(p);
200  }
201  this->m_rect.p2.y = y_pos + 7;
202  this->m_rect.p2.x = this->getDisplay().width();
203  }
204 };
205 
211 template <class T> class NanoFixedWidthMenu: public NanoMenu<T>
212 {
213 public:
220  NanoFixedWidthMenu(const NanoPoint &pos, const NanoPoint &size)
221  : NanoMenu<T>(pos)
222  {
223  this->setSize(size);
224  }
225 
229  void draw() override
230  {
231  this->getTiler().getCanvas().setColor(0xFFFF);
232  this->getTiler().getCanvas().drawRect(
233  {this->m_rect.p1 + (NanoPoint){2, 2}, this->m_rect.p2 - (NanoPoint){2, 2}});
235  }
236 
237 private:
238  void updateMenuItemsPosition() override
239  {
240  NanoObject<T> *p = this->getNext();
241  lcdint_t y_pos = this->m_rect.p1.y + 4;
242  while ( p )
243  {
244  p->setPos({(lcdint_t)(this->m_rect.p1.x + 4), y_pos});
245  p->setSize({(lcdint_t)(this->m_rect.width() - 8), p->height()});
246  y_pos += p->height() + 1;
247  p = this->getNext(p);
248  }
249  }
250 };
251 
256 #endif
void add(NanoObject< T > &item)
Adds new menu item to the end of the list.
Definition: menu.h:77
struct _NanoPoint NanoPoint
Describes point.
lcdint_t height() const
Returns height of NanoObject.
Definition: object.h:108
Describes point.
Definition: point.h:39
void setSize(const NanoPoint &size)
Sets new size of NanoObject.
Definition: object.h:150
NanoMenu()
Creates instance of NanoMenu at 0,0 position.
Definition: menu.h:67
Template class for NanoEngine objects lists.
Definition: object.h:44
Point class.
Class implements menu, organized as the list.
Definition: menu.h:211
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
NanoMenu(const NanoPoint &pos)
Creates menu object.
Definition: menu.h:59
void up()
Moves active menu item position to the previous item.
Definition: menu.h:142
SSD1306 HAL IO communication functions.
Class implements menu, organized as the list.
Definition: menu.h:174
void focus()
Sets logic focus on NanoEngineObject.
Definition: tiler.h:112
lcdint_t y
y position in pixels
Definition: point.h:44
NanoObject< T > * getSelected()
Returns pointer to active menu item.
Definition: menu.h:110
void insert(NanoObject< T > &item)
Inserts new menu item to the beginning of the list.
Definition: menu.h:94
Rectangle class.
NanoFixedWidthMenu(const NanoPoint &pos, const NanoPoint &size)
Creates instance of NanoFixedWidthMenu.
Definition: menu.h:220
void insert(NanoObject< T > &object)
Adds new NanoObject to the beginning of the list and marks it for refresh.
Definition: object.h:362
This is base class for all NanoObjects.
Definition: object.h:49
This is base class for user menu implementations.
Definition: menu.h:51
void down()
Moves active menu item position to the next item.
Definition: menu.h:120
T & getTiler()
Returns reference to NanoEngine.
Definition: tiler.h:148
NanoRect m_rect
Rectangle area occupied by the object.
Definition: object.h:239
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 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
void draw() override
Draw all menu items in NanoEngine buffer.
Definition: menu.h:229
NanoPoint p1
top-left point of the rectangle area
Definition: rect.h:45
lcdint_t x
x position in pixels
Definition: point.h:42
void setPos(const NanoPoint &p)
Sets position of NanoObject, doesn&#39;t mark for update content on the screen.
Definition: object.h:159
void draw() override
Draw all menu items in NanoEngine buffer.
Definition: menu.h:182