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) 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}; mPosition = glm::vec3{x, y, z};
onPositionChanged(); onPositionChanged();
} }
void GuiComponent::setOrigin(float x, float y) void GuiComponent::setOrigin(float x, float y)
{ {
if (mOrigin.x == x && mOrigin.y == y)
return;
mOrigin = glm::vec2{x, y}; mOrigin = glm::vec2{x, y};
onOriginChanged(); onOriginChanged();
} }
void GuiComponent::setSize(float w, float h) void GuiComponent::setSize(float w, float h)
{ {
if (mSize.x == w && mSize.y == h)
return;
mSize = glm::vec2{w, h}; mSize = glm::vec2{w, h};
onSizeChanged(); onSizeChanged();
} }
@ -166,6 +175,9 @@ int GuiComponent::getChildIndex() const
void GuiComponent::setOpacity(unsigned char opacity) void GuiComponent::setOpacity(unsigned char opacity)
{ {
if (mOpacity == opacity)
return;
mOpacity = opacity; mOpacity = opacity;
for (auto it = mChildren.cbegin(); it != mChildren.cend(); it++) for (auto it = mChildren.cbegin(); it != mChildren.cend(); it++)
(*it)->setOpacity(opacity); (*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++) for (int y = cell.pos.y; y < cell.pos.y + cell.dim.y; y++)
size.y += getRowHeight(y); size.y += getRowHeight(y);
if (cell.resize) if (cell.resize && size != glm::vec2{} && cell.component->getSize() != size)
cell.component->setSize(size); cell.component->setSize(size);
// Find top left corner. // Find top left corner.

View file

@ -156,12 +156,14 @@ public:
e.selected = selected; e.selected = selected;
mEntries.push_back(e); mEntries.push_back(e);
onSelectedChanged();
if (selected)
onSelectedChanged();
} }
bool selectEntry(unsigned int entry) bool selectEntry(unsigned int entry)
{ {
if (entry > mEntries.size()) { if (mEntries.empty() || entry > mEntries.size()) {
return false; return false;
} }
else { else {
@ -258,6 +260,10 @@ private:
mParent->onSizeChanged(); mParent->onSizeChanged();
} }
else { 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. // Display the selected entry and left/right option arrows.
for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++) { for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++) {
if (it->selected) { if (it->selected) {

View file

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

View file

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