Refactor code structure for improved readability and maintainability
This commit is contained in:
+28
-1
@@ -196,6 +196,28 @@ def resolve_keybindings(preferred_profile=None, preferred_file=None, render_engi
|
||||
|
||||
|
||||
class KeyBindings:
|
||||
def _binding_sections_for_action(self):
|
||||
game_end_active, game_end_reason = getattr(self, "game_end", (False, None))
|
||||
if game_end_active:
|
||||
if game_end_reason == "level_clear":
|
||||
return ["keybinding_level_clear", "keybinding_paused"]
|
||||
if game_end_reason == "defeat":
|
||||
return ["keybinding_defeat", "keybinding_paused"]
|
||||
if game_end_reason == "run_complete":
|
||||
return ["keybinding_run_complete", "keybinding_paused"]
|
||||
return ["keybinding_paused"]
|
||||
|
||||
if getattr(self, "game_status", None) == "start_menu":
|
||||
if getattr(self, "menu_screen", None) == "level_intro":
|
||||
return ["keybinding_level_intro", "keybinding_start_menu"]
|
||||
return ["keybinding_start_menu"]
|
||||
|
||||
status = getattr(self, "game_status", None)
|
||||
if status:
|
||||
return [f"keybinding_{status}"]
|
||||
|
||||
return []
|
||||
|
||||
def initialize_keybindings(self):
|
||||
preferred_profile = None
|
||||
preferred_file = None
|
||||
@@ -253,7 +275,12 @@ class KeyBindings:
|
||||
if not hasattr(self, "bindings"):
|
||||
self.initialize_keybindings()
|
||||
|
||||
value = self.bindings.get(f"keybinding_{self.game_status}", {}).get(action)
|
||||
value = None
|
||||
for section_name in self._binding_sections_for_action():
|
||||
value = self.bindings.get(section_name, {}).get(action)
|
||||
if value:
|
||||
break
|
||||
|
||||
if not value:
|
||||
return None
|
||||
|
||||
|
||||
+23
-3
@@ -494,9 +494,29 @@ class GameWindow:
|
||||
|
||||
if image := kwargs.get("image"):
|
||||
image_width, image_height = self.get_image_size(image)
|
||||
image_x = self.target_size[0] // 2 - image_width // 2
|
||||
self.draw_image(image_x - self.w_offset, content_y - self.h_offset, image, "win")
|
||||
content_y += image_height + max(14, panel_height // 30)
|
||||
image_scale = kwargs.get("image_scale")
|
||||
image_max_width = kwargs.get("image_max_width")
|
||||
image_max_height = kwargs.get("image_max_height")
|
||||
|
||||
scale_limit = 1.0
|
||||
if image_scale is not None:
|
||||
scale_limit = min(scale_limit, max(0.01, float(image_scale)))
|
||||
if image_max_width:
|
||||
scale_limit = min(scale_limit, float(image_max_width) / image_width)
|
||||
if image_max_height:
|
||||
scale_limit = min(scale_limit, float(image_max_height) / image_height)
|
||||
|
||||
render_width = max(1, int(round(image_width * scale_limit)))
|
||||
render_height = max(1, int(round(image_height * scale_limit)))
|
||||
image_x = self.target_size[0] // 2 - render_width // 2
|
||||
self.draw_image(
|
||||
image_x - self.w_offset,
|
||||
content_y - self.h_offset,
|
||||
image,
|
||||
"win",
|
||||
dest_size=(render_width, render_height),
|
||||
)
|
||||
content_y += render_height + max(14, panel_height // 30)
|
||||
|
||||
if subtitle := kwargs.get("subtitle"):
|
||||
subtitle_sprites = [
|
||||
|
||||
Reference in New Issue
Block a user