Skip to content

Commit f447802

Browse files
authored
Update context menu logic and implement shortcuts (#562)
* Salvage from #562 * Add missing new line * Review comments and fix shortcut * Review comments
1 parent 08b06e1 commit f447802

File tree

8 files changed

+529
-335
lines changed

8 files changed

+529
-335
lines changed

addons/gaea/editor/editor_settings.gd

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,31 @@ static func get_preview_max_simulation_size() -> int:
177177
if editor_interface.get_editor_settings().has_setting(PREVIEW_MAX_SIMULATION_SIZE):
178178
return editor_interface.get_editor_settings().get_setting(PREVIEW_MAX_SIMULATION_SIZE)
179179
return PREVIEW_MAX_SIMULATION_SIZE_DEFAULT
180+
181+
182+
static func get_file_list_action_shortcut(action: GaeaFileList.Action, shortcut_key: Key) -> Shortcut:
183+
return _get_shortcut("file_list", GaeaFileList.Action.find_key(action), shortcut_key)
184+
185+
186+
static func get_node_action_shortcut(action: GaeaGraphEdit.Action, shortcut_key: Key) -> Shortcut:
187+
return _get_shortcut("node_context_menu", GaeaGraphEdit.Action.find_key(action), shortcut_key)
188+
189+
190+
static func _get_shortcut(category: String, action_key: String, shortcut_key: Key) -> Shortcut:
191+
var editor_interface = Engine.get_singleton("EditorInterface")
192+
var shortcut_path = StringName(("gaea/%s - %s" % [category.capitalize(), action_key.capitalize()]))
193+
194+
if editor_interface.get_editor_settings().has_shortcut(shortcut_path):
195+
return editor_interface.get_editor_settings().get_shortcut(shortcut_path)
196+
197+
var shortcut = Shortcut.new()
198+
var key_event = InputEventKey.new()
199+
key_event.keycode = shortcut_key & KeyModifierMask.KEY_CODE_MASK
200+
key_event.command_or_control_autoremap = shortcut_key & KeyModifierMask.KEY_MASK_CMD_OR_CTRL
201+
key_event.alt_pressed = shortcut_key & KeyModifierMask.KEY_MASK_ALT
202+
key_event.shift_pressed = shortcut_key & KeyModifierMask.KEY_MASK_SHIFT
203+
shortcut.events = [key_event]
204+
205+
editor_interface.get_editor_settings().add_shortcut(shortcut_path, shortcut)
206+
207+
return shortcut

addons/gaea/editor/file_list/file_context_menu.gd

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,45 @@ class_name GaeaPopupFileContextMenu
33
extends PopupMenu
44

55

6-
signal close_file_selected(file: GaeaGraph)
7-
signal close_all_selected
8-
signal close_others_selected(file: GaeaGraph)
9-
signal save_as_selected(file: GaeaGraph)
10-
signal file_saved(file: GaeaGraph)
11-
signal unsaved_file_found(file: GaeaGraph)
6+
@export var file_system_container: GaeaFileList
127

13-
enum Action {
14-
SAVE,
15-
SAVE_AS,
16-
CLOSE,
17-
CLOSE_ALL,
18-
CLOSE_OTHER,
19-
COPY_PATH,
20-
SHOW_IN_FILESYSTEM,
21-
OPEN_IN_INSPECTOR
22-
}
23-
24-
var graph: GaeaGraph
258

269
func _ready() -> void:
2710
if is_part_of_edited_scene():
2811
return
2912

3013
clear()
31-
add_item("Save File", Action.SAVE)
32-
add_item("Save File As...", Action.SAVE_AS)
33-
add_item("Close", Action.CLOSE)
34-
add_item("Close All", Action.CLOSE_ALL)
35-
add_item("Close Other Tabs", Action.CLOSE_OTHER)
36-
add_separator()
37-
add_item("Copy File Path", Action.COPY_PATH)
38-
add_item("Show in FileSystem", Action.SHOW_IN_FILESYSTEM)
39-
add_item("Open File in Inspector", Action.OPEN_IN_INSPECTOR)
40-
41-
id_pressed.connect(_on_id_pressed)
14+
_add_menu_item(GaeaFileList.Action.SAVE, "Save", KeyModifierMask.KEY_MASK_CMD_OR_CTRL | KeyModifierMask.KEY_MASK_ALT | KEY_S)
15+
_add_menu_item(GaeaFileList.Action.SAVE, "Save As...")
4216

17+
add_separator()
18+
_add_menu_item(GaeaFileList.Action.COPY_PATH, "Copy Graph Path")
19+
_add_menu_item(GaeaFileList.Action.SHOW_IN_FILESYSTEM, "Show in FileSystem")
20+
_add_menu_item(GaeaFileList.Action.OPEN_IN_INSPECTOR, "Open File in Inspector")
4321

44-
func _on_id_pressed(id: int) -> void:
45-
match id:
46-
Action.SAVE:
47-
if graph.resource_path.is_empty():
48-
unsaved_file_found.emit(graph)
49-
return
50-
51-
if not graph.is_built_in():
52-
ResourceSaver.save(graph)
53-
else:
54-
var scene_path := graph.resource_path.get_slice("::", 0)
55-
ResourceSaver.save(load(scene_path))
56-
# Necessary for open scenes.
57-
EditorInterface.reload_scene_from_path(scene_path)
58-
file_saved.emit(graph)
59-
Action.SAVE_AS:
60-
save_as_selected.emit(graph)
61-
Action.CLOSE:
62-
close_file_selected.emit(graph)
63-
Action.CLOSE_ALL:
64-
close_all_selected.emit()
65-
Action.CLOSE_OTHER:
66-
close_others_selected.emit(graph)
67-
Action.COPY_PATH:
68-
DisplayServer.clipboard_set(graph.resource_path)
69-
Action.SHOW_IN_FILESYSTEM:
70-
if not graph.is_built_in():
71-
EditorInterface.select_file(graph.resource_path)
72-
else:
73-
EditorInterface.select_file(graph.resource_path.get_slice("::", 0))
74-
Action.OPEN_IN_INSPECTOR:
75-
EditorInterface.edit_resource(graph)
22+
add_separator()
23+
_add_menu_item(GaeaFileList.Action.CLOSE, "Close", KeyModifierMask.KEY_MASK_CMD_OR_CTRL | KEY_W)
24+
_add_menu_item(GaeaFileList.Action.CLOSE_ALL, "Close All")
25+
_add_menu_item(GaeaFileList.Action.CLOSE_OTHER, "Close Other Tabs")
26+
27+
28+
func _add_menu_item(id: GaeaFileList.Action, text: String, shortcut_key: Variant = KEY_NONE) -> void:
29+
add_item(tr(text), id)
30+
if shortcut_key is StringName and InputMap.has_action(shortcut_key):
31+
var shortcut = Shortcut.new()
32+
shortcut.events = InputMap.action_get_events(shortcut_key)
33+
set_item_shortcut(
34+
get_item_index(id),
35+
shortcut
36+
)
37+
elif shortcut_key is Key and shortcut_key != KEY_NONE:
38+
set_item_shortcut(
39+
get_item_index(id),
40+
GaeaEditorSettings.get_file_list_action_shortcut(id, shortcut_key)
41+
)
42+
43+
44+
func _on_about_to_popup() -> void:
45+
for item_index in item_count:
46+
var action: GaeaFileList.Action = get_item_id(item_index) as GaeaFileList.Action
47+
set_item_disabled(item_index, not file_system_container.can_do_action(action))

addons/gaea/editor/file_list/file_system_container.gd

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22
class_name GaeaFileList
33
extends 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

620
const 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]
1630
var _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
4140
func 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+
100114
func _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:
145159
func _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-
171184
func _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

Comments
 (0)