ScreenComponents: Added padding for each textbox. modified Display() logic in component. In screen, added padding for Connecting().

This commit is contained in:
Yohan Boujon 2023-11-28 22:23:24 +01:00
parent 629bae5fd5
commit f2153f53af
3 changed files with 59 additions and 22 deletions

View file

@ -22,13 +22,13 @@ void Screen::Setup(uint8_t *font)
_font = font; _font = font;
_screen->setFont(_font); _screen->setFont(_font);
headerSetup = TextBox("Clover Setup", StyleWidth::LEFT, StyleHeight::TOP, U8G2_BTN_INV); headerSetup = TextBox("Clover Setup", StyleWidth::LEFT, StyleHeight::TOP, U8G2_BTN_INV,2,5, true);
// Static Components // Static Components
connectingWindow.Add({headerSetup, TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)}); connectingWindow.Add({headerSetup, TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)});
connectedWindow.Add({headerSetup, connectedWindow.Add({headerSetup,
TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0), TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2),
TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0), TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2),
TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)}); TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2)});
loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1)); loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1));
} }

View file

@ -10,6 +10,7 @@ Components::Components()
void Components::Add(TextBox box) void Components::Add(TextBox box)
{ {
// When Added, boxes should be reordered by style.
_boxes.push_back(box); _boxes.push_back(box);
} }
@ -28,23 +29,41 @@ void Components::Update(size_t index, String text)
void Components::Display() void Components::Display()
{ {
const auto size_boxes = _boxes.size();
size_t i(0); size_t i(0);
for(auto it = _boxes.begin() ; it!= _boxes.end() ;it++) u8g2_uint_t verticalPadding(0);
for (auto it = _boxes.begin(); it != _boxes.end(); it++)
{ {
it->Display(size_boxes,i); const auto size_boxes = GetSize(it->getStyleHeight());
if (it+1 != _boxes.end() && ((it+1)->getStyleHeight() == it->getStyleHeight())) it->Display(size_boxes, i, verticalPadding);
// Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.)
if (it + 1 != _boxes.end() && ((it + 1)->getStyleHeight() == it->getStyleHeight()))
{
verticalPadding += it->getPadding();
i++; i++;
}
else else
i=0; {
verticalPadding = 0;
i = 0;
}
} }
} }
TextBox::TextBox() size_t Components::GetSize(StyleHeight sh)
{} {
size_t returnSize(0);
// returnSize is equal to FONT_SIZE + vertical padding * boxes with style sh
for(auto& box : _boxes)
returnSize += (box.getStyleHeight() == sh ? (FONT_SIZE+box.getPadding()) : 0);
return returnSize;
}
TextBox::TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style) TextBox::TextBox()
: _text(str), _style(style), _styleWidth(sw), _styleHeight(sh) {
}
TextBox::TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style, u8g2_uint_t w_padding, u8g2_uint_t h_padding, bool takeWholeLine)
: _text(str), _style(style), _paddingWidth(w_padding), _paddingHeight(h_padding), _styleWidth(sw), _styleHeight(sh), _takeWholeLine(takeWholeLine)
{ {
Calculate(); Calculate();
} }
@ -58,7 +77,7 @@ void TextBox::Update(String str)
void TextBox::Calculate() void TextBox::Calculate()
{ {
const auto width = Screen::GetInstance().getWidth(); const auto width = Screen::GetInstance().getWidth();
_textWidth = Screen::GetInstance().getTextWidth(_text.c_str()); _textWidth = _takeWholeLine ? width : Screen::GetInstance().getTextWidth(_text.c_str());
switch (_styleWidth) switch (_styleWidth)
{ {
case StyleWidth::LEFT: case StyleWidth::LEFT:
@ -73,23 +92,24 @@ void TextBox::Calculate()
} }
} }
void TextBox::Display(size_t size, size_t position) void TextBox::Display(size_t size, size_t position, u8g2_uint_t offsetY)
{ {
const auto centeredOffset = (Screen::GetInstance().getHeight() - (size * FONT_SIZE)); const auto centeredOffset = (Screen::GetInstance().getHeight() - size);
// Later will be used with padding/margin // Later will be used with padding/margin
const auto x = (_styleWidth == StyleWidth::CENTERED) ? _x : _x; const auto x = (_styleWidth == StyleWidth::CENTERED) ? _x : _x;
switch (_styleHeight) switch (_styleHeight)
{ {
case StyleHeight::TOP: case StyleHeight::TOP:
Screen::GetInstance().getScreen().drawButtonUTF8(x, (position+1)*FONT_SIZE, _style, _textWidth, 0, 0, _text.c_str()); Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str());
break; break;
case StyleHeight::CENTERED: case StyleHeight::CENTERED:
Screen::GetInstance().getScreen().drawButtonUTF8(x, static_cast<uint16_t>((centeredOffset/2)) + (position+1)*FONT_SIZE, _style, _textWidth, 0, 0, _text.c_str()); Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, static_cast<uint16_t>((centeredOffset / 2)) + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str());
break; break;
case StyleHeight::BOTTOM: case StyleHeight::BOTTOM:
Screen::GetInstance().getScreen().drawButtonUTF8(x, centeredOffset + (position+1)*FONT_SIZE, _style, _textWidth, 0, 0, _text.c_str()); Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, centeredOffset + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str());
break; break;
} }
} }
StyleHeight TextBox::getStyleHeight() { return _styleHeight; } StyleHeight TextBox::getStyleHeight() { return _styleHeight; }
u8g2_uint_t TextBox::getPadding() { return _paddingHeight; }

View file

@ -26,19 +26,35 @@ namespace Display
{ {
public: public:
TextBox(); TextBox();
TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style); /**
void Display(size_t size, size_t position); * @brief Construct a new Text Box object
*
* @param str the string to display
* @param sw style width (LEFT, CENTERED, RIGHT)
* @param sh style height (TOP, CENTERED, BOTTOM)
* @param style u8g2lib button style (see doc @ )
* @param w_padding width padding (default: 0)
* @param h_padding height padding (default: 0)
* @param takeWholeLine if true, the button takes the whole line
*/
TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style, u8g2_uint_t w_padding=0, u8g2_uint_t h_padding=0, bool takeWholeLine=false);
void Display(size_t size, size_t position, u8g2_uint_t offsetY);
void Update(String str); void Update(String str);
StyleHeight getStyleHeight(); StyleHeight getStyleHeight();
// Only vertical/height padding
u8g2_uint_t getPadding();
private: private:
void Calculate(); void Calculate();
String _text; String _text;
uint8_t *_font; uint8_t *_font;
u8g2_uint_t _style; u8g2_uint_t _style;
u8g2_uint_t _paddingWidth;
u8g2_uint_t _paddingHeight;
StyleWidth _styleWidth; StyleWidth _styleWidth;
StyleHeight _styleHeight; StyleHeight _styleHeight;
uint16_t _x; uint16_t _x;
uint16_t _textWidth; uint16_t _textWidth;
bool _takeWholeLine;
}; };
// Components which contains every type // Components which contains every type
@ -51,6 +67,7 @@ namespace Display
void Update(size_t index, String text); void Update(size_t index, String text);
void Display(); void Display();
private: private:
size_t GetSize(StyleHeight sh);
// Boxes // Boxes
std::vector<TextBox> _boxes; std::vector<TextBox> _boxes;
}; };