Remove legacy string enum comparisons from InputState/Key/MouseButton, closes #306

Removed custom __eq__/__ne__ that allowed comparing enums to legacy string
names (e.g., Key.ESCAPE == "Escape"). Removed _legacy_names dicts and
to_legacy_string() functions. Kept from_legacy_string() in PyKey.cpp as
it's used by C++ event dispatch. Updated ~50 Python test/demo/cookbook
files to use enum members instead of string comparisons. Also updates
grid.position -> grid.pos in files that had both types of changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
John McCardle 2026-04-09 22:19:02 -04:00
commit 6d5e99a114
52 changed files with 372 additions and 533 deletions

View file

@ -245,20 +245,20 @@ class ButtonDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
sys.exit(0)
elif key == "Num1" and len(self.buttons) > 0:
elif key == mcrfpy.Key.NUM_1 and len(self.buttons) > 0:
self.buttons[0].callback()
elif key == "Num2" and len(self.buttons) > 1:
elif key == mcrfpy.Key.NUM_2 and len(self.buttons) > 1:
self.buttons[1].callback()
elif key == "Num3" and len(self.buttons) > 2:
elif key == mcrfpy.Key.NUM_3 and len(self.buttons) > 2:
self.buttons[2].callback()
elif key == "Num4" and len(self.buttons) > 3:
elif key == mcrfpy.Key.NUM_4 and len(self.buttons) > 3:
self.buttons[3].callback()
elif key == "D":
elif key == mcrfpy.Key.D:
# Toggle disabled button
self.disabled_btn.enabled = not self.disabled_btn.enabled
if self.disabled_btn.enabled:

View file

@ -230,31 +230,31 @@ class ChoiceListDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
sys.exit(0)
# Get active list
active = self.lists[self.active_list_idx] if self.lists else None
if key == "Up" and active:
if key == mcrfpy.Key.UP and active:
active.navigate(-1)
elif key == "Down" and active:
elif key == mcrfpy.Key.DOWN and active:
active.navigate(1)
elif key == "Enter" and active:
elif key == mcrfpy.Key.ENTER and active:
active.confirm()
elif key == "Tab":
elif key == mcrfpy.Key.TAB:
# Switch active list
self.active_list_idx = (self.active_list_idx + 1) % len(self.lists)
self._update_active_indicator()
elif key == "A":
elif key == mcrfpy.Key.A:
# Add item to dynamic list
self.add_counter += 1
self.dynamic_list.add_choice(f"New Item {self.add_counter}")
self.dynamic_info.text = f"Items: {len(self.dynamic_list.choices)}"
elif key == "R":
elif key == mcrfpy.Key.R:
# Remove selected from dynamic list
if len(self.dynamic_list.choices) > 1:
self.dynamic_list.remove_choice(self.dynamic_list.selected_index)

View file

@ -187,7 +187,7 @@ class ClickPickupDemo:
def _on_grid_click(self, pos, button, action):
"""Handle grid click."""
if action != "start":
if action != mcrfpy.InputState.PRESSED:
return
cell = self._get_grid_cell(pos)
@ -196,13 +196,13 @@ class ClickPickupDemo:
x, y = cell
if button == "right":
if button == mcrfpy.MouseButton.RIGHT:
# Cancel pickup
if self.held_entity:
self._cancel_pickup()
return
if button != "left":
if button != mcrfpy.MouseButton.LEFT:
return
if self.held_entity is None:
@ -334,10 +334,10 @@ class ClickPickupDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
if self.held_entity:
self._cancel_pickup()
return

View file

@ -234,9 +234,9 @@ class DragDropFrameDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
# Return to cookbook menu or exit
try:
from cookbook_main import main

View file

@ -142,7 +142,7 @@ class GridDragDropDemo:
def _on_grid_click(self, pos, button, action):
"""Handle grid click for drag start/end."""
if button != "left":
if button != mcrfpy.MouseButton.LEFT:
return
# Convert screen pos to grid cell
@ -154,7 +154,7 @@ class GridDragDropDemo:
if not (0 <= grid_x < grid_w and 0 <= grid_y < grid_h):
return
if action == "start":
if action == mcrfpy.InputState.PRESSED:
# Start drag if there's an entity here
entity = self._get_entity_at(grid_x, grid_y)
if entity:
@ -166,7 +166,7 @@ class GridDragDropDemo:
# Highlight start cell yellow
self.color_layer.set((grid_x, grid_y), (255, 255, 100, 200))
elif action == "end":
elif action == mcrfpy.InputState.RELEASED:
if self.dragging_entity:
# Drop the entity
target_cell = (grid_x, grid_y)
@ -224,9 +224,9 @@ class GridDragDropDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
# Cancel any drag in progress
if self.dragging_entity and self.drag_start_cell:
self.dragging_entity.grid_pos = self.drag_start_cell

View file

@ -275,15 +275,15 @@ class StatBarDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
sys.exit(0)
# Number keys to modify bars
bar_keys = ['hp', 'mp', 'stamina', 'xp']
key_map = {"Num1": 0, "Num2": 1, "Num3": 2, "Num4": 3}
key_map = {mcrfpy.Key.NUM_1: 0, mcrfpy.Key.NUM_2: 1, mcrfpy.Key.NUM_3: 2, mcrfpy.Key.NUM_4: 3}
if key in key_map:
idx = key_map[key]
@ -293,11 +293,11 @@ class StatBarDemo:
bar.set_value(bar.current - 10, animate=True)
self.status.text = f"Status: Decreased {bar_keys[idx].upper()}"
elif key == "F":
elif key == mcrfpy.Key.F:
self.flash_bar.flash()
self.status.text = "Status: Flash effect triggered!"
elif key == "R":
elif key == mcrfpy.Key.R:
# Reset all bars
self.bars['hp'].set_value(75, 100, animate=True)
self.bars['mp'].set_value(50, 80, animate=True)

View file

@ -182,18 +182,18 @@ class TextBoxDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
sys.exit(0)
elif key == "Num1":
elif key == mcrfpy.Key.NUM_1:
# Start typewriter animation
self.typewriter_box.on_complete = self.on_typewriter_complete
self.typewriter_box.set_text(self.sample_text, animate=True)
self.completion_label.text = "Status: Playing..."
self.completion_label.fill_color = mcrfpy.Color(200, 200, 100)
elif key == "Num2":
elif key == mcrfpy.Key.NUM_2:
# Change instant text
texts = [
"This text appeared instantly. Press 2 to change it to different content.",
@ -203,17 +203,17 @@ class TextBoxDemo:
]
import random
self.instant_box.set_text(random.choice(texts), animate=False)
elif key == "Num3":
elif key == mcrfpy.Key.NUM_3:
# Skip animation
self.typewriter_box.skip_animation()
self.completion_label.text = "Status: Skipped"
self.completion_label.fill_color = mcrfpy.Color(150, 150, 150)
elif key == "Num4":
elif key == mcrfpy.Key.NUM_4:
# Clear text
self.typewriter_box.clear()
self.completion_label.text = "Status: Cleared"
self.completion_label.fill_color = mcrfpy.Color(150, 150, 150)
elif key == "D":
elif key == mcrfpy.Key.D:
# Cycle dialogue
self.dialogue_index = (self.dialogue_index + 1) % len(self.dialogues)
speaker, text = self.dialogues[self.dialogue_index]

View file

@ -163,32 +163,32 @@ class ToastDemo:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
if key == "Escape":
if key == mcrfpy.Key.ESCAPE:
sys.exit(0)
elif key == "Num1":
elif key == mcrfpy.Key.NUM_1:
self.toast_count += 1
self.toasts.show(f"Default notification #{self.toast_count}")
self.update_stats()
elif key == "Num2":
elif key == mcrfpy.Key.NUM_2:
self.toast_count += 1
self.toasts.show_success("Operation completed successfully!")
self.update_stats()
elif key == "Num3":
elif key == mcrfpy.Key.NUM_3:
self.toast_count += 1
self.toasts.show_error("An error occurred!")
self.update_stats()
elif key == "Num4":
elif key == mcrfpy.Key.NUM_4:
self.toast_count += 1
self.toasts.show_warning("Warning: Low health!")
self.update_stats()
elif key == "Num5":
elif key == mcrfpy.Key.NUM_5:
self.toast_count += 1
self.toasts.show_info("New quest available")
self.update_stats()
elif key == "S":
elif key == mcrfpy.Key.S:
# Spam multiple toasts
messages = [
"Game saved!",
@ -201,7 +201,7 @@ class ToastDemo:
self.toast_count += 1
self.toasts.show(msg)
self.update_stats()
elif key == "C":
elif key == mcrfpy.Key.C:
self.toasts.dismiss_all()
self.update_stats()