load scene by state

This commit is contained in:
Fabien Freling 2023-12-07 23:14:24 +01:00
parent 810a3aa9b5
commit d1992c62d9
5 changed files with 35 additions and 9 deletions

View file

@ -23,13 +23,13 @@ func _ready():
var db_path = config.get_value("general", "db_path") var db_path = config.get_value("general", "db_path")
load_db(db_path) load_db(db_path)
func switch_state(new: State): func switch_state(new: State):
var old = current_state var old = current_state
if old == new: if old == new:
return return
current_state = new current_state = new
emit_signal("state_changed", old, new)
#signal state_changed(old, new) #signal state_changed(old, new)
func load_db(path: String): func load_db(path: String):
@ -38,8 +38,10 @@ func load_db(path: String):
if success: if success:
config.set_value("general", "db_path", path) config.set_value("general", "db_path", path)
config.save(config_path) config.save(config_path)
switch_state(State.LOADED)
else: else:
db.path = "" db.path = ""
switch_state(State.UNLOADED)
func goto_scene(path): func goto_scene(path):
call_deferred("_deferred_goto_scene", path) call_deferred("_deferred_goto_scene", path)

7
godot/loaded.tscn Normal file
View file

@ -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 !"

View file

@ -6,6 +6,16 @@ func _ready():
$FileDialog.access = FileDialog.ACCESS_FILESYSTEM $FileDialog.access = FileDialog.ACCESS_FILESYSTEM
$FileDialog.use_native_dialog = true $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. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(delta):
pass pass
@ -16,3 +26,7 @@ func _on_button_pressed():
func _on_file_dialog_file_selected(path): func _on_file_dialog_file_selected(path):
Global.load_db(path) Global.load_db(path)
func _update_state(old: Global.State, new: Global.State):
print("updated state")
pass

View file

@ -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="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"] [node name="Control" type="Control"]
layout_mode = 3 layout_mode = 3
@ -24,15 +25,9 @@ grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
metadata/_edit_use_anchors_ = true metadata/_edit_use_anchors_ = true
[node name="CenterContainer" type="CenterContainer" parent="MarginContainer"] [node name="Unloaded" parent="MarginContainer" instance=ExtResource("2_diny5")]
layout_mode = 2 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="."] [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"] [connection signal="file_selected" from="FileDialog" to="." method="_on_file_dialog_file_selected"]

8
godot/unloaded.tscn Normal file
View file

@ -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"