Skip to content

Commit 8a087c7

Browse files
committed
Add context menu for shader editor list
1 parent db76de5 commit 8a087c7

File tree

2 files changed

+121
-20
lines changed

2 files changed

+121
-20
lines changed

editor/plugins/shader_editor_plugin.cpp

+106-17
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
#include "scene/gui/item_list.h"
4646
#include "scene/gui/texture_rect.h"
4747

48+
Ref<Resource> ShaderEditorPlugin::_get_current_shader() {
49+
int index = shader_tabs->get_current_tab();
50+
ERR_FAIL_INDEX_V(index, shader_tabs->get_tab_count(), Ref<Resource>());
51+
if (edited_shaders[index].shader.is_valid()) {
52+
return edited_shaders[index].shader;
53+
} else {
54+
return edited_shaders[index].shader_inc;
55+
}
56+
}
57+
4858
void ShaderEditorPlugin::_update_shader_list() {
4959
shader_list->clear();
5060
for (EditedShader &edited_shader : edited_shaders) {
@@ -93,9 +103,7 @@ void ShaderEditorPlugin::_update_shader_list() {
93103
shader_list->select(shader_tabs->get_current_tab());
94104
}
95105

96-
for (int i = FILE_SAVE; i < FILE_MAX; i++) {
97-
file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), edited_shaders.is_empty());
98-
}
106+
_set_file_specific_items_disabled(edited_shaders.is_empty());
99107

100108
_update_shader_list_status();
101109
}
@@ -362,6 +370,61 @@ void ShaderEditorPlugin::_shader_list_clicked(int p_item, Vector2 p_local_mouse_
362370
if (p_mouse_button_index == MouseButton::MIDDLE) {
363371
_close_shader(p_item);
364372
}
373+
if (p_mouse_button_index == MouseButton::RIGHT) {
374+
_make_script_list_context_menu();
375+
}
376+
}
377+
378+
void ShaderEditorPlugin::_setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu) {
379+
if (p_type == FILE) {
380+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new", TTR("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_NEW);
381+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/new_include", TTR("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_NEW_INCLUDE);
382+
p_menu->add_separator();
383+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open", TTR("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_OPEN);
384+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/open_include", TTR("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O), FILE_OPEN_INCLUDE);
385+
}
386+
387+
if (p_type == FILE || p_type == CONTEXT_VALID_ITEM) {
388+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save", TTR("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_SAVE);
389+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTR("Save File As...")), FILE_SAVE_AS);
390+
}
391+
392+
if (p_type == FILE) {
393+
p_menu->add_separator();
394+
p_menu->add_item(TTR("Open File in Inspector"), FILE_INSPECT);
395+
p_menu->add_separator();
396+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTR("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
397+
} else {
398+
p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTR("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
399+
p_menu->add_item(TTR("Close All"), CLOSE_ALL);
400+
p_menu->add_item(TTR("Close Other Tabs"), CLOSE_OTHER_TABS);
401+
if (p_type == CONTEXT_VALID_ITEM) {
402+
p_menu->add_separator();
403+
p_menu->add_item(TTR("Copy Script Path"), COPY_PATH);
404+
p_menu->add_item(TTR("Show in File System"), SHOW_IN_FILE_SYSTEM);
405+
}
406+
}
407+
}
408+
409+
void ShaderEditorPlugin::_make_script_list_context_menu() {
410+
context_menu->clear();
411+
412+
int selected = shader_tabs->get_current_tab();
413+
if (selected < 0 || selected >= shader_tabs->get_tab_count()) {
414+
return;
415+
}
416+
417+
Control *control = shader_tabs->get_tab_control(selected);
418+
bool is_valid_editor_control = Object::cast_to<TextShaderEditor>(control) || Object::cast_to<VisualShaderEditor>(control);
419+
420+
_setup_popup_menu(is_valid_editor_control ? CONTEXT_VALID_ITEM : CONTEXT, context_menu);
421+
422+
context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_ALL), shader_tabs->get_tab_count() <= 0);
423+
context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_OTHER_TABS), shader_tabs->get_tab_count() <= 1);
424+
425+
context_menu->set_position(main_split->get_screen_position() + main_split->get_local_mouse_position());
426+
context_menu->reset_size();
427+
context_menu->popup();
365428
}
366429

367430
void ShaderEditorPlugin::_close_shader(int p_index) {
@@ -486,6 +549,31 @@ void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
486549
case FILE_CLOSE: {
487550
_close_shader(shader_tabs->get_current_tab());
488551
} break;
552+
case CLOSE_ALL: {
553+
while (shader_tabs->get_tab_count() > 0) {
554+
_close_shader(0);
555+
}
556+
} break;
557+
case CLOSE_OTHER_TABS: {
558+
int index = shader_tabs->get_current_tab();
559+
for (int i = 0; i < index; i++) {
560+
_close_shader(0);
561+
}
562+
while (shader_tabs->get_tab_count() > 1) {
563+
_close_shader(1);
564+
}
565+
} break;
566+
case SHOW_IN_FILE_SYSTEM: {
567+
Ref<Resource> shader = _get_current_shader();
568+
String path = shader->get_path();
569+
if (!path.is_empty()) {
570+
FileSystemDock::get_singleton()->navigate_to_path(path);
571+
}
572+
} break;
573+
case COPY_PATH: {
574+
Ref<Resource> shader = _get_current_shader();
575+
DisplayServer::get_singleton()->clipboard_set(shader->get_path());
576+
} break;
489577
}
490578
}
491579

@@ -652,6 +740,14 @@ void ShaderEditorPlugin::_res_saved_callback(const Ref<Resource> &p_res) {
652740
}
653741
}
654742

743+
void ShaderEditorPlugin::_set_file_specific_items_disabled(bool p_disabled) {
744+
PopupMenu *file_popup_menu = file_menu->get_popup();
745+
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_SAVE), p_disabled);
746+
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_SAVE_AS), p_disabled);
747+
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_INSPECT), p_disabled);
748+
file_popup_menu->set_item_disabled(file_popup_menu->get_item_index(FILE_CLOSE), p_disabled);
749+
}
750+
655751
void ShaderEditorPlugin::_notification(int p_what) {
656752
switch (p_what) {
657753
case NOTIFICATION_READY: {
@@ -679,23 +775,15 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
679775
file_menu = memnew(MenuButton);
680776
file_menu->set_text(TTR("File"));
681777
file_menu->set_shortcut_context(main_split);
682-
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/new", TTR("New Shader..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_NEW);
683-
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/new_include", TTR("New Shader Include..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_NEW_INCLUDE);
684-
file_menu->get_popup()->add_separator();
685-
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/open", TTR("Load Shader File..."), KeyModifierMask::CMD_OR_CTRL | Key::O), FILE_OPEN);
686-
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/open_include", TTR("Load Shader Include File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::O), FILE_OPEN_INCLUDE);
687-
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/save", TTR("Save File"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_SAVE);
688-
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/save_as", TTR("Save File As...")), FILE_SAVE_AS);
689-
file_menu->get_popup()->add_separator();
690-
file_menu->get_popup()->add_item(TTR("Open File in Inspector"), FILE_INSPECT);
691-
file_menu->get_popup()->add_separator();
692-
file_menu->get_popup()->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTR("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
778+
_setup_popup_menu(FILE, file_menu->get_popup());
693779
file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
694780
menu_hb->add_child(file_menu);
695781

696-
for (int i = FILE_SAVE; i < FILE_MAX; i++) {
697-
file_menu->get_popup()->set_item_disabled(file_menu->get_popup()->get_item_index(i), true);
698-
}
782+
_set_file_specific_items_disabled(true);
783+
784+
context_menu = memnew(PopupMenu);
785+
add_child(context_menu);
786+
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
699787

700788
Control *padding = memnew(Control);
701789
padding->set_h_size_flags(Control::SIZE_EXPAND_FILL);
@@ -718,6 +806,7 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
718806
vb->add_child(shader_list);
719807
shader_list->connect(SceneStringName(item_selected), callable_mp(this, &ShaderEditorPlugin::_shader_selected));
720808
shader_list->connect("item_clicked", callable_mp(this, &ShaderEditorPlugin::_shader_list_clicked));
809+
shader_list->set_allow_rmb_select(true);
721810
SET_DRAG_FORWARDING_GCD(shader_list, ShaderEditorPlugin);
722811

723812
main_split->add_child(vb);

editor/plugins/shader_editor_plugin.h

+15-3
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ class ShaderEditorPlugin : public EditorPlugin {
6060

6161
LocalVector<EditedShader> edited_shaders;
6262

63-
// Always valid operations come first in the enum, file-specific ones
64-
// should go after FILE_SAVE which is used to build the menu accordingly.
6563
enum {
6664
FILE_NEW,
6765
FILE_NEW_INCLUDE,
@@ -71,7 +69,16 @@ class ShaderEditorPlugin : public EditorPlugin {
7169
FILE_SAVE_AS,
7270
FILE_INSPECT,
7371
FILE_CLOSE,
74-
FILE_MAX
72+
CLOSE_ALL,
73+
CLOSE_OTHER_TABS,
74+
SHOW_IN_FILE_SYSTEM,
75+
COPY_PATH,
76+
};
77+
78+
enum PopupMenuType {
79+
FILE,
80+
CONTEXT,
81+
CONTEXT_VALID_ITEM,
7582
};
7683

7784
HSplitContainer *main_split = nullptr;
@@ -80,6 +87,7 @@ class ShaderEditorPlugin : public EditorPlugin {
8087

8188
Button *button = nullptr;
8289
MenuButton *file_menu = nullptr;
90+
PopupMenu *context_menu = nullptr;
8391

8492
WindowWrapper *window_wrapper = nullptr;
8593
Button *make_floating = nullptr;
@@ -88,15 +96,19 @@ class ShaderEditorPlugin : public EditorPlugin {
8896

8997
float text_shader_zoom_factor = 1.0f;
9098

99+
Ref<Resource> _get_current_shader();
91100
void _update_shader_list();
92101
void _shader_selected(int p_index);
93102
void _shader_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index);
103+
void _setup_popup_menu(PopupMenuType p_type, PopupMenu *p_menu);
104+
void _make_script_list_context_menu();
94105
void _menu_item_pressed(int p_index);
95106
void _resource_saved(Object *obj);
96107
void _close_shader(int p_index);
97108
void _close_builtin_shaders_from_scene(const String &p_scene);
98109
void _file_removed(const String &p_removed_file);
99110
void _res_saved_callback(const Ref<Resource> &p_res);
111+
void _set_file_specific_items_disabled(bool p_disabled);
100112

101113
void _shader_created(Ref<Shader> p_shader);
102114
void _shader_include_created(Ref<ShaderInclude> p_shader_inc);

0 commit comments

Comments
 (0)