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
button.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 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 _LCDGFX_BUTTON_H_
29 #define _LCDGFX_BUTTON_H_
30 
31 #include "nano_gfx_types.h"
32 #include "canvas/point.h"
33 #include "canvas/rect.h"
34 #include "canvas/font.h"
35 
45 {
46 public:
53  LcdGfxButton(const char *text, const NanoRect &rect);
54 
60  template <typename D> void show(D &d)
61  {
62  updateSize(d);
63  d.invertColors();
64  d.fillRect(m_rect);
65  d.invertColors();
66  if ( m_focus )
67  {
68  d.fillRect(m_rect);
69  d.invertColors();
70  }
71  else
72  {
73  d.drawRect(m_rect);
74  }
75  d.printFixed(m_rect.p1.x + (m_rect.width() - d.getFont().getTextSize(m_text)) / 2,
76  m_rect.p1.y + (m_rect.height() - d.getFont().getHeader().height) / 2, m_text);
77  if ( m_focus )
78  {
79  d.invertColors();
80  }
81  }
82 
89  void setFocus(bool focus);
90 
94  bool isActive();
95 
101  void setRect(const NanoRect &rect);
102 
106  const NanoPoint getSize();
107 
111  const NanoRect &getRect() const
112  {
113  return m_rect;
114  }
115 
119  template <typename D> void updateSize(D &d)
120  {
121  if ( !m_rect.p2.x )
122  {
123  lcduint_t width = d.getFont().getTextSize(m_text);
124  lcduint_t height = d.getFont().getHeader().height;
125  m_rect.p2.x = m_rect.p1.x + width - 1 + 8;
126  m_rect.p2.y = m_rect.p1.y + height - 1 + 8;
127  }
128  }
129 
130 private:
131  bool m_focus = false;
132  const char *m_text;
133  NanoRect m_rect;
134 };
135 
140 #endif
uint8_t lcduint_t
Definition: canvas_types.h:79
Definition: rect.h:42
lcdint_t height() const
Definition: rect.h:69
const NanoRect & getRect() const
Definition: button.h:111
NanoPoint p2
Definition: rect.h:48
lcdint_t y
Definition: point.h:44
LcdGfxButton(const char *text, const NanoRect &rect)
void setFocus(bool focus)
bool isActive()
lcdint_t width() const
Definition: rect.h:51
NanoPoint p1
Definition: rect.h:45
void show(D &d)
Definition: button.h:60
lcdint_t x
Definition: point.h:42
void setRect(const NanoRect &rect)
void updateSize(D &d)
Definition: button.h:119
const NanoPoint getSize()