rat-times/scripts/config_manager.gd

122 lines
2.9 KiB
GDScript3
Raw Normal View History

2023-03-03 21:12:54 +00:00
class_name ConfigManager extends Resource
2023-03-09 20:26:57 +00:00
signal entry_started
signal entry_stopped
signal file_changed
2023-03-03 21:12:54 +00:00
const CONFIG_PATH := "user://settings.cfg"
var _config := ConfigFile.new()
2023-03-09 20:26:57 +00:00
var _watcher: FileWatcher
2023-03-03 21:12:54 +00:00
2023-03-09 20:26:57 +00:00
var timesheet: TimeSheet setget ,get_timesheet
2023-03-03 21:12:54 +00:00
2023-03-09 20:26:57 +00:00
func get_timesheet() -> TimeSheet:
if timesheet == null:
timesheet = _load_timesheet(get_current_file())
return timesheet
2023-03-04 13:31:27 +00:00
2023-03-09 20:26:57 +00:00
func _load_timesheet(path: String) -> TimeSheet:
timesheet = TimeSheet.restore(path)
if timesheet == null:
_watcher = null
return null
_watcher = FileWatcher.new()
_watcher.file_name = path
_watcher.start()
# warning-ignore:return_value_discarded
_watcher.connect("file_changed", self, "_on_file_changed")
# warning-ignore:return_value_discarded
timesheet.connect("entry_started", self, "_on_entry_started")
# warning-ignore:return_value_discarded
timesheet.connect("entry_stopped", self, "_on_entry_stopped")
return timesheet
func _on_entry_started() -> void:
emit_signal("entry_started")
func _on_entry_stopped() -> void:
emit_signal("entry_stopped")
func _on_file_changed() -> void:
emit_signal("file_changed")
var current_file: String = "" setget set_current_file, get_current_file
2023-03-04 13:31:27 +00:00
func set_current_file(value: String) -> void:
2023-03-09 20:26:57 +00:00
timesheet = _load_timesheet(value)
2023-03-04 13:31:27 +00:00
if timesheet == null:
return
current_file = value
_config.set_value("MAIN", "file", value)
2023-03-09 20:26:57 +00:00
emit_signal("file_changed")
2023-03-04 13:31:27 +00:00
save()
func get_current_file() -> String:
2023-03-09 20:26:57 +00:00
var _default_path := OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS, true).plus_file("mouse_timer.csv")
2023-03-04 13:31:27 +00:00
return _config.get_value("MAIN", "file", _default_path)
2023-03-09 20:26:57 +00:00
var theme: Theme setget , get_theme
func get_theme() -> Theme:
2023-03-04 13:31:27 +00:00
if theme == null:
theme = ResourceLoader.load(theme_path, "Theme")
return theme
2023-03-03 21:12:54 +00:00
2023-03-04 13:31:27 +00:00
signal theme_changed
2023-03-09 20:26:57 +00:00
var theme_path: String = "" setget set_theme_path, get_theme_path
2023-03-04 13:31:27 +00:00
func set_theme_path(value: String) -> void:
var new_theme: Theme = ResourceLoader.load(value, "Theme")
if new_theme != null:
theme = new_theme
theme_path = value
_config.set_value("MAIN", "theme", value)
2023-03-09 20:26:57 +00:00
emit_signal("theme_changed")
2023-03-04 13:31:27 +00:00
save()
func get_theme_path() -> String:
return _config.get_value("MAIN", "theme", preload("res://assets/default_theme.theme").resource_path)
2023-03-03 21:12:54 +00:00
2023-03-09 20:26:57 +00:00
var last_task_name: String = "" setget set_last_task_name, get_last_task_name
func set_last_task_name(value: String) -> void:
2023-03-03 21:12:54 +00:00
last_task_name = value
_config.set_value("MAIN", "last_task_name", value)
save()
2023-03-09 20:26:57 +00:00
func get_last_task_name() -> String:
2023-03-03 21:12:54 +00:00
return _config.get_value("MAIN", "last_task_name", "")
2023-03-09 20:26:57 +00:00
var sound_fx_on: bool = true setget set_sound_fx_on, get_sound_fx_on
func set_sound_fx_on(value: bool) -> void:
2023-03-03 21:12:54 +00:00
sound_fx_on = value
_config.set_value("MAIN", "sound_fx_on", value)
save()
2023-03-09 20:26:57 +00:00
func get_sound_fx_on() -> bool:
2023-03-03 21:12:54 +00:00
return _config.get_value("MAIN", "sound_fx", true)
func _init() -> void:
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
2023-03-03 21:12:54 +00:00
_config.load(CONFIG_PATH)
func save() -> void:
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
2023-03-03 21:12:54 +00:00
_config.save(CONFIG_PATH)