diff options
author | metamuffin <metamuffin@disroot.org> | 2025-09-15 16:44:46 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-09-15 16:44:46 +0200 |
commit | 2ab827a9975b59490a9f46a7d22b9fd387879c18 (patch) | |
tree | fb2375442b7d179250d6b8a14ea16b00123fbe12 /client | |
parent | 676d4dcb6c439677b91b1b5cbc0bf08f98e2dd9d (diff) | |
download | hurrycurry-2ab827a9975b59490a9f46a7d22b9fd387879c18.tar hurrycurry-2ab827a9975b59490a9f46a7d22b9fd387879c18.tar.bz2 hurrycurry-2ab827a9975b59490a9f46a7d22b9fd387879c18.tar.zst |
implement JSON InputEvent load+save
Diffstat (limited to 'client')
-rw-r--r-- | client/gui/menus/settings/input/input_setting.gd | 56 | ||||
-rw-r--r-- | client/gui/menus/settings/input/input_value_node.gd | 13 |
2 files changed, 57 insertions, 12 deletions
diff --git a/client/gui/menus/settings/input/input_setting.gd b/client/gui/menus/settings/input/input_setting.gd index 5617b26a..3431ff38 100644 --- a/client/gui/menus/settings/input/input_setting.gd +++ b/client/gui/menus/settings/input/input_setting.gd @@ -33,4 +33,58 @@ func create_row(): row.value_node.changed.connect(func(): Settings.write(key, row.value_node.value)) return row -func save(_d: Dictionary): pass # TODO +func save(d: Dictionary): + var value = Settings.read(key) + if event_arrays_equal(default, value): return + var out = [] + for ev in value: + if ev is InputEventJoypadButton: out.push_back(["joypad_button", ev.button_index]) + elif ev is InputEventJoypadMotion: out.push_back(["joypad_motion", ev.axis]) + elif ev is InputEventMouseButton: out.push_back(["mouse_button", ev.button_index]) + elif ev is InputEventKey: out.push_back(["key", ev.physical_keycode]) + d[key] = out + +func load(d: Dictionary): + if d.has(key) and typeof(d[key]) == typeof(default): + var out = [] + for ev in d[key]: + if ev[0] == "joypad_button": + var k = InputEventJoypadButton.new() + k.button_index = int(ev[1]) + out.push_back(k) + elif ev[0] == "joypad_motion": + var k = InputEventJoypadMotion.new() + k.axis = int(ev[1]) + out.push_back(k) + elif ev[0] == "mouse_button": + var k = InputEventMouseButton.new() + k.button_index = int(ev[1]) + out.push_back(k) + elif ev[0] == "key": + var k = InputEventKey.new() + k.physical_keycode = int(ev[1]) + out.push_back(k) + Settings.write_unchecked(key, out) + elif default != null: + Settings.write_unchecked(key, default) + +# TODO types: static func event_arrays_equal(a1: Array[InputEvent], a2: Array[InputEvent]) -> bool: +static func event_arrays_equal(a1, a2) -> bool: + if a1.size() != a2.size(): return false + for e1 in a1: + var has = false + for e2 in a2: + has = has or events_equal(e1, e2) + if not has: return false + return true + +static func events_equal(e1: InputEvent, e2: InputEvent) -> bool: + if e1 is InputEventKey and e2 is InputEventKey: + return e1.physical_keycode == e2.physical_keycode + elif e1 is InputEventMouseButton and e2 is InputEventMouseButton: + return e1.button_index == e2.button_index + elif e1 is InputEventJoypadButton and e2 is InputEventJoypadButton: + return e1.button_index == e2.button_index + elif e1 is InputEventJoypadMotion and e2 is InputEventJoypadMotion: + return e1.axis == e2.axis + return false diff --git a/client/gui/menus/settings/input/input_value_node.gd b/client/gui/menus/settings/input/input_value_node.gd index 7c718e25..29b53850 100644 --- a/client/gui/menus/settings/input/input_value_node.gd +++ b/client/gui/menus/settings/input/input_value_node.gd @@ -16,7 +16,7 @@ extends VBoxContainer class_name InputValueNode -var value: Array[InputEvent] = [] +var value = [] var listening := false signal changed() @@ -53,22 +53,13 @@ func _input(e: InputEvent): if e is InputEventKey or e is InputEventMouseButton or e is InputEventJoypadButton or e is InputEventJoypadMotion: # Check if key was already added for e2 in value: - if events_equal(e, e2): return + if InputSetting.events_equal(e, e2): return value.append(e) _on_add_pressed() update() changed.emit() -func events_equal(e1: InputEvent, e2: InputEvent) -> bool: - if e1 is InputEventKey and e2 is InputEventKey: - return e1.physical_keycode == e2.physical_keycode - if (e1 is InputEventMouseButton and e2 is InputEventMouseButton) or (e1 is InputEventJoypadButton and e2 is InputEventJoypadButton): - return e1.button_index == e2.button_index - if e1 is InputEventJoypadMotion and e2 is InputEventJoypadMotion: - return e1.axis == e2.axis - return false - func _on_add_pressed() -> void: listening = not listening add_button.text = tr("c.settings.input.press_any_key") if listening else add_text |