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

@ -183,24 +183,23 @@ class Calculator:
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:
# Switch to game scene
mcrfpy.current_scene = game_scene
return
# Map keys to buttons
key_map = {
"Num0": "0", "Num1": "1", "Num2": "2", "Num3": "3",
"Num4": "4", "Num5": "5", "Num6": "6", "Num7": "7",
"Num8": "8", "Num9": "9",
"Period": ".", "Add": "+", "Subtract": "-",
"Multiply": "*", "Divide": "/",
"Enter": "=", "Return": "=",
"C": "C", "Backspace": "DEL",
"LParen": "(", "RParen": ")",
mcrfpy.Key.NUM_0: "0", mcrfpy.Key.NUM_1: "1", mcrfpy.Key.NUM_2: "2", mcrfpy.Key.NUM_3: "3",
mcrfpy.Key.NUM_4: "4", mcrfpy.Key.NUM_5: "5", mcrfpy.Key.NUM_6: "6", mcrfpy.Key.NUM_7: "7",
mcrfpy.Key.NUM_8: "8", mcrfpy.Key.NUM_9: "9",
mcrfpy.Key.PERIOD: ".", mcrfpy.Key.ADD: "+", mcrfpy.Key.SUBTRACT: "-",
mcrfpy.Key.MULTIPLY: "*", mcrfpy.Key.DIVIDE: "/",
mcrfpy.Key.ENTER: "=",
mcrfpy.Key.C: "C", mcrfpy.Key.BACKSPACE: "DEL",
}
if key in key_map:
@ -271,10 +270,10 @@ class GamePlaceholder:
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:
# Switch to calculator
calculator.activate()

View file

@ -444,25 +444,26 @@ class DialogueSystem:
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 in ("Num1", "Num2", "Num3", "Num4"):
idx = int(key[-1]) - 1
elif key in (mcrfpy.Key.NUM_1, mcrfpy.Key.NUM_2, mcrfpy.Key.NUM_3, mcrfpy.Key.NUM_4):
_num_idx = {mcrfpy.Key.NUM_1: 0, mcrfpy.Key.NUM_2: 1, mcrfpy.Key.NUM_3: 2, mcrfpy.Key.NUM_4: 3}
idx = _num_idx[key]
if idx < len(self.choice_list.choices):
self.choice_list.set_selected(idx)
self.choice_list.confirm()
elif key == "Up":
elif key == mcrfpy.Key.UP:
self.choice_list.navigate(-1)
elif key == "Down":
elif key == mcrfpy.Key.DOWN:
self.choice_list.navigate(1)
elif key == "Enter":
elif key == mcrfpy.Key.ENTER:
self.choice_list.confirm()
elif key == "Space":
elif key == mcrfpy.Key.SPACE:
self.dialogue_box.skip_animation()
elif key == "R":
elif key == mcrfpy.Key.R:
# Restart
self.npc.mood = "neutral"
self.npc.trust = 50

View file

@ -396,10 +396,10 @@ class ShopDemo:
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.manager.held_item:
self.manager.cancel_pickup()
self.tooltip.text = "Cancelled"

View file

@ -206,7 +206,7 @@ class CookbookLauncher:
def on_key(self, key, state):
"""Handle keyboard input."""
if state != "start":
if state != mcrfpy.InputState.PRESSED:
return
category = self.categories[self.selected_category]

View file

@ -377,22 +377,22 @@ class AnimationDemo:
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.run_chain_demo()
elif key == "Num2":
elif key == mcrfpy.Key.NUM_2:
self.run_group_demo()
elif key == "Num3":
elif key == mcrfpy.Key.NUM_3:
self.run_callback_demo()
elif key == "Num4":
elif key == mcrfpy.Key.NUM_4:
self.run_loop_demo()
elif key == "Num5":
elif key == mcrfpy.Key.NUM_5:
self.run_combined_demo()
elif key == "R":
elif key == mcrfpy.Key.R:
self.reset_all()
def activate(self):

View file

@ -365,38 +365,39 @@ class RotationDemo:
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 == "Left":
elif key == mcrfpy.Key.LEFT:
# Rotate left (counter-clockwise)
name, element = self.elements[self.selected]
try:
element.rotation = (element.rotation - 15) % 360
except AttributeError:
pass
elif key == "Right":
elif key == mcrfpy.Key.RIGHT:
# Rotate right (clockwise)
name, element = self.elements[self.selected]
try:
element.rotation = (element.rotation + 15) % 360
except AttributeError:
pass
elif key == "Up":
elif key == mcrfpy.Key.UP:
self.rotation_speed = min(180, self.rotation_speed + 15)
self.speed_label.text = f"Speed: {self.rotation_speed}°/sec"
elif key == "Down":
elif key == mcrfpy.Key.DOWN:
self.rotation_speed = max(15, self.rotation_speed - 15)
self.speed_label.text = f"Speed: {self.rotation_speed}°/sec"
elif key in ("Num1", "Num2", "Num3", "Num4"):
self.selected = int(key[-1]) - 1
elif key in (mcrfpy.Key.NUM_1, mcrfpy.Key.NUM_2, mcrfpy.Key.NUM_3, mcrfpy.Key.NUM_4):
_num_idx = {mcrfpy.Key.NUM_1: 0, mcrfpy.Key.NUM_2: 1, mcrfpy.Key.NUM_3: 2, mcrfpy.Key.NUM_4: 3}
self.selected = _num_idx[key]
if self.selected < len(self.elements):
self.selected_label.text = f"Selected: {self.elements[self.selected][0]}"
elif key == "O":
elif key == mcrfpy.Key.O:
self._cycle_origin()
elif key == "A":
elif key == mcrfpy.Key.A:
self.auto_rotate = not self.auto_rotate
if self.auto_rotate:
self.auto_label.text = "Auto-rotate: On"
@ -404,7 +405,7 @@ class RotationDemo:
else:
self.auto_label.text = "Auto-rotate: Off"
self.auto_label.fill_color = mcrfpy.Color(200, 100, 100)
elif key == "R":
elif key == mcrfpy.Key.R:
# Reset all rotations
for name, element in self.elements:
try:

View file

@ -296,20 +296,21 @@ class ShaderDemo:
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 == "Space":
elif key == mcrfpy.Key.SPACE:
self.toggle_shaders()
elif key == "R":
elif key == mcrfpy.Key.R:
# Re-enable all shaders
self.shaders_enabled = False
self.toggle_shaders()
elif key in ("Num1", "Num2", "Num3", "Num4"):
elif key in (mcrfpy.Key.NUM_1, mcrfpy.Key.NUM_2, mcrfpy.Key.NUM_3, mcrfpy.Key.NUM_4):
# Focus on specific shader (could zoom in)
idx = int(key[-1]) - 1
_num_idx = {mcrfpy.Key.NUM_1: 0, mcrfpy.Key.NUM_2: 1, mcrfpy.Key.NUM_3: 2, mcrfpy.Key.NUM_4: 3}
idx = _num_idx[key]
if idx < len(self.shader_frames):
self.status.text = f"Focused: Shader {idx + 1}"

View file

@ -102,13 +102,13 @@ class Button:
if not self._enabled:
return
if button == "left":
if action == "start":
if button == mcrfpy.MouseButton.LEFT:
if action == mcrfpy.InputState.PRESSED:
self.is_pressed = True
self.frame.fill_color = self.press_color
# Animate a subtle press effect
self._animate_press()
elif action == "end":
elif action == mcrfpy.InputState.RELEASED:
self.is_pressed = False
# Restore hover or normal state
if self.is_hovered:

View file

@ -123,7 +123,7 @@ class ChoiceList:
idx = i # Capture index in closure
def make_click_handler(index):
def handler(pos, button, action):
if button == "left" and action == "end":
if button == mcrfpy.MouseButton.LEFT and action == mcrfpy.InputState.RELEASED:
self.set_selected(index)
if self.on_select:
self.on_select(index, self._choices[index])

View file

@ -114,7 +114,7 @@ class GridContainer:
# Set up event handlers
def make_click(cx, cy):
def handler(pos, button, action):
if button == "left" and action == "end":
if button == mcrfpy.MouseButton.LEFT and action == mcrfpy.InputState.RELEASED:
self._on_cell_clicked(cx, cy)
return handler

View file

@ -138,7 +138,7 @@ class ItemSlot(mcrfpy.Frame):
return item.slot_type == self.slot_type
def _on_click(self, pos, button, action):
if action != "start" or button != "left":
if action != mcrfpy.InputState.PRESSED or button != mcrfpy.MouseButton.LEFT:
return
if self.manager:
self.manager.handle_slot_click(self.slot_name)
@ -274,14 +274,14 @@ class ItemManager:
def _on_grid_click(self, grid_name, pos, button, action):
"""Handle click on a registered grid."""
if action != "start":
if action != mcrfpy.InputState.PRESSED:
return
if button == "right":
if button == mcrfpy.MouseButton.RIGHT:
self.cancel_pickup()
return
if button != "left":
if button != mcrfpy.MouseButton.LEFT:
return
grid, item_map, color_layer = self.grids[grid_name]

View file

@ -194,7 +194,7 @@ class Modal:
def make_click(cb):
def handler(pos, button, action):
if button == "left" and action == "end" and cb:
if button == mcrfpy.MouseButton.LEFT and action == mcrfpy.InputState.RELEASED and cb:
cb()
return handler
@ -208,7 +208,7 @@ class Modal:
def _on_overlay_click(self, pos, button, action):
"""Handle clicks on overlay (outside modal)."""
# Check if click is outside modal
if button == "left" and action == "end":
if button == mcrfpy.MouseButton.LEFT and action == mcrfpy.InputState.RELEASED:
mx, my = self.modal_frame.x, self.modal_frame.y
mw, mh = self.modal_frame.w, self.modal_frame.h
px, py = pos.x, pos.y

View file

@ -138,7 +138,7 @@ class ScrollableList:
# Set up click handler
def make_click_handler(index):
def handler(pos, button, action):
if button == "left" and action == "end":
if button == mcrfpy.MouseButton.LEFT and action == mcrfpy.InputState.RELEASED:
self.select(index)
return handler

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()