Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/windowing/StarLabelWidget.cpp
blob: 5159704c4c61a4e0812ef7d0544ec2e2f0e53f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#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_color(color),
    m_hAnchor(hAnchor),
    m_vAnchor(vAnchor),
    m_wrapWidth(move(wrapWidth)),
    m_lineSpacing(move(lineSpacing)) {
  auto assets = Root::singleton().assets();
  auto fontConfig = assets->json("/interface.config:font");
  m_fontSize = fontConfig.getInt("baseSize");
  m_processingDirectives = fontConfig.getString("defaultDirectives");
  setText(move(text));
}

String const& LabelWidget::text() const {
  return m_text;
}

Maybe<unsigned> LabelWidget::getTextCharLimit() const {
  return m_textCharLimit;
}

void LabelWidget::setText(String newText) {
  m_text = move(newText);
  updateTextRegion();
}

void LabelWidget::setFontSize(int fontSize) {
  m_fontSize = fontSize;
  updateTextRegion();
}

void LabelWidget::setColor(Color newColor) {
  m_color = move(newColor);
}

void LabelWidget::setAnchor(HorizontalAnchor hAnchor, VerticalAnchor vAnchor) {
  m_hAnchor = hAnchor;
  m_vAnchor = vAnchor;
  updateTextRegion();
}

void LabelWidget::setWrapWidth(Maybe<unsigned> wrapWidth) {
  m_wrapWidth = move(wrapWidth);
  updateTextRegion();
}

void LabelWidget::setLineSpacing(Maybe<float> lineSpacing) {
  m_lineSpacing = move(lineSpacing);
  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() {
  context()->setFontSize(m_fontSize);
  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});

  context()->setFontProcessingDirectives("");
  context()->setDefaultLineSpacing();
}

void LabelWidget::updateTextRegion() {
  context()->setFontSize(m_fontSize);
  context()->setFontColor(m_color.toRgba());
  context()->setFontProcessingDirectives(m_processingDirectives);

  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());

  context()->setFontProcessingDirectives("");
  context()->setDefaultLineSpacing();
}

}