From d74c9516d26ac8f97110b6b34402cd532e1baa8b Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Thu, 30 Nov 2023 22:46:52 +0100 Subject: [PATCH] Update Components size_pos parameter. Updated SpriteBox to actually use Styles. --- embedded/lib/Display/Components/Box.hpp | 16 +++---- .../lib/Display/Components/Components.cpp | 10 ++-- embedded/lib/Display/Components/SpriteBox.cpp | 46 +++++++++++++++---- embedded/lib/Display/Components/SpriteBox.hpp | 9 ++-- embedded/lib/Display/Components/TextBox.cpp | 17 +++---- embedded/lib/Display/Components/TextBox.hpp | 6 ++- embedded/lib/Display/Screen.cpp | 2 +- 7 files changed, 67 insertions(+), 39 deletions(-) diff --git a/embedded/lib/Display/Components/Box.hpp b/embedded/lib/Display/Components/Box.hpp index 0f8d897..e577eb8 100644 --- a/embedded/lib/Display/Components/Box.hpp +++ b/embedded/lib/Display/Components/Box.hpp @@ -5,11 +5,6 @@ namespace Display { - /** Size of the actual font from Screen. Used for many calculations - * ! Must be changed if the _font size is updated - */ - constexpr uint8_t FONT_SIZE = 8; - /** * @brief Where each Box element is centered on the x axis. */ @@ -47,8 +42,8 @@ namespace Display /** * @brief Construct a new Box object, can be ignored. */ - Box(StyleHeight sh, u8g2_uint_t h_padding) - : _styleHeight(sh), _paddingHeight(h_padding){}; + Box(StyleHeight sh, u8g2_uint_t h_padding, uint16_t height) + : _styleHeight(sh), _paddingHeight(h_padding), _height(height) {}; /** * @brief Used to display the element on the screen. @@ -56,10 +51,10 @@ namespace Display * ! Maybe a font will be added to arguments next... * * @param size the total size of the elements from a styleheight - * @param position the actual position of the element depending of the previous size + * @param size_pos all the above sizes from each components in a same style height * @param offsetY an offset in Y, given by the 'Components' */ - virtual void Display(size_t size, size_t position, u8g2_uint_t offsetY){}; + virtual void Display(size_t size, size_t size_pos, u8g2_uint_t offsetY){}; /** * @brief Will update by recalculating the 'Box' constants @@ -84,9 +79,12 @@ namespace Display */ virtual u8g2_uint_t getPadding() { return _paddingHeight; }; + virtual uint16_t getHeight() { return _height; } + protected: StyleHeight _styleHeight; u8g2_uint_t _paddingHeight; + uint16_t _height; }; } diff --git a/embedded/lib/Display/Components/Components.cpp b/embedded/lib/Display/Components/Components.cpp index 8aae7c8..9ed0356 100644 --- a/embedded/lib/Display/Components/Components.cpp +++ b/embedded/lib/Display/Components/Components.cpp @@ -27,22 +27,22 @@ void Components::Update(size_t index, String text) void Components::Display() { - size_t i(0); + size_t totalSize(0); u8g2_uint_t verticalPadding(0); for (auto it = _boxes.begin(); it != _boxes.end(); it++) { const auto size_boxes = GetSize((*it)->getStyleHeight()); - (*it)->Display(size_boxes, i, verticalPadding); + (*it)->Display(size_boxes, totalSize, 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++; + totalSize += (*it)->getHeight(); } else { verticalPadding = 0; - i = 0; + totalSize = 0; } } } @@ -52,6 +52,6 @@ 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); + returnSize += (box->getStyleHeight() == sh ? (box->getHeight()+box->getPadding()) : 0); return returnSize; } \ No newline at end of file diff --git a/embedded/lib/Display/Components/SpriteBox.cpp b/embedded/lib/Display/Components/SpriteBox.cpp index 08f892d..d125156 100644 --- a/embedded/lib/Display/Components/SpriteBox.cpp +++ b/embedded/lib/Display/Components/SpriteBox.cpp @@ -4,24 +4,54 @@ using namespace Display; SpriteBox::SpriteBox() - :Box(StyleHeight::UNDEFINED,0) + :Box(StyleHeight::UNDEFINED,0,0) {} -SpriteBox::SpriteBox(unsigned char *sprite, uint16_t width, uint16_t height, u8g2_uint_t x, u8g2_uint_t y) - :Box(StyleHeight::TOP,0) +SpriteBox::SpriteBox(unsigned char *sprite, uint16_t width, uint16_t height, StyleWidth sw, StyleHeight sh) + :Box(sh,0,height) , _sprite(sprite) + , _styleWidth(sw) , _width(width) , _height(height) - , _x(x) - , _y(y) -{} +{ + Calculate(); +} void SpriteBox::Update(std::any data) { _sprite = std::any_cast(data); + Calculate(); } -void SpriteBox::Display(size_t size, size_t position, u8g2_uint_t offsetY) +void SpriteBox::Calculate() { - Screen::GetInstance().getScreen().drawXBM(_x,_y,_width,_height,_sprite); + const auto screenWidth = Screen::GetInstance().getWidth(); + switch (_styleWidth) + { + case StyleWidth::CENTERED: + _x = (screenWidth - _width) / 2; + break; + case StyleWidth::RIGHT: + _x = screenWidth - _width; + break; + default: + _x = 0; + } +} + +void SpriteBox::Display(size_t size, size_t size_pos, u8g2_uint_t offsetY) +{ + const auto centeredOffset = (Screen::GetInstance().getHeight() - size); + switch(this->_styleHeight) + { + case StyleHeight::CENTERED: + // idk must be the size of all the above + Screen::GetInstance().getScreen().drawXBM(_x, static_cast((centeredOffset / 2)) + size_pos + offsetY,_width,_height,_sprite); + break; + case StyleHeight::BOTTOM: + Screen::GetInstance().getScreen().drawXBM(_x, centeredOffset + size_pos + offsetY,_width,_height,_sprite); + break; + default: + Screen::GetInstance().getScreen().drawXBM(_x, size_pos + offsetY,_width,_height,_sprite); + } } \ No newline at end of file diff --git a/embedded/lib/Display/Components/SpriteBox.hpp b/embedded/lib/Display/Components/SpriteBox.hpp index 06dcae5..357231c 100644 --- a/embedded/lib/Display/Components/SpriteBox.hpp +++ b/embedded/lib/Display/Components/SpriteBox.hpp @@ -14,8 +14,8 @@ namespace Display * * @param sprite array from an .xbm format */ - SpriteBox(unsigned char *sprite, uint16_t width, uint16_t height, u8g2_uint_t x=0, u8g2_uint_t y=0); - void Display(size_t size, size_t position, u8g2_uint_t offsetY) override; + SpriteBox(unsigned char *sprite, uint16_t width, uint16_t height, StyleWidth sw, StyleHeight sh); + void Display(size_t size, size_t size_pos, u8g2_uint_t offsetY) override; /** * @brief Updates sprite @@ -25,11 +25,12 @@ namespace Display void Update(std::any data) override; private: + void Calculate(); unsigned char* _sprite; + StyleWidth _styleWidth; uint16_t _width; uint16_t _height; - u8g2_uint_t _x; - u8g2_uint_t _y; + uint16_t _x; }; } diff --git a/embedded/lib/Display/Components/TextBox.cpp b/embedded/lib/Display/Components/TextBox.cpp index 03b5d7d..ef7d11f 100644 --- a/embedded/lib/Display/Components/TextBox.cpp +++ b/embedded/lib/Display/Components/TextBox.cpp @@ -4,12 +4,12 @@ using namespace Display; TextBox::TextBox() - : Box(StyleHeight::UNDEFINED, 0) + : Box(StyleHeight::UNDEFINED, 0, 0) { } TextBox::TextBox(String str, StyleWidth sw, StyleHeight sh, u8g2_uint_t style, u8g2_uint_t w_padding, u8g2_uint_t h_padding, bool takeWholeLine) - : Box(sh, h_padding), _text(str), _style(style), _paddingWidth(w_padding), _styleWidth(sw), _takeWholeLine(takeWholeLine) + : Box(sh, h_padding, FONT_SIZE), _text(str), _style(style), _paddingWidth(w_padding), _styleWidth(sw), _takeWholeLine(takeWholeLine) { Calculate(); } @@ -40,23 +40,18 @@ void TextBox::Calculate() } } -void TextBox::Display(size_t size, size_t position, u8g2_uint_t offsetY) +void TextBox::Display(size_t size, size_t size_pos, u8g2_uint_t offsetY) { const auto centeredOffset = (Screen::GetInstance().getHeight() - size); - // Later will be used with padding/margin - const auto x = (_styleWidth == StyleWidth::CENTERED) ? _x : _x; switch (this->_styleHeight) { - case StyleHeight::TOP: - Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); - break; case StyleHeight::CENTERED: - Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, static_cast((centeredOffset / 2)) + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); + Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, static_cast((centeredOffset / 2)) + size_pos + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); break; case StyleHeight::BOTTOM: - Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + x, centeredOffset + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); + Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, centeredOffset + size_pos + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); break; default: - break; + Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, size_pos + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); } } \ No newline at end of file diff --git a/embedded/lib/Display/Components/TextBox.hpp b/embedded/lib/Display/Components/TextBox.hpp index 6bb6c41..5200a54 100644 --- a/embedded/lib/Display/Components/TextBox.hpp +++ b/embedded/lib/Display/Components/TextBox.hpp @@ -4,6 +4,10 @@ namespace Display { + /** Size of the actual font from Screen. Used for many calculations + * ! Must be changed if the _font size is updated + */ + constexpr uint8_t FONT_SIZE = 8; class TextBox : public Box { @@ -21,7 +25,7 @@ namespace Display * @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) override; + void Display(size_t size, size_t size_pos, u8g2_uint_t offsetY) override; /** * @brief Updates String data diff --git a/embedded/lib/Display/Screen.cpp b/embedded/lib/Display/Screen.cpp index 5da8036..23ab7c4 100644 --- a/embedded/lib/Display/Screen.cpp +++ b/embedded/lib/Display/Screen.cpp @@ -34,7 +34,7 @@ void Screen::Setup(uint8_t *font) std::make_shared(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), std::make_shared(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)), std::make_shared(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))}); - loopWindow.Add(std::make_shared(SpriteBox(clover_bits,clover_height,clover_width))); + loopWindow.Add(std::make_shared(SpriteBox(clover_bits,clover_height,clover_width,StyleWidth::CENTERED,StyleHeight::CENTERED))); } void Screen::connecting(uint8_t state)