Skip to content

Commit

Permalink
Split tilecodes from dependencies into separate palettes so that they…
Browse files Browse the repository at this point in the history
… aren't automatically added to the level file when saving, but are still available in the palette to select.
  • Loading branch information
jaythebusinessgoose committed Dec 24, 2023
1 parent bf6eb07 commit f91d8d5
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/modlunky2/ui/levels/custom_levels/custom_level_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def toggle_panel_hidden():
self.delete_tilecode,
self.add_tilecode,
None,
None,
self.texture_fetcher,
self.texture_fetcher.sprite_fetcher,
)
Expand Down Expand Up @@ -454,6 +455,7 @@ def populate_tilecode_palette(self):
self.palette_panel.update_with_palette(
self.tile_palette_ref_in_use,
self.tile_palette_suggestions,
None,
self.lvl_biome,
self.lvl,
)
Expand Down
53 changes: 52 additions & 1 deletion src/modlunky2/ui/levels/shared/palette_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def __init__(
on_delete_tilecode,
on_add_tilecode,
on_select_tile,
on_use_dependency_tile,
texture_fetcher,
sprite_fetcher,
*args,
Expand All @@ -173,6 +174,7 @@ def __init__(
self.on_delete_tilecode = on_delete_tilecode
self.on_add_tilecode = on_add_tilecode
self.on_select_tile = on_select_tile
self.on_use_dependency_tile = on_use_dependency_tile

# The tile palettes are loaded into here as buttons with their image
# as a tile and text as their value to grab when needed.
Expand Down Expand Up @@ -203,7 +205,7 @@ def delete_tilecode(self, tile_name, tile_code):
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):
def update_with_palette(self, new_palette, suggestions, dependency_tiles, biome, lvl):
for widget in self.palette.scrollable_frame.winfo_children():
widget.destroy()

Expand Down Expand Up @@ -306,6 +308,49 @@ def update_with_palette(self, new_palette, suggestions, biome, lvl):
event, ts, ti
),
)

if dependency_tiles and len(dependency_tiles):
for dependency in dependency_tiles:
if len(dependency.tiles) == 0:
continue
count_col = -1
self.palette.scrollable_frame.rowconfigure(count_row + 1, minsize=15)
count_row += 2
dependency_label = ttk.Label(self.palette.scrollable_frame, text=str(dependency.name) + ":")
dependency_label.grid(row=count_row, column=0, columnspan=5, sticky="nw")
count_row += 1

for tile in dependency.tiles:
count_col += 1
if count_col == TILES_PER_ROW:
count_col = 0
count_row += 1

tile_image = tile[2]
self.tile_images.append(tile_image)

new_tile = tk.Button(
self.palette.scrollable_frame,
text=tile[0],
width=40,
height=40,
image=tile_image,
)
new_tile.grid(row=count_row, column=count_col)
new_tile.bind(
"<Button-1>",
lambda event, t=tile, d=dependency: self.dependency_tile_pick(
event, t, d
),
)
new_tile.bind(
"<Button-3>",
lambda event, t=tile, d=dependency: self.dependency_tile_pick(
event, t, d
),
)


self.primary_tile_view.enable()
self.secondary_tile_view.enable()
self.new_tile_panel.reset()
Expand All @@ -325,6 +370,12 @@ def suggested_tile_pick(self, event, suggested_tile, tile_image):
return
self.select_tile(tile[0], tile_image, event.num == 1, True)

def dependency_tile_pick(self, event, tile, dependency):
if self.on_use_dependency_tile:
self.on_use_dependency_tile(tile, dependency)

self.select_tile(tile[0], tile[2], event.num == 1, True)

def select_tile(self, tile_name, tile_image, is_primary, tell_delegate=False):
tile_view = self.primary_tile_view
if not is_primary:
Expand Down
10 changes: 10 additions & 0 deletions src/modlunky2/ui/levels/shared/tile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from dataclasses import dataclass
from typing import List, Optional

from modlunky2.levels.level_templates import TemplateSetting
from modlunky2.ui.levels.shared.setrooms import MatchedSetroom

@dataclass
class DependencyPalette:
name: str
tiles: List
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(
on_add_tilecode,
on_delete_tilecode,
on_select_palette_tile,
on_use_dependency_tile,
on_modify_room,
on_change_filetree,
on_add_room,
Expand Down Expand Up @@ -139,6 +140,7 @@ def toggle_panel_hidden():
self.delete_tilecode,
self.add_tilecode,
self.palette_selected_tile,
on_use_dependency_tile,
self.texture_fetcher,
self.texture_fetcher.sprite_fetcher,
)
Expand Down Expand Up @@ -720,10 +722,11 @@ def room_setting_change_at(self, setting, value, map_index, row, col):
if setting == TemplateSetting.DUAL or setting == TemplateSetting.ONLYFLIP:
self.redraw()

def populate_tilecode_palette(self, tile_palette, suggestions):
def populate_tilecode_palette(self, tile_palette, suggestions, dependency_tiles):
self.palette_panel.update_with_palette(
tile_palette,
suggestions,
dependency_tiles,
self.lvl_biome,
self.lvl,
)
Expand Down
110 changes: 72 additions & 38 deletions src/modlunky2/ui/levels/vanilla_levels/vanilla_level_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from modlunky2.ui.levels.shared.palette_panel import PalettePanel
from modlunky2.ui.levels.shared.setrooms import Setroom, MatchedSetroom
from modlunky2.ui.levels.shared.tile import DependencyPalette
from modlunky2.ui.levels.vanilla_levels.dual_util import make_dual, remove_dual
from modlunky2.ui.levels.vanilla_levels.level_list_panel import LevelListPanel
from modlunky2.ui.levels.vanilla_levels.level_settings_bar import LevelSettingsBar
Expand Down Expand Up @@ -112,6 +113,7 @@ def __init__(
self.current_selected_room = None
self.usable_codes = ShortCode.usable_codes()
self.tile_palette_ref_in_use = []
self.dependency_tile_palette_ref_in_use = []
self.tile_palette_map = {}
self.tile_codes = []
self.template_list = []
Expand Down Expand Up @@ -168,6 +170,7 @@ def tab_selected(event):
),
self.delete_tilecode,
self.multiroom_editor_selected_tile,
self.use_dependency_tile,
self.multiroom_editor_modified_room,
self.multiroom_editor_changed_filetree,
self.on_insert_room,
Expand Down Expand Up @@ -283,6 +286,7 @@ def tab_selected(event):
tile, percent, alt_tile, self.palette_panel, self.mag
),
self.palette_selected_tile,
self.use_dependency_tile,
self.texture_fetcher,
self.texture_fetcher.sprite_fetcher,
)
Expand Down Expand Up @@ -327,7 +331,20 @@ def zoom_changed(_):
self.mag,
)
)
self.room_select()

if self.dependency_tile_palette_ref_in_use:
for palette in self.dependency_tile_palette_ref_in_use:
for tile in palette.tiles:
tile_name = tile[0].split(" ", 2)[0]
tile[1] = ImageTk.PhotoImage(
self.texture_fetcher.get_texture(
tile_name,
self.lvl_biome,
self.lvl,
self.mag,
)
)
self.room_select()

self.slider_zoom = tk.Scale(
side_panel_container,
Expand Down Expand Up @@ -359,6 +376,7 @@ def read_lvl_file(self, lvl):
self.button_replace["state"] = tk.NORMAL

self.tile_palette_ref_in_use = []
self.dependency_tile_palette_ref_in_use = []
self.tile_palette_map = {}
self.lvl = lvl

Expand All @@ -377,23 +395,11 @@ def read_lvl_file(self, lvl):
else:
lvl_path = self.extracts_path / lvl

# Levels to load dependency tilecodes from.
level_dependencies = LevelDependencies.dependencies_for_level(lvl)
levels = []
for dependency in level_dependencies:
levels.append(
LevelDependencies.loaded_level_file_for_path(
dependency, self.lvls_path, self.extracts_path
)
)
levels.append(LevelFile.from_path(Path(lvl_path)))

level = None
for level in levels:
def get_level_tilecodes(level):
logger.debug("%s loaded.", level.comment)
level_tilecodes = level.tile_codes.all()

for tilecode in level_tilecodes:
def tilecode_item(tilecode):
tilecode_item = []
tilecode_item.append(str(tilecode.name) + " " + str(tilecode.value))

Expand All @@ -407,18 +413,44 @@ def read_lvl_file(self, lvl):
tilecode_item.append(ImageTk.PhotoImage(img))
tilecode_item.append(ImageTk.PhotoImage(selection_img))

self.select_palette_tile(tilecode_item, True)
self.select_palette_tile(tilecode_item, False)
return tilecode_item

return [tilecode_item(tc) for tc in level_tilecodes]
def clear_tile_from_dependencies(tile):
for palette in self.dependency_tile_palette_ref_in_use:
for i in palette.tiles:
if str(i[0]).split(" ", 1)[1] == str(tile[0]).split(" ", 1)[1]:
palette.tiles.remove(i)

for i in self.tile_palette_ref_in_use:
if str(i[0]).split(" ", 1)[1] == str(tilecode.value):
self.tile_palette_ref_in_use.remove(i)
def register_tile_code(tile):
self.select_palette_tile(tile, True)
self.select_palette_tile(tile, False)
code = tile[0].split(" ", 1)[1]
if code in self.usable_codes:
self.usable_codes.remove(code)

if tilecode.value in self.usable_codes:
self.usable_codes.remove(tilecode.value)
self.tile_palette_map[code] = tile

self.tile_palette_ref_in_use.append(tilecode_item)
self.tile_palette_map[tilecode.value] = tilecode_item
level_dependencies = LevelDependencies.dependencies_for_level(lvl)

for dependency in level_dependencies:
level = LevelDependencies.loaded_level_file_for_path(
dependency, self.lvls_path, self.extracts_path
)
logger.debug("%s loaded.", level.comment)
tiles = get_level_tilecodes(level)
for tile in tiles:
clear_tile_from_dependencies(tile)
register_tile_code(tile)

self.dependency_tile_palette_ref_in_use.insert(0, DependencyPalette("From " + dependency, tiles))

level = LevelFile.from_path(Path(lvl_path))
tiles = get_level_tilecodes(level)
self.tile_palette_ref_in_use = tiles
for tile in tiles:
clear_tile_from_dependencies(tile)
register_tile_code(tile)

# Populate the default tile code for left clicks.
if "1" in self.tile_palette_map:
Expand Down Expand Up @@ -508,11 +540,12 @@ def populate_tilecode_palette(self):
self.palette_panel.update_with_palette(
self.tile_palette_ref_in_use,
None,
self.dependency_tile_palette_ref_in_use,
self.lvl_biome,
self.lvl,
)
self.multi_room_editor_tab.populate_tilecode_palette(
self.tile_palette_ref_in_use, None
self.tile_palette_ref_in_use, None, self.dependency_tile_palette_ref_in_use
)

def palette_selected_tile(self, tile_name, image, is_primary):
Expand Down Expand Up @@ -880,18 +913,9 @@ def room_select(self): # Loads room when click if not parent node.
for canvas_index, layer_tile_codes in enumerate(self.tile_codes):
for row_index, row in enumerate(layer_tile_codes):
for column_index, tile_code in enumerate(row):
tile_name = ""
tiles = [
c
for c in self.tile_palette_ref_in_use
if str(" " + tile_code) in str(c[0])
]
if tiles:
tile_image = tiles[-1][1]
tile_name = str(tiles[-1][0]).split(" ", 1)[0]
else:
# There's a missing tile id somehow
logger.debug("%s Not Found", tile_code)
tile = self.tile_palette_map[tile_code]
tile_name = str(tile[0]).split(" ", 1)[0]
tile_image = tile[1]
x_coord, y_coord = self.texture_fetcher.adjust_texture_xy(
tile_image.width(),
tile_image.height(),
Expand Down Expand Up @@ -948,6 +972,14 @@ def log_codes_left(self):
codes += str(code)
logger.debug("%s codes left (%s)", len(self.usable_codes), codes)

def use_dependency_tile(self, tile, dependency):
self.tile_palette_ref_in_use.append(tile)
dependency.tiles.remove(tile)

self.populate_tilecode_palette()
self.changes_made()


def add_tilecode(
self,
tile,
Expand Down Expand Up @@ -996,8 +1028,9 @@ def add_tilecode(
)

# Compares tile id to tile ids in palette list.
for palette_tile in self.tile_palette_ref_in_use:
for _, palette_tile in self.tile_palette_map.items():
palette_tile = palette_tile[0].split()[0].strip()
print(palette_tile)
if new_tile_code == palette_tile:
tkMessageBox.showinfo("Uh Oh!", "You already have that!")
return
Expand Down Expand Up @@ -1172,6 +1205,7 @@ def reset(self):
self.canvas.clear()
self.tile_palette_map = {}
self.tile_palette_ref_in_use = None
self.dependency_tile_palette_ref_in_use = None
self.lvl = None
self.lvl_biome = None
self.reset_save_button()
Expand Down

0 comments on commit f91d8d5

Please sign in to comment.