From f2153f53afaa5157a87bc6d8e8ea4e9333a2bcc7 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Tue, 28 Nov 2023 22:23:24 +0100 Subject: [PATCH] ScreenComponents: Added padding for each textbox. modified Display() logic in component. In screen, added padding for Connecting(). --- embedded/lib/Component/Screen.cpp | 8 ++-- embedded/lib/Component/ScreenComponents.cpp | 52 ++++++++++++++------- embedded/lib/Component/ScreenComponents.hpp | 21 ++++++++- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/embedded/lib/Component/Screen.cpp b/embedded/lib/Component/Screen.cpp index bb54f30..7084fec 100644 --- a/embedded/lib/Component/Screen.cpp +++ b/embedded/lib/Component/Screen.cpp @@ -22,13 +22,13 @@ void Screen::Setup(uint8_t *font) _font = 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 connectingWindow.Add({headerSetup, TextBox("connect", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0)}); connectedWindow.Add({headerSetup, - TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0), - TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0), - TextBox("addr", StyleWidth::CENTERED, 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,0,2), + TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0,0,2)}); loopWindow.Add(TextBox("Hello, Plant!", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW1)); } diff --git a/embedded/lib/Component/ScreenComponents.cpp b/embedded/lib/Component/ScreenComponents.cpp index 6bbd5a0..abb5c07 100644 --- a/embedded/lib/Component/ScreenComponents.cpp +++ b/embedded/lib/Component/ScreenComponents.cpp @@ -10,6 +10,7 @@ Components::Components() void Components::Add(TextBox box) { + // When Added, boxes should be reordered by style. _boxes.push_back(box); } @@ -28,23 +29,41 @@ void Components::Update(size_t index, String text) void Components::Display() { - const auto size_boxes = _boxes.size(); 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); - if (it+1 != _boxes.end() && ((it+1)->getStyleHeight() == it->getStyleHeight())) + const auto size_boxes = GetSize(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++; + } 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) - : _text(str), _style(style), _styleWidth(sw), _styleHeight(sh) +TextBox::TextBox() +{ +} + +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(); } @@ -58,7 +77,7 @@ void TextBox::Update(String str) void TextBox::Calculate() { const auto width = Screen::GetInstance().getWidth(); - _textWidth = Screen::GetInstance().getTextWidth(_text.c_str()); + _textWidth = _takeWholeLine ? width : Screen::GetInstance().getTextWidth(_text.c_str()); switch (_styleWidth) { 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 const auto x = (_styleWidth == StyleWidth::CENTERED) ? _x : _x; switch (_styleHeight) { 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; case StyleHeight::CENTERED: - Screen::GetInstance().getScreen().drawButtonUTF8(x, static_cast((centeredOffset/2)) + (position+1)*FONT_SIZE, _style, _textWidth, 0, 0, _text.c_str()); + Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, static_cast((centeredOffset / 2)) + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, _paddingHeight, _paddingWidth, _text.c_str()); break; 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; } } -StyleHeight TextBox::getStyleHeight() { return _styleHeight; } \ No newline at end of file +StyleHeight TextBox::getStyleHeight() { return _styleHeight; } +u8g2_uint_t TextBox::getPadding() { return _paddingHeight; } \ No newline at end of file diff --git a/embedded/lib/Component/ScreenComponents.hpp b/embedded/lib/Component/ScreenComponents.hpp index 51b707d..816490f 100644 --- a/embedded/lib/Component/ScreenComponents.hpp +++ b/embedded/lib/Component/ScreenComponents.hpp @@ -26,19 +26,35 @@ namespace Display { public: 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); StyleHeight getStyleHeight(); + // Only vertical/height padding + u8g2_uint_t getPadding(); private: void Calculate(); String _text; uint8_t *_font; u8g2_uint_t _style; + u8g2_uint_t _paddingWidth; + u8g2_uint_t _paddingHeight; StyleWidth _styleWidth; StyleHeight _styleHeight; uint16_t _x; uint16_t _textWidth; + bool _takeWholeLine; }; // Components which contains every type @@ -51,6 +67,7 @@ namespace Display void Update(size_t index, String text); void Display(); private: + size_t GetSize(StyleHeight sh); // Boxes std::vector _boxes; };