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
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)
Definition: menu.h:77
struct _NanoPoint NanoPoint
lcdint_t height() const
Definition: object.h:108
void setSize(const NanoPoint &size)
Definition: object.h:150
NanoMenu()
Definition: menu.h:67
NanoPoint p2
Definition: rect.h:48
int8_t lcdint_t
Definition: canvas_types.h:77
NanoMenu(const NanoPoint &pos)
Definition: menu.h:59
void up()
Definition: menu.h:142
void focus()
Definition: tiler.h:112
lcdint_t y
Definition: point.h:44
NanoObject< T > * getSelected()
Definition: menu.h:110
void insert(NanoObject< T > &item)
Definition: menu.h:94
NanoFixedWidthMenu(const NanoPoint &pos, const NanoPoint &size)
Definition: menu.h:220
void insert(NanoObject< T > &object)
Definition: object.h:362
Definition: menu.h:51
void down()
Definition: menu.h:120
T & getTiler()
Definition: tiler.h:148
NanoRect m_rect
Definition: object.h:239
void update() override
Definition: object.h:93
void add(NanoObject< T > &object)
Definition: object.h:338
void draw() override
Definition: object.h:308
lcdint_t width() const
Definition: rect.h:51
void draw() override
Definition: menu.h:229
NanoPoint p1
Definition: rect.h:45
lcdint_t x
Definition: point.h:42
void setPos(const NanoPoint &p)
Definition: object.h:159
void draw() override
Definition: menu.h:182