Skip to content

Commit 187e4eb

Browse files
cullumiBenjaTK
andauthored
GaeaGraph deep duplication + Duplicate button (#431)
* Added a custom _duplicate method for to GaeaGraph Still needs to be set up to properly duplicate collections * Added a duplicate button to the Gaea editor panel; used GaeaGraph.duplicate and a file dialog to duplicate the graph. * Light tweaks to GaeaGraph (added _get_unique_resources method), added duplicate_test (just a copy of generate_test for now). * Gave the Duplicate button an icon * GaeaGraph now properly initializes (by calling _setup_local_to_scene) when assigned to a generator. * Removed (incomplete) duplicate test * Simplified GaeaGraph's _duplicate method to only what actually gets saved to it's resource file. * Added a class name to walker_demo * Added a series of graph tests (assign_to_generator, duplicate, assign_duplicate) * Added github workflow for new Graph testing * Added the contents of testing\graph\graph_test.gd into testing\other\gaea_graph.gd * generator.gd is no longer calling GaeaGraph's private _refresh method. Instead _refresh gets called upon setting the generator property. * Using duplicate_deep for duplication of GaeaGraph. * Added tooltip for the Duplicate button, and elaborated the Load button. * Added compare_dictionaries helper method to gaea_graph testing class * Added proper assertions to duplicate and assign_to_generator methods. Deleted assign_duplicate test (it was redundant). * Renamed "duplicate" stuff to "duplicate_graph" for clarity, in case other kinds of duplication appear later. * Naming, typos and formatting tweaks. * Added get_raw_connections and get_all_node_data as getters for _connections and _node_data respectively * Fixed trailing whitespace --------- Co-authored-by: BenjaTK <73806216+BenjaTK@users.noreply.github.com>
1 parent d6acc78 commit 187e4eb

File tree

6 files changed

+657
-17
lines changed

6 files changed

+657
-17
lines changed

addons/gaea/editor/panel.gd

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ var _dragged_from_left: bool = false
2828
@onready var _search_bar: LineEdit = %SearchBar
2929
@onready var _save_button: Button = %SaveButton
3030
@onready var _load_button: Button = %LoadButton
31+
@onready var _duplicate_graph_button: Button = %DuplicateGraphButton
3132
@onready var _reload_node_tree_button: Button = %ReloadNodeTreeButton
3233
@onready var _reload_parameters_list_button: Button = %ReloadParametersListButton
33-
@onready var _file_dialog: FileDialog = $FileDialog
34+
@onready var _file_open_dialog: FileDialog = $OpenFileDialog
35+
@onready var _file_save_dialog: FileDialog = $SaveFileDialog
3436
@onready var _online_docs_button: Button = %OnlineDocsButton
3537
@onready var _window_popout_separator: VSeparator = %WindowPopoutSeparator
3638
@onready var _window_popout_button: Button = %WindowPopoutButton
@@ -52,6 +54,9 @@ func _ready() -> void:
5254
_reload_parameters_list_button.icon = preload("uid://cwg7oy4i2cbwq")
5355
_save_button.icon = EditorInterface.get_base_control().get_theme_icon(&"Save", &"EditorIcons")
5456
_load_button.icon = EditorInterface.get_base_control().get_theme_icon(&"Load", &"EditorIcons")
57+
_duplicate_graph_button.icon = EditorInterface.get_base_control().get_theme_icon(
58+
&"Duplicate", &"EditorIcons"
59+
)
5560
_window_popout_button.icon = EditorInterface.get_base_control().get_theme_icon(
5661
&"MakeFloating", &"EditorIcons"
5762
)
@@ -539,6 +544,10 @@ func _on_graph_edit_connection_from_empty(
539544

540545

541546
#region Buttons
547+
func _on_duplicate_graph_button_pressed() -> void:
548+
_file_save_dialog.popup_centered()
549+
550+
542551
func _on_generate_button_pressed() -> void:
543552
_selected_generator.generate()
544553

@@ -547,13 +556,18 @@ func _on_reload_node_tree_button_pressed() -> void:
547556
_create_node_tree.populate()
548557

549558
func _on_load_button_pressed() -> void:
550-
_file_dialog.popup_centered()
559+
_file_open_dialog.popup_centered()
551560

552561

553-
func _on_file_dialog_file_selected(path: String) -> void:
562+
func _on_open_file_dialog_selected(path: String) -> void:
554563
_selected_generator.data = load(path)
555564

556565

566+
func _on_save_file_dialog_selected(path: String) -> void:
567+
var copy: GaeaGraph = _selected_generator.data.duplicate_deep(Resource.DEEP_DUPLICATE_INTERNAL)
568+
ResourceSaver.save(copy, path)
569+
570+
557571
func _on_reload_parameters_list_button_pressed() -> void:
558572
if (
559573
not is_instance_valid(_selected_generator)

addons/gaea/editor/panel.tscn

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,19 @@ flat = true
128128

129129
[node name="LoadButton" type="Button" parent="Editor/VBoxContainer/HBoxContainer"]
130130
unique_name_in_owner = true
131-
visible = false
132131
layout_mode = 2
133-
tooltip_text = "Load data."
132+
tooltip_text = "Assign a graph to this generator."
134133
icon = SubResource("ImageTexture_ve3i7")
135134
flat = true
136135

136+
[node name="DuplicateGraphButton" type="Button" parent="Editor/VBoxContainer/HBoxContainer"]
137+
unique_name_in_owner = true
138+
layout_mode = 2
139+
size_flags_horizontal = 8
140+
tooltip_text = "Duplicate this graph."
141+
flat = true
142+
137143
[node name="VSeparator" type="VSeparator" parent="Editor/VBoxContainer/HBoxContainer"]
138-
visible = false
139144
layout_mode = 2
140145

141146
[node name="ReloadNodeTreeButton" type="Button" parent="Editor/VBoxContainer/HBoxContainer"]
@@ -373,19 +378,25 @@ item_0/id = 0
373378
script = ExtResource("7_dhpru")
374379
graph_edit = NodePath("../VBoxContainer/GraphEdit")
375380

376-
[node name="FileDialog" type="FileDialog" parent="."]
381+
[node name="OpenFileDialog" type="FileDialog" parent="."]
377382
title = "Open a File"
378383
ok_button_text = "Open"
379384
file_mode = 0
380385
filters = PackedStringArray("*.tres,*.res;Resources")
381386

387+
[node name="SaveFileDialog" type="FileDialog" parent="."]
388+
auto_translate_mode = 1
389+
title = "Save a Copy"
390+
filters = PackedStringArray("*.tres,*.res;Resources")
391+
382392
[node name="AboutWindow" parent="." instance=ExtResource("9_affj1")]
383393
visible = false
384394
exclusive = true
385395

386396
[connection signal="pressed" from="NoData/NewDataButton" to="." method="_on_new_data_button_pressed"]
387397
[connection signal="pressed" from="Editor/VBoxContainer/HBoxContainer/SaveButton" to="." method="_on_save_button_pressed"]
388398
[connection signal="pressed" from="Editor/VBoxContainer/HBoxContainer/LoadButton" to="." method="_on_load_button_pressed"]
399+
[connection signal="pressed" from="Editor/VBoxContainer/HBoxContainer/DuplicateGraphButton" to="." method="_on_duplicate_graph_button_pressed"]
389400
[connection signal="pressed" from="Editor/VBoxContainer/HBoxContainer/ReloadNodeTreeButton" to="." method="_on_reload_node_tree_button_pressed"]
390401
[connection signal="pressed" from="Editor/VBoxContainer/HBoxContainer/ReloadParametersListButton" to="." method="_on_reload_parameters_list_button_pressed"]
391402
[connection signal="pressed" from="Editor/VBoxContainer/HBoxContainer/GenerateButton" to="." method="_on_generate_button_pressed"]
@@ -419,4 +430,5 @@ exclusive = true
419430
[connection signal="pressed" from="Editor/CreateNodePopup/MainContentContainer/VBox/HBoxContainer/CreateButton" to="Editor/CreateNodePopup/MainContentContainer/VBox/CreateNodeTree" method="_on_create_button_pressed"]
420431
[connection signal="create_node_popup_requested" from="Editor/ContextMenu" to="." method="_popup_create_node_menu_at_mouse"]
421432
[connection signal="new_reroute_requested" from="Editor/LinkPopup" to="." method="_on_new_reroute_requested"]
422-
[connection signal="file_selected" from="FileDialog" to="." method="_on_file_dialog_file_selected"]
433+
[connection signal="file_selected" from="OpenFileDialog" to="." method="_on_open_file_dialog_selected"]
434+
[connection signal="file_selected" from="SaveFileDialog" to="." method="_on_save_file_dialog_selected"]

addons/gaea/resources/gaea_graph.gd

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ const CURRENT_SAVE_VERSION := 5
4444
## "from_node-from_port-to_node-to_port" (ex.: 1-0-2-1). That format
4545
## can be converted into a connections dictionary using various methods in this class.[br]
4646
## [br][color=yellow][b]Warning:[/b][/color] Setting this directly can break your saved graph.
47-
@export_storage var _connections: Array[StringName]
47+
@export_storage var _connections: Array[StringName] :
48+
get = get_raw_connections
4849
## Saved data for each [GaeaNodeResource] such as position in the graph and changed arguments.
4950
## [br][color=yellow][b]Warning:[/b][/color] Setting this directly can break your saved graph.
50-
@export_storage var _node_data: Dictionary[int, Dictionary]
51+
@export_storage var _node_data: Dictionary[int, Dictionary] :
52+
get = get_all_node_data
5153
## List of parameters created with [GaeaNodeParameter].
5254
## [br][color=yellow][b]Warning:[/b][/color] Setting this directly can break your saved graph.
5355
## Use [method set_parameter] instead.
54-
@export_storage var _parameters: Dictionary[StringName, Variant] : get = get_parameter_list
56+
@export_storage var _parameters: Dictionary[StringName, Variant] :
57+
get = get_parameter_list
5558

5659
## @deprecated: Kept for migration of old save data.
5760
var connections: Array[Dictionary]
@@ -73,7 +76,10 @@ var scroll_offset: Vector2 = Vector2(NAN, NAN)
7376
## in the current session.
7477
var zoom: float = 1.0
7578
## The currently related generator.
76-
var generator: GaeaGenerator
79+
var generator: GaeaGenerator :
80+
set(value):
81+
generator = value
82+
_refresh()
7783
## Cache used during generation to avoid recalculating data unnecessarily.
7884
## The inner dictionary keys are the slot output port names, and the values are the cached data.
7985
var cache: Dictionary[GaeaNodeResource, Dictionary] = {}
@@ -84,7 +90,12 @@ var _resources: Dictionary[int, GaeaNodeResource]
8490

8591

8692
func _init() -> void:
93+
_refresh()
94+
95+
96+
func _refresh() -> void:
8797
resource_local_to_scene = true
98+
_setup_local_to_scene()
8899
notify_property_list_changed()
89100

90101

@@ -293,6 +304,11 @@ func get_ids() -> Array[int]:
293304
return _node_data.keys()
294305

295306

307+
## Returns all node data.
308+
func get_all_node_data() -> Dictionary[int, Dictionary]:
309+
return _node_data
310+
311+
296312
## Returns an available id.
297313
func get_next_available_id() -> int:
298314
var ids := get_ids()
@@ -356,6 +372,11 @@ func get_all_connections() -> Array[Dictionary]:
356372
return all_connections
357373

358374

375+
## Returns all connections in the graph in the form "from_node-from_port-to_node-to_port" (ex.: 1-0-2-1)
376+
func get_raw_connections() -> Array[StringName]:
377+
return _connections
378+
379+
359380
## Returns all connections to and from the specified node as dictionaries.
360381
func get_node_connections(id: int) -> Array[Dictionary]:
361382
return get_connections_to(id) + get_connections_from(id)
@@ -487,6 +508,13 @@ func _setup_local_to_scene() -> void:
487508
GaeaGraphMigration.migrate(self)
488509

489510
_resources.clear()
511+
var uniques = _get_unique_resources()
512+
for id in uniques.keys():
513+
_resources.set(id, uniques[id])
514+
515+
516+
func _get_unique_resources() -> Dictionary[Variant, GaeaNodeResource]:
517+
var uniques: Dictionary[Variant, GaeaNodeResource] = {}
490518
for id in _node_data.keys():
491519
var base_uid: String = get_node_data(id).get(&"uid", "")
492520
if base_uid.is_empty():
@@ -495,6 +523,7 @@ func _setup_local_to_scene() -> void:
495523
var resource: GaeaNodeResource = load(base_uid).new()
496524
if not resource is GaeaNodeResource:
497525
push_error("Something went wrong, the resource at %s is not a GaeaNodeResource" % base_uid)
498-
return
526+
return uniques
499527
resource._load_save_data(data)
500-
_resources.set(id, resource)
528+
uniques.set(id, resource)
529+
return uniques

scenes/walker_demo/walker_demo.gd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
extends Node2D
2-
2+
class_name GaeaWalkerDemo
33

44
@onready var gaea_generator: GaeaGenerator = $GaeaGenerator
55

66
var last_grid: GaeaGrid
77

88

9-
10-
119
## Used for integration testing.
1210
func test_generation(fixed_seed: int = 0) -> void:
1311
gaea_generator.world_size = Vector3i(45, 45, 1)

0 commit comments

Comments
 (0)