started work on multiple input bindinghs, but is very shit, so need to rewrite how these are stored

This commit is contained in:
JHDev2006
2025-09-29 13:52:10 +01:00
parent 0a5e2ea211
commit 00015d2650
5 changed files with 108 additions and 62 deletions

View File

@@ -65,22 +65,35 @@ func load_inputs() -> void:
for action_name in i.action_names:
var action = action_name
var value = Settings.file[[\"keyboard\", \"controller\"][type]].get(action, null)
var event: InputEvent = null
if value == null:
continue
if type == 0:
event = InputEventKey.new()
event.keycode = OS.find_keycode_from_string(value)
elif type == 1:
if value is String:
var array = value.split(\",\")
event = InputEventJoypadMotion.new()
event.axis = int(array[0])
event.axis_value = int(array[1])
elif value is int:
event = InputEventJoypadButton.new()
event.button_index = value
i.map_event_to_action(event)
if value is Array:
var idx := 0
for x in value:
bind_value_to_event(i, x, idx)
idx += 1
else:
bind_value_to_event(i, value, 0)
func bind_value_to_event(input_node, value, idx := 0) -> void:
var event: InputEvent = null
if value is String:
if value == \"\":
input_node.map_event_to_action(null, idx)
return
if type == 0:
event = InputEventKey.new()
event.keycode = OS.find_keycode_from_string(value)
elif type == 1:
if value is String:
var array = value.split(\",\")
event = InputEventJoypadMotion.new()
event.axis = int(array[0])
event.axis_value = int(array[1])
elif value is int:
event = InputEventJoypadButton.new()
event.button_index = value
input_node.map_event_to_action(event, idx)
func update_starting_values() -> void:
load_inputs()
@@ -88,20 +101,33 @@ func update_starting_values() -> void:
func save_inputs() -> void:
for i in actions:
var event = null
var events := []
if i.contains(\"ui\") or i == \"pause\":
event = InputMap.action_get_events(i)[type]
event = InputMap.action_get_events(i)
else:
event = InputMap.action_get_events(i + \"_0\")[type]
var rep
if event is InputEventKey:
rep = OS.get_keycode_string(event.keycode)
elif event is InputEventJoypadButton:
rep = event.button_index
elif event is InputEventJoypadMotion:
rep = str(event.axis) + \",\" + str(event.axis_value)
else:
pass
Settings.file[[\"keyboard\", \"controller\"][type]][i] = rep
event = InputMap.action_get_events(i + \"_0\")
var size = event.size()
if type == 0:
if size > 2:
events = [get_str_for_event(event[0]), get_str_for_event(event[2])]
else:
events = [get_str_for_event(event[0])]
elif type == 1:
if size > 3:
events = [get_str_for_event(event[1]), get_str_for_event(event[3])]
else:
events = [get_str_for_event(event[1])]
Settings.file[[\"keyboard\", \"controller\"][type]][i] = events
func get_str_for_event(event: InputEvent):
var rep = \"\"
if event is InputEventKey:
rep = OS.get_keycode_string(event.keycode)
elif event is InputEventJoypadButton:
rep = event.button_index
elif event is InputEventJoypadMotion:
rep = str(event.axis) + \",\" + str(event.axis_value)
return rep
"
[sub_resource type="StyleBoxLine" id="StyleBoxLine_k6yev"]

View File

@@ -32,6 +32,7 @@ func handle_movement(delta: float) -> void:
move_and_slide()
func destroy() -> void:
await get_tree().physics_frame
AudioManager.play_sfx("icicle_break", global_position)
summon_particles()
queue_free()

View File

@@ -41,9 +41,9 @@ var file := {
},
"controller":
{
"jump": 0,
"run": 2,
"action": 2,
"jump": [0, 1],
"run": [2, 3],
"action": [2, 3],
"move_left": "0,-1",
"move_right": "0,1",
"move_up": "1,-1",

View File

@@ -22,7 +22,9 @@ var can_remap := true
var current_device_brand := 0
var input_event: InputEvent = null
var current_binding_idx := 0
var input_events: Array[InputEvent] = [null, null]
const button_id_translation := [
["A", "B", ""],
@@ -52,12 +54,25 @@ func _process(_delta: float) -> void:
func update_value() -> void:
$Title.text = tr(title) + ":"
$Value.text = get_event_string(input_event) if not awaiting_input else "Press Any..."
if awaiting_input:
$Value.text = "Press Any..."
else:
if current_binding_idx == 0:
$Value.text = "(" + get_event_string(input_events[0]) + "), " + get_event_string(input_events[1]) + " "
else:
$Value.text = " " + get_event_string(input_events[0]) + " ,(" + get_event_string(input_events[1]) + ")"
func handle_inputs() -> void:
if selected and can_remap:
if Input.is_action_just_pressed("ui_accept"):
begin_remap()
if can_remap:
if selected:
if Input.is_action_just_pressed("ui_accept"):
begin_remap()
if Input.is_action_just_pressed("ui_right"):
current_binding_idx = 1
update_value()
elif Input.is_action_just_pressed("ui_left"):
current_binding_idx = 0
update_value()
func begin_remap() -> void:
$Timer.stop()
@@ -81,24 +96,30 @@ func _input(event: InputEvent) -> void:
#return
if type == 0 and event is InputEventKey:
map_event_to_action(event)
map_event_to_action(event, current_binding_idx)
elif type == 1 and (event is InputEventJoypadButton or event is InputEventJoypadMotion):
if event is InputEventJoypadMotion:
event.axis_value = sign(event.axis_value)
map_event_to_action(event)
map_event_to_action(event, current_binding_idx)
func map_event_to_action(event) -> void:
func map_event_to_action(event, idx := 0) -> void:
for action_name in action_names:
var action = action_name
if action.contains("ui_") == false and action != "pause":
action = action_name + "_" + str(player_idx)
var events = InputMap.action_get_events(action).duplicate()
events[type] = event
if events.size() < 4:
for i in abs(4 - events.size()):
var dummy = InputEventKey.new()
dummy.keycode = KEY_UNKNOWN
events.append(dummy)
events[type + (idx * 2)] = event
InputMap.action_erase_events(action)
for i in events:
print([action, i])
InputMap.action_add_event(action, i)
input_changed.emit(action, event)
input_event = event
input_events[idx] = event
awaiting_input = false
await get_tree().create_timer(0.1).timeout
rebinding_input = false
@@ -108,8 +129,9 @@ func map_event_to_action(event) -> void:
func get_event_string(event: InputEvent) -> String:
var event_string := ""
if event == null:
return "---"
if event is InputEventKey:
print(event.keycode)
event_string = OS.get_keycode_string(event.keycode)
elif event is InputEventJoypadButton:
var translation = button_id_translation[event.button_index]
@@ -158,3 +180,4 @@ func cancel_remap() -> void:
rebinding_input = false
get_parent().can_input = true
can_remap = true
update_value()

View File

@@ -130,7 +130,7 @@ ui_accept={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":90,"physical_keycode":0,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":true,"script":null)
]
, null]
}
ui_select={
"deadzone": 0.5,
@@ -172,42 +172,48 @@ jump_0={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":90,"physical_keycode":0,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":true,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":32,"physical_keycode":0,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":1,"pressure":0.0,"pressed":true,"script":null)
]
}
run_0={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":88,"physical_keycode":0,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":2,"pressure":0.0,"pressed":true,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194325,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":3,"pressure":0.0,"pressed":true,"script":null)
]
}
action_0={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":88,"physical_keycode":0,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":2,"pressure":0.0,"pressed":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194325,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":3,"pressure":0.0,"pressed":true,"script":null)
]
}
move_left_0={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194319,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":-1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":65,"physical_keycode":0,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":13,"pressure":0.0,"pressed":true,"script":null)
]
}
move_right_0={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194321,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":1.0,"script":null)
]
}
move_down_0={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194322,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":68,"physical_keycode":0,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":14,"pressure":0.0,"pressed":true,"script":null)
]
}
move_up_0={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194320,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":-1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":11,"pressure":0.0,"pressed":true,"script":null)
]
}
pause={
@@ -246,10 +252,12 @@ move_up_1={
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":11,"pressure":0.0,"pressed":true,"script":null)
]
}
move_down_1={
move_down_0={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":true,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null)
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194322,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":12,"pressure":0.0,"pressed":true,"script":null)
]
}
run_1={
@@ -281,12 +289,6 @@ move_up_2={
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":-1.0,"script":null)
]
}
move_down_2={
"deadzone": 0.5,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":1.0,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":1.0,"script":null)
]
}
run_2={
"deadzone": 0.5,
"events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":2,"pressure":0.0,"pressed":true,"script":null)
@@ -311,12 +313,6 @@ move_right_3={
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":2,"button_index":14,"pressure":0.0,"pressed":true,"script":null)
]
}
move_down_3={
"deadzone": 0.5,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":2,"axis":1,"axis_value":1.0,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":2,"button_index":12,"pressure":0.0,"pressed":true,"script":null)
]
}
move_up_3={
"deadzone": 0.5,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":2,"axis":1,"axis_value":-1.0,"script":null)