Skip to content

Commit

Permalink
GH-592 Add optional delete node confirmation dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Jul 27, 2024
1 parent b7e2622 commit 26b28b1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void OrchestratorSettings::_register_settings()
_settings.emplace_back(BOOL_SETTING("ui/components_panel/show_graph_friendly_names", true));
_settings.emplace_back(BOOL_SETTING("ui/components_panel/show_function_friendly_names", true));

_settings.emplace_back(BOOL_SETTING("ui/graph/confirm_on_delete", true));
_settings.emplace_back(BOOL_SETTING("ui/graph/disconnect_control_flow_when_dragged", true));
_settings.emplace_back(BOOL_SETTING("ui/graph/show_autowire_selection_dialog", true));
_settings.emplace_back(BOOL_SETTING("ui/graph/show_minimap", false));
Expand Down
55 changes: 44 additions & 11 deletions src/editor/graph/graph_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,6 @@ void OrchestratorGraphEdit::_notification(int p_what)
validate_and_build->connect("pressed", callable_mp(this, &OrchestratorGraphEdit::_on_validate_and_build));
get_menu_hbox()->add_child(validate_and_build);

_confirm_window = memnew(ConfirmationDialog);
_confirm_window->set_hide_on_ok(true);
_confirm_window->get_cancel_button()->hide();
_confirm_window->set_initial_position(Window::WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS);
add_child(_confirm_window);

const Ref<Resource> self = get_orchestration()->get_self();
self->connect("connections_changed", callable_mp(this, &OrchestratorGraphEdit::_on_graph_connections_changed));
self->connect("changed", callable_mp(this, &OrchestratorGraphEdit::_on_script_changed));
Expand Down Expand Up @@ -712,6 +706,37 @@ void OrchestratorGraphEdit::_drop_data(const Vector2& p_position, const Variant&
}
}

void OrchestratorGraphEdit::_confirm_yes_no(const String& p_text, const String& p_title, Callable p_confirm_callback)
{
ConfirmationDialog* dialog = memnew(ConfirmationDialog);
dialog->set_title(p_title);
dialog->set_text(p_text);
dialog->set_ok_button_text("Yes");
dialog->set_cancel_button_text("No");
dialog->set_initial_position(Window::WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS);
add_child(dialog);

dialog->connect("confirmed", p_confirm_callback);
dialog->connect("close_requested", callable_mp_lambda(this, [dialog] { dialog->queue_free(); }));

dialog->popup_centered();
}

void OrchestratorGraphEdit::_notify(const String& p_text, const String& p_title)
{
ConfirmationDialog* dialog = memnew(ConfirmationDialog);
dialog->set_title(p_title);
dialog->set_text(p_text);
dialog->set_ok_button_text("OK");
dialog->get_cancel_button()->hide();
dialog->set_initial_position(Window::WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS);
add_child(dialog);

dialog->connect("close_requested", callable_mp_lambda(this, [dialog] { dialog->queue_free(); }));

dialog->popup_centered();
}

bool OrchestratorGraphEdit::_is_position_within_node_rect(const Vector2& p_position) const
{
for (int i = 0; i < get_child_count(); ++i)
Expand Down Expand Up @@ -1497,6 +1522,18 @@ void OrchestratorGraphEdit::_on_node_deselected(Node* p_node)
}

void OrchestratorGraphEdit::_on_delete_nodes_requested(const PackedStringArray& p_node_names)
{
OrchestratorSettings* settings = OrchestratorSettings::get_singleton();
if (settings->get_setting("ui/graph/confirm_on_delete", true))
{
const String message = vformat("Do you wish to delete %d node(s)?", p_node_names.size());
_confirm_yes_no(message, "Confirm deletion", callable_mp(this, &OrchestratorGraphEdit::_delete_nodes).bind(p_node_names));
}
else
_delete_nodes(p_node_names);
}

void OrchestratorGraphEdit::_delete_nodes(const PackedStringArray& p_node_names)
{
for (const String& node_name : p_node_names)
{
Expand All @@ -1508,11 +1545,7 @@ void OrchestratorGraphEdit::_on_delete_nodes_requested(const PackedStringArray&
"It may be that this node represents a function entry or some other node type that requires "
"deletion via the component menu instead.";

_confirm_window->set_initial_position(Window::WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS);
_confirm_window->set_text(vformat(message, node->get_script_node_id(), node->get_title().strip_edges()));
_confirm_window->set_title("Delete canceled");
_confirm_window->reset_size();
_confirm_window->show();
_notify(vformat(message, node->get_script_node_id(), node->get_script_node()->get_node_title()), "Delete canceled");
return;
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/editor/graph/graph_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class OrchestratorGraphEdit : public GraphEdit
#endif
Ref<OScriptGraph> _script_graph; //! The underlying orchestration script graph
OrchestratorGraphActionMenu* _action_menu{ nullptr }; //! Actions menu
ConfirmationDialog* _confirm_window{ nullptr }; //! Confirmation window
Vector2 _saved_mouse_position; //! Mouse position where node/dialog is placed
DragContext _drag_context; //! Drag context details
int _deferred_tween_node{ -1 }; //! Node id to tween to upon load
Expand Down Expand Up @@ -246,6 +245,17 @@ class OrchestratorGraphEdit : public GraphEdit
void sync();

private:
/// Displays a yes/no confirmation dialog to the user.
/// @param p_text the text to be shown.
/// @param p_title the confirmation window title text
/// @param p_confirm_callback the callback if the user presses 'yes'
void _confirm_yes_no(const String& p_text, const String& p_title, Callable p_confirm_callback);

/// Displays a notification to the user
/// @param p_text the text to be shown.
/// @param p_title the notification window title text
void _notify(const String& p_text, const String& p_title);

/// Checks whether the specified position is within any node rect.
/// @param p_position the position to check
/// @return true if the position is within any node rect, false otherwise
Expand Down Expand Up @@ -412,6 +422,10 @@ class OrchestratorGraphEdit : public GraphEdit
/// @param p_node_names the nodes to be deleted
void _on_delete_nodes_requested(const PackedStringArray& p_node_names);

/// Deletes nodes with the given names
/// @param p_node_names the nodes to be deleted
void _delete_nodes(const PackedStringArray& p_node_names);

/// Dispatched when the user right clicks the graph edit
/// @param p_position position where the click event occurred
void _on_right_mouse_clicked(const Vector2& p_position);
Expand Down

0 comments on commit 26b28b1

Please sign in to comment.