Update Components size_pos parameter. Updated SpriteBox to actually use Styles.

This commit is contained in:
Yohan Boujon 2023-11-30 22:46:52 +01:00
parent 38308f77f8
commit d74c9516d2
7 changed files with 67 additions and 39 deletions

View file

@ -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;
};
}

View file

@ -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;
}

View file

@ -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<unsigned char*>(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<uint16_t>((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);
}
}

View file

@ -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;
};
}

View file

@ -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<uint16_t>((centeredOffset / 2)) + (position + 1) * FONT_SIZE + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str());
Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, static_cast<uint16_t>((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());
}
}

View file

@ -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

View file

@ -34,7 +34,7 @@ void Screen::Setup(uint8_t *font)
std::make_shared<TextBox>(TextBox("Connected to Wi-Fi !", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
std::make_shared<TextBox>(TextBox("IP address: ", StyleWidth::LEFT, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2)),
std::make_shared<TextBox>(TextBox("addr", StyleWidth::CENTERED, StyleHeight::CENTERED, U8G2_BTN_BW0, 0, 2))});
loopWindow.Add(std::make_shared<SpriteBox>(SpriteBox(clover_bits,clover_height,clover_width)));
loopWindow.Add(std::make_shared<SpriteBox>(SpriteBox(clover_bits,clover_height,clover_width,StyleWidth::CENTERED,StyleHeight::CENTERED)));
}
void Screen::connecting(uint8_t state)