class_name SettingsSlider
extends HSlider

## A normal slider that changes a float (or int(?)) value in the project settings.
## This slider will automatically load and save it's value when it enters the tree
## or changes it's value.[br]
## INFO: You don't need to change [param min_value] and [param max_value]. It is being saved
## as a ratio towards them.

## The path towards the setting that get's it's value changed.
@export var section: StringName = &""
@export var key: StringName = &""
@export var default_value: float = 1.0


func _ready() -> void:
	value_changed.connect(func(_value_changed: bool) -> void:
		set_setting(value)
	)
	#value_changed.connect(set_setting)

	var err: Variant = SettingsManager.get_setting(section, key)
	if not is_nan(err) and err is float:
		var new_value: float = remap(err, 0.0, 1.0, min_value, max_value)
		set_value_no_signal(new_value)
		set_setting(new_value)
		drag_ended.emit(value)


## Set's the setting in the project setting with a path from [param setting_path].[br]
## Note that this won't change [param value].
func set_setting(new_value: float) -> void:
	SettingsManager.set_setting(section, key, new_value)
