-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattlefield_editor.py
795 lines (628 loc) · 32.4 KB
/
battlefield_editor.py
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
__author__ = "Fox Cunning"
from tkinter import Canvas
from typing import List
from PIL import Image, ImageTk
import colour
from appJar import gui
# ----------------------------------------------------------------------------------------------------------------------
from appJar.appjar import ItemLookupError
from debug import log
from palette_editor import PaletteEditor
from rom import ROM
from undo_redo import UndoRedo
class BattlefieldEditor:
# ------------------------------------------------------------------------------------------------------------------
def __init__(self, app: gui, rom: ROM, palette_editor: PaletteEditor):
self.app = app
self.rom = rom
self.palette_editor = palette_editor
self._unsaved_changes = False
self._map_address: List[int] = []
# TODO Maybe read these from a customised (optional) text file, or config file section
self._map_names: List[str] = ["Grass", "Brush", "Forest", "Player vs Sea Monsters",
"Player vs Pirate Ship", "Door", "Stone Floor 1", "Lava",
"Wall", "Table", "Chest", "Stone Floor 2", "Wall Top", "Castle / Dungeon",
"Dungeon Entrance", "Town", "Player Ship vs Sea Monsters",
"Player Ship vs Pirate Ship",
"Player Ship vs Land Enemies"]
# Pattern info
self._pattern_info: List[str] = ["Normal", "Normal", "Normal", "Water", "Blocking", "Normal", "Normal",
"Normal", "Blocking", "Blocking", "Normal", "Normal", "Blocking", "Normal",
"Normal", "Normal"]
# Map being currently edited. Only modify this in show_window() and close_window().
self._selected_map = 0
# Current pick to put on the map when drawing
self._selected_tile = 0
# Index of the last map entry that was modified or selected
self._last_edited = 0
# Logical map data (i.e. tile indices 0x0-0xF)
self._map_data: List[int] = []
self._grid_colour = "#C0C0C0"
# Image cache
self._patterns_cache: List[ImageTk.PhotoImage] = []
# Canvas item IDs
self._tiles_grid: List[int] = [0] * 16
self._tile_items: List[int] = [0] * 16
self._tile_rectangle: int = 0
self._map_grid: List[int] = [0] * 8 * 12
self._map_items: List[int] = [0] * 9 * 13
self._map_rectangle: int = 0
self._canvas_tiles: Canvas = Canvas()
self._canvas_map: Canvas = Canvas()
# We keep track of how many tiles are modified with a single operation, for the undo/redo manager
self._modified_tiles: int = 0
self._undo_redo: UndoRedo = UndoRedo()
# These keep count of how many actions to process in each single undo/redo
self._undo_actions: List[int] = []
self._redo_actions: List[int] = []
# ------------------------------------------------------------------------------------------------------------------
def error(self, message: str):
log(2, f"{self.__class__.__name__}", message)
# ------------------------------------------------------------------------------------------------------------------
def warning(self, message: str):
log(3, f"{self.__class__.__name__}", message)
# ------------------------------------------------------------------------------------------------------------------
def info(self, message: str):
log(4, f"{self.__class__.__name__}", message)
# ------------------------------------------------------------------------------------------------------------------
def show_window(self, map_index: int = -1) -> None:
"""
Opens the graphical battlefield editor.
Parameters
----------
map_index: int
Index of the map to edit, or -1 to edit the last selected map
"""
if map_index >= 0:
self._selected_map = map_index
else:
map_index = self._selected_map
# Create widgets if needed
try:
self.app.getCanvasWidget("BE_Canvas_Map")
except ItemLookupError:
self._create_widgets()
self._canvas_tiles = self.app.getCanvasWidget("BE_Canvas_Tiles")
self._canvas_tiles.bind("<Button-1>", self._tiles_left_click, add='')
self._canvas_map = self.app.getCanvasWidget("BE_Canvas_Map")
self._canvas_map.bind("<ButtonPress-1>", self._map_left_down, add='')
self._canvas_map.bind("<ButtonRelease-1>", self._map_left_up, add='')
self._canvas_map.bind("<B1-Motion>", self._map_left_drag, add='')
self._canvas_map.bind("<Button-3>", self._map_right_click, add='')
self._load_patterns()
self._read_map_data(self._map_address[map_index])
self.draw_map()
# Default selections
self.select_map(0)
self.select_pattern(0)
# Clear undo/redo history
self._modified_tiles = 0
self._undo_redo.clear()
self._undo_actions = []
self._redo_actions = []
self._update_undo_buttons()
self.app.showSubWindow("Battlefield_Editor", hide=False)
sw = self.app.openSubWindow("Battlefield_Editor")
sw.bind("<Control-z>", self._undo, add='')
sw.bind("<Control-y>", self._redo, add='')
# ------------------------------------------------------------------------------------------------------------------
def close_window(self) -> bool:
"""
Closes the battlefield editor window and destroys its widgets.
Returns
-------
bool
True if the window was closed. False if cancelled by user.
"""
if self._unsaved_changes is True:
if self.app.yesNoBox("Battlefield Editor",
"Are you sure you want to close this window?\nUnsaved changes will be lost.",
parent="Battlefield_Editor") is False:
return False
self.app.hideSubWindow("Battlefield_Editor")
self.app.emptySubWindow("Battlefield_Editor")
# Clear image cache
self._patterns_cache: List[ImageTk.PhotoImage] = []
# Reset canvas item IDs
self._tiles_grid: List[int] = [0] * 16
self._tile_items: List[int] = [0] * 16
self._tile_rectangle: int = 0
self._map_grid: List[int] = [0] * 8 * 12
self._map_items: List[int] = [0] * 9 * 13
self._map_rectangle: int = 0
return True
# ------------------------------------------------------------------------------------------------------------------
def _create_widgets(self) -> None:
with self.app.subWindow("Battlefield_Editor"):
# noinspection PyArgumentList
self.app.setStopFunction(self.close_window)
with self.app.frame("BE_Frame_Buttons", padding=[4, 2], row=0, column=0):
self.app.button("BE_Button_Save", self.map_input, image="res/floppy.gif", width=32, height=32,
tooltip="Save Changes and Close",
sticky="E", row=0, column=0)
self.app.button("BE_Button_Cancel", self.map_input, image="res/close.gif", width=32, height=32,
tooltip="Cancel and Close Window",
sticky="W", row=0, column=1)
self.app.canvas("BE_Separator", width=32, height=32, row=0, column=2)
self.app.button("BE_Button_Undo", self.map_input, image="res/undo.gif", width=32, height=32,
tooltip="Nothing to Undo",
sticky="E", row=0, column=3)
self.app.button("BE_Button_Redo", self.map_input, image="res/redo.gif", width=32, height=32,
tooltip="Nothing to Redo",
sticky="E", row=0, column=4)
with self.app.frame("BE_Frame_Controls", padding=[4, 1], row=1, column=0):
self.app.checkBox("BE_Check_Grid", True, name="Show grid", change=self.map_input,
row=0, column=0, sticky="W")
self.app.button("BE_Grid_Colour", self.map_input, name="Change Grid Colour", row=0, column=1, font=9)
with self.app.frame("BE_Frame_Tiles", padding=[4, 2], row=2, column=0):
self.app.canvas("BE_Canvas_Tiles", width=256, height=64, row=0, column=0, bg="#808080")
self.app.setCanvasCursor("BE_Canvas_Tiles", "hand2")
self.app.label("BE_Tile_Info", "", sticky="WE", row=1, column=0, font=10, fg=colour.BLACK)
with self.app.frame("BE_Frame_Map", padding=[4, 2], row=3, column=0):
self.app.canvas("BE_Canvas_Map", width=288, height=416, row=0, column=0, colspan=2, bg="#808080")
self.app.setCanvasCursor("BE_Canvas_Map", "pencil")
# ------------------------------------------------------------------------------------------------------------------
def _load_patterns(self) -> None:
# First, create a list of pattern addresses using the "default" tileset
addresses: List[int] = []
for p in range(16):
addresses.append(0x8A40 + (p * 64)) # Each 2x2 tile is 64 bytes long
banks: List[int] = [0xA] * 16 # Bank $0A by default
if self.rom.has_feature("map tilesets"):
# v1.09+ map tiles
# The same substitutions to Town maps apply
# The tables at $0A:B600 and $0B:B868 have the same format:
# Source address in ROM, destination address in PPU, number of bytes to transfer
address = 0xB642
pattern_address = self.rom.read_word(0xA, address)
tile_index = (self.rom.read_word(0xA, address + 2) - 0x1A40) >> 6
tile_count = self.rom.read_word(0xA, address + 4) >> 6
for t in range(tile_count):
addresses[tile_index] = pattern_address
pattern_address = pattern_address + 64
tile_index = tile_index + 1
# Further substitutions are in bank $0B
address = 0xB868
pattern_address = self.rom.read_word(0xB, address)
tile_index = (self.rom.read_word(0xB, address + 2) - 0x1A40) >> 6
tile_count = self.rom.read_word(0xB, address + 4) >> 6
for t in range(tile_count):
banks[tile_index] = 0xB
addresses[tile_index] = pattern_address
pattern_address = pattern_address + 64
tile_index = tile_index + 1
address = 0xB86E
pattern_address = self.rom.read_word(0xB, address)
tile_index = (self.rom.read_word(0xB, address + 2) - 0x1A40) >> 6
tile_count = self.rom.read_word(0xB, address + 4) >> 6
for t in range(tile_count):
banks[tile_index] = 0xB
addresses[tile_index] = pattern_address
pattern_address = pattern_address + 64
tile_index = tile_index + 1
address = 0xB874
pattern_address = self.rom.read_word(0xB, address)
tile_index = (self.rom.read_word(0xB, address + 2) - 0x1A40) >> 6
tile_count = self.rom.read_word(0xB, address + 4) >> 6
for t in range(tile_count):
banks[tile_index] = 0xB
addresses[tile_index] = pattern_address
pattern_address = pattern_address + 64
tile_index = tile_index + 1
address = 0xB87A
pattern_address = self.rom.read_word(0xB, address)
tile_index = (self.rom.read_word(0xB, address + 2) - 0x1A40) >> 6
tile_count = self.rom.read_word(0xB, address + 4) >> 6
for t in range(tile_count):
banks[tile_index] = 0xB
addresses[tile_index] = pattern_address
pattern_address = pattern_address + 64
tile_index = tile_index + 1
else:
# TODO Hardcoded tile substitutions
self.app.warningBox("Battlefield Editor", "Tile substitutions not implemented for this ROM.",
parent="Battlefield_Editor")
# TODO Detect vanilla game and use its hardcoded substitutions + "blank" ship tile
pass
# Now we have a full list of pattern addresses, we can use it to create our images
tile_index = 0
map_palette = self.palette_editor.palettes[0]
self._patterns_cache.clear()
for a in addresses:
# Get palette index for this tile from table in ROM at 0D:856D
palette_index = self.rom.read_byte(0x0D, 0x856D + tile_index) * 4
colours = []
for c in range(palette_index, palette_index + 4):
colour_index = map_palette[c]
rgb = bytearray(self.palette_editor.get_colour(colour_index))
colours.append(rgb[0]) # Red
colours.append(rgb[1]) # Green
colours.append(rgb[2]) # Blue
# We will combine the four patterns in a single up-scaled 32x32 image and then cache it
tile = Image.new('P', (32, 32), 0)
tile.putpalette(colours)
# Top-left pattern
pixels = bytes(bytearray(self.rom.read_pattern(banks[tile_index], a)))
image = Image.frombytes('P', (8, 8), pixels)
image.putpalette(colours)
tile.paste(image.resize((16, 16), Image.NONE), (0, 0))
# Bottom-left pattern
pixels = bytes(bytearray(self.rom.read_pattern(banks[tile_index], a + 0x10)))
image = Image.frombytes('P', (8, 8), pixels)
image.putpalette(colours)
tile.paste(image.resize((16, 16), Image.NONE), (0, 16))
# Top-right pattern
pixels = bytes(bytearray(self.rom.read_pattern(banks[tile_index], a + 0x20)))
image = Image.frombytes('P', (8, 8), pixels)
image.putpalette(colours)
tile.paste(image.resize((16, 16), Image.NONE), (16, 0))
# Bottom-right pattern
pixels = bytes(bytearray(self.rom.read_pattern(banks[tile_index], a + 0x30)))
image = Image.frombytes('P', (8, 8), pixels)
image.putpalette(colours)
tile.paste(image.resize((16, 16), Image.NONE), (16, 16))
image = ImageTk.PhotoImage(tile)
self._patterns_cache.append(image)
# Show this tile in the tile pattern canvas (8 x 2 tiles)
x = 16 + (32 * (tile_index % 8))
y = 16 + (32 * (tile_index >> 3))
if self._tile_items[tile_index] > 0:
self._canvas_tiles.itemconfigure(self._tile_items[tile_index], image=image)
else:
self._tile_items[tile_index] = self.app.addCanvasImage("BE_Canvas_Tiles", x, y, image)
# Next tile
tile_index = tile_index + 1
# Show / create grid lines
grid_index = 0
# 7 vertical lines
for x in range(1, 8):
if self._tiles_grid[grid_index] > 0:
self._canvas_tiles.tag_raise(self._tiles_grid[grid_index])
else:
left = x << 5
self._tiles_grid[grid_index] = self._canvas_tiles.create_line(left, 0, left, 64, fill=self._grid_colour)
# Next item
grid_index = grid_index + 1
# Just 1 horizontal line is needed
if self._tiles_grid[grid_index] > 0:
self._canvas_tiles.tag_raise(self._tiles_grid[grid_index])
else:
self._tiles_grid[grid_index] = self._canvas_tiles.create_line(0, 32, 256, 32, fill=self._grid_colour)
if self._tile_rectangle > 0:
self._canvas_tiles.tag_raise(self._tile_rectangle)
# ------------------------------------------------------------------------------------------------------------------
def _get_selection_index(self, widget: str) -> int:
"""
Returns
-------
int:
The index of the currently selected option from an OptionBox widget
"""
value = "(nothing)"
try:
value = self.app.getOptionBox(widget)
box = self.app.getOptionBoxWidget(widget)
return box.options.index(value)
except ValueError as error:
self.error(f"ERROR: Getting selection index for '{value}' in '{widget}': {error}.")
return 0
# ------------------------------------------------------------------------------------------------------------------
def get_map_names(self) -> List[str]:
return self._map_names.copy()
# ------------------------------------------------------------------------------------------------------------------
def save_tab_data(self) -> None:
"""
Saves battle music ID and table with map pointers.
"""
# Save address table
if self.rom.read_byte(0xF, 0xC7ED) == 0xBD:
table_address = self.rom.read_word(0xF, 0xC7EE)
else:
if self.app.yesNoBox("Battlefield Editor", "Couldn't read battlefield maps address table pointer.\n" +
"Do you want to continue using default value?", parent="Battlefield_Editor") is True:
table_address = 0xCEAC
else:
table_address = -1
if table_address >= 0xC000:
for a in range(19):
self.rom.write_word(0xF, table_address, self._map_address[a])
table_address = table_address + 2
elif table_address != -1:
# Don't warn if action cancelled
self.warning(f"Invalid address for battlefield map pointers: 0x{table_address:04X}.")
# Save battle music ID
if self.rom.read_byte(0xF, 0xC7F7) == 0xA9:
music_id = self._get_selection_index("Battlefield_Option_Music")
self.rom.write_byte(0xF, 0xC7F8, music_id | 0x80)
# ------------------------------------------------------------------------------------------------------------------
def map_input(self, widget: str) -> None:
if widget == "BE_Button_Cancel":
self.close_window()
elif widget == "BE_Button_Save":
self.save_map_data()
self.app.soundWarning()
self.close_window()
elif widget == "BE_Button_Undo":
self._undo()
elif widget == "BE_Button_Redo":
self._redo()
elif widget == "BE_Grid_Colour":
# Pick a new colour
self._grid_colour = self.app.colourBox(self._grid_colour, parent="Battlefield_Editor")
# Update both grids
for item in self._tiles_grid:
if item > 0:
self._canvas_tiles.itemconfigure(item, fill=self._grid_colour)
for item in self._map_grid:
if item > 0:
self._canvas_map.itemconfigure(item, fill=self._grid_colour)
elif widget == "BE_Check_Grid":
if self.app.getCheckBox(widget) is True:
# Show grid items
for item in self._tiles_grid:
if item > 0:
self._canvas_tiles.itemconfigure(item, state="normal")
for item in self._map_grid:
if item > 0:
self._canvas_map.itemconfigure(item, state="normal")
else:
# Hide grid items
for item in self._tiles_grid:
if item > 0:
self._canvas_tiles.itemconfigure(item, state="hidden")
for item in self._map_grid:
if item > 0:
self._canvas_map.itemconfigure(item, state="hidden")
else:
self.warning(f"Unimplemented input from map widget: '{widget}'.")
# ------------------------------------------------------------------------------------------------------------------
def read_tab_data(self) -> None:
"""
Reads battle music ID and table with map pointers.
"""
if self.rom.read_byte(0xF, 0xC7ED) == 0xBD:
table_address = self.rom.read_word(0xF, 0xC7EE)
else:
self.warning("Couldn't read battlefield maps address table pointer.\nUsing default value.")
table_address = 0xCEAC
self._map_address.clear()
for a in range(19):
self._map_address.append(self.rom.read_word(0xF, table_address))
table_address = table_address + 2
# Battle music
data = self.rom.read_bytes(0xF, 0xC7F7, 2)
if data[0] == 0xA9:
self.app.setOptionBox("Battlefield_Option_Music", data[1] & 0x7F, callFunction=False)
# ------------------------------------------------------------------------------------------------------------------
def get_map_address(self, map_index: int = -1) -> int:
if map_index == -1:
map_index = self._selected_map
try:
return self._map_address[map_index]
except IndexError:
return 0
# ------------------------------------------------------------------------------------------------------------------
def set_map_address(self, address: int, map_index: int = -1) -> None:
if map_index == -1:
map_index = self._selected_map
if address < 0x8000 or address > 0xBFFF:
self.warning(f"Invalid address 0x{address:04X} for battlefield map #{map_index}.")
self._map_address[map_index] = address
# ------------------------------------------------------------------------------------------------------------------
def _read_map_data(self, address: int) -> None:
# Clear previous data
self._map_data.clear()
# We need to skip the first and last row
address = address + 16
# We will also skip the first column, and anything after the first 10 tiles in each row
for y in range(13):
for x in range(16):
if x == 0:
# Skip first column
address = address + 1
continue
if x > 9:
address = address + 1
continue
tile = self.rom.read_byte(0x4, address) & 0x0F
self._map_data.append(tile)
address = address + 1
# ------------------------------------------------------------------------------------------------------------------
def save_map_data(self) -> None:
address = self._map_address[self._selected_map]
# Skip the first row
address = address + 16
t = 0
# We will also skip the first column, and anything after the first 10 tiles in each row
for y in range(13):
for x in range(16):
if x == 0:
# Skip first column
address = address + 1
continue
if x > 9:
# Skip last 6 bytes
address = address + 1
continue
self.rom.write_byte(0x4, address, self._map_data[t])
t = t + 1
address = address + 1
self._unsaved_changes = False
# ------------------------------------------------------------------------------------------------------------------
def _update_undo_buttons(self) -> None:
if len(self._undo_actions) < 1:
self.app.disableButton("BE_Button_Undo")
self.app.setButtonTooltip("BE_Button_Undo", "Nothing to Undo")
else:
self.app.enableButton("BE_Button_Undo")
self.app.setButtonTooltip("BE_Button_Undo", "Undo: " + self._undo_redo.get_undo_text())
if len(self._redo_actions) < 1:
self.app.disableButton("BE_Button_Redo")
self.app.setButtonTooltip("BE_Button_Redo", "Nothing to Redo")
else:
self.app.enableButton("BE_Button_Redo")
self.app.setButtonTooltip("BE_Button_Undo", "Undo: " + self._undo_redo.get_redo_text())
# ------------------------------------------------------------------------------------------------------------------
def draw_map(self) -> None:
"""
Populates the map canvas with patterns and draws /creates the grid if required.
"""
tile_index = 0
for y in range(13):
for x in range(9):
tile_id = self._map_data[tile_index]
# If this item already existed, only change the image
if self._map_items[tile_index] > 0:
self._canvas_map.itemconfigure(self._map_items[tile_index], image=self._patterns_cache[tile_id])
else:
self._map_items[tile_index] = self._canvas_map.create_image(x << 5, y << 5,
image=self._patterns_cache[tile_id],
anchor="nw")
# Next tile
tile_index = tile_index + 1
# Show / create / hide grid as needed
if self.app.getCheckBox("BE_Check_Grid") is True:
# Show / create
grid_index = 0
# 8 vertical lines
for x in range(1, 9):
if self._map_grid[grid_index] > 0:
self._canvas_map.itemconfigure(self._map_grid[grid_index], state="normal")
self._canvas_map.tag_raise(self._map_grid[grid_index])
else:
left = x << 5
self._map_grid[grid_index] = self._canvas_map.create_line(left, 0, left, 416,
fill=self._grid_colour)
# Next line
grid_index = grid_index + 1
# 12 horizontal lines
for y in range(1, 13):
if self._map_grid[grid_index] > 0:
self._canvas_map.itemconfigure(self._map_grid[grid_index], state="normal")
self._canvas_map.tag_raise(self._map_grid[grid_index])
else:
top = y << 5
self._map_grid[grid_index] = self._canvas_map.create_line(0, top, 288, top,
fill=self._grid_colour)
# Next line
grid_index = grid_index + 1
# Raise selection rectangle if it exists
if self._tile_rectangle > 0:
self._canvas_tiles.tag_raise(self._tile_rectangle)
# ------------------------------------------------------------------------------------------------------------------
def select_pattern(self, tile_index: int) -> None:
"""
Moves the selection rectangle around the given tile and shows info about the selection.
The selection rectangle will be created if needed.
"""
self._selected_tile = tile_index
# Calculate coordinates on the canvas, keep in mind there are 8x2 patterns, 32x32 pixels each
x = (tile_index % 8) << 5
y = (tile_index >> 3) << 5
if self._tile_rectangle > 0:
# Update coordinates of existing rectangle
self._canvas_tiles.coords(self._tile_rectangle, x + 1, y + 1, x + 31, y + 31)
else:
# Create a rectangle at the selection's coordinates
self._tile_rectangle = self._canvas_tiles.create_rectangle(x + 1, y + 1, x + 31, y + 31,
width=2, outline="#FF3030")
self.app.setLabel("BE_Tile_Info", f"Selected: {self._pattern_info[tile_index]} tile #0x{tile_index:02X}")
# ------------------------------------------------------------------------------------------------------------------
def select_map(self, selection_index: int) -> None:
"""
Moves the selection rectangle around the given map square.
The selection rectangle will be created if needed.
"""
self._last_edited = selection_index
# Calculate coordinates on the canvas, keep in mind there are 9x13 patterns, 32x32 pixels each
x = (selection_index % 9) << 5
y = (int(selection_index / 9)) << 5
if self._map_rectangle > 0:
# Update coordinates of existing rectangle
self._canvas_map.coords(self._map_rectangle, x + 1, y + 1, x + 31, y + 31)
else:
# Create a rectangle at the selection's coordinates
self._map_rectangle = self._canvas_map.create_rectangle(x + 1, y + 1, x + 31, y + 31,
width=2, outline="#FF3030")
# ------------------------------------------------------------------------------------------------------------------
def _undo(self, _event=None) -> None:
try:
count = self._undo_actions.pop()
except IndexError:
return
self._undo_redo.undo(count)
self._redo_actions.append(count)
self._update_undo_buttons()
# ------------------------------------------------------------------------------------------------------------------
def _redo(self, _event=None) -> None:
try:
count = self._redo_actions.pop()
except IndexError:
return
self._undo_redo.redo(count)
self._undo_actions.append(count)
self._update_undo_buttons()
# ------------------------------------------------------------------------------------------------------------------
def _tiles_left_click(self, event: any) -> None:
"""
Callback for left mouse click on the tile picker canvas.
"""
# Calculate tile index depending on position
x = event.x >> 5
y = event.y >> 5
self.select_pattern(x + (y << 3))
# ------------------------------------------------------------------------------------------------------------------
def _map_left_drag(self, event: any) -> None:
# Prevent dragging outside the canvas
if event.x < 0 or event.x >= 288 or event.y < 0 or event.y >= 416:
return
# Calculate tile index
x = event.x >> 5
y = event.y >> 5
t = x + (y * 9)
# Avoid re-editing same tile we are already on
if t == self._last_edited:
return
self._last_edited = t
old_tile = self._map_data[t]
self._undo_redo(self._map_edit_tile, (t, self._selected_tile), (t, old_tile), text="Draw")
self._modified_tiles += 1
# Move selection rectangle
if self._map_rectangle > 0:
x = (x << 5) + 1
y = (y << 5) + 1
self._canvas_map.coords(self._map_rectangle, x, y, x + 30, y + 30)
# ------------------------------------------------------------------------------------------------------------------
def _map_left_up(self, _event: any = None) -> None:
self._undo_actions.append(self._modified_tiles)
self._redo_actions = []
self._modified_tiles = 0
self._update_undo_buttons()
# ------------------------------------------------------------------------------------------------------------------
def _map_left_down(self, event: any) -> None:
# Calculate tile index
x = event.x >> 5
y = event.y >> 5
t = x + (y * 9)
self._last_edited = t
old_tile = self._map_data[t]
self._modified_tiles += 1
self._undo_redo(self._map_edit_tile, (t, self._selected_tile), (t, old_tile), text="Draw")
# Move selection rectangle
if self._map_rectangle > 0:
x = (x << 5) + 1
y = (y << 5) + 1
self._canvas_map.coords(self._map_rectangle, x, y, x + 30, y + 30)
# ------------------------------------------------------------------------------------------------------------------
def _map_right_click(self, event: any) -> None:
# Calculate tile index
x = event.x >> 5
y = event.y >> 5
t = x + (y * 9)
self.select_map(t)
self.select_pattern(self._map_data[t])
# ------------------------------------------------------------------------------------------------------------------
def _map_edit_tile(self, tile_index: int, new_tile: int) -> None:
self._map_data[tile_index] = new_tile
if self._map_items[tile_index] > 0:
self._canvas_map.itemconfigure(self._map_items[tile_index], image=self._patterns_cache[new_tile])