-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
1691 lines (1353 loc) · 59.5 KB
/
main.cpp
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "build_timestamp.h"
#include "common.h"
#include "core/assets.h"
#include "imgui-extra/imgui_impl.h"
#include "imgui/imgui_internal.h"
#ifndef ICON_FA_COGS
#include "icons_font_awesome.h"
#endif
#ifdef USE_LINE_SHADER
#include "core/frame-buffer.h"
#include "core/shader-program.h"
#endif
#include <SDL.h>
#include <SDL_opengl.h>
#include <set>
#include <cmath>
#include <fstream>
#include <vector>
#include <functional>
#include <unordered_map>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/bind.h>
#else
#define EMSCRIPTEN_KEEPALIVE
#endif
#include "imgui_helpers.h"
// Constants
const float kFontScale = 2.0f;
const float kSizeX0 = 1000.0f;
const float kStepPos = 100.0f;
const float kAnimTime = 0.25f;
const float kWindowFadeTime = 0.25f;
const auto kColorBackground = ImGui::ColorConvertFloat4ToU32({ float(0x0A)/256.0f, float(0x10)/256.0f, float(0x16)/256.0f, 0.50f });
const auto kColorEdge = ImGui::ColorConvertFloat4ToU32({ float(0x1D)/256.0f, float(0xA1)/256.0f, float(0xF2)/256.0f, 0.40f });
const auto kColorEdgeSelected = ImGui::ColorConvertFloat4ToU32({ float(0x1D)/256.0f, float(0xA1)/256.0f, float(0xF2)/256.0f, 0.80f });
const auto kColorNode = ImGui::ColorConvertFloat4ToU32({ float(0x00)/256.0f, float(0xFF)/256.0f, float(0x7D)/256.0f, 0.80f });
const auto kColorNodeSelected = ImGui::ColorConvertFloat4ToU32({ float(0x00)/256.0f, float(0xFF)/256.0f, float(0x7D)/256.0f, 1.00f });
const auto kColorCommand = ImGui::ColorConvertFloat4ToU32({ float(0x1D)/256.0f, float(0xA1)/256.0f, float(0xA2)/256.0f, 0.40f });
const auto kColorCommandSelected = ImGui::ColorConvertFloat4ToU32({ float(0x1D)/256.0f, float(0xA1)/256.0f, float(0xA2)/256.0f, 0.80f });
namespace {
template <typename T>
int sgn(T x) { return x < 0 ? -1 : x > 0 ? 1 : 0; }
bool ScrollWhenDraggingOnVoid(ImVec2 delta, ImGuiMouseButton mouse_button) {
if (ImGui::GetIO().MouseDownDuration[ImGuiMouseButton_Left] == 0.0f) {
delta = { 0.0f, 0.0f, };
}
ImGuiContext& g = *ImGui::GetCurrentContext();
ImGuiWindow* window = g.CurrentWindow;
bool hovered = false;
bool held = false;
bool dragging = false;
ImGuiButtonFlags button_flags = (mouse_button == 0) ? ImGuiButtonFlags_MouseButtonLeft : (mouse_button == 1) ? ImGuiButtonFlags_MouseButtonRight : ImGuiButtonFlags_MouseButtonMiddle;
if (g.HoveredId == 0) // If nothing hovered so far in the frame (not same as IsAnyItemHovered()!)
ImGui::ButtonBehavior(window->Rect(), window->GetID("##scrolldraggingoverlay"), &hovered, &held, button_flags);
if (held && delta.x != 0.0f) {
ImGui::SetScrollX(window, window->Scroll.x + delta.x);
}
if (held && delta.y != 0.0f) {
auto dy = delta.y;
// fix vertical scroll snapping:
if (window->Scroll.y == 0.0f && delta.y > 0.0f && delta.y < 11.0f) dy = 11.0f;
ImGui::SetScrollY(window, window->Scroll.y + dy);
dragging = true;
}
return dragging;
}
}
using NodeId = int64_t;
static std::function<bool()> g_doInit;
static std::function<void(int, int)> g_setWindowSize;
static std::function<void(float, float, float, int)> g_setPinch;
static std::function<bool()> g_mainUpdate;
static std::function<void(const NodeId & , const std::string & , int, int, int, int)> g_addNode;
static std::function<void(const NodeId & , int, int)> g_updateNodePosition;
static std::function<void(const NodeId & , const NodeId & )> g_addEdge;
static std::function<void(const NodeId & )> g_focusNode;
static std::function<std::string()> g_getActionOpenUrl;
static std::function<void()> g_treeChanged;
void mainUpdate(void *) {
g_mainUpdate();
}
// JS interface
extern "C" {
EMSCRIPTEN_KEEPALIVE
int do_init() {
return g_doInit();
}
EMSCRIPTEN_KEEPALIVE
void set_window_size(int sizeX, int sizeY) {
g_setWindowSize(sizeX, sizeY);
}
EMSCRIPTEN_KEEPALIVE
void set_pinch(float x, float y, float scale, int type) {
g_setPinch(x, y, scale, type);
}
EMSCRIPTEN_KEEPALIVE
void tree_changed() {
g_treeChanged();
}
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_BINDINGS(tweet2doom) {
emscripten::function("add_node", emscripten::optional_override(
[](const std::string & id, const std::string & username, int level, int type, int x, int y) {
g_addNode(std::stoll(id), username, level, type, x, y);
}));
emscripten::function("add_edge", emscripten::optional_override(
[](const std::string & src, const std::string & dst) {
g_addEdge(std::stoll(src), std::stoll(dst));
}));
emscripten::function("update_node_position", emscripten::optional_override(
[](const std::string & id, int x, int y) {
g_updateNodePosition(std::stoll(id), x, y);
}));
emscripten::function("focus_node", emscripten::optional_override(
[](const std::string & id) {
if (id.empty()) return;
g_focusNode(std::stoll(id));
}));
emscripten::function("get_action_open_url", emscripten::optional_override(
[]() {
return g_getActionOpenUrl();
}));
}
#endif
// Core
struct Node {
NodeId id;
NodeId parentId;
std::string username;
int level;
int type;
float x;
float y;
};
struct Edge {
NodeId src;
NodeId dst;
};
const auto kZoomMin = 0.1f;
const auto kZoomMax = 1.0f;
const auto kZoomMinLog = std::log(kZoomMin);
const auto kZoomMaxLog = std::log(kZoomMax);
float zoomFromLog(float x) {
return std::exp(kZoomMinLog + x*(kZoomMaxLog - kZoomMinLog));
}
enum class EWindowKind {
None,
Help,
Statistics,
Achievements,
};
enum class EAchievementType {
LevelCompleted,
Speedrun,
};
struct View {
float x;
float y;
float z;
};
struct Animation {
float t0;
float t1;
View v0;
View v1;
int type;
};
struct Rendering {
float T;
ImVec2 wSize;
bool isAnimating;
float scale;
float iscale;
float xmin;
float ymin;
float xmax;
float ymax;
float dx;
float dy;
float idx;
float idy;
float textHScaled;
};
struct Achievement {
NodeId id;
NodeId announcmentId;
EAchievementType type;
std::string desc;
};
std::unordered_map<int64_t, Node> g_nodes;
std::vector<Edge> g_edges;
std::map<NodeId, Achievement> g_achievementsMap;
std::vector<Achievement> g_achievements = {
{ 1451989230201315328, 1452303875990593539, EAchievementType::Speedrun, "E1M2 Best time 0:31", },
{ 1451577865045254145, 1451579974234824708, EAchievementType::Speedrun, "E1M1 Best time 0:10", },
{ 1451135258993250309, 1451216719566024705, EAchievementType::LevelCompleted, "E1M5 Completed", },
{ 1450067719299215360, 1450491942119243776, EAchievementType::LevelCompleted, "E1M4 Completed", },
{ 1449750989595152391, 1450129558255009792, EAchievementType::LevelCompleted, "E1M3 Completed", },
{ 1449678007149416449, 1449767161103323142, EAchievementType::LevelCompleted, "E1M2 Completed", },
{ 1449157956096991234, 1449404776286810113, EAchievementType::Speedrun, "E1M1 Best time 0:11", },
{ 1448867734671040514, 1449042393391673351, EAchievementType::Speedrun, "E1M1 Best time 0:16", },
{ 1447603566877757445, 1447940128735961108, EAchievementType::Speedrun, "E1M1 Best time 0:19", },
{ 1445839902130769921, 1445845544748740612, EAchievementType::LevelCompleted, "E1M1 Completed", },
};
struct State {
// bounding box of the nodes
float bbxmin = 1e10;
float bbxmax = -1e10;
float bbymin = 1e10;
float bbymax = -1e10;
// viewport size at zoom = 1.0f
float sizex0 = kSizeX0;
float sizey0 = 0.0f;
float sceneScale = 1.0;
// pinch
float pinchPosX1 = 0.0f;
float pinchPosY1 = 0.0f;
View viewPinch;
float pinchScale = 1.0f;
View viewCur;
Animation anim;
// used to render the line shader texture
// stores infro on time of rendering
float posEdgesX = 0.0f;
float posEdgesY = 0.0f;
float scaleEdges = 0.0f;
float aspectRatio = 1.0f;
NodeId rootId;
NodeId focusId;
NodeId selectedId;
int nUpdates = 2;
bool isMoving = false;
bool wasMoving = true;
bool isZooming = false;
bool wasZooming = true;
bool treeChanged = false;
bool isFirstChange = true;
bool isMouseDown = false;
bool isPinching = false;
bool isPanning = false;
bool isPopupOpen = false;
bool doSelect = false;
float mouseDownX = 0.0f;
float mouseDownY = 0.0f;
float posDownX = 0.0f;
float posDownY = 0.0f;
float heightControls = 0.0f;
// stats
int statsNumNodesRendered = 0;
int statsNumCommandsRendered = 0;
int statsNumEdgesRendered = 0;
int statsNumUniquePlayers = 0;
// popup
ImVec2 popupPos;
ImVec2 popupSize;
float popupShowT0 = 0.0f;
// rendering
bool forceRender = false;
int nSkipUpdate = 0;
Rendering rendering;
// windows
bool windowShow = false;
float windowShowT0 = 0.0f;
EWindowKind windowKind = EWindowKind::None;
// rendering options
float renderingEdgesMinZ = 0.0f;
// open action
std::string actionOpenUrl = "";
::ImVid::Assets assets;
#ifdef USE_LINE_SHADER
::ImVid::FrameBuffer fboEdges;
::ImVid::ShaderProgram shaderEdges;
#endif
void initRendering() {
rendering.T = ImGui::GetTime();
rendering.wSize = ImGui::GetContentRegionAvail();
rendering.isAnimating = rendering.T < anim.t1;
rendering.scale = getScale(viewCur.z);
rendering.iscale = (1.0/rendering.scale)/ImGui::GetIO().DisplayFramebufferScale.x;
rendering.xmin = viewCur.x - 0.5*sizex0*rendering.scale;
rendering.ymin = viewCur.y - 0.5*sizey0*rendering.scale;
rendering.xmax = viewCur.x + 0.5*sizex0*rendering.scale;
rendering.ymax = viewCur.y + 0.5*sizey0*rendering.scale;
rendering.dx = (rendering.xmax - rendering.xmin);
rendering.dy = (rendering.ymax - rendering.ymin);
rendering.idx = 1.0f/rendering.dx;
rendering.idy = 1.0f/rendering.dy;
}
inline float getScale(float z) const {
return 0.5 + (1.0 - zoomFromLog(z))*sceneScale;
}
void onWindowResize() {
sceneScale = std::max(
1.1*(((bbxmax - bbxmin) / sizex0 - 1.0) / 0.9),
1.1*(((bbymax - bbymin) / sizey0 - 1.0) / 0.9));
}
inline bool isMouseInMainCanvas() const {
return (ImGui::GetIO().MousePos.y > 0.625f*heightControls &&
ImGui::GetIO().MousePos.y < ImGui::GetIO().DisplaySize.y - 1.25f*heightControls);
}
inline ImVec2 getRenderPosition(const Node & node) const {
return ImVec2{
(node.x - rendering.xmin)*rendering.idx*rendering.wSize.x,
(node.y - rendering.ymin)*rendering.idy*rendering.wSize.y,
};
}
inline float getRenderRadius(const Node & node) const {
return std::max(0.5f, (node.type == 0 ? 92.0f : 32.0f)*rendering.iscale);
}
inline auto getRenderCommand(const Node & node, const ImVec2 & pos) const {
const ImVec2 tSize = { ImGui::CalcTextSize(node.username.c_str()).x, rendering.textHScaled };
const ImVec2 tMargin = { 12.0f*rendering.iscale, 8.0f*rendering.iscale, };
const ImVec2 pt = { pos.x - 0.5f*tSize.x, pos.y - 0.5f*tSize.y, };
const ImVec2 p0 = { pos.x - 0.5f*tSize.x - tMargin.x, pos.y - 0.5f*tSize.y - tMargin.y, };
const ImVec2 p1 = { pos.x + 0.5f*tSize.x + tMargin.x, pos.y + 0.5f*tSize.y + tMargin.y, };
return std::tuple { pt, p0, p1 };
}
inline void focusNode(const NodeId & id, bool zoomOut) {
focusId = id;
selectedId = id;
anim.v1.x = g_nodes[id].x;
anim.v1.y = g_nodes[id].y;
anim.v1.z = 0.999f;
anim.t0 = rendering.T + 0.0f;
anim.t1 = rendering.T + 3.0f;
anim.v0 = viewCur;
anim.type = zoomOut ? 4 : 3;
}
};
State g_state;
void loadData() {
const std::string kPath = "../data/";
printf("Loading data from '%s'\n", kPath.c_str());
{
int n = 0;
const auto fname = kPath + "nodes.dat";
std::ifstream fin(fname);
while (true) {
Node cur;
fin >> cur.id >> cur.username >> cur.level >> cur.type;
if (fin.eof()) break;
g_addNode(cur.id, cur.username.c_str(), cur.level, cur.type, 0, 0);
++n;
}
printf("Loaded %d entries from '%s'\n", n, fname.c_str());
fin.close();
}
{
int n = 0;
const auto fname = kPath + "coordinates.dat";
std::ifstream fin(fname);
while (true) {
Node cur;
fin >> cur.id >> cur.x >> cur.y;
if (fin.eof()) break;
g_updateNodePosition(cur.id, cur.x, cur.y);
++n;
}
printf("Loaded %d entries from '%s'\n", n, fname.c_str());
fin.close();
}
{
int n = 0;
const auto fname = kPath + "edges.dat";
std::ifstream fin(fname);
while (true) {
Edge cur;
fin >> cur.src >> cur.dst;
if (fin.eof()) break;
g_addEdge(cur.src, cur.dst);
++n;
}
printf("Loaded %d entries from '%s'\n", n, fname.c_str());
fin.close();
}
g_treeChanged();
}
void initMain() {
}
void renderMain() {
ImGui::NewFrame();
static bool isFirstFrame = true;
if (isFirstFrame) {
ImGui_SetStyle();
ImGuiStyle & style = ImGui::GetStyle();
style.Colors[ImGuiCol_Border] = ImGui::ColorConvertU32ToFloat4(kColorNode);
isFirstFrame = false;
}
auto & style = ImGui::GetStyle();
const auto saveWindowPadding = style.WindowPadding;
const auto saveWindowBorderSize = style.WindowBorderSize;
style.WindowPadding.x = 0.0f;
style.WindowPadding.y = 0.0f;
style.WindowBorderSize = 0.0f;
ImGui::SetNextWindowPos({ 0.0f, 0.0f });
ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
ImGui::Begin("main", NULL,
ImGuiWindowFlags_NoNav |
ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoBackground);
style.WindowPadding = saveWindowPadding;
style.WindowBorderSize = saveWindowBorderSize;
auto drawList = ImGui::GetWindowDrawList();
g_state.initRendering();
const auto & T = g_state.rendering.T;
const auto & wSize = g_state.rendering.wSize;
const auto & isAnimating = g_state.rendering.isAnimating;
const auto & scale = g_state.rendering.scale;
const auto & iscale = g_state.rendering.iscale;
g_state.statsNumEdgesRendered = 0;
g_state.statsNumNodesRendered = 0;
g_state.statsNumCommandsRendered = 0;
// background
drawList->AddRectFilled({ 0.0f, 0.0f }, wSize, kColorBackground);
if (g_state.viewCur.z >= g_state.renderingEdgesMinZ) {
#ifdef USE_LINE_SHADER
// shader-based line rendering
const float f = scale/g_state.scaleEdges;
const float ds = 0.5*(1.0 - f);
const float dx = f*(g_state.viewCur.x - g_state.posEdgesX)*g_state.rendering.idx;
const float dy = f*(g_state.viewCur.y - g_state.posEdgesY)*g_state.rendering.idy;
const float x0 = ds + dx;
const float x1 = 1.0f - ds + dx;
const float y0 = ds + dy;
const float y1 = 1.0f - ds + dy;
const float px0 = 0.25 + 0.25*(x0 + x1) - 0.25*(x1 - x0);
const float px1 = 0.25 + 0.25*(x0 + x1) + 0.25*(x1 - x0);
const float py0 = 0.25 + 0.25*(y0 + y1) - 0.25*(y1 - y0);
const float py1 = 0.25 + 0.25*(y0 + y1) + 0.25*(y1 - y0);
ImGui::SetCursorScreenPos({ 0.0f, 0.0f, });
ImGui::Image((void *)(intptr_t) g_state.fboEdges.getIdTex(), ImGui::GetContentRegionAvail(), { px0, py0, }, { px1, py1, });
#else
// imgui line rendering
const auto thickness = std::max(0.1, 2.0*iscale);
for (const auto & edge : g_edges) {
const auto col = (edge.src == g_state.selectedId || edge.dst == g_state.selectedId) ? kColorEdgeSelected : kColorEdge;
const auto p0 = g_state.getRenderPosition(g_nodes[edge.src]);
const auto p1 = g_state.getRenderPosition(g_nodes[edge.dst]);
const float cull = 1000.0f;
const float pmaxx = std::max(p0.x, p1.x);
const float pminx = std::min(p0.x, p1.x);
const float pmaxy = std::max(p0.y, p1.y);
const float pminy = std::min(p0.y, p1.y);
if (pmaxx < -cull || pminx > wSize.x + cull ||
pmaxy < -cull || pminy > wSize.y + cull) {
continue;
}
drawList->AddLine(p0, p1, col, thickness);
g_state.statsNumEdgesRendered++;
}
#endif
}
ImGui::SetWindowFontScale(1.0f*iscale/kFontScale);
g_state.rendering.textHScaled = ImGui::CalcTextSize("X").y;
// render nodes
{
if (g_state.viewCur.z > 0.900f) {
drawList->PushTextureID((void *)(intptr_t) g_state.assets.getTexId(::ImVid::Assets::ICON_T2D_SMALL_BLUR));
}
for (const auto & [id, node] : g_nodes) {
if (node.type == 2) continue;
const auto pos = g_state.getRenderPosition(node);
const auto radius = g_state.getRenderRadius(node);
if (pos.x < -2.0*radius || pos.x > wSize.x + 2.0*radius) continue;
if (pos.y < -2.0*radius || pos.y > wSize.y + 2.0*radius) continue;
const auto col = kColorNode;
const ImVec2 h0 = { pos.x - 2.0f*radius, pos.y - 2.0f*radius };
const ImVec2 h1 = { pos.x + 2.0f*radius, pos.y + 2.0f*radius };
if (node.type == 0) {
const float w = (1.8f*radius);
const float h = (3.2f*radius);
ImGui::SetCursorScreenPos({ pos.x - w, pos.y - h, });
ImGui::Image((void *)(intptr_t) g_state.assets.getTexId(::ImVid::Assets::ICON_T2D_BIG), { 2.0f*w, 2.0f*h });
} else {
if (g_state.viewCur.z > 0.900f) {
const float w = (1.0f*radius);
const float h = (1.0f*radius);
ImGui::SetCursorScreenPos({ pos.x - w, pos.y - h, });
if (id == g_state.selectedId) {
ImGui::Image((void *)(intptr_t) g_state.assets.getTexId(::ImVid::Assets::ICON_T2D_SMALL_BLUR), { 2.0f*w, 2.0f*h }, { 0.0f, 0.0f }, { 1.0f, 1.0f }, ImGui::ColorConvertU32ToFloat4(kColorNodeSelected));
} else {
ImGui::Image((void *)(intptr_t) g_state.assets.getTexId(::ImVid::Assets::ICON_T2D_SMALL_BLUR), { 2.0f*w, 2.0f*h }, { 0.0f, 0.0f }, { 1.0f, 1.0f }, ImGui::ColorConvertU32ToFloat4(kColorNode));
}
} else if (g_state.viewCur.z > 0.500f) {
drawList->AddCircleFilled(pos, radius, col);
} else {
drawList->AddRectFilled({ float(pos.x - radius), float(pos.y - radius) }, { float(pos.x + radius), float(pos.y + radius) }, col);
}
}
g_state.statsNumNodesRendered++;
if (g_state.viewCur.z > 0.90 && g_state.isPopupOpen == false) {
if (g_state.isMouseInMainCanvas()) {
if (ImGui::IsMouseHoveringRect(h0, h1, true)) {
if (ImGui::IsMouseReleased(0) && g_state.isPanning == false && isAnimating == false && g_state.windowShow == false) {
ImGui::SetNextWindowPos({ pos.x + 0.05f*wSize.x, pos.y - std::max(200.0f, 0.25f*wSize.y) });
ImGui::OpenPopup("Node");
g_state.selectedId = id;
g_state.popupShowT0 = T;
g_state.nUpdates = kWindowFadeTime/0.016f + 1.0f;
}
}
}
}
if (g_state.doSelect && g_state.focusId == id && isAnimating == false) {
ImGui::SetNextWindowPos({ pos.x + 0.05f*wSize.x, pos.y - std::max(200.0f, 0.25f*wSize.y) });
ImGui::OpenPopup("Node");
g_state.selectedId = id;
g_state.popupShowT0 = T;
g_state.nUpdates = kWindowFadeTime/0.016f + 1.0f;
g_state.doSelect = false;
}
}
if (g_state.viewCur.z > 0.900f) {
drawList->PopTextureID();
}
}
// render commands
for (const auto & [id, node] : g_nodes) {
if (node.type != 2) continue;
const auto pos = g_state.getRenderPosition(node);
const auto radius = g_state.getRenderRadius(node);
if (pos.x < -2.0*radius || pos.x > wSize.x + 2.0*radius) continue;
if (pos.y < -2.0*radius || pos.y > wSize.y + 2.0*radius) continue;
const auto col = (id == g_state.selectedId) ? kColorCommandSelected : kColorCommand;
const auto [ pt, p0, p1 ] = g_state.getRenderCommand(node, pos);
if (g_state.viewCur.z > 0.98) {
drawList->AddRectFilled(p0, p1, col, 8.0);
drawList->AddRect(p0, p1, g_state.selectedId == id ? kColorNodeSelected : kColorNode, 8.0);
if (g_state.viewCur.z > 0.980) {
ImGui::SetCursorScreenPos(pt);
ImGui::Text("%s", node.username.c_str());
}
} else {
drawList->AddRectFilled(p0, p1, col);
}
g_state.statsNumCommandsRendered++;
if (g_state.viewCur.z > 0.90 && g_state.isPopupOpen == false) {
if (g_state.isMouseInMainCanvas()) {
if (ImGui::IsMouseHoveringRect(p0, p1, true)) {
if (ImGui::IsMouseReleased(0) && g_state.isPanning == false && isAnimating == false) {
ImGui::SetNextWindowPos({ pos.x + 0.05f*wSize.x, pos.y - std::max(200.0f, 0.25f*wSize.y) });
ImGui::OpenPopup("Node");
g_state.selectedId = id;
g_state.popupShowT0 = T;
g_state.nUpdates = kWindowFadeTime/0.016f + 1.0f;
}
}
}
}
}
if (g_nodes.find(g_state.selectedId) != g_nodes.end()) {
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts.back());
ImGui::SetWindowFontScale(1.0f/kFontScale);
const float t = (T - g_state.popupShowT0)/kWindowFadeTime;
ImGui::GetStyle().Alpha = std::min(1.0f, t);
if (ImGui::BeginPopup("Node")) {
g_state.popupPos = ImGui::GetWindowPos();
g_state.popupSize = ImGui::GetWindowSize();
if (g_state.isPopupOpen) {
const auto & node = g_nodes[g_state.selectedId];
const auto pos = g_state.getRenderPosition(node);
const auto col4 = ImGui::ColorConvertU32ToFloat4(kColorNode);
const auto col = ImGui::ColorConvertFloat4ToU32(ImVec4(col4.x, col4.y, col4.z, t));
const ImVec2 pp0 = { g_state.popupPos.x , g_state.popupPos.y };
const ImVec2 pp1 = { g_state.popupPos.x + g_state.popupSize.x, g_state.popupPos.y };
const ImVec2 pp2 = { g_state.popupPos.x + g_state.popupSize.x, g_state.popupPos.y + g_state.popupSize.y };
const ImVec2 pp3 = { g_state.popupPos.x , g_state.popupPos.y + g_state.popupSize.y };
//ImVec2 pn0;
//ImVec2 pn1;
//ImVec2 pn2;
//ImVec2 pn3;
//if (node.type == 2) {
// const auto [ pt, p0, p1 ] = g_state.getRenderCommand(node, pos);
// pn0 = { pos.x, p0.y };
// pn1 = { pos.x, p1.y };
//} else {
// const auto radius = g_state.getRenderRadius(node);
// pn0 = { pos.x, pos.y - radius };
// pn1 = { pos.x, pos.y + radius };
//}
drawList->AddLine(pos, pp0, col);
drawList->AddLine(pos, pp1, col);
drawList->AddLine(pos, pp2, col);
drawList->AddLine(pos, pp3, col);
}
g_state.isPopupOpen = true;
const auto & node = g_nodes[g_state.selectedId];
ImGui::Text("Node: %" PRIu64 "", g_state.selectedId);
if (node.type == 1) {
ImGui::Text("Author: %s", g_nodes[node.parentId].username.c_str());
} else if (node.type == 2) {
ImGui::Text("Author: %s", node.username.c_str());
}
//ImGui::Text("Pos: %.0f %.0f", node.x, node.y);
ImGui::Text("Type: %s", node.type == 0 ? "ROOT" : node.type == 1 ? "Node" : "Command");
ImGui::Text("Depth: %d", node.level);
if (g_achievementsMap.find(node.id) != g_achievementsMap.end()) {
const auto & achievement = g_achievementsMap[node.id];
const auto col = ImGui::ColorConvertU32ToFloat4(kColorNodeSelected);
ImGui::Text("%s\n", "");
auto icon = ICON_FA_TROPHY;
switch (achievement.type) {
case EAchievementType::LevelCompleted: { icon = ICON_FA_TROPHY; } break;
case EAchievementType::Speedrun: { icon = ICON_FA_HOURGLASS_HALF; } break;
};
const int nFonts = ImGui::GetIO().Fonts->Fonts.size();
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[nFonts - 2]);
ImGui::TextColored(col, "%s", icon);
ImGui::PopFont();
ImGui::SameLine();
ImGui::TextColored(col, "%s", achievement.desc.c_str());
}
ImGui::Separator();
if (ImGui::Button("Twitter")) {
g_state.actionOpenUrl = "https://twitter.com/tweet2doom/status/" + std::to_string(g_state.selectedId);
}
if (g_achievementsMap.find(node.id) != g_achievementsMap.end()) {
ImGui::SameLine();
if (ImGui::Button("Announcement")) {
const auto & achievement = g_achievementsMap[node.id];
g_state.actionOpenUrl = "https://twitter.com/tweet2doom/status/" + std::to_string(achievement.announcmentId);
}
}
ImGui::SameLine();
if (ImGui::Button("Focus")) {
g_state.focusNode(g_state.selectedId, false);
}
ImGui::EndPopup();
} else {
if (g_state.isPopupOpen) {
g_state.isPopupOpen = false;
g_state.selectedId = 0;
}
}
ImGui::GetStyle().Alpha = 1.0f;
ImGui::PopFont();
}
// render controls
{
ImGui::SetWindowFontScale(1.0f/kFontScale);
const float kGridSize = 1.5f*ImGui::GetTextLineHeightWithSpacing();
const ImVec2 kGridOffset = { 2.0f*style.ItemInnerSpacing.x, 2.0f*style.ItemInnerSpacing.y, };
// help
{
ImGui::SetCursorScreenPos({ kGridOffset.x + 0.0f*(kGridSize + kGridOffset.x), 1.0f*(kGridSize + kGridOffset.y) - kGridSize, });
if (ImGui::Button(ICON_FA_QUESTION, ImVec2 { kGridSize, kGridSize })) {
ImGui::SetNextWindowPos({ kGridOffset.x + 0.0f*(kGridSize + kGridOffset.x), 2.0f*(kGridSize + kGridOffset.y) - kGridSize, });
ImGui::SetNextWindowFocus();
g_state.windowShow = true;
g_state.windowShowT0 = T;
g_state.windowKind = EWindowKind::Help;
g_state.nUpdates = kWindowFadeTime/0.016f + 1.0f;
}
}
// statistics
{
ImGui::SetCursorScreenPos({ kGridOffset.x + 1.0f*(kGridSize + kGridOffset.x), 1.0f*(kGridSize + kGridOffset.y) - kGridSize, });
if (ImGui::Button(ICON_FA_CHART_PIE, ImVec2 { kGridSize, kGridSize })) {
ImGui::SetNextWindowPos({ kGridOffset.x + 0.0f*(kGridSize + kGridOffset.x), 2.0f*(kGridSize + kGridOffset.y) - kGridSize, });
ImGui::SetNextWindowFocus();
g_state.windowShow = true;
g_state.windowShowT0 = T;
g_state.windowKind = EWindowKind::Statistics;
g_state.nUpdates = kWindowFadeTime/0.016f + 1.0f;
}
}
// achievements
{
ImGui::SetCursorScreenPos({ kGridOffset.x + 2.0f*(kGridSize + kGridOffset.x), 1.0f*(kGridSize + kGridOffset.y) - kGridSize, });
if (ImGui::Button(ICON_FA_TROPHY, ImVec2 { kGridSize, kGridSize })) {
ImGui::SetNextWindowPos({ kGridOffset.x + 0.0f*(kGridSize + kGridOffset.x), 2.0f*(kGridSize + kGridOffset.y) - kGridSize, });
ImGui::SetNextWindowFocus();
g_state.windowShow = true;
g_state.windowShowT0 = T;
g_state.windowKind = EWindowKind::Achievements;
g_state.nUpdates = kWindowFadeTime/0.016f + 1.0f;
}
}
// fit scene
{
ImGui::SetCursorScreenPos({ wSize.x - 1.0f*(kGridSize + kGridOffset.x), wSize.y - 2.0f*(kGridSize + kGridOffset.y), });
if (ImGui::Button(ICON_FA_EXPAND, ImVec2 { kGridSize, kGridSize })) {
g_state.anim.t0 = T;
g_state.anim.t1 = T + 3.0f;
g_state.anim.v0 = g_state.viewCur;
g_state.anim.v1.z = 0.1f;
g_state.anim.v1.x = 0.0f;
g_state.anim.v1.y = g_nodes[g_state.rootId].y + 0.45f*g_state.sizex0*(1.0f + (1.0f - g_state.anim.v1.z)*g_state.sceneScale)*g_state.aspectRatio;
g_state.anim.type = 1;
}
}
// zoom out
{
ImGui::SetCursorScreenPos({ wSize.x - 2.0f*(kGridSize + kGridOffset.x), wSize.y - 1.0f*(kGridSize + kGridOffset.y), });
if (ImGui::Button(ICON_FA_SEARCH_MINUS, ImVec2 { kGridSize, kGridSize })) {
}
if (ImGui::IsMouseDown(0) && ImGui::IsItemHovered()) {
g_state.anim.t0 = T - 0.016f;
g_state.anim.t1 = T + kAnimTime;
g_state.anim.v0 = g_state.viewCur;
g_state.anim.v1.z -= 0.05f*(1.0001f - g_state.viewCur.z);
g_state.anim.type = 2;
}
}
// zoom in
{
ImGui::SetCursorScreenPos({ wSize.x - 1.0f*(kGridSize + kGridOffset.x), wSize.y - 1.0f*(kGridSize + kGridOffset.y), });
if (ImGui::Button(ICON_FA_SEARCH_PLUS, ImVec2 { kGridSize, kGridSize })) {
}
if (ImGui::IsMouseDown(0) && ImGui::IsItemHovered()) {
g_state.anim.t0 = T - 0.016f;
g_state.anim.t1 = T + kAnimTime;
g_state.anim.v0 = g_state.viewCur;
g_state.anim.v1.z += 0.05f*(1.0001f - g_state.viewCur.z);
g_state.anim.type = 2;
}
}
// left
{
ImGui::SetCursorScreenPos({ kGridOffset.x + 0.0f*(kGridSize + kGridOffset.x), wSize.y - 1.0f*(kGridSize + kGridOffset.y), });
if (ImGui::Button(ICON_FA_ARROW_LEFT, ImVec2 { kGridSize, kGridSize })) {
}
if (ImGui::IsMouseDown(0) && ImGui::IsItemHovered()) {
g_state.anim.t0 = T - 0.016f;
g_state.anim.t1 = T + kAnimTime;
g_state.anim.v0 = g_state.viewCur;
g_state.anim.v1.x -= 0.2f*kStepPos*scale;
g_state.anim.type = 2;
}
}
// right
{
ImGui::SetCursorScreenPos({ kGridOffset.x + 2.0f*(kGridSize + kGridOffset.x), wSize.y - 1.0f*(kGridSize + kGridOffset.y), });
if (ImGui::Button(ICON_FA_ARROW_RIGHT, ImVec2 { kGridSize, kGridSize })) {
}
if (ImGui::IsMouseDown(0) && ImGui::IsItemHovered()) {
g_state.anim.t0 = T - 0.016f;
g_state.anim.t1 = T + kAnimTime;
g_state.anim.v0 = g_state.viewCur;
g_state.anim.v1.x += 0.2f*kStepPos*scale;
g_state.anim.type = 2;
}
}
// up
{
ImGui::SetCursorScreenPos({ kGridOffset.x + 1.0f*(kGridSize + kGridOffset.x), wSize.y - 2.0f*(kGridSize + kGridOffset.y), });
if (ImGui::Button(ICON_FA_ARROW_UP, ImVec2 { kGridSize, kGridSize })) {
}
if (ImGui::IsMouseDown(0) && ImGui::IsItemHovered()) {
g_state.anim.t0 = T - 0.016f;
g_state.anim.t1 = T + kAnimTime;
g_state.anim.v0 = g_state.viewCur;
g_state.anim.v1.y -= 0.2f*kStepPos*scale;
g_state.anim.type = 2;
}
}
// down
{
ImGui::SetCursorScreenPos({ kGridOffset.x + 1.0f*(kGridSize + kGridOffset.x), wSize.y - 1.0f*(kGridSize + kGridOffset.y), });
if (ImGui::Button(ICON_FA_ARROW_DOWN, ImVec2 { kGridSize, kGridSize })) {
}
if (ImGui::IsMouseDown(0) && ImGui::IsItemHovered()) {
g_state.anim.t0 = T - 0.016f;
g_state.anim.t1 = T + kAnimTime;
g_state.anim.v0 = g_state.viewCur;
g_state.anim.v1.y += 0.2f*kStepPos*scale;
g_state.anim.type = 2;
}
}
g_state.heightControls = 2.0f*(kGridSize + kGridOffset.y);
}
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts.back());
// window : Help
if (g_state.windowShow && g_state.windowKind == EWindowKind::Help) {
const float kIconSize = ImGui::GetTextLineHeightWithSpacing();
ImGui::Begin("What is this?", nullptr,
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_AlwaysAutoResize);
ImGui::GetStyle().Alpha = std::min(1.0f, (T - g_state.windowShowT0)/kWindowFadeTime);
ImGui::SetWindowFontScale(1.0f/kFontScale);
ImGui::PushTextWrapPos(std::min(0.65f*ImGui::GetIO().DisplaySize.x, 400.0f));
ImGui::Text("This is the State Tree Explorer of the @tweet2doom Twitter bot. "
"The tree contains all commands ever tweeted to the bot and the resulting game states (i.e. nodes). "
"\n\n");
{
ImGui::Image((void *)(intptr_t) g_state.assets.getTexId(::ImVid::Assets::ICON_T2D_SMALL_BLUR), { kIconSize, kIconSize, }, { 0.0f, 0.0f }, { 1.0f, 1.0f }, ImGui::ColorConvertU32ToFloat4(kColorNode));
ImGui::SameLine();
ImGui::Text("- Node");
}
{
const auto pos = ImGui::GetCursorScreenPos();
ImGui::GetWindowDrawList()->AddRectFilled(pos, { pos.x + kIconSize, pos.y + kIconSize }, kColorCommand, 4.0f);
ImGui::GetWindowDrawList()->AddRect (pos, { pos.x + kIconSize, pos.y + kIconSize }, kColorNode, 4.0f);
ImGui::Image((void *)(intptr_t) g_state.assets.getTexId(::ImVid::Assets::ICON_T2D_SMALL_BLUR), { kIconSize, kIconSize, }, {}, {}, {}, {});
ImGui::SameLine();