From 4711a47e2e676627b0c69efa3f753e38af81d905 Mon Sep 17 00:00:00 2001 From: myin Date: Sat, 7 Oct 2023 16:55:00 +0200 Subject: [PATCH] update files --- godot/addons/debug/Debug.gd | 130 ------------------ godot/addons/debug/Debug.tscn | 47 ------- godot/addons/debug/Logger.gd | 31 ++--- godot/addons/debug/README.md | 8 -- .../save-system/settings/AudioSettings.gd | 28 ++++ godot/addons/save-system/settings/Settings.gd | 30 ++++ godot/project.godot | 6 - godot/shared/TweenCreator.gd | 38 ++--- 8 files changed, 92 insertions(+), 226 deletions(-) delete mode 100644 godot/addons/debug/Debug.gd delete mode 100644 godot/addons/debug/Debug.tscn delete mode 100644 godot/addons/debug/README.md create mode 100644 godot/addons/save-system/settings/AudioSettings.gd create mode 100644 godot/addons/save-system/settings/Settings.gd diff --git a/godot/addons/debug/Debug.gd b/godot/addons/debug/Debug.gd deleted file mode 100644 index 6155a9c..0000000 --- a/godot/addons/debug/Debug.gd +++ /dev/null @@ -1,130 +0,0 @@ -extends CanvasLayer - -# Order used to check what should be printed -# More verbose the further below the level -enum Level { - OFF, - ERROR, - INFO, - WARN, - DEBUG, - TRACE, -} - -const WINDOW_STRETCH = "display/window/stretch/mode" -const APPLICATION = "application/config/name" - -@export var lines: VBoxContainer -@export var scroll: ScrollContainer -@export var input: LineEdit - -@export var max_lines := 100 - -var log_level = Level.DEBUG - -var _original_zoom = null -var _commands = { - "/help": { - "action": func(x): _help_cmd(x) - }, - "/version": { - "desc": "Print game name with version", - "action": func(x): _version_cmd(x) - }, - "/logging": { - "desc": "Print or change logging level " + str(Level.keys()), - "action": func(x): _logging_cmd(x) - }, - "/zoom": { - "desc": "Set zoom of camera. Call without arguments to reset to original zoom", - "action": func(x): _zoom_cmd(x) - } -} - -func _ready(): - hide() - if Env.is_prod(): - log_level = Level.INFO - - _version_cmd() - _logging_cmd() - - -func _unhandled_input(event): - if event.is_action_pressed("dev"): - visible = !visible - if visible: - input.grab_focus() - - -func print_line(str: String): - if lines.get_child_count() >= max_lines: - lines.remove_child(lines.get_child(0)) - - var line = Label.new() - line.text = str - lines.add_child(line) - scroll.scroll_vertical = scroll.get_v_scroll_bar().max_value - - -func _on_text_edit_text_submitted(new_text): - if new_text != "": - _on_command_input(new_text) - input.clear() - - -func _on_command_input(input: String): - if not input.begins_with("/"): - print_line(input) - return - - var args = input.split(" ") - var cmd = args[0] - - if cmd in _commands: - _commands[cmd]["action"].call(args.slice(1)) - else: - print_line("Unknown command: %s" % cmd) - - -func _help_cmd(args: Array[String] = []): - print_line("Available commands:") - for cmd in _commands: - var obj = _commands[cmd] - if 'desc' in obj: - print_line("%s: %s" % [cmd, obj["desc"]]) - - -func _version_cmd(args: Array[String] = []): - print_line("Playing %s in version %s" % [ProjectSettings.get(APPLICATION), Env.version]) - - -func _logging_cmd(args: Array[String] = []): - if args.size() < 1: - print_line("Logging level: %s" % Level.keys()[log_level]) - else: - var level = args[0] - var new_level = Level[level] - if new_level != null: - log_level = new_level - print_line("Changed logging level to %s" % level) - else: - print_line("Unknown logging level %s" % level) - - -func _zoom_cmd(args: Array[String] = []): - var cam = get_viewport().get_camera_2d() - if args.size() == 0: - if _original_zoom != null: - cam.zoom = Vector2(_original_zoom, _original_zoom) - _original_zoom = null - print_line("Restore camera zoom to %s" % _original_zoom) - else: - if args[0].is_valid_float(): - var zoom = float(args[0]) - if _original_zoom == null: - _original_zoom = cam.zoom.x - cam.zoom = Vector2(zoom, zoom) - print_line("Set camera zoom to %s" % zoom) - else: - print_line("Invalid zoom value for camera: %s" % args[0]) diff --git a/godot/addons/debug/Debug.tscn b/godot/addons/debug/Debug.tscn deleted file mode 100644 index 2318976..0000000 --- a/godot/addons/debug/Debug.tscn +++ /dev/null @@ -1,47 +0,0 @@ -[gd_scene load_steps=3 format=3 uid="uid://cib1n1b248gx6"] - -[ext_resource type="Script" path="res://addons/debug/Debug.gd" id="1"] -[ext_resource type="Theme" uid="uid://cvvavdkxk7llv" path="res://theme/theme.tres" id="2_6fu5f"] - -[node name="Debug" type="CanvasLayer" node_paths=PackedStringArray("lines", "scroll", "input")] -layer = 100 -script = ExtResource("1") -lines = NodePath("MarginContainer/VBoxContainer/ScrollContainer/VBoxContainer") -scroll = NodePath("MarginContainer/VBoxContainer/ScrollContainer") -input = NodePath("MarginContainer/VBoxContainer/TextEdit") - -[node name="ColorRect" type="ColorRect" parent="."] -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -color = Color(0, 0, 0, 0.705882) - -[node name="MarginContainer" type="MarginContainer" parent="."] -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 -theme = ExtResource("2_6fu5f") -theme_override_constants/margin_left = 30 -theme_override_constants/margin_top = 30 -theme_override_constants/margin_right = 30 -theme_override_constants/margin_bottom = 30 - -[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"] -layout_mode = 2 - -[node name="ScrollContainer" type="ScrollContainer" parent="MarginContainer/VBoxContainer"] -layout_mode = 2 -size_flags_vertical = 3 -follow_focus = true -horizontal_scroll_mode = 0 - -[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer/VBoxContainer/ScrollContainer"] -layout_mode = 2 - -[node name="TextEdit" type="LineEdit" parent="MarginContainer/VBoxContainer"] -layout_mode = 2 -caret_blink = true - -[connection signal="text_submitted" from="MarginContainer/VBoxContainer/TextEdit" to="." method="_on_text_edit_text_submitted"] diff --git a/godot/addons/debug/Logger.gd b/godot/addons/debug/Logger.gd index 4aaea69..b659c1d 100644 --- a/godot/addons/debug/Logger.gd +++ b/godot/addons/debug/Logger.gd @@ -1,38 +1,38 @@ class_name Logger -var name = "" +enum Level { + OFF, + ERROR, + WARN, + INFO, + DEBUG, + TRACE, +} +var name = "" func _init(n: String): name = n - func info(msg: String): - _log_for_level(Debug.Level.INFO, msg) - + _log_for_level(Level.INFO, msg) func warn(msg: String): - _log_for_level(Debug.Level.WARN, msg) - + _log_for_level(Level.WARN, msg) func error(msg: String): - _log_for_level(Debug.Level.ERROR, msg) - + _log_for_level(Level.ERROR, msg) func debug(msg: String): - _log_for_level(Debug.Level.DEBUG, msg) - + _log_for_level(Level.DEBUG, msg) func trace(msg: String): - _log_for_level(Debug.Level.TRACE, msg) - + _log_for_level(Level.TRACE, msg) func _log_for_level(level: int, msg: String): - if level <= Debug.log_level: + if level <= Env.log_level: var text = "[%s - %s]: %s" % [_now(), name, msg] print(text) - Debug.print_line(text) - func _now() -> String: var dt = Time.get_datetime_dict_from_system() @@ -44,7 +44,6 @@ func _now() -> String: var second = _leading_zero(dt["second"]) return "%s-%s-%s %s:%s:%s" % [year, month, day, hour, minute, second] - func _leading_zero(num: int) -> String: if num < 10: return "0" + str(num) diff --git a/godot/addons/debug/README.md b/godot/addons/debug/README.md deleted file mode 100644 index 95cf960..0000000 --- a/godot/addons/debug/README.md +++ /dev/null @@ -1,8 +0,0 @@ -# Debug - -- `Env` is autoloaded to provide environment information -- using the `dev` key (default F12), opens a debug window - - view version - - change logging level -- `Logger` for logging information with various logging levels -- `Build` will be overwritten by the CI for build information diff --git a/godot/addons/save-system/settings/AudioSettings.gd b/godot/addons/save-system/settings/AudioSettings.gd new file mode 100644 index 0000000..c75ccea --- /dev/null +++ b/godot/addons/save-system/settings/AudioSettings.gd @@ -0,0 +1,28 @@ +class_name AudioSettings +extends Control + +signal loaded() + +const AUDIO_SECTION = "audio" + +var _logger = Logger.new("AudioSettings") + +func load_settings(config: ConfigFile): + for i in range(0, AudioServer.bus_count): + var name = AudioServer.get_bus_name(i) + var value = config.get_value(AUDIO_SECTION, name) + if value != null: + var split = str(value).split(";") + _logger.debug("Loading audio setting for %s: %s" % [name, value]) + AudioServer.set_bus_volume_db(i, float(split[0])) + AudioServer.set_bus_mute(i, split.size() > 1 and split[1] == "true") + + loaded.emit() + +func save_settings(config: ConfigFile): + for i in range(0, AudioServer.bus_count): + var name = AudioServer.get_bus_name(i) + var value = AudioServer.get_bus_volume_db(i) + var muted = AudioServer.is_bus_mute(i) + _logger.debug("Saving audio setting for %s: %s, %s" % [name, value, muted]) + config.set_value(AUDIO_SECTION, name, "%s;%s" % [value, muted]) diff --git a/godot/addons/save-system/settings/Settings.gd b/godot/addons/save-system/settings/Settings.gd new file mode 100644 index 0000000..9721eb9 --- /dev/null +++ b/godot/addons/save-system/settings/Settings.gd @@ -0,0 +1,30 @@ +class_name Settings +extends Control + +const CONFIG_FILE = "user://settings.cfg" + +@onready var _audio := $Audio + +var _logger = Logger.new("Settings") +var _config = ConfigFile.new() + +func _ready(): + _load_settings() + +func _load_settings(): + var error = _config.load(CONFIG_FILE) + if error != OK: + _logger.error("Failed to load settings: %s" % error) + + _logger.debug("Loading settings") + _audio.load_settings(_config) + +func _exit_tree(): + _save_config() + +func _save_config(): + if Env.is_web(): return + + _logger.debug("Saving settings") + _audio.save_settings(_config) + _config.save(CONFIG_FILE) diff --git a/godot/project.godot b/godot/project.godot index 00ba7d9..12901b2 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -20,7 +20,6 @@ config/icon="res://icon.svg" Env="*res://addons/debug/Env.gd" SceneManager="*res://addons/scene-manager/SceneManager.tscn" -Debug="*res://addons/debug/Debug.tscn" [debug] @@ -77,11 +76,6 @@ move_right={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null) ] } -dev={ -"deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194343,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) -] -} [layer_names] diff --git a/godot/shared/TweenCreator.gd b/godot/shared/TweenCreator.gd index acc536d..7d8b20f 100644 --- a/godot/shared/TweenCreator.gd +++ b/godot/shared/TweenCreator.gd @@ -28,28 +28,28 @@ func new_tween(on_finish = null): tw.finished.connect(on_finish) return true -func fade_out(node, duration = default_duration): - return tw.tween_property(node, "modulate", Color.TRANSPARENT, duration).from(Color.WHITE).set_ease(Tween.EASE_IN) +func fade_out(n, duration = default_duration): + return prop(n, "modulate", Color.WHITE, Color.TRANSPARENT, duration).set_ease(Tween.EASE_IN) -func fade_in(node, duration = default_duration): - return tw.tween_property(node, "modulate", Color.WHITE, duration).from(Color.TRANSPARENT).set_ease(Tween.EASE_OUT) +func fade_in(n, duration = default_duration): + return prop(n, "modulate", Color.TRANSPARENT, Color.WHITE, duration).set_ease(Tween.EASE_OUT) -func slide_in(node, dir: Vector2, pos = Vector2.ZERO, dist = node.size, duration = default_duration): - return move(node, pos - dir * dist, pos, duration).set_ease(Tween.EASE_OUT) +func slide_in(n, dir: Vector2, pos = Vector2.ZERO, dist = n.size, duration = default_duration): + return prop(n, "position", pos - dir * dist, pos, duration).set_ease(Tween.EASE_OUT) -func slide_out(node, dir: Vector2, dist = node.size, duration = default_duration): - return move(node, node.position, node.position + dir * dist, duration).set_ease(Tween.EASE_IN) +func slide_out(n, dir: Vector2, dist = n.size, duration = default_duration): + return prop(n, "position", n.position, n.position + dir * dist, duration).set_ease(Tween.EASE_IN) -func move(node, from: Vector2, to: Vector2, duration = default_duration): - return tw.tween_property(node, "position", to, duration).from(from) - -func scale_in(node, target_scale = Vector2(1, 1), duration = default_duration): - return scale(node, Vector2.ZERO, target_scale, duration).set_ease(Tween.EASE_OUT) +func scale_in(n, target_scale = Vector2(1, 1), duration = default_duration): + return scale(n, Vector2.ZERO, target_scale, duration).set_ease(Tween.EASE_OUT) -func scale_out(node, init_scale = Vector2(1, 1), duration = default_duration): - return scale(node, init_scale, Vector2.ZERO, duration).set_ease(Tween.EASE_IN) +func scale_out(n, init_scale = Vector2(1, 1), duration = default_duration): + return scale(n, init_scale, Vector2.ZERO, duration).set_ease(Tween.EASE_IN) -func scale(node, from: Vector2, to: Vector2, duration = default_duration): - if node is Control: - node.pivot_offset = node.size / 2 - return tw.tween_property(node, "scale", to, duration).from(from) +func scale(n, from: Vector2, to: Vector2, duration = default_duration): + if n is Control: + n.pivot_offset = n.size / 2 + return prop(n, "scale", from, to, duration) + +func prop(n, p: String, from, to, duration = default_duration): + return tw.tween_property(n, p, to, duration).from(from)