Skip to content

Commit 21fbf03

Browse files
committed
Merge pull request #109973 from aaronfranke/shader-menu-bar
Make shader editor menu position consistent between shader languages
2 parents eef9912 + 0831b4b commit 21fbf03

File tree

7 files changed

+82
-113
lines changed

7 files changed

+82
-113
lines changed

editor/shader/shader_editor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@
3333
#include "scene/gui/control.h"
3434
#include "scene/resources/shader.h"
3535

36+
class Button;
37+
class MenuButton;
38+
3639
class ShaderEditor : public Control {
3740
GDCLASS(ShaderEditor, Control);
3841

3942
public:
4043
virtual void edit_shader(const Ref<Shader> &p_shader) = 0;
4144
virtual void edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {}
45+
virtual void use_menu_bar_items(MenuButton *p_file_menu, Button *p_make_floating) = 0;
4246

4347
virtual void apply_shaders() = 0;
4448
virtual bool is_unsaved() const = 0;
4549
virtual void save_external_data(const String &p_str = "") = 0;
4650
virtual void validate_script() = 0;
47-
virtual Control *get_top_bar() = 0;
4851
};

editor/shader/shader_editor_plugin.cpp

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,6 @@ void ShaderEditorPlugin::edit(Object *p_object) {
190190
cte->connect("zoomed", callable_mp(this, &ShaderEditorPlugin::_set_text_shader_zoom_factor));
191191
cte->connect(SceneStringName(visibility_changed), callable_mp(this, &ShaderEditorPlugin::_update_shader_editor_zoom_factor).bind(cte));
192192
}
193-
194-
if (text_shader_editor->get_top_bar()) {
195-
text_shader_editor->get_top_bar()->set_h_size_flags(Control::SIZE_EXPAND_FILL);
196-
menu_hb->add_child(text_shader_editor->get_top_bar());
197-
menu_hb->move_child(text_shader_editor->get_top_bar(), 1);
198-
}
199193
}
200194

201195
shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
@@ -459,29 +453,22 @@ void ShaderEditorPlugin::_make_script_list_context_menu() {
459453

460454
void ShaderEditorPlugin::_close_shader(int p_index) {
461455
ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
462-
463-
Control *c = shader_tabs->get_tab_control(p_index);
464-
VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(c);
465-
if (vs_editor) {
466-
vs_editor->save_editor_layout();
456+
if (file_menu->get_parent() != nullptr) {
467457
file_menu->get_parent()->remove_child(file_menu);
468-
menu_hb->add_child(file_menu);
469-
menu_hb->move_child(file_menu, 0);
470-
458+
}
459+
if (make_floating->get_parent()) {
471460
make_floating->get_parent()->remove_child(make_floating);
472-
menu_hb->add_child(make_floating);
473-
} else {
474-
memdelete(edited_shaders[p_index].shader_editor->get_top_bar());
475461
}
462+
ShaderEditor *shader_editor = Object::cast_to<ShaderEditor>(shader_tabs->get_tab_control(p_index));
463+
ERR_FAIL_NULL(shader_editor);
476464

477-
memdelete(c);
465+
memdelete(shader_editor);
478466
edited_shaders.remove_at(p_index);
479467
_update_shader_list();
480468
EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
481469

482470
if (shader_tabs->get_tab_count() == 0) {
483471
shader_list->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.
484-
menu_spacer->show();
485472
} else {
486473
_switch_to_editor(edited_shaders[shader_tabs->get_current_tab()].shader_editor);
487474
}
@@ -795,44 +782,13 @@ void ShaderEditorPlugin::_update_shader_editor_zoom_factor(CodeTextEditor *p_sha
795782
}
796783

797784
void ShaderEditorPlugin::_switch_to_editor(ShaderEditor *p_editor) {
798-
Control *bar = p_editor->get_top_bar();
799-
800-
VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(p_editor);
801-
if (vs_editor) {
785+
if (file_menu->get_parent() != nullptr) {
802786
file_menu->get_parent()->remove_child(file_menu);
803-
file_menu->set_switch_on_hover(false);
804-
bar->add_child(file_menu);
805-
bar->move_child(file_menu, 2); // Toggle Files Panel button + separator.
806-
807-
make_floating->get_parent()->remove_child(make_floating);
808-
bar->add_child(make_floating);
809-
} else {
810-
if (menu_spacer->is_visible()) {
811-
menu_spacer->hide();
812-
}
813-
814-
// Just swapped from a visual shader editor.
815-
if (file_menu->get_parent() != menu_hb) {
816-
file_menu->get_parent()->remove_child(file_menu);
817-
file_menu->set_switch_on_hover(true);
818-
menu_hb->add_child(file_menu);
819-
menu_hb->move_child(file_menu, 0);
820-
821-
make_floating->get_parent()->remove_child(make_floating);
822-
menu_hb->add_child(make_floating);
823-
}
824787
}
825-
826-
for (int i = 0; i < shader_tabs->get_tab_count(); i++) {
827-
ShaderEditor *se = Object::cast_to<ShaderEditor>(shader_tabs->get_tab_control(i));
828-
if (se && se->get_top_bar()) {
829-
if (se == p_editor) {
830-
se->get_top_bar()->show();
831-
} else {
832-
se->get_top_bar()->hide();
833-
}
834-
}
788+
if (make_floating->get_parent()) {
789+
make_floating->get_parent()->remove_child(make_floating);
835790
}
791+
p_editor->use_menu_bar_items(file_menu, make_floating);
836792
}
837793

838794
void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
@@ -902,8 +858,6 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
902858
files_split->set_split_offset(200 * EDSCALE);
903859
files_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
904860

905-
menu_hb = memnew(HBoxContainer);
906-
main_container->add_child(menu_hb);
907861
file_menu = memnew(MenuButton);
908862
file_menu->set_flat(false);
909863
file_menu->set_theme_type_variation("FlatMenuButton");
@@ -912,24 +866,20 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
912866
file_menu->set_shortcut_context(files_split);
913867
_setup_popup_menu(FILE, file_menu->get_popup());
914868
file_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
915-
menu_hb->add_child(file_menu);
916869

917870
_set_file_specific_items_disabled(true);
918871

919872
context_menu = memnew(PopupMenu);
920873
add_child(context_menu);
921874
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
922875

923-
menu_spacer = menu_hb->add_spacer();
924-
925876
make_floating = memnew(ScreenSelect);
926877
make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
927878
if (!make_floating->is_disabled()) {
928879
// Override default ScreenSelect tooltip if multi-window support is available.
929880
make_floating->set_tooltip_text(TTR("Make the shader editor floating.") + "\n" + TTR("Right-click to open the screen selector."));
930881
}
931882

932-
menu_hb->add_child(make_floating);
933883
window_wrapper->connect("window_visibility_changed", callable_mp(this, &ShaderEditorPlugin::_window_changed));
934884

935885
shader_list = memnew(ItemList);
@@ -962,3 +912,8 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
962912
shader_create_dialog->connect("shader_created", callable_mp(this, &ShaderEditorPlugin::_shader_created));
963913
shader_create_dialog->connect("shader_include_created", callable_mp(this, &ShaderEditorPlugin::_shader_include_created));
964914
}
915+
916+
ShaderEditorPlugin::~ShaderEditorPlugin() {
917+
memdelete(file_menu);
918+
memdelete(make_floating);
919+
}

editor/shader/shader_editor_plugin.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ class ShaderEditorPlugin : public EditorPlugin {
8383

8484
VBoxContainer *main_container = nullptr;
8585
HSplitContainer *files_split = nullptr;
86-
HBoxContainer *menu_hb = nullptr;
87-
Control *menu_spacer = nullptr;
8886

8987
ItemList *shader_list = nullptr;
9088
TabContainer *shader_tabs = nullptr;
@@ -150,4 +148,5 @@ class ShaderEditorPlugin : public EditorPlugin {
150148
virtual void apply_changes() override;
151149

152150
ShaderEditorPlugin();
151+
~ShaderEditorPlugin();
153152
};

editor/shader/text_shader_editor.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,13 @@ void TextShaderEditor::edit_shader_include(const Ref<ShaderInclude> &p_shader_in
979979
code_editor->set_edited_shader_include(p_shader_inc);
980980
}
981981

982+
void TextShaderEditor::use_menu_bar_items(MenuButton *p_file_menu, Button *p_make_floating) {
983+
p_file_menu->set_switch_on_hover(true);
984+
menu_bar_hbox->add_child(p_file_menu);
985+
menu_bar_hbox->move_child(p_file_menu, 0);
986+
menu_bar_hbox->add_child(p_make_floating);
987+
}
988+
982989
void TextShaderEditor::save_external_data(const String &p_str) {
983990
if (shader.is_null() && shader_inc.is_null()) {
984991
disk_changed->hide();
@@ -1027,10 +1034,6 @@ void TextShaderEditor::validate_script() {
10271034
code_editor->_validate_script();
10281035
}
10291036

1030-
Control *TextShaderEditor::get_top_bar() {
1031-
return hbc;
1032-
}
1033-
10341037
bool TextShaderEditor::is_unsaved() const {
10351038
return code_editor->get_text_editor()->get_saved_version() != code_editor->get_text_editor()->get_version();
10361039
}
@@ -1204,7 +1207,7 @@ TextShaderEditor::TextShaderEditor() {
12041207

12051208
VBoxContainer *main_container = memnew(VBoxContainer);
12061209
main_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
1207-
hbc = memnew(HBoxContainer);
1210+
menu_bar_hbox = memnew(HBoxContainer);
12081211

12091212
edit_menu = memnew(MenuButton);
12101213
edit_menu->set_flat(false);
@@ -1267,20 +1270,21 @@ TextShaderEditor::TextShaderEditor() {
12671270
bookmarks_menu->connect("index_pressed", callable_mp(this, &TextShaderEditor::_bookmark_item_pressed));
12681271

12691272
add_child(main_container);
1270-
hbc->add_child(edit_menu);
1271-
hbc->add_child(search_menu);
1272-
hbc->add_child(goto_menu);
1273-
hbc->add_spacer();
1273+
main_container->add_child(menu_bar_hbox);
1274+
menu_bar_hbox->add_child(edit_menu);
1275+
menu_bar_hbox->add_child(search_menu);
1276+
menu_bar_hbox->add_child(goto_menu);
1277+
menu_bar_hbox->add_spacer();
12741278

12751279
site_search = memnew(Button);
12761280
site_search->set_theme_type_variation(SceneStringName(FlatButton));
12771281
site_search->connect(SceneStringName(pressed), callable_mp(this, &TextShaderEditor::_menu_option).bind(HELP_DOCS));
12781282
site_search->set_text(TTR("Online Docs"));
12791283
site_search->set_tooltip_text(TTR("Open Godot online documentation."));
1280-
hbc->add_child(site_search);
1281-
hbc->add_child(memnew(VSeparator));
1284+
menu_bar_hbox->add_child(site_search);
1285+
menu_bar_hbox->add_child(memnew(VSeparator));
12821286

1283-
hbc->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));
1287+
menu_bar_hbox->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));
12841288

12851289
VSplitContainer *editor_box = memnew(VSplitContainer);
12861290
main_container->add_child(editor_box);

editor/shader/text_shader_editor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class TextShaderEditor : public ShaderEditor {
136136
EDIT_EMOJI_AND_SYMBOL,
137137
};
138138

139-
HBoxContainer *hbc = nullptr;
139+
HBoxContainer *menu_bar_hbox = nullptr;
140140
MenuButton *edit_menu = nullptr;
141141
MenuButton *search_menu = nullptr;
142142
PopupMenu *bookmarks_menu = nullptr;
@@ -191,12 +191,12 @@ class TextShaderEditor : public ShaderEditor {
191191
public:
192192
virtual void edit_shader(const Ref<Shader> &p_shader) override;
193193
virtual void edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) override;
194+
virtual void use_menu_bar_items(MenuButton *p_file_menu, Button *p_make_floating) override;
194195

195196
virtual void apply_shaders() override;
196197
virtual bool is_unsaved() const override;
197198
virtual void save_external_data(const String &p_str = "") override;
198199
virtual void validate_script() override;
199-
virtual Control *get_top_bar() override;
200200

201201
bool was_compilation_successful() const { return compilation_success; }
202202
bool get_trim_trailing_whitespace_on_save() const { return trim_trailing_whitespace_on_save; }

0 commit comments

Comments
 (0)