-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.cpp
More file actions
512 lines (412 loc) · 18.3 KB
/
ui.cpp
File metadata and controls
512 lines (412 loc) · 18.3 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include "ui.h"
#include <GL/glew.h>
#include "rendering.h"
#include "events_state.h"
#include "input.h"
const FVector2 RenderComponent2::UNIT_UPPER_RIGHT = FVector2(1.0F, 1.0F);
const FVector2 RenderComponent2::UNIT_UPPER_LEFT = FVector2(-1.0F, 1.0F);
const FVector2 RenderComponent2::UNIT_LOWER_LEFT = FVector2(-1.0F, -1.0F);
const FVector2 RenderComponent2::UNIT_LOWER_RIGHT = FVector2(1.0F, -1.0F);
FVector2* Internal::Storage::GameObjects2::batchVertices = nullptr;
FVector2* Internal::Storage::GameObjects2::batchTexCoords = nullptr;
FColor* Internal::Storage::GameObjects2::batchColors = nullptr;
Mesh Internal::Storage::GameObjects2::batchMesh;
unsigned short Internal::Storage::GameObjects2::batchCapacity = 0;
Texture2DQuadAtlas Internal::Storage::GameObjects2::texture;
UIComponent* Internal::Storage::GameObjects2::underMouse = nullptr;
UIComponent* Internal::Storage::GameObjects2::selected = nullptr;
FMatrix3x3 Internal::Storage::GameObjects2::activeCameraMatrix = FMatrix3x3(1.0F);
Shader Internal::Storage::GameObjects2::shader;
Shader Internal::Storage::GameObjects2::independentShader;
SpecialState::SpecialState() { }
void SpecialState::CPUStart() { }
void SpecialState::CPUStop() { }
void SpecialState::GPUStart() { }
void SpecialState::GPUStop() { }
namespace {
inline FMatrix3x3 ConstructCameraMatrix(const Transform2& transform) {
FMatrix3x3 matrix(1.0F);
FMatrix3x3 temp(1.0F);
temp[0][0] = transform.GetScale().x;
temp[1][1] = transform.GetScale().y;
matrix *= temp;
temp = FLOAT_MAT3_IDENTITY;
temp[2][0] = -transform.GetPosition().x;
temp[2][1] = -transform.GetPosition().y;
matrix *= temp;
return matrix;
}
}
Camera2::Camera2() { }
void Camera2::CPUStart() {
Internal::Storage::GameObjects2::activeCameraMatrix = ConstructCameraMatrix(gameObject->transform);
}
void Camera2::CPUStop() {
Internal::Storage::GameObjects2::activeCameraMatrix = FMatrix3x3(1.0F);
}
void Camera2::GPUStart() {
Shader::LoadMatrix3x3(0, ConstructCameraMatrix(gameObject->transform));
}
void Camera2::GPUStop() {
Shader::LoadMatrix3x3(0, FMatrix3x3(1.0F));
}
ScissorState::ScissorState() :
x(0),
y(0),
width(EventsState::windowWidth),
height(EventsState::windowHeight)
{ }
ScissorState::ScissorState(GLint x, GLint y, GLsizei width, GLsizei height) :
x(x),
y(y),
width(width),
height(height) { }
void ScissorState::GPUStart() {
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, width, height);
}
void ScissorState::GPUStop() {
glDisable(GL_SCISSOR_TEST);
}
ScissorStateComponent::ScissorStateComponent() { }
void ScissorStateComponent::GPUStart() {
FVector3 llCorner = gameObject->transform.GetMatrix() * FVector3(-1.0F, -1.0F, 1.0F);
FVector3 urCorner = gameObject->transform.GetMatrix() * FVector3(1.0F, 1.0F, 1.0F);
int x0 = static_cast<int>((llCorner.x + 1.0F) * 0.5F * EventsState::windowWidth);
int y0 = static_cast<int>((llCorner.y + 1.0F) * 0.5F * EventsState::windowHeight);
int x1 = static_cast<int>((urCorner.x + 1.0F) * 0.5F * EventsState::windowWidth);
int y1 = static_cast<int>((urCorner.y + 1.0F) * 0.5F * EventsState::windowHeight);
glEnable(GL_SCISSOR_TEST);
glScissor(x0, y0, x1 - x0, y1 - y0);
}
void ScissorStateComponent::GPUStop() {
glDisable(GL_SCISSOR_TEST);
}
namespace {
// TODO: use general common version!
struct ListRequest {
UIComponent* object;
bool isAdd;
ListRequest(UIComponent* object, bool isAdd) {
this->object = object;
this->isAdd = isAdd;
}
};
std::list<UIComponent*> activeUI;
std::list<ListRequest> activeUIRequests;
std::list<RenderComponent2*> rendering;
std::list<IndependentRenderComponent2*> independentRendering;
}
RenderComponent2::RenderComponent2(const std::string& textureName, const FColor& color, unsigned short quadCount) :
color(color),
quadCount(quadCount),
specialState(nullptr) {
SetTexture(textureName);
}
RenderComponent2::RenderComponent2(const RenderComponent2& other) :
atlasTexInfo(other.atlasTexInfo),
color(other.color),
quadCount(other.quadCount),
specialState(other.specialState) { }
RenderComponent2* RenderComponent2::WithSpecialState(SpecialState* specialState) {
this->specialState = specialState;
return this;
}
void RenderComponent2::SetTexture(const std::string& textureName) {
atlasTexInfo = Internal::Storage::GameObjects2::texture.GetTexInfo(textureName);
}
void RenderComponent2::LoadData(FVector2* vertices, FVector2* texCoords, FColor* colors) {
const FMatrix3x3& matrix = gameObject->transform.GetMatrix();
vertices[0] = Transform(UNIT_UPPER_RIGHT, matrix);
vertices[1] = Transform(UNIT_UPPER_LEFT, matrix);
vertices[2] = Transform(UNIT_LOWER_LEFT, matrix);
vertices[3] = Transform(UNIT_LOWER_RIGHT, matrix);
FVector4 compositeTexInfo(
atlasTexInfo.x + gameObject->transform.GetTexInfo().x * atlasTexInfo.z,
atlasTexInfo.y + gameObject->transform.GetTexInfo().y * atlasTexInfo.w,
atlasTexInfo.z * gameObject->transform.GetTexInfo().z,
atlasTexInfo.w * gameObject->transform.GetTexInfo().w
);
//std::cerr << glm::to_string(compositeTexInfo) << std::endl;
texCoords[0].x = compositeTexInfo.x + compositeTexInfo.z;
texCoords[0].y = compositeTexInfo.y + compositeTexInfo.w;
texCoords[1].x = compositeTexInfo.x;
texCoords[1].y = compositeTexInfo.y + compositeTexInfo.w;
texCoords[2].x = compositeTexInfo.x;
texCoords[2].y = compositeTexInfo.y;
texCoords[3].x = compositeTexInfo.x + compositeTexInfo.z;
texCoords[3].y = compositeTexInfo.y;
colors[0] = color;
colors[1] = color;
colors[2] = color;
colors[3] = color;
}
void RenderComponent2::OnEnable() {
rendering.push_back(this);
if (specialState == nullptr && gameObject->transform.GetParent() != nullptr && gameObject->transform.GetParent()->GetOwner() != nullptr)
if (RenderComponent2* parentRenderComponent = gameObject->transform.GetParent()->GetOwner()->GetComponent<RenderComponent2>())
specialState = parentRenderComponent->specialState;
}
void RenderComponent2::OnDisable() {
rendering.remove(this);
}
IndependentRenderComponent2::IndependentRenderComponent2(const Texture2D& texture, const FColor& color) :
texture(texture),
color(color) { }
IndependentRenderComponent2::IndependentRenderComponent2(const IndependentRenderComponent2& other) :
texture(other.texture),
color(other.color) { }
void IndependentRenderComponent2::OnEnable() {
independentRendering.push_back(this);
}
void IndependentRenderComponent2::OnDisable() {
independentRendering.remove(this);
}
UIComponent::UIComponent() { }
void UIComponent::OnHover(const GameTime& deltaTime) { }
void UIComponent::OnClick(int button) { }
void UIComponent::OnRelease(int button) { }
void UIComponent::OnSelect() { }
void UIComponent::OnDeselect() { }
bool UIComponent::ContainsPoint(float x, float y) {
FVector3 min = gameObject->transform.GetMatrix() * FVector3(-1.0F, -1.0F, 1.0F);
FVector3 max = gameObject->transform.GetMatrix() * FVector3(1.0F, 1.0F, 1.0F);
return x >= min.x && x < max.x && y >= min.y && y < max.y;
}
void UIComponent::OnEnable() {
activeUIRequests.push_back(ListRequest(this, true));
}
void UIComponent::OnDisable() {
activeUIRequests.push_back(ListRequest(this, false));
if (Internal::Storage::GameObjects2::selected == this) {
OnDeselect();
Internal::Storage::GameObjects2::selected = nullptr;
}
}
/*void UINode::Render()
{
texture.Bind(0);
Shader::LoadMatrix3x3(0, transform.GetMatrix());
Shader::LoadVector4(1, transform.GetTexInfo());
Shader::LoadVector4(3, &color.r);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}*/
namespace Internal {
namespace GameObjects2 {
void Init(unsigned int initialAtlasSize, bool pixelArt) {
Storage::GameObjects2::shader = Shader("Internal/Shaders/2d");
Storage::GameObjects2::shader.Bind();
Shader::LoadMatrix3x3(0, FMatrix3x3(1.0F));
Storage::GameObjects2::independentShader = Shader("Internal/Shaders/independent2d");
Storage::GameObjects2::independentShader.Bind();
Storage::GameObjects2::texture = Texture2DQuadAtlas(GL_RGBA, initialAtlasSize, !pixelArt);
/*Storage::FGameObjects2::batchCapacity = initialBatchCapacity;
Storage::FGameObjects2::batchVertices = new FVector2[Storage::FGameObjects2::batchCapacity * 4];
Storage::FGameObjects2::batchTexCoords = new FVector2[Storage::FGameObjects2::batchCapacity * 4];
Storage::FGameObjects2::batchColors = new FColor[Storage::FGameObjects2::batchCapacity * 4];
unsigned short* batchIndices = new unsigned short[Storage::FGameObjects2::batchCapacity * 6];
unsigned int vertexIndex = 0;
for (unsigned int i = 0; i < Storage::FGameObjects2::batchCapacity; i += 6)
{
batchIndices[i] = vertexIndex;
batchIndices[i + 1] = vertexIndex + 1;
batchIndices[i + 2] = vertexIndex + 2;
batchIndices[i + 3] = vertexIndex;
batchIndices[i + 4] = vertexIndex + 2;
batchIndices[i + 5] = vertexIndex + 3;
vertexIndex += 4;
}
Storage::FGameObjects2::batchMesh = Mesh(Mesh::Descriptor(Storage::FGameObjects2::batchCapacity * 4).AddArray(Storage::FGameObjects2::batchVertices, false).AddArray(Storage::FGameObjects2::batchTexCoords, false).AddArray(Storage::FGameObjects2::batchColors, false).AddIndices(batchIndices, Storage::FGameObjects2::batchCapacity * 6));
delete[] batchIndices;*/
}
void Clear() {
if (Storage::GameObjects2::batchVertices == nullptr) return
delete[] Storage::GameObjects2::batchVertices;
delete[] Storage::GameObjects2::batchTexCoords;
delete[] Storage::GameObjects2::batchColors;
}
void Update(const GameTime& time) {
for (ListRequest& request : activeUIRequests) {
if (request.isAdd)
activeUI.push_back(request.object);
else
activeUI.remove(request.object);
}
activeUIRequests.clear();
Internal::Storage::GameObjects2::underMouse = nullptr;
// Need to bind special state
float mx = ((EventsState::mouseX / static_cast<float>(EventsState::windowWidth)) * 2.0F - 1.0F);
float my = ((1.0F - EventsState::mouseY / static_cast<float>(EventsState::windowHeight)) * 2.0F - 1.0F);
float localizedMX = mx;
float localizedMY = my;
SpecialState* activeSpecialState = nullptr;
for (auto it = activeUI.rbegin(); it != activeUI.rend(); ++it) {
UIComponent& node = **it;
if (RenderComponent2* renderComponent = node.gameObject->GetComponent<RenderComponent2>())
if (renderComponent->specialState != activeSpecialState) {
if (activeSpecialState != nullptr)
activeSpecialState->CPUStop();
activeSpecialState = renderComponent->specialState;
if (activeSpecialState != nullptr)
activeSpecialState->CPUStart();
FMatrix3x3 inverseCameraMatrix = glm::inverse(Internal::Storage::GameObjects2::activeCameraMatrix);
FVector3 localizedMouseCoords = inverseCameraMatrix * FVector3(mx, my, 1.0F);
localizedMX = localizedMouseCoords.x;
localizedMY = localizedMouseCoords.y;
}
if (node.ContainsPoint(localizedMX, localizedMY)) {
node.OnHover(time);
if (Input::WasButtonPressed(Input::LEFT_MOUSE_BUTTON))
node.OnClick(Input::LEFT_MOUSE_BUTTON);
if (Input::WasButtonPressed(Input::MIDDLE_MOUSE_BUTTON))
node.OnClick(Input::MIDDLE_MOUSE_BUTTON);
if (Input::WasButtonPressed(Input::RIGHT_MOUSE_BUTTON))
node.OnClick(Input::RIGHT_MOUSE_BUTTON);
if (Input::WasButtonReleased(Input::LEFT_MOUSE_BUTTON))
node.OnRelease(Input::LEFT_MOUSE_BUTTON);
if (Input::WasButtonReleased(Input::MIDDLE_MOUSE_BUTTON))
node.OnRelease(Input::MIDDLE_MOUSE_BUTTON);
if (Input::WasButtonReleased(Input::RIGHT_MOUSE_BUTTON))
node.OnRelease(Input::RIGHT_MOUSE_BUTTON);
if (Internal::Storage::GameObjects2::selected != &node && (Input::WasButtonPressed(Input::LEFT_MOUSE_BUTTON) || Input::WasButtonPressed(Input::MIDDLE_MOUSE_BUTTON) || Input::WasButtonPressed(Input::RIGHT_MOUSE_BUTTON))) {
if (Internal::Storage::GameObjects2::selected != nullptr)
Internal::Storage::GameObjects2::selected->OnDeselect();
Internal::Storage::GameObjects2::selected = &node;
Internal::Storage::GameObjects2::selected->OnSelect();
}
Internal::Storage::GameObjects2::underMouse = *it;
return;
}
}
if (Input::WasButtonPressed(Input::LEFT_MOUSE_BUTTON) || Input::WasButtonPressed(Input::MIDDLE_MOUSE_BUTTON) || Input::WasButtonPressed(Input::RIGHT_MOUSE_BUTTON)) {
if (Internal::Storage::GameObjects2::selected != nullptr)
Internal::Storage::GameObjects2::selected->OnDeselect();
Internal::Storage::GameObjects2::selected = nullptr;
}
}
void Render() {
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Storage::GameObjects2::independentShader.Bind();
Storage::Rendering::quadMesh.Bind();
for (IndependentRenderComponent2* component : independentRendering) {
component->texture.Bind(0);
Shader::LoadMatrix3x3(0, component->gameObject->transform.GetMatrix());
Shader::LoadVector4(1, &component->color.r);
Storage::Rendering::quadMesh.DrawArrays(GL_TRIANGLE_STRIP);
}
if (rendering.empty())
return;
Storage::GameObjects2::shader.Bind();
auto nextLoadComponent = rendering.begin();
SpecialState* specialState = nullptr;
batch_start:
auto component = nextLoadComponent;
bool isLoading = true;
unsigned short nextLoadIndex = 0;
unsigned short index = 0;
bool needsReturn = false;
while (component != rendering.end()) {
if (isLoading) {
if ((*nextLoadComponent)->specialState != specialState) {
if (nextLoadIndex != 0) {
Internal::Storage::GameObjects2::batchMesh.Bind();
Internal::Storage::GameObjects2::batchMesh.Substitute(0, Storage::GameObjects2::batchVertices, 0, nextLoadIndex * 4);
Internal::Storage::GameObjects2::batchMesh.Substitute(1, Storage::GameObjects2::batchTexCoords, 0, nextLoadIndex * 4);
Internal::Storage::GameObjects2::batchMesh.Substitute(2, Storage::GameObjects2::batchColors, 0, nextLoadIndex * 4);
Internal::Storage::GameObjects2::texture.Bind(0);
Internal::Storage::GameObjects2::batchMesh.SetDrawCount(nextLoadIndex * 6);
Internal::Storage::GameObjects2::batchMesh.DrawElements();
}
if (specialState != nullptr)
specialState->GPUStop();
specialState = (*nextLoadComponent)->specialState;
if (specialState != nullptr)
specialState->GPUStart();
goto batch_start;
}
if ((*nextLoadComponent)->quadCount + nextLoadIndex <= Storage::GameObjects2::batchCapacity) {
(*nextLoadComponent)->LoadData(Storage::GameObjects2::batchVertices + nextLoadIndex * 4, Storage::GameObjects2::batchTexCoords + nextLoadIndex * 4, Storage::GameObjects2::batchColors + nextLoadIndex * 4);
nextLoadIndex += (*nextLoadComponent)->quadCount;
++nextLoadComponent;
} else {
isLoading = false;
}
} else {
if ((*component)->specialState != specialState) {
//std::cerr << "found special state while counting" << std::endl;
needsReturn = true;
goto mesh_build;
}
}
index += (*component)->quadCount;
++component;
}
if (!isLoading) {
mesh_build:
unsigned short oldCapacity = Storage::GameObjects2::batchCapacity;
FVector2* oldVertices = Storage::GameObjects2::batchVertices;
FVector2* oldTexCoords = Storage::GameObjects2::batchTexCoords;
FColor* oldColors = Storage::GameObjects2::batchColors;
Storage::GameObjects2::batchCapacity = index;
Storage::GameObjects2::batchVertices = new FVector2[Storage::GameObjects2::batchCapacity * 4];
Storage::GameObjects2::batchTexCoords = new FVector2[Storage::GameObjects2::batchCapacity * 4];
Storage::GameObjects2::batchColors = new FColor[Storage::GameObjects2::batchCapacity * 4];
if (oldCapacity != 0) {
memcpy(Storage::GameObjects2::batchVertices, oldVertices, oldCapacity * 4 * sizeof(FVector2));
memcpy(Storage::GameObjects2::batchTexCoords, oldTexCoords, oldCapacity * 4 * sizeof(FVector2));
memcpy(Storage::GameObjects2::batchColors, oldColors, oldCapacity * 4 * sizeof(FColor));
}
delete[] oldVertices;
delete[] oldTexCoords;
delete[] oldColors;
while (nextLoadComponent != component) {
(*nextLoadComponent)->LoadData(Storage::GameObjects2::batchVertices + nextLoadIndex * 4, Storage::GameObjects2::batchTexCoords + nextLoadIndex * 4, Storage::GameObjects2::batchColors + nextLoadIndex * 4);
nextLoadIndex += (*nextLoadComponent)->quadCount;
++nextLoadComponent;
}
unsigned int indexCount = Storage::GameObjects2::batchCapacity * 6;
unsigned short* batchIndices = new unsigned short[indexCount];
unsigned int vertexIndex = 0;
for (unsigned int i = 0; i < indexCount; i += 6) {
batchIndices[i ] = vertexIndex + 0;
batchIndices[i + 1] = vertexIndex + 1;
batchIndices[i + 2] = vertexIndex + 2;
batchIndices[i + 3] = vertexIndex + 0;
batchIndices[i + 4] = vertexIndex + 2;
batchIndices[i + 5] = vertexIndex + 3;
vertexIndex += 4;
}
Storage::GameObjects2::batchMesh = Mesh(Mesh::Descriptor(Storage::GameObjects2::batchCapacity * 4).AddArray(Storage::GameObjects2::batchVertices, false).AddArray(Storage::GameObjects2::batchTexCoords, false).AddArray(Storage::GameObjects2::batchColors, false).AddIndices(batchIndices, Storage::GameObjects2::batchCapacity * 6));
delete[] batchIndices;
Internal::Storage::GameObjects2::batchMesh.Bind();
} else {
Internal::Storage::GameObjects2::batchMesh.Bind();
Internal::Storage::GameObjects2::batchMesh.Substitute(0, Storage::GameObjects2::batchVertices, 0, nextLoadIndex * 4);
Internal::Storage::GameObjects2::batchMesh.Substitute(1, Storage::GameObjects2::batchTexCoords, 0, nextLoadIndex * 4);
Internal::Storage::GameObjects2::batchMesh.Substitute(2, Storage::GameObjects2::batchColors, 0, nextLoadIndex * 4);
}
Internal::Storage::GameObjects2::texture.Bind(0);
Internal::Storage::GameObjects2::batchMesh.SetDrawCount(nextLoadIndex * 6);
Internal::Storage::GameObjects2::batchMesh.DrawElements();
if (needsReturn) {
if (specialState != nullptr)
specialState->GPUStop();
specialState = (*nextLoadComponent)->specialState;
if (specialState != nullptr)
specialState->GPUStart();
goto batch_start;
}
}
}
}
MouseReleasingComponent2::MouseReleasingComponent2() { }
MouseReleasingComponent2::MouseReleasingComponent2(MouseReleasingComponent2& other) { }
void MouseReleasingComponent2::OnEnable() {
EventsState::AddCursorFreeLock();
}
void MouseReleasingComponent2::OnDisable() {
EventsState::FreeCursorFreeLock();
}