77 lines
2.5 KiB
GDScript3
77 lines
2.5 KiB
GDScript3
|
extends BaseUI
|
||
|
|
||
|
var last_key_pressed = null
|
||
|
|
||
|
func input_list_to_string(inputlist: Array) -> String:
|
||
|
var arr_str = []
|
||
|
for input in inputlist:
|
||
|
arr_str.append(input.as_text())
|
||
|
|
||
|
var psa = PoolStringArray(arr_str)
|
||
|
return psa.join(", ")
|
||
|
|
||
|
onready var keymap_btns = {
|
||
|
$VBoxContainer/GridContainer/Forward: "move_forward",
|
||
|
$VBoxContainer/GridContainer/Backward: "move_backward",
|
||
|
$VBoxContainer/GridContainer/Left: "move_left",
|
||
|
$VBoxContainer/GridContainer/Right: "move_right",
|
||
|
$VBoxContainer/GridContainer/Jump: "move_jump",
|
||
|
$VBoxContainer/GridContainer/Frontcamera: "camera_front",
|
||
|
$VBoxContainer/GridContainer/Sprint: "move_sprint",
|
||
|
$VBoxContainer/GridContainer/Changeweapon: "weapon_forward",
|
||
|
}
|
||
|
|
||
|
var called_ui: BaseUI = null
|
||
|
|
||
|
func _ready():
|
||
|
add_user_signal("key_pressed", [{"name": "event", "type": InputEvent}])
|
||
|
refresh_keymap_btns()
|
||
|
$VBoxContainer/GridContainer/MusicVolume.value = db2linear(get_bus_volume("Music"))
|
||
|
$VBoxContainer/GridContainer/MasterVolume.value = db2linear(get_bus_volume("Master"))
|
||
|
for input_btn in keymap_btns:
|
||
|
input_btn.connect("pressed", self, "_on_keymap_pressed", [input_btn, keymap_btns[input_btn]])
|
||
|
|
||
|
func refresh_keymap_btns():
|
||
|
for keymap_btn in keymap_btns:
|
||
|
refresh_keymap_btn(keymap_btn, keymap_btns[keymap_btn])
|
||
|
|
||
|
func refresh_keymap_btn(button: Button, action: String):
|
||
|
button.text = input_list_to_string(InputMap.get_action_list(action))
|
||
|
|
||
|
func get_bus_volume(bus: String):
|
||
|
var idx = AudioServer.get_bus_index(bus)
|
||
|
return AudioServer.get_bus_volume_db(idx)
|
||
|
|
||
|
func set_bus_volume(bus: String, volume_percent: float):
|
||
|
var idx = AudioServer.get_bus_index(bus)
|
||
|
AudioServer.set_bus_volume_db(idx, linear2db(volume_percent))
|
||
|
|
||
|
func _on_Master_value_changed(value):
|
||
|
set_bus_volume("Master", value)
|
||
|
|
||
|
func _on_Music_value_changed(value):
|
||
|
set_bus_volume("Music", value)
|
||
|
|
||
|
func _on_keymap_pressed(button: Button, action: String):
|
||
|
wait_for_new_key(button, action)
|
||
|
|
||
|
func wait_for_new_key(btn: Button, action: String):
|
||
|
btn.text = "Press a key..."
|
||
|
last_key_pressed = null
|
||
|
connect("key_pressed", self, "set_key", [action, btn], CONNECT_ONESHOT)
|
||
|
|
||
|
func set_key(key: InputEvent, action: String, btn: Button):
|
||
|
InputMap.action_erase_events(action)
|
||
|
InputMap.action_add_event(action, key)
|
||
|
refresh_keymap_btn(btn, action)
|
||
|
|
||
|
func _input(event: InputEvent):
|
||
|
if last_key_pressed or not event is InputEventKey:
|
||
|
return
|
||
|
last_key_pressed = event
|
||
|
emit_signal("key_pressed", event)
|
||
|
|
||
|
func _on_Save_pressed():
|
||
|
get_parent().add_child(called_ui)
|
||
|
get_parent().remove_child(self)
|