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
text_entry.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2025, 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_TEXT_ENTRY_H_
29 #define _LCDGFX_TEXT_ENTRY_H_
30 
31 #include "nano_gfx_types.h"
32 #include "canvas/point.h"
33 #include "canvas/rect.h"
34 #include "canvas/font.h"
35 
61 {
62 public:
82  LcdGfxTextEntry(char *buffer, uint8_t maxLen, const NanoRect &rect,
83  const char *charset = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-_");
84 
88  void up();
89 
93  void down();
94 
98  void left();
99 
103  void right();
104 
108  void setCursor(uint8_t cursor);
109 
113  uint8_t getCursor() const
114  {
115  return m_cursor;
116  }
117 
121  const char *getText() const
122  {
123  return m_buffer;
124  }
125 
129  uint8_t getMaxLen() const
130  {
131  return m_maxLen;
132  }
133 
143  template <typename D> void show(D &d)
144  {
145  updateSize(d);
146  d.drawRect(m_rect);
147  const lcduint_t fontH = d.getFont().getHeader().height;
148  const lcduint_t fontW = d.getFont().getHeader().width;
149  const lcdint_t baseX = m_rect.p1.x + 2;
150  const lcdint_t baseY = m_rect.p1.y + (m_rect.height() - fontH) / 2;
151  d.printFixed(baseX, baseY, m_buffer);
152  const lcdint_t cursorX = baseX + (lcdint_t)m_cursor * (lcdint_t)fontW;
153  const lcdint_t cursorY = baseY + (lcdint_t)fontH;
154  d.drawHLine(cursorX, cursorY, cursorX + (lcdint_t)fontW - 2);
155  }
156 
161  template <typename D> void updateSize(D &d)
162  {
163  if ( !m_rect.p2.x )
164  {
165  const lcduint_t fontH = d.getFont().getHeader().height;
166  const lcduint_t fontW = d.getFont().getHeader().width;
167  m_rect.p2.x = m_rect.p1.x + (lcdint_t)fontW * (lcdint_t)m_maxLen + 4 - 1;
168  m_rect.p2.y = m_rect.p1.y + (lcdint_t)fontH + 4 - 1;
169  }
170  }
171 
175  const NanoRect &getRect() const
176  {
177  return m_rect;
178  }
179 
180 private:
181  char *m_buffer;
182  const char *m_charset;
183  uint8_t m_charsetLen;
184  uint8_t m_maxLen;
185  uint8_t m_cursor = 0;
186  NanoRect m_rect;
187 
188  uint8_t indexOfCurrentChar() const;
189 };
190 
195 #endif
void setCursor(uint8_t cursor)
Sets the cursor position.
uint8_t lcduint_t
internal int type, used by the library.
Definition: canvas_types.h:79
NanoRect structure describes rectangle area.
Definition: rect.h:42
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
uint8_t getCursor() const
Returns the current cursor position.
Definition: text_entry.h:113
void down()
Cycles the character at the cursor backward through the charset.
lcdint_t y
y position in pixels
Definition: point.h:44
void left()
Moves the cursor one cell to the left, clamped at 0.
void up()
Cycles the character at the cursor forward through the charset.
uint8_t getMaxLen() const
Returns the maximum number of editable cells.
Definition: text_entry.h:129
Rectangle class.
Single-line text-entry widget.
Definition: text_entry.h:60
const NanoRect & getRect() const
Returns the rectangle occupied by the widget.
Definition: text_entry.h:175
const char * getText() const
Returns the underlying NUL-terminated text buffer.
Definition: text_entry.h:121
LcdGfxTextEntry(char *buffer, uint8_t maxLen, const NanoRect &rect, const char *charset=" ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-_")
Creates a text-entry widget bound to the user&#39;s buffer.
Basic structures of nano gfx library.
void updateSize(D &d)
Auto-sizes the widget rectangle from the active font if a zero rect was supplied to the constructor...
Definition: text_entry.h:161
void show(D &d)
Renders the widget to the display.
Definition: text_entry.h:143
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 right()
Moves the cursor one cell to the right, clamped at maxLen - 1.
Font class.