Skip to content

Commit

Permalink
Allow editing VSG
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometror committed Nov 6, 2024
1 parent f3de8f6 commit 3607fe2
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 97 deletions.
70 changes: 65 additions & 5 deletions editor/plugins/visual_shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id, bool
Button *edit_group_btn = memnew(Button);
edit_group_btn->set_text(TTR("Edit"));
edit_group_btn->set_icon(editor->get_theme_icon("Edit", "EditorIcons"));
edit_group_btn->connect("pressed", callable_mp(editor, &VisualShaderEditor::_edit_group).bind(p_id));
edit_group_btn->connect("pressed", callable_mp(editor, &VisualShaderEditor::_edit_group_in_graph).bind(p_id));
titlebar->add_child(edit_group_btn);
}
}
Expand Down Expand Up @@ -1541,6 +1541,7 @@ void VisualShaderEditor::edit_shader(const Ref<Shader> &p_shader) {
}
visual_shader = p_shader;
editing_shader_graph = visual_shader->get_graph(current_type);
visual_shader_group = nullptr;

graph_plugin->register_shader(visual_shader.ptr());

Expand Down Expand Up @@ -1580,7 +1581,18 @@ bool VisualShaderEditor::is_unsaved() const {
}

void VisualShaderEditor::save_external_data(const String &p_str) {
ResourceSaver::save(visual_shader, visual_shader->get_path());
if (visual_shader.is_valid()) {
ResourceSaver::save(visual_shader, visual_shader->get_path());
}
if (visual_shader_group.is_valid()) {
ResourceSaver::save(visual_shader_group, visual_shader_group->get_path());
}

for (Ref<VisualShaderGroup> edited_group : group_edit_stack) {
if (edited_group.is_valid()) {
ResourceSaver::save(edited_group, edited_group->get_path());
}
}
}

void VisualShaderEditor::validate_script() {
Expand Down Expand Up @@ -1927,8 +1939,17 @@ void VisualShaderEditor::_resources_removed() {
}
if (has_any_instance) {
EditorUndoRedoManager::get_singleton()->clear_history(); // Need to clear undo history, otherwise it may lead to hard-detected errors and crashes (since the script was removed).
// TODO: Logic to save visual_shader as well as group if necessary.
ResourceSaver::save(visual_shader, visual_shader->get_path());
if (visual_shader.is_valid()) {
ResourceSaver::save(visual_shader, visual_shader->get_path());
}
if (visual_shader_group.is_valid()) {
ResourceSaver::save(visual_shader_group, visual_shader_group->get_path());
}
for (Ref<VisualShaderGroup> edited_group : group_edit_stack) {
if (edited_group.is_valid()) {
ResourceSaver::save(edited_group, edited_group->get_path());
}
}
}
_update_options_menu();

Expand Down Expand Up @@ -2438,8 +2459,33 @@ void VisualShaderEditor::_set_mode(int p_which) {
set_current_shader_type((VisualShader::Type)saved_type);
}

void VisualShaderEditor::_edit_group(int p_idx) {
void VisualShaderEditor::_edit_group_in_graph(int p_idx) {
print_line("Edit group: " + itos(p_idx));

Ref<VisualShaderNodeGroup> group_node = editing_shader_graph->get_node(p_idx);
ERR_FAIL_COND(group_node.is_null());
editing_shader_graph = group_node->get_group()->get_graph().ptr();
group_edit_stack.push_back(group_node->get_group());

_update_graph();
}

void VisualShaderEditor::_exit_group() {
if (group_edit_stack.is_empty()) {
DEV_ASSERT(false);
return;
}

group_edit_stack.pop_back();
if (group_edit_stack.is_empty()) {
editing_shader_graph = visual_shader->get_graph(get_current_shader_type());
} else {
Ref<VisualShaderGroup> group = group_edit_stack.back()->get();
ERR_FAIL_COND(group.is_null());
editing_shader_graph = group->get_graph().ptr();
}

_update_graph();
}

Size2 VisualShaderEditor::get_minimum_size() const {
Expand Down Expand Up @@ -2589,6 +2635,12 @@ void VisualShaderEditor::_update_graph() {
graph->set_minimap_opacity(graph_minimap_opacity);
float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
graph->set_connection_lines_curvature(graph_lines_curvature);

if (group_edit_stack.size() > 0) {
exit_group_button->set_visible(true);
} else {
exit_group_button->set_visible(false);
}
}

void VisualShaderEditor::_restore_editor_state() {
Expand Down Expand Up @@ -5147,6 +5199,7 @@ void VisualShaderEditor::_notification(int p_what) {

code_preview_button->set_icon(Control::get_editor_theme_icon(SNAME("Shader")));
shader_preview_button->set_icon(Control::get_editor_theme_icon(SNAME("SubViewport")));
exit_group_button->set_icon(Control::get_editor_theme_icon(SNAME("MoveUp")));

{
Color background_color = EDITOR_GET("text_editor/theme/highlighting/background_color");
Expand Down Expand Up @@ -6500,6 +6553,13 @@ VisualShaderEditor::VisualShaderEditor() {
toolbar->add_child(shader_preview_button);
shader_preview_button->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_show_shader_preview));

exit_group_button = memnew(Button);
exit_group_button->set_theme_type_variation("FlatButton");
exit_group_button->set_tooltip_text(TTR("Exit currently editing group."));
exit_group_button->set_visible(false);
toolbar->add_child(exit_group_button);
exit_group_button->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_exit_group));

///////////////////////////////////////
// CODE PREVIEW
///////////////////////////////////////
Expand Down
8 changes: 6 additions & 2 deletions editor/plugins/visual_shader_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class VisualShaderEditor : public ShaderEditor {

ShaderGraph *editing_shader_graph = nullptr;
Ref<VisualShader> visual_shader; // Could be null (editing just a VisualShaderGroup).
Ref<VisualShaderGroup> editing_visual_shader_group; // Could be null.
Ref<VisualShaderGroup> visual_shader_group; // Could be null.

Ref<ShaderMaterial> preview_material;
Ref<Environment> env;
Expand All @@ -225,6 +225,7 @@ class VisualShaderEditor : public ShaderEditor {
MenuButton *varying_button = nullptr;
Button *code_preview_button = nullptr;
Button *shader_preview_button = nullptr;
Button *exit_group_button = nullptr;

OptionButton *edit_type = nullptr;
OptionButton *edit_type_standard = nullptr;
Expand Down Expand Up @@ -287,6 +288,8 @@ class VisualShaderEditor : public ShaderEditor {
VBoxContainer *param_vbox = nullptr;
VBoxContainer *param_vbox2 = nullptr;

List<Ref<VisualShaderGroup>> group_edit_stack;

enum ShaderModeFlags {
MODE_FLAGS_SPATIAL_CANVASITEM = 1,
MODE_FLAGS_SKY = 2,
Expand Down Expand Up @@ -437,7 +440,8 @@ class VisualShaderEditor : public ShaderEditor {
void _remove_varying(const String &p_name);
void _update_options_menu();
void _set_mode(int p_which);
void _edit_group(int p_idx);
void _edit_group_in_graph(int p_idx);
void _exit_group();

void _show_preview_text();
void _preview_close_requested();
Expand Down
2 changes: 2 additions & 0 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ void register_scene_types() {
GDREGISTER_CLASS(ShaderInclude);
GDREGISTER_CLASS(VisualShaderGroup);
GDREGISTER_ABSTRACT_CLASS(VisualShaderNode);
GDREGISTER_CLASS(VisualShaderNodeGroupInput);
GDREGISTER_CLASS(VisualShaderNodeGroupOutput);
GDREGISTER_CLASS(VisualShaderNodeCustom);
GDREGISTER_CLASS(VisualShaderNodeInput);
GDREGISTER_ABSTRACT_CLASS(VisualShaderNodeOutput);
Expand Down
Loading

0 comments on commit 3607fe2

Please sign in to comment.