Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/gui/gui2_label.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "gui2_label.h"
#include "theme.h"


GuiLabel::GuiLabel(GuiContainer* owner, string id, string text, float text_size)
: GuiElement(owner, id), text(text), text_size(text_size), text_color(glm::u8vec4{255,255,255,255}), text_alignment(sp::Alignment::Center), background(false), bold(false), vertical(false)
: GuiElement(owner, id), text(text), text_size(text_size), text_color(glm::u8vec4{255,255,255,255}), text_alignment(sp::Alignment::Center), background(false), font_flag(0)
{
front_style = theme->getStyle("label.front");
back_style = theme->getStyle("label.back");
Expand All @@ -14,12 +13,9 @@ void GuiLabel::onDraw(sp::RenderTarget& renderer)
const auto& back = back_style->get(getState());
const auto& front = front_style->get(getState());

if (background)
renderer.drawStretched(rect, back.texture, back.color);
if (vertical)
renderer.drawText(rect, text, text_alignment, text_size, front.font, front.color, sp::Font::FlagVertical);
else
renderer.drawText(rect, text, text_alignment, text_size, front.font, front.color);
if (background) renderer.drawStretched(rect, back.texture, back.color);

renderer.drawText(rect, text, text_alignment, text_size, front.font, front.color, font_flag);
}

GuiLabel* GuiLabel::setText(string text)
Expand Down Expand Up @@ -47,42 +43,45 @@ GuiLabel* GuiLabel::addBackground()

GuiLabel* GuiLabel::setVertical()
{
vertical = true;
font_flag |= sp::Font::FlagVertical;
return this;
}

GuiAutoSizeLabel::GuiAutoSizeLabel(GuiContainer* owner, string id, string text, glm::vec2 min_size, glm::vec2 max_size, float min_text_size, float max_text_size)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@oznogon oznogon Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sp::Font::FlagVertical flag is implemented in GuiLabel as a Boolean value defaulting to false at the end of the constructor. If I change the wrap_text GuiAutoSizeLabel parameter to a flag value, should I'll refactor GuiLabel and its invocations to use the flag instead of a Boolean.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the issue isn't storing a boolean in the object. It's the boolean parameter.

Reading: GuiAutoSizeLabel(parent, "ID", "Test", {100, 100}, {300, 300}, true) doesn't make it obvious what the true means (well, it's also not that obvious for the sizes... :P )

: GuiLabel(owner, id, text, max_text_size), min_size(min_size), max_size(max_size), min_text_size(min_text_size), max_text_size(max_text_size)
GuiLabel* GuiLabel::setWrapped()
{
font_flag |= sp::Font::FlagLineWrap;
return this;
}

void GuiAutoSizeLabel::onDraw(sp::RenderTarget& renderer)
GuiLabel* GuiLabel::setClipped()
{
font_flag |= sp::Font::FlagClip;
return this;
}

GuiAutoSizeLabel::GuiAutoSizeLabel(GuiContainer* owner, string id, string text, glm::vec2 min_size, glm::vec2 max_size, float min_text_size, float max_text_size)
: GuiLabel(owner, id, text, max_text_size), min_size(min_size), max_size(max_size), min_text_size(min_text_size), max_text_size(max_text_size)
{
const auto& back = back_style->get(getState());
const auto& front = front_style->get(getState());

if (background)
renderer.drawStretched(rect, back.texture, back.color);
renderer.drawText(rect, text, text_alignment, text_size, front.font, front.color, sp::Font::FlagLineWrap);
}

void GuiAutoSizeLabel::onUpdate()
{
auto font = front_style->get(getState()).font;
text_size = max_text_size;
glm::vec2 size;
while(true) {

while (true)
{
size = min_size;
auto pfs = font->prepare(text, 32, text_size, {255, 255, 255, 255}, size, text_alignment, sp::Font::FlagLineWrap);
auto pfs = font->prepare(text, 32, text_size, {255, 255, 255, 255}, size, text_alignment, font_flag);
size = pfs.getUsedAreaSize();
size.x = std::max(size.x, min_size.x);
size.y = std::max(size.y, min_size.y);
if (size.x <= max_size.x && size.y <= max_size.y)
break;
if (size.x <= max_size.x && size.y <= max_size.y) break;
text_size -= 1.0f;
if (text_size < min_text_size)
break;
if (text_size < min_text_size) break;
}

text_size = std::max(text_size, min_text_size);
setSize(size);
}
12 changes: 4 additions & 8 deletions src/gui/gui2_label.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef GUI2_LABEL_H
#define GUI2_LABEL_H
#pragma once

#include "gui2_element.h"


class GuiThemeStyle;
class GuiLabel : public GuiElement
{
Expand All @@ -13,8 +11,7 @@ class GuiLabel : public GuiElement
glm::u8vec4 text_color{255,255,255,255};
sp::Alignment text_alignment;
bool background;
bool bold;
bool vertical;
int font_flag;
const GuiThemeStyle* front_style;
const GuiThemeStyle* back_style;
public:
Expand All @@ -27,6 +24,8 @@ class GuiLabel : public GuiElement
GuiLabel* setAlignment(sp::Alignment alignment);
GuiLabel* addBackground();
GuiLabel* setVertical();
GuiLabel* setWrapped();
GuiLabel* setClipped();
};

class GuiAutoSizeLabel : public GuiLabel
Expand All @@ -39,8 +38,5 @@ class GuiAutoSizeLabel : public GuiLabel
public:
GuiAutoSizeLabel(GuiContainer* owner, string id, string text, glm::vec2 min_size, glm::vec2 max_size, float min_text_size, float max_text_size);

virtual void onDraw(sp::RenderTarget& renderer) override;
virtual void onUpdate() override;
};

#endif//GUI2_LABEL_H
Loading