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:
parent
354faca838
commit
6d5e99a114
52 changed files with 372 additions and 533 deletions
|
|
@ -146,21 +146,24 @@ class DemoRunner:
|
|||
self.create_menu()
|
||||
|
||||
def handle_key(key, state):
|
||||
if state != "start":
|
||||
if state != mcrfpy.InputState.PRESSED:
|
||||
return
|
||||
|
||||
# Number keys 1-9 for direct screen access
|
||||
if key in [f"Num{n}" for n in "123456789"]:
|
||||
idx = int(key[-1]) - 1
|
||||
_num_key_map = {mcrfpy.Key.NUM_1: 0, mcrfpy.Key.NUM_2: 1, mcrfpy.Key.NUM_3: 2,
|
||||
mcrfpy.Key.NUM_4: 3, mcrfpy.Key.NUM_5: 4, mcrfpy.Key.NUM_6: 5,
|
||||
mcrfpy.Key.NUM_7: 6, mcrfpy.Key.NUM_8: 7, mcrfpy.Key.NUM_9: 8}
|
||||
if key in _num_key_map:
|
||||
idx = _num_key_map[key]
|
||||
if idx < len(self.screens):
|
||||
mcrfpy.setScene(self.screens[idx].scene_name)
|
||||
|
||||
# ESC returns to menu
|
||||
elif key == "Escape":
|
||||
elif key == mcrfpy.Key.ESCAPE:
|
||||
menu.activate()
|
||||
|
||||
# Q quits
|
||||
elif key == "Q":
|
||||
elif key == mcrfpy.Key.Q:
|
||||
sys.exit(0)
|
||||
|
||||
# Register keyboard handler on menu scene
|
||||
|
|
|
|||
|
|
@ -162,18 +162,18 @@ def on_keypress(key, state):
|
|||
"""Handle keyboard input"""
|
||||
global patrol_paused
|
||||
|
||||
if state != "start":
|
||||
if state != mcrfpy.InputState.PRESSED:
|
||||
return
|
||||
|
||||
if key == "R":
|
||||
if key == mcrfpy.Key.R:
|
||||
reset_vision()
|
||||
elif key == "Space":
|
||||
elif key == mcrfpy.Key.SPACE:
|
||||
patrol_paused = not patrol_paused
|
||||
if patrol_paused:
|
||||
update_status("Status: PAUSED")
|
||||
else:
|
||||
update_status("Status: Patrolling")
|
||||
elif key == "Q":
|
||||
elif key == mcrfpy.Key.Q:
|
||||
mcrfpy.current_scene = None
|
||||
|
||||
def reset_vision():
|
||||
|
|
|
|||
|
|
@ -31,14 +31,14 @@ class ModifierTracker:
|
|||
self.ctrl = False
|
||||
self.alt = False
|
||||
|
||||
def update(self, key: str, action: str):
|
||||
def update(self, key, action):
|
||||
"""Call this from your key handler to update modifier state."""
|
||||
if key in ("LShift", "RShift"):
|
||||
self.shift = (action == "start")
|
||||
elif key in ("LControl", "RControl"):
|
||||
self.ctrl = (action == "start")
|
||||
elif key in ("LAlt", "RAlt"):
|
||||
self.alt = (action == "start")
|
||||
if key in (mcrfpy.Key.LEFT_SHIFT, mcrfpy.Key.RIGHT_SHIFT):
|
||||
self.shift = (action == mcrfpy.InputState.PRESSED)
|
||||
elif key in (mcrfpy.Key.LEFT_CONTROL, mcrfpy.Key.RIGHT_CONTROL):
|
||||
self.ctrl = (action == mcrfpy.InputState.PRESSED)
|
||||
elif key in (mcrfpy.Key.LEFT_ALT, mcrfpy.Key.RIGHT_ALT):
|
||||
self.alt = (action == mcrfpy.InputState.PRESSED)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
|
|
@ -163,16 +163,16 @@ class FocusManager:
|
|||
self.modifiers.update(key, action)
|
||||
|
||||
# Only process on key press, not release (key repeat sends multiple "start")
|
||||
if action != "start":
|
||||
if action != mcrfpy.InputState.PRESSED:
|
||||
return False
|
||||
|
||||
# Global: Escape closes modals
|
||||
if key == "Escape":
|
||||
if key == mcrfpy.Key.ESCAPE:
|
||||
if self.pop_modal():
|
||||
return True
|
||||
|
||||
# Global: Tab cycles focus
|
||||
if key == "Tab":
|
||||
if key == mcrfpy.Key.TAB:
|
||||
direction = -1 if self.modifiers.shift else 1
|
||||
self.cycle(direction)
|
||||
return True
|
||||
|
|
@ -251,7 +251,7 @@ class FocusableGrid:
|
|||
|
||||
def _on_click(self, x, y, button, action):
|
||||
"""Handle click to focus this grid."""
|
||||
if self._focus_manager and action == "start":
|
||||
if self._focus_manager and action == mcrfpy.InputState.PRESSED:
|
||||
self._focus_manager.focus(self._focus_index)
|
||||
|
||||
def _update_player_display(self):
|
||||
|
|
@ -272,13 +272,13 @@ class FocusableGrid:
|
|||
self.outline_frame.outline_color = FocusManager.UNFOCUS_COLOR
|
||||
self.outline_frame.outline = FocusManager.UNFOCUS_OUTLINE
|
||||
|
||||
def handle_key(self, key: str, action: str) -> bool:
|
||||
def handle_key(self, key, action) -> bool:
|
||||
"""Handle WASD movement."""
|
||||
moves = {
|
||||
"W": (0, -1), "Up": (0, -1),
|
||||
"A": (-1, 0), "Left": (-1, 0),
|
||||
"S": (0, 1), "Down": (0, 1),
|
||||
"D": (1, 0), "Right": (1, 0),
|
||||
mcrfpy.Key.W: (0, -1), mcrfpy.Key.UP: (0, -1),
|
||||
mcrfpy.Key.A: (-1, 0), mcrfpy.Key.LEFT: (-1, 0),
|
||||
mcrfpy.Key.S: (0, 1), mcrfpy.Key.DOWN: (0, 1),
|
||||
mcrfpy.Key.D: (1, 0), mcrfpy.Key.RIGHT: (1, 0),
|
||||
}
|
||||
|
||||
if key in moves:
|
||||
|
|
@ -373,7 +373,7 @@ class TextInputWidget:
|
|||
|
||||
def _on_click(self, x, y, button, action):
|
||||
"""Handle click to focus."""
|
||||
if self._focus_manager and action == "start":
|
||||
if self._focus_manager and action == mcrfpy.InputState.PRESSED:
|
||||
self._focus_manager.focus(self._focus_index)
|
||||
|
||||
def _update_display(self):
|
||||
|
|
@ -404,7 +404,7 @@ class TextInputWidget:
|
|||
self.cursor.visible = False
|
||||
self._update_display()
|
||||
|
||||
def handle_key(self, key: str, action: str) -> bool:
|
||||
def handle_key(self, key, action) -> bool:
|
||||
"""Handle text input and editing keys."""
|
||||
if not self.focused:
|
||||
return False
|
||||
|
|
@ -412,27 +412,27 @@ class TextInputWidget:
|
|||
old_text = self.text
|
||||
handled = True
|
||||
|
||||
if key == "BackSpace":
|
||||
if key == mcrfpy.Key.BACKSPACE:
|
||||
if self.cursor_pos > 0:
|
||||
self.text = self.text[:self.cursor_pos-1] + self.text[self.cursor_pos:]
|
||||
self.cursor_pos -= 1
|
||||
elif key == "Delete":
|
||||
elif key == mcrfpy.Key.DELETE:
|
||||
if self.cursor_pos < len(self.text):
|
||||
self.text = self.text[:self.cursor_pos] + self.text[self.cursor_pos+1:]
|
||||
elif key == "Left":
|
||||
elif key == mcrfpy.Key.LEFT:
|
||||
self.cursor_pos = max(0, self.cursor_pos - 1)
|
||||
elif key == "Right":
|
||||
elif key == mcrfpy.Key.RIGHT:
|
||||
self.cursor_pos = min(len(self.text), self.cursor_pos + 1)
|
||||
elif key == "Home":
|
||||
elif key == mcrfpy.Key.HOME:
|
||||
self.cursor_pos = 0
|
||||
elif key == "End":
|
||||
elif key == mcrfpy.Key.END:
|
||||
self.cursor_pos = len(self.text)
|
||||
elif key in ("Return", "Tab"):
|
||||
elif key in (mcrfpy.Key.ENTER, mcrfpy.Key.TAB):
|
||||
# Don't consume - let focus manager handle
|
||||
handled = False
|
||||
elif len(key) == 1 and key.isprintable():
|
||||
# Insert character
|
||||
self.text = self.text[:self.cursor_pos] + key + self.text[self.cursor_pos:]
|
||||
elif len(key.name) == 1 and key.name.isprintable():
|
||||
# Insert character (key.name is "A"-"Z" for letter keys)
|
||||
self.text = self.text[:self.cursor_pos] + key.name.lower() + self.text[self.cursor_pos:]
|
||||
self.cursor_pos += 1
|
||||
else:
|
||||
handled = False
|
||||
|
|
@ -509,7 +509,7 @@ class MenuIcon:
|
|||
if not self._focus_manager:
|
||||
return
|
||||
|
||||
if action == "start":
|
||||
if action == mcrfpy.InputState.PRESSED:
|
||||
# If already focused, activate; otherwise just focus
|
||||
if self._focus_manager.focus_index == self._focus_index:
|
||||
self._activate()
|
||||
|
|
@ -535,9 +535,9 @@ class MenuIcon:
|
|||
self.frame.fill_color = mcrfpy.Color(60, 60, 80)
|
||||
self.tooltip_caption.visible = False
|
||||
|
||||
def handle_key(self, key: str, action: str) -> bool:
|
||||
def handle_key(self, key, action) -> bool:
|
||||
"""Handle activation keys."""
|
||||
if key in ("Space", "Return"):
|
||||
if key in (mcrfpy.Key.SPACE, mcrfpy.Key.ENTER):
|
||||
self._activate()
|
||||
return True
|
||||
return False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue