rat-times/ui/time_counter.gd

75 lines
2.3 KiB
GDScript3
Raw Normal View History

2023-03-09 20:26:57 +00:00
extends VBoxContainer
var config: ConfigManager = preload("res://config_manager.tres")
onready var task_name_line_edit := $"%TaskNameLineEdit" as LineEdit
onready var time_label := $"%TimeLabel" as Label
onready var start_button := $"%StartButton" as Button
onready var timer := $"%Timer" as Timer
onready var audio_stream_player := $"%AudioStreamPlayer" as AudioStreamPlayer
2023-04-22 01:08:05 +00:00
var current_time_entry: TimeEntry
2023-03-09 20:26:57 +00:00
func _ready() -> void:
# warning-ignore:return_value_discarded
timer.connect("timeout", self, "_on_timer_timeout")
2023-03-09 23:25:50 +00:00
start_button.hint_tooltip = tr(Consts.START)
2023-03-09 20:26:57 +00:00
start_button.toggle_mode = true
# warning-ignore:return_value_discarded
start_button.connect("toggled", self, "_on_start_button_toggled")
2023-04-22 01:08:05 +00:00
task_name_line_edit.text = config.current_task_name
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
task_name_line_edit.connect("text_changed", self, "_on_task_name_line_edit_text_changed")
# warning-ignore:return_value_discarded
2023-04-22 01:08:05 +00:00
config.connect("time_sheet_loaded", self, "_on_time_sheet_loaded")
update_timer_state()
2023-03-09 20:26:57 +00:00
func _on_timer_timeout() -> void:
config.timesheet.update()
2023-04-22 01:08:05 +00:00
time_label.text = current_time_entry.get_period()
2023-03-09 20:26:57 +00:00
2023-04-22 01:08:05 +00:00
func _on_start_button_toggled(_is_on: bool) -> void:
2023-03-09 20:26:57 +00:00
if config.sound_fx_on:
audio_stream_player.play()
2023-04-22 01:08:05 +00:00
if current_time_entry != null:
2023-04-22 02:04:35 +00:00
# warning-ignore:return_value_discarded
2023-04-22 01:08:05 +00:00
config.timesheet.stop_entry(task_name_line_edit.text)
2023-03-09 20:26:57 +00:00
else:
2023-04-22 02:04:35 +00:00
# warning-ignore:return_value_discarded
2023-04-22 01:08:05 +00:00
config.timesheet.add_entry(task_name_line_edit.text)
2023-03-09 20:26:57 +00:00
func _on_task_name_line_edit_text_changed(new_text: String) -> void:
2023-04-22 01:08:05 +00:00
config.current_task_name = new_text
update_timer_state()
func _on_time_sheet_loaded():
update_timer_state()
func update_timer_state() -> void:
current_time_entry = config.timesheet.get_active_entry_from_name(config.current_task_name)
if current_time_entry:
set_button_as_started()
else:
set_button_as_stopped()
2023-03-09 20:26:57 +00:00
func set_button_as_stopped() -> void:
start_button.set_pressed_no_signal(false)
2023-03-09 23:25:50 +00:00
start_button.hint_tooltip = tr(Consts.START)
2023-03-09 20:26:57 +00:00
time_label.text = Consts.NO_TIME
start_button.theme_type_variation = Consts.THEME_OVERRIDE_START
timer.stop()
func set_button_as_started() -> void:
start_button.set_pressed_no_signal(true)
2023-03-09 23:25:50 +00:00
start_button.hint_tooltip = tr(Consts.STOP)
2023-03-09 20:26:57 +00:00
start_button.theme_type_variation = Consts.THEME_OVERRIDE_STOP
timer.start()