@tool

extends PanelContainer

func set_text(val: String): $button.set_text(val)
func set_pressed(val: bool): $button.set_pressed(val)
func set_disabled(val: bool): disabled = val
func get_text(): return $button.get_text()
func is_pressed(): return $button.is_pressed()
func is_toggle_mode(): return $button.is_toggle_mode()


@export var texture_state0: Texture2D = preload('res://table/элемент-в.tscn::GradientTexture2D_d3qtt'):
    set(val): $state.set_texture(val)


@export var texture_state1: Texture2D = preload('res://table/элемент-в.tscn::GradientTexture2D_k8o6s'):
    set(val): $state.set_texture(val)


@export var strips_rotation: float = 0.0:
    set(val):
        strips_rotation = val
        $back.strips_rotation = deg_to_rad(val)
    get: return strips_rotation


@export var text: String = '':
    set(val): $button.set_text(val)
    get: return $button.get_text()


@export var disabled: bool = false:
    set(val):
        disabled = val
        $button.set_disabled(val)
        $back.disabled = val
        $state.modulate.a = 0.5 if val else 1.0
    get: return disabled


@export var pressed: bool = false:
    set(val):
        pressed = val
        $button.set_pressed(val)
    get:
        return pressed


@export var toggle_mode: bool = false:
    set(val):
        toggle_mode = val
        $button.set_toggle_mode(val)
    get:
        return toggle_mode


func button_connect(button_signal: String, proc: Callable):
    var rc = $button.connect(button_signal, proc)
    if rc != Error.OK:
        push_error('возникла ошибка \"%s\" при подключении сигнала \"%s\" к обработчику \"%s\"' % [error_string(rc), button_signal, proc])


func _on_resized() -> void:
    $back.scale = size
    $back.material.set('shader_parameter/scale', size / Vector2(20.0, 20.0))


func _enter_tree() -> void:
    $back.scale = size
    $back.material.set('shader_parameter/scale', size / Vector2(20.0, 20.0))
    $button.set_toggle_mode(true)
    $button.connect('toggled', _on_button_toggled)


func _exit_tree() -> void:
    $button.disconnect('toggled', _on_button_toggled)


func set_toggle_mode(val: bool):
    $button.set_toggle_mode(val)


func _on_button_toggled(val: bool):
    if val: $state.set_texture(texture_state1)
    else:   $state.set_texture(texture_state0)
