| 1 | /**************************************************************************/ |
| 2 | /* graph_edit.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "graph_edit.h" |
| 32 | |
| 33 | #include "core/input/input.h" |
| 34 | #include "core/math/math_funcs.h" |
| 35 | #include "core/os/keyboard.h" |
| 36 | #include "scene/gui/box_container.h" |
| 37 | #include "scene/gui/button.h" |
| 38 | #include "scene/gui/graph_edit_arranger.h" |
| 39 | #include "scene/gui/view_panner.h" |
| 40 | #include "scene/resources/style_box_flat.h" |
| 41 | #include "scene/theme/theme_db.h" |
| 42 | |
| 43 | constexpr int MINIMAP_OFFSET = 12; |
| 44 | constexpr int MINIMAP_PADDING = 5; |
| 45 | constexpr int MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION = 20; |
| 46 | constexpr int MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES = 5; |
| 47 | constexpr int GRID_MINOR_STEPS_PER_MAJOR_LINE = 10; |
| 48 | constexpr int GRID_MIN_SNAPPING_DISTANCE = 2; |
| 49 | constexpr int GRID_MAX_SNAPPING_DISTANCE = 100; |
| 50 | constexpr float CONNECTING_TARGET_LINE_COLOR_BRIGHTENING = 0.4; |
| 51 | |
| 52 | bool GraphEditFilter::has_point(const Point2 &p_point) const { |
| 53 | return ge->_filter_input(p_point); |
| 54 | } |
| 55 | |
| 56 | GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) { |
| 57 | ge = p_edit; |
| 58 | } |
| 59 | |
| 60 | Control::CursorShape GraphEditMinimap::get_cursor_shape(const Point2 &p_pos) const { |
| 61 | if (is_resizing || (p_pos.x < theme_cache.resizer->get_width() && p_pos.y < theme_cache.resizer->get_height())) { |
| 62 | return CURSOR_FDIAGSIZE; |
| 63 | } |
| 64 | |
| 65 | return Control::get_cursor_shape(p_pos); |
| 66 | } |
| 67 | |
| 68 | void GraphEditMinimap::update_minimap() { |
| 69 | Vector2 graph_offset = _get_graph_offset(); |
| 70 | Vector2 graph_size = _get_graph_size(); |
| 71 | |
| 72 | camera_position = ge->get_scroll_offset() - graph_offset; |
| 73 | camera_size = ge->get_size(); |
| 74 | |
| 75 | Vector2 render_size = _get_render_size(); |
| 76 | float target_ratio = render_size.width / render_size.height; |
| 77 | float graph_ratio = graph_size.width / graph_size.height; |
| 78 | |
| 79 | graph_proportions = graph_size; |
| 80 | graph_padding = Vector2(0, 0); |
| 81 | if (graph_ratio > target_ratio) { |
| 82 | graph_proportions.width = graph_size.width; |
| 83 | graph_proportions.height = graph_size.width / target_ratio; |
| 84 | graph_padding.y = Math::abs(graph_size.height - graph_proportions.y) / 2; |
| 85 | } else { |
| 86 | graph_proportions.width = graph_size.height * target_ratio; |
| 87 | graph_proportions.height = graph_size.height; |
| 88 | graph_padding.x = Math::abs(graph_size.width - graph_proportions.x) / 2; |
| 89 | } |
| 90 | |
| 91 | // This centers minimap inside the minimap rectangle. |
| 92 | minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding); |
| 93 | } |
| 94 | |
| 95 | Rect2 GraphEditMinimap::get_camera_rect() { |
| 96 | Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset; |
| 97 | Vector2 camera_viewport = _convert_from_graph_position(camera_size); |
| 98 | Vector2 camera_pos = (camera_center - camera_viewport / 2); |
| 99 | return Rect2(camera_pos, camera_viewport); |
| 100 | } |
| 101 | |
| 102 | Vector2 GraphEditMinimap::_get_render_size() { |
| 103 | if (!is_inside_tree()) { |
| 104 | return Vector2(0, 0); |
| 105 | } |
| 106 | |
| 107 | return get_size() - 2 * minimap_padding; |
| 108 | } |
| 109 | |
| 110 | Vector2 GraphEditMinimap::_get_graph_offset() { |
| 111 | return Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min()); |
| 112 | } |
| 113 | |
| 114 | Vector2 GraphEditMinimap::_get_graph_size() { |
| 115 | Vector2 graph_size = Vector2(ge->h_scrollbar->get_max(), ge->v_scrollbar->get_max()) - Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min()); |
| 116 | |
| 117 | if (graph_size.width == 0) { |
| 118 | graph_size.width = 1; |
| 119 | } |
| 120 | if (graph_size.height == 0) { |
| 121 | graph_size.height = 1; |
| 122 | } |
| 123 | |
| 124 | return graph_size; |
| 125 | } |
| 126 | |
| 127 | Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) { |
| 128 | Vector2 map_position = Vector2(0, 0); |
| 129 | Vector2 render_size = _get_render_size(); |
| 130 | |
| 131 | map_position.x = p_position.x * render_size.width / graph_proportions.x; |
| 132 | map_position.y = p_position.y * render_size.height / graph_proportions.y; |
| 133 | |
| 134 | return map_position; |
| 135 | } |
| 136 | |
| 137 | Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) { |
| 138 | Vector2 graph_position = Vector2(0, 0); |
| 139 | Vector2 render_size = _get_render_size(); |
| 140 | |
| 141 | graph_position.x = p_position.x * graph_proportions.x / render_size.width; |
| 142 | graph_position.y = p_position.y * graph_proportions.y / render_size.height; |
| 143 | |
| 144 | return graph_position; |
| 145 | } |
| 146 | |
| 147 | void GraphEditMinimap::gui_input(const Ref<InputEvent> &p_ev) { |
| 148 | ERR_FAIL_COND(p_ev.is_null()); |
| 149 | |
| 150 | if (!ge->is_minimap_enabled()) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | Ref<InputEventMouseButton> mb = p_ev; |
| 155 | Ref<InputEventMouseMotion> mm = p_ev; |
| 156 | |
| 157 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { |
| 158 | if (mb->is_pressed()) { |
| 159 | is_pressing = true; |
| 160 | |
| 161 | Rect2 resizer_hitbox = Rect2(Point2(), theme_cache.resizer->get_size()); |
| 162 | if (resizer_hitbox.has_point(mb->get_position())) { |
| 163 | is_resizing = true; |
| 164 | } else { |
| 165 | Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding; |
| 166 | _adjust_graph_scroll(click_position); |
| 167 | } |
| 168 | } else { |
| 169 | is_pressing = false; |
| 170 | is_resizing = false; |
| 171 | } |
| 172 | accept_event(); |
| 173 | } else if (mm.is_valid() && is_pressing) { |
| 174 | if (is_resizing) { |
| 175 | // Prevent setting minimap wider than GraphEdit. |
| 176 | Vector2 new_minimap_size; |
| 177 | new_minimap_size.width = MIN(get_size().width - mm->get_relative().x, ge->get_size().width - 2.0 * minimap_padding.x); |
| 178 | new_minimap_size.height = MIN(get_size().height - mm->get_relative().y, ge->get_size().height - 2.0 * minimap_padding.y); |
| 179 | ge->set_minimap_size(new_minimap_size); |
| 180 | |
| 181 | queue_redraw(); |
| 182 | } else { |
| 183 | Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding; |
| 184 | _adjust_graph_scroll(click_position); |
| 185 | } |
| 186 | accept_event(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) { |
| 191 | Vector2 graph_offset = _get_graph_offset(); |
| 192 | ge->set_scroll_offset(p_offset + graph_offset - camera_size / 2); |
| 193 | } |
| 194 | |
| 195 | void GraphEditMinimap::_bind_methods() { |
| 196 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, panel); |
| 197 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, node_style, "node" ); |
| 198 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, camera_style, "camera" ); |
| 199 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEditMinimap, resizer); |
| 200 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEditMinimap, resizer_color); |
| 201 | } |
| 202 | |
| 203 | GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) { |
| 204 | ge = p_edit; |
| 205 | |
| 206 | minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING); |
| 207 | minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding); |
| 208 | } |
| 209 | |
| 210 | Control::CursorShape GraphEdit::get_cursor_shape(const Point2 &p_pos) const { |
| 211 | if (moving_selection) { |
| 212 | return CURSOR_MOVE; |
| 213 | } |
| 214 | |
| 215 | return Control::get_cursor_shape(p_pos); |
| 216 | } |
| 217 | |
| 218 | PackedStringArray GraphEdit::get_configuration_warnings() const { |
| 219 | PackedStringArray warnings = Control::get_configuration_warnings(); |
| 220 | |
| 221 | warnings.push_back(RTR("Please be aware that GraphEdit and GraphNode will undergo extensive refactoring in a future 4.x version involving compatibility-breaking API changes." )); |
| 222 | |
| 223 | return warnings; |
| 224 | } |
| 225 | |
| 226 | Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) { |
| 227 | if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) { |
| 228 | return OK; |
| 229 | } |
| 230 | Connection c; |
| 231 | c.from_node = p_from; |
| 232 | c.from_port = p_from_port; |
| 233 | c.to_node = p_to; |
| 234 | c.to_port = p_to_port; |
| 235 | c.activity = 0; |
| 236 | connections.push_back(c); |
| 237 | top_layer->queue_redraw(); |
| 238 | minimap->queue_redraw(); |
| 239 | queue_redraw(); |
| 240 | connections_layer->queue_redraw(); |
| 241 | |
| 242 | return OK; |
| 243 | } |
| 244 | |
| 245 | bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) { |
| 246 | for (const Connection &E : connections) { |
| 247 | if (E.from_node == p_from && E.from_port == p_from_port && E.to_node == p_to && E.to_port == p_to_port) { |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) { |
| 256 | for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) { |
| 257 | if (E->get().from_node == p_from && E->get().from_port == p_from_port && E->get().to_node == p_to && E->get().to_port == p_to_port) { |
| 258 | connections.erase(E); |
| 259 | top_layer->queue_redraw(); |
| 260 | minimap->queue_redraw(); |
| 261 | queue_redraw(); |
| 262 | connections_layer->queue_redraw(); |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void GraphEdit::get_connection_list(List<Connection> *r_connections) const { |
| 269 | *r_connections = connections; |
| 270 | } |
| 271 | |
| 272 | void GraphEdit::set_scroll_offset(const Vector2 &p_offset) { |
| 273 | setting_scroll_offset = true; |
| 274 | h_scrollbar->set_value(p_offset.x); |
| 275 | v_scrollbar->set_value(p_offset.y); |
| 276 | _update_scroll(); |
| 277 | setting_scroll_offset = false; |
| 278 | } |
| 279 | |
| 280 | Vector2 GraphEdit::get_scroll_offset() const { |
| 281 | return Vector2(h_scrollbar->get_value(), v_scrollbar->get_value()); |
| 282 | } |
| 283 | |
| 284 | void GraphEdit::_scroll_moved(double) { |
| 285 | if (!awaiting_scroll_offset_update) { |
| 286 | callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred(); |
| 287 | awaiting_scroll_offset_update = true; |
| 288 | } |
| 289 | top_layer->queue_redraw(); |
| 290 | minimap->queue_redraw(); |
| 291 | queue_redraw(); |
| 292 | } |
| 293 | |
| 294 | void GraphEdit::_update_scroll_offset() { |
| 295 | set_block_minimum_size_adjust(true); |
| 296 | |
| 297 | for (int i = 0; i < get_child_count(); i++) { |
| 298 | GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i)); |
| 299 | if (!graph_element) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | Point2 pos = graph_element->get_position_offset() * zoom; |
| 304 | pos -= Point2(h_scrollbar->get_value(), v_scrollbar->get_value()); |
| 305 | graph_element->set_position(pos); |
| 306 | if (graph_element->get_scale() != Vector2(zoom, zoom)) { |
| 307 | graph_element->set_scale(Vector2(zoom, zoom)); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | connections_layer->set_position(-Point2(h_scrollbar->get_value(), v_scrollbar->get_value())); |
| 312 | set_block_minimum_size_adjust(false); |
| 313 | awaiting_scroll_offset_update = false; |
| 314 | |
| 315 | // In Godot, signals on value change are avoided by convention. |
| 316 | if (!setting_scroll_offset) { |
| 317 | emit_signal(SNAME("scroll_offset_changed" ), get_scroll_offset()); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void GraphEdit::_update_scroll() { |
| 322 | if (updating) { |
| 323 | return; |
| 324 | } |
| 325 | updating = true; |
| 326 | |
| 327 | set_block_minimum_size_adjust(true); |
| 328 | |
| 329 | Rect2 screen_rect; |
| 330 | for (int i = 0; i < get_child_count(); i++) { |
| 331 | GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i)); |
| 332 | if (!graph_element) { |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | Rect2 node_rect; |
| 337 | node_rect.position = graph_element->get_position_offset() * zoom; |
| 338 | node_rect.size = graph_element->get_size() * zoom; |
| 339 | screen_rect = screen_rect.merge(node_rect); |
| 340 | } |
| 341 | |
| 342 | screen_rect.position -= get_size(); |
| 343 | screen_rect.size += get_size() * 2.0; |
| 344 | |
| 345 | h_scrollbar->set_min(screen_rect.position.x); |
| 346 | h_scrollbar->set_max(screen_rect.position.x + screen_rect.size.width); |
| 347 | h_scrollbar->set_page(get_size().x); |
| 348 | if (h_scrollbar->get_max() - h_scrollbar->get_min() <= h_scrollbar->get_page()) { |
| 349 | h_scrollbar->hide(); |
| 350 | } else { |
| 351 | h_scrollbar->show(); |
| 352 | } |
| 353 | |
| 354 | v_scrollbar->set_min(screen_rect.position.y); |
| 355 | v_scrollbar->set_max(screen_rect.position.y + screen_rect.size.height); |
| 356 | v_scrollbar->set_page(get_size().height); |
| 357 | |
| 358 | if (v_scrollbar->get_max() - v_scrollbar->get_min() <= v_scrollbar->get_page()) { |
| 359 | v_scrollbar->hide(); |
| 360 | } else { |
| 361 | v_scrollbar->show(); |
| 362 | } |
| 363 | |
| 364 | Size2 hmin = h_scrollbar->get_combined_minimum_size(); |
| 365 | Size2 vmin = v_scrollbar->get_combined_minimum_size(); |
| 366 | |
| 367 | // Avoid scrollbar overlapping. |
| 368 | h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scrollbar->is_visible() ? -vmin.width : 0); |
| 369 | v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scrollbar->is_visible() ? -hmin.height : 0); |
| 370 | |
| 371 | set_block_minimum_size_adjust(false); |
| 372 | |
| 373 | if (!awaiting_scroll_offset_update) { |
| 374 | callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred(); |
| 375 | awaiting_scroll_offset_update = true; |
| 376 | } |
| 377 | |
| 378 | updating = false; |
| 379 | } |
| 380 | |
| 381 | void GraphEdit::_graph_element_moved_to_front(Node *p_node) { |
| 382 | GraphElement *graph_element = Object::cast_to<GraphElement>(p_node); |
| 383 | ERR_FAIL_NULL(graph_element); |
| 384 | |
| 385 | graph_element->move_to_front(); |
| 386 | } |
| 387 | |
| 388 | void GraphEdit::_graph_element_selected(Node *p_node) { |
| 389 | GraphElement *graph_element = Object::cast_to<GraphElement>(p_node); |
| 390 | ERR_FAIL_NULL(graph_element); |
| 391 | |
| 392 | emit_signal(SNAME("node_selected" ), graph_element); |
| 393 | } |
| 394 | |
| 395 | void GraphEdit::_graph_element_deselected(Node *p_node) { |
| 396 | GraphElement *graph_element = Object::cast_to<GraphElement>(p_node); |
| 397 | ERR_FAIL_NULL(graph_element); |
| 398 | |
| 399 | emit_signal(SNAME("node_deselected" ), graph_element); |
| 400 | } |
| 401 | |
| 402 | void GraphEdit::_graph_element_resized(Vector2 p_new_minsize, Node *p_node) { |
| 403 | GraphElement *graph_element = Object::cast_to<GraphElement>(p_node); |
| 404 | ERR_FAIL_NULL(graph_element); |
| 405 | |
| 406 | graph_element->set_size(p_new_minsize); |
| 407 | } |
| 408 | |
| 409 | void GraphEdit::_graph_element_moved(Node *p_node) { |
| 410 | GraphElement *graph_element = Object::cast_to<GraphElement>(p_node); |
| 411 | ERR_FAIL_NULL(graph_element); |
| 412 | |
| 413 | top_layer->queue_redraw(); |
| 414 | minimap->queue_redraw(); |
| 415 | queue_redraw(); |
| 416 | connections_layer->queue_redraw(); |
| 417 | } |
| 418 | |
| 419 | void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) { |
| 420 | GraphNode *graph_node = Object::cast_to<GraphNode>(p_node); |
| 421 | ERR_FAIL_NULL(graph_node); |
| 422 | |
| 423 | top_layer->queue_redraw(); |
| 424 | minimap->queue_redraw(); |
| 425 | queue_redraw(); |
| 426 | connections_layer->queue_redraw(); |
| 427 | } |
| 428 | |
| 429 | void GraphEdit::add_child_notify(Node *p_child) { |
| 430 | Control::add_child_notify(p_child); |
| 431 | |
| 432 | // Keep the top layer always on top! |
| 433 | callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred(); |
| 434 | |
| 435 | GraphElement *graph_element = Object::cast_to<GraphElement>(p_child); |
| 436 | if (graph_element) { |
| 437 | graph_element->connect("position_offset_changed" , callable_mp(this, &GraphEdit::_graph_element_moved).bind(graph_element)); |
| 438 | graph_element->connect("node_selected" , callable_mp(this, &GraphEdit::_graph_element_selected).bind(graph_element)); |
| 439 | graph_element->connect("node_deselected" , callable_mp(this, &GraphEdit::_graph_element_deselected).bind(graph_element)); |
| 440 | |
| 441 | GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element); |
| 442 | if (graph_node) { |
| 443 | graph_element->connect("slot_updated" , callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(graph_element)); |
| 444 | } |
| 445 | |
| 446 | graph_element->connect("raise_request" , callable_mp(this, &GraphEdit::_graph_element_moved_to_front).bind(graph_element)); |
| 447 | graph_element->connect("resize_request" , callable_mp(this, &GraphEdit::_graph_element_resized).bind(graph_element)); |
| 448 | graph_element->connect("item_rect_changed" , callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw)); |
| 449 | graph_element->connect("item_rect_changed" , callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw)); |
| 450 | |
| 451 | graph_element->set_scale(Vector2(zoom, zoom)); |
| 452 | _graph_element_moved(graph_element); |
| 453 | graph_element->set_mouse_filter(MOUSE_FILTER_PASS); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | void GraphEdit::remove_child_notify(Node *p_child) { |
| 458 | Control::remove_child_notify(p_child); |
| 459 | |
| 460 | if (p_child == top_layer) { |
| 461 | top_layer = nullptr; |
| 462 | minimap = nullptr; |
| 463 | } else if (p_child == connections_layer) { |
| 464 | connections_layer = nullptr; |
| 465 | } |
| 466 | |
| 467 | if (top_layer != nullptr && is_inside_tree()) { |
| 468 | // Keep the top layer always on top! |
| 469 | callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred(); |
| 470 | } |
| 471 | |
| 472 | GraphElement *graph_element = Object::cast_to<GraphElement>(p_child); |
| 473 | if (graph_element) { |
| 474 | graph_element->disconnect("position_offset_changed" , callable_mp(this, &GraphEdit::_graph_element_moved)); |
| 475 | graph_element->disconnect("node_selected" , callable_mp(this, &GraphEdit::_graph_element_selected)); |
| 476 | graph_element->disconnect("node_deselected" , callable_mp(this, &GraphEdit::_graph_element_deselected)); |
| 477 | |
| 478 | GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element); |
| 479 | if (graph_node) { |
| 480 | graph_element->disconnect("slot_updated" , callable_mp(this, &GraphEdit::_graph_node_slot_updated)); |
| 481 | } |
| 482 | |
| 483 | graph_element->disconnect("raise_request" , callable_mp(this, &GraphEdit::_graph_element_moved_to_front)); |
| 484 | graph_element->disconnect("resize_request" , callable_mp(this, &GraphEdit::_graph_element_resized)); |
| 485 | |
| 486 | // In case of the whole GraphEdit being destroyed these references can already be freed. |
| 487 | if (connections_layer != nullptr && connections_layer->is_inside_tree()) { |
| 488 | graph_element->disconnect("item_rect_changed" , callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw)); |
| 489 | } |
| 490 | if (minimap != nullptr && minimap->is_inside_tree()) { |
| 491 | graph_element->disconnect("item_rect_changed" , callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw)); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void GraphEdit::_update_theme_item_cache() { |
| 497 | Control::_update_theme_item_cache(); |
| 498 | |
| 499 | theme_cache.base_scale = get_theme_default_base_scale(); |
| 500 | } |
| 501 | |
| 502 | void GraphEdit::_notification(int p_what) { |
| 503 | switch (p_what) { |
| 504 | case NOTIFICATION_THEME_CHANGED: { |
| 505 | zoom_minus_button->set_icon(theme_cache.zoom_out); |
| 506 | zoom_reset_button->set_icon(theme_cache.zoom_reset); |
| 507 | zoom_plus_button->set_icon(theme_cache.zoom_in); |
| 508 | |
| 509 | toggle_snapping_button->set_icon(theme_cache.snapping_toggle); |
| 510 | show_grid_button->set_icon(theme_cache.grid_toggle); |
| 511 | minimap_button->set_icon(theme_cache.minimap_toggle); |
| 512 | layout_button->set_icon(theme_cache.layout); |
| 513 | |
| 514 | zoom_label->set_custom_minimum_size(Size2(48, 0) * theme_cache.base_scale); |
| 515 | } break; |
| 516 | |
| 517 | case NOTIFICATION_READY: { |
| 518 | Size2 hmin = h_scrollbar->get_combined_minimum_size(); |
| 519 | Size2 vmin = v_scrollbar->get_combined_minimum_size(); |
| 520 | |
| 521 | h_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0); |
| 522 | h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0); |
| 523 | h_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height); |
| 524 | h_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0); |
| 525 | |
| 526 | v_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width); |
| 527 | v_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0); |
| 528 | v_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0); |
| 529 | v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0); |
| 530 | } break; |
| 531 | |
| 532 | case NOTIFICATION_DRAW: { |
| 533 | // Draw background fill. |
| 534 | draw_style_box(theme_cache.panel, Rect2(Point2(), get_size())); |
| 535 | |
| 536 | // Draw background grid. |
| 537 | if (show_grid) { |
| 538 | Vector2 offset = get_scroll_offset() / zoom; |
| 539 | Size2 size = get_size() / zoom; |
| 540 | |
| 541 | Point2i from_pos = (offset / float(snapping_distance)).floor(); |
| 542 | Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1); |
| 543 | |
| 544 | for (int i = from_pos.x; i < from_pos.x + len.x; i++) { |
| 545 | Color color; |
| 546 | |
| 547 | if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) { |
| 548 | color = theme_cache.grid_major; |
| 549 | } else { |
| 550 | color = theme_cache.grid_minor; |
| 551 | } |
| 552 | |
| 553 | float base_offset = i * snapping_distance * zoom - offset.x * zoom; |
| 554 | draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color); |
| 555 | } |
| 556 | |
| 557 | for (int i = from_pos.y; i < from_pos.y + len.y; i++) { |
| 558 | Color color; |
| 559 | |
| 560 | if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) { |
| 561 | color = theme_cache.grid_major; |
| 562 | } else { |
| 563 | color = theme_cache.grid_minor; |
| 564 | } |
| 565 | |
| 566 | float base_offset = i * snapping_distance * zoom - offset.y * zoom; |
| 567 | draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color); |
| 568 | } |
| 569 | } |
| 570 | } break; |
| 571 | |
| 572 | case NOTIFICATION_RESIZED: { |
| 573 | _update_scroll(); |
| 574 | top_layer->queue_redraw(); |
| 575 | minimap->queue_redraw(); |
| 576 | } break; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | bool GraphEdit::_filter_input(const Point2 &p_point) { |
| 581 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 582 | GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i)); |
| 583 | if (!graph_node || !graph_node->is_visible_in_tree()) { |
| 584 | continue; |
| 585 | } |
| 586 | |
| 587 | Ref<Texture2D> port_icon = graph_node->theme_cache.port; |
| 588 | |
| 589 | for (int j = 0; j < graph_node->get_input_port_count(); j++) { |
| 590 | Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height()); |
| 591 | |
| 592 | // Determine slot height. |
| 593 | int slot_index = graph_node->get_input_port_slot(j); |
| 594 | Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index)); |
| 595 | |
| 596 | port_size.height = MAX(port_size.height, child ? child->get_size().y : 0); |
| 597 | |
| 598 | if (is_in_input_hotzone(graph_node, j, p_point / zoom, port_size)) { |
| 599 | return true; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | for (int j = 0; j < graph_node->get_output_port_count(); j++) { |
| 604 | Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height()); |
| 605 | |
| 606 | // Determine slot height. |
| 607 | int slot_index = graph_node->get_output_port_slot(j); |
| 608 | Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index)); |
| 609 | port_size.height = MAX(port_size.height, child ? child->get_size().y : 0); |
| 610 | |
| 611 | if (is_in_output_hotzone(graph_node, j, p_point / zoom, port_size)) { |
| 612 | return true; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { |
| 621 | Ref<InputEventMouseButton> mb = p_ev; |
| 622 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) { |
| 623 | connecting_valid = false; |
| 624 | click_pos = mb->get_position() / zoom; |
| 625 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 626 | GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i)); |
| 627 | if (!graph_node || !graph_node->is_visible_in_tree()) { |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | Ref<Texture2D> port_icon = graph_node->theme_cache.port; |
| 632 | |
| 633 | for (int j = 0; j < graph_node->get_output_port_count(); j++) { |
| 634 | Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position(); |
| 635 | Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height()); |
| 636 | |
| 637 | // Determine slot height. |
| 638 | int slot_index = graph_node->get_output_port_slot(j); |
| 639 | Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index)); |
| 640 | port_size.height = MAX(port_size.height, child ? child->get_size().y : 0); |
| 641 | |
| 642 | if (is_in_output_hotzone(graph_node, j, click_pos, port_size)) { |
| 643 | if (valid_left_disconnect_types.has(graph_node->get_output_port_type(j))) { |
| 644 | // Check disconnect. |
| 645 | for (const Connection &E : connections) { |
| 646 | if (E.from_node == graph_node->get_name() && E.from_port == j) { |
| 647 | Node *to = get_node(NodePath(E.to_node)); |
| 648 | if (Object::cast_to<GraphNode>(to)) { |
| 649 | connecting_from = E.to_node; |
| 650 | connecting_index = E.to_port; |
| 651 | connecting_out = false; |
| 652 | connecting_type = Object::cast_to<GraphNode>(to)->get_input_port_type(E.to_port); |
| 653 | connecting_color = Object::cast_to<GraphNode>(to)->get_input_port_color(E.to_port); |
| 654 | connecting_target = false; |
| 655 | connecting_to = pos; |
| 656 | |
| 657 | if (connecting_type >= 0) { |
| 658 | just_disconnected = true; |
| 659 | |
| 660 | emit_signal(SNAME("disconnection_request" ), E.from_node, E.from_port, E.to_node, E.to_port); |
| 661 | to = get_node(NodePath(connecting_from)); // Maybe it was erased. |
| 662 | if (Object::cast_to<GraphNode>(to)) { |
| 663 | connecting = true; |
| 664 | emit_signal(SNAME("connection_drag_started" ), connecting_from, connecting_index, false); |
| 665 | } |
| 666 | } |
| 667 | return; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | connecting_from = graph_node->get_name(); |
| 674 | connecting_index = j; |
| 675 | connecting_out = true; |
| 676 | connecting_type = graph_node->get_output_port_type(j); |
| 677 | connecting_color = graph_node->get_output_port_color(j); |
| 678 | connecting_target = false; |
| 679 | connecting_to = pos; |
| 680 | if (connecting_type >= 0) { |
| 681 | connecting = true; |
| 682 | just_disconnected = false; |
| 683 | emit_signal(SNAME("connection_drag_started" ), connecting_from, connecting_index, true); |
| 684 | } |
| 685 | return; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | for (int j = 0; j < graph_node->get_input_port_count(); j++) { |
| 690 | Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position(); |
| 691 | |
| 692 | Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height()); |
| 693 | |
| 694 | // Determine slot height. |
| 695 | int slot_index = graph_node->get_input_port_slot(j); |
| 696 | Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index)); |
| 697 | port_size.height = MAX(port_size.height, child ? child->get_size().y : 0); |
| 698 | |
| 699 | if (is_in_input_hotzone(graph_node, j, click_pos, port_size)) { |
| 700 | if (right_disconnects || valid_right_disconnect_types.has(graph_node->get_input_port_type(j))) { |
| 701 | // Check disconnect. |
| 702 | for (const Connection &E : connections) { |
| 703 | if (E.to_node == graph_node->get_name() && E.to_port == j) { |
| 704 | Node *fr = get_node(NodePath(E.from_node)); |
| 705 | if (Object::cast_to<GraphNode>(fr)) { |
| 706 | connecting_from = E.from_node; |
| 707 | connecting_index = E.from_port; |
| 708 | connecting_out = true; |
| 709 | connecting_type = Object::cast_to<GraphNode>(fr)->get_output_port_type(E.from_port); |
| 710 | connecting_color = Object::cast_to<GraphNode>(fr)->get_output_port_color(E.from_port); |
| 711 | connecting_target = false; |
| 712 | connecting_to = pos; |
| 713 | just_disconnected = true; |
| 714 | |
| 715 | if (connecting_type >= 0) { |
| 716 | emit_signal(SNAME("disconnection_request" ), E.from_node, E.from_port, E.to_node, E.to_port); |
| 717 | fr = get_node(NodePath(connecting_from)); |
| 718 | if (Object::cast_to<GraphNode>(fr)) { |
| 719 | connecting = true; |
| 720 | emit_signal(SNAME("connection_drag_started" ), connecting_from, connecting_index, true); |
| 721 | } |
| 722 | } |
| 723 | return; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | connecting_from = graph_node->get_name(); |
| 730 | connecting_index = j; |
| 731 | connecting_out = false; |
| 732 | connecting_type = graph_node->get_input_port_type(j); |
| 733 | connecting_color = graph_node->get_input_port_color(j); |
| 734 | connecting_target = false; |
| 735 | connecting_to = pos; |
| 736 | if (connecting_type >= 0) { |
| 737 | connecting = true; |
| 738 | just_disconnected = false; |
| 739 | emit_signal(SNAME("connection_drag_started" ), connecting_from, connecting_index, false); |
| 740 | } |
| 741 | return; |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | Ref<InputEventMouseMotion> mm = p_ev; |
| 748 | if (mm.is_valid() && connecting) { |
| 749 | connecting_to = mm->get_position(); |
| 750 | connecting_target = false; |
| 751 | top_layer->queue_redraw(); |
| 752 | minimap->queue_redraw(); |
| 753 | |
| 754 | connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION; |
| 755 | |
| 756 | if (connecting_valid) { |
| 757 | Vector2 mpos = mm->get_position() / zoom; |
| 758 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 759 | GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i)); |
| 760 | if (!graph_node || !graph_node->is_visible_in_tree()) { |
| 761 | continue; |
| 762 | } |
| 763 | |
| 764 | Ref<Texture2D> port_icon = graph_node->theme_cache.port; |
| 765 | |
| 766 | if (!connecting_out) { |
| 767 | for (int j = 0; j < graph_node->get_output_port_count(); j++) { |
| 768 | Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position(); |
| 769 | Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height()); |
| 770 | |
| 771 | // Determine slot height. |
| 772 | int slot_index = graph_node->get_output_port_slot(j); |
| 773 | Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index)); |
| 774 | port_size.height = MAX(port_size.height, child ? child->get_size().y : 0); |
| 775 | |
| 776 | int type = graph_node->get_output_port_type(j); |
| 777 | if ((type == connecting_type || |
| 778 | valid_connection_types.has(ConnectionType(type, connecting_type))) && |
| 779 | is_in_output_hotzone(graph_node, j, mpos, port_size)) { |
| 780 | if (!is_node_hover_valid(graph_node->get_name(), j, connecting_from, connecting_index)) { |
| 781 | continue; |
| 782 | } |
| 783 | connecting_target = true; |
| 784 | connecting_to = pos; |
| 785 | connecting_target_to = graph_node->get_name(); |
| 786 | connecting_target_index = j; |
| 787 | return; |
| 788 | } |
| 789 | } |
| 790 | } else { |
| 791 | for (int j = 0; j < graph_node->get_input_port_count(); j++) { |
| 792 | Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position(); |
| 793 | Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height()); |
| 794 | |
| 795 | // Determine slot height. |
| 796 | int slot_index = graph_node->get_input_port_slot(j); |
| 797 | Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index)); |
| 798 | port_size.height = MAX(port_size.height, child ? child->get_size().y : 0); |
| 799 | |
| 800 | int type = graph_node->get_input_port_type(j); |
| 801 | if ((type == connecting_type || valid_connection_types.has(ConnectionType(connecting_type, type))) && |
| 802 | is_in_input_hotzone(graph_node, j, mpos, port_size)) { |
| 803 | if (!is_node_hover_valid(connecting_from, connecting_index, graph_node->get_name(), j)) { |
| 804 | continue; |
| 805 | } |
| 806 | connecting_target = true; |
| 807 | connecting_to = pos; |
| 808 | connecting_target_to = graph_node->get_name(); |
| 809 | connecting_target_index = j; |
| 810 | return; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) { |
| 819 | if (connecting_valid) { |
| 820 | if (connecting && connecting_target) { |
| 821 | if (connecting_out) { |
| 822 | emit_signal(SNAME("connection_request" ), connecting_from, connecting_index, connecting_target_to, connecting_target_index); |
| 823 | } else { |
| 824 | emit_signal(SNAME("connection_request" ), connecting_target_to, connecting_target_index, connecting_from, connecting_index); |
| 825 | } |
| 826 | } else if (!just_disconnected) { |
| 827 | if (connecting_out) { |
| 828 | emit_signal(SNAME("connection_to_empty" ), connecting_from, connecting_index, mb->get_position()); |
| 829 | } else { |
| 830 | emit_signal(SNAME("connection_from_empty" ), connecting_from, connecting_index, mb->get_position()); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | if (connecting) { |
| 836 | force_connection_drag_end(); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) { |
| 842 | if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) { |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | Rect2 control_rect = p_control->get_rect(); |
| 847 | control_rect.position *= zoom; |
| 848 | control_rect.size *= zoom; |
| 849 | control_rect.position += p_offset; |
| 850 | |
| 851 | if (!control_rect.has_point(mpos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) { |
| 852 | // Test children. |
| 853 | for (int i = 0; i < p_control->get_child_count(); i++) { |
| 854 | Control *child_rect = Object::cast_to<Control>(p_control->get_child(i)); |
| 855 | if (!child_rect) { |
| 856 | continue; |
| 857 | } |
| 858 | if (_check_clickable_control(child_rect, mpos, control_rect.position)) { |
| 859 | return true; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | return false; |
| 864 | } else { |
| 865 | return true; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | bool GraphEdit::is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) { |
| 870 | bool success; |
| 871 | if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) { |
| 872 | return success; |
| 873 | } else { |
| 874 | Vector2 pos = p_graph_node->get_input_port_position(p_port_idx) * zoom + p_graph_node->get_position(); |
| 875 | return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | bool GraphEdit::is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) { |
| 880 | if (p_graph_node->is_resizable()) { |
| 881 | Ref<Texture2D> resizer = p_graph_node->theme_cache.resizer; |
| 882 | Rect2 resizer_rect = Rect2(p_graph_node->get_position() / zoom + p_graph_node->get_size() - resizer->get_size(), resizer->get_size()); |
| 883 | if (resizer_rect.has_point(p_mouse_pos)) { |
| 884 | return false; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | bool success; |
| 889 | if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) { |
| 890 | return success; |
| 891 | } else { |
| 892 | Vector2 pos = p_graph_node->get_output_port_position(p_port_idx) * zoom + p_graph_node->get_position(); |
| 893 | return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | bool GraphEdit::is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) { |
| 898 | Rect2 hotzone = Rect2( |
| 899 | p_pos.x - (p_left ? theme_cache.port_hotzone_outer_extent : theme_cache.port_hotzone_inner_extent), |
| 900 | p_pos.y - p_port_size.height / 2.0, |
| 901 | theme_cache.port_hotzone_inner_extent + theme_cache.port_hotzone_outer_extent, |
| 902 | p_port_size.height); |
| 903 | |
| 904 | if (!hotzone.has_point(p_mouse_pos)) { |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | for (int i = 0; i < get_child_count(); i++) { |
| 909 | GraphNode *child = Object::cast_to<GraphNode>(get_child(i)); |
| 910 | if (!child) { |
| 911 | continue; |
| 912 | } |
| 913 | |
| 914 | Rect2 child_rect = child->get_rect(); |
| 915 | if (child_rect.has_point(p_mouse_pos * zoom)) { |
| 916 | for (int j = 0; j < child->get_child_count(); j++) { |
| 917 | Control *subchild = Object::cast_to<Control>(child->get_child(j)); |
| 918 | if (!subchild) { |
| 919 | continue; |
| 920 | } |
| 921 | |
| 922 | if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) { |
| 923 | return false; |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | return true; |
| 930 | } |
| 931 | |
| 932 | PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) { |
| 933 | Vector<Vector2> ret; |
| 934 | if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) { |
| 935 | return ret; |
| 936 | } |
| 937 | |
| 938 | float x_diff = (p_to.x - p_from.x); |
| 939 | float cp_offset = x_diff * lines_curvature; |
| 940 | if (x_diff < 0) { |
| 941 | cp_offset *= -1; |
| 942 | } |
| 943 | |
| 944 | Curve2D curve; |
| 945 | curve.add_point(p_from); |
| 946 | curve.set_point_out(0, Vector2(cp_offset, 0)); |
| 947 | curve.add_point(p_to); |
| 948 | curve.set_point_in(1, Vector2(-cp_offset, 0)); |
| 949 | |
| 950 | if (lines_curvature > 0) { |
| 951 | return curve.tessellate(MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES, 2.0); |
| 952 | } else { |
| 953 | return curve.tessellate(1); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | void GraphEdit::_draw_connection_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color, float p_width, float p_zoom) { |
| 958 | Vector<Vector2> points = get_connection_line(p_from / p_zoom, p_to / p_zoom); |
| 959 | Vector<Vector2> scaled_points; |
| 960 | Vector<Color> colors; |
| 961 | float length = (p_from / p_zoom).distance_to(p_to / p_zoom); |
| 962 | for (int i = 0; i < points.size(); i++) { |
| 963 | float d = (p_from / p_zoom).distance_to(points[i]) / length; |
| 964 | colors.push_back(p_color.lerp(p_to_color, d)); |
| 965 | scaled_points.push_back(points[i] * p_zoom); |
| 966 | } |
| 967 | |
| 968 | // Thickness below 0.5 doesn't look good on the graph or its minimap. |
| 969 | p_where->draw_polyline_colors(scaled_points, colors, MAX(0.5, Math::floor(p_width * theme_cache.base_scale)), lines_antialiased); |
| 970 | } |
| 971 | |
| 972 | void GraphEdit::_connections_layer_draw() { |
| 973 | // Draw connections. |
| 974 | List<List<Connection>::Element *> to_erase; |
| 975 | for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { |
| 976 | const Connection &c = E->get(); |
| 977 | |
| 978 | Node *from = get_node(NodePath(c.from_node)); |
| 979 | GraphNode *gnode_from = Object::cast_to<GraphNode>(from); |
| 980 | |
| 981 | if (!gnode_from) { |
| 982 | to_erase.push_back(E); |
| 983 | continue; |
| 984 | } |
| 985 | |
| 986 | Node *to = get_node(NodePath(c.to_node)); |
| 987 | GraphNode *gnode_to = Object::cast_to<GraphNode>(to); |
| 988 | |
| 989 | if (!gnode_to) { |
| 990 | to_erase.push_back(E); |
| 991 | continue; |
| 992 | } |
| 993 | |
| 994 | Vector2 frompos = gnode_from->get_output_port_position(c.from_port) * zoom + gnode_from->get_position_offset() * zoom; |
| 995 | Color color = gnode_from->get_output_port_color(c.from_port); |
| 996 | Vector2 topos = gnode_to->get_input_port_position(c.to_port) * zoom + gnode_to->get_position_offset() * zoom; |
| 997 | Color tocolor = gnode_to->get_input_port_color(c.to_port); |
| 998 | |
| 999 | if (c.activity > 0) { |
| 1000 | color = color.lerp(theme_cache.activity_color, c.activity); |
| 1001 | tocolor = tocolor.lerp(theme_cache.activity_color, c.activity); |
| 1002 | } |
| 1003 | _draw_connection_line(connections_layer, frompos, topos, color, tocolor, lines_thickness, zoom); |
| 1004 | } |
| 1005 | |
| 1006 | for (List<Connection>::Element *&E : to_erase) { |
| 1007 | connections.erase(E); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | void GraphEdit::_top_layer_draw() { |
| 1012 | _update_scroll(); |
| 1013 | |
| 1014 | if (connecting) { |
| 1015 | Node *node_from = get_node_or_null(NodePath(connecting_from)); |
| 1016 | ERR_FAIL_NULL(node_from); |
| 1017 | GraphNode *graph_node_from = Object::cast_to<GraphNode>(node_from); |
| 1018 | ERR_FAIL_NULL(graph_node_from); |
| 1019 | Vector2 pos; |
| 1020 | if (connecting_out) { |
| 1021 | pos = graph_node_from->get_output_port_position(connecting_index) * zoom; |
| 1022 | } else { |
| 1023 | pos = graph_node_from->get_input_port_position(connecting_index) * zoom; |
| 1024 | } |
| 1025 | pos += graph_node_from->get_position(); |
| 1026 | |
| 1027 | Vector2 to_pos = connecting_to; |
| 1028 | Color line_color = connecting_color; |
| 1029 | |
| 1030 | // Draw the line to the mouse cursor brighter when it's over a valid target port. |
| 1031 | if (connecting_target) { |
| 1032 | line_color.r += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING; |
| 1033 | line_color.g += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING; |
| 1034 | line_color.b += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING; |
| 1035 | } |
| 1036 | |
| 1037 | if (!connecting_out) { |
| 1038 | SWAP(pos, to_pos); |
| 1039 | } |
| 1040 | _draw_connection_line(top_layer, pos, to_pos, line_color, line_color, lines_thickness, zoom); |
| 1041 | } |
| 1042 | |
| 1043 | if (box_selecting) { |
| 1044 | top_layer->draw_rect(box_selecting_rect, theme_cache.selection_fill); |
| 1045 | top_layer->draw_rect(box_selecting_rect, theme_cache.selection_stroke, false); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | void GraphEdit::_minimap_draw() { |
| 1050 | if (!is_minimap_enabled()) { |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | minimap->update_minimap(); |
| 1055 | |
| 1056 | // Draw the minimap background. |
| 1057 | Rect2 minimap_rect = Rect2(Point2(), minimap->get_size()); |
| 1058 | minimap->draw_style_box(minimap->theme_cache.panel, minimap_rect); |
| 1059 | |
| 1060 | Vector2 graph_offset = minimap->_get_graph_offset(); |
| 1061 | Vector2 minimap_offset = minimap->minimap_offset; |
| 1062 | |
| 1063 | // Draw graph nodes. |
| 1064 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1065 | GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i)); |
| 1066 | if (!graph_node || !graph_node->is_visible()) { |
| 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | Vector2 node_position = minimap->_convert_from_graph_position(graph_node->get_position_offset() * zoom - graph_offset) + minimap_offset; |
| 1071 | Vector2 node_size = minimap->_convert_from_graph_position(graph_node->get_size() * zoom); |
| 1072 | Rect2 node_rect = Rect2(node_position, node_size); |
| 1073 | |
| 1074 | Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate(); |
| 1075 | |
| 1076 | // Override default values with colors provided by the GraphNode's stylebox, if possible. |
| 1077 | Ref<StyleBoxFlat> sb_frame = graph_node->is_selected() ? graph_node->theme_cache.panel_selected : graph_node->theme_cache.panel; |
| 1078 | if (sb_frame.is_valid()) { |
| 1079 | Color node_color = sb_frame->get_bg_color(); |
| 1080 | sb_minimap->set_bg_color(node_color); |
| 1081 | } |
| 1082 | |
| 1083 | minimap->draw_style_box(sb_minimap, node_rect); |
| 1084 | } |
| 1085 | |
| 1086 | // Draw node connections. |
| 1087 | for (const Connection &E : connections) { |
| 1088 | Node *from = get_node(NodePath(E.from_node)); |
| 1089 | GraphNode *graph_node_from = Object::cast_to<GraphNode>(from); |
| 1090 | if (!graph_node_from) { |
| 1091 | continue; |
| 1092 | } |
| 1093 | |
| 1094 | Node *node_to = get_node(NodePath(E.to_node)); |
| 1095 | GraphNode *graph_node_to = Object::cast_to<GraphNode>(node_to); |
| 1096 | if (!graph_node_to) { |
| 1097 | continue; |
| 1098 | } |
| 1099 | |
| 1100 | Vector2 from_port_position = graph_node_from->get_position_offset() * zoom + graph_node_from->get_output_port_position(E.from_port) * zoom; |
| 1101 | Vector2 from_position = minimap->_convert_from_graph_position(from_port_position - graph_offset) + minimap_offset; |
| 1102 | Color from_color = graph_node_from->get_output_port_color(E.from_port); |
| 1103 | Vector2 to_port_position = graph_node_to->get_position_offset() * zoom + graph_node_to->get_input_port_position(E.to_port) * zoom; |
| 1104 | Vector2 to_position = minimap->_convert_from_graph_position(to_port_position - graph_offset) + minimap_offset; |
| 1105 | Color to_color = graph_node_to->get_input_port_color(E.to_port); |
| 1106 | |
| 1107 | if (E.activity > 0) { |
| 1108 | from_color = from_color.lerp(theme_cache.activity_color, E.activity); |
| 1109 | to_color = to_color.lerp(theme_cache.activity_color, E.activity); |
| 1110 | } |
| 1111 | _draw_connection_line(minimap, from_position, to_position, from_color, to_color, 0.5, minimap->_convert_from_graph_position(Vector2(zoom, zoom)).length()); |
| 1112 | } |
| 1113 | |
| 1114 | // Draw the "camera" viewport. |
| 1115 | Rect2 camera_rect = minimap->get_camera_rect(); |
| 1116 | minimap->draw_style_box(minimap->theme_cache.camera_style, camera_rect); |
| 1117 | |
| 1118 | // Draw the resizer control. |
| 1119 | Ref<Texture2D> resizer = minimap->theme_cache.resizer; |
| 1120 | Color resizer_color = minimap->theme_cache.resizer_color; |
| 1121 | minimap->draw_texture(resizer, Point2(), resizer_color); |
| 1122 | } |
| 1123 | |
| 1124 | void GraphEdit::set_selected(Node *p_child) { |
| 1125 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1126 | GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i)); |
| 1127 | if (!graph_node) { |
| 1128 | continue; |
| 1129 | } |
| 1130 | |
| 1131 | graph_node->set_selected(graph_node == p_child); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) { |
| 1136 | ERR_FAIL_COND(p_ev.is_null()); |
| 1137 | if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) { |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | Ref<InputEventMouseMotion> mm = p_ev; |
| 1142 | |
| 1143 | if (mm.is_valid() && dragging) { |
| 1144 | if (!moving_selection) { |
| 1145 | emit_signal(SNAME("begin_node_move" )); |
| 1146 | moving_selection = true; |
| 1147 | } |
| 1148 | |
| 1149 | just_selected = true; |
| 1150 | drag_accum += mm->get_relative(); |
| 1151 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1152 | GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1153 | if (graph_element && graph_element->is_selected() && graph_element->is_draggable()) { |
| 1154 | Vector2 pos = (graph_element->get_drag_from() * zoom + drag_accum) / zoom; |
| 1155 | |
| 1156 | // Snapping can be toggled temporarily by holding down Ctrl. |
| 1157 | // This is done here as to not toggle the grid when holding down Ctrl. |
| 1158 | if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) { |
| 1159 | pos = pos.snapped(Vector2(snapping_distance, snapping_distance)); |
| 1160 | } |
| 1161 | |
| 1162 | graph_element->set_position_offset(pos); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | if (mm.is_valid() && box_selecting) { |
| 1168 | box_selecting_to = mm->get_position(); |
| 1169 | |
| 1170 | box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs()); |
| 1171 | |
| 1172 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1173 | GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1174 | if (!graph_element) { |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | Rect2 r = graph_element->get_rect(); |
| 1179 | bool in_box = r.intersects(box_selecting_rect); |
| 1180 | |
| 1181 | if (in_box) { |
| 1182 | graph_element->set_selected(box_selection_mode_additive); |
| 1183 | } else { |
| 1184 | graph_element->set_selected(prev_selected.find(graph_element) != nullptr); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | top_layer->queue_redraw(); |
| 1189 | minimap->queue_redraw(); |
| 1190 | } |
| 1191 | |
| 1192 | Ref<InputEventMouseButton> mb = p_ev; |
| 1193 | if (mb.is_valid()) { |
| 1194 | if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { |
| 1195 | if (box_selecting) { |
| 1196 | box_selecting = false; |
| 1197 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1198 | GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1199 | if (!graph_element) { |
| 1200 | continue; |
| 1201 | } |
| 1202 | |
| 1203 | graph_element->set_selected(prev_selected.find(graph_element) != nullptr); |
| 1204 | } |
| 1205 | top_layer->queue_redraw(); |
| 1206 | minimap->queue_redraw(); |
| 1207 | } else { |
| 1208 | if (connecting) { |
| 1209 | force_connection_drag_end(); |
| 1210 | } else { |
| 1211 | emit_signal(SNAME("popup_request" ), mb->get_position()); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && dragging) { |
| 1217 | if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CTRL)) { |
| 1218 | // Deselect current node. |
| 1219 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1220 | GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1221 | |
| 1222 | if (graph_element) { |
| 1223 | Rect2 r = graph_element->get_rect(); |
| 1224 | if (r.has_point(mb->get_position())) { |
| 1225 | graph_element->set_selected(false); |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | if (drag_accum != Vector2()) { |
| 1232 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1233 | GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1234 | if (graph_element && graph_element->is_selected()) { |
| 1235 | graph_element->set_drag(false); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | if (moving_selection) { |
| 1241 | emit_signal(SNAME("end_node_move" )); |
| 1242 | moving_selection = false; |
| 1243 | } |
| 1244 | |
| 1245 | dragging = false; |
| 1246 | |
| 1247 | top_layer->queue_redraw(); |
| 1248 | minimap->queue_redraw(); |
| 1249 | queue_redraw(); |
| 1250 | connections_layer->queue_redraw(); |
| 1251 | } |
| 1252 | |
| 1253 | // Node selection logic. |
| 1254 | if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) { |
| 1255 | GraphElement *graph_element = nullptr; |
| 1256 | |
| 1257 | // Find node which was clicked on. |
| 1258 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1259 | GraphElement *selected_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1260 | |
| 1261 | if (!selected_element) { |
| 1262 | continue; |
| 1263 | } |
| 1264 | |
| 1265 | if (selected_element->is_resizing()) { |
| 1266 | continue; |
| 1267 | } |
| 1268 | |
| 1269 | if (selected_element->has_point((mb->get_position() - selected_element->get_position()) / zoom)) { |
| 1270 | graph_element = selected_element; |
| 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | if (graph_element) { |
| 1276 | if (_filter_input(mb->get_position())) { |
| 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | // Left-clicked on a node, select it. |
| 1281 | dragging = true; |
| 1282 | drag_accum = Vector2(); |
| 1283 | just_selected = !graph_element->is_selected(); |
| 1284 | if (!graph_element->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CTRL)) { |
| 1285 | for (int i = 0; i < get_child_count(); i++) { |
| 1286 | GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1287 | if (!child_element) { |
| 1288 | continue; |
| 1289 | } |
| 1290 | |
| 1291 | child_element->set_selected(child_element == graph_element); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | graph_element->set_selected(true); |
| 1296 | for (int i = 0; i < get_child_count(); i++) { |
| 1297 | GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1298 | if (!child_element) { |
| 1299 | continue; |
| 1300 | } |
| 1301 | if (child_element->is_selected()) { |
| 1302 | child_element->set_drag(true); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | } else { |
| 1307 | if (_filter_input(mb->get_position())) { |
| 1308 | return; |
| 1309 | } |
| 1310 | if (panner->is_panning()) { |
| 1311 | return; |
| 1312 | } |
| 1313 | |
| 1314 | // Left-clicked on empty space, start box select. |
| 1315 | box_selecting = true; |
| 1316 | box_selecting_from = mb->get_position(); |
| 1317 | if (mb->is_ctrl_pressed()) { |
| 1318 | box_selection_mode_additive = true; |
| 1319 | prev_selected.clear(); |
| 1320 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1321 | GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1322 | if (!child_element || !child_element->is_selected()) { |
| 1323 | continue; |
| 1324 | } |
| 1325 | |
| 1326 | prev_selected.push_back(child_element); |
| 1327 | } |
| 1328 | } else if (mb->is_shift_pressed()) { |
| 1329 | box_selection_mode_additive = false; |
| 1330 | prev_selected.clear(); |
| 1331 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1332 | GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1333 | if (!child_element || !child_element->is_selected()) { |
| 1334 | continue; |
| 1335 | } |
| 1336 | |
| 1337 | prev_selected.push_back(child_element); |
| 1338 | } |
| 1339 | } else { |
| 1340 | box_selection_mode_additive = true; |
| 1341 | prev_selected.clear(); |
| 1342 | for (int i = get_child_count() - 1; i >= 0; i--) { |
| 1343 | GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i)); |
| 1344 | if (!child_element) { |
| 1345 | continue; |
| 1346 | } |
| 1347 | |
| 1348 | child_element->set_selected(false); |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) { |
| 1355 | // Box selection ended. Nodes were selected during mouse movement. |
| 1356 | box_selecting = false; |
| 1357 | box_selecting_rect = Rect2(); |
| 1358 | prev_selected.clear(); |
| 1359 | top_layer->queue_redraw(); |
| 1360 | minimap->queue_redraw(); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | if (p_ev->is_pressed()) { |
| 1365 | if (p_ev->is_action("ui_graph_duplicate" , true)) { |
| 1366 | emit_signal(SNAME("duplicate_nodes_request" )); |
| 1367 | accept_event(); |
| 1368 | } else if (p_ev->is_action("ui_copy" , true)) { |
| 1369 | emit_signal(SNAME("copy_nodes_request" )); |
| 1370 | accept_event(); |
| 1371 | } else if (p_ev->is_action("ui_paste" , true)) { |
| 1372 | emit_signal(SNAME("paste_nodes_request" )); |
| 1373 | accept_event(); |
| 1374 | } else if (p_ev->is_action("ui_graph_delete" , true)) { |
| 1375 | TypedArray<StringName> nodes; |
| 1376 | |
| 1377 | for (int i = 0; i < get_child_count(); i++) { |
| 1378 | GraphNode *gn = Object::cast_to<GraphNode>(get_child(i)); |
| 1379 | if (!gn) { |
| 1380 | continue; |
| 1381 | } |
| 1382 | if (gn->is_selected()) { |
| 1383 | nodes.push_back(gn->get_name()); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | emit_signal(SNAME("close_nodes_request" ), nodes); |
| 1388 | accept_event(); |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) { |
| 1394 | h_scrollbar->set_value(h_scrollbar->get_value() - p_scroll_vec.x); |
| 1395 | v_scrollbar->set_value(v_scrollbar->get_value() - p_scroll_vec.y); |
| 1396 | } |
| 1397 | |
| 1398 | void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) { |
| 1399 | set_zoom_custom(zoom * p_zoom_factor, p_origin); |
| 1400 | } |
| 1401 | |
| 1402 | void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) { |
| 1403 | for (Connection &E : connections) { |
| 1404 | if (E.from_node == p_from && E.from_port == p_from_port && E.to_node == p_to && E.to_port == p_to_port) { |
| 1405 | if (Math::is_equal_approx(E.activity, p_activity)) { |
| 1406 | // Update only if changed. |
| 1407 | top_layer->queue_redraw(); |
| 1408 | minimap->queue_redraw(); |
| 1409 | connections_layer->queue_redraw(); |
| 1410 | } |
| 1411 | E.activity = p_activity; |
| 1412 | return; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | void GraphEdit::clear_connections() { |
| 1418 | connections.clear(); |
| 1419 | minimap->queue_redraw(); |
| 1420 | queue_redraw(); |
| 1421 | connections_layer->queue_redraw(); |
| 1422 | } |
| 1423 | |
| 1424 | void GraphEdit::force_connection_drag_end() { |
| 1425 | ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!" ); |
| 1426 | connecting = false; |
| 1427 | connecting_valid = false; |
| 1428 | top_layer->queue_redraw(); |
| 1429 | minimap->queue_redraw(); |
| 1430 | queue_redraw(); |
| 1431 | connections_layer->queue_redraw(); |
| 1432 | emit_signal(SNAME("connection_drag_ended" )); |
| 1433 | } |
| 1434 | |
| 1435 | bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) { |
| 1436 | bool valid = true; |
| 1437 | GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid); |
| 1438 | return valid; |
| 1439 | } |
| 1440 | |
| 1441 | void GraphEdit::set_panning_scheme(PanningScheme p_scheme) { |
| 1442 | panning_scheme = p_scheme; |
| 1443 | panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme); |
| 1444 | } |
| 1445 | |
| 1446 | GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const { |
| 1447 | return panning_scheme; |
| 1448 | } |
| 1449 | |
| 1450 | void GraphEdit::set_zoom(float p_zoom) { |
| 1451 | set_zoom_custom(p_zoom, get_size() / 2); |
| 1452 | } |
| 1453 | |
| 1454 | void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) { |
| 1455 | p_zoom = CLAMP(p_zoom, zoom_min, zoom_max); |
| 1456 | if (zoom == p_zoom) { |
| 1457 | return; |
| 1458 | } |
| 1459 | |
| 1460 | Vector2 scrollbar_offset = (Vector2(h_scrollbar->get_value(), v_scrollbar->get_value()) + p_center) / zoom; |
| 1461 | |
| 1462 | zoom = p_zoom; |
| 1463 | top_layer->queue_redraw(); |
| 1464 | |
| 1465 | zoom_minus_button->set_disabled(zoom == zoom_min); |
| 1466 | zoom_plus_button->set_disabled(zoom == zoom_max); |
| 1467 | |
| 1468 | _update_scroll(); |
| 1469 | minimap->queue_redraw(); |
| 1470 | connections_layer->queue_redraw(); |
| 1471 | |
| 1472 | if (is_visible_in_tree()) { |
| 1473 | Vector2 offset = scrollbar_offset * zoom - p_center; |
| 1474 | h_scrollbar->set_value(offset.x); |
| 1475 | v_scrollbar->set_value(offset.y); |
| 1476 | } |
| 1477 | |
| 1478 | _update_zoom_label(); |
| 1479 | queue_redraw(); |
| 1480 | } |
| 1481 | |
| 1482 | float GraphEdit::get_zoom() const { |
| 1483 | return zoom; |
| 1484 | } |
| 1485 | |
| 1486 | void GraphEdit::set_zoom_step(float p_zoom_step) { |
| 1487 | p_zoom_step = abs(p_zoom_step); |
| 1488 | ERR_FAIL_COND(!isfinite(p_zoom_step)); |
| 1489 | if (zoom_step == p_zoom_step) { |
| 1490 | return; |
| 1491 | } |
| 1492 | |
| 1493 | zoom_step = p_zoom_step; |
| 1494 | panner->set_scroll_zoom_factor(zoom_step); |
| 1495 | } |
| 1496 | |
| 1497 | float GraphEdit::get_zoom_step() const { |
| 1498 | return zoom_step; |
| 1499 | } |
| 1500 | |
| 1501 | void GraphEdit::set_zoom_min(float p_zoom_min) { |
| 1502 | ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level." ); |
| 1503 | |
| 1504 | if (zoom_min == p_zoom_min) { |
| 1505 | return; |
| 1506 | } |
| 1507 | |
| 1508 | zoom_min = p_zoom_min; |
| 1509 | set_zoom(zoom); |
| 1510 | } |
| 1511 | |
| 1512 | float GraphEdit::get_zoom_min() const { |
| 1513 | return zoom_min; |
| 1514 | } |
| 1515 | |
| 1516 | void GraphEdit::set_zoom_max(float p_zoom_max) { |
| 1517 | ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level." ); |
| 1518 | |
| 1519 | if (zoom_max == p_zoom_max) { |
| 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | zoom_max = p_zoom_max; |
| 1524 | set_zoom(zoom); |
| 1525 | } |
| 1526 | |
| 1527 | float GraphEdit::get_zoom_max() const { |
| 1528 | return zoom_max; |
| 1529 | } |
| 1530 | |
| 1531 | void GraphEdit::set_show_zoom_label(bool p_enable) { |
| 1532 | if (zoom_label->is_visible() == p_enable) { |
| 1533 | return; |
| 1534 | } |
| 1535 | |
| 1536 | zoom_label->set_visible(p_enable); |
| 1537 | } |
| 1538 | |
| 1539 | bool GraphEdit::is_showing_zoom_label() const { |
| 1540 | return zoom_label->is_visible(); |
| 1541 | } |
| 1542 | |
| 1543 | void GraphEdit::set_right_disconnects(bool p_enable) { |
| 1544 | right_disconnects = p_enable; |
| 1545 | } |
| 1546 | |
| 1547 | bool GraphEdit::is_right_disconnects_enabled() const { |
| 1548 | return right_disconnects; |
| 1549 | } |
| 1550 | |
| 1551 | void GraphEdit::add_valid_right_disconnect_type(int p_type) { |
| 1552 | valid_right_disconnect_types.insert(p_type); |
| 1553 | } |
| 1554 | |
| 1555 | void GraphEdit::remove_valid_right_disconnect_type(int p_type) { |
| 1556 | valid_right_disconnect_types.erase(p_type); |
| 1557 | } |
| 1558 | |
| 1559 | void GraphEdit::add_valid_left_disconnect_type(int p_type) { |
| 1560 | valid_left_disconnect_types.insert(p_type); |
| 1561 | } |
| 1562 | |
| 1563 | void GraphEdit::remove_valid_left_disconnect_type(int p_type) { |
| 1564 | valid_left_disconnect_types.erase(p_type); |
| 1565 | } |
| 1566 | |
| 1567 | TypedArray<Dictionary> GraphEdit::_get_connection_list() const { |
| 1568 | List<Connection> conns; |
| 1569 | get_connection_list(&conns); |
| 1570 | TypedArray<Dictionary> arr; |
| 1571 | for (const Connection &E : conns) { |
| 1572 | Dictionary d; |
| 1573 | d["from_node" ] = E.from_node; |
| 1574 | d["from_port" ] = E.from_port; |
| 1575 | d["to_node" ] = E.to_node; |
| 1576 | d["to_port" ] = E.to_port; |
| 1577 | arr.push_back(d); |
| 1578 | } |
| 1579 | return arr; |
| 1580 | } |
| 1581 | |
| 1582 | void GraphEdit::_zoom_minus() { |
| 1583 | set_zoom(zoom / zoom_step); |
| 1584 | } |
| 1585 | |
| 1586 | void GraphEdit::_zoom_reset() { |
| 1587 | set_zoom(1); |
| 1588 | } |
| 1589 | |
| 1590 | void GraphEdit::_zoom_plus() { |
| 1591 | set_zoom(zoom * zoom_step); |
| 1592 | } |
| 1593 | |
| 1594 | void GraphEdit::_update_zoom_label() { |
| 1595 | int zoom_percent = static_cast<int>(Math::round(zoom * 100)); |
| 1596 | String zoom_text = itos(zoom_percent) + "%" ; |
| 1597 | zoom_label->set_text(zoom_text); |
| 1598 | } |
| 1599 | |
| 1600 | void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) { |
| 1601 | ConnectionType ct(p_type, p_with_type); |
| 1602 | valid_connection_types.insert(ct); |
| 1603 | } |
| 1604 | |
| 1605 | void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) { |
| 1606 | ConnectionType ct(p_type, p_with_type); |
| 1607 | valid_connection_types.erase(ct); |
| 1608 | } |
| 1609 | |
| 1610 | bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const { |
| 1611 | ConnectionType ct(p_type, p_with_type); |
| 1612 | return valid_connection_types.has(ct); |
| 1613 | } |
| 1614 | |
| 1615 | void GraphEdit::set_snapping_enabled(bool p_enable) { |
| 1616 | if (snapping_enabled == p_enable) { |
| 1617 | return; |
| 1618 | } |
| 1619 | |
| 1620 | snapping_enabled = p_enable; |
| 1621 | toggle_snapping_button->set_pressed(p_enable); |
| 1622 | queue_redraw(); |
| 1623 | } |
| 1624 | |
| 1625 | bool GraphEdit::is_snapping_enabled() const { |
| 1626 | return snapping_enabled; |
| 1627 | } |
| 1628 | |
| 1629 | void GraphEdit::set_snapping_distance(int p_snapping_distance) { |
| 1630 | ERR_FAIL_COND_MSG(p_snapping_distance < GRID_MIN_SNAPPING_DISTANCE || p_snapping_distance > GRID_MAX_SNAPPING_DISTANCE, |
| 1631 | vformat("GraphEdit's snapping distance must be between %d and %d (inclusive)" , GRID_MIN_SNAPPING_DISTANCE, GRID_MAX_SNAPPING_DISTANCE)); |
| 1632 | snapping_distance = p_snapping_distance; |
| 1633 | snapping_distance_spinbox->set_value(p_snapping_distance); |
| 1634 | queue_redraw(); |
| 1635 | } |
| 1636 | |
| 1637 | int GraphEdit::get_snapping_distance() const { |
| 1638 | return snapping_distance; |
| 1639 | } |
| 1640 | |
| 1641 | void GraphEdit::set_show_grid(bool p_show) { |
| 1642 | if (show_grid == p_show) { |
| 1643 | return; |
| 1644 | } |
| 1645 | |
| 1646 | show_grid = p_show; |
| 1647 | show_grid_button->set_pressed(p_show); |
| 1648 | queue_redraw(); |
| 1649 | } |
| 1650 | |
| 1651 | bool GraphEdit::is_showing_grid() const { |
| 1652 | return show_grid; |
| 1653 | } |
| 1654 | |
| 1655 | void GraphEdit::_snapping_toggled() { |
| 1656 | snapping_enabled = toggle_snapping_button->is_pressed(); |
| 1657 | } |
| 1658 | |
| 1659 | void GraphEdit::_snapping_distance_changed(double) { |
| 1660 | snapping_distance = snapping_distance_spinbox->get_value(); |
| 1661 | queue_redraw(); |
| 1662 | } |
| 1663 | |
| 1664 | void GraphEdit::_show_grid_toggled() { |
| 1665 | show_grid = show_grid_button->is_pressed(); |
| 1666 | queue_redraw(); |
| 1667 | } |
| 1668 | |
| 1669 | void GraphEdit::set_minimap_size(Vector2 p_size) { |
| 1670 | minimap->set_size(p_size); |
| 1671 | Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size. |
| 1672 | |
| 1673 | minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT); |
| 1674 | minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET); |
| 1675 | minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET); |
| 1676 | minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET); |
| 1677 | minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET); |
| 1678 | minimap->queue_redraw(); |
| 1679 | } |
| 1680 | |
| 1681 | Vector2 GraphEdit::get_minimap_size() const { |
| 1682 | return minimap->get_size(); |
| 1683 | } |
| 1684 | |
| 1685 | void GraphEdit::set_minimap_opacity(float p_opacity) { |
| 1686 | if (minimap->get_modulate().a == p_opacity) { |
| 1687 | return; |
| 1688 | } |
| 1689 | minimap->set_modulate(Color(1, 1, 1, p_opacity)); |
| 1690 | minimap->queue_redraw(); |
| 1691 | } |
| 1692 | |
| 1693 | float GraphEdit::get_minimap_opacity() const { |
| 1694 | Color minimap_modulate = minimap->get_modulate(); |
| 1695 | return minimap_modulate.a; |
| 1696 | } |
| 1697 | |
| 1698 | void GraphEdit::set_minimap_enabled(bool p_enable) { |
| 1699 | if (minimap_button->is_pressed() == p_enable) { |
| 1700 | return; |
| 1701 | } |
| 1702 | minimap_button->set_pressed(p_enable); |
| 1703 | _minimap_toggled(); |
| 1704 | minimap->queue_redraw(); |
| 1705 | } |
| 1706 | |
| 1707 | bool GraphEdit::is_minimap_enabled() const { |
| 1708 | return minimap_button->is_pressed(); |
| 1709 | } |
| 1710 | |
| 1711 | void GraphEdit::set_arrange_nodes_button_hidden(bool p_enable) { |
| 1712 | arrange_nodes_button_hidden = p_enable; |
| 1713 | if (arrange_nodes_button_hidden) { |
| 1714 | layout_button->hide(); |
| 1715 | } else { |
| 1716 | layout_button->show(); |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | bool GraphEdit::is_arrange_nodes_button_hidden() const { |
| 1721 | return arrange_nodes_button_hidden; |
| 1722 | } |
| 1723 | |
| 1724 | void GraphEdit::_minimap_toggled() { |
| 1725 | if (is_minimap_enabled()) { |
| 1726 | minimap->set_visible(true); |
| 1727 | minimap->queue_redraw(); |
| 1728 | } else { |
| 1729 | minimap->set_visible(false); |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | void GraphEdit::set_connection_lines_curvature(float p_curvature) { |
| 1734 | lines_curvature = p_curvature; |
| 1735 | queue_redraw(); |
| 1736 | } |
| 1737 | |
| 1738 | float GraphEdit::get_connection_lines_curvature() const { |
| 1739 | return lines_curvature; |
| 1740 | } |
| 1741 | |
| 1742 | void GraphEdit::set_connection_lines_thickness(float p_thickness) { |
| 1743 | if (lines_thickness == p_thickness) { |
| 1744 | return; |
| 1745 | } |
| 1746 | lines_thickness = p_thickness; |
| 1747 | queue_redraw(); |
| 1748 | } |
| 1749 | |
| 1750 | float GraphEdit::get_connection_lines_thickness() const { |
| 1751 | return lines_thickness; |
| 1752 | } |
| 1753 | |
| 1754 | void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) { |
| 1755 | if (lines_antialiased == p_antialiased) { |
| 1756 | return; |
| 1757 | } |
| 1758 | lines_antialiased = p_antialiased; |
| 1759 | queue_redraw(); |
| 1760 | } |
| 1761 | |
| 1762 | bool GraphEdit::is_connection_lines_antialiased() const { |
| 1763 | return lines_antialiased; |
| 1764 | } |
| 1765 | |
| 1766 | HBoxContainer *GraphEdit::() { |
| 1767 | return menu_hbox; |
| 1768 | } |
| 1769 | |
| 1770 | Ref<ViewPanner> GraphEdit::get_panner() { |
| 1771 | return panner; |
| 1772 | } |
| 1773 | |
| 1774 | void GraphEdit::set_warped_panning(bool p_warped) { |
| 1775 | warped_panning = p_warped; |
| 1776 | } |
| 1777 | |
| 1778 | void GraphEdit::arrange_nodes() { |
| 1779 | arranger->arrange_nodes(); |
| 1780 | } |
| 1781 | |
| 1782 | void GraphEdit::_bind_methods() { |
| 1783 | ClassDB::bind_method(D_METHOD("connect_node" , "from_node" , "from_port" , "to_node" , "to_port" ), &GraphEdit::connect_node); |
| 1784 | ClassDB::bind_method(D_METHOD("is_node_connected" , "from_node" , "from_port" , "to_node" , "to_port" ), &GraphEdit::is_node_connected); |
| 1785 | ClassDB::bind_method(D_METHOD("disconnect_node" , "from_node" , "from_port" , "to_node" , "to_port" ), &GraphEdit::disconnect_node); |
| 1786 | ClassDB::bind_method(D_METHOD("set_connection_activity" , "from_node" , "from_port" , "to_node" , "to_port" , "amount" ), &GraphEdit::set_connection_activity); |
| 1787 | ClassDB::bind_method(D_METHOD("get_connection_list" ), &GraphEdit::_get_connection_list); |
| 1788 | ClassDB::bind_method(D_METHOD("clear_connections" ), &GraphEdit::clear_connections); |
| 1789 | ClassDB::bind_method(D_METHOD("force_connection_drag_end" ), &GraphEdit::force_connection_drag_end); |
| 1790 | ClassDB::bind_method(D_METHOD("get_scroll_offset" ), &GraphEdit::get_scroll_offset); |
| 1791 | ClassDB::bind_method(D_METHOD("set_scroll_offset" , "offset" ), &GraphEdit::set_scroll_offset); |
| 1792 | |
| 1793 | ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type" , "type" ), &GraphEdit::add_valid_right_disconnect_type); |
| 1794 | ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type" , "type" ), &GraphEdit::remove_valid_right_disconnect_type); |
| 1795 | ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type" , "type" ), &GraphEdit::add_valid_left_disconnect_type); |
| 1796 | ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type" , "type" ), &GraphEdit::remove_valid_left_disconnect_type); |
| 1797 | ClassDB::bind_method(D_METHOD("add_valid_connection_type" , "from_type" , "to_type" ), &GraphEdit::add_valid_connection_type); |
| 1798 | ClassDB::bind_method(D_METHOD("remove_valid_connection_type" , "from_type" , "to_type" ), &GraphEdit::remove_valid_connection_type); |
| 1799 | ClassDB::bind_method(D_METHOD("is_valid_connection_type" , "from_type" , "to_type" ), &GraphEdit::is_valid_connection_type); |
| 1800 | ClassDB::bind_method(D_METHOD("get_connection_line" , "from_node" , "to_node" ), &GraphEdit::get_connection_line); |
| 1801 | |
| 1802 | ClassDB::bind_method(D_METHOD("set_panning_scheme" , "scheme" ), &GraphEdit::set_panning_scheme); |
| 1803 | ClassDB::bind_method(D_METHOD("get_panning_scheme" ), &GraphEdit::get_panning_scheme); |
| 1804 | |
| 1805 | ClassDB::bind_method(D_METHOD("set_zoom" , "zoom" ), &GraphEdit::set_zoom); |
| 1806 | ClassDB::bind_method(D_METHOD("get_zoom" ), &GraphEdit::get_zoom); |
| 1807 | |
| 1808 | ClassDB::bind_method(D_METHOD("set_zoom_min" , "zoom_min" ), &GraphEdit::set_zoom_min); |
| 1809 | ClassDB::bind_method(D_METHOD("get_zoom_min" ), &GraphEdit::get_zoom_min); |
| 1810 | |
| 1811 | ClassDB::bind_method(D_METHOD("set_zoom_max" , "zoom_max" ), &GraphEdit::set_zoom_max); |
| 1812 | ClassDB::bind_method(D_METHOD("get_zoom_max" ), &GraphEdit::get_zoom_max); |
| 1813 | |
| 1814 | ClassDB::bind_method(D_METHOD("set_zoom_step" , "zoom_step" ), &GraphEdit::set_zoom_step); |
| 1815 | ClassDB::bind_method(D_METHOD("get_zoom_step" ), &GraphEdit::get_zoom_step); |
| 1816 | |
| 1817 | ClassDB::bind_method(D_METHOD("set_show_zoom_label" , "enable" ), &GraphEdit::set_show_zoom_label); |
| 1818 | ClassDB::bind_method(D_METHOD("is_showing_zoom_label" ), &GraphEdit::is_showing_zoom_label); |
| 1819 | |
| 1820 | ClassDB::bind_method(D_METHOD("set_show_grid" , "enable" ), &GraphEdit::set_show_grid); |
| 1821 | ClassDB::bind_method(D_METHOD("is_showing_grid" ), &GraphEdit::is_showing_grid); |
| 1822 | |
| 1823 | ClassDB::bind_method(D_METHOD("set_snapping_enabled" , "enable" ), &GraphEdit::set_snapping_enabled); |
| 1824 | ClassDB::bind_method(D_METHOD("is_snapping_enabled" ), &GraphEdit::is_snapping_enabled); |
| 1825 | |
| 1826 | ClassDB::bind_method(D_METHOD("set_snapping_distance" , "pixels" ), &GraphEdit::set_snapping_distance); |
| 1827 | ClassDB::bind_method(D_METHOD("get_snapping_distance" ), &GraphEdit::get_snapping_distance); |
| 1828 | |
| 1829 | ClassDB::bind_method(D_METHOD("set_connection_lines_curvature" , "curvature" ), &GraphEdit::set_connection_lines_curvature); |
| 1830 | ClassDB::bind_method(D_METHOD("get_connection_lines_curvature" ), &GraphEdit::get_connection_lines_curvature); |
| 1831 | |
| 1832 | ClassDB::bind_method(D_METHOD("set_connection_lines_thickness" , "pixels" ), &GraphEdit::set_connection_lines_thickness); |
| 1833 | ClassDB::bind_method(D_METHOD("get_connection_lines_thickness" ), &GraphEdit::get_connection_lines_thickness); |
| 1834 | |
| 1835 | ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased" , "pixels" ), &GraphEdit::set_connection_lines_antialiased); |
| 1836 | ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased" ), &GraphEdit::is_connection_lines_antialiased); |
| 1837 | |
| 1838 | ClassDB::bind_method(D_METHOD("set_minimap_size" , "size" ), &GraphEdit::set_minimap_size); |
| 1839 | ClassDB::bind_method(D_METHOD("get_minimap_size" ), &GraphEdit::get_minimap_size); |
| 1840 | ClassDB::bind_method(D_METHOD("set_minimap_opacity" , "opacity" ), &GraphEdit::set_minimap_opacity); |
| 1841 | ClassDB::bind_method(D_METHOD("get_minimap_opacity" ), &GraphEdit::get_minimap_opacity); |
| 1842 | |
| 1843 | ClassDB::bind_method(D_METHOD("set_minimap_enabled" , "enable" ), &GraphEdit::set_minimap_enabled); |
| 1844 | ClassDB::bind_method(D_METHOD("is_minimap_enabled" ), &GraphEdit::is_minimap_enabled); |
| 1845 | |
| 1846 | ClassDB::bind_method(D_METHOD("set_arrange_nodes_button_hidden" , "enable" ), &GraphEdit::set_arrange_nodes_button_hidden); |
| 1847 | ClassDB::bind_method(D_METHOD("is_arrange_nodes_button_hidden" ), &GraphEdit::is_arrange_nodes_button_hidden); |
| 1848 | |
| 1849 | ClassDB::bind_method(D_METHOD("set_right_disconnects" , "enable" ), &GraphEdit::set_right_disconnects); |
| 1850 | ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled" ), &GraphEdit::is_right_disconnects_enabled); |
| 1851 | |
| 1852 | GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node" , "in_port" , "mouse_position" ); |
| 1853 | GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node" , "in_port" , "mouse_position" ); |
| 1854 | |
| 1855 | ClassDB::bind_method(D_METHOD("get_menu_hbox" ), &GraphEdit::get_menu_hbox); |
| 1856 | |
| 1857 | ClassDB::bind_method(D_METHOD("arrange_nodes" ), &GraphEdit::arrange_nodes); |
| 1858 | |
| 1859 | ClassDB::bind_method(D_METHOD("set_selected" , "node" ), &GraphEdit::set_selected); |
| 1860 | |
| 1861 | GDVIRTUAL_BIND(_get_connection_line, "from_position" , "to_position" ) |
| 1862 | GDVIRTUAL_BIND(_is_node_hover_valid, "from_node" , "from_port" , "to_node" , "to_port" ); |
| 1863 | |
| 1864 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects" ), "set_right_disconnects" , "is_right_disconnects_enabled" ); |
| 1865 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset" , PROPERTY_HINT_NONE, "suffix:px" ), "set_scroll_offset" , "get_scroll_offset" ); |
| 1866 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid" ), "set_show_grid" , "is_showing_grid" ); |
| 1867 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snapping_enabled" ), "set_snapping_enabled" , "is_snapping_enabled" ); |
| 1868 | ADD_PROPERTY(PropertyInfo(Variant::INT, "snapping_distance" , PROPERTY_HINT_NONE, "suffix:px" ), "set_snapping_distance" , "get_snapping_distance" ); |
| 1869 | ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme" , PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans" ), "set_panning_scheme" , "get_panning_scheme" ); |
| 1870 | |
| 1871 | ADD_GROUP("Connection Lines" , "connection_lines" ); |
| 1872 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature" ), "set_connection_lines_curvature" , "get_connection_lines_curvature" ); |
| 1873 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness" , PROPERTY_HINT_NONE, "suffix:px" ), "set_connection_lines_thickness" , "get_connection_lines_thickness" ); |
| 1874 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased" ), "set_connection_lines_antialiased" , "is_connection_lines_antialiased" ); |
| 1875 | |
| 1876 | ADD_GROUP("Zoom" , "" ); |
| 1877 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom" ), "set_zoom" , "get_zoom" ); |
| 1878 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min" ), "set_zoom_min" , "get_zoom_min" ); |
| 1879 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max" ), "set_zoom_max" , "get_zoom_max" ); |
| 1880 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step" ), "set_zoom_step" , "get_zoom_step" ); |
| 1881 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label" ), "set_show_zoom_label" , "is_showing_zoom_label" ); |
| 1882 | |
| 1883 | ADD_GROUP("Minimap" , "minimap_" ); |
| 1884 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled" ), "set_minimap_enabled" , "is_minimap_enabled" ); |
| 1885 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size" , PROPERTY_HINT_NONE, "suffix:px" ), "set_minimap_size" , "get_minimap_size" ); |
| 1886 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity" ), "set_minimap_opacity" , "get_minimap_opacity" ); |
| 1887 | |
| 1888 | ADD_GROUP("UI" , "" ); |
| 1889 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arrange_nodes_button_hidden" ), "set_arrange_nodes_button_hidden" , "is_arrange_nodes_button_hidden" ); |
| 1890 | |
| 1891 | ADD_SIGNAL(MethodInfo("connection_request" , PropertyInfo(Variant::STRING_NAME, "from_node" ), PropertyInfo(Variant::INT, "from_port" ), PropertyInfo(Variant::STRING_NAME, "to_node" ), PropertyInfo(Variant::INT, "to_port" ))); |
| 1892 | ADD_SIGNAL(MethodInfo("disconnection_request" , PropertyInfo(Variant::STRING_NAME, "from_node" ), PropertyInfo(Variant::INT, "from_port" ), PropertyInfo(Variant::STRING_NAME, "to_node" ), PropertyInfo(Variant::INT, "to_port" ))); |
| 1893 | ADD_SIGNAL(MethodInfo("popup_request" , PropertyInfo(Variant::VECTOR2, "position" ))); |
| 1894 | ADD_SIGNAL(MethodInfo("duplicate_nodes_request" )); |
| 1895 | ADD_SIGNAL(MethodInfo("copy_nodes_request" )); |
| 1896 | ADD_SIGNAL(MethodInfo("paste_nodes_request" )); |
| 1897 | ADD_SIGNAL(MethodInfo("node_selected" , PropertyInfo(Variant::OBJECT, "node" , PROPERTY_HINT_RESOURCE_TYPE, "Node" ))); |
| 1898 | ADD_SIGNAL(MethodInfo("node_deselected" , PropertyInfo(Variant::OBJECT, "node" , PROPERTY_HINT_RESOURCE_TYPE, "Node" ))); |
| 1899 | ADD_SIGNAL(MethodInfo("connection_to_empty" , PropertyInfo(Variant::STRING_NAME, "from_node" ), PropertyInfo(Variant::INT, "from_port" ), PropertyInfo(Variant::VECTOR2, "release_position" ))); |
| 1900 | ADD_SIGNAL(MethodInfo("connection_from_empty" , PropertyInfo(Variant::STRING_NAME, "to_node" ), PropertyInfo(Variant::INT, "to_port" ), PropertyInfo(Variant::VECTOR2, "release_position" ))); |
| 1901 | ADD_SIGNAL(MethodInfo("close_nodes_request" , PropertyInfo(Variant::ARRAY, "nodes" , PROPERTY_HINT_ARRAY_TYPE, "StringName" ))); |
| 1902 | ADD_SIGNAL(MethodInfo("begin_node_move" )); |
| 1903 | ADD_SIGNAL(MethodInfo("end_node_move" )); |
| 1904 | ADD_SIGNAL(MethodInfo("scroll_offset_changed" , PropertyInfo(Variant::VECTOR2, "offset" ))); |
| 1905 | ADD_SIGNAL(MethodInfo("connection_drag_started" , PropertyInfo(Variant::STRING_NAME, "from_node" ), PropertyInfo(Variant::INT, "from_port" ), PropertyInfo(Variant::BOOL, "is_output" ))); |
| 1906 | ADD_SIGNAL(MethodInfo("connection_drag_ended" )); |
| 1907 | |
| 1908 | BIND_ENUM_CONSTANT(SCROLL_ZOOMS); |
| 1909 | BIND_ENUM_CONSTANT(SCROLL_PANS); |
| 1910 | |
| 1911 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, panel); |
| 1912 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_major); |
| 1913 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_minor); |
| 1914 | |
| 1915 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, GraphEdit, activity_color, "activity" ); |
| 1916 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_fill); |
| 1917 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_stroke); |
| 1918 | |
| 1919 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_in); |
| 1920 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_out); |
| 1921 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_reset); |
| 1922 | |
| 1923 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, snapping_toggle); |
| 1924 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, grid_toggle); |
| 1925 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, minimap_toggle); |
| 1926 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, layout); |
| 1927 | |
| 1928 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_inner_extent); |
| 1929 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_outer_extent); |
| 1930 | } |
| 1931 | |
| 1932 | GraphEdit::GraphEdit() { |
| 1933 | set_focus_mode(FOCUS_ALL); |
| 1934 | |
| 1935 | // Allow dezooming 8 times from the default zoom level. |
| 1936 | // At low zoom levels, text is unreadable due to its small size and poor filtering, |
| 1937 | // but this is still useful for previewing and navigation. |
| 1938 | zoom_min = (1 / Math::pow(zoom_step, 8)); |
| 1939 | // Allow zooming 4 times from the default zoom level. |
| 1940 | zoom_max = (1 * Math::pow(zoom_step, 4)); |
| 1941 | |
| 1942 | panner.instantiate(); |
| 1943 | panner->set_callbacks(callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback)); |
| 1944 | |
| 1945 | top_layer = memnew(GraphEditFilter(this)); |
| 1946 | add_child(top_layer, false, INTERNAL_MODE_BACK); |
| 1947 | top_layer->set_mouse_filter(MOUSE_FILTER_PASS); |
| 1948 | top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 1949 | top_layer->connect("draw" , callable_mp(this, &GraphEdit::_top_layer_draw)); |
| 1950 | top_layer->connect("gui_input" , callable_mp(this, &GraphEdit::_top_layer_input)); |
| 1951 | top_layer->connect("focus_exited" , callable_mp(panner.ptr(), &ViewPanner::release_pan_key)); |
| 1952 | |
| 1953 | connections_layer = memnew(Control); |
| 1954 | add_child(connections_layer, false); |
| 1955 | connections_layer->connect("draw" , callable_mp(this, &GraphEdit::_connections_layer_draw)); |
| 1956 | connections_layer->set_name("_connection_layer" ); |
| 1957 | connections_layer->set_disable_visibility_clip(true); // Necessary, so it can draw freely and be offset. |
| 1958 | connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1959 | |
| 1960 | h_scrollbar = memnew(HScrollBar); |
| 1961 | h_scrollbar->set_name("_h_scroll" ); |
| 1962 | top_layer->add_child(h_scrollbar); |
| 1963 | |
| 1964 | v_scrollbar = memnew(VScrollBar); |
| 1965 | v_scrollbar->set_name("_v_scroll" ); |
| 1966 | top_layer->add_child(v_scrollbar); |
| 1967 | |
| 1968 | // Set large minmax so it can scroll even if not resized yet. |
| 1969 | h_scrollbar->set_min(-10000); |
| 1970 | h_scrollbar->set_max(10000); |
| 1971 | |
| 1972 | v_scrollbar->set_min(-10000); |
| 1973 | v_scrollbar->set_max(10000); |
| 1974 | |
| 1975 | h_scrollbar->connect("value_changed" , callable_mp(this, &GraphEdit::_scroll_moved)); |
| 1976 | v_scrollbar->connect("value_changed" , callable_mp(this, &GraphEdit::_scroll_moved)); |
| 1977 | |
| 1978 | menu_hbox = memnew(HBoxContainer); |
| 1979 | top_layer->add_child(menu_hbox); |
| 1980 | menu_hbox->set_position(Vector2(10, 10)); |
| 1981 | |
| 1982 | zoom_label = memnew(Label); |
| 1983 | menu_hbox->add_child(zoom_label); |
| 1984 | zoom_label->set_visible(false); |
| 1985 | zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 1986 | zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 1987 | zoom_label->set_custom_minimum_size(Size2(48, 0)); |
| 1988 | _update_zoom_label(); |
| 1989 | |
| 1990 | zoom_minus_button = memnew(Button); |
| 1991 | zoom_minus_button->set_flat(true); |
| 1992 | menu_hbox->add_child(zoom_minus_button); |
| 1993 | zoom_minus_button->set_tooltip_text(RTR("Zoom Out" )); |
| 1994 | zoom_minus_button->connect("pressed" , callable_mp(this, &GraphEdit::_zoom_minus)); |
| 1995 | zoom_minus_button->set_focus_mode(FOCUS_NONE); |
| 1996 | |
| 1997 | zoom_reset_button = memnew(Button); |
| 1998 | zoom_reset_button->set_flat(true); |
| 1999 | menu_hbox->add_child(zoom_reset_button); |
| 2000 | zoom_reset_button->set_tooltip_text(RTR("Zoom Reset" )); |
| 2001 | zoom_reset_button->connect("pressed" , callable_mp(this, &GraphEdit::_zoom_reset)); |
| 2002 | zoom_reset_button->set_focus_mode(FOCUS_NONE); |
| 2003 | |
| 2004 | zoom_plus_button = memnew(Button); |
| 2005 | zoom_plus_button->set_flat(true); |
| 2006 | menu_hbox->add_child(zoom_plus_button); |
| 2007 | zoom_plus_button->set_tooltip_text(RTR("Zoom In" )); |
| 2008 | zoom_plus_button->connect("pressed" , callable_mp(this, &GraphEdit::_zoom_plus)); |
| 2009 | zoom_plus_button->set_focus_mode(FOCUS_NONE); |
| 2010 | |
| 2011 | show_grid_button = memnew(Button); |
| 2012 | show_grid_button->set_flat(true); |
| 2013 | show_grid_button->set_toggle_mode(true); |
| 2014 | show_grid_button->set_tooltip_text(RTR("Toggle the visual grid." )); |
| 2015 | show_grid_button->connect("pressed" , callable_mp(this, &GraphEdit::_show_grid_toggled)); |
| 2016 | show_grid_button->set_pressed(true); |
| 2017 | show_grid_button->set_focus_mode(FOCUS_NONE); |
| 2018 | menu_hbox->add_child(show_grid_button); |
| 2019 | |
| 2020 | toggle_snapping_button = memnew(Button); |
| 2021 | toggle_snapping_button->set_flat(true); |
| 2022 | toggle_snapping_button->set_toggle_mode(true); |
| 2023 | toggle_snapping_button->set_tooltip_text(RTR("Toggle snapping to the grid." )); |
| 2024 | toggle_snapping_button->connect("pressed" , callable_mp(this, &GraphEdit::_snapping_toggled)); |
| 2025 | toggle_snapping_button->set_pressed(snapping_enabled); |
| 2026 | toggle_snapping_button->set_focus_mode(FOCUS_NONE); |
| 2027 | menu_hbox->add_child(toggle_snapping_button); |
| 2028 | |
| 2029 | snapping_distance_spinbox = memnew(SpinBox); |
| 2030 | snapping_distance_spinbox->set_min(GRID_MIN_SNAPPING_DISTANCE); |
| 2031 | snapping_distance_spinbox->set_max(GRID_MAX_SNAPPING_DISTANCE); |
| 2032 | snapping_distance_spinbox->set_step(1); |
| 2033 | snapping_distance_spinbox->set_value(snapping_distance); |
| 2034 | snapping_distance_spinbox->set_tooltip_text(RTR("Change the snapping distance." )); |
| 2035 | snapping_distance_spinbox->connect("value_changed" , callable_mp(this, &GraphEdit::_snapping_distance_changed)); |
| 2036 | menu_hbox->add_child(snapping_distance_spinbox); |
| 2037 | |
| 2038 | minimap_button = memnew(Button); |
| 2039 | minimap_button->set_flat(true); |
| 2040 | minimap_button->set_toggle_mode(true); |
| 2041 | minimap_button->set_tooltip_text(RTR("Toggle the graph minimap." )); |
| 2042 | minimap_button->connect("pressed" , callable_mp(this, &GraphEdit::_minimap_toggled)); |
| 2043 | minimap_button->set_pressed(show_grid); |
| 2044 | minimap_button->set_focus_mode(FOCUS_NONE); |
| 2045 | menu_hbox->add_child(minimap_button); |
| 2046 | |
| 2047 | layout_button = memnew(Button); |
| 2048 | layout_button->set_flat(true); |
| 2049 | menu_hbox->add_child(layout_button); |
| 2050 | layout_button->set_tooltip_text(RTR("Automatically arrange selected nodes." )); |
| 2051 | layout_button->connect("pressed" , callable_mp(this, &GraphEdit::arrange_nodes)); |
| 2052 | layout_button->set_focus_mode(FOCUS_NONE); |
| 2053 | |
| 2054 | Vector2 minimap_size = Vector2(240, 160); |
| 2055 | float minimap_opacity = 0.65; |
| 2056 | |
| 2057 | minimap = memnew(GraphEditMinimap(this)); |
| 2058 | top_layer->add_child(minimap); |
| 2059 | minimap->set_name("_minimap" ); |
| 2060 | minimap->set_modulate(Color(1, 1, 1, minimap_opacity)); |
| 2061 | minimap->set_mouse_filter(MOUSE_FILTER_PASS); |
| 2062 | minimap->set_custom_minimum_size(Vector2(50, 50)); |
| 2063 | minimap->set_size(minimap_size); |
| 2064 | minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT); |
| 2065 | minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET); |
| 2066 | minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET); |
| 2067 | minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET); |
| 2068 | minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET); |
| 2069 | minimap->connect("draw" , callable_mp(this, &GraphEdit::_minimap_draw)); |
| 2070 | |
| 2071 | set_clip_contents(true); |
| 2072 | |
| 2073 | arranger = Ref<GraphEditArranger>(memnew(GraphEditArranger(this))); |
| 2074 | } |
| 2075 | |