osb/source/windowing/StarLabelWidget.cpp

102 lines
2.5 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarLabelWidget.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
namespace Star {
LabelWidget::LabelWidget(String text,
Color const& color,
HorizontalAnchor const& hAnchor,
VerticalAnchor const& vAnchor,
Maybe<unsigned> wrapWidth,
Maybe<float> lineSpacing)
: m_hAnchor(hAnchor),
2023-06-20 04:33:09 +00:00
m_vAnchor(vAnchor),
m_wrapWidth(std::move(wrapWidth)) {
2023-06-20 04:33:09 +00:00
auto assets = Root::singleton().assets();
m_style = assets->json("/interface.config:labelTextStyle");
m_style.color = color.toRgba();
if (lineSpacing)
m_style.lineSpacing = *lineSpacing;
setText(std::move(text));
2023-06-20 04:33:09 +00:00
}
String const& LabelWidget::text() const {
return m_text;
}
Maybe<unsigned> LabelWidget::getTextCharLimit() const {
return m_textCharLimit;
}
void LabelWidget::setText(String newText) {
m_text = std::move(newText);
2023-06-20 04:33:09 +00:00
updateTextRegion();
}
void LabelWidget::setFontSize(int fontSize) {
m_style.fontSize = fontSize;
2023-06-20 04:33:09 +00:00
updateTextRegion();
}
void LabelWidget::setFontMode(FontMode fontMode) {
m_style.shadow = fontModeToColor(fontMode).toRgba();
}
2023-06-20 04:33:09 +00:00
void LabelWidget::setColor(Color newColor) {
m_style.color = newColor.toRgba();
2023-06-20 04:33:09 +00:00
}
void LabelWidget::setAnchor(HorizontalAnchor hAnchor, VerticalAnchor vAnchor) {
m_hAnchor = hAnchor;
m_vAnchor = vAnchor;
updateTextRegion();
}
void LabelWidget::setWrapWidth(Maybe<unsigned> wrapWidth) {
m_wrapWidth = std::move(wrapWidth);
2023-06-20 04:33:09 +00:00
updateTextRegion();
}
void LabelWidget::setLineSpacing(Maybe<float> lineSpacing) {
m_style.lineSpacing = lineSpacing.value(DefaultLineSpacing);
2023-06-20 04:33:09 +00:00
updateTextRegion();
}
void LabelWidget::setDirectives(String const& directives) {
m_style.directives = directives;
2023-06-20 04:33:09 +00:00
updateTextRegion();
}
void LabelWidget::setTextCharLimit(Maybe<unsigned> charLimit) {
m_textCharLimit = charLimit;
updateTextRegion();
}
void LabelWidget::setTextStyle(TextStyle const& textStyle) {
m_style = textStyle;
updateTextRegion();
}
2023-06-20 04:33:09 +00:00
RectI LabelWidget::relativeBoundRect() const {
return RectI(m_textRegion).translated(relativePosition());
}
RectI LabelWidget::getScissorRect() const {
return noScissor();
}
void LabelWidget::renderImpl() {
context()->setTextStyle(m_style);
2023-06-20 04:33:09 +00:00
context()->renderInterfaceText(m_text, {Vec2F(screenPosition()), m_hAnchor, m_vAnchor, m_wrapWidth, m_textCharLimit});
}
void LabelWidget::updateTextRegion() {
context()->setTextStyle(m_style);
2023-06-20 04:33:09 +00:00
m_textRegion = RectI(context()->determineInterfaceTextSize(m_text, {Vec2F(), m_hAnchor, m_vAnchor, m_wrapWidth, m_textCharLimit}));
setSize(m_textRegion.size());
}
}