rat-times/scripts/config_manager.gd

181 lines
4.4 KiB
GDScript3
Raw Normal View History

2023-04-22 01:08:05 +00:00
## Reads the config, sets values. Acts a singleton because it proxies a const
## file path.
2023-03-03 21:12:54 +00:00
class_name ConfigManager extends Resource
const CONFIG_PATH := "user://settings.cfg"
2023-03-09 23:25:50 +00:00
var _config := ConfigFile.new()
var _watcher: FileWatcher
2023-04-22 01:08:05 +00:00
###############################################################################
#
# SIGNAL
#
signal time_sheet_loaded
func emit_loaded() -> void:
emit_signal("time_sheet_loaded")
###############################################################################
#
# TIMESHEET FILE LOADING AND PARSING
#
var timesheet: TimeSheet setget ,get_timesheet
2023-03-09 20:26:57 +00:00
func get_timesheet() -> TimeSheet:
if timesheet == null:
2023-04-22 01:08:05 +00:00
timesheet = _load_timesheet(get_current_timesheet_file_path())
2023-03-09 20:26:57 +00:00
return timesheet
2023-03-04 13:31:27 +00:00
2023-03-09 20:26:57 +00:00
func _load_timesheet(path: String) -> TimeSheet:
2023-04-22 01:08:05 +00:00
var new_timesheet := TimeSheet.restore(path)
if new_timesheet == null:
2023-03-09 20:26:57 +00:00
return null
_watcher = FileWatcher.new()
_watcher.file_name = path
# warning-ignore:return_value_discarded
2023-03-09 23:25:50 +00:00
_watcher.connect("file_changed", self, "reload_timesheet")
_watcher.start()
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
2023-03-09 23:25:50 +00:00
#timesheet.connect("entry_started", self, "_on_entry_started")
2023-03-09 20:26:57 +00:00
# warning-ignore:return_value_discarded
2023-03-09 23:25:50 +00:00
#timesheet.connect("entry_stopped", self, "_on_entry_stopped")
2023-04-22 01:08:05 +00:00
return new_timesheet
2023-03-09 20:26:57 +00:00
2023-03-09 23:25:50 +00:00
func reload_timesheet() -> void:
2023-04-22 01:08:05 +00:00
var new_timesheet = _load_timesheet(get_current_timesheet_file_path())
2023-03-09 23:25:50 +00:00
if new_timesheet == null:
printerr("failed to load new timesheet")
return
timesheet = new_timesheet
2023-04-22 01:08:05 +00:00
emit_loaded()
2023-03-09 20:26:57 +00:00
2023-04-22 01:08:05 +00:00
###############################################################################
#
# TIMESHEET FILE PATH
#
2023-03-04 13:31:27 +00:00
2023-04-22 01:08:05 +00:00
var current_timesheet_file_path: String = "" setget set_current_timesheet_file_path, get_current_timesheet_file_path
2023-03-04 13:31:27 +00:00
2023-04-22 01:08:05 +00:00
func set_current_timesheet_file_path(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
2023-04-22 01:08:05 +00:00
current_timesheet_file_path = value
2023-03-04 13:31:27 +00:00
_config.set_value("MAIN", "file", value)
2023-04-22 01:08:05 +00:00
emit_loaded()
2023-03-04 13:31:27 +00:00
save()
2023-04-22 01:08:05 +00:00
func get_current_timesheet_file_path() -> 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-04-22 01:08:05 +00:00
###############################################################################
#
# THEME FILE LOADING AND PARSING
#
2023-03-09 20:26:57 +00:00
var theme: Theme setget , get_theme
2023-04-22 01:08:05 +00:00
2023-03-09 20:26:57 +00:00
func get_theme() -> Theme:
2023-03-04 13:31:27 +00:00
if theme == null:
2023-04-22 01:08:05 +00:00
theme = ResourceLoader.load(theme_file_path, "Theme")
2023-03-04 13:31:27 +00:00
return theme
2023-03-03 21:12:54 +00:00
2023-04-22 01:08:05 +00:00
###############################################################################
#
# THEME FILE PATH
#
2023-03-04 13:31:27 +00:00
signal theme_changed
2023-04-22 01:08:05 +00:00
var theme_file_path: String = "" setget set_theme_file_path, get_theme_file_path
2023-03-04 13:31:27 +00:00
2023-04-22 01:08:05 +00:00
func set_theme_file_path(value: String) -> void:
2023-03-04 13:31:27 +00:00
var new_theme: Theme = ResourceLoader.load(value, "Theme")
if new_theme != null:
theme = new_theme
2023-04-22 01:08:05 +00:00
theme_file_path = value
2023-03-04 13:31:27 +00:00
_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()
2023-04-22 01:08:05 +00:00
func get_theme_file_path() -> String:
2023-03-04 13:31:27 +00:00
return _config.get_value("MAIN", "theme", preload("res://assets/default_theme.theme").resource_path)
2023-03-03 21:12:54 +00:00
2023-04-22 01:08:05 +00:00
#var current_task_name: String = "" setget set_current_task_name, get_current_task_name
#
#func set_current_task_name(value: String) -> void:
# current_task_name = value
# _config.set_value("MAIN", "current_task_name", value)
# save()
#
#func get_current_task_name() -> String:
# return _config.get_value("MAIN", "current_task_name", "")
2023-03-09 20:26:57 +00:00
2023-03-03 21:12:54 +00:00
2023-04-22 01:08:05 +00:00
###############################################################################
#
# SOUND OPTION
#
2023-03-03 21:12:54 +00:00
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)
2023-04-22 01:08:05 +00:00
###############################################################################
#
# SOME SETTINGS CACHE
#
var current_task_name := "" setget set_current_task_name, get_current_task_name
func set_current_task_name(value: String) -> void:
current_task_name = value
_config.set_value("CACHE", "current_task_name", value)
save()
func get_current_task_name() -> String:
return _config.get_value("CACHE", "current_task_name", "")
###############################################################################
#
# BOOTSTRAP
#
2023-03-03 21:12:54 +00:00
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)