Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Level editor] Add keyboard shortcut for selecting tiles #1136

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/modlunky2/ui/levels/custom_levels/custom_level_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,16 @@ def add_tilecode(self, tile, percent, alt_tile):
return ref_tile

def delete_tilecode(self, tile_name, tile_code):
if tile_name == r"empty":
tkMessageBox.showinfo("Uh Oh!", "Can't delete empty!")
return False

msg_box = tk.messagebox.askquestion(
"Delete Tilecode?",
"Are you sure you want to delete this Tilecode?\nAll of its placements will be replaced with air.",
icon="warning",
)
if msg_box == "yes":
if tile_name == r"empty":
tkMessageBox.showinfo("Uh Oh!", "Can't delete empty!")
return

new_tile = self.tile_palette_map["0"]
for matrix_index, tile_code_matrix in enumerate(self.tile_codes):
for row in range(len(tile_code_matrix)):
Expand All @@ -690,6 +690,10 @@ def delete_tilecode(self, tile_name, tile_code):
self.log_codes_left()
self.changes_made()

return True
else:
return False

def log_codes_left(self):
codes = ""
for code in self.usable_codes:
Expand Down
55 changes: 39 additions & 16 deletions src/modlunky2/ui/levels/shared/palette_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __init__(

self.delete_button = tk.Button(
self,
text="Del",
text="Delete",
bg="red",
fg="white",
width=10,
Expand Down Expand Up @@ -143,9 +143,10 @@ def tile_name(self):
def tile_code(self):
return self.name.split(" ", 1)[1]

def reset(self):
def reset(self, disable=True):
self.select_tile("empty 0", None)
self.disable()
if disable:
self.disable()

def enable(self):
self.delete_button["state"] = tk.NORMAL
Expand All @@ -163,7 +164,7 @@ def __init__(
texture_fetcher,
sprite_fetcher,
*args,
**kwargs
**kwargs,
):
super().__init__(parent, *args, **kwargs)

Expand Down Expand Up @@ -193,25 +194,30 @@ def __init__(
self.new_tile_panel.grid(row=3, column=0, sticky="swne")

def delete_tilecode(self, tile_name, tile_code):
self.on_delete_tilecode(tile_name, tile_code)
if self.primary_tile_view.tile_code() == tile_code:
self.primary_tile_view.reset()
if self.secondary_tile_view.tile_code() == tile_code:
self.secondary_tile_view.reset()
deleted = self.on_delete_tilecode(tile_name, tile_code)
if deleted:
if self.primary_tile_view.tile_code() == tile_code:
self.primary_tile_view.reset(disable=False)
if self.secondary_tile_view.tile_code() == tile_code:
self.secondary_tile_view.reset(disable=False)

def update_with_palette(self, new_palette, suggestions, biome, lvl):
for widget in self.palette.scrollable_frame.winfo_children():
widget.destroy()

TILES_PER_ROW = 8

count_row = 0
count_col = -1
self.tile_images = []
used_tile_names = []

for tile_keep in new_palette:
if count_col == 7:
count_col = -1
count_col += 1
if count_col == TILES_PER_ROW:
count_col = 0
count_row = count_row + 1
count_col = count_col + 1

tile_name = tile_keep[0].split(" ", 2)[0]
used_tile_names.append(tile_name)

Expand All @@ -234,6 +240,18 @@ def update_with_palette(self, new_palette, suggestions, biome, lvl):
lambda event, r=count_row, c=count_col: self.tile_pick(event, r, c),
)

# Bind first ten tiles to number keys
tile_index = count_col + (count_row * TILES_PER_ROW) + 1
if tile_index <= 10:
self.bind_all(
f"{tile_index%10}",
lambda event, r=count_row, c=count_col: self.tile_pick(event, r, c),
)
self.bind_all(
f"<Alt-Key-{tile_index%10}>",
lambda event, r=count_row, c=count_col: self.tile_pick(event, r, c),
)

if suggestions and len(suggestions):
count_col = -1
self.palette.scrollable_frame.rowconfigure(count_row + 1, minsize=15)
Expand All @@ -248,10 +266,12 @@ def update_with_palette(self, new_palette, suggestions, biome, lvl):
if suggestion in used_tile_names:
# Do not suggest a tile that already exists in the palette.
continue
if count_col == 7:
count_col = -1

count_col += 1
if count_col == TILES_PER_ROW:
count_col = 0
count_row = count_row + 1
count_col = count_col + 1

tile_image = ImageTk.PhotoImage(
self.texture_fetcher.get_texture(suggestion, biome, lvl, 40)
)
Expand Down Expand Up @@ -282,8 +302,11 @@ def update_with_palette(self, new_palette, suggestions, biome, lvl):
self.new_tile_panel.enable()

def tile_pick(self, event, row, col):
if not self.palette.scrollable_frame.grid_slaves(row, col):
return
selected_tile = self.palette.scrollable_frame.grid_slaves(row, col)[0]
self.select_tile(selected_tile["text"], selected_tile["image"], event.num == 1)
is_primary = (event.num == 1) or (event.state & 0x20000 == 0)
self.select_tile(selected_tile["text"], selected_tile["image"], is_primary)

def suggested_tile_pick(self, event, suggested_tile, tile_image):
tile = self.on_add_tilecode(suggested_tile, 100, "empty")
Expand Down