Files
DungeonCrawlerWorld/scripts/presentation/options_panel.gd
T

106 lines
3.9 KiB
GDScript

class_name OptionsPanel
extends PanelContainer
const InputBindingsType = preload("res://scripts/game/input_bindings.gd")
const SCALE_STEPS := [0.85, 1.0, 1.15, 1.3]
@onready var body: RichTextLabel = $Body
var ui_scale := 1.0
var _selected_index := 0
var _waiting_for_rebind := false
var _bindings: Dictionary[StringName, Key] = {}
var _audio_library: AudioLibrary
var waiting_for_rebind: bool:
get:
return _waiting_for_rebind
func _ready() -> void:
for action: StringName in InputBindingsType.input_actions():
var events := InputMap.action_get_events(action)
if not events.is_empty() and events[0] is InputEventKey:
_bindings[action] = (events[0] as InputEventKey).physical_keycode
func present() -> void:
visible = true
_waiting_for_rebind = false
_audio_library = get_node_or_null("../../AudioLibrary") as AudioLibrary
refresh()
func dismiss() -> void:
visible = false
_waiting_for_rebind = false
func binding_for(action: StringName) -> Key:
return _bindings.get(action, KEY_NONE)
func handle_key(event: InputEventKey) -> bool:
if not event.is_pressed():
return false
if _waiting_for_rebind:
if event.keycode == KEY_ESCAPE:
_waiting_for_rebind = false
else:
_rebind(&"inventory", event.physical_keycode)
_waiting_for_rebind = false
refresh()
return true
if event.keycode == KEY_A and _audio_library != null:
_audio_library.set_ambience_volume_percent(_audio_library.get_ambience_volume_percent() - AudioLibrary.VOLUME_STEP_PERCENT)
elif event.keycode == KEY_Z and _audio_library != null:
_audio_library.set_ambience_volume_percent(_audio_library.get_ambience_volume_percent() + AudioLibrary.VOLUME_STEP_PERCENT)
elif event.keycode == KEY_S and _audio_library != null:
_audio_library.set_sfx_volume_percent(_audio_library.get_sfx_volume_percent() - AudioLibrary.VOLUME_STEP_PERCENT)
elif event.keycode == KEY_X and _audio_library != null:
_audio_library.set_sfx_volume_percent(_audio_library.get_sfx_volume_percent() + AudioLibrary.VOLUME_STEP_PERCENT)
elif event.keycode == KEY_UP:
_selected_index = posmod(_selected_index - 1, 3)
elif event.keycode == KEY_DOWN:
_selected_index = posmod(_selected_index + 1, 3)
elif event.keycode == KEY_LEFT:
_adjust_selected(-1)
elif event.keycode == KEY_RIGHT:
_adjust_selected(1)
elif event.keycode == KEY_ENTER or event.keycode == KEY_SPACE:
if _selected_index == 2:
_waiting_for_rebind = true
else:
_adjust_selected(1)
else:
return false
refresh()
return true
func refresh() -> void:
if body == null:
return
var cursor := func(index: int) -> String: return "> " if index == _selected_index else " "
var contrast := "Renforcé" if _selected_index == 1 else "Standard"
var ambience_volume := _audio_library.get_ambience_volume_percent() if _audio_library != null else 100
var sfx_volume := _audio_library.get_sfx_volume_percent() if _audio_library != null else 100
body.text = "%sÉchelle UI : %d%%\n%sMarqueurs color-blind : %s\n%sRemapper Inventaire : %s\n\nAmbiance : %d%% Effets sonores : %d%%\nA / Z — volume ambiance S / X — volume effets\nFlèches — ajuster Entrée — sélectionner Échap / O — fermer" % [cursor.call(0), int(ui_scale * 100.0), cursor.call(1), contrast, cursor.call(2), OS.get_keycode_string(binding_for(&"inventory")), ambience_volume, sfx_volume]
func _adjust_selected(direction: int) -> void:
if _selected_index == 0:
var index := SCALE_STEPS.find(ui_scale)
ui_scale = SCALE_STEPS[clampi(index + direction, 0, SCALE_STEPS.size() - 1)]
_apply_scale()
func _apply_scale() -> void:
var hud := get_parent() as CanvasLayer
if hud == null:
return
for node in hud.get_children():
if node is Control and node != self:
(node as Control).scale = Vector2.ONE * ui_scale
func _rebind(action: StringName, keycode: Key) -> void:
var event := InputEventKey.new()
event.physical_keycode = keycode
event.keycode = keycode
InputMap.action_erase_events(action)
InputMap.action_add_event(action, event)
_bindings[action] = keycode