CheeseBoard
A library for use with the CheeseBoard Cheddar platform
GfxTextBox2.cpp
1 #include <MutilaDebug.h>
2 #include "GfxTextBox2.h"
3 #include "CbOledDisplay.h"
4 
5 GfxTextBox2::GfxTextBox2(String initialText, GfxFont& font, bool border, uint8_t padding, char justify, uint16_t width, uint16_t height) :
6  _text(initialText),
7  _font(font),
8  _border(border),
9  _padding(padding),
10  _justify(justify),
11  _width(width),
12  _height(height)
13 {
14  if (_justify != 'L' && _justify != 'C' && _justify != 'R') {
15  _justify = 'C';
16  }
17 
18  if (height == 0) {
19  _height = _font.height() + 2 + (2*_padding);
20  }
21 
22  if (width == 0) {
23  _font.use();
24  _width = CbOledDisplay.getStrWidth(_text.c_str()) + 2 + (2*_padding);
25  }
26 }
27 
28 void GfxTextBox2::draw(uint16_t xOffset, uint16_t yOffset) {
29  _DBF("GfxText draw@%d,%d : text=%s\n", xOffset, yOffset, _text.c_str());
30  _font.use();
31 
32  uint16_t useLen = _text.length();
33  // If the text does not fit in the box, trucate it until it does
34  while (true) {
35  if (CbOledDisplay.getStrWidth(_text.substring(0, useLen).c_str()) <= _width-2-(2*_padding) || useLen == 0) {
36  break;
37  } else {
38  useLen--;
39  }
40  }
41 
42  CbOledDisplay.drawStr(xOffset + 1 + _padding,
43  yOffset + _height - 1 - _padding,
44  _text.substring(0, useLen+1).c_str());
45 
46  if (_border) {
47  CbOledDisplay.drawFrame(xOffset, yOffset, _width, _height);
48  }
49 }
50 
52 {
53  return _width;
54 }
55 
56 uint16_t GfxTextBox2::height() {
57  return _height;
58 }
59 
60 const String& GfxTextBox2::text()
61 {
62  return _text;
63 }
64 
65 void GfxTextBox2::setText(const String& newText)
66 {
67  _text = newText;
68 }
69 
void draw(uint16_t xOffset=0, uint16_t yOffset=0)
Definition: GfxTextBox2.cpp:28
const String & text()
Definition: GfxTextBox2.cpp:60
uint16_t width()
Definition: GfxTextBox2.cpp:51
Definition: GfxFont.h:8
GfxTextBox2(String initialText, GfxFont &font=GfxDefaultFont, bool border=true, uint8_t padding=1, char justify='C', uint16_t width=0, uint16_t height=0)
Definition: GfxTextBox2.cpp:5
uint16_t height()
Definition: GfxTextBox2.cpp:56
uint8_t height()
Definition: GfxFont.cpp:11
void setText(const String &newText)
Definition: GfxTextBox2.cpp:65
void use()
Definition: GfxFont.cpp:16