Files
Seele/src/Engine/UI/Element/Text.h
T

43 lines
1.4 KiB
C++
Raw Normal View History

2025-01-08 19:15:12 +01:00
#pragma once
#include "UI/Element.h"
#include "UI/Style/Class.h"
namespace Seele {
namespace UI {
2025-01-12 11:26:52 +01:00
template <StyleClass... classes> class Text : public Element {
2025-01-08 19:15:12 +01:00
public:
Text(std::string text) : Element({}, {}), text(text) {}
2025-01-12 11:26:52 +01:00
virtual void applyStyle(Style parentStyle) {
style = parentStyle;
style.outerDisplay = OuterDisplayType::Inline;
(classes::apply(style), ...);
}
virtual void layout(UVector2 parentSize) override {
size_t cursor = 0;
for (uint32 i = 0; i < text.size() - 1; ++i) {
2025-01-19 16:07:38 +01:00
dimensions.x += style.fontFamily->getGlyphData(text[i], style.fontSize).advance / 64.0f;
2025-01-12 11:26:52 +01:00
}
2025-01-19 16:07:38 +01:00
dimensions.x += style.fontFamily->getGlyphData(text[text.size() - 1], style.fontSize).size.x;
2025-01-12 11:26:52 +01:00
dimensions.y = style.fontSize;
}
virtual Array<UIRender> render(Vector2 anchor, uint32 level) override {
return {
UIRender{
.text = text,
.font = style.fontFamily,
.fontSize = style.fontSize,
.position = position + anchor,
.dimensions = dimensions,
2025-01-19 16:07:38 +01:00
.baseline = style.fontSize * 3 / 4, // TODO: improve
2025-01-12 11:26:52 +01:00
.z = style.z,
.level = level,
},
};
}
// height = fontsize * 1.12
2025-01-08 19:15:12 +01:00
private:
std::string text;
};
} // namespace UI
} // namespace Seele