22class_name GaeaFileList
33extends VBoxContainer
44
5+ enum Action {
6+ NEW_GRAPH ,
7+ OPEN ,
8+ OPEN_RECENT ,
9+ SAVE ,
10+ SAVE_AS ,
11+ CLOSE ,
12+ CLOSE_ALL ,
13+ CLOSE_OTHER ,
14+ COPY_PATH ,
15+ SHOW_IN_FILESYSTEM ,
16+ OPEN_IN_INSPECTOR ,
17+ }
18+
519
620const GRAPH_ICON := preload ("uid://cerisdpavr7v3" )
721
822@export var graph_edit : GaeaGraphEdit
923@export var main_editor : GaeaMainEditor
10- @export var menu_bar : MenuBar
24+ @export var menu_bar : GaeaFileListMenuBar
1125@export var file_list : ItemList
1226@export var context_menu : GaeaPopupFileContextMenu
1327@export var file_dialog : FileDialog
@@ -16,28 +30,13 @@ var edited_graphs: Array[EditedGraph]
1630var _current_saving_graph : GaeaGraph = null
1731
1832
19- func _ready () -> void :
20- if is_part_of_edited_scene ():
21- return
22-
23- file_list .item_selected .connect (_on_item_selected )
24- file_list .item_clicked .connect (_on_item_clicked )
25-
26- context_menu .close_file_selected .connect (close_file )
27- context_menu .close_all_selected .connect (close_all )
28- context_menu .close_others_selected .connect (close_others )
29- context_menu .save_as_selected .connect (_start_save_as )
30- context_menu .file_saved .connect (_on_file_saved )
31- context_menu .unsaved_file_found .connect (_on_unsaved_file_found )
32-
33- menu_bar .open_file_selected .connect (open_file )
34- menu_bar .create_new_graph_selected .connect (_start_new_graph_creation )
3533
36- file_dialog .file_selected .connect (_on_file_dialog_file_selected )
37- file_dialog .canceled .connect (_on_file_dialog_canceled )
34+ #region Opening
35+ func open_file_from_path (path : String ) -> void :
36+ if not path .is_empty ():
37+ open_file (load (path ))
3838
3939
40- #region Opening
4140func open_file (graph : GaeaGraph ) -> void :
4241 if not is_instance_valid (graph ):
4342 return
@@ -57,9 +56,9 @@ func open_file(graph: GaeaGraph) -> void:
5756 file_list .set_item_tooltip (idx , graph .resource_path )
5857 file_list .select (idx )
5958
60- _on_item_selected (idx )
6159 var edited_graph := EditedGraph .new (graph )
6260 edited_graphs .append (edited_graph )
61+ _on_item_selected (idx )
6362 edited_graph .dirty_changed .connect (_on_edited_graph_dirty_changed .bind (edited_graph ))
6463#endregion
6564
@@ -97,6 +96,21 @@ func _remove(idx: int) -> void:
9796
9897
9998#region Saving
99+ func save (file : GaeaGraph ) -> void :
100+ if file .resource_path .is_empty ():
101+ _on_unsaved_file_found (file )
102+ return
103+
104+ if not file .is_built_in ():
105+ ResourceSaver .save (file )
106+ else :
107+ var scene_path := file .resource_path .get_slice ("::" , 0 )
108+ ResourceSaver .save (load (scene_path ))
109+ # Necessary for open scenes.
110+ EditorInterface .reload_scene_from_path (scene_path )
111+ _on_file_saved (file )
112+
113+
100114func _start_save_as (file : GaeaGraph ) -> void :
101115 file_dialog .title = "Save Graph As..."
102116 var path : String = "res://"
@@ -145,7 +159,6 @@ func _on_unsaved_file_found(file: GaeaGraph) -> void:
145159func _on_item_clicked (index : int , _at_position : Vector2 , mouse_button_index : int ) -> void :
146160 if mouse_button_index == MOUSE_BUTTON_RIGHT :
147161 main_editor .move_popup_at_mouse (context_menu )
148- context_menu .graph = file_list .get_item_metadata (index )
149162 context_menu .popup ()
150163 elif mouse_button_index == MOUSE_BUTTON_MIDDLE :
151164 _remove (index )
@@ -159,15 +172,15 @@ func _on_item_selected(index: int) -> void:
159172 if metadata is not GaeaGraph or not is_instance_valid (metadata ):
160173 return
161174
162- graph_edit .unpopulate ()
163- graph_edit .populate (metadata )
175+ if graph_edit .graph != metadata :
176+ graph_edit .unpopulate ()
177+ graph_edit .populate (metadata )
164178
165179 var edited : Object = EditorInterface .get_inspector ().get_edited_object ()
166180 if edited is not GaeaGenerator or (edited as GaeaGenerator ).graph != metadata :
167181 EditorInterface .inspect_object .call_deferred (metadata )
168182
169183
170-
171184func _on_file_dialog_file_selected (path : String ) -> void :
172185 var extension : String = path .get_extension ()
173186 if extension .is_empty ():
@@ -206,6 +219,43 @@ func _on_edited_graph_dirty_changed(new_value: bool, edited_graph: EditedGraph)
206219 if new_value == true :
207220 text += "(*)"
208221 file_list .set_item_text (idx , text )
222+
223+
224+ func can_do_action (id : Action ) -> bool :
225+ match id :
226+ Action .NEW_GRAPH , Action .OPEN :
227+ return true
228+ Action .OPEN_RECENT :
229+ return not menu_bar .is_history_empty ()
230+ _ :
231+ return not edited_graphs .is_empty ()
232+
233+
234+ func _on_action_pressed (id : Action ) -> void :
235+ match id :
236+ Action .NEW_GRAPH :
237+ _start_new_graph_creation ()
238+ Action .OPEN :
239+ EditorInterface .popup_quick_open (open_file_from_path , [& "GaeaGraph" ])
240+ Action .SAVE :
241+ save (graph_edit .graph )
242+ Action .SAVE_AS :
243+ _start_save_as (graph_edit .graph )
244+ Action .CLOSE :
245+ close_file (graph_edit .graph )
246+ Action .CLOSE_ALL :
247+ close_all ()
248+ Action .CLOSE_OTHER :
249+ close_others (graph_edit .graph )
250+ Action .COPY_PATH :
251+ DisplayServer .clipboard_set (graph_edit .graph .resource_path )
252+ Action .SHOW_IN_FILESYSTEM :
253+ if not graph_edit .graph .is_built_in ():
254+ EditorInterface .select_file (graph_edit .graph .resource_path )
255+ else :
256+ EditorInterface .select_file (graph_edit .graph .resource_path .get_slice ("::" , 0 ))
257+ Action .OPEN_IN_INSPECTOR :
258+ EditorInterface .edit_resource (graph_edit .graph )
209259#endregion
210260
211261
0 commit comments