extends TestCase ## A10: key bindings, rebinding and conflicts. func test_defaults_are_the_documented_keys_and_none_clash() -> void: var bindings: KeyBindings = KeyBindings.new() assert_eq(bindings.code_of("forward"), KEY_W) assert_eq(bindings.code_of("build"), KEY_B) assert_eq(bindings.code_of("fire"), -MOUSE_BUTTON_LEFT) assert_eq(bindings.code_of("slot_0"), KEY_1) assert_eq(bindings.conflicts(), [] as Array[String]) for action: Dictionary in KeyBindings.ACTIONS: assert_true(KeyBindings.is_valid_code(int(action["default"])), String(action["id"])) func test_rebinding_to_a_free_key_works() -> void: var bindings: KeyBindings = KeyBindings.new() assert_eq(bindings.bind("jump", KEY_F), "") assert_eq(bindings.code_of("jump"), KEY_F) assert_eq(bindings.bind("aim", -MOUSE_BUTTON_MIDDLE), "") assert_eq(bindings.action_using(-MOUSE_BUTTON_MIDDLE), "aim") func test_rebinding_to_a_used_key_is_refused_and_names_the_clash() -> void: var bindings: KeyBindings = KeyBindings.new() assert_eq(bindings.bind("jump", KEY_W), "forward") assert_eq(bindings.code_of("jump"), KEY_SPACE, "unchanged") assert_eq(bindings.bind("aim", -MOUSE_BUTTON_LEFT), "fire") # Binding an action to the key it already has is no clash. assert_eq(bindings.bind("forward", KEY_W), "") func test_escape_wheel_and_nonsense_cannot_be_bound() -> void: var bindings: KeyBindings = KeyBindings.new() assert_eq(bindings.bind("jump", KEY_ESCAPE), "reserved") assert_eq(bindings.bind("jump", -MOUSE_BUTTON_WHEEL_UP), "not an input") assert_eq(bindings.bind("jump", 0), "not an input") assert_eq(bindings.bind("fly", KEY_F), "unknown action") func test_a_hand_edited_clash_is_repaired_on_load() -> void: var config: ConfigFile = ConfigFile.new() config.set_value("keys", "jump", KEY_W) config.set_value("keys", "sprint", "shift") config.set_value("keys", "reload", KEY_ESCAPE) var bindings: KeyBindings = KeyBindings.read(config) assert_eq(bindings.code_of("forward"), KEY_W, "the first in the list keeps it") assert_eq(bindings.code_of("jump"), KEY_SPACE, "the clashing one goes back to its default") assert_eq(bindings.code_of("sprint"), KEY_SHIFT) assert_eq(bindings.code_of("reload"), KEY_R) assert_eq(bindings.conflicts(), [] as Array[String]) func test_events_find_their_action() -> void: var bindings: KeyBindings = KeyBindings.new() var key: InputEventKey = InputEventKey.new() key.physical_keycode = KEY_B key.pressed = true assert_eq(bindings.action_for_event(key), "build") key.echo = true assert_eq(bindings.action_for_event(key), "", "a held key's repeats don't act") var click: InputEventMouseButton = InputEventMouseButton.new() click.button_index = MOUSE_BUTTON_RIGHT click.pressed = true assert_eq(bindings.action_for_event(click), "aim") func test_client_input_follows_rebound_keys() -> void: var input: ClientInput = ClientInput.new() assert_eq(input.bindings.bind("build", KEY_Q), "") var key: InputEventKey = InputEventKey.new() key.physical_keycode = KEY_Q key.pressed = true input.handle(key, true) assert_true(input.build_mode, "Q now toggles build mode") key.physical_keycode = KEY_B input.handle(key, true) assert_true(input.build_mode, "B no longer does") func test_describe_names_keys_and_buttons() -> void: assert_eq(KeyBindings.describe(KEY_W), "W") assert_eq(KeyBindings.describe(-MOUSE_BUTTON_LEFT), "Left mouse") assert_eq(KeyBindings.describe(0), "None")