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
|
|
@ -100,7 +100,7 @@ enemy.sprite_index = 69 # E
|
|||
# UI setup
|
||||
ui = chain_test.children
|
||||
ui.append(grid)
|
||||
grid.position = (100, 100)
|
||||
grid.pos = (100, 100)
|
||||
grid.size = (600, 450)
|
||||
|
||||
title = mcrfpy.Caption(pos=(300, 20), text="Animation Chaining Test")
|
||||
|
|
@ -179,23 +179,21 @@ def update_camera(timer, runtime):
|
|||
def handle_input(key, state):
|
||||
global camera_follow
|
||||
|
||||
if state != "start":
|
||||
if state != mcrfpy.InputState.PRESSED:
|
||||
return
|
||||
|
||||
key = key.lower()
|
||||
|
||||
if key == "q":
|
||||
|
||||
if key == mcrfpy.Key.Q:
|
||||
sys.exit(0)
|
||||
elif key == "num1":
|
||||
elif key == mcrfpy.Key.NUM_1:
|
||||
animate_player()
|
||||
elif key == "num2":
|
||||
elif key == mcrfpy.Key.NUM_2:
|
||||
animate_enemy()
|
||||
elif key == "num3":
|
||||
elif key == mcrfpy.Key.NUM_3:
|
||||
animate_both()
|
||||
elif key == "c":
|
||||
elif key == mcrfpy.Key.C:
|
||||
camera_follow = not camera_follow
|
||||
info.text = f"Camera follow: {'ON' if camera_follow else 'OFF'}"
|
||||
elif key == "r":
|
||||
elif key == mcrfpy.Key.R:
|
||||
# Reset positions
|
||||
player.x, player.y = 2, 2
|
||||
enemy.x, enemy.y = 17, 12
|
||||
|
|
|
|||
|
|
@ -17,12 +17,12 @@ def test_click_callback_signature(pos, button, action):
|
|||
results.append(("on_click pos is Vector", False))
|
||||
print(f"FAIL: on_click receives {type(pos).__name__} instead of Vector: {pos}")
|
||||
|
||||
# Verify button and action types
|
||||
if isinstance(button, str) and isinstance(action, str):
|
||||
results.append(("on_click button/action are strings", True))
|
||||
# Verify button and action types (enums since #306)
|
||||
if isinstance(button, mcrfpy.MouseButton) and isinstance(action, mcrfpy.InputState):
|
||||
results.append(("on_click button/action are enums", True))
|
||||
print(f"PASS: button={button!r}, action={action!r}")
|
||||
else:
|
||||
results.append(("on_click button/action are strings", False))
|
||||
results.append(("on_click button/action are enums", False))
|
||||
print(f"FAIL: button={type(button).__name__}, action={type(action).__name__}")
|
||||
|
||||
# #230 - Hover callbacks now receive only (pos), not (pos, button, action)
|
||||
|
|
@ -112,7 +112,7 @@ print("\n--- Simulating callback calls ---")
|
|||
|
||||
# Test that the callbacks are set up correctly
|
||||
# on_click still takes (pos, button, action)
|
||||
test_click_callback_signature(mcrfpy.Vector(150, 150), "left", "start")
|
||||
test_click_callback_signature(mcrfpy.Vector(150, 150), mcrfpy.MouseButton.LEFT, mcrfpy.InputState.PRESSED)
|
||||
# #230 - Hover callbacks now take only (pos)
|
||||
test_on_enter_callback_signature(mcrfpy.Vector(100, 100))
|
||||
test_on_exit_callback_signature(mcrfpy.Vector(300, 300))
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ entity.sprite_index = 64 # @
|
|||
# UI setup
|
||||
ui = test_anim.children
|
||||
ui.append(grid)
|
||||
grid.position = (100, 100)
|
||||
grid.pos = (100, 100)
|
||||
grid.size = (450, 450) # 15 * 30 pixels per cell
|
||||
|
||||
# Title
|
||||
|
|
@ -160,23 +160,21 @@ def test_immediate_position():
|
|||
|
||||
# Input handler
|
||||
def handle_input(key, state):
|
||||
if state != "start":
|
||||
if state != mcrfpy.InputState.PRESSED:
|
||||
return
|
||||
|
||||
key = key.lower()
|
||||
|
||||
if key == "q":
|
||||
|
||||
if key == mcrfpy.Key.Q:
|
||||
print("Exiting test...")
|
||||
sys.exit(0)
|
||||
elif key == "space":
|
||||
elif key == mcrfpy.Key.SPACE:
|
||||
if not animating:
|
||||
start_animation()
|
||||
else:
|
||||
print("Animation already in progress!")
|
||||
elif key == "t":
|
||||
elif key == mcrfpy.Key.T:
|
||||
# Test immediate position change
|
||||
test_immediate_position()
|
||||
elif key == "r":
|
||||
elif key == mcrfpy.Key.R:
|
||||
# Reset position
|
||||
entity.x = 5
|
||||
entity.y = 5
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Test Key, MouseButton, and InputState enum functionality.
|
||||
|
||||
Tests the new input-related enums that provide type-safe alternatives to
|
||||
string-based key codes, mouse buttons, and event states.
|
||||
Tests the input-related enums that provide type-safe key codes,
|
||||
mouse buttons, and event states. Legacy string comparison was
|
||||
removed in #306 -- these enums now use standard IntEnum comparison.
|
||||
"""
|
||||
|
||||
import mcrfpy
|
||||
|
|
@ -10,7 +11,7 @@ import sys
|
|||
|
||||
|
||||
def test_key_enum():
|
||||
"""Test Key enum members and backwards compatibility."""
|
||||
"""Test Key enum members and int values."""
|
||||
print("Testing Key enum...")
|
||||
|
||||
# Test that enum exists and has expected members
|
||||
|
|
@ -23,24 +24,19 @@ def test_key_enum():
|
|||
assert int(mcrfpy.Key.A) == 0, "Key.A should be 0"
|
||||
assert int(mcrfpy.Key.ESCAPE) == 36, "Key.ESCAPE should be 36"
|
||||
|
||||
# Test backwards compatibility with legacy strings
|
||||
assert mcrfpy.Key.A == "A", "Key.A should equal 'A'"
|
||||
assert mcrfpy.Key.ESCAPE == "Escape", "Key.ESCAPE should equal 'Escape'"
|
||||
assert mcrfpy.Key.LEFT_SHIFT == "LShift", "Key.LEFT_SHIFT should equal 'LShift'"
|
||||
assert mcrfpy.Key.RIGHT_CONTROL == "RControl", "Key.RIGHT_CONTROL should equal 'RControl'"
|
||||
assert mcrfpy.Key.NUM_1 == "Num1", "Key.NUM_1 should equal 'Num1'"
|
||||
assert mcrfpy.Key.NUMPAD_5 == "Numpad5", "Key.NUMPAD_5 should equal 'Numpad5'"
|
||||
assert mcrfpy.Key.F12 == "F12", "Key.F12 should equal 'F12'"
|
||||
assert mcrfpy.Key.SPACE == "Space", "Key.SPACE should equal 'Space'"
|
||||
# Test enum self-comparison
|
||||
assert mcrfpy.Key.ESCAPE == mcrfpy.Key.ESCAPE, "Key.ESCAPE should equal itself"
|
||||
assert mcrfpy.Key.A != mcrfpy.Key.ESCAPE, "Key.A should not equal Key.ESCAPE"
|
||||
|
||||
# Test that enum name also matches
|
||||
assert mcrfpy.Key.ESCAPE == "ESCAPE", "Key.ESCAPE should also equal 'ESCAPE'"
|
||||
# Verify legacy string comparison is removed (#306)
|
||||
assert not (mcrfpy.Key.ESCAPE == "Escape"), "Legacy string comparison should be removed"
|
||||
assert not (mcrfpy.Key.A == "A"), "Legacy string comparison should be removed"
|
||||
|
||||
print(" All Key tests passed")
|
||||
|
||||
|
||||
def test_mouse_button_enum():
|
||||
"""Test MouseButton enum members and backwards compatibility."""
|
||||
"""Test MouseButton enum members."""
|
||||
print("Testing MouseButton enum...")
|
||||
|
||||
# Test that enum exists and has expected members
|
||||
|
|
@ -54,21 +50,19 @@ def test_mouse_button_enum():
|
|||
assert int(mcrfpy.MouseButton.RIGHT) == 1, "MouseButton.RIGHT should be 1"
|
||||
assert int(mcrfpy.MouseButton.MIDDLE) == 2, "MouseButton.MIDDLE should be 2"
|
||||
|
||||
# Test backwards compatibility with legacy strings
|
||||
assert mcrfpy.MouseButton.LEFT == "left", "MouseButton.LEFT should equal 'left'"
|
||||
assert mcrfpy.MouseButton.RIGHT == "right", "MouseButton.RIGHT should equal 'right'"
|
||||
assert mcrfpy.MouseButton.MIDDLE == "middle", "MouseButton.MIDDLE should equal 'middle'"
|
||||
assert mcrfpy.MouseButton.X1 == "x1", "MouseButton.X1 should equal 'x1'"
|
||||
assert mcrfpy.MouseButton.X2 == "x2", "MouseButton.X2 should equal 'x2'"
|
||||
# Test enum self-comparison
|
||||
assert mcrfpy.MouseButton.LEFT == mcrfpy.MouseButton.LEFT
|
||||
assert mcrfpy.MouseButton.LEFT != mcrfpy.MouseButton.RIGHT
|
||||
|
||||
# Test that enum name also matches
|
||||
assert mcrfpy.MouseButton.LEFT == "LEFT", "MouseButton.LEFT should also equal 'LEFT'"
|
||||
# Verify legacy string comparison is removed (#306)
|
||||
assert not (mcrfpy.MouseButton.LEFT == "left"), "Legacy string comparison should be removed"
|
||||
assert not (mcrfpy.MouseButton.RIGHT == "right"), "Legacy string comparison should be removed"
|
||||
|
||||
print(" All MouseButton tests passed")
|
||||
|
||||
|
||||
def test_input_state_enum():
|
||||
"""Test InputState enum members and backwards compatibility."""
|
||||
"""Test InputState enum members."""
|
||||
print("Testing InputState enum...")
|
||||
|
||||
# Test that enum exists and has expected members
|
||||
|
|
@ -80,13 +74,13 @@ def test_input_state_enum():
|
|||
assert int(mcrfpy.InputState.PRESSED) == 0, "InputState.PRESSED should be 0"
|
||||
assert int(mcrfpy.InputState.RELEASED) == 1, "InputState.RELEASED should be 1"
|
||||
|
||||
# Test backwards compatibility with legacy strings
|
||||
assert mcrfpy.InputState.PRESSED == "start", "InputState.PRESSED should equal 'start'"
|
||||
assert mcrfpy.InputState.RELEASED == "end", "InputState.RELEASED should equal 'end'"
|
||||
# Test enum self-comparison
|
||||
assert mcrfpy.InputState.PRESSED == mcrfpy.InputState.PRESSED
|
||||
assert mcrfpy.InputState.PRESSED != mcrfpy.InputState.RELEASED
|
||||
|
||||
# Test that enum name also matches
|
||||
assert mcrfpy.InputState.PRESSED == "PRESSED", "InputState.PRESSED should also equal 'PRESSED'"
|
||||
assert mcrfpy.InputState.RELEASED == "RELEASED", "InputState.RELEASED should also equal 'RELEASED'"
|
||||
# Verify legacy string comparison is removed (#306)
|
||||
assert not (mcrfpy.InputState.PRESSED == "start"), "Legacy string comparison should be removed"
|
||||
assert not (mcrfpy.InputState.RELEASED == "end"), "Legacy string comparison should be removed"
|
||||
|
||||
print(" All InputState tests passed")
|
||||
|
||||
|
|
|
|||
|
|
@ -70,81 +70,81 @@ def handle_key(key, action):
|
|||
"""Handle keyboard input for scene transitions."""
|
||||
global current_transition, transition_duration
|
||||
|
||||
if action != "start":
|
||||
if action != mcrfpy.InputState.PRESSED:
|
||||
return
|
||||
|
||||
current_scene = (mcrfpy.current_scene.name if mcrfpy.current_scene else None)
|
||||
|
||||
# Number keys set transition type
|
||||
keyselections = {
|
||||
"Num1": mcrfpy.Transition.FADE,
|
||||
"Num2": mcrfpy.Transition.SLIDE_LEFT,
|
||||
"Num3": mcrfpy.Transition.SLIDE_RIGHT,
|
||||
"Num4": mcrfpy.Transition.SLIDE_UP,
|
||||
"Num5": mcrfpy.Transition.SLIDE_DOWN,
|
||||
"Num6": mcrfpy.Transition.NONE
|
||||
mcrfpy.Key.NUM_1: mcrfpy.Transition.FADE,
|
||||
mcrfpy.Key.NUM_2: mcrfpy.Transition.SLIDE_LEFT,
|
||||
mcrfpy.Key.NUM_3: mcrfpy.Transition.SLIDE_RIGHT,
|
||||
mcrfpy.Key.NUM_4: mcrfpy.Transition.SLIDE_UP,
|
||||
mcrfpy.Key.NUM_5: mcrfpy.Transition.SLIDE_DOWN,
|
||||
mcrfpy.Key.NUM_6: mcrfpy.Transition.NONE
|
||||
}
|
||||
if key in keyselections:
|
||||
current_transition = keyselections[key]
|
||||
print(f"Transition set to: {current_transition}")
|
||||
#if key == "Num1":
|
||||
#if key == mcrfpy.Key.NUM_1:
|
||||
# current_transition = "fade"
|
||||
# print("Transition set to: fade")
|
||||
#elif key == "Num2":
|
||||
#elif key == mcrfpy.Key.NUM_2:
|
||||
# current_transition = "slide_left"
|
||||
# print("Transition set to: slide_left")
|
||||
#elif key == "Num3":
|
||||
#elif key == mcrfpy.Key.NUM_3:
|
||||
# current_transition = "slide_right"
|
||||
# print("Transition set to: slide_right")
|
||||
#elif key == "Num4":
|
||||
#elif key == mcrfpy.Key.NUM_4:
|
||||
# current_transition = "slide_up"
|
||||
# print("Transition set to: slide_up")
|
||||
#elif key == "Num5":
|
||||
#elif key == mcrfpy.Key.NUM_5:
|
||||
# current_transition = "slide_down"
|
||||
# print("Transition set to: slide_down")
|
||||
#elif key == "Num6":
|
||||
#elif key == mcrfpy.Key.NUM_6:
|
||||
# current_transition = None # Instant
|
||||
# print("Transition set to: instant")
|
||||
|
||||
# Letter keys change scene
|
||||
keytransitions = {
|
||||
"R": red_scene,
|
||||
"B": blue_scene,
|
||||
"G": green_scene,
|
||||
"M": menu_scene
|
||||
mcrfpy.Key.R: red_scene,
|
||||
mcrfpy.Key.B: blue_scene,
|
||||
mcrfpy.Key.G: green_scene,
|
||||
mcrfpy.Key.M: menu_scene
|
||||
}
|
||||
if key in keytransitions:
|
||||
if mcrfpy.current_scene != keytransitions[key]:
|
||||
keytransitions[key].activate(current_transition, transition_duration)
|
||||
#elif key == "R":
|
||||
#elif key == mcrfpy.Key.R:
|
||||
# if current_scene != "red_scene":
|
||||
# print(f"Transitioning to red_scene with {current_transition}")
|
||||
# if current_transition:
|
||||
# mcrfpy.setScene("red_scene", current_transition, transition_duration)
|
||||
# else:
|
||||
# red_scene.activate()
|
||||
#elif key == "B":
|
||||
#elif key == mcrfpy.Key.B:
|
||||
# if current_scene != "blue_scene":
|
||||
# print(f"Transitioning to blue_scene with {current_transition}")
|
||||
# if current_transition:
|
||||
# mcrfpy.setScene("blue_scene", current_transition, transition_duration)
|
||||
# else:
|
||||
# blue_scene.activate()
|
||||
#elif key == "G":
|
||||
#elif key == mcrfpy.Key.G:
|
||||
# if current_scene != "green_scene":
|
||||
# print(f"Transitioning to green_scene with {current_transition}")
|
||||
# if current_transition:
|
||||
# mcrfpy.setScene("green_scene", current_transition, transition_duration)
|
||||
# else:
|
||||
# green_scene.activate()
|
||||
#elif key == "M":
|
||||
#elif key == mcrfpy.Key.M:
|
||||
# if current_scene != "menu_scene":
|
||||
# print(f"Transitioning to menu_scene with {current_transition}")
|
||||
# if current_transition:
|
||||
# mcrfpy.setScene("menu_scene", current_transition, transition_duration)
|
||||
# else:
|
||||
# menu_scene.activate()
|
||||
elif key == "Escape":
|
||||
elif key == mcrfpy.Key.ESCAPE:
|
||||
print("Exiting...")
|
||||
sys.exit(0)
|
||||
|
||||
|
|
|
|||
|
|
@ -78,9 +78,9 @@ def create_demo():
|
|||
# Keyboard handler
|
||||
def handle_keys(scene_name, key):
|
||||
if not focus_mgr.handle_key(key):
|
||||
if key == "Tab":
|
||||
if key == mcrfpy.Key.TAB:
|
||||
focus_mgr.focus_next()
|
||||
elif key == "Escape":
|
||||
elif key == mcrfpy.Key.ESCAPE:
|
||||
print("\nFinal values:")
|
||||
for i, inp in enumerate(inputs):
|
||||
print(f" Field {i+1}: '{inp.get_text()}'")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue