osb/source/windowing/StarLabelWidget.cpp

126 lines
3.3 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_fontMode(FontMode::Normal),
m_color(color),
2023-06-20 04:33:09 +00:00
m_hAnchor(hAnchor),
m_vAnchor(vAnchor),
m_wrapWidth(std::move(wrapWidth)),
m_lineSpacing(std::move(lineSpacing)) {
2023-06-20 04:33:09 +00:00
auto assets = Root::singleton().assets();
auto fontConfig = assets->json("/interface.config:font");
m_fontSize = fontConfig.getInt("baseSize");
m_processingDirectives = fontConfig.getString("defaultDirectives");
2023-06-21 12:29:40 +00:00
m_font = fontConfig.queryString("defaultFont", "");
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_fontSize = fontSize;
updateTextRegion();
}
void LabelWidget::setFontMode(FontMode fontMode) {
m_fontMode = fontMode;
}
2023-06-20 04:33:09 +00:00
void LabelWidget::setColor(Color newColor) {
m_color = std::move(newColor);
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_lineSpacing = std::move(lineSpacing);
2023-06-20 04:33:09 +00:00
updateTextRegion();
}
void LabelWidget::setDirectives(String const& directives) {
m_processingDirectives = directives;
updateTextRegion();
}
void LabelWidget::setTextCharLimit(Maybe<unsigned> charLimit) {
m_textCharLimit = charLimit;
updateTextRegion();
}
RectI LabelWidget::relativeBoundRect() const {
return RectI(m_textRegion).translated(relativePosition());
}
RectI LabelWidget::getScissorRect() const {
return noScissor();
}
void LabelWidget::renderImpl() {
2023-06-21 12:29:40 +00:00
context()->setFont(m_font);
2023-06-20 04:33:09 +00:00
context()->setFontSize(m_fontSize);
context()->setFontMode(m_fontMode);
2023-06-20 04:33:09 +00:00
context()->setFontColor(m_color.toRgba());
context()->setFontProcessingDirectives(m_processingDirectives);
if (m_lineSpacing)
context()->setLineSpacing(*m_lineSpacing);
else
context()->setDefaultLineSpacing();
context()->renderInterfaceText(m_text, {Vec2F(screenPosition()), m_hAnchor, m_vAnchor, m_wrapWidth, m_textCharLimit});
2023-06-21 12:29:40 +00:00
context()->setDefaultFont();
context()->setFontMode(FontMode::Normal);
2023-06-20 04:33:09 +00:00
context()->setFontProcessingDirectives("");
context()->setDefaultLineSpacing();
}
void LabelWidget::updateTextRegion() {
context()->setFontSize(m_fontSize);
context()->setFontColor(m_color.toRgba());
context()->setFontProcessingDirectives(m_processingDirectives);
2023-06-21 12:29:40 +00:00
context()->setFont(m_font);
2023-06-20 04:33:09 +00:00
if (m_lineSpacing)
context()->setLineSpacing(*m_lineSpacing);
else
context()->setDefaultLineSpacing();
m_textRegion = RectI(context()->determineInterfaceTextSize(m_text, {Vec2F(), m_hAnchor, m_vAnchor, m_wrapWidth, m_textCharLimit}));
setSize(m_textRegion.size());
2023-06-21 12:29:40 +00:00
context()->setDefaultFont();
2023-06-20 04:33:09 +00:00
context()->setFontProcessingDirectives("");
context()->setDefaultLineSpacing();
}
}