Fixed TextBox not well aligned. (Not tested on real hw.)

This commit is contained in:
Yohan Boujon 2023-12-01 14:33:21 +01:00
parent d74c9516d2
commit ad71d0997b
6 changed files with 16 additions and 24 deletions

View file

@ -51,10 +51,9 @@ namespace Display
* ! Maybe a font will be added to arguments next... * ! Maybe a font will be added to arguments next...
* *
* @param size the total size of the elements from a styleheight * @param size the total size of the elements from a styleheight
* @param size_pos all the above sizes from each components in a same style height * @param size_pos all the above sizes from each components in a same style height plus its height padding
* @param offsetY an offset in Y, given by the 'Components'
*/ */
virtual void Display(size_t size, size_t size_pos, u8g2_uint_t offsetY){}; virtual void Display(u8g2_uint_t size, u8g2_uint_t size_pos){};
/** /**
* @brief Will update by recalculating the 'Box' constants * @brief Will update by recalculating the 'Box' constants

View file

@ -27,23 +27,16 @@ void Components::Update(size_t index, String text)
void Components::Display() void Components::Display()
{ {
size_t totalSize(0); u8g2_uint_t totalSize(0);
u8g2_uint_t verticalPadding(0);
for (auto it = _boxes.begin(); it != _boxes.end(); it++) for (auto it = _boxes.begin(); it != _boxes.end(); it++)
{ {
const auto size_boxes = GetSize((*it)->getStyleHeight()); const auto size_boxes = GetSize((*it)->getStyleHeight());
(*it)->Display(size_boxes, totalSize, verticalPadding); (*it)->Display(size_boxes, totalSize);
// Index and verticalPadding only incrementing for the same style. (eg : it and it+1 as the same style.) // 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())) if (it + 1 != _boxes.end() && ((*(it+1))->getStyleHeight() == (*it)->getStyleHeight()))
{ totalSize += (*it)->getHeight() + (2*(*it)->getPadding());
verticalPadding += (*it)->getPadding();
totalSize += (*it)->getHeight();
}
else else
{
verticalPadding = 0;
totalSize = 0; totalSize = 0;
}
} }
} }
@ -52,6 +45,6 @@ size_t Components::GetSize(StyleHeight sh)
size_t returnSize(0); size_t returnSize(0);
// returnSize is equal to FONT_SIZE + vertical padding * boxes with style sh // returnSize is equal to FONT_SIZE + vertical padding * boxes with style sh
for(auto& box : _boxes) for(auto& box : _boxes)
returnSize += (box->getStyleHeight() == sh ? (box->getHeight()+box->getPadding()) : 0); returnSize += (box->getStyleHeight() == sh ? (box->getHeight()+(2*box->getPadding())) : 0);
return returnSize; return returnSize;
} }

View file

@ -39,19 +39,19 @@ void SpriteBox::Calculate()
} }
} }
void SpriteBox::Display(size_t size, size_t size_pos, u8g2_uint_t offsetY) void SpriteBox::Display(u8g2_uint_t size, u8g2_uint_t size_pos)
{ {
const auto centeredOffset = (Screen::GetInstance().getHeight() - size); const auto centeredOffset = (Screen::GetInstance().getHeight() - size);
switch(this->_styleHeight) switch(this->_styleHeight)
{ {
case StyleHeight::CENTERED: case StyleHeight::CENTERED:
// idk must be the size of all the above // 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); Screen::GetInstance().getScreen().drawXBM(_x, static_cast<uint16_t>((centeredOffset / 2)) + size_pos,_width,_height,_sprite);
break; break;
case StyleHeight::BOTTOM: case StyleHeight::BOTTOM:
Screen::GetInstance().getScreen().drawXBM(_x, centeredOffset + size_pos + offsetY,_width,_height,_sprite); Screen::GetInstance().getScreen().drawXBM(_x, centeredOffset + size_pos,_width,_height,_sprite);
break; break;
default: default:
Screen::GetInstance().getScreen().drawXBM(_x, size_pos + offsetY,_width,_height,_sprite); Screen::GetInstance().getScreen().drawXBM(_x, size_pos,_width,_height,_sprite);
} }
} }

View file

@ -15,7 +15,7 @@ namespace Display
* @param sprite array from an .xbm format * @param sprite array from an .xbm format
*/ */
SpriteBox(unsigned char *sprite, uint16_t width, uint16_t height, StyleWidth sw, StyleHeight sh); 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; void Display(u8g2_uint_t size, u8g2_uint_t size_pos) override;
/** /**
* @brief Updates sprite * @brief Updates sprite

View file

@ -40,18 +40,18 @@ void TextBox::Calculate()
} }
} }
void TextBox::Display(size_t size, size_t size_pos, u8g2_uint_t offsetY) void TextBox::Display(u8g2_uint_t size, u8g2_uint_t size_pos)
{ {
const auto centeredOffset = (Screen::GetInstance().getHeight() - size); const auto centeredOffset = (Screen::GetInstance().getHeight() - size);
switch (this->_styleHeight) switch (this->_styleHeight)
{ {
case StyleHeight::CENTERED: case StyleHeight::CENTERED:
Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, static_cast<uint16_t>((centeredOffset / 2)) + size_pos + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, static_cast<uint16_t>((centeredOffset / 2)) + size_pos + _height, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str());
break; break;
case StyleHeight::BOTTOM: case StyleHeight::BOTTOM:
Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, centeredOffset + size_pos + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, centeredOffset + size_pos + _height, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str());
break; break;
default: default:
Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, size_pos + offsetY, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str()); Screen::GetInstance().getScreen().drawButtonUTF8(_paddingWidth + _x, size_pos + _height, _style, _textWidth, this->_paddingHeight, _paddingWidth, _text.c_str());
} }
} }

View file

@ -25,7 +25,7 @@ namespace Display
* @param takeWholeLine if true, the button takes the whole line * @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); 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 size_pos, u8g2_uint_t offsetY) override; void Display(u8g2_uint_t size, u8g2_uint_t size_pos) override;
/** /**
* @brief Updates String data * @brief Updates String data