فهرست منبع

Merge pull request #100287 from Chaosus/shader_ui_update

Re-organize UI in the shader editor
Thaddeus Crews 5 ماه پیش
والد
کامیت
38a29f6139

+ 1 - 0
editor/plugins/shader/shader_editor.h

@@ -44,4 +44,5 @@ public:
 	virtual bool is_unsaved() const = 0;
 	virtual void save_external_data(const String &p_str = "") = 0;
 	virtual void validate_script() = 0;
+	virtual Control *get_top_bar() = 0;
 };

+ 56 - 5
editor/plugins/shader_editor_plugin.cpp

@@ -146,6 +146,7 @@ void ShaderEditorPlugin::edit(Object *p_object) {
 			if (edited_shaders[i].shader_inc.ptr() == si) {
 				shader_tabs->set_current_tab(i);
 				shader_list->select(i);
+				_switch_to_editor(edited_shaders[i].shader_editor);
 				return;
 			}
 		}
@@ -161,13 +162,16 @@ void ShaderEditorPlugin::edit(Object *p_object) {
 			if (edited_shaders[i].shader.ptr() == s) {
 				shader_tabs->set_current_tab(i);
 				shader_list->select(i);
+				_switch_to_editor(edited_shaders[i].shader_editor);
 				return;
 			}
 		}
 		es.shader = Ref<Shader>(s);
 		Ref<VisualShader> vs = es.shader;
 		if (vs.is_valid()) {
-			es.shader_editor = memnew(VisualShaderEditor);
+			VisualShaderEditor *vs_editor = memnew(VisualShaderEditor);
+			vs_editor->set_toggle_list_control(left_panel);
+			es.shader_editor = vs_editor;
 		} else {
 			TextShaderEditor *text_shader = memnew(TextShaderEditor);
 			text_shader->get_code_editor()->set_toggle_list_control(left_panel);
@@ -190,6 +194,7 @@ void ShaderEditorPlugin::edit(Object *p_object) {
 	shader_tabs->set_current_tab(shader_tabs->get_tab_count() - 1);
 	edited_shaders.push_back(es);
 	_update_shader_list();
+	_switch_to_editor(es.shader_editor);
 }
 
 bool ShaderEditorPlugin::handles(Object *p_object) const {
@@ -364,6 +369,7 @@ void ShaderEditorPlugin::_shader_selected(int p_index) {
 	}
 
 	if (edited_shaders[p_index].shader_editor) {
+		_switch_to_editor(edited_shaders[p_index].shader_editor);
 		edited_shaders[p_index].shader_editor->validate_script();
 	}
 
@@ -400,6 +406,8 @@ void ShaderEditorPlugin::_setup_popup_menu(PopupMenuType p_type, PopupMenu *p_me
 		p_menu->add_item(TTR("Inspect Native Shader Code..."), FILE_INSPECT_NATIVE_SHADER_CODE);
 		p_menu->add_separator();
 		p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
+		p_menu->add_separator();
+		p_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_scripts_panel"), TOGGLE_SHADERS_PANEL);
 	} else {
 		p_menu->add_shortcut(ED_SHORTCUT("shader_editor/close_file", TTRC("Close File"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
 		p_menu->add_item(TTR("Close All"), CLOSE_ALL);
@@ -435,15 +443,27 @@ void ShaderEditorPlugin::_make_script_list_context_menu() {
 
 void ShaderEditorPlugin::_close_shader(int p_index) {
 	ERR_FAIL_INDEX(p_index, shader_tabs->get_tab_count());
+
+	file_menu->get_parent()->remove_child(file_menu);
+	make_floating->get_parent()->remove_child(make_floating);
+
 	Control *c = shader_tabs->get_tab_control(p_index);
 	memdelete(c);
 	edited_shaders.remove_at(p_index);
 	_update_shader_list();
 	EditorUndoRedoManager::get_singleton()->clear_history(); // To prevent undo on deleted graphs.
 
+	Control *bar = nullptr;
 	if (shader_tabs->get_tab_count() == 0) {
 		left_panel->show(); // Make sure the panel is visible, because it can't be toggled without open shaders.
+		bar = menu_hb;
+	} else {
+		bar = edited_shaders[shader_tabs->get_current_tab()].shader_editor->get_top_bar();
 	}
+
+	bar->add_child(file_menu);
+	bar->move_child(file_menu, 0);
+	bar->add_child(make_floating);
 }
 
 void ShaderEditorPlugin::_close_builtin_shaders_from_scene(const String &p_scene) {
@@ -590,6 +610,21 @@ void ShaderEditorPlugin::_menu_item_pressed(int p_index) {
 			Ref<Resource> shader = _get_current_shader();
 			DisplayServer::get_singleton()->clipboard_set(shader->get_path());
 		} break;
+		case TOGGLE_SHADERS_PANEL: {
+			left_panel->set_visible(!left_panel->is_visible());
+
+			int index = shader_tabs->get_current_tab();
+			ERR_FAIL_INDEX(index, shader_tabs->get_tab_count());
+			TextShaderEditor *editor = Object::cast_to<TextShaderEditor>(edited_shaders[index].shader_editor);
+			if (editor) {
+				editor->get_code_editor()->update_toggle_scripts_button();
+			} else {
+				VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(edited_shaders[index].shader_editor);
+				if (vs_editor) {
+					vs_editor->update_toggle_scripts_button();
+				}
+			}
+		} break;
 	}
 }
 
@@ -723,6 +758,24 @@ void ShaderEditorPlugin::_set_text_shader_zoom_factor(float p_zoom_factor) {
 	}
 }
 
+void ShaderEditorPlugin::_switch_to_editor(ShaderEditor *p_editor) {
+	Control *bar = p_editor->get_top_bar();
+
+	int file_menu_index = 0;
+
+	VisualShaderEditor *vs_editor = Object::cast_to<VisualShaderEditor>(p_editor);
+	if (vs_editor) {
+		file_menu_index = 2; // Toggle Scripts Panel button + separator
+	}
+
+	file_menu->get_parent()->remove_child(file_menu);
+	bar->add_child(file_menu);
+	bar->move_child(file_menu, file_menu_index);
+
+	make_floating->get_parent()->remove_child(make_floating);
+	bar->add_child(make_floating);
+}
+
 void ShaderEditorPlugin::_file_removed(const String &p_removed_file) {
 	for (uint32_t i = 0; i < edited_shaders.size(); i++) {
 		if (edited_shaders[i].path == p_removed_file) {
@@ -789,7 +842,7 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
 
 	left_panel = memnew(VBoxContainer);
 
-	HBoxContainer *menu_hb = memnew(HBoxContainer);
+	menu_hb = memnew(HBoxContainer);
 	left_panel->add_child(menu_hb);
 	file_menu = memnew(MenuButton);
 	file_menu->set_text(TTR("File"));
@@ -804,9 +857,7 @@ ShaderEditorPlugin::ShaderEditorPlugin() {
 	add_child(context_menu);
 	context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ShaderEditorPlugin::_menu_item_pressed));
 
-	Control *padding = memnew(Control);
-	padding->set_h_size_flags(Control::SIZE_EXPAND_FILL);
-	menu_hb->add_child(padding);
+	menu_hb->add_spacer();
 
 	make_floating = memnew(ScreenSelect);
 	make_floating->set_flat(true);

+ 5 - 0
editor/plugins/shader_editor_plugin.h

@@ -40,6 +40,7 @@ class ShaderEditor;
 class TabContainer;
 class TextShaderEditor;
 class VBoxContainer;
+class HBoxContainer;
 class VisualShaderEditor;
 class WindowWrapper;
 
@@ -74,6 +75,7 @@ class ShaderEditorPlugin : public EditorPlugin {
 		CLOSE_OTHER_TABS,
 		SHOW_IN_FILE_SYSTEM,
 		COPY_PATH,
+		TOGGLE_SHADERS_PANEL,
 	};
 
 	enum PopupMenuType {
@@ -84,6 +86,8 @@ class ShaderEditorPlugin : public EditorPlugin {
 
 	HSplitContainer *main_split = nullptr;
 	VBoxContainer *left_panel = nullptr;
+	HBoxContainer *menu_hb = nullptr;
+
 	ItemList *shader_list = nullptr;
 	TabContainer *shader_tabs = nullptr;
 
@@ -124,6 +128,7 @@ class ShaderEditorPlugin : public EditorPlugin {
 	void _window_changed(bool p_visible);
 
 	void _set_text_shader_zoom_factor(float p_zoom_factor);
+	void _switch_to_editor(ShaderEditor *p_editor);
 
 protected:
 	void _notification(int p_what);

+ 18 - 11
editor/plugins/text_shader_editor.cpp

@@ -38,6 +38,7 @@
 #include "editor/editor_string_names.h"
 #include "editor/themes/editor_scale.h"
 #include "editor/themes/editor_theme_manager.h"
+#include "scene/gui/separator.h"
 #include "scene/gui/split_container.h"
 #include "servers/rendering/shader_preprocessor.h"
 #include "servers/rendering/shader_types.h"
@@ -749,8 +750,7 @@ void TextShaderEditor::_notification(int p_what) {
 	switch (p_what) {
 		case NOTIFICATION_ENTER_TREE:
 		case NOTIFICATION_THEME_CHANGED: {
-			PopupMenu *popup = help_menu->get_popup();
-			popup->set_item_icon(popup->get_item_index(HELP_DOCS), get_editor_theme_icon(SNAME("ExternalLink")));
+			site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink")));
 		} break;
 
 		case NOTIFICATION_APPLICATION_FOCUS_IN: {
@@ -976,6 +976,10 @@ void TextShaderEditor::validate_script() {
 	code_editor->_validate_script();
 }
 
+Control *TextShaderEditor::get_top_bar() {
+	return hbc;
+}
+
 bool TextShaderEditor::is_unsaved() const {
 	return code_editor->get_text_editor()->get_saved_version() != code_editor->get_text_editor()->get_version();
 }
@@ -1149,7 +1153,7 @@ TextShaderEditor::TextShaderEditor() {
 
 	VBoxContainer *main_container = memnew(VBoxContainer);
 	main_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
-	HBoxContainer *hbc = memnew(HBoxContainer);
+	hbc = memnew(HBoxContainer);
 
 	edit_menu = memnew(MenuButton);
 	edit_menu->set_shortcut_context(this);
@@ -1205,18 +1209,21 @@ TextShaderEditor::TextShaderEditor() {
 	bookmarks_menu->connect("about_to_popup", callable_mp(this, &TextShaderEditor::_update_bookmark_list));
 	bookmarks_menu->connect("index_pressed", callable_mp(this, &TextShaderEditor::_bookmark_item_pressed));
 
-	help_menu = memnew(MenuButton);
-	help_menu->set_text(TTR("Help"));
-	help_menu->set_switch_on_hover(true);
-	help_menu->get_popup()->add_item(TTR("Online Docs"), HELP_DOCS);
-	help_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &TextShaderEditor::_menu_option));
-
 	add_child(main_container);
 	main_container->add_child(hbc);
-	hbc->add_child(search_menu);
 	hbc->add_child(edit_menu);
+	hbc->add_child(search_menu);
 	hbc->add_child(goto_menu);
-	hbc->add_child(help_menu);
+	hbc->add_spacer();
+
+	site_search = memnew(Button);
+	site_search->set_flat(true);
+	site_search->connect(SceneStringName(pressed), callable_mp(this, &TextShaderEditor::_menu_option).bind(HELP_DOCS));
+	site_search->set_text(TTR("Online Docs"));
+	site_search->set_tooltip_text(TTR("Open Godot online documentation."));
+	hbc->add_child(site_search);
+	hbc->add_child(memnew(VSeparator));
+
 	hbc->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("ScriptEditorPanel"), EditorStringName(EditorStyles)));
 
 	VSplitContainer *editor_box = memnew(VSplitContainer);

+ 3 - 1
editor/plugins/text_shader_editor.h

@@ -136,10 +136,11 @@ class TextShaderEditor : public ShaderEditor {
 		EDIT_EMOJI_AND_SYMBOL,
 	};
 
+	HBoxContainer *hbc = nullptr;
 	MenuButton *edit_menu = nullptr;
 	MenuButton *search_menu = nullptr;
 	PopupMenu *bookmarks_menu = nullptr;
-	MenuButton *help_menu = nullptr;
+	Button *site_search = nullptr;
 	PopupMenu *context_menu = nullptr;
 	RichTextLabel *warnings_panel = nullptr;
 	uint64_t idle = 0;
@@ -195,6 +196,7 @@ public:
 	virtual bool is_unsaved() const override;
 	virtual void save_external_data(const String &p_str = "") override;
 	virtual void validate_script() override;
+	virtual Control *get_top_bar() override;
 
 	bool was_compilation_successful() const { return compilation_success; }
 	bool get_trim_trailing_whitespace_on_save() const { return trim_trailing_whitespace_on_save; }

+ 59 - 1
editor/plugins/visual_shader_editor_plugin.cpp

@@ -34,6 +34,7 @@
 #include "core/io/resource_loader.h"
 #include "core/math/math_defs.h"
 #include "core/os/keyboard.h"
+#include "core/version_generated.gen.h"
 #include "editor/editor_node.h"
 #include "editor/editor_properties.h"
 #include "editor/editor_properties_vector.h"
@@ -1572,6 +1573,10 @@ void VisualShaderEditor::validate_script() {
 	}
 }
 
+Control *VisualShaderEditor::get_top_bar() {
+	return toolbar;
+}
+
 void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
 	if (plugins.has(p_plugin)) {
 		return;
@@ -2463,6 +2468,13 @@ Size2 VisualShaderEditor::get_minimum_size() const {
 	return Size2(10, 200);
 }
 
+void VisualShaderEditor::update_toggle_scripts_button() {
+	ERR_FAIL_NULL(toggle_scripts_list);
+	bool forward = toggle_scripts_list->is_visible() == is_layout_rtl();
+	toggle_scripts_button->set_button_icon(get_editor_theme_icon(forward ? SNAME("Forward") : SNAME("Back")));
+	toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
+}
+
 void VisualShaderEditor::_draw_color_over_button(Object *p_obj, Color p_color) {
 	Button *button = Object::cast_to<Button>(p_obj);
 	if (!button) {
@@ -5147,6 +5159,10 @@ void VisualShaderEditor::_param_unselected() {
 	_clear_preview_param();
 }
 
+void VisualShaderEditor::_help_open() {
+	OS::get_singleton()->shell_open(vformat("%s/tutorials/shaders/visual_shaders.html", GODOT_VERSION_DOCS_URL));
+}
+
 void VisualShaderEditor::_notification(int p_what) {
 	switch (p_what) {
 		case NOTIFICATION_POSTINITIALIZE: {
@@ -5188,6 +5204,7 @@ void VisualShaderEditor::_notification(int p_what) {
 		} break;
 
 		case NOTIFICATION_THEME_CHANGED: {
+			site_search->set_button_icon(get_editor_theme_icon(SNAME("ExternalLink")));
 			highend_label->set_modulate(get_theme_color(SNAME("highend_color"), EditorStringName(Editor)));
 
 			param_filter->set_right_icon(Control::get_editor_theme_icon(SNAME("Search")));
@@ -5251,6 +5268,11 @@ void VisualShaderEditor::_notification(int p_what) {
 			if (is_visible_in_tree()) {
 				_update_graph();
 			}
+			update_toggle_scripts_button();
+		} break;
+
+		case NOTIFICATION_VISIBILITY_CHANGED: {
+			update_toggle_scripts_button();
 		} break;
 
 		case NOTIFICATION_DRAG_BEGIN: {
@@ -6354,6 +6376,16 @@ void VisualShaderEditor::_show_shader_preview() {
 	}
 }
 
+void VisualShaderEditor::set_toggle_list_control(Control *p_control) {
+	toggle_scripts_list = p_control;
+}
+
+void VisualShaderEditor::_toggle_scripts_pressed() {
+	ERR_FAIL_NULL(toggle_scripts_list);
+	toggle_scripts_list->set_visible(!toggle_scripts_list->is_visible());
+	update_toggle_scripts_button();
+}
+
 void VisualShaderEditor::_bind_methods() {
 	ClassDB::bind_method("_update_nodes", &VisualShaderEditor::_update_nodes);
 	ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
@@ -6490,7 +6522,7 @@ VisualShaderEditor::VisualShaderEditor() {
 	toolbar_panel->set_anchors_and_offsets_preset(Control::PRESET_TOP_WIDE, PRESET_MODE_MINSIZE, 10);
 	toolbar_panel->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
 
-	HFlowContainer *toolbar = memnew(HFlowContainer);
+	toolbar = memnew(HFlowContainer);
 	{
 		LocalVector<Node *> nodes;
 		for (int i = 0; i < graph->get_menu_hbox()->get_child_count(); i++) {
@@ -6589,6 +6621,32 @@ VisualShaderEditor::VisualShaderEditor() {
 	toolbar->add_child(shader_preview_button);
 	shader_preview_button->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_show_shader_preview));
 
+	Control *spacer = memnew(Control);
+	spacer->set_h_size_flags(Control::SIZE_EXPAND);
+	toolbar->add_child(spacer);
+
+	site_search = memnew(Button);
+	site_search->set_flat(true);
+	site_search->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_help_open));
+	site_search->set_text(TTR("Online Docs"));
+	site_search->set_tooltip_text(TTR("Open Godot online documentation."));
+	toolbar->add_child(site_search);
+	toolbar->add_child(memnew(VSeparator));
+
+	VSeparator *separator = memnew(VSeparator);
+	toolbar->add_child(separator);
+	toolbar->move_child(separator, 0);
+
+	separator = memnew(VSeparator);
+	toolbar->add_child(separator);
+	toolbar->move_child(separator, 0);
+
+	toggle_scripts_button = memnew(Button);
+	toggle_scripts_button->set_flat(true);
+	toggle_scripts_button->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_toggle_scripts_pressed));
+	toolbar->add_child(toggle_scripts_button);
+	toolbar->move_child(toggle_scripts_button, 0);
+
 	///////////////////////////////////////
 	// CODE PREVIEW
 	///////////////////////////////////////

+ 11 - 0
editor/plugins/visual_shader_editor_plugin.h

@@ -211,11 +211,15 @@ class VisualShaderEditor : public ShaderEditor {
 	String param_filter_name;
 	EditorProperty *current_prop = nullptr;
 	VBoxContainer *shader_preview_vbox = nullptr;
+	Button *site_search = nullptr;
+	Button *toggle_scripts_button = nullptr;
+	Control *toggle_scripts_list = nullptr;
 	GraphEdit *graph = nullptr;
 	Button *add_node = nullptr;
 	MenuButton *varying_button = nullptr;
 	Button *code_preview_button = nullptr;
 	Button *shader_preview_button = nullptr;
+	Control *toolbar = nullptr;
 
 	int last_to_node = -1;
 	int last_to_port = -1;
@@ -446,6 +450,8 @@ class VisualShaderEditor : public ShaderEditor {
 
 	void _show_shader_preview();
 
+	void _toggle_scripts_pressed();
+
 	Vector<int> nodes_link_to_frame_buffer; // Contains the nodes that are requested to be linked to a frame. This is used to perform one Undo/Redo operation for dragging nodes.
 	int frame_node_id_to_link_to = -1;
 
@@ -633,6 +639,8 @@ class VisualShaderEditor : public ShaderEditor {
 	void _param_selected();
 	void _param_unselected();
 
+	void _help_open();
+
 protected:
 	void _notification(int p_what);
 	static void _bind_methods();
@@ -643,6 +651,7 @@ public:
 	virtual bool is_unsaved() const override;
 	virtual void save_external_data(const String &p_str = "") override;
 	virtual void validate_script() override;
+	virtual Control *get_top_bar() override;
 
 	void add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
 	void remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
@@ -651,11 +660,13 @@ public:
 
 	void clear_custom_types();
 	void add_custom_type(const String &p_name, const String &p_type, const Ref<Script> &p_script, const String &p_description, int p_return_icon_type, const String &p_category, bool p_highend);
+	void set_toggle_list_control(Control *p_control);
 
 	Dictionary get_custom_node_data(Ref<VisualShaderNodeCustom> &p_custom_node);
 	void update_custom_type(const Ref<Resource> &p_resource);
 
 	virtual Size2 get_minimum_size() const override;
+	void update_toggle_scripts_button();
 
 	Ref<VisualShader> get_visual_shader() const { return visual_shader; }