SelectableOptionNode.gd 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. extends HBoxContainer
  2. @export var option_key := ""
  3. @export var title := ""
  4. @export var value_descs: Array[String] = []
  5. @export var values := []
  6. @export var settings_category := "video"
  7. @export var selected := false
  8. signal value_changed(new_value)
  9. var selected_index := 0:
  10. set(value):
  11. selected_index = value
  12. func _ready() -> void:
  13. await get_tree().process_frame
  14. update_starting_values()
  15. func update_starting_values() -> void:
  16. if Settings.file.has(settings_category):
  17. if Settings.file[settings_category].has(option_key):
  18. if Settings.file[settings_category][option_key] is String:
  19. selected_index = values.find(Settings.file[settings_category][option_key])
  20. else:
  21. selected_index = Settings.file[settings_category][option_key]
  22. func _process(_delta: float) -> void:
  23. if selected:
  24. handle_inputs()
  25. $Cursor.modulate.a = int(selected)
  26. for i in [$AutoScrollContainer, %AutoScrollContainer2]:
  27. i.is_focused = selected
  28. %Title.text = tr(title) + ":"
  29. %Value.text = tr(str(values[selected_index]))
  30. %LeftArrow.modulate.a = int(selected and selected_index > 0)
  31. %RightArrow.modulate.a = int(selected and selected_index < values.size() - 1)
  32. func set_selected(active := false) -> void:
  33. selected = active
  34. func handle_inputs() -> void:
  35. var old := selected_index
  36. if Input.is_action_just_pressed("ui_left"):
  37. selected_index -= 1
  38. if Settings.file.audio.extra_sfx == 1:
  39. AudioManager.play_global_sfx("menu_move")
  40. if Input.is_action_just_pressed("ui_right"):
  41. selected_index += 1
  42. if Settings.file.audio.extra_sfx == 1:
  43. AudioManager.play_global_sfx("menu_move")
  44. selected_index = clamp(selected_index, 0, values.size() - 1)
  45. if old != selected_index:
  46. value_changed.emit(selected_index)