Skip to content

Commit

Permalink
Make visual shader editor create node window a popup
Browse files Browse the repository at this point in the history
  • Loading branch information
badsectoracula committed Nov 23, 2024
1 parent 0c45ace commit 6a6fce5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
51 changes: 21 additions & 30 deletions editor/plugins/visual_shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,6 @@ String VisualShaderEditor::_get_description(int p_idx) {
void VisualShaderEditor::_update_options_menu() {
node_desc->set_text("");
highend_label->set_visible(false);
members_dialog->get_ok_button()->set_disabled(true);

members->clear();
TreeItem *root = members->create_item();
Expand Down Expand Up @@ -2373,8 +2372,6 @@ void VisualShaderEditor::_update_options_menu() {
item->select(0);
node_desc->set_text(options[i].description);
is_first_item = false;

members_dialog->get_ok_button()->set_disabled(false);
}
switch (options[i].return_type) {
case VisualShaderNode::PORT_TYPE_SCALAR:
Expand Down Expand Up @@ -4206,7 +4203,7 @@ void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_s
if (node.is_valid()) {
output_port_type = node->get_output_port_type(from_slot);
}
_show_members_dialog(true, input_port_type, output_port_type);
_show_members_popup(true, input_port_type, output_port_type);
}

void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
Expand All @@ -4218,7 +4215,7 @@ void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slo
if (node.is_valid()) {
input_port_type = node->get_input_port_type(to_slot);
}
_show_members_dialog(true, input_port_type, output_port_type);
_show_members_popup(true, input_port_type, output_port_type);
}

bool VisualShaderEditor::_check_node_drop_on_connection(const Vector2 &p_position, Ref<GraphEdit::Connection> *r_closest_connection, int *r_from_port, int *r_to_port) {
Expand Down Expand Up @@ -4874,7 +4871,7 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
connection_popup_menu->reset_size();
connection_popup_menu->popup();
} else if (selected_graph_elements.is_empty() && copy_buffer_empty) {
_show_members_dialog(true);
_show_members_popup(true);
} else {
popup_menu->set_item_disabled(NodeMenuOptions::CUT, selected_deletable_graph_elements.is_empty());
popup_menu->set_item_disabled(NodeMenuOptions::COPY, selected_deletable_graph_elements.is_empty());
Expand Down Expand Up @@ -4987,7 +4984,7 @@ void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
}
}

void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos, VisualShaderNode::PortType p_input_port_type, VisualShaderNode::PortType p_output_port_type) {
void VisualShaderEditor::_show_members_popup(bool at_mouse_pos, VisualShaderNode::PortType p_input_port_type, VisualShaderNode::PortType p_output_port_type) {
if (members_input_port_type != p_input_port_type || members_output_port_type != p_output_port_type) {
members_input_port_type = p_input_port_type;
members_output_port_type = p_output_port_type;
Expand All @@ -4999,24 +4996,24 @@ void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos, VisualShaderNod
saved_node_pos = graph->get_local_mouse_position();

Point2 gpos = get_screen_position() + get_local_mouse_position();
members_dialog->set_position(gpos);
members_popup->set_position(gpos);
} else {
saved_node_pos_dirty = false;
members_dialog->set_position(graph->get_screen_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
members_popup->set_position(graph->get_screen_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
}

if (members_dialog->is_visible()) {
members_dialog->grab_focus();
if (members_popup->is_visible()) {
members_popup->grab_focus();
return;
}

members_dialog->popup();
members_popup->popup();

// Keep dialog within window bounds.
Rect2 window_rect = Rect2(get_window()->get_position(), get_window()->get_size());
Rect2 dialog_rect = Rect2(members_dialog->get_position(), members_dialog->get_size());
Rect2 dialog_rect = Rect2(members_popup->get_position(), members_popup->get_size());
Vector2 difference = (dialog_rect.get_end() - window_rect.get_end()).maxf(0);
members_dialog->set_position(members_dialog->get_position() - difference);
members_popup->set_position(members_popup->get_position() - difference);

node_filter->grab_focus();
node_filter->select_all();
Expand Down Expand Up @@ -5067,7 +5064,7 @@ void VisualShaderEditor::_sbox_input(const Ref<InputEvent> &p_event) {
// Redirect navigational key events to the tree.
Ref<InputEventKey> key = p_event;
if (key.is_valid()) {
if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down") || key->is_action("ui_accept")) {
members->gui_input(key);
node_filter->accept_event();
}
Expand Down Expand Up @@ -5795,12 +5792,10 @@ void VisualShaderEditor::_member_selected() {
TreeItem *item = members->get_selected();

if (item != nullptr && item->has_meta("id")) {
members_dialog->get_ok_button()->set_disabled(false);
highend_label->set_visible(add_options[item->get_meta("id")].highend);
node_desc->set_text(_get_description(item->get_meta("id")));
} else {
highend_label->set_visible(false);
members_dialog->get_ok_button()->set_disabled(true);
node_desc->set_text("");
}
}
Expand Down Expand Up @@ -5831,7 +5826,7 @@ void VisualShaderEditor::_member_create() {
saved_node_pos = 0.5 * (from_graph_element->get_position() + zoom * from_graph_element->get_output_port_position(from_slot) + to_graph_element->get_position() + zoom * to_graph_element->get_input_port_position(to_slot));
}
_add_node(idx, add_options[idx].ops);
members_dialog->hide();
members_popup->hide();
}
}

Expand Down Expand Up @@ -5975,7 +5970,7 @@ void VisualShaderEditor::_tools_menu_option(int p_idx) {
void VisualShaderEditor::_node_menu_id_pressed(int p_idx) {
switch (p_idx) {
case NodeMenuOptions::ADD:
_show_members_dialog(true);
_show_members_popup(true);
break;
case NodeMenuOptions::CUT:
_copy_nodes(true);
Expand Down Expand Up @@ -6045,7 +6040,7 @@ void VisualShaderEditor::_connection_menu_id_pressed(int p_idx) {
}

connection_node_insert_requested = true;
_show_members_dialog(true, input_port_type, output_port_type);
_show_members_popup(true, input_port_type, output_port_type);
} break;
case ConnectionMenuOptions::INSERT_NEW_REROUTE: {
from_node = String(clicked_connection->from_node).to_int();
Expand Down Expand Up @@ -6532,7 +6527,7 @@ VisualShaderEditor::VisualShaderEditor() {
add_node->set_text(TTR("Add Node..."));
toolbar->add_child(add_node);
toolbar->move_child(add_node, 0);
add_node->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_show_members_dialog).bind(false, VisualShaderNode::PORT_TYPE_MAX, VisualShaderNode::PORT_TYPE_MAX));
add_node->connect(SceneStringName(pressed), callable_mp(this, &VisualShaderEditor::_show_members_popup).bind(false, VisualShaderNode::PORT_TYPE_MAX, VisualShaderNode::PORT_TYPE_MAX));

graph->connect("graph_elements_linked_to_frame_request", callable_mp(this, &VisualShaderEditor::_nodes_linked_to_frame_request));
graph->connect("frame_rect_changed", callable_mp(this, &VisualShaderEditor::_frame_rect_changed));
Expand Down Expand Up @@ -6752,15 +6747,11 @@ VisualShaderEditor::VisualShaderEditor() {
node_desc->set_v_size_flags(SIZE_FILL);
node_desc->set_custom_minimum_size(Size2(0, 70 * EDSCALE));

members_dialog = memnew(ConfirmationDialog);
members_dialog->set_title(TTR("Create Shader Node"));
members_dialog->add_child(members_vb);
members_dialog->set_ok_button_text(TTR("Create"));
members_dialog->connect(SceneStringName(confirmed), callable_mp(this, &VisualShaderEditor::_member_create));
members_dialog->get_ok_button()->set_disabled(true);
members_dialog->connect("canceled", callable_mp(this, &VisualShaderEditor::_member_cancel));
members_dialog->register_text_enter(node_filter);
add_child(members_dialog);
members_popup = memnew(PopupPanel);
members_popup->set_title(TTR("Create Shader Node"));
members_popup->add_child(members_vb);
members_popup->set_min_size(Size2i(300 * EDSCALE, members_popup->get_min_size().y));
add_child(members_popup);

// add varyings dialog
{
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/visual_shader_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class VisualShaderEditor : public ShaderEditor {
Point2 saved_node_pos;
bool saved_node_pos_dirty = false;

ConfirmationDialog *members_dialog = nullptr;
PopupPanel *members_popup = nullptr;
VisualShaderNode::PortType members_input_port_type = VisualShaderNode::PORT_TYPE_MAX;
VisualShaderNode::PortType members_output_port_type = VisualShaderNode::PORT_TYPE_MAX;
PopupMenu *popup_menu = nullptr;
Expand Down Expand Up @@ -371,7 +371,7 @@ class VisualShaderEditor : public ShaderEditor {
Label *highend_label = nullptr;

void _tools_menu_option(int p_idx);
void _show_members_dialog(bool at_mouse_pos, VisualShaderNode::PortType p_input_port_type = VisualShaderNode::PORT_TYPE_MAX, VisualShaderNode::PortType p_output_port_type = VisualShaderNode::PORT_TYPE_MAX);
void _show_members_popup(bool at_mouse_pos, VisualShaderNode::PortType p_input_port_type = VisualShaderNode::PORT_TYPE_MAX, VisualShaderNode::PortType p_output_port_type = VisualShaderNode::PORT_TYPE_MAX);

void _varying_menu_id_pressed(int p_idx);
void _show_add_varying_dialog();
Expand Down

0 comments on commit 6a6fce5

Please sign in to comment.