From 6594fa0e7b1bbeebe2ffdb7346d735e9c001f81d Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Fri, 16 Aug 2024 01:13:57 -0400 Subject: [PATCH] GH-736 Fix knot movement/alignment --- src/editor/graph/graph_knot.cpp | 70 +++++++++++++++++++++++---------- src/editor/graph/graph_knot.h | 11 +++--- 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/editor/graph/graph_knot.cpp b/src/editor/graph/graph_knot.cpp index 3efbefb5..7a345732 100644 --- a/src/editor/graph/graph_knot.cpp +++ b/src/editor/graph/graph_knot.cpp @@ -31,23 +31,18 @@ void OrchestratorGraphKnot::_connections_changed(const String& p_caller) void OrchestratorGraphKnot::_position_changed() { _knot->point = get_position_offset(); - - set_block_signals(true); - set_position_offset(get_position_offset() - RENDER_OFFSET); - set_block_signals(false); - emit_signal("knot_position_changed", _knot->point); } void OrchestratorGraphKnot::_node_selected() { OrchestratorSettings* os = OrchestratorSettings::get_singleton(); - _icon->set_modulate(os->get_setting("ui/graph/knot_selected_color", Color(0.68f, 0.44f, 0.09f))); + _draw_color = os->get_setting("ui/graph/knot_selected_color", Color(0.68f, 0.44f, 0.09f)); } void OrchestratorGraphKnot::_node_deselected() { - _icon->set_modulate(_color); + _draw_color = _color; } void OrchestratorGraphKnot::set_graph(const Ref& p_graph) @@ -62,7 +57,15 @@ void OrchestratorGraphKnot::set_knot(const Ref& p_knot) { _knot = p_knot; - set_position_offset(_knot->point - RENDER_OFFSET); + set_position_offset(_knot->point); +} + +void OrchestratorGraphKnot::set_color(const Color& p_color) +{ + _color = p_color; + + if (!is_selected()) + _draw_color = _color; } void OrchestratorGraphKnot::_gui_input(const Ref& p_event) @@ -85,24 +88,39 @@ void OrchestratorGraphKnot::_gui_input(const Ref& p_event) GraphElement::_gui_input(p_event); } +bool OrchestratorGraphKnot::_has_point(const Vector2& p_point) const +{ + // Wrap mouse actions around the top-left point rather than around the snapped GraphElement + return Rect2(-(get_size() /2), get_size()).has_point(p_point); +} + void OrchestratorGraphKnot::_notification(int p_what) { - if (p_what == NOTIFICATION_READY) + switch (p_what) { - set_mouse_filter(MOUSE_FILTER_STOP); - - VBoxContainer* vbox = memnew(VBoxContainer); - add_child(vbox); + case NOTIFICATION_ENTER_TREE: + { + const double scale = EditorInterface::get_singleton()->get_editor_scale(); - _icon = memnew(TextureRect); - _icon->set_texture(SceneUtils::get_editor_icon("GuiGraphNodePort")); - _icon->set_custom_minimum_size(RENDER_ICON_SIZE); - _icon->set_modulate(_color); - vbox->add_child(_icon); + _icon = SceneUtils::get_editor_icon("GuiGraphNodePort"); + set_custom_minimum_size(_icon.is_valid() ? scale * _icon->get_size() : Size2(16, 16) * scale); + break; + } + case NOTIFICATION_READY: + { + connect("position_offset_changed", callable_mp(this, &OrchestratorGraphKnot::_position_changed)); + connect("node_selected", callable_mp(this, &OrchestratorGraphKnot::_node_selected)); + connect("node_deselected", callable_mp(this, &OrchestratorGraphKnot::_node_deselected)); + break; + } + case NOTIFICATION_DRAW: + { + const Vector2 icon_position = -get_size() / 2.0; - connect("position_offset_changed", callable_mp(this, &OrchestratorGraphKnot::_position_changed)); - connect("node_selected", callable_mp(this, &OrchestratorGraphKnot::_node_selected)); - connect("node_deselected", callable_mp(this, &OrchestratorGraphKnot::_node_deselected)); + // todo: add rim color + draw_texture(_icon, icon_position, _draw_color); + break; + } } } @@ -111,3 +129,13 @@ void OrchestratorGraphKnot::_bind_methods() ADD_SIGNAL(MethodInfo("knot_position_changed", PropertyInfo(Variant::VECTOR2, "position"))); ADD_SIGNAL(MethodInfo("knot_delete_requested", PropertyInfo(Variant::STRING, "name"))); } + +OrchestratorGraphKnot::OrchestratorGraphKnot() +{ + set_mouse_filter(MOUSE_FILTER_STOP); + + VBoxContainer* vbox = memnew(VBoxContainer); + vbox->set_h_size_flags(SIZE_EXPAND_FILL); + vbox->set_v_size_flags(SIZE_EXPAND_FILL); + add_child(vbox); +} diff --git a/src/editor/graph/graph_knot.h b/src/editor/graph/graph_knot.h index 757f4693..2918b6dc 100644 --- a/src/editor/graph/graph_knot.h +++ b/src/editor/graph/graph_knot.h @@ -35,8 +35,9 @@ class OrchestratorGraphKnot : public GraphElement OScriptConnection _connection; //! The connection this knot belongs Ref _graph; //! The owning graph Ref _knot; //! The knot - TextureRect* _icon; //! The icon Color _color; //! The knot color + Color _draw_color; //! The knot draw color + Ref _icon; //! Port icon //~ Begin Signal Handlers void _connections_changed(const String& p_caller); @@ -45,9 +46,6 @@ class OrchestratorGraphKnot : public GraphElement void _node_deselected(); //~ End Signal Handlers - const Vector2 RENDER_OFFSET{ 8, 8 }; - const Vector2 RENDER_ICON_SIZE = RENDER_OFFSET * 2; - public: //~ Begin Wrapped Interface void _notification(int p_what); @@ -55,6 +53,7 @@ class OrchestratorGraphKnot : public GraphElement //~ Begin Control Interface void _gui_input(const Ref& p_event) override; + bool _has_point(const Vector2& p_point) const override; //~ End Control Interface /// Set the owning graph @@ -79,7 +78,9 @@ class OrchestratorGraphKnot : public GraphElement /// Set the knot's color /// @param p_color the color - void set_color(const Color& p_color) { _color = p_color; } + void set_color(const Color& p_color); + + OrchestratorGraphKnot(); }; #endif // ORCHESTRATOR_GRAPH_KNOT_H \ No newline at end of file