/
lime2d.cpp
2326 lines (2159 loc) · 122 KB
/
lime2d.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
/*
* Lime2d.cpp
* By: Mark Guerra
* Created on: 6/9/2016
* Copyright (c) 2016 Limeoats
*/
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include "lime2d.h"
#include "../libext/imgui.h"
#include "../libext/imgui-SFML.h"
#include "../libext/imgui_internal.h"
/*******************
* Lime2D Editor *
*******************/
l2d::Editor::Editor(bool enabled, sf::RenderWindow* window) :
_windowHasFocus(true),
_showGridLines(true),
_showEntityList(false),
_eraserActive(false),
_tilesetEnabled(false),
_mainHasFocus(true),
_removingShape(false),
_hideShapes(false),
_showConsole(false),
_menuClicks(0),
_lastFrameMousePos(0.0f, 0.0f),
_currentFeature(l2d_internal::Features::Map),
_graphics(new l2d_internal::Graphics(window)),
_level(this->_graphics, "l2dSTART"),
_currentDrawShape(l2d_internal::DrawShapes::None),
_currentMapEditorMode(l2d_internal::MapEditorMode::Object),
_currentEvent(),
_selectedShape(nullptr),
_currentWindowType(l2d_internal::WindowTypes::None)
{
this->_enabled = enabled;
ImGui::SFML::Init(*window);
this->_window = window;
if (!this->_ambientLight.loadFromFile("content/shaders/ambient.frag", sf::Shader::Fragment)) {
return;
}
}
void l2d::Editor::toggle(std::string mapName) {
this->_enabled = !this->_enabled;
this->_level.loadMap(mapName);
this->createGridLines(true);
if (mapName != "l2dSTART") {
this->_currentFeature = l2d_internal::Features::Map;
this->_currentMapEditorMode = l2d_internal::MapEditorMode::Object;
}
}
bool l2d::Editor::isEnabled() {
return this->_enabled;
}
bool l2d::Editor::isMapLoaded() {
return this->_level.getName() != "l2dSTART";
}
std::string l2d::Editor::getLevelName() {
return this->_level.getName();
}
void l2d::Editor::processEvent(sf::Event &event) {
if (this->_enabled) {
ImGui::SFML::ProcessEvent(event);
this->_currentEvent = event;
switch (event.type) {
case sf::Event::GainedFocus:
this->_windowHasFocus = true;
break;
case sf::Event::LostFocus:
this->_windowHasFocus = false;
break;
case sf::Event::KeyReleased:
switch (event.key.code) {
case sf::Keyboard::T:
if (this->_level.isLoaded() &&
this->_currentFeature == l2d_internal::Features::Map &&
this->_currentMapEditorMode == l2d_internal::MapEditorMode::Tile &&
(this->_currentWindowType == l2d_internal::WindowTypes::None ||
this->_currentWindowType == l2d_internal::WindowTypes::TilesetWindow)) {
this->_tilesetEnabled = !this->_tilesetEnabled;
this->_currentWindowType = this->_tilesetEnabled ? l2d_internal::WindowTypes::TilesetWindow
: l2d_internal::WindowTypes::None;
}
break;
case sf::Keyboard::G:
if (this->_level.isLoaded() &&
this->_currentFeature == l2d_internal::Features::Map &&
this->_currentWindowType == l2d_internal::WindowTypes::None) {
this->_showGridLines = !this->_showGridLines;
}
break;
case sf::Keyboard::U:
if (this->_currentWindowType == l2d_internal::WindowTypes::None) {
this->_level.undo();
}
break;
case sf::Keyboard::R:
if (this->_currentWindowType == l2d_internal::WindowTypes::None) {
this->_level.redo();
}
break;
case sf::Keyboard::E:
if (this->_level.isLoaded() &&
this->_currentFeature == l2d_internal::Features::Map &&
this->_currentMapEditorMode == l2d_internal::MapEditorMode::Object &&
(this->_currentWindowType == l2d_internal::WindowTypes::None ||
this->_currentWindowType == l2d_internal::WindowTypes::EntityListWindow)) {
this->_showEntityList = !this->_showEntityList;
this->_currentWindowType = this->_showEntityList
? l2d_internal::WindowTypes::EntityListWindow
: l2d_internal::WindowTypes::None;
}
break;
case sf::Keyboard::M:
if (this->_currentFeature == l2d_internal::Features::Map &&
this->_currentWindowType == l2d_internal::WindowTypes::None) {
this->_currentMapEditorMode =
this->_currentMapEditorMode == l2d_internal::MapEditorMode::Object ?
l2d_internal::MapEditorMode::Tile : l2d_internal::MapEditorMode::Object;
}
break;
default:
break;
}
break;
case sf::Event::MouseWheelScrolled:
if (this->_currentFeature == l2d_internal::Features::Map && this->_level.isLoaded()) {
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) {
this->_graphics.get()->zoom(event.mouseWheelScroll.delta,
{event.mouseWheelScroll.x, event.mouseWheelScroll.y});
}
}
break;
default:
break;
}
}
}
void l2d::Editor::render() {
if (this->_enabled) {
//Return the current mouse position
static auto getMousePos = [&]() {
return this->_window->mapPixelToCoords(sf::Vector2i(
sf::Mouse::getPosition(*this->_window).x +
static_cast<int>(this->_graphics->getView().getViewport().left),
sf::Mouse::getPosition(*this->_window).y +
static_cast<int>(this->_graphics->getView().getViewport().top)));
};
this->_window->clear(sf::Color(30, 30, 30, 255));
//If map editor
if (this->_level.isLoaded() && this->_currentFeature != l2d_internal::Features::Animation) {
this->_ambientLight.setUniform("texture", sf::Shader::CurrentTexture);
this->_ambientLight.setUniform("color", sf::Glsl::Vec3(this->_level.getAmbientColor().r / 255.0f,
this->_level.getAmbientColor().g / 255.0f,
this->_level.getAmbientColor().b / 255.0f));
this->_ambientLight.setUniform("intensity", this->_level.getAmbientIntensity());
this->_level.draw(&this->_ambientLight);
if (this->_showGridLines) {
for (auto &t : this->_gridLines) {
this->_graphics->draw(t.data(), 2, sf::Lines);
}
if (this->_currentDrawShape == l2d_internal::DrawShapes::None &&
this->_currentMapEditorMode == l2d_internal::MapEditorMode::Tile) {
//Get the mouse position and draw a square around the correct grid tile
sf::Vector2f mousePos = getMousePos();
if (mousePos.x >= 0 && mousePos.x <= (this->_level.getSize().x * this->_level.getTileSize().x *
std::stof(
l2d_internal::utils::getConfigValue("tile_scale_x")) -
1) &&
mousePos.y >= 0 && mousePos.y <= (this->_level.getSize().y * this->_level.getTileSize().y *
std::stof(
l2d_internal::utils::getConfigValue("tile_scale_y")) -
1)) {
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(this->_level.getTileSize().x *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_x")) -
1,
this->_level.getTileSize().y *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_y")) -
1));
rectangle.setOutlineColor(this->_eraserActive ? sf::Color::Blue : sf::Color::Magenta);
rectangle.setOutlineThickness(2);
rectangle.setPosition(
std::floor(mousePos.x - ((int) mousePos.x % (int) (this->_level.getTileSize().x *
std::stof(
l2d_internal::utils::getConfigValue(
"tile_scale_x"))))),
std::floor(mousePos.y - ((int) mousePos.y % (int) (this->_level.getTileSize().y *
std::stof(
l2d_internal::utils::getConfigValue(
"tile_scale_y"))))));
rectangle.setFillColor(sf::Color::Transparent);
this->_window->draw(rectangle);
}
}
}
//Draw shapes
if (!this->_hideShapes) {
for (std::shared_ptr<l2d_internal::Shape> shape : this->_level.getShapeList()) {
shape.get()->draw(this->_window);
}
}
}
//Draw the status bar
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(this->_window->getSize().x , 30) / (this->_graphics->getZoomPercentage() / 100.0f));
rectangle.setFillColor(sf::Color::Black);
sf::Vector2f cameraOffset = (this->_graphics->getView().getCenter() - (this->_graphics->getView().getSize() / 2.0f));
rectangle.setPosition(0 + cameraOffset.x, (this->_window->getSize().y / (this->_graphics->getZoomPercentage() / 100.0f)) - (30 / (this->_graphics->getZoomPercentage() / 100.0f)) + cameraOffset.y);
this->_window->draw(rectangle);
//Shape creation
//Rectangles
if (!this->_hideShapes) {
if (this->_currentDrawShape == l2d_internal::DrawShapes::Rectangle &&
this->_currentMapEditorMode == l2d_internal::MapEditorMode::Object) {
static sf::Vector2f mousePos = getMousePos();
static sf::Vector2f startPos = mousePos;
static sf::Vector2f currentPosition;
static sf::RectangleShape rect;
static bool started = false;
if (this->_currentEvent.type == sf::Event::MouseButtonPressed &&
this->_currentEvent.mouseButton.button == sf::Mouse::Left) {
++this->_menuClicks;
if (this->_menuClicks > 1) {
mousePos = this->_window->mapPixelToCoords(sf::Vector2i(
sf::Mouse::getPosition(*this->_window).x +
static_cast<int>(this->_graphics->getView().getViewport().left),
sf::Mouse::getPosition(*this->_window).y +
static_cast<int>(this->_graphics->getView().getViewport().top)));
startPos.x = mousePos.x;
startPos.y = mousePos.y;
}
else {
this->_currentEvent = sf::Event();
}
}
if (this->_currentEvent.type == sf::Event::MouseMoved && sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
if (this->_menuClicks > 1) {
mousePos = getMousePos();
//Check rectangle bounds to make sure it is not drawn outside of the map
mousePos.x = std::max(0.0f, mousePos.x);
mousePos.x = std::min(this->_level.getSize().x *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_x")) *
this->_level.getTileSize().x, mousePos.x);
mousePos.y = std::max(0.0f, mousePos.y);
mousePos.y = std::min(this->_level.getSize().y *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_y")) *
this->_level.getTileSize().y, mousePos.y);
currentPosition.x = mousePos.x - startPos.x;
currentPosition.y = mousePos.y - startPos.y;
rect.setSize(currentPosition);
rect.setPosition(startPos.x, startPos.y);
rect.setFillColor(sf::Color(0, 0, 0, 80));
rect.setOutlineThickness(2.0f);
rect.setOutlineColor(sf::Color(0, 0, 0, 160));
started = true;
this->_window->draw(rect); //Temporarily draw the box while it's being created
}
else {
this->_currentEvent = sf::Event();
}
}
if (this->_currentEvent.type == sf::Event::MouseButtonPressed &&
this->_currentEvent.mouseButton.button == sf::Mouse::Left && started) {
this->_level.addShape(std::make_shared<l2d_internal::Rectangle>("Rectangle", sf::Color::White,
l2d_internal::ObjectTypes::Other,
rect));
this->_currentEvent = sf::Event();
started = false;
this->_menuClicks = 0;
this->_currentDrawShape = l2d_internal::DrawShapes::None; //Stop drawing rectangles and return to select mode
}
}
//Points
if (this->_currentDrawShape == l2d_internal::DrawShapes::Point && this->_currentMapEditorMode == l2d_internal::MapEditorMode::Object) {
static sf::Vector2f mousePos = getMousePos();
static sf::CircleShape dot;
static const float DOT_RADIUS = 6.0f;
if (this->_currentEvent.type == sf::Event::MouseButtonReleased && this->_currentEvent.mouseButton.button == sf::Mouse::Left) {
if (++this->_menuClicks > 1) {
mousePos = getMousePos();
mousePos.x = std::max(0.0f, mousePos.x);
mousePos.x = std::min(this->_level.getSize().x *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_x")) *
this->_level.getTileSize().x, mousePos.x);
mousePos.y = std::max(0.0f, mousePos.y);
mousePos.y = std::min(this->_level.getSize().y *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_y")) *
this->_level.getTileSize().y, mousePos.y);
dot.setRadius(DOT_RADIUS);
dot.setPosition(sf::Vector2f(mousePos.x - DOT_RADIUS, mousePos.y - DOT_RADIUS));
dot.setFillColor(sf::Color(0, 0, 255, 80));
dot.setOutlineColor(sf::Color(0, 0, 255, 160));
dot.setOutlineThickness(2.0f);
this->_level.addShape(std::make_shared<l2d_internal::Point>("Point", sf::Color::Blue, dot));
this->_currentEvent = sf::Event();
this->_currentDrawShape = l2d_internal::DrawShapes::None;
this->_menuClicks = 0;
}
else {
this->_currentEvent = sf::Event();
}
}
}
//Lines
if (this->_currentDrawShape == l2d_internal::DrawShapes::Line && this->_currentMapEditorMode == l2d_internal::MapEditorMode::Object) {
static sf::Vector2f mousePos = getMousePos();
static std::vector<std::shared_ptr<l2d_internal::Point>> points;
//Draw temporary points / line
for (auto &p : points) {
p->draw(this->_window);
}
//Draw connecting lines between the points
if (points.size() >= 2) {
for (unsigned int i = 0; i < points.size() - 1; ++i) {
sf::Vertex v[4];
sf::Vector2f point1 = sf::Vector2f(points[i]->getCircle().getPosition().x + points[i]->getCircle().getRadius(),
points[i]->getCircle().getPosition().y + points[i]->getCircle().getRadius());
sf::Vector2f point2 = sf::Vector2f(points[i + 1]->getCircle().getPosition().x + points[i + 1]->getCircle().getRadius(),
points[i + 1]->getCircle().getPosition().y + points[i + 1]->getCircle().getRadius());
sf::Vector2f direction = point2 - point1;
sf::Vector2f unitDirection = direction/std::sqrt(direction.x*direction.x+direction.y*direction.y);
sf::Vector2f unitPerpendicular(-unitDirection.y,unitDirection.x);
sf::Vector2f offset = (3.0f / 2.0f) * unitPerpendicular;
v[0].position = point1 + offset;
v[1].position = point2 + offset;
v[2].position = point2 - offset;
v[3].position = point1 - offset;
this->_graphics->draw(v, 4, sf::Quads);
}
}
if (this->_currentEvent.type == sf::Event::MouseButtonReleased) {
if (this->_currentEvent.mouseButton.button == sf::Mouse::Left) {
if (++this->_menuClicks > 1) {
mousePos = getMousePos();
mousePos.x = std::max(0.0f, mousePos.x);
mousePos.x = std::min(this->_level.getSize().x *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_x")) *
this->_level.getTileSize().x, mousePos.x);
mousePos.y = std::max(0.0f, mousePos.y);
mousePos.y = std::min(this->_level.getSize().y *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_y")) *
this->_level.getTileSize().y, mousePos.y);
sf::CircleShape c;
c.setRadius(6.0f);
c.setPosition(sf::Vector2f(mousePos.x - 6.0f, mousePos.y - 6.0f));
c.setFillColor(sf::Color(0, 180, 0, 80));
c.setOutlineColor(sf::Color(0, 180, 0, 160));
c.setOutlineThickness(2.0f);
points.push_back(std::make_shared<l2d_internal::Point>("p" + std::to_string((points.size() + 1)), sf::Color(0, 255, 0), c));
this->_currentEvent = sf::Event();
}
else {
this->_currentEvent = sf::Event();
}
}
if (this->_currentEvent.mouseButton.button == sf::Mouse::Right) {
if (points.size() >= 2) {
//Save the line!
this->_level.addShape(std::make_shared<l2d_internal::Line>("Line", sf::Color::White, points));
}
points.clear();
this->_currentEvent = sf::Event();
this->_currentDrawShape = l2d_internal::DrawShapes::None;
this->_menuClicks = 0;
}
}
}
//Shape selection
if (this->_currentEvent.type == sf::Event::MouseButtonPressed &&
this->_currentDrawShape == l2d_internal::DrawShapes::None &&
this->_currentMapEditorMode == l2d_internal::MapEditorMode::Object &&
this->_mainHasFocus) {
//Check the mouse pos and determine if it is inside a shape.
sf::Vector2f mousePos = getMousePos();
bool sel = false;
auto shapes = this->_level.getShapeList();
for (int i = this->_level.getShapeList().size() - 1; i >= 0; --i) {
if (shapes[i]->isPointInside(mousePos)) {
if (shapes[i] == this->_selectedShape) {
this->_lastFrameMousePos = sf::Vector2f(0.0f, 0.0f);
}
//Check if line. If so, use mousePos to figure out which point is selected
//and return it. Use that point for _selectedShape.
std::shared_ptr<l2d_internal::Shape> shape = nullptr;
auto l = std::dynamic_pointer_cast<l2d_internal::Line>(shapes[i]);
if (l != nullptr) {
shape = l->getSelectedPoint(mousePos);
}
else {
shape = shapes[i];
}
shape->select();
sel = true;
this->_selectedShape = shape;
for (auto &t : this->_level.getShapeList()) {
if (shape != t) {
t->unselect();
}
}
//Left click on a shape
if (this->_currentEvent.mouseButton.button == sf::Mouse::Left) {
break;
}
//Right click on a shape
else if (this->_currentEvent.mouseButton.button == sf::Mouse::Right) {
this->_removingShape = true;
break;
}
}
}
//If user clicked but none were selected, unselect all shapes
if (!sel) {
for (auto &t : this->_level.getShapeList()) {
t->unselect();
}
this->_selectedShape = nullptr;
}
}
}
ImGui::Render();
}
}
void l2d::Editor::createGridLines(bool always) {
if (this->_level.isLoaded() && (this->_currentFeature == l2d_internal::Features::Map || always)) {
this->_gridLines.clear();
std::array<sf::Vertex, 2> line;
//Horizontal lines
for (int i = 0; i < this->_level.getSize().y + 1; ++i) {
line = {
sf::Vertex(sf::Vector2f(0, i * (this->_level.getTileSize().y * std::stof(
l2d_internal::utils::getConfigValue("tile_scale_y"))))),
sf::Vertex(sf::Vector2f(this->_level.getSize().x * this->_level.getTileSize().x *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_x")),
i * (this->_level.getTileSize().y *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_y")))))
};
this->_gridLines.push_back(line);
}
//Vertical lines
for (int i = 0; i < this->_level.getSize().x + 1; ++i) {
line = {
sf::Vertex(sf::Vector2f(i * (this->_level.getTileSize().x * std::stof(
l2d_internal::utils::getConfigValue("tile_scale_x"))), 0)),
sf::Vertex(sf::Vector2f(i * this->_level.getTileSize().x *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_x")),
this->_level.getSize().y * (this->_level.getTileSize().y *
std::stof(l2d_internal::utils::getConfigValue("tile_scale_y")))))
};
this->_gridLines.push_back(line);
}
}
}
void l2d::Editor::update(sf::Time t) {
if (this->_enabled) {
ImGui::SFML::Update(*this->_window, t);
/*
* Menu
* File, View, Map, Animation, Help
*/
static bool cbMapEditor = true;
static bool cbAnimationEditor = false;
static bool cbShowGridLines = true;
static bool aboutBoxVisible = false;
static bool mapSelectBoxVisible = false;
static bool configWindowVisible = false;
static bool newMapBoxVisible = false;
static bool newMapExistsOverwriteVisible = false;
static bool tilesetWindowVisible = false;
static bool lightEditorWindowVisible = false;
static bool newAnimatedSpriteWindowVisible = false;
static bool newAnimationWindowVisible = false;
static bool removeAnimationWindowVisible = false;
static bool cbShowEntityList = false;
static bool showEntityProperties = false;
static bool shapeColorWindowVisible = false;
static bool entityPropertiesLoaded = false;
static bool cbHideShapes = false;
static bool configureMapWindowVisible = false;
static bool cbShowConsole = false;
static std::shared_ptr<l2d_internal::Shape> rightClickedShape = nullptr;
static sf::Vector2f mousePos(0.0f, 0.0f);
static std::string currentFeature = "Lime2D";
static std::string currentStatus = "";
static bool showCurrentStatus = false;
static int currentStatusTimer = 0;
static int mapSelectIndex = 0;
static int animationSelectIndex = -1;
static int animationSpriteSelectIndex = -1;
static int spritesheetSelectIndex = -1;
static std::string selectedAnimationFileName = "";
static std::string selectedAnimationName = "";
//Entity list variables
static ImVec4 selectedEntityColor = sf::Color::White;
static int selectedEntitySelectedObjectTypeIndex = -1;
//Rectangle
static std::shared_ptr<l2d_internal::Rectangle> selectedEntityRectangle;
static std::shared_ptr<l2d_internal::Rectangle> originalSelectedEntityRectangle;
//Point
static std::shared_ptr<l2d_internal::Point> selectedEntityPoint;
static std::shared_ptr<l2d_internal::Point> originalSelectedEntityPoint;
//Line
static std::shared_ptr<l2d_internal::Line> selectedEntityLine;
static std::shared_ptr<l2d_internal::Line> originalSelectedEntityLine;
//Custom properties
static std::vector<l2d_internal::CustomProperty> customProperties;
//Drawing tiles variables
static bool tileHasBeenSelected = false;
static std::string selectedTilesetPath = "content/tilesets/outside.png";
static sf::Vector2i selectedTileSrcPos(0,0);
static int selectedTileLayer = 1;
static sf::Vector2i selectedTilesetSize(0,0);
//Light variables
static l2d_internal::LightType selectedLightType = l2d_internal::LightType::None;
//Console variables
static std::vector<l2d_internal::ConsoleItem> consoleItems;
static char consoleInputBuffer[256];
static std::vector<l2d_internal::ConsoleItem> consoleHistory;
static int consoleHistoryPos = -1;
//startStatusTimer function is written like this so that it can exist within the update function
//This way, it can access the static timer variables without making them member variables
static auto startStatusTimer = [&](std::string newStatus, int time) {
currentStatus = newStatus;
currentStatusTimer = time;
showCurrentStatus = true;
};
//FilterLuaKeyInput function is a lambda function because it's only relevant in this function
//Use it on ImGui::TextInputs to avoid bad characters
static auto FilterLuaKeyInput = [](ImGuiTextEditCallbackData* data)->int {
if (data->EventChar == ' ' || data->EventChar == '.' || data->EventChar == '{' || data->EventChar == '}' ||
data->EventChar == '<' || data->EventChar == '>' || data->EventChar == '-' || data->EventChar == ';' ||
data->EventChar == '~' || data->EventChar == '`' || data->EventChar == '!' || data->EventChar == '@' ||
data->EventChar == '#' || data->EventChar == '$' || data->EventChar == '%' || data->EventChar == '^' ||
data->EventChar == '&' || data->EventChar == '*' || data->EventChar == '(' || data->EventChar == ')' ||
data->EventChar == '+' || data->EventChar == '=' || data->EventChar == '\'' || data->EventChar == '\"' ||
data->EventChar == '?' || data->EventChar == '[' || data->EventChar == ']' || data->EventChar == '\\' ||
data->EventChar == '|' || data->EventChar == ',' || data->EventChar == '/')
return 1;
return 0;
};
//Return the current mouse position
static auto getMousePos = [&]() {
return this->_window->mapPixelToCoords(sf::Vector2i(
sf::Mouse::getPosition(*this->_window).x +
static_cast<int>(this->_graphics->getView().getViewport().left),
sf::Mouse::getPosition(*this->_window).y +
static_cast<int>(this->_graphics->getView().getViewport().top)));
};
//Clear all of the selected entity objects
static auto clearSelectedEntityObjects = [&]() {
selectedEntityRectangle = nullptr;
originalSelectedEntityRectangle = nullptr;
selectedEntityPoint = nullptr;
originalSelectedEntityPoint = nullptr;
selectedEntityLine = nullptr;
originalSelectedEntityLine = nullptr;
};
//Set mainHasFocus (very important)
//This tells Lime2D that it can draw tiles to the screen. We don't want it drawing if other windows have focus.
this->_mainHasFocus = this->_currentWindowType == l2d_internal::WindowTypes::None;
cbShowGridLines = this->_showGridLines;
cbShowEntityList = this->_showEntityList;
//Config window
if (configWindowVisible) {
this->_currentWindowType = l2d_internal::WindowTypes::ConfigWindow;
static bool loaded = false;
ImGui::SetNextWindowPosCenter();
ImGui::SetNextWindowSize(ImVec2(480, 380));
static std::string configureMapErrorText = "";
ImGui::Begin("Configure", nullptr, ImVec2(480,380), 100.0f, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ShowBorders);
ImGui::PushID("ConfigureMapPath");
ImGui::Text("Map path");
static char mapPath[500] = "";
if (l2d_internal::utils::getConfigValue("map_path") != "" && !loaded) {
strcpy(mapPath, l2d_internal::utils::getConfigValue("map_path").c_str());
}
ImGui::PushItemWidth(300);
ImGui::InputText("", mapPath, 500);
ImGui::PopItemWidth();
ImGui::Separator();
ImGui::PopID();
ImGui::PushID("ConfigureTilesetPath");
ImGui::Text("Tileset path");
static char tilesetPath[500] = "";
if (l2d_internal::utils::getConfigValue("tileset_path") != "" && !loaded) {
strcpy(tilesetPath, l2d_internal::utils::getConfigValue("tileset_path").c_str());
}
ImGui::PushItemWidth(300);
ImGui::InputText("", tilesetPath, 500);
ImGui::PopItemWidth();
ImGui::Separator();
ImGui::PopID();
ImGui::PushItemWidth(100);
ImGui::PushID("ConfigureSpriteScale");
ImGui::Text("Sprite scale");
static float spriteScaleX = l2d_internal::utils::getConfigValue("sprite_scale_x") == "" ? 1.0f : std::stof(l2d_internal::utils::getConfigValue("sprite_scale_x"));
static float spriteScaleY = l2d_internal::utils::getConfigValue("sprite_scale_y") == "" ? 1.0f : std::stof(l2d_internal::utils::getConfigValue("sprite_scale_y"));
ImGui::InputFloat("x", &spriteScaleX, 0.1f, 0.0f, 2);
ImGui::InputFloat("y", &spriteScaleY, 0.1f, 0.0f, 2);
ImGui::Separator();
ImGui::PopID();
ImGui::PushID("ConfigureTileScale");
ImGui::Text("Tile scale");
static float tileScaleX = l2d_internal::utils::getConfigValue("tile_scale_x") == "" ? 1.0f : std::stof(l2d_internal::utils::getConfigValue("tile_scale_x"));
static float tileScaleY = l2d_internal::utils::getConfigValue("tile_scale_y") == "" ? 1.0f : std::stof(l2d_internal::utils::getConfigValue("tile_scale_y"));
ImGui::InputFloat("x", &tileScaleX, 0.1f, 0.0f, 2);
ImGui::InputFloat("y", &tileScaleY, 0.1f, 0.0f, 2);
ImGui::Separator();
ImGui::PopID();
ImGui::PushID("ConfigureScreenSize");
ImGui::Text("Screen size");
static int screenSizeX = l2d_internal::utils::getConfigValue("screen_size_x") == "" ? 1 : std::stoi(l2d_internal::utils::getConfigValue("screen_size_x"));
static int screenSizeY = l2d_internal::utils::getConfigValue("screen_size_y") == "" ? 1 : std::stoi(l2d_internal::utils::getConfigValue("screen_size_y"));
ImGui::InputInt("x", &screenSizeX, 5);
ImGui::InputInt("y", &screenSizeY, 5);
ImGui::Separator();
ImGui::PopID();
ImGui::PushID("ConfigureSpritesPath");
ImGui::Text("Sprite path");
static char spritePath[500] = "";
if (l2d_internal::utils::getConfigValue("sprite_path") != "" && !loaded) {
strcpy(spritePath, l2d_internal::utils::getConfigValue("sprite_path").c_str());
}
ImGui::PushItemWidth(300);
ImGui::InputText("", spritePath, 500);
ImGui::PopItemWidth();
ImGui::Separator();
ImGui::PopID();
ImGui::PushID("ConfigureAnimationPath");
ImGui::Text("Animation path");
static char animationPath[500] = "";
if (l2d_internal::utils::getConfigValue("animation_path") != "" && !loaded) {
strcpy(animationPath, l2d_internal::utils::getConfigValue("animation_path").c_str());
}
ImGui::PushItemWidth(300);
ImGui::InputText("", animationPath, 500);
ImGui::PopItemWidth();
ImGui::Separator();
ImGui::PopID();
ImGui::PushID("ConfigureCameraPanAmount");
ImGui::Text("Camera pan factor");
static float cameraPanFactor = l2d_internal::utils::getConfigValue("camera_pan_factor") == "" ? 4.0f : std::stof(l2d_internal::utils::getConfigValue("camera_pan_factor"));
ImGui::InputFloat("", &cameraPanFactor, 0.25f, 0.0f, 2);
ImGui::Separator();
ImGui::PopID();
ImGui::PopItemWidth();
if (ImGui::Button("Save")) {
//Lots of error checking on all of the values
//Saving to lime2d.config
if (strlen(mapPath) <= 0) {
configureMapErrorText = "You must enter the location of your maps!";
}
else if (strlen(tilesetPath) <= 0) {
configureMapErrorText = "You must enter the location of your tilesets!";
}
else if (strlen(spritePath) <= 0) {
configureMapErrorText = "You must enter the location of your sprites!";
}
else if (strlen(animationPath) <= 0) {
configureMapErrorText = "You must enter the location of your animations!";
}
else {
configureMapErrorText = "";
//Everything checks out, so save.
std::ofstream os("lime2d.config");
if (os.is_open()) {
os << "map_path=" << mapPath << "\n";
os << "tileset_path=" << tilesetPath << "\n";
os << "sprite_scale_x=" << spriteScaleX << "\n";
os << "sprite_scale_y=" << spriteScaleY << "\n";
os << "tile_scale_x=" << tileScaleX << "\n";
os << "tile_scale_y=" << tileScaleY << "\n";
os << "screen_size_x=" << screenSizeX << "\n";
os << "screen_size_y=" << screenSizeY << "\n";
os << "sprite_path=" << spritePath << "\n";
os << "animation_path=" << animationPath << "\n";
os << "camera_pan_factor=" << cameraPanFactor << "\n";
os.close();
if (this->_level.isLoaded()) {
std::string name = this->_level.getName();
configureMapErrorText = this->_level.loadMap(name);
if (configureMapErrorText.length() <= 0) {
createGridLines();
this->_currentWindowType = l2d_internal::WindowTypes::None;
configWindowVisible = false;
startStatusTimer("Configurations saved successfully!", 200);
}
}
else {
this->_currentWindowType = l2d_internal::WindowTypes::None;
configWindowVisible = false;
startStatusTimer("Configurations saved successfully!", 200);
}
}
else {
configureMapErrorText = "Unable to save file. Please refer to www.limeoats.com/lime2d for more information.";
}
}
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
this->_currentWindowType = l2d_internal::WindowTypes::None;
configWindowVisible = false;
configureMapErrorText = "";
}
ImGui::Text("%s", configureMapErrorText.c_str());
spriteScaleX = std::max(0.0f, spriteScaleX);
spriteScaleY = std::max(0.0f, spriteScaleY);
tileScaleX = std::max(0.0f, tileScaleX);
tileScaleY = std::max(0.0f, tileScaleY);
screenSizeX = std::max(0, screenSizeX);
screenSizeY = std::max(0, screenSizeY);
cameraPanFactor = std::max(0.0f, cameraPanFactor);
ImGui::End();
loaded = true;
}
//About box
if (aboutBoxVisible) {
this->_currentWindowType = l2d_internal::WindowTypes::AboutWindow;
ImGui::SetNextWindowSize(ImVec2(300, 130));
ImGui::Begin("About Lime2D", nullptr, ImVec2(300, 130), 100.0f, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ShowBorders);
ImGui::Text("Lime2D Editor\nVersion: 1.1\n\nBy: Limeoats\nCopyright \u00a9 2016");
ImGui::Separator();
if (ImGui::Button("Close")) {
this->_currentWindowType = l2d_internal::WindowTypes::None;
aboutBoxVisible = false;
}
ImGui::End();
}
//Map select box
if (mapSelectBoxVisible) {
this->_currentWindowType = l2d_internal::WindowTypes::MapSelectWindow;
std::string mapSelectErrorMessage = "";
std::stringstream ss;
ss << l2d_internal::utils::getConfigValue("map_path") << "*";
std::vector<const char*> mapFiles = l2d_internal::utils::getFilesInDirectory(ss.str());
ImGui::SetNextWindowPosCenter();
ImGui::SetNextWindowSize(ImVec2(500, 270));
ImGui::Begin("Select a map", nullptr, ImVec2(500, 270), 100.0f, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ShowBorders);
ImGui::Text("Select a map from the list below.");
ImGui::Separator();
ImGui::PushItemWidth(-1);
ImGui::ListBox("", &mapSelectIndex, &mapFiles[0], static_cast<int>(mapFiles.size()), 10);
ImGui::Separator();
if (ImGui::Button("Open")) {
//Get the name of the file
std::vector<std::string> fullNameSplit = l2d_internal::utils::split(mapFiles[mapSelectIndex], '/');
std::vector<std::string> fileNameSplit = l2d_internal::utils::split(fullNameSplit.back(), '.');
mapSelectErrorMessage = this->_level.loadMap(fileNameSplit.front());
if (mapSelectErrorMessage.length() <= 0) {
createGridLines();
this->_currentWindowType = l2d_internal::WindowTypes::None;
mapSelectBoxVisible = false;
}
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
this->_currentWindowType = l2d_internal::WindowTypes::None;
mapSelectBoxVisible = false;
}
ImGui::Text("%s", mapSelectErrorMessage.c_str());
ImGui::End();
}
//New map box
if (newMapBoxVisible) {
this->_currentWindowType = l2d_internal::WindowTypes::NewMapWindow;
ImGui::SetNextWindowPosCenter();
ImGui::SetNextWindowSize(ImVec2(460, 250));
static std::string newMapErrorText = "";
ImGui::Begin("New map properties", nullptr, ImVec2(460, 250), 100.0f, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_ShowBorders);
ImGui::Text("Name");
static char name[500] = "";
ImGui::PushItemWidth(300);
ImGui::InputText("", name, 500);
ImGui::PopItemWidth();
ImGui::Separator();
ImGui::PushItemWidth(100);
ImGui::PushID("NewMapSize");
ImGui::Text("Size");
static int mapSizeX = 0;
static int mapSizeY = 0;
ImGui::InputInt("x", &mapSizeX, 1, 0);
ImGui::InputInt("y", &mapSizeY, 1, 0);
ImGui::Separator();
ImGui::PopID();
ImGui::PushID("NewMapTileSize");
ImGui::Text("Tile size");
static int mapTileSizeX = 8;
static int mapTileSizeY = 8;
ImGui::InputInt("x", &mapTileSizeX, 1, 0);
ImGui::InputInt("y", &mapTileSizeY, 1, 0);
ImGui::Separator();
ImGui::PopID();
ImGui::PopItemWidth();
if (ImGui::Button("Create")) {
if (strlen(name) <= 0) {
newMapErrorText = "You must enter a name for the new map!";
}
else if (mapSizeX < 1 || mapSizeY < 1) {
newMapErrorText = "The map's height and width must be at least 1!";
}
else if (mapTileSizeX < 1 || mapTileSizeY < 1) {
newMapErrorText = "The map's tile height and tile width must be at least 1!";
}
else {
//Check if map with that name already exists. If so, give a box asking to overwrite
std::stringstream ss;
ss << l2d_internal::utils::getConfigValue("map_path") << "*";
std::vector<const char*> mapFiles = l2d_internal::utils::getFilesInDirectory(ss.str());
ss.str("");
ss << l2d_internal::utils::getConfigValue("map_path") << name << ".xml";
if (l2d_internal::utils::contains(mapFiles, ss.str())) {
newMapExistsOverwriteVisible = true;
}
else {
this->_level.createMap(std::string(name), sf::Vector2i(mapSizeX, mapSizeY),
sf::Vector2i(mapTileSizeX, mapTileSizeY));
createGridLines();
strcpy(name, "");
mapSizeX = 0;
mapSizeY = 0;
mapTileSizeX = 8;
mapTileSizeY = 8;
newMapErrorText = "";
this->_currentWindowType = l2d_internal::WindowTypes::None;
newMapBoxVisible = false;
newMapExistsOverwriteVisible = false;
}
}
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
strcpy(name, "");
mapSizeX = 0;
mapSizeY = 0;
mapTileSizeX = 8;
mapTileSizeY = 8;
newMapErrorText = "";
this->_currentWindowType = l2d_internal::WindowTypes::None;
newMapBoxVisible = false;
newMapExistsOverwriteVisible = false;
}
ImGui::Text("%s", newMapErrorText.c_str());
if (newMapExistsOverwriteVisible) {
newMapErrorText = "";
ImGui::Separator();
ImGui::Text("The name you have chosen already exists in your map directory.");
ImGui::Text("Would you like to overwrite the existing map?");
if (ImGui::Button("Sure!")) {
this->_level.createMap(std::string(name), sf::Vector2i(mapSizeX, mapSizeY),
sf::Vector2i(mapTileSizeX, mapTileSizeY));
createGridLines();
strcpy(name, "");
mapSizeX = 0;
mapSizeY = 0;
mapTileSizeX = 8;
mapTileSizeY = 8;
newMapErrorText = "";
this->_currentWindowType = l2d_internal::WindowTypes::None;
newMapBoxVisible = false;
newMapExistsOverwriteVisible = false;
}
ImGui::SameLine();
if (ImGui::Button("No!")) {
newMapExistsOverwriteVisible = false;
}
}
ImGui::End();
}
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
this->_mainHasFocus = false;
if (ImGui::MenuItem("Configure")) {
configWindowVisible = true;
}
if (ImGui::MenuItem("Exit")) {
this->_enabled = false; //TODO: do you want to save?
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit")) {
this->_mainHasFocus = false;
if (ImGui::MenuItem("Undo", "U", false, !this->_level.isUndoListEmpty())) {
this->_mainHasFocus = false;
this->_level.undo();
}
if (ImGui::MenuItem("Redo", "R", false, !this->_level.isRedoListEmpty())) {
this->_mainHasFocus = false;
this->_level.redo();
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("View")) {
this->_mainHasFocus = false;
if (ImGui::Checkbox("Map Editor", &cbMapEditor)) {
cbAnimationEditor = false;
if (cbMapEditor) {
this->_currentFeature = l2d_internal::Features::Map;
this->_currentMapEditorMode = l2d_internal::MapEditorMode::Object;
}
else {
this->_currentFeature = l2d_internal::Features::None;
currentFeature = "Lime2D";
}
}
if (ImGui::Checkbox("Animation Editor", &cbAnimationEditor)) {
cbMapEditor = false;
if (cbAnimationEditor) {
this->_currentFeature = l2d_internal::Features::Animation;
currentFeature = "Animation Editor";
}
else {
this->_currentFeature = l2d_internal::Features::None;
currentFeature = "Lime2D";
}
}
if (ImGui::Checkbox("Console", &cbShowConsole)) {
this->_showConsole = cbShowConsole;
}
ImGui::EndMenu();
}