convert to Godot 3.5

This commit is contained in:
2023-03-10 00:26:57 +04:00
parent bf3a9a35ce
commit 66b25bae97
36 changed files with 733 additions and 1468 deletions

View File

@ -1,5 +1,15 @@
class_name CMD
var command_line_arguments: Dictionary = {}
func unsurround(value: String, quotes := PoolStringArray(['"', "'"])) -> String:
for quote_str in quotes:
if value.begins_with(quote_str) \
and value.ends_with(quote_str) \
and value[value.length() - 2] != "\\":
return value.trim_prefix(quote_str).trim_suffix(quote_str).strip_edges()
return value
## Returns a dictionary of all arguments passed after `--` on the command line
## arguments take one of 2 forms:
## - `--arg` which is a boolean (using `--no-arg` for `false` is possible)
@ -7,21 +17,14 @@ class_name CMD
## unsurround the string
## This function does no evaluation and does not attempt to guess the type of
## arguments. You will receive either bools, or strings.
var command_line_arguments: Dictionary = (func () -> Dictionary:
var unsurround := func unsurround(value: String, quotes := PackedStringArray(['"', "'"])) -> String:
for quote_str in quotes:
if value.begins_with(quote_str) \
and value.ends_with(quote_str) \
and value[value.length() - 2] != "\\":
return value.trim_prefix(quote_str).trim_suffix(quote_str).strip_edges()
return value
func _read_arguments() -> Dictionary:
var arguments := {}
for argument in OS.get_cmdline_user_args():
argument = argument.lstrip("--").to_lower()
for arg in OS.get_cmdline_args():
var argument: String = arg.lstrip("--").to_lower()
if argument.find("=") > -1:
var arg_tuple := argument.split("=")
var key := arg_tuple[0]
var value:String = unsurround.call(arg_tuple[1])
var value := unsurround(arg_tuple[1])
arguments[key] = value
else:
var key := argument
@ -30,10 +33,11 @@ var command_line_arguments: Dictionary = (func () -> Dictionary:
value = false
key = argument.lstrip("no-")
arguments[key] = value
return arguments).call()
return arguments
func get_argument(name: String, default: Variant = null) -> Variant:
func get_argument(name: String, default = null):
if command_line_arguments.has(name):
return command_line_arguments[name]
return default

View File

@ -1,48 +1,78 @@
class_name ConfigManager extends Resource
signal entry_started
signal entry_stopped
signal file_changed
const CONFIG_PATH := "user://settings.cfg"
var _config := ConfigFile.new()
var _watcher: FileWatcher
var timesheet: TimeSheet setget ,get_timesheet
func get_timesheet() -> TimeSheet:
if timesheet == null:
timesheet = _load_timesheet(get_current_file())
return timesheet
var timesheet: TimeSheet:
get:
if timesheet == null:
timesheet = TimeSheet.restore(current_file)
return timesheet
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
signal file_changed
var current_file: String = "":
set = set_current_file,
get = get_current_file
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
func set_current_file(value: String) -> void:
timesheet = TimeSheet.restore(value)
timesheet = _load_timesheet(value)
if timesheet == null:
return
current_file = value
_config.set_value("MAIN", "file", value)
file_changed.emit()
emit_signal("file_changed")
save()
func get_current_file() -> String:
var _default_path := OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS, true).path_join("mouse_timer.csv")
var _default_path := OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS, true).plus_file("mouse_timer.csv")
return _config.get_value("MAIN", "file", _default_path)
var theme: Theme:
get:
var theme: Theme setget , get_theme
func get_theme() -> Theme:
if theme == null:
theme = ResourceLoader.load(theme_path, "Theme")
return theme
signal theme_changed
var theme_path: String = "":
set = set_theme_path,
get = get_theme_path
var theme_path: String = "" setget set_theme_path, get_theme_path
func set_theme_path(value: String) -> void:
@ -51,7 +81,7 @@ func set_theme_path(value: String) -> void:
theme = new_theme
theme_path = value
_config.set_value("MAIN", "theme", value)
theme_changed.emit()
emit_signal("theme_changed")
save()
@ -59,27 +89,33 @@ func get_theme_path() -> String:
return _config.get_value("MAIN", "theme", preload("res://assets/default_theme.theme").resource_path)
var last_task_name: String = "":
set(value):
var last_task_name: String = "" setget set_last_task_name, get_last_task_name
func set_last_task_name(value: String) -> void:
last_task_name = value
_config.set_value("MAIN", "last_task_name", value)
save()
get:
func get_last_task_name() -> String:
return _config.get_value("MAIN", "last_task_name", "")
var sound_fx_on: bool = true:
set(value):
var sound_fx_on: bool = true setget set_sound_fx_on, get_sound_fx_on
func set_sound_fx_on(value: bool) -> void:
sound_fx_on = value
_config.set_value("MAIN", "sound_fx_on", value)
save()
get:
func get_sound_fx_on() -> bool:
return _config.get_value("MAIN", "sound_fx", true)
func _init() -> void:
# warning-ignore:return_value_discarded
_config.load(CONFIG_PATH)
func save() -> void:
# warning-ignore:return_value_discarded
_config.save(CONFIG_PATH)

60
scripts/file_watcher.gd Normal file
View File

@ -0,0 +1,60 @@
class_name FileWatcher
signal file_changed
var file_name: String setget set_file_name
var scan_delay_s: float = 1.0 setget set_scan_delay_s
var _file := File.new()
var _last_modified: int = 0
var _timer := Timer.new()
func _init() -> void:
# warning-ignore:return_value_discarded
_timer.connect("timeout", self, "check")
_timer.wait_time = scan_delay_s
func set_file_name(new_file_name: String) -> void:
if file_name == new_file_name:
return
file_name = new_file_name
if file_name.begins_with("res://") or file_name.begins_with("user://"):
file_name = ProjectSettings.globalize_path(file_name)
_last_modified = _file.get_modified_time(file_name)
_timer.name = "__file_watcher_%s"%[file_name]
func set_scan_delay_s(new_scan_delay_s: float) -> void:
scan_delay_s = new_scan_delay_s
_timer.wait_time = scan_delay_s
func check() -> void:
if file_name == "":
return
var new_modified := _file.get_modified_time(file_name)
if new_modified != _last_modified:
_last_modified = new_modified
emit_signal("file_changed")
func start() -> void:
if not _timer.is_inside_tree():
var main_loop: SceneTree = Engine.get_main_loop()
var root := main_loop.root
if root == null:
printerr("Called start before any scene is loaded")
return
if not root.is_inside_tree():
yield(root, "ready")
root.call_deferred("add_child", _timer, true)
yield(_timer, "ready")
_timer.start()
func stop() -> void:
_timer.stop()

View File

@ -31,47 +31,51 @@ func get_total_elapsed_seconds() -> int:
func get_period() -> String:
var time_in_secs := get_elapsed_seconds()
return TimeEntry.time_to_period(time_in_secs)
return time_to_period(time_in_secs)
func get_total_period() -> String:
var time_in_secs := get_total_elapsed_seconds()
return TimeEntry.time_to_period(time_in_secs)
return time_to_period(time_in_secs)
static func time_to_period(time_in_secs: int) -> String:
# warning-ignore:integer_division
var seconds := time_in_secs%60
@warning_ignore("integer_division")
# warning-ignore:integer_division
var minutes := (time_in_secs/60)%60
@warning_ignore("integer_division")
# warning-ignore:integer_division
var hours := (time_in_secs/60)/60
return "%02d:%02d:%02d" % [hours, minutes, seconds]
func to_csv_line() -> PackedStringArray:
return PackedStringArray([
func to_csv_line() -> PoolStringArray:
return PoolStringArray([
name,
start_time,
end_time,
str(get_elapsed_seconds()) if closed else tr(Consts.ONGOING)
])
static func is_csv_line_valid(line: PackedStringArray) -> bool:
static func is_csv_line_valid(line: PoolStringArray) -> bool:
return line.size() > 3
func from_csv_line(line: PackedStringArray) -> TimeEntry:
func from_csv_line(line: PoolStringArray) -> TimeEntry:
name = line[0]
var start_time_string = line[1]
# warning-ignore:return_value_discarded
start_time.from_string(start_time_string)
var elapsed_seconds = int(line[3]) if line[3].is_valid_int() else 0
var elapsed_seconds = int(line[3]) if line[3].is_valid_integer() else 0
closed = elapsed_seconds > 0
if closed == true:
var end_time_string = line[2]
# warning-ignore:return_value_discarded
end_time.from_string(end_time_string)
else:
# warning-ignore:return_value_discarded
end_time.from_current_time()
return self

View File

@ -1,17 +1,21 @@
class_name TimeSheet
signal entry_started
signal entry_stopped
var source_path := ""
var entries: Array[TimeEntry] = []
var entries := []
var entries_names := {}
var current_entry: TimeEntry
## Loads the data file
func load_file() -> bool:
var file := FileAccess.open(source_path, FileAccess.READ)
if file == null:
file = FileAccess.open(source_path, FileAccess.WRITE)
if file == null:
var file := File.new()
var success := file.open(source_path, File.READ)
if success != OK:
success = file.open(source_path, File.WRITE)
if success != OK:
printerr("Failed to open file %s"%[ProjectSettings.globalize_path(source_path)])
return false
return true
@ -40,12 +44,14 @@ func start_entry(entry_name: String) -> void:
current_entry.closed = false
if entry_name in entries_names:
current_entry.previous_total = entries_names[entry_name]
var file := FileAccess.open(source_path, FileAccess.READ_WRITE)
if file == null:
var file := File.new()
var success := file.open(source_path, File.READ_WRITE)
if success != OK:
printerr("Could not open file")
return
entries.append(current_entry)
file.store_csv_line(current_entry.to_csv_line())
emit_signal("entry_started")
func update() -> void:
@ -56,6 +62,7 @@ func update() -> void:
func close_entry() -> void:
current_entry.closed = true
save()
emit_signal("entry_stopped")
func get_period() -> String:
@ -67,8 +74,9 @@ func get_total_elapsed_seconds() -> int:
func save() -> void:
var file := FileAccess.open(source_path, FileAccess.WRITE)
if file == null:
var file := File.new()
var success := file.open(source_path, File.WRITE)
if success != OK:
printerr("Could not open file")
return
for time_entry in entries:
@ -76,9 +84,9 @@ func save() -> void:
static func restore(file_path: String) -> TimeSheet:
var timesheet := TimeSheet.new()
var timesheet = load("res://scripts/time_sheet.gd").new()
timesheet.source_path = file_path
var success := timesheet.load_file()
var success: bool = timesheet.load_file()
if success:
return timesheet
return null