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
spinbox.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_SPINBOX_H_
29 #define _LCDGFX_SPINBOX_H_
30 
31 #include "nano_gfx_types.h"
32 #include "canvas/point.h"
33 #include "canvas/rect.h"
34 #include "canvas/font.h"
35 #include <stdio.h>
36 
50 {
51 public:
62  LcdGfxSpinbox(const NanoRect &rect, int16_t minValue, int16_t maxValue, int16_t value, int16_t step = 1,
63  bool wrap = false);
64 
69  template <typename D> void show(D &d)
70  {
71  char buf[8];
72  snprintf(buf, sizeof(buf), "%d", (int)m_value);
73  lcduint_t fontH = d.getFont().getHeader().height;
74  lcduint_t arrowW = d.getFont().getTextSize("<");
75  lcduint_t textW = d.getFont().getTextSize(buf);
76  lcdint_t innerLeft = m_rect.p1.x + 2 + (lcdint_t)arrowW + 2;
77  lcdint_t innerRight = m_rect.p2.x - 2 - (lcdint_t)arrowW - 2;
78  lcdint_t cy = m_rect.p1.y + (m_rect.height() - (lcdint_t)fontH) / 2;
79  lcdint_t cx = innerLeft + ((innerRight - innerLeft) - (lcdint_t)textW) / 2;
80 
81  // Clear only the central text band so a shorter number doesn't
82  // leave digits behind. Then redraw border + arrows + value.
83  uint16_t color = d.getColor();
84  d.setColor(0x0000);
85  d.fillRect(innerLeft, m_rect.p1.y + 1, innerRight, m_rect.p2.y - 1);
86  d.setColor(color);
87  d.drawRect(m_rect);
88  d.printFixed(m_rect.p1.x + 2, cy, "<");
89  d.printFixed(m_rect.p2.x - 2 - (lcdint_t)d.getFont().getTextSize(">"), cy, ">");
90  d.printFixed(cx, cy, buf);
91  }
92 
96  void up();
97 
101  void down();
102 
106  void setValue(int16_t value);
107 
111  int16_t value() const
112  {
113  return m_value;
114  }
115 
116  int16_t minValue() const
117  {
118  return m_min;
119  }
120 
121  int16_t maxValue() const
122  {
123  return m_max;
124  }
125 
126  bool wraps() const
127  {
128  return m_wrap;
129  }
130 
131  const NanoRect &getRect() const
132  {
133  return m_rect;
134  }
135 
136 private:
137  NanoRect m_rect;
138  int16_t m_min;
139  int16_t m_max;
140  int16_t m_value;
141  int16_t m_step;
142  bool m_wrap;
143 };
144 
149 #endif
uint8_t lcduint_t
internal int type, used by the library.
Definition: canvas_types.h:79
LcdGfxSpinbox(const NanoRect &rect, int16_t minValue, int16_t maxValue, int16_t value, int16_t step=1, bool wrap=false)
Creates a spinbox widget.
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
lcdint_t y
y position in pixels
Definition: point.h:44
Rectangle class.
Basic structures of nano gfx library.
int16_t value() const
Returns the current value.
Definition: spinbox.h:111
Class implements an integer spinbox widget for lcdgfx library.
Definition: spinbox.h:49
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 down()
Decrements the value (wraps or clamps depending on configuration).
Font class.
void setValue(int16_t value)
Sets the current value, clamped into [min, max].
void show(D &d)
Renders the spinbox on the display: a border with a centered numeric value and "<" ">" arrows on the ...
Definition: spinbox.h:69
void up()
Increments the value (wraps or clamps depending on configuration).