Skip to content

Commit

Permalink
Merge branch 'Gugubo-level-editor' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gmjosack committed Dec 18, 2023
2 parents 82f2d52 + bafd606 commit 21c6cbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
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 @@ -664,16 +664,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 @@ -697,6 +697,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 @@ -164,7 +165,7 @@ def __init__(
texture_fetcher,
sprite_fetcher,
*args,
**kwargs
**kwargs,
):
super().__init__(parent, *args, **kwargs)

Expand Down Expand Up @@ -195,25 +196,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 @@ -236,6 +242,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 @@ -250,10 +268,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 @@ -284,9 +304,12 @@ 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]
is_primary = (event.num == 1) or (event.state & 0x20000 == 0)
self.select_tile(
selected_tile["text"], selected_tile["image"], event.num == 1, True
selected_tile["text"], selected_tile["image"], is_primary, True
)

def suggested_tile_pick(self, event, suggested_tile, tile_image):
Expand Down

0 comments on commit 21c6cbe

Please sign in to comment.