Files
uarep-ctl/scenes/контроль/кнопка-прибора.gd
2026-01-21 09:52:51 +03:00

181 lines
5.6 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@tool
extends TextureButton
@onready var tween: Tween
@export var state_colors: Array
var progress_tween: Tween = null
var current_timeout: float = 0.0
func _ready():
toggle_mode = true
focus_mode = Control.FOCUS_ALL
connect('focus_entered', _on_focus_entered)
connect('focus_exited', _on_focus_exited)
ProjectSettings.connect('settings_changed', Callable(self, '_on_settings_changed'))
_on_settings_changed()
func _enter_tree() -> void:
if is_inside_tree():
$label.self_modulate = state_colors[state]
func update_progress_bar(target_value) -> void:
if tween:
tween.kill()
tween = create_tween()
tween.tween_property($control_progress, 'value', target_value, 0.3 if target_value > 0 else 0.0)
# Функция для анимации прогресса от 0 до 100 за указанное время
func animate_progress_from_zero(time_req: float) -> void:
_reset_progress_animation()
$control_progress.value = 0
$control_progress.visible = true
current_timeout = time_req
progress_tween = create_tween()
progress_tween.set_trans(Tween.TRANS_LINEAR)
progress_tween.set_ease(Tween.EASE_IN_OUT)
progress_tween.tween_method(
_update_progress_with_tooltip, # Метод для вызова каждый кадр
0.0, # Начальное значение
$control_progress.max_value, # Конечное значение
time_req # Длительность
)
progress_tween.finished.connect(_on_progress_animation_finished.bind())
# Метод для обновления прогресса и tooltip во время анимации
func _update_progress_with_tooltip(value: float) -> void:
# Устанавливаем значение прогресс-бара
$control_progress.value = value
# Вычисляем процент и обновляем tooltip
var max_val = $control_progress.get('max_value')
var percentage = (value / max_val) * 100.0
$control_progress.tooltip_text = 'Процесс контроля %01d%%' % percentage
# Функция для полного сброса анимации
func _reset_progress_animation() -> void:
if progress_tween:
progress_tween.kill()
progress_tween = null
$control_progress.value = 0
current_timeout = 0.0
# Функция вызывается при завершении анимации
func _on_progress_animation_finished() -> void:
$control_progress.visible = false
# Функция для принудительной остановки анимации с сохранением текущего значения
func stop_progress_animation() -> void:
if progress_tween:
progress_tween.stop()
progress_tween = null
func _on_toggled(toggled_on):
if has_node('frame'):
$frame.visible = toggled_on
func _on_focus_entered():
button_pressed = true
#enter_press() TODO: Пока что без выбора прибора
func _on_focus_exited():
pass
func enter_press():
var enter_event = InputEventAction.new()
enter_event.action = 'ui_accept'
enter_event.pressed = true
Input.parse_input_event(enter_event)
func _on_settings_changed():
if not is_inside_tree(): return
var white_color = ProjectSettings.get_setting('application/config/%s' % 'Цвет фона схемы', Color.DIM_GRAY)
var json_col_w = ProjectSettings.get_setting('application/config/%s' % 'Цвет фона схемы', '[0.0, 0.0, 0.0, 0.0]')
var arr_col_w = JSON.parse_string(json_col_w)
white_color = Color(arr_col_w[0], arr_col_w[1], arr_col_w[2], arr_col_w[3]) if arr_col_w else Color.DIM_GRAY
var black_color = ProjectSettings.get_setting('application/config/%s' % 'Цвет схемы', Color.WHITE_SMOKE)
var json_col_b = ProjectSettings.get_setting('application/config/%s' % 'Цвет схемы', '[1.0, 1.0, 1.0, 1.0]')
var arr_col_b = JSON.parse_string(json_col_b)
black_color = Color(arr_col_b[0], arr_col_b[1], arr_col_b[2], arr_col_b[3]) if arr_col_b else Color.WHITE_SMOKE
$pic_functional.material.set('shader_parameter/white', white_color)
$pic_functional.material.set('shader_parameter/black', black_color)
@export var state: int:
set(v):
v = 0 if v < 0 else v % state_colors.size()
state = v
if is_inside_tree():
$label.self_modulate = state_colors[v]
@export var progress_value: float = 0.0:
set(v):
progress_value = v
if is_inside_tree():
if progress_tween:
stop_progress_animation()
update_progress_bar(v)
var max_val = $control_progress.get('max_value')
var percentage = (v / max_val) * 100.0
$control_progress.tooltip_text = 'Процесс контроля %01d%%' % percentage
@export var progress_visible: bool:
set(v):
progress_visible = v
if is_inside_tree():
$control_progress.visible = v
@export var progress_max_value: int:
set(v):
progress_max_value = v
if is_inside_tree():
$control_progress.max_value = v
@export var progress_min_value: int:
set(v):
progress_min_value = v
if is_inside_tree():
$control_progress.min_value = v
@export var show_functional: bool:
set(v):
show_functional = v
if is_inside_tree():
$pic_functional.visible = v
@export var timeout_tween: float:
set(v):
if v <= 0:
_reset_progress_animation()
$control_progress.visible = false
return
animate_progress_from_zero(v)