-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTKMapper.gd
More file actions
1448 lines (1306 loc) · 54.9 KB
/
TKMapper.gd
File metadata and controls
1448 lines (1306 loc) · 54.9 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
extends Node2D
const NTK_Frame = preload("res://DataTypes/NTK_Frame.gd")
# State Variables
var cursor_map_renderer: NTK_MapRenderer = null
var cursor: NTK_Cursor = NTK_Cursor.new()
var cursor_state: NTK_Cursor.CursorState = NTK_Cursor.CursorState.IDLE
var cursor_sprite: FrameSprite = null
var cursor_tile := Sprite2D.new()
var cursor_rect := Rect2(Vector2i.ZERO, Resources.tile_size_vector)
var cursor_inner_rect := Rect2(Vector2i(0.1, 0.1), Vector2i(0.8, 0.8))
var map_regex: RegEx = RegEx.new()
var start_copy_position: Vector2i = Vector2i(-1, -1)
var start_paste_position: Vector2i = Vector2i(-1, -1)
var start_selection_position: int = -1
var previous_target_box_size: Vector2i = Resources.tile_size_vector
var max_tile_count := 0
var max_object_count := 0
var current_tile_index := 0
var current_object_index := 0
var hover_tile_index := 0
var hover_object_index := 0
var current_tile_page := 0
var current_object_page := 0
enum MapMode {
TILE = 0,
OBJECT = 1,
UNPASSABLE = 2,
}
var mode := MapMode.TILE
var undo_stack := []
# Map State
var map_tiles := []
var map_copy_tiles := []
var map_objects := {}
var map_unpassables := {}
# Selection Area
var thread_ids: Array[int] = []
# Scene Nodes
@onready var camera: Camera2D = $Camera2D
@onready var tiles: Node2D = $Tiles
@onready var objects: Node2D = $Objects
@onready var unpassables: Node2D = $Unpassables
@onready var cursor_preview: Node2D = $CursorPreview
@onready var cursor_tiles: Node2D = $CursorPreview/Tiles
@onready var cursor_objects: Node2D = $CursorPreview/Objects
@onready var target_box: Panel = $TargetBox
@onready var map_limits_box: Panel = $MapLimitsBox
@onready var map_bounds_box: Panel = $MapBoundsBox
@onready var tool_tip_label: Label = $CanvasLayer/ToolTipLabel
@onready var title_bar := $CanvasLayer/Title
@onready var tile_selection_area := $CanvasLayer/TileSelectionBackground
@onready var object_selection_area := $CanvasLayer/ObjectSelectionBackground
@onready var tile_set_container := $CanvasLayer/TileSelectionBackground/ScrollContainer/Container
@onready var object_set_container := $CanvasLayer/ObjectSelectionBackground/ScrollContainer/HBoxContainer
@onready var file_dialog := $CanvasLayer/Title/FileDialog
@onready var title_label := $CanvasLayer/Title/TitleLabel
@onready var load_map_button := $CanvasLayer/Title/LoadMap
@onready var save_map_button := $CanvasLayer/Title/SaveMap
@onready var tile_mode_button := $CanvasLayer/Title/TileMode
@onready var object_mode_button := $CanvasLayer/Title/ObjectMode
@onready var unpassable_mode_button := $CanvasLayer/Title/UnpassableMode
@onready var hide_objects_button := $CanvasLayer/Title/HideObjects
@onready var undo_button := $CanvasLayer/Title/Undo
@onready var settings_button := $CanvasLayer/Title/Settings
@onready var status_bar := $CanvasLayer/StatusBar
@onready var page_info_label := $CanvasLayer/StatusBar/PageInfoLabel
@onready var status_label := $CanvasLayer/StatusBar/StatusLabel
@onready var prev_button := $CanvasLayer/StatusBar/PreviousTile
@onready var goto_page_button := $CanvasLayer/StatusBar/GoToPage
@onready var next_button := $CanvasLayer/StatusBar/NextTile
@onready var hide_panel_button := $CanvasLayer/StatusBar/HidePanel
@onready var settings_menu := $CanvasLayer/SettingsMenu
@onready var data_dir_line_edit := $CanvasLayer/SettingsMenu/VBoxContainer/DataDirectoryContainer/LineEdit
@onready var tile_page_size_spinbox := $CanvasLayer/SettingsMenu/VBoxContainer/TilePageSizeContainer/SpinBox
@onready var object_page_size_spinbox := $CanvasLayer/SettingsMenu/VBoxContainer/ObjectPageSizeContainer/SpinBox
@onready var tile_cache_size_spinbox := $CanvasLayer/SettingsMenu/VBoxContainer/TileCacheSizeContainer/SpinBox
@onready var object_cache_size_spinbox := $CanvasLayer/SettingsMenu/VBoxContainer/ObjectCacheSizeContainer/SpinBox
@onready var goto_page := $CanvasLayer/GoToPageMenu
@onready var goto_page_spinbox := $CanvasLayer/GoToPageMenu/VBoxContainer/PageNumberContainer/SpinBox
var map_renderer: NTK_MapRenderer
var initialized: bool = false
var cursor_animation_last_tick: int = 0
var cursor_animation_last_state: NTK_Cursor.CursorState = NTK_Cursor.CursorState.IDLE
func initialize() -> void:
Renderers.map_renderer.tiles = tiles
Renderers.map_renderer.objects = objects
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
self.cursor_map_renderer = NTK_MapRenderer.new()
self.cursor_map_renderer.tiles = cursor_tiles
self.cursor_map_renderer.objects = cursor_objects
# Settings Panel
settings_menu.set_parent(self)
goto_page.set_parent(self)
#cursor = NTK_Cursor.new()
file_dialog.access = FileDialog.Access.ACCESS_FILESYSTEM
var last_map_path_parts: PackedStringArray = Database.get_config_item_value("last_map_path").split("/")
var last_map_dir: String = "/".join(last_map_path_parts.slice(0, len(last_map_path_parts) - 1))
file_dialog.current_dir = last_map_dir
file_dialog.add_filter("*.cmp", "Map Files")
map_regex.compile("TK(\\d+).cmp")
# Camera Limits
camera.limit_left = 0
camera.limit_top = -480
# Load Map
load_map(Database.get_config_item_value("last_map_path"))
# Create Cursor Tile
current_tile_index = map_tiles[0][0]["ab_index"]
for y in range(len(map_tiles)):
for x in range(len(map_tiles[y])):
if "ab_index" in map_tiles[y][x] and map_tiles[y][x]["ab_index"] != 0:
current_tile_index = map_tiles[y][x]["ab_index"]
if current_tile_index > 0:
var palette_index: int = Renderers.map_renderer.tile_renderer.tbl.palette_indices[current_tile_index]
var frame: NTK_Frame = Renderers.map_renderer.tile_renderer.get_frame(current_tile_index)
if frame.width > 0 \
and frame.height > 0:
map_copy_tiles.append([])
map_copy_tiles[0].append({
"ab_index": current_tile_index,
"sobj_index": -1,
"unpassable": false,
})
cursor_map_renderer.update_tile(current_tile_index, Vector2i(0, 0))
cursor_tile.z_index = 2
cursor_tile.centered = false
add_child(cursor_tile)
set_target_box_color(Color.GREEN)
# TileSet
max_tile_count = Renderers.map_renderer.tile_renderer.tbl.tile_count
max_object_count = Renderers.map_renderer.sobj_renderer.sobj.object_count
var max_tile_pages: int = ceil(max_tile_count / int(tile_page_size_spinbox.value))
current_tile_page = current_tile_index / int(tile_page_size_spinbox.value)
page_info_label.text = "Page " + str(current_tile_page + 1) + "/" + str(max_tile_pages + 1)
change_to_tile_mode(current_tile_page)
# Connect Signals
## Viewport
get_viewport().connect("mouse_entered", func(): GameState.over_window = true)
get_viewport().connect("mouse_exited", func(): GameState.over_window = false)
## Title Bar
title_bar.connect("mouse_entered", func():
GameState.over_title_bar = true
GameState.over_title_label = true
)
title_bar.connect("mouse_exited", func():
GameState.over_title_bar = false
GameState.over_title_label = false
)
title_label.connect("mouse_entered", func(): GameState.over_title_label = true)
title_label.connect("mouse_exited", func(): GameState.over_title_label = false)
load_map_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Load Map (L)", load_map_button.global_position + Vector2(10, 42))
)
load_map_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
save_map_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Save Map (S)", save_map_button.global_position + Vector2(-24, 42))
)
save_map_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
tile_mode_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Tile Mode (T)", tile_mode_button.global_position + Vector2(-32, 42))
)
tile_mode_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
object_mode_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Object Mode (O)", object_mode_button.global_position + Vector2(-42, 42))
)
object_mode_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
unpassable_mode_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Unpassable Mode (P)", unpassable_mode_button.global_position + Vector2(-64, 42))
)
unpassable_mode_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
hide_objects_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Hide Objects (H)", hide_objects_button.global_position + Vector2(-38, 42))
)
hide_objects_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
undo_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Undo (U)", undo_button.global_position + Vector2(-16, 42))
)
undo_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
settings_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Settings", settings_button.global_position + Vector2(-36, 42))
)
settings_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
## Selection Area
tile_selection_area.connect("mouse_entered", func(): GameState.over_selection_area = true)
tile_selection_area.connect("mouse_exited", func(): GameState.over_selection_area = false)
object_selection_area.connect("mouse_entered", func(): GameState.over_selection_area = true)
object_selection_area.connect("mouse_exited", func(): GameState.over_selection_area = false)
## Status Bar
status_bar.connect("mouse_entered", func(): GameState.over_status_bar = true)
status_bar.connect("mouse_exited", func(): GameState.over_status_bar = false)
page_info_label.connect("mouse_entered", func(): GameState.over_status_bar = true)
page_info_label.connect("mouse_exited", func(): GameState.over_status_bar = false)
prev_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Previous Page (←)", prev_button.global_position + Vector2(-48, -30))
)
prev_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
goto_page_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Goto Page (G)", goto_page_button.global_position + Vector2(-50, -30))
)
goto_page_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
next_button.connect("mouse_entered", func():
GameState.over_button = true
update_tool_tip("Next Page (→)", next_button.global_position + Vector2(-56, -30))
)
next_button.connect("mouse_exited", func():
GameState.over_button = false
update_tool_tip("")
)
hide_panel_button.connect("mouse_entered", func():
GameState.over_button = true
GameState.over_toggle_selection_area_button = true
var verb := "Hide" if tile_selection_area.visible or object_selection_area.visible else "Show"
var selection_area := "Tile Panel" if mode == MapMode.TILE else "Object Panel"
var shortcut := "(↓)" if verb == "Hide" else "(↑)"
update_tool_tip(
"%s %s %s" % [verb, selection_area, shortcut],
next_button.global_position + Vector2(-92, -30)
)
)
hide_panel_button.connect("mouse_exited", func():
GameState.over_button = false
GameState.over_toggle_selection_area_button = false
update_tool_tip("")
)
# Settings Panel
data_dir_line_edit.text = Database.get_config_item_value("data_dir")
tile_page_size_spinbox.value = int(Database.get_config_item_value("tile_page_size"))
object_page_size_spinbox.value = int(Database.get_config_item_value("object_page_size"))
tile_cache_size_spinbox.value = int(Database.get_config_item_value("tile_cache_size"))
object_cache_size_spinbox.value = int(Database.get_config_item_value("object_cache_size"))
initialized = true
func _process(delta):
# Initialize the Mapper
if not Database.database_initialized:
return
if not Database.config_key_exists("data_dir"):
print_rich("\n [b][color=red][ERROR][/color]: Unable to find a valid data directory![/b]\n")
for data_dir in Database.default_data_dirs:
print_rich(" [b][color=red]Does Not Exist[/color][/b]: [b]%s[/b]" % data_dir)
print("\n")
get_tree().quit()
return
if not initialized:
initialize()
# Load / Save Map
if Input.is_action_just_pressed("load-map") and \
not GameState.menu_open:
_load_map() # L
elif Input.is_action_just_pressed("save-map") and \
not GameState.menu_open:
_save_map() # S
# Mode Switches
if Input.is_action_just_pressed("toggle-mode") and \
not GameState.menu_open:
change_map_mode() # M
elif Input.is_action_just_pressed("mode-tile") and \
not GameState.menu_open:
change_to_tile_mode(current_tile_page) # T
elif Input.is_action_just_pressed("mode-object") and \
not GameState.menu_open:
change_to_object_mode(current_object_page) # O
elif Input.is_action_just_pressed("mode-unpassable") and \
not GameState.menu_open:
change_to_unpassable_mode() # P
# Toggle Objects
if Input.is_action_just_pressed("toggle-objects") \
and not GameState.menu_open:
_toggle_hide_objects()
# Undo Tile
if Input.is_action_just_pressed("undo") and \
not GameState.menu_open:
undo()
# Toggle Insert / Erase Modes
if Input.is_action_just_pressed("insert-mode") and \
not GameState.menu_open:
GameState.is_erase_mode = false # I
target_box.size = previous_target_box_size
elif Input.is_action_just_pressed("erase-mode") and \
not GameState.menu_open:
GameState.is_erase_mode = true # D | E | X
previous_target_box_size = target_box.size
target_box.size = Resources.tile_size_vector
# Page Switching
if Input.is_action_just_pressed("next-page") and \
not Input.is_key_pressed(KEY_SHIFT) and \
not GameState.menu_open:
_next_page()
elif Input.is_action_just_pressed("goto-page") and \
not GameState.menu_open:
_on_go_to_page_pressed()
elif Input.is_action_just_pressed("previous-page") and \
not Input.is_key_pressed(KEY_SHIFT) and \
not GameState.menu_open:
_prev_page()
elif Input.is_action_just_pressed("show-selection-area") and \
not Input.is_key_pressed(KEY_SHIFT) and \
not GameState.menu_open:
_toggle_selection_area(true, true)
elif Input.is_action_just_pressed("hide-selection-area") and \
not Input.is_key_pressed(KEY_SHIFT) and \
not GameState.menu_open:
_toggle_selection_area(true, false)
# Map Shifting
if Input.is_action_just_pressed("shift-map-up") and \
Input.is_key_pressed(KEY_SHIFT) and \
not GameState.shifting and \
not GameState.menu_open:
GameState.shifting = true
shift_map(Resources.Direction.UP)
elif Input.is_action_just_pressed("shift-map-right") and \
Input.is_key_pressed(KEY_SHIFT) and \
not GameState.shifting and \
not GameState.menu_open:
GameState.shifting = true
shift_map(Resources.Direction.RIGHT)
elif Input.is_action_just_pressed("shift-map-down") and \
Input.is_key_pressed(KEY_SHIFT) and \
not GameState.shifting and \
not GameState.menu_open:
GameState.shifting = true
shift_map(Resources.Direction.DOWN)
elif Input.is_action_just_pressed("shift-map-left") and \
Input.is_key_pressed(KEY_SHIFT) and \
not GameState.shifting and \
not GameState.menu_open:
GameState.shifting = true
shift_map(Resources.Direction.LEFT)
# Cursor
var mouse_position := get_global_mouse_position()
var mouse_coordinate := Vector2i(get_global_mouse_position()) / Resources.tile_size_vector
var snapped_mouse_position := (Vector2i(get_global_mouse_position()) - (Resources.tile_size_vector / 2)).snapped(Resources.tile_size_vector)
var grabbing_map := false
# ALT + RMB (Right Mouse Button)
GameState.copying_multiple = Input.is_key_pressed(KEY_ALT) \
and (Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or \
Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)) \
and mode != MapMode.UNPASSABLE \
and mouse_over_tile_map() \
and not GameState.is_erase_mode
if Input.is_key_pressed(KEY_CTRL) or Input.is_action_pressed("move-map"):
cursor_state = NTK_Cursor.CursorState.GRAB
grabbing_map = true
cursor_tile.visible = false
cursor_preview.visible = false
target_box.visible = false
elif GameState.is_erase_mode:
set_target_box_color(Color.RED)
cursor_tile.visible = false
cursor_preview.visible = false
target_box.visible = true
cursor_state = NTK_Cursor.CursorState.ATTACK
elif mode == MapMode.UNPASSABLE:
cursor_tile.visible = true
cursor_preview.visible = false
target_box.visible = false
cursor_state = NTK_Cursor.CursorState.IDLE
elif Input.is_key_pressed(KEY_ALT):
set_target_box_color(Color.CYAN)
if start_copy_position == Vector2i(-1, -1) \
and GameState.copying_multiple:
start_copy_position = snapped_mouse_position
cursor_tile.visible = false
cursor_preview.visible = false
target_box.visible = true
cursor_state = NTK_Cursor.CursorState.SELECT
else:
set_target_box_color(Color.GREEN)
if mode == MapMode.UNPASSABLE:
cursor_tile.visible = true
cursor_preview.visible = false
else:
cursor_tile.visible = false
cursor_preview.visible = true
target_box.visible = true
cursor_state = NTK_Cursor.CursorState.IDLE
# Copy Multiple (Done on Release of ALT + RMB)
if not GameState.copying_multiple \
and start_copy_position != Vector2i(-1, -1):
var copy_dims: Vector2i = Vector2i(
target_box.size.x / Resources.tile_size,
target_box.size.y / Resources.tile_size
)
var real_start_x: int = min(start_copy_position.x, target_box.position.x)
var real_start_y: int = min(start_copy_position.y, target_box.position.y)
var start_copy_coordinate: Vector2i = Vector2i(
real_start_x / Resources.tile_size,
real_start_y / Resources.tile_size,
)
map_copy_tiles.clear()
self.cursor_map_renderer.clear_map()
for y in range(copy_dims.y):
map_copy_tiles.append([])
for x in range(copy_dims.x):
var ab_index: int = map_tiles[start_copy_coordinate.y + y][start_copy_coordinate.x + x]["ab_index"] if mode == MapMode.TILE or Input.is_key_pressed(KEY_SHIFT) else -10
var sobj_index: int = map_tiles[start_copy_coordinate.y + y][start_copy_coordinate.x + x]["sobj_index"] if mode == MapMode.OBJECT or Input.is_key_pressed(KEY_SHIFT) else -10
var unpassable: bool = map_tiles[start_copy_coordinate.y + y][start_copy_coordinate.x + x]["unpassable"] if mode == MapMode.UNPASSABLE or Input.is_key_pressed(KEY_SHIFT) else false
map_copy_tiles[y].append({
"ab_index": ab_index,
"sobj_index": sobj_index,
"unpassable": unpassable,
})
var palette_index: int = Renderers.map_renderer.tile_renderer.tbl.palette_indices[ab_index]
var frame: NTK_Frame = Renderers.map_renderer.tile_renderer.get_frame(ab_index)
var frame_rect := Rect2i(0, 0, frame.width, frame.height)
if ab_index >= 0:
pass
cursor_map_renderer.update_tile(ab_index, Vector2i(x, y))
if sobj_index >= 0:
self.cursor_map_renderer.update_object(sobj_index, Vector2i(x, y))
start_copy_position = Vector2i(-1, -1)
update_mouse_cursor()
# Change Tile on Left Mouse Button (LMB) - Insert Mode
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and \
not Input.is_action_pressed("move-map") and \
not Input.is_key_pressed(KEY_ALT) and \
not GameState.is_erase_mode and \
mouse_over_tile_map() and \
not GameState.menu_open and \
not GameState.copying_multiple and \
coordinate_on_map(mouse_coordinate):
paste_cursor_preview(mouse_coordinate)
if mouse_coordinate.x + len(map_copy_tiles[0]) > GameState.map_size.x:
GameState.map_size.x = mouse_coordinate.x + len(map_copy_tiles[0])
if mouse_coordinate.y + len(map_copy_tiles) > GameState.map_size.y:
GameState.map_size.y = mouse_coordinate.y + len(map_copy_tiles)
map_bounds_box.size = Vector2i(GameState.map_size.x, GameState.map_size.y) * Resources.tile_size
# Copy Tile(s) from Selection Area (LMB/RMB) - Start Trigger
if (Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or \
Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)) and \
not Input.is_action_pressed("move-map") and \
not Input.is_key_pressed(KEY_ALT) and \
not GameState.is_erase_mode and \
not mouse_over_tile_map() and \
GameState.over_selection_area and \
not GameState.menu_open and \
not GameState.copying_multiple and \
start_selection_position == -1:
if mode == MapMode.TILE:
start_selection_position = self.hover_tile_index
elif mode == MapMode.OBJECT:
start_selection_position = self.hover_object_index
# Copy Tile(s) from Selection Area (LMB/RMB) - End Trigger
elif (not Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and \
not Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)) and \
not Input.is_action_pressed("move-map") and \
not Input.is_key_pressed(KEY_ALT) and \
not GameState.is_erase_mode and \
not mouse_over_tile_map() and \
GameState.over_selection_area and \
not GameState.menu_open and \
not GameState.copying_multiple and \
not start_selection_position == -1:
map_copy_tiles.clear()
map_copy_tiles.append([])
self.cursor_map_renderer.clear_map()
var end_selection_position: int = self.hover_tile_index if mode == MapMode.TILE else self.hover_object_index
var real_start: int = min(start_selection_position, end_selection_position)
var real_end: int = max(start_selection_position, end_selection_position)
target_box.size = Vector2i((real_end - real_start + 1) * Resources.tile_size, Resources.tile_size)
if mode == MapMode.TILE:
for i in range(real_start, real_end + 1):
cursor_map_renderer.update_tile(i, Vector2i(i - real_start, 0))
map_copy_tiles[0].append({
"ab_index": i,
"sobj_index": -10,
"unpassable": false,
})
elif mode == MapMode.OBJECT:
for i in range(real_start, real_end + 1):
self.cursor_map_renderer.update_object(i, Vector2i(i - real_start, 0))
map_copy_tiles[0].append({
"ab_index": -10,
"sobj_index": i,
"unpassable": false,
})
start_selection_position = -1
# Erase Tile on Mouse Button (LMB/RMB) - Eraser Mode
if (Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or \
Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)) and \
not Input.is_key_pressed(KEY_ALT) and \
not Input.is_action_pressed("move-map") and \
GameState.is_erase_mode and \
mouse_over_tile_map() and \
not GameState.menu_open and \
not GameState.copying_multiple and \
coordinate_on_map(mouse_coordinate):
if mode == MapMode.TILE:
erase_tile(mouse_coordinate)
elif mode == MapMode.OBJECT:
erase_object(mouse_coordinate)
elif mode == MapMode.UNPASSABLE:
erase_unpassable_tile(mouse_coordinate)
GameState.map_size = calculate_map_size()
map_bounds_box.size = GameState.map_size * Resources.tile_size
# Copy Tile on Right Mouse Button (RMB) - Insert Mode
if Input.is_action_just_pressed("copy-tile") and \
not Input.is_key_pressed(KEY_ALT) and \
cursor_state == NTK_Cursor.CursorState.IDLE and \
mouse_over_tile_map() and \
not GameState.copying_multiple and \
not GameState.menu_open:
var cursor_tile_coord := get_global_mouse_position()
cursor_tile_coord.x = floor(cursor_tile_coord.x / Resources.tile_size)
cursor_tile_coord.y = floor(cursor_tile_coord.y / Resources.tile_size)
if coordinate_on_map(cursor_tile_coord):
var index: int = map_tiles[cursor_tile_coord.y][cursor_tile_coord.x]["ab_index"]
if mode == MapMode.OBJECT:
index = map_tiles[cursor_tile_coord.y][cursor_tile_coord.x]["sobj_index"]
update_cursor_preview(index)
target_box.size = Resources.tile_size_vector
# Seek to Page
if mode == MapMode.TILE:
var previous_tile_page = current_tile_page
current_tile_page = current_tile_index / int(tile_page_size_spinbox.value)
if previous_tile_page != current_tile_page:
load_tileset(current_tile_page)
elif mode == MapMode.OBJECT:
var previous_object_page = current_object_page
current_object_page = current_object_index / int(object_page_size_spinbox.value)
if previous_object_page != current_object_page:
load_objectset(current_object_page)
# Tile Preview
if coordinate_on_map(mouse_coordinate) and \
mouse_position.y >= 4 and \
not grabbing_map and \
mouse_over_tile_map() and \
not GameState.menu_open:
if GameState.copying_multiple:
var real_start_x: int = min(start_copy_position.x, snapped_mouse_position.x)
var real_start_y: int = min(start_copy_position.y, snapped_mouse_position.y)
var real_end_x: int = max(start_copy_position.x, snapped_mouse_position.x)
var real_end_y: int = max(start_copy_position.y, snapped_mouse_position.y)
target_box.position = Vector2i(real_start_x, real_start_y)
target_box.size = Vector2(
max(real_end_x - real_start_x + Resources.tile_size, Resources.tile_size),
max(real_end_y - real_start_y + Resources.tile_size, Resources.tile_size),
)
else:
cursor_tile.position = snapped_mouse_position
cursor_preview.position = snapped_mouse_position
target_box.position = snapped_mouse_position
if mode != MapMode.UNPASSABLE:
target_box.visible = true
else:
#print("---------------chasing-down-focus-bug---------------------")
#print("mouse_coordinate: ", mouse_coordinate)
#print("coordinate_on_map: ", coordinate_on_map(mouse_coordinate))
#print("mouse_position.y >= 4 :: ", mouse_position.y >= 4)
#print("not grabbing_map :: ", not grabbing_map)
#print("mouse_over_tile_map() :: ", mouse_over_tile_map())
#print("not GameState.menu_open :: ", not GameState.menu_open)
cursor_tile.visible = false
cursor_preview.visible = false
target_box.visible = false
if Input.is_action_just_pressed("zoom-in") and \
not Input.is_key_pressed(KEY_CTRL) and \
mouse_over_tile_map() and \
not GameState.menu_open:
camera.position = get_global_mouse_position()
if camera.zoom.x < camera.max_zoom:
camera.zoom.x *= camera.zoom_step
if camera.zoom.y < camera.max_zoom:
camera.zoom.y *= camera.zoom_step
if Input.is_action_just_pressed("zoom-out") and \
not Input.is_key_pressed(KEY_CTRL) and \
mouse_over_tile_map() and \
not GameState.menu_open:
if camera.zoom.x > camera.min_zoom:
camera.zoom.x /= camera.zoom_step
if camera.zoom.y > camera.min_zoom:
camera.zoom.y /= camera.zoom_step
if mouse_over_tile_map() and \
not GameState.menu_open:
var info_tile_index = "ERROR" # Renderers.map_renderer.update_tile(0, mouse_coordinate)
status_label.text = "(" + str(mouse_coordinate.x) + ", " + str(mouse_coordinate.y) + ")"
elif not mouse_over_tile_map() and \
not GameState.menu_open:
if mode == MapMode.TILE:
status_label.text = "Tile Index: " + str(self.hover_tile_index)
elif mode == MapMode.OBJECT:
status_label.text = "Object Index: " + str(self.hover_object_index)
func update_tool_tip(
tool_tip_text: String,
tool_tip_position: Vector2=Vector2(0, 0)) -> void:
tool_tip_label.visible = false
tool_tip_label.size = Vector2(0, 0)
tool_tip_label.text = tool_tip_text
tool_tip_label.position = tool_tip_position
tool_tip_label.visible = true if len(tool_tip_text) > 0 else false
func mouse_over_tile_map() -> bool:
return GameState.over_window and \
not GameState.over_button and \
not GameState.over_title_bar and \
not GameState.over_title_label and \
not GameState.over_selection_area and \
not GameState.over_status_bar
func coordinate_on_map(coordinate: Vector2i) -> bool:
return coordinate.x >= 0 and \
coordinate.x <= 255 and \
coordinate.y >= 0 and \
coordinate.y <= 255
func set_target_box_color(color: Color) -> void:
var target_box_stylebox: StyleBoxFlat = target_box.get_theme_stylebox("panel")
target_box_stylebox.border_color = color
func clear_map() -> void:
Renderers.map_renderer.clear_map()
for unpassable in unpassables.get_children():
if unpassable != null:
unpassable.queue_free()
unpassable = null
func load_map(map_path: String) -> void:
clear_map()
Renderers.map_renderer.render_map(map_path, true)
map_tiles.clear()
for y in range(256):
map_tiles.append([])
for x in range(256):
map_tiles[y].append({
"ab_index": 0,
"sobj_index": -1,
"unpassable": false,
})
map_tiles = Renderers.map_renderer.get_map_tile_indices(map_tiles)
if current_tile_index == 0:
current_tile_index = map_tiles[0][0]["ab_index"]
# Add Objects to map_objects dictionary (by coordinate Vector2i)
for object in objects.get_children():
var object_coordinate := Vector2i(object.position) / Resources.tile_size_vector
object_coordinate.y -= 1
map_objects[object_coordinate] = object
# Load Unpassable Tiles in
for i in range(len(Renderers.map_renderer.cmp.tiles)):
var tile := Renderers.map_renderer.cmp.tiles[i]
var x := i % Renderers.map_renderer.cmp.width
var y := i / Renderers.map_renderer.cmp.width
if tile.unpassable_tile:
map_tiles[y][x]["unpassable"] = true
var unpassable_sprite := Sprite2D.new()
unpassable_sprite.texture = load("res://Images/placeholder-red.svg")
unpassable_sprite.centered = false
unpassable_sprite.position = Vector2i(x, y) * Resources.tile_size_vector
unpassables.add_child(unpassable_sprite)
map_unpassables[Vector2i(x, y)] = unpassable_sprite
undo_stack.clear()
undo_button.disabled = true
GameState.map_size = Vector2i(Renderers.map_renderer.cmp.width, Renderers.map_renderer.cmp.height)
map_bounds_box.size = GameState.map_size * Resources.tile_size
camera.position = Vector2(-1000, 400)
title_label.text = map_path.split("/")[-1].replace(".cmp", "")
func paste_cursor_preview(paste_coordinate: Vector2i) -> void:
for y in range(len(map_copy_tiles)):
for x in range(len(map_copy_tiles[y])):
var paste_location: Vector2i = Vector2i(
paste_coordinate.x + x,
paste_coordinate.y + y
)
if paste_location.x >= 0 \
and paste_location.x <= 255 \
and paste_location.y >= 0 \
and paste_location.y <= 255:
var tile: Dictionary = map_copy_tiles[y][x]
# Tile
current_tile_index = tile["ab_index"]
if current_tile_index >= 0:
insert_tile(Vector2i(paste_location.x, paste_location.y))
elif mode == MapMode.TILE or Input.is_key_pressed(KEY_SHIFT):
erase_tile(Vector2i(paste_location.x, paste_location.y))
# Object
current_object_index = tile["sobj_index"]
if current_object_index >= 0:
insert_object(Vector2i(paste_location.x, paste_location.y))
elif mode == MapMode.OBJECT or Input.is_key_pressed(KEY_SHIFT):
erase_object(Vector2i(paste_location.x, paste_location.y))
# Unpassable
var source_tile_unpassable = tile["unpassable"]
if source_tile_unpassable:
insert_unpassable_tile(Vector2i(paste_location.x, paste_location.y))
elif mode == MapMode.UNPASSABLE or Input.is_key_pressed(KEY_SHIFT):
erase_unpassable_tile(Vector2i(paste_location.x, paste_location.y))
start_paste_position = Vector2i(-1, -1)
func shift_map(direction: Resources.Direction) -> void:
var previous_tile_index: int = current_tile_index
var previous_object_index: int = current_object_index
if direction == Resources.Direction.UP:
# Shift Content Up
for y in range(GameState.map_size.y):
for x in range(GameState.map_size.x):
if y > 0:
# Tile
current_tile_index = map_tiles[y][x]["ab_index"]
insert_tile(Vector2i(x, y - 1), false)
# Object
current_object_index = map_tiles[y][x]["sobj_index"]
if current_object_index >= 0:
insert_object(Vector2i(x, y - 1), false)
else:
erase_object(Vector2i(x, y - 1), false)
# Unpassable
var source_tile_unpassable = map_tiles[y][x]["unpassable"]
if source_tile_unpassable:
insert_unpassable_tile(Vector2i(x, y - 1), false)
else:
erase_unpassable_tile(Vector2i(x, y - 1), false)
# Remove Content on X = GameState.map_size.y - 1
for x in range(GameState.map_size.x):
erase_tile(Vector2i(x, GameState.map_size.y - 1), false)
erase_object(Vector2i(x, GameState.map_size.y - 1), false)
erase_unpassable_tile(Vector2i(x, GameState.map_size.y - 1), false)
GameState.map_size = calculate_map_size()
map_bounds_box.size = GameState.map_size * Resources.tile_size
elif direction == Resources.Direction.RIGHT:
# Shift Content Right
for y in range(GameState.map_size.y):
for x in range(GameState.map_size.x, -1, -1):
if x < 255:
# Tile
current_tile_index = map_tiles[y][x]["ab_index"]
insert_tile(Vector2i(x + 1, y), false)
# Object
current_object_index = map_tiles[y][x]["sobj_index"]
if current_object_index >= 0:
insert_object(Vector2i(x + 1, y), false)
else:
erase_object(Vector2i(x + 1, y), false)
# Unpassable
var source_tile_unpassable = map_tiles[y][x]["unpassable"]
if source_tile_unpassable:
insert_unpassable_tile(Vector2i(x + 1, y), false)
else:
erase_unpassable_tile(Vector2i(x + 1, y), false)
# Remove Content on Y = 0
for y in range(GameState.map_size.y):
erase_tile(Vector2i(0, y), false)
erase_object(Vector2i(0, y), false)
erase_unpassable_tile(Vector2i(0, y), false)
GameState.map_size = calculate_map_size()
map_bounds_box.size = GameState.map_size * Resources.tile_size
elif direction == Resources.Direction.DOWN:
# Shift Content Down
for y in range(GameState.map_size.y, -1, -1):
for x in range(GameState.map_size.x):
if y < 255:
# Tile
current_tile_index = map_tiles[y][x]["ab_index"]
insert_tile(Vector2i(x, y + 1), false)
# Object
current_object_index = map_tiles[y][x]["sobj_index"]
if current_object_index >= 0:
insert_object(Vector2i(x, y + 1), false)
else:
erase_object(Vector2i(x, y + 1), false)
# Unpassable
var source_tile_unpassable = map_tiles[y][x]["unpassable"]
if source_tile_unpassable:
insert_unpassable_tile(Vector2i(x, y + 1), false)
else:
erase_unpassable_tile(Vector2i(x, y + 1), false)
# Remove Content on X = 0
for x in range(GameState.map_size.x):
erase_tile(Vector2i(x, 0), false)
erase_object(Vector2i(x, 0), false)
erase_unpassable_tile(Vector2i(x, 0), false)
GameState.map_size = calculate_map_size()
map_bounds_box.size = GameState.map_size * Resources.tile_size
elif direction == Resources.Direction.LEFT:
# Shift Content Left
for y in range(GameState.map_size.y):
for x in range(GameState.map_size.x):
if x > 0:
# Tile
current_tile_index = map_tiles[y][x]["ab_index"]
insert_tile(Vector2i(x - 1, y), false)
# Object
current_object_index = map_tiles[y][x]["sobj_index"]
if current_object_index >= 0:
insert_object(Vector2i(x - 1, y), false)
else:
erase_object(Vector2i(x - 1, y), false)
# Unpassable
var source_tile_unpassable = map_tiles[y][x]["unpassable"]
if source_tile_unpassable:
insert_unpassable_tile(Vector2i(x - 1, y), false)
else:
erase_unpassable_tile(Vector2i(x - 1, y), false)
# Remove Content on Y = GameState.map_size.x - 1
for y in range(GameState.map_size.y):
erase_tile(Vector2i(GameState.map_size.x - 1, y), false)
erase_object(Vector2i(GameState.map_size.x - 1, y), false)
erase_unpassable_tile(Vector2i(GameState.map_size.x - 1, y), false)
GameState.map_size = calculate_map_size()
map_bounds_box.size = GameState.map_size * Resources.tile_size
current_tile_index = previous_tile_index
current_object_index = previous_object_index
GameState.shifting = false
func insert_tile(coordinate: Vector2i, add_to_undo_stack: bool=true) -> void:
if current_tile_index < 0:
return
var previous_tile_index = map_tiles[coordinate.y][coordinate.x]["ab_index"]
if previous_tile_index != current_tile_index and add_to_undo_stack:
undo_stack.insert(0, {
"mouse_coordinate": coordinate,
"previous_index": previous_tile_index,
"new_index": current_tile_index,
"type": MapMode.TILE,
})
undo_button.disabled = false
Renderers.map_renderer.update_tile(current_tile_index, coordinate)
undo_button.disabled = false
map_tiles[coordinate.y][coordinate.x]["ab_index"] = current_tile_index
func erase_tile(coordinate: Vector2i, add_to_undo_stack: bool=true) -> void:
var previous_tile_index = map_tiles[coordinate.y][coordinate.x]["ab_index"]
if previous_tile_index != -1 and add_to_undo_stack:
undo_stack.insert(0, {
"mouse_coordinate": coordinate,
"previous_index": previous_tile_index,
"new_index": -1,
"type": MapMode.TILE,
})
undo_button.disabled = false
Renderers.map_renderer.update_tile(0, coordinate)
map_tiles[coordinate.y][coordinate.x]["ab_index"] = -1
func insert_object(coordinate: Vector2i, add_to_undo_stack: bool=true) -> void:
if current_object_index < 0:
return
var previous_object_index = map_tiles[coordinate.y][coordinate.x]["sobj_index"]
if previous_object_index != current_object_index and add_to_undo_stack:
undo_stack.insert(0, {
"mouse_coordinate": coordinate,
"previous_index": previous_object_index,
"new_index": current_object_index,
"type": MapMode.OBJECT,
})
undo_button.disabled = false
if coordinate in map_objects and \
map_objects[coordinate] != null:
map_objects[coordinate].queue_free()
map_objects[coordinate] = null
map_tiles[coordinate.y][coordinate.x]["sobj_index"] = current_object_index
Renderers.map_renderer.update_object(current_object_index, coordinate)
map_objects[coordinate] = Renderers.map_renderer.object_locations[coordinate]
func erase_object(coordinate: Vector2i, add_to_undo_stack: bool=true) -> void:
if coordinate in map_objects and \
map_objects[coordinate] != null:
map_objects[coordinate].queue_free()
map_objects[coordinate] = null
if add_to_undo_stack:
undo_stack.insert(0, {
"mouse_coordinate": coordinate,
"previous_index": map_tiles[coordinate.y][coordinate.x]["sobj_index"],
"new_index": -1,
"type": MapMode.OBJECT,
})
undo_button.disabled = false
map_tiles[coordinate.y][coordinate.x]["sobj_index"] = -1
func insert_unpassable_tile(coordinate: Vector2i, add_to_undo_stack: bool=true) -> void:
var unpassable = map_tiles[coordinate.y][coordinate.x]["unpassable"]
if not unpassable and add_to_undo_stack:
undo_stack.insert(0, {
"mouse_coordinate": coordinate,
"visible": true,
"type": MapMode.UNPASSABLE,
})
undo_button.disabled = false
if coordinate in map_unpassables and \
map_unpassables[coordinate] != null:
map_unpassables[coordinate].queue_free()
map_unpassables[coordinate] = null
map_tiles[coordinate.y][coordinate.x]["unpassable"] = true
var unpassable_sprite := Sprite2D.new()
unpassable_sprite.texture = load("res://Images/placeholder-red.svg")
unpassable_sprite.centered = false