Skip to content

Commit

Permalink
CraterCrashGH-92 Add short-cut keybindings for Variable drag-n-drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Feb 2, 2024
1 parent e34c057 commit 37f474b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
80 changes: 65 additions & 15 deletions src/editor/graph/graph_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

#include <godot_cpp/classes/center_container.hpp>
#include <godot_cpp/classes/confirmation_dialog.hpp>
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/margin_container.hpp>
#include <godot_cpp/classes/method_tweener.hpp>
#include <godot_cpp/classes/os.hpp>
#include <godot_cpp/classes/project_settings.hpp>
Expand Down Expand Up @@ -121,6 +123,14 @@ void OrchestratorGraphEdit::_notification(int p_what)
{
if (p_what == NOTIFICATION_READY)
{
_drag_hint = memnew(Label);
_drag_hint->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, 0);
_drag_hint->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -50);
_drag_hint->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
_drag_hint->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
_drag_hint->set_vertical_alignment(VERTICAL_ALIGNMENT_BOTTOM);
add_child(_drag_hint);

Label* label = memnew(Label);
label->set_text("Use Right Mouse Button To Add New Nodes");
label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
Expand All @@ -135,6 +145,11 @@ void OrchestratorGraphEdit::_notification(int p_what)
if (!_script_graph->get_nodes().is_empty())
_status->hide();

_drag_hint_timer = memnew(Timer);
_drag_hint_timer->set_wait_time(5);
_drag_hint_timer->connect("timeout", callable_mp(this, &OrchestratorGraphEdit::_hide_drag_hint));
add_child(_drag_hint_timer);

Button* show_script_details = memnew(Button);
show_script_details->set_text("Script Details");
show_script_details->set_tooltip_text("Shows script details in the Inspector");
Expand Down Expand Up @@ -289,7 +304,16 @@ bool OrchestratorGraphEdit::_can_drop_data(const Vector2& p_position, const Vari
allowed_types.push_back("variable");
allowed_types.push_back("signal");
allowed_types.push_back("function");
return allowed_types.has(type);

if (allowed_types.has(type))
{
if (type == "variable")
_show_drag_hint("Hint: Use Ctrl to drop a Setter, Shift to drop a Getter");

return true;
}

return false;
}

void OrchestratorGraphEdit::_drop_data(const Vector2& p_position, const Variant& p_data)
Expand Down Expand Up @@ -394,22 +418,36 @@ void OrchestratorGraphEdit::_drop_data(const Vector2& p_position, const Variant&
}
else if (type == "variable")
{
const String variable_name = String(Array(data["variables"])[0]);
_hide_drag_hint();

// Create context-menu handlers
Ref<OrchestratorGraphActionHandler> get_handler(memnew(OrchestratorGraphNodeSpawnerVariableGet(variable_name)));
Ref<OrchestratorGraphActionHandler> set_handler(memnew(OrchestratorGraphNodeSpawnerVariableSet(variable_name)));
const String variable_name = String(Array(data["variables"])[0]);
if (Input::get_singleton()->is_key_pressed(Key::KEY_CTRL))
{
OrchestratorGraphNodeSpawnerVariableSet setter(variable_name);
setter.execute(this, _saved_mouse_position);
}
else if (Input::get_singleton()->is_key_pressed(Key::KEY_SHIFT))
{
OrchestratorGraphNodeSpawnerVariableGet getter(variable_name);
getter.execute(this, _saved_mouse_position);
}
else
{
// Create context-menu handlers
Ref<OrchestratorGraphActionHandler> get_handler(memnew(OrchestratorGraphNodeSpawnerVariableGet(variable_name)));
Ref<OrchestratorGraphActionHandler> set_handler(memnew(OrchestratorGraphNodeSpawnerVariableSet(variable_name)));

// Create context-menu to specify variable get or set choice
_context_menu->clear();
_context_menu->add_separator("Variable " + variable_name);
_context_menu->add_item("Get " + variable_name, CM_VARIABLE_GET);
_context_menu->add_item("Set " + variable_name, CM_VARIABLE_SET);
_context_menu->set_item_metadata(_context_menu->get_item_index(CM_VARIABLE_GET), get_handler);
_context_menu->set_item_metadata(_context_menu->get_item_index(CM_VARIABLE_SET), set_handler);
_context_menu->reset_size();
_context_menu->set_position(get_screen_position() + p_position);
_context_menu->popup();
// Create context-menu to specify variable get or set choice
_context_menu->clear();
_context_menu->add_separator("Variable " + variable_name);
_context_menu->add_item("Get " + variable_name, CM_VARIABLE_GET);
_context_menu->add_item("Set " + variable_name, CM_VARIABLE_SET);
_context_menu->set_item_metadata(_context_menu->get_item_index(CM_VARIABLE_GET), get_handler);
_context_menu->set_item_metadata(_context_menu->get_item_index(CM_VARIABLE_SET), set_handler);
_context_menu->reset_size();
_context_menu->set_position(get_screen_position() + p_position);
_context_menu->popup();
}
}
else if (type == "signal")
{
Expand Down Expand Up @@ -638,6 +676,18 @@ bool OrchestratorGraphEdit::_can_duplicate_node(OrchestratorGraphNode* p_node) c
return !event.is_valid() && !function_entry.is_valid() && !function_result.is_valid() && !local_variable.is_valid();
}

void OrchestratorGraphEdit::_show_drag_hint(const godot::String& p_message) const
{
_drag_hint->set_text(p_message);
_drag_hint->show();
_drag_hint_timer->start();
}

void OrchestratorGraphEdit::_hide_drag_hint()
{
_drag_hint->hide();
}

void OrchestratorGraphEdit::_on_connection_drag_started(const StringName& p_from, int p_from_port, bool p_output)
{
_drag_context.start_drag(p_from, p_from_port, p_output);
Expand Down
10 changes: 10 additions & 0 deletions src/editor/graph/graph_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <godot_cpp/classes/editor_interface.hpp>
#include <godot_cpp/classes/graph_edit.hpp>
#include <godot_cpp/classes/input_event.hpp>
#include <godot_cpp/classes/timer.hpp>

using namespace godot;

Expand Down Expand Up @@ -108,6 +109,8 @@ class OrchestratorGraphEdit : public GraphEdit
PopupMenu* _context_menu{ nullptr }; //! Graph context menu
OrchestratorPlugin* _plugin{ nullptr }; //! The plugin
Control* _status{ nullptr }; //! Displays status in the center of graphs
Label* _drag_hint{ nullptr }; //! Displays the drag status at the bottom of the graph
Timer* _drag_hint_timer{ nullptr }; //! Timer for drag hint messages
OrchestratorGraphEdit() = default;

protected:
Expand Down Expand Up @@ -241,6 +244,13 @@ class OrchestratorGraphEdit : public GraphEdit
/// @return true if the node can be duplicated, false otherwise
bool _can_duplicate_node(OrchestratorGraphNode* p_node) const;

/// Displays the drag status hint
/// @param p_message the hint message
void _show_drag_hint(const String& p_message) const;

/// Hides the drag status hint
void _hide_drag_hint();

/// Connection drag started
/// @param p_from the source node
/// @param p_from_port source node port
Expand Down

0 comments on commit 37f474b

Please sign in to comment.