From d1992c62d9c407703a67d9739708b5b991b137b7 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Thu, 7 Dec 2023 23:14:24 +0100 Subject: [PATCH] load scene by state --- godot/global.gd | 4 +++- godot/loaded.tscn | 7 +++++++ godot/main.gd | 14 ++++++++++++++ godot/main.tscn | 11 +++-------- godot/unloaded.tscn | 8 ++++++++ 5 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 godot/loaded.tscn create mode 100644 godot/unloaded.tscn diff --git a/godot/global.gd b/godot/global.gd index 4c1e83b..e9528ea 100644 --- a/godot/global.gd +++ b/godot/global.gd @@ -22,7 +22,6 @@ func _ready(): if err == OK: var db_path = config.get_value("general", "db_path") load_db(db_path) - func switch_state(new: State): var old = current_state @@ -30,6 +29,7 @@ func switch_state(new: State): return current_state = new + emit_signal("state_changed", old, new) #signal state_changed(old, new) func load_db(path: String): @@ -38,8 +38,10 @@ func load_db(path: String): if success: config.set_value("general", "db_path", path) config.save(config_path) + switch_state(State.LOADED) else: db.path = "" + switch_state(State.UNLOADED) func goto_scene(path): call_deferred("_deferred_goto_scene", path) diff --git a/godot/loaded.tscn b/godot/loaded.tscn new file mode 100644 index 0000000..1f30306 --- /dev/null +++ b/godot/loaded.tscn @@ -0,0 +1,7 @@ +[gd_scene format=3 uid="uid://h1sdn3cjclqi"] + +[node name="CenterContainer" type="CenterContainer"] + +[node name="Label" type="Label" parent="."] +layout_mode = 2 +text = "Loaded !" diff --git a/godot/main.gd b/godot/main.gd index 5912bbb..04bde40 100644 --- a/godot/main.gd +++ b/godot/main.gd @@ -5,6 +5,16 @@ func _ready(): $FileDialog.mode = FileDialog.FILE_MODE_OPEN_FILE $FileDialog.access = FileDialog.ACCESS_FILESYSTEM $FileDialog.use_native_dialog = true + + Global.state_changed.connect(_update_state) + if Global.current_state == Global.State.UNLOADED: + pass + elif Global.current_state == Global.State.LOADED: + $MarginContainer.remove_child($MarginContainer/Unloaded) + var s = ResourceLoader.load("res://loaded.tscn") + var current_scene = s.instantiate() + $MarginContainer.add_child(current_scene) + pass # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): @@ -16,3 +26,7 @@ func _on_button_pressed(): func _on_file_dialog_file_selected(path): Global.load_db(path) + +func _update_state(old: Global.State, new: Global.State): + print("updated state") + pass diff --git a/godot/main.tscn b/godot/main.tscn index 59546ee..fb18b4a 100644 --- a/godot/main.tscn +++ b/godot/main.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=2 format=3 uid="uid://y5v0kmse8n10"] +[gd_scene load_steps=3 format=3 uid="uid://y5v0kmse8n10"] [ext_resource type="Script" path="res://main.gd" id="1_2tuu3"] +[ext_resource type="PackedScene" uid="uid://b71wpb1vts86r" path="res://unloaded.tscn" id="2_diny5"] [node name="Control" type="Control"] layout_mode = 3 @@ -24,15 +25,9 @@ grow_horizontal = 2 grow_vertical = 2 metadata/_edit_use_anchors_ = true -[node name="CenterContainer" type="CenterContainer" parent="MarginContainer"] +[node name="Unloaded" parent="MarginContainer" instance=ExtResource("2_diny5")] layout_mode = 2 -[node name="Button" type="Button" parent="MarginContainer/CenterContainer"] -custom_minimum_size = Vector2(200, 200) -layout_mode = 2 -text = "Load database" - [node name="FileDialog" type="FileDialog" parent="."] -[connection signal="pressed" from="MarginContainer/CenterContainer/Button" to="." method="_on_button_pressed"] [connection signal="file_selected" from="FileDialog" to="." method="_on_file_dialog_file_selected"] diff --git a/godot/unloaded.tscn b/godot/unloaded.tscn new file mode 100644 index 0000000..62acad0 --- /dev/null +++ b/godot/unloaded.tscn @@ -0,0 +1,8 @@ +[gd_scene format=3 uid="uid://b71wpb1vts86r"] + +[node name="CenterContainer" type="CenterContainer"] + +[node name="Button" type="Button" parent="."] +custom_minimum_size = Vector2(200, 200) +layout_mode = 2 +text = "Load database"