LCDGFX LCD display driver  1.0.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, 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>
42 class NanoMenuItem: public NanoObject<T>
43 {
44 public:
46 };
47 
52 template<class T>
53 class NanoMenu: public NanoObjectList<T>
54 {
55 public:
61  NanoMenu(const NanoPoint &pos )
62  : NanoObjectList<T>( pos )
63  {
64  }
65 
69  NanoMenu(): NanoObjectList<T>( {0,0} )
70  {
71  }
72 
78  void add( NanoObject<T> &item )
79  {
80  NanoObjectList<T>::add( item );
81  item.update(); // update item to init all params
82  updateMenuItemsPosition();
83  if ( !m_selected )
84  {
85  m_selected = &item;
86  item.focus();
87  }
88  }
89 
95  void insert( NanoObject<T> &item )
96  {
98  item.update(); // update item to init all params
99  updateMenuItemsPosition();
100  if ( !m_selected )
101  {
102  m_selected = &item;
103  item.focus();
104  }
105  }
106 
112  {
113  return m_selected;
114  }
115 
121  void down()
122  {
123  m_selected->defocus();
124  m_selected = this->getNext( m_selected );
125  if ( !m_selected )
126  {
127  m_selected = this->getNext();
128  }
129  m_selected->focus();
130  }
131 
137  void up()
138  {
139  m_selected->defocus();
140  m_selected = getPrev( m_selected );
141  if ( !m_selected )
142  {
143  m_selected = this->getPrev();
144  }
145  m_selected->focus();
146  }
147 
148 protected:
153  virtual void updateMenuItemsPosition() = 0;
154 
155 private:
156  NanoObject<T> *m_selected = nullptr;
157 };
158 
163 template<class T>
164 class NanoListMenu: public NanoMenu<T>
165 {
166 public:
167  using NanoMenu<T>::NanoMenu;
168 
172  void draw() override
173  {
174  this->getTiler().getCanvas().setColor( 0xFFFF );
175  this->getTiler().getCanvas().drawRect( { this->m_rect.p1 + (NanoPoint){2, 2},
176  this->m_rect.p2 - (NanoPoint){2, 2} } );
178  }
179 
180 private:
181  void updateMenuItemsPosition() override
182  {
183  NanoObject<T> *p = this->getNext();
184  lcdint_t y_pos = this->m_rect.p1.y + 4;
185  while (p)
186  {
187  p->setPos( { (lcdint_t)(this->m_rect.p1.x + 4), y_pos } );
188  y_pos += p->height() + 1;
189  p = this->getNext( p );
190  }
191  this->m_rect.p2.y = y_pos + 7;
192  this->m_rect.p2.x = this->getDisplay().width();
193  }
194 };
195 
201 template<class T>
202 class NanoFixedWidthMenu: public NanoMenu<T>
203 {
204 public:
211  NanoFixedWidthMenu(const NanoPoint &pos, const NanoPoint &size)
212  : NanoMenu<T>( pos )
213  {
214  this->setSize( size );
215  }
216 
220  void draw() override
221  {
222  this->getTiler().getCanvas().setColor( 0xFFFF );
223  this->getTiler().getCanvas().drawRect( { this->m_rect.p1 + (NanoPoint){2, 2},
224  this->m_rect.p2 - (NanoPoint){2, 2} } );
226  }
227 
228 private:
229  void updateMenuItemsPosition() override
230  {
231  NanoObject<T> *p = this->getNext();
232  lcdint_t y_pos = this->m_rect.p1.y + 4;
233  while (p)
234  {
235  p->setPos( { (lcdint_t)(this->m_rect.p1.x + 4), y_pos } );
236  p->setSize( { (lcdint_t)(this->m_rect.width() - 8), p->height() } );
237  y_pos += p->height() + 1;
238  p = this->getNext( p );
239  }
240  }
241 };
242 
247 #endif
248 
void add(NanoObject< T > &item)
Definition: menu.h:78
struct _NanoPoint NanoPoint
lcdint_t height() const
Definition: object.h:108
void setSize(const NanoPoint &size)
Definition: object.h:150
NanoMenu()
Definition: menu.h:69
NanoPoint p2
Definition: rect.h:48
int8_t lcdint_t
Definition: canvas_types.h:79
NanoMenu(const NanoPoint &pos)
Definition: menu.h:61
void up()
Definition: menu.h:137
void focus()
Definition: tiler.h:114
lcdint_t y
Definition: point.h:45
NanoObject< T > * getSelected()
Definition: menu.h:111
void insert(NanoObject< T > &item)
Definition: menu.h:95
NanoFixedWidthMenu(const NanoPoint &pos, const NanoPoint &size)
Definition: menu.h:211
void insert(NanoObject< T > &object)
Definition: object.h:353
Definition: menu.h:53
void down()
Definition: menu.h:121
T & getTiler()
Definition: tiler.h:147
NanoRect m_rect
Definition: object.h:229
void update() override
Definition: object.h:95
void add(NanoObject< T > &object)
Definition: object.h:329
void draw() override
Definition: object.h:299
lcdint_t width() const
Definition: rect.h:51
void draw() override
Definition: menu.h:220
NanoPoint p1
Definition: rect.h:45
lcdint_t x
Definition: point.h:43
void setPos(const NanoPoint &p)
Definition: object.h:159
void draw() override
Definition: menu.h:172