diff --git a/doc/classes/VisualShader.xml b/doc/classes/VisualShader.xml index c8230d94e4a..c9e1849e94e 100644 --- a/doc/classes/VisualShader.xml +++ b/doc/classes/VisualShader.xml @@ -186,11 +186,6 @@ - - - The offset vector of the whole graph. - - A vertex shader, operating on vertices. diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp index 618888e0796..d9f9bc381d1 100644 --- a/editor/plugins/shader_editor_plugin.cpp +++ b/editor/plugins/shader_editor_plugin.cpp @@ -454,6 +454,7 @@ void ShaderEditorPlugin::_close_shader(int p_index) { Control *c = shader_tabs->get_tab_control(p_index); VisualShaderEditor *vs_editor = Object::cast_to(c); if (vs_editor) { + vs_editor->save_editor_layout(); file_menu->get_parent()->remove_child(file_menu); menu_hb->add_child(file_menu); menu_hb->move_child(file_menu, 0); diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index 38050d71aca..93c0caa6f40 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -36,6 +36,7 @@ #include "core/os/keyboard.h" #include "core/version_generated.gen.h" #include "editor/editor_node.h" +#include "editor/editor_paths.h" #include "editor/editor_properties.h" #include "editor/editor_properties_vector.h" #include "editor/editor_settings.h" @@ -224,7 +225,7 @@ void VisualShaderGraphPlugin::set_connections(const Listget_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].output_ports.has(p_port_id)) { + if (editor->get_current_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].output_ports.has(p_port_id)) { Link &link = links[p_node_id]; for (const KeyValue &E : link.output_ports) { @@ -261,7 +262,7 @@ void VisualShaderGraphPlugin::show_port_preview(VisualShader::Type p_type, int p vbox->add_child(offset); VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview); - port_preview->setup(visual_shader, editor->preview_material, visual_shader->get_shader_type(), links[p_node_id].output_ports[p_port_id].type == VisualShaderNode::PORT_TYPE_VECTOR_4D, p_node_id, p_port_id, p_is_valid); + port_preview->setup(visual_shader, editor->preview_material, editor->get_current_shader_type(), links[p_node_id].output_ports[p_port_id].type == VisualShaderNode::PORT_TYPE_VECTOR_4D, p_node_id, p_port_id, p_is_valid); port_preview->set_h_size_flags(Control::SIZE_SHRINK_CENTER); vbox->add_child(port_preview); link.preview_visible = true; @@ -276,7 +277,7 @@ void VisualShaderGraphPlugin::update_node_deferred(VisualShader::Type p_type, in } void VisualShaderGraphPlugin::update_node(VisualShader::Type p_type, int p_node_id) { - if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) { + if (p_type != editor->get_current_shader_type() || !links.has(p_node_id)) { return; } remove_node(p_type, p_node_id, true); @@ -286,7 +287,7 @@ void VisualShaderGraphPlugin::update_node(VisualShader::Type p_type, int p_node_ } void VisualShaderGraphPlugin::set_input_port_default_value(VisualShader::Type p_type, int p_node_id, int p_port_id, const Variant &p_value) { - if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) { + if (p_type != editor->get_current_shader_type() || !links.has(p_node_id)) { return; } @@ -326,7 +327,7 @@ void VisualShaderGraphPlugin::set_input_port_default_value(VisualShader::Type p_ } void VisualShaderGraphPlugin::set_parameter_name(VisualShader::Type p_type, int p_node_id, const String &p_name) { - if (visual_shader->get_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].parameter_name != nullptr) { + if (editor->get_current_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].parameter_name != nullptr) { links[p_node_id].parameter_name->set_text(p_name); } } @@ -367,14 +368,14 @@ int VisualShaderGraphPlugin::get_constant_index(float p_constant) const { } void VisualShaderGraphPlugin::set_expression(VisualShader::Type p_type, int p_node_id, const String &p_expression) { - if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links[p_node_id].expression_edit) { + if (p_type != editor->get_current_shader_type() || !links.has(p_node_id) || !links[p_node_id].expression_edit) { return; } links[p_node_id].expression_edit->set_text(p_expression); } void VisualShaderGraphPlugin::attach_node_to_frame(VisualShader::Type p_type, int p_node_id, int p_frame_id) { - if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links.has(p_frame_id)) { + if (p_type != editor->get_current_shader_type() || !links.has(p_node_id) || !links.has(p_frame_id)) { return; } @@ -463,7 +464,7 @@ void VisualShaderGraphPlugin::update_reroute_nodes() { for (const KeyValue &E : links) { Ref reroute_node = Object::cast_to(E.value.visual_node); if (reroute_node.is_valid()) { - update_node(visual_shader->get_shader_type(), E.key); + update_node(editor->get_current_shader_type(), E.key); } } } @@ -503,10 +504,6 @@ void VisualShaderGraphPlugin::update_parameter_refs() { } } -VisualShader::Type VisualShaderGraphPlugin::get_shader_type() const { - return visual_shader->get_shader_type(); -} - // Only updates the linked frames of the given node, not the node itself (in case it's a frame node). void VisualShaderGraphPlugin::update_frames(VisualShader::Type p_type, int p_node) { GraphEdit *graph = editor->graph; @@ -540,7 +537,7 @@ void VisualShaderGraphPlugin::update_frames(VisualShader::Type p_type, int p_nod } void VisualShaderGraphPlugin::set_node_position(VisualShader::Type p_type, int p_id, const Vector2 &p_position) { - if (visual_shader->get_shader_type() == p_type && links.has(p_id)) { + if (editor->get_current_shader_type() == p_type && links.has(p_id)) { links[p_id].graph_element->set_position_offset(p_position); } } @@ -606,7 +603,7 @@ bool VisualShaderGraphPlugin::is_node_has_parameter_instances_relatively(VisualS } void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id, bool p_just_update, bool p_update_frames) { - if (visual_shader.is_null() || p_type != visual_shader->get_shader_type()) { + if (visual_shader.is_null() || p_type != editor->get_current_shader_type()) { return; } GraphEdit *graph = editor->graph; @@ -1441,7 +1438,7 @@ void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id, bool } void VisualShaderGraphPlugin::remove_node(VisualShader::Type p_type, int p_id, bool p_just_update) { - if (visual_shader->get_shader_type() == p_type && links.has(p_id)) { + if (editor->get_current_shader_type() == p_type && links.has(p_id)) { GraphEdit *graph_edit = editor->graph; if (!graph_edit) { return; @@ -1461,7 +1458,7 @@ void VisualShaderGraphPlugin::connect_nodes(VisualShader::Type p_type, int p_fro return; } - if (visual_shader.is_valid() && visual_shader->get_shader_type() == p_type) { + if (visual_shader.is_valid() && editor->get_current_shader_type() == p_type) { // Update reroute nodes since their port type might have changed. Ref reroute_to = visual_shader->get_node(p_type, p_to_node); Ref reroute_from = visual_shader->get_node(p_type, p_from_node); @@ -1484,7 +1481,7 @@ void VisualShaderGraphPlugin::disconnect_nodes(VisualShader::Type p_type, int p_ return; } - if (visual_shader.is_valid() && visual_shader->get_shader_type() == p_type) { + if (visual_shader.is_valid() && editor->get_current_shader_type() == p_type) { graph->disconnect_node(itos(p_from_node), p_from_port, itos(p_to_node), p_to_port); for (List::Element *E = connections.front(); E; E = E->next()) { @@ -1524,6 +1521,7 @@ List VisualShaderEditor::copy_items_buffer; List VisualShaderEditor::copy_connections_buffer; void VisualShaderEditor::edit_shader(const Ref &p_shader) { + shader_fully_loaded = false; bool changed = false; VisualShader *visual_shader_ptr = Object::cast_to(p_shader.ptr()); if (visual_shader_ptr) { @@ -1538,7 +1536,6 @@ void VisualShaderEditor::edit_shader(const Ref &p_shader) { graph_plugin->register_shader(visual_shader.ptr()); visual_shader->connect_changed(callable_mp(this, &VisualShaderEditor::_update_preview)); - visual_shader->set_graph_offset(graph->get_scroll_offset() / EDSCALE); _set_mode(visual_shader->get_mode()); preview_material->set_shader(visual_shader); @@ -1558,6 +1555,7 @@ void VisualShaderEditor::edit_shader(const Ref &p_shader) { _update_options_menu(); _update_preview(); _update_graph(); + callable_mp(this, &VisualShaderEditor::_restore_editor_state).call_deferred(); } } } @@ -1581,6 +1579,37 @@ void VisualShaderEditor::validate_script() { } } +void VisualShaderEditor::save_editor_layout() { + const String id_string = _get_cache_id_string(); + + const String offset_cache_key = _get_cache_key("offset"); + const String zoom_cache_key = _get_cache_key("zoom"); + vs_editor_cache->set_value(id_string, offset_cache_key, graph->get_scroll_offset() / EDSCALE); + vs_editor_cache->set_value(id_string, zoom_cache_key, graph->get_zoom()); + vs_editor_cache->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("vs_editor_cache.cfg")); +} + +void VisualShaderEditor::set_current_shader_type(VisualShader::Type p_type) { + current_type = p_type; + + const String id_string = _get_cache_id_string(); + + vs_editor_cache->set_value(id_string, "edited_type", p_type); + vs_editor_cache->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("vs_editor_cache.cfg")); + + const String offset_cache_key = _get_cache_key("offset"); + const String zoom_cache_key = _get_cache_key("zoom"); + const Vector2 saved_scroll_offset = vs_editor_cache->get_value(id_string, offset_cache_key, Vector2()); + const real_t saved_zoom = vs_editor_cache->get_value(id_string, zoom_cache_key, 1.0); + + graph->set_scroll_offset(saved_scroll_offset); + graph->set_zoom(saved_zoom); +} + +VisualShader::Type VisualShaderEditor::get_current_shader_type() const { + return current_type; +} + Control *VisualShaderEditor::get_top_bar() { return toolbar; } @@ -1723,7 +1752,7 @@ void VisualShaderEditor::_update_custom_script(const Ref