Made multiple optimizations to the GUI components.

This commit is contained in:
Leon Styhre 2021-09-28 21:46:45 +02:00
parent 32d4353d69
commit 0077e334b7
5 changed files with 34 additions and 10 deletions

View file

@ -95,18 +95,27 @@ void GuiComponent::renderChildren(const glm::mat4& transform) const
void GuiComponent::setPosition(float x, float y, float z)
{
if (mPosition.x == x && mPosition.y == y && mPosition.z == z)
return;
mPosition = glm::vec3{x, y, z};
onPositionChanged();
}
void GuiComponent::setOrigin(float x, float y)
{
if (mOrigin.x == x && mOrigin.y == y)
return;
mOrigin = glm::vec2{x, y};
onOriginChanged();
}
void GuiComponent::setSize(float w, float h)
{
if (mSize.x == w && mSize.y == h)
return;
mSize = glm::vec2{w, h};
onSizeChanged();
}
@ -166,6 +175,9 @@ int GuiComponent::getChildIndex() const
void GuiComponent::setOpacity(unsigned char opacity)
{
if (mOpacity == opacity)
return;
mOpacity = opacity;
for (auto it = mChildren.cbegin(); it != mChildren.cend(); it++)
(*it)->setOpacity(opacity);

View file

@ -138,7 +138,7 @@ void ComponentGrid::updateCellComponent(const GridEntry& cell)
for (int y = cell.pos.y; y < cell.pos.y + cell.dim.y; y++)
size.y += getRowHeight(y);
if (cell.resize)
if (cell.resize && size != glm::vec2{} && cell.component->getSize() != size)
cell.component->setSize(size);
// Find top left corner.

View file

@ -156,12 +156,14 @@ public:
e.selected = selected;
mEntries.push_back(e);
onSelectedChanged();
if (selected)
onSelectedChanged();
}
bool selectEntry(unsigned int entry)
{
if (entry > mEntries.size()) {
if (mEntries.empty() || entry > mEntries.size()) {
return false;
}
else {
@ -258,6 +260,10 @@ private:
mParent->onSizeChanged();
}
else {
// Make a size update so the text for the first entry is properly aligned.
if (mText.getSize().x > 0.0f && mText.getSize().y > 0.0f)
setSize(mText.getSize());
// Display the selected entry and left/right option arrows.
for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++) {
if (it->selected) {

View file

@ -53,7 +53,7 @@ TextComponent::TextComponent(Window* window,
setFont(font);
setColor(color);
setBackgroundColor(bgcolor);
setText(text);
setText(text, false);
setPosition(pos);
setSize(size);
}
@ -66,6 +66,9 @@ void TextComponent::onSizeChanged()
void TextComponent::setFont(const std::shared_ptr<Font>& font)
{
if (mFont == font)
return;
mFont = font;
onTextChanged();
}
@ -103,10 +106,15 @@ void TextComponent::setOpacity(unsigned char opacity)
GuiComponent::setOpacity(opacity);
}
void TextComponent::setText(const std::string& text)
void TextComponent::setText(const std::string& text, bool update)
{
if (mText == text)
return;
mText = text;
onTextChanged();
if (update)
onTextChanged();
}
void TextComponent::setUppercase(bool uppercase)
@ -262,8 +270,6 @@ void TextComponent::setHorizontalAlignment(Alignment align)
onTextChanged();
}
void TextComponent::setVerticalAlignment(Alignment align) { mVerticalAlignment = align; }
void TextComponent::setLineSpacing(float spacing)
{
mLineSpacing = spacing;

View file

@ -38,11 +38,11 @@ public:
void setFont(const std::shared_ptr<Font>& font);
void setUppercase(bool uppercase);
void onSizeChanged() override;
void setText(const std::string& text);
void setText(const std::string& text, bool update = true);
void setHiddenText(const std::string& text) { mHiddenText = text; }
void setColor(unsigned int color) override;
void setHorizontalAlignment(Alignment align);
void setVerticalAlignment(Alignment align);
void setVerticalAlignment(Alignment align) { mVerticalAlignment = align; }
void setLineSpacing(float spacing);
void setNoTopMargin(bool margin);
void setBackgroundColor(unsigned int color);