-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
1633 lines (1344 loc) · 38.7 KB
/
main.cpp
File metadata and controls
1633 lines (1344 loc) · 38.7 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
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 '../../lib/std.cpp';
#include '../../lib/layer.cpp';
#include '../../lib/string/common.cpp';
#include '../../lib/math/math.cpp';
#include '../../lib/math/geom.cpp';
#include '../../lib/input/VK.cpp';
#include '../../lib/embed_utils.cpp';
#include '../../lib/drawing/common.cpp';
#include '../../lib/drawing/circle.cpp';
#include '../../lib/debug/Debug.cpp';
#include '../../lib/enums/ColType.cpp';
#include '../../lib/input/common.cpp';
#include '../../lib/input/GVB.cpp';
#include '../../lib/input/Mouse.cpp';
#include '../../lib/math/Line.cpp';
#include '../../lib/utils/colour.cpp';
#include '../../lib/utils/copy_vars.cpp';
#include '../../lib/utils/print_vars.cpp';
#include '../../lib/ui3/UI.cpp';
#include '../../lib/ui3/elements/Toolbar.cpp';
#include '../../lib/ui3/popups/PopupOptions.cpp';
#include '../../lib/ui3/window_manager/WindowManager.cpp';
#include '../../lib/debug/Debug.cpp';
#include 'handles/Handles.cpp';
#include 'misc/DragHandleType.cpp';
#include 'misc/EditorKey.cpp';
#include 'misc/InfoOverlay.cpp';
#include 'misc/IWorldBoundingBox.cpp';
#include 'misc/LayerInfoDisplay.cpp';
#include 'misc/ShortcutKeySorter.cpp';
#include 'misc/WorldBoundingBox.cpp';
#include 'settings/Config.cpp';
#include 'settings/Settings.cpp';
#include 'tools/edge_brush/EdgeBrushTool.cpp';
#include 'tools/emitter_tool/EmitterTool.cpp';
#include 'tools/shape_tool/ShapeTool.cpp';
#include 'tools/pen_tool/PenTool.cpp';
#include 'tools/prop_line_tool/PropLineTool.cpp';
#include 'tools/prop_tool/PropTool.cpp';
#include 'tools/trigger_tool/ExtendedTriggerTool.cpp';
#include 'tools/DepthTool.cpp';
#include 'tools/ExtendedTileTool.cpp';
#include 'tools/ExtendedPropTool.cpp';
#include 'tools/HelpTool.cpp';
#include 'tools/ParticleEditorTool.cpp';
#include 'ToolGroup.cpp';
const string SCRIPT_BASE = 'ed/adv_tools/';
const string SPRITES_BASE = SCRIPT_BASE + 'sprites/';
const string EMBED_spr_icon_edit = SPRITES_BASE + 'icon_edit.png';
const string EMBED_spr_icon_visible = SPRITES_BASE + 'icon_visible.png';
const string EMBED_spr_icon_invisible = SPRITES_BASE + 'icon_invisible.png';
const bool AS_EDITOR_PLUGIN = true;
const string SPRITE_SET = AS_EDITOR_PLUGIN ? 'plugin' : 'script';
class script : AdvToolScript {}
class AdvToolScript
{
scene@ g = get_scene();
editor_api@ editor = get_editor_api();
input_api@ input = get_input_api();
camera@ cam;
UI@ ui;
Mouse@ mouse;
Handles handles;
Debug debug();
Line line;
sprites@ editor_spr;
sprites@ script_spr;
float zoom;
bool mouse_in_gui;
bool mouse_in_scene;
bool ui_focus;
bool scene_focus;
bool in_editor = true;
EditorKey ctrl = EditorKey(input, GVB::Control, ModifierKey::Ctrl);
EditorKey shift = EditorKey(input, GVB::Shift, ModifierKey::Shift);
EditorKey alt = EditorKey(input, GVB::Alt, ModifierKey::Alt);
EditorKey space = EditorKey(input, GVB::Space);
bool return_press, escape_press;
bool space_on_press;
bool pressed_in_scene;
bool shortcut_keys_enabled = true;
int layer = 19, sub_layer = 19;
/// Kind of a hack, but can be set to "consume" a single shortcut key.
/// Any tool shortcuts matching this will not be able to trigger
ShortcutKey blocked_key;
InfoOverlay info_overlay;
bool debug_ui;
string clipboard;
WindowManager window_manager;
PropsClipboardData props_clipboard;
string selected_tool_name;
Config config(this);
private bool initialised;
private bool state_persisted = true;
private bool queue_load_config;
private float frame;
private Toolbar@ toolbar;
private array<ToolGroup> tool_groups;
private array<Tool@> tools;
private array<Tool@> tools_shortcut;
private int num_tools_shortcut;
private dictionary tool_groups_map;
private dictionary tools_map;
private ButtonGroup@ button_group;
private PopupOptions@ shortcut_keys_enabled_popup;
private array<Image@> icon_images;
private PopupOptions@ info_popup;
private float info_popup_timer;
private Label@ info_label;
/// '_' = Tool has not been initialised yet
private string selected_tab = '_';
private string previous_selected_tab = '_';
private Tool@ selected_tool;
private int num_tool_group_popups;
private EventCallback@ on_after_layout_delegate;
private int pressed_key = -1;
private bool pressed_key_active;
private int pressed_timer;
private bool ignore_toolbar_select_event;
private bool hide_gui;
private bool hide_panels_gui;
private bool hide_layers_gui;
private bool hide_gui_user = false;
private bool hide_toolbar_user = false;
private bool hide_panels_user = false;
//
float view_x, view_y;
float view_x1, view_y1;
float view_x2, view_y2;
float view_w, view_h;
float screen_w, screen_h;
array<array<float>> layer_scales(23);
/// Maps a layer index to it's order
array<int> layer_positions(23);
/// Maps an order to a layer index
array<int> layer_indices(23);
int min_layer = 6;
int min_sub_layer = 0;
float min_layer_scale = 1;
int min_fg_layer = 6;
int min_fg_sub_layer = 0;
float min_fg_layer_scale = 1;
Toolbar@ main_toolbar { get { return @toolbar; } }
// //////////////////////////////////////////////////////////
// Init
// //////////////////////////////////////////////////////////
AdvToolScript()
{
puts('>> Initialising AdvTools');
@cam = get_active_camera();
@ui = UI(true);
@mouse = Mouse(false, 22, 22);
mouse.use_input(input);
for(int i = 0; i <= 22; i++)
{
layer_scales[i].resize(25);
}
blocked_key.init(this);
@button_group = ButtonGroup(ui, false);
if(@editor == null)
{
initialised = true;
return;
}
layer = editor.selected_layer;
sub_layer = editor.selected_sub_layer;
editor.hide_gui(false);
do_load_config(false);
create_tools();
//
@ui.debug = debug;
debug.text_font = font::ENVY_BOLD;
debug.text_size = 20;
debug.text_align_y = 1;
debug.text_display_newset_first = false;
}
void editor_loaded()
{
in_editor = true;
if(@selected_tool != null)
{
selected_tool.on_editor_loaded();
}
editor.hide_toolbar_gui(true);
editor.hide_gui(hide_gui);
hide_gui_panels(hide_panels_gui);
hide_gui_layers(hide_layers_gui);
store_layer_values();
}
void editor_unloaded()
{
in_editor = false;
if(@selected_tool != null)
{
selected_tool.on_editor_unloaded();
}
editor.hide_toolbar_gui(false);
editor.hide_gui(false);
editor.hide_panels_gui(false);
editor.hide_layers_gui(false);
}
void build_sprites(message@ msg)
{
build_sprite(@msg, 'icon_edit', 0, 0);
build_sprite(@msg, 'icon_visible', 0, 0);
build_sprite(@msg, 'icon_invisible', 0, 0);
for(uint i = 0; i < tools.length(); i++)
{
tools[i].build_sprites(msg);
}
}
void reload_config()
{
queue_load_config = true;
}
private bool do_load_config(const bool trigger_load=true)
{
if(!config.load())
return false;
bool requires_reload = false;
if(!requires_reload && !config.compare_float('UISpacing', ui.style.spacing))
requires_reload = true;
if(!requires_reload && !config.compare_colour('UIBGColour', ui.style.normal_bg_clr))
requires_reload = true;
if(!requires_reload && !config.compare_colour('UIBorderColour', ui.style.normal_border_clr))
requires_reload = true;
if(!requires_reload)
{
initialise_ui_style();
for(uint i = 0; i < icon_images.length; i++)
{
icon_images[i].colour = config.UIIconColour;
}
}
for(uint i = 0; i < tools.length(); i++)
{
// Just easier to reload everything if a shortcut changes
if(tools[i].reload_shortcut_key())
{
return true;
}
}
if(trigger_load && !requires_reload)
{
for(uint i = 0; i < tools.length(); i++)
{
tools[i].on_settings_loaded();
}
for(uint i = 0; i < tool_groups.length(); i++)
{
tool_groups[i].on_settings_loaded();
}
ui.after_layout.on(on_after_layout_delegate);
position_toolbar();
}
return requires_reload;
}
private void do_full_reload()
{
@ui = UI(true);
@toolbar = null;
tool_groups.resize(0);
tools.resize(0);
tools_shortcut.resize(0);
num_tools_shortcut = 0;
tool_groups_map.deleteAll();
tools_map.deleteAll();
icon_images.resize(0);
selected_tab = '';
@selected_tool = null;
num_tool_group_popups = 0;
create_tools();
initialised = false;
}
private void initialise()
{
@editor_spr = create_sprites();
editor_spr.add_sprite_set('editor');
@script_spr = create_sprites();
script_spr.add_sprite_set(SPRITE_SET);
initialise_ui();
initialise_tools();
select_tool(selected_tool_name != '' ? selected_tool_name : editor.editor_tab(), false);
info_overlay.init(this);
handles.init(this);
}
private void initialise_ui()
{
@ui.debug = debug;
ui.clipboard = clipboard;
ui.clipboard_change.on(EventCallback(on_clipboard_change));
ui.auto_fit_screen = true;
initialise_ui_style();
// ui.style.text_clr = 0xffffffff;
// ui.style.normal_bg_clr = 0xd9050505;
// ui.style.normal_border_clr = 0x33ffffff;
// ui.style.highlight_bg_clr = 0xd933307c;
// ui.style.highlight_border_clr = 0xd96663c2;
// ui.style.selected_bg_clr = 0xd933307c;
// ui.style.selected_border_clr = 0xff7d7acb;
// ui.style.selected_highlight_bg_clr = 0xd9423fa0;
// ui.style.selected_highlight_border_clr = 0xff7d7acb;
// ui.style.disabled_bg_clr = 0xa6000000;
// ui.style.disabled_border_clr = 0x26ffffff;
// ui.style.secondary_bg_clr = 0x667f7daf;
// ui.style.scrollbar_light_bg_clr = 0xd9111111;
window_manager.initialise(ui);
@toolbar = Toolbar(ui, false, true);
toolbar.name = 'ToolsToolbar';
toolbar.x = 400;
toolbar.y = 200;
button_group.allow_reselect = true;
button_group.select.on(EventCallback(on_tool_button_select));
for(uint i = 0; i < tool_groups.length(); i++)
{
ToolGroup@ group = @tool_groups[i];
group.init_ui();
toolbar.add_child(group.button);
}
ui.add_child(toolbar);
position_toolbar();
// Info popup
//{
@info_label = Label(ui, '', true, font::SANS_BOLD, 20);
info_label.scale_x = 0.75;
info_label.scale_y = 0.75;
@info_popup = PopupOptions(ui, info_label, false, PopupPosition::BelowLeft, PopupTriggerType::Manual, PopupHideType::Manual);
info_popup.spacing = 0;
info_popup.background_colour = multiply_alpha(ui.style.normal_bg_clr, 0.5);
//}
@on_after_layout_delegate = EventCallback(on_after_layout);
ui.after_layout.on(on_after_layout_delegate);
ui.screen_resize.on(EventCallback(on_screen_resize));
update_shortcut_keys_enabled_popup();
}
private void initialise_ui_style()
{
ui.style.spacing = config.get_float('UISpacing', ui.style.spacing);
if(config.has_value('UITextColour'))
ui.style.auto_text_colour(config.get_colour('UITextColour'));
if(config.has_value('UIBGColour'))
ui.style.auto_base_colour(config.get_colour('UIBGColour'));
if(config.has_value('UIBorderColour'))
ui.style.auto_border_colour(config.get_colour('UIBorderColour'));
if(config.has_value('UIAccentColour'))
ui.style.auto_accent_colour(config.get_colour('UIAccentColour'));
}
private void initialise_tools()
{
for(uint i = 0; i < tool_groups.length(); i++)
{
tool_groups[i].on_init();
}
}
private void create_tools()
{
// Built in
add_tool(Tool(this, 'Select') .set_icon('editor', 'selecticon').init_shortcut_key(VK::R));
add_tool(ExtendedTileTool(this));
add_tool(ExtendedPropTool(this));
add_tool(Tool(this, 'Entities') .set_icon('editor', 'entityicon').init_shortcut_key(VK::E));
add_tool(ExtendedTriggerTool(this));
add_tool(Tool(this, 'Camera') .set_icon('editor', 'cameraicon').init_shortcut_key(VK::C));
add_tool(EmitterTool(this));
add_tool(Tool(this, 'Level Settings') .set_icon('editor', 'settingsicon'));
add_tool(Tool(this, 'Scripts') .set_icon('dustmod', 'scripticon').init_shortcut_key(VK::S));
add_tool(HelpTool(this, 'Help') .set_icon('editor', 'helpicon'));
@tools_map[''] = null;
@tools_map['Wind'] = null;
@tools_map['Particle'] = ParticleEditorTool(this);
// Custom
add_tool('Tiles', EdgeBrushTool(this));
add_tool('Tiles', ShapeTool(this));
add_tool('Tiles', DepthTool(this));
add_tool('Tiles', PenTool(this));
add_tool('Props', PropTool(this));
add_tool('Props', PropLineTool(this));
sort_shortcut_tools();
}
private void sort_shortcut_tools()
{
array<ShortcutKeySorter> sort_list(tools_shortcut.length);
for(uint i = 0; i < tools_shortcut.length; i++)
{
@sort_list[i].tool = tools_shortcut[i];
sort_list[i].index = i;
}
dictionary shortcut_map;
sort_list.sortAsc();
for(uint i = 0; i < sort_list.length; i++)
{
Tool@ tool = sort_list[i].tool;
const string map_key = tool.key.to_string();
@tool.shortcut_key_group = shortcut_map.exists(map_key)
? cast<Tool@>(shortcut_map[map_key]).shortcut_key_group
: array<Tool@>();
tool.shortcut_key_group.insertLast(tool);
@tools_shortcut[i] = tool;
@shortcut_map[map_key] = tool;
}
}
// //////////////////////////////////////////////////////////
// Main Methods
// //////////////////////////////////////////////////////////
void editor_step()
{
if(@editor == null)
return;
if(!initialised)
{
initialise();
initialised = true;
}
screen_w = g.hud_screen_width(false);
screen_h = g.hud_screen_height(false);
view_x = cam.x();
view_y = cam.y();
cam.get_layer_draw_rect(0, 22, 22, view_x1, view_y1, view_w, view_h);
view_x2 = view_x1 + view_w;
view_y2 = view_y1 + view_h;
zoom = cam.editor_zoom();
ctrl.update(frame);
shift.update(frame);
alt.update(frame);
space.update(frame);
return_press = input.key_check_pressed_gvb(GVB::Return);
escape_press = input.key_check_pressed_gvb(GVB::Escape);
layer = editor.selected_layer;
sub_layer = editor.selected_sub_layer;
mouse_in_gui = editor.mouse_in_gui();
mouse_in_scene = !mouse_in_gui && !ui.is_mouse_over_ui && !ui.is_mouse_active && !space.down;
ui_focus = input.is_polling_keyboard();
scene_focus = @ui.focus == null && !ui_focus;
handle_keyboard();
handles.step();
mouse.step(space.down || !mouse_in_scene);
const string new_selected_tab = editor.editor_tab();
if(
new_selected_tab != selected_tab && (
new_selected_tab == '' || new_selected_tab == 'Help' ||
new_selected_tab == 'Particle' || new_selected_tab == 'Wind'))
{
select_tool(new_selected_tab, false);
}
if(mouse.left_press)
{
space_on_press = space.down;
pressed_in_scene = mouse_in_scene;
}
else if(mouse.left_release)
{
space_on_press = false;
pressed_in_scene = false;
}
if(
config.EnableShortcuts && @ui.focus == null && shortcut_keys_enabled && !ui_focus &&
(@selected_tool == null || !selected_tool.active))
{
if(config.KeyPrevTool.check())
{
select_next_tool(-1);
}
else if(config.KeyNextTool.check())
{
select_next_tool(1);
}
else
{
//if(selected_tab == 'Particle')
for(int i = num_tools_shortcut - 1; i >= 0; i--)
{
Tool@ tool = @tools_shortcut[i];
if(!tool.key.matches(blocked_key) && tool.key.check())
{
select_tool(tool.on_shortcut_key());
persist_state();
break;
}
}
}
if(config.KeyToggleToolbars.check())
{
hide_toolbar_user = !hide_toolbar_user;
ui.visible = !hide_toolbar_user;
}
if(config.KeyToggleUI.check())
{
hide_gui_user = !hide_gui_user;
hide_gui_panels(hide_panels_gui);
hide_gui_layers(hide_layers_gui);
}
if(config.KeyTogglePanels.check())
{
hide_panels_user = !hide_panels_user;
hide_gui_panels(hide_panels_gui);
}
if(config.KeyPreviewLayer.check())
{
preview_layer();
}
}
if(@selected_tool != null)
{
selected_tool.step();
}
// get_tool('Prop Tool').step();
if(info_popup_timer > 0)
{
info_popup_timer = max(info_popup_timer - DT, 0.0);
if(info_popup_timer <= 0)
{
hide_info_popup();
}
}
info_overlay.step();
ui.step();
debug.step();
if(toolbar.hovered || num_tool_group_popups > 0)
{
if(toolbar.alpha < 1)
{
toolbar.alpha = min(toolbar.alpha + Settings::UIFadeSpeed * DT, 1.0);
}
}
else if(toolbar.alpha > Settings::UIFadeAlpha)
{
toolbar.alpha = max(toolbar.alpha - Settings::UIFadeSpeed * DT, Settings::UIFadeAlpha);
}
state_persisted = false;
if(queue_load_config)
{
if(do_load_config())
{
do_full_reload();
}
queue_load_config = false;
}
frame++;
}
private void handle_keyboard()
{
if(input.key_check_pressed_vk(VK::Pause))
{
shortcut_keys_enabled = !shortcut_keys_enabled;
update_shortcut_keys_enabled_popup();
}
if(!shortcut_keys_enabled)
return;
if(pressed_key != -1)
{
pressed_key_active = false;
if(ui_focus || !input.key_check_gvb(pressed_key))
{
pressed_key = -1;
}
else
{
if(--pressed_timer == 0)
{
pressed_key_active = true;
pressed_timer = Settings::KeyRepeatPeriod;
}
return;
}
}
if(!ui_focus)
{
for(int i = int(Settings::RepeatKeys.length()) - 1; i >= 0; i--)
{
const int key = Settings::RepeatKeys[i];
if(!input.key_check_pressed_gvb(key))
continue;
pressed_key = key;
pressed_timer = Settings::KeyPressDelay;
pressed_key_active = true;
break;
}
}
}
private void preview_layer()
{
const int start_layer = editor.get_selected_layer() + 1;
const bool visible = !editor.get_layer_visible(start_layer);
for(int i = start_layer; i <= 20; i++)
{
editor.set_layer_visible(i, visible);
}
}
void editor_draw(float sub_frame)
{
if(@selected_tool != null)
{
selected_tool.draw(sub_frame);
}
// get_tool('Prop Tool').draw(sub_frame);
handles.draw();
ui.draw();
if(debug_ui)
{
ui.debug_draw();
}
debug.draw(sub_frame);
}
// //////////////////////////////////////////////////////////
// Public Methods
// //////////////////////////////////////////////////////////
Tool@ get_tool(const string name)
{
if(!tools_map.exists(name))
return null;
return cast<Tool@>(tools_map[name]);
}
bool select_tool(const string &in name, const bool update_editor_tab=true)
{
if(!tools_map.exists(name))
return false;
select_tool(cast<Tool@>(tools_map[name]), update_editor_tab);
return true;
}
void select_next_tool(const int dir=1)
{
if(@selected_tool == null)
{
select_tool(tools[0]);
return;
}
ToolGroup@ group = selected_tool.group;
Tool@ next_tool = selected_tool;
do
{
if(@next_tool != null)
{
@next_tool = group.get_next_selectable_tool(next_tool, dir);
}
if(@next_tool != null)
break;
const int index = tool_groups.findByRef(group);
if(index == -1)
{
@group = tool_groups[0];
}
else
{
@group = tool_groups[mod(index + (dir >= 0 ? 1 : -1), tool_groups.length())];
}
@next_tool = dir >= 1
? group.get_first_selectable_tool()
: group.get_last_selectable_tool();
}
while(@next_tool == null);
select_tool(next_tool);
}
void track_tool_group_popups(const bool open)
{
num_tool_group_popups += open ? 1 : -1;
}
void init_icon(Image@ img)
{
if(@img == null)
return;
img.colour = config.UIIconColour;
icon_images.insertLast(img);
}
void init_icon(Button@ button)
{
init_icon(button.icon);
}
void init_icon(MultiButton@ mbutton)
{
for(uint i = 0, count = mbutton.num_items; i < count; i++)
{
init_icon(mbutton.get_image(i));
}
}
void world_to_hud(
const int layer, const int sub_layer,
const float x, const float y,
float &out hud_x, float &out hud_y, const bool ui_coords=true)
{
float wx, wy;
transform(x, y, layer, sub_layer, 19, 0, wx, wy);
hud_x = (wx - view_x1) / view_w * screen_w;
hud_y = (wy - view_y1) / view_h * screen_h;
if(!ui_coords)
{
hud_x -= screen_w * 0.5;
hud_y -= screen_h * 0.5;
}
}
void world_to_hud(
const int layer, const int sub_layer, entity@ e,
float &out hud_x, float &out hud_y, const bool ui_coords=true)
{
world_to_hud(layer, sub_layer, e.x(), e.y(), hud_x, hud_y, ui_coords);
}
void world_to_hud(Mouse@ mouse, float &out hud_x, float &out hud_y, const bool ui_coords=true)
{
world_to_hud(mouse.layer, mouse.sub_layer, mouse.x, mouse.y, hud_x, hud_y, ui_coords);
}
int layer_position(const int layer_index)
{
return layer_positions[layer_index];
}
int layer_index(const int layer_position)
{
return layer_indices[layer_position];
}
float layer_scale(const uint layer, const uint sub_layer)
{
return layer_scales[layer <= 22 ? layer : 22][sub_layer <= 24 ? sub_layer : 24];
}
float layer_scale(const int from_layer, const int from_sub_layer, const int to_layer, const int to_sub_layer)
{
return layer_scale(from_layer, from_sub_layer) / layer_scale(to_layer, to_sub_layer);
}
void select_layer(const int layer)
{
if(this.layer == layer)
return;
this.layer = layer;
editor.selected_layer = layer;
}
void select_sub_layer(const int sub_layer)
{
if(this.sub_layer == sub_layer)
return;
this.sub_layer = sub_layer;
editor.selected_sub_layer = sub_layer;
}
/// Change layer/sub layer with mouse wheel.
bool scroll_layer(
const bool allow_layer_update=true, const bool allow_sub_layer_update=true,
const bool tiles_only=false, LayerInfoDisplay show_info=LayerInfoDisplay::Individual,
IWorldBoundingBox@ target=null, Element@ target_element=null,
const float time=0.5)
{
if(mouse.scroll == 0)
return false;
int updated = 0;
int updated_layer = 0;
if(allow_layer_update && ctrl.down)
{
const int prev_layer = this.layer;
int layer = clamp(prev_layer + mouse.scroll,
tiles_only ? 6 : 0, 20);
if(tiles_only && layer == 18)
{
layer += mouse.scroll;
}
if(layer != prev_layer)
{
select_layer(layer);
updated = 1;
updated_layer = layer;
}
}
else if(allow_sub_layer_update && alt.down)
{
const int prev_sub_layer = this.sub_layer;
int sub_layer = clamp(prev_sub_layer + mouse.scroll, 0, 24);
if(sub_layer != prev_sub_layer)
{
select_sub_layer(sub_layer);
updated = 2;
updated_layer = sub_layer;
}
}
if(show_info != LayerInfoDisplay::None && updated != 0)
{
if(show_info == LayerInfoDisplay::Compound && (!allow_sub_layer_update || !allow_layer_update))
{
show_info = LayerInfoDisplay::Individual;
}
const string info = show_info == LayerInfoDisplay::Individual
? (updated == 1 ? 'Layer: ' : 'Sub Layer: ') + updated_layer
: 'Layer: ' + layer + '.' + sub_layer;
if(@target_element != null)
{
info_overlay.show(target_element, info, time);
}
else if(@target != null)
{
info_overlay.show(target, info, time);
}
else
{
info_overlay.show(mouse, info, time);
}
}
return updated != 0;
}
void transform(
const float x, const float y,
const int from_layer, const int from_sub_layer, const int to_layer, const int to_sub_layer,
float &out out_x, float &out out_y)
{
const float scale = layer_scale(from_layer, from_sub_layer) / layer_scale(to_layer, to_sub_layer);
const float dx = (x - view_x) * scale;
const float dy = (y - view_y) * scale;
out_x = view_x + dx;
out_y = view_y + dy;
}
void transform(
const float x, const float y,
const int from_layer, const int from_sub_layer, Mouse@ mouse,
float &out out_x, float &out out_y)
{
transform(x, y, from_layer, from_sub_layer, mouse.layer, mouse.sub_layer, out_x, out_y);
}
float transform_size(
const float size,
const int from_layer, const int from_sub_layer, const int to_layer, const int to_sub_layer)
{
return size * (layer_scale(from_layer, from_sub_layer) / layer_scale(to_layer, to_sub_layer));
}
void transform_size(
const float x, const float y,
const int from_layer, const int from_sub_layer, const int to_layer, const int to_sub_layer,
float &out out_x, float &out out_y)
{
const float scale = layer_scale(from_layer, from_sub_layer) / layer_scale(to_layer, to_sub_layer);
out_x = x * scale;
out_y = y * scale;