extends HSlider

@export var bus_name: String
var bus_index: int

func _ready():
	mouse_entered.connect(_on_mouse_entered)
	focus_entered.connect(_on_focus_entered)
	focus_exited.connect(_on_focus_exited)
	
	bus_index = AudioServer.get_bus_index(bus_name)
	value_changed.connect(_on_value_changed)
	value = db_to_linear(AudioServer.get_bus_volume_db(bus_index))
	
func _on_value_changed(value_: float):
	AudioServer.set_bus_volume_db(bus_index, linear_to_db(value_))


var entered = false
func _on_focus_entered():
	if entered: entered = false
	else: get_viewport().warp_mouse(get_global_rect().position + get_global_rect().size / 2)

func _on_focus_exited():
	pass
	
func _on_mouse_entered():
	entered = true
	grab_focus()
	entered = false
	
