forked from bobsayshilol/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui_element.cpp
More file actions
286 lines (228 loc) · 7.41 KB
/
ui_element.cpp
File metadata and controls
286 lines (228 loc) · 7.41 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "../include/ui_element.h"
#include "../include/engine_sim_application.h"
#include <assert.h>
UiElement::UiElement() {
m_app = nullptr;
m_parent = nullptr;
m_signalTarget = nullptr;
m_checkMouse = false;
m_disabled = false;
m_index = -1;
m_draggable = false;
m_mouseOver = false;
m_mouseHeld = false;
m_visible = true;
}
UiElement::~UiElement() {
/* void */
}
void UiElement::initialize(EngineSimApplication *app) {
m_app = app;
}
void UiElement::destroy() {
/* void */
}
void UiElement::update(float dt) {
for (UiElement *child : m_children) {
child->update(dt);
}
}
void UiElement::render() {
for (UiElement *child : m_children) {
if (child->isVisible()) child->render();
}
}
void UiElement::signal(UiElement *element, Event event) {
/* void */
}
void UiElement::onMouseDown(const Point &mouseLocal) {
m_mouseHeld = true;
}
void UiElement::onMouseUp(const Point &mouseLocal) {
m_mouseHeld = false;
}
void UiElement::onMouseClick(const Point &mouseLocal) {
signal(Event::Clicked);
}
void UiElement::onDrag(const Point &p0, const Point &mouse0, const Point &mouse) {
if (m_draggable) {
m_localPosition = p0 + (mouse - mouse0);
}
}
void UiElement::onMouseOver(const Point &mouseLocal) {
m_mouseOver = true;
}
void UiElement::onMouseLeave() {
m_mouseOver = false;
}
void UiElement::onMouseScroll(int mouseScroll) {
/* void */
}
UiElement *UiElement::mouseOver(const Point &mouseLocal) {
if (m_disabled) return nullptr;
const int n = (int)getChildCount();
for (int i = n - 1; i >= 0; --i) {
UiElement *child = m_children[i];
UiElement *clickedElement = child->mouseOver(mouseLocal - child->m_localPosition);
if (clickedElement != nullptr) {
return clickedElement;
}
}
return (m_checkMouse && m_mouseBounds.overlaps(mouseLocal))
? this
: nullptr;
}
Point UiElement::getWorldPosition() const {
return (m_parent != nullptr)
? m_parent->getWorldPosition() + m_localPosition
: m_localPosition;
}
void UiElement::setLocalPosition(const Point &p, const Point &ref) {
const Point current = m_bounds.getPosition(ref) + m_localPosition;
m_localPosition += (p - current);
}
void UiElement::bringToFront(UiElement *element) {
assert(element->m_parent == this);
m_children.erase(m_children.begin() + element->m_index);
m_children.push_back(element);
int i = 0;
for (UiElement *element : m_children) {
element->m_index = i++;
}
}
void UiElement::activate() {
if (m_parent != nullptr) {
m_parent->bringToFront(this);
m_parent->activate();
}
}
void UiElement::signal(Event event) {
if (m_signalTarget == nullptr) return;
m_signalTarget->signal(this, event);
}
float UiElement::pixelsToUnits(float length) const {
return length;// m_app->pixelsToUnits(length);
}
Point UiElement::pixelsToUnits(const Point &p) const {
return { pixelsToUnits(p.x), pixelsToUnits(p.y) };
}
float UiElement::unitsToPixels(float x) const {
return x; // m_app->unitsToPixels(x);
}
Point UiElement::unitsToPixels(const Point &p) const {
return { unitsToPixels(p.x), unitsToPixels(p.y) };
}
Point UiElement::getRenderPoint(const Point &p) const {
const Point offset(
-(float)m_app->getScreenWidth() / 2,
-(float)m_app->getScreenHeight() / 2);
const Point posPixels = localToWorld(p) + offset;
return pixelsToUnits(posPixels);
}
Bounds UiElement::getRenderBounds(const Bounds &b) const {
return { getRenderPoint(b.m0), getRenderPoint(b.m1) };
}
Bounds UiElement::unitsToPixels(const Bounds &b) const {
return { unitsToPixels(b.m0), unitsToPixels(b.m1) };
}
void UiElement::resetShader() {
m_app->getShaders()->ResetBaseColor();
m_app->getShaders()->SetObjectTransform(ysMath::LoadIdentity());
}
void UiElement::drawModel(
dbasic::ModelAsset *model,
const ysVector &color,
const Point &p,
const Point &s)
{
resetShader();
const Point p_render = getRenderPoint(p);
const Point s_render = pixelsToUnits(s);
m_app->getShaders()->SetObjectTransform(
ysMath::MatMult(
ysMath::TranslationTransform(ysMath::LoadVector(p_render.x, p_render.y, 0.0)),
ysMath::ScaleTransform(ysMath::LoadVector(s_render.x, s_render.y, 0.0))
)
);
m_app->getShaders()->SetBaseColor(color);
m_app->getEngine()->DrawModel(m_app->getShaders()->GetUiFlags(), model, 0x11);
}
void UiElement::drawFrame(
const Bounds &bounds,
float thickness,
const ysVector &frameColor,
const ysVector &fillColor,
bool fill)
{
GeometryGenerator *generator = m_app->getGeometryGenerator();
const Bounds worldBounds = getRenderBounds(bounds);
const Point position = worldBounds.getPosition(Bounds::center);
GeometryGenerator::FrameParameters params;
params.frameWidth = worldBounds.width();
params.frameHeight = worldBounds.height();
params.lineWidth = pixelsToUnits(thickness);
params.x = position.x;
params.y = position.y;
GeometryGenerator::Line2dParameters lineParams;
lineParams.lineWidth = worldBounds.height();
lineParams.y0 = lineParams.y1 = worldBounds.getPosition(Bounds::center).y;
lineParams.x0 = worldBounds.left();
lineParams.x1 = worldBounds.right();
GeometryGenerator::GeometryIndices frame, body;
resetShader();
if (fill) {
generator->startShape();
generator->generateLine2d(lineParams);
generator->endShape(&body);
m_app->getShaders()->SetBaseColor(fillColor);
m_app->drawGenerated(body, 0x11, m_app->getShaders()->GetUiFlags());
}
generator->startShape();
generator->generateFrame(params);
generator->endShape(&frame);
m_app->getShaders()->SetBaseColor(frameColor);
m_app->drawGenerated(frame, 0x11, m_app->getShaders()->GetUiFlags());
}
void UiElement::drawText(
const std::string &s,
const Bounds &bounds,
float height,
const Point &ref)
{
const Bounds renderBounds = unitsToPixels(getRenderBounds(bounds));
const Point origin = renderBounds.getPosition(ref);
m_app->getTextRenderer()->RenderText(
s, origin.x, origin.y - height / 4, height);
}
void UiElement::drawAlignedText(
const std::string &s,
const Bounds &bounds,
float height,
const Point &ref,
const Point &refText)
{
const Bounds renderBounds = unitsToPixels(getRenderBounds(bounds));
const Point origin = renderBounds.getPosition(ref);
const float textWidth = m_app->getTextRenderer()->CalculateWidth(s, height);
const float textHeight = height;
const Bounds textBounds(
textWidth,
textHeight,
{ 0.0f, textHeight - textHeight * 0.25f },
Bounds::tl);
const Point r = textBounds.getPosition(refText);
m_app->getTextRenderer()->RenderText(
s, origin.x - r.x, origin.y - r.y, height);
}
void UiElement::drawCenteredText(
const std::string &s,
const Bounds &bounds,
float height,
const Point &ref)
{
const Bounds renderBounds = unitsToPixels(getRenderBounds(bounds));
const Point origin = renderBounds.getPosition(ref);
const float width = m_app->getTextRenderer()->CalculateWidth(s, height);
m_app->getTextRenderer()->RenderText(
s, origin.x - width / 2, origin.y - height / 4, height);
}