| 1 | /**************************************************************************/ |
| 2 | /* graph_node.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_node.h" |
| 32 | |
| 33 | #include "scene/gui/box_container.h" |
| 34 | #include "scene/gui/graph_edit.h" |
| 35 | #include "scene/gui/label.h" |
| 36 | #include "scene/theme/theme_db.h" |
| 37 | |
| 38 | bool GraphNode::_set(const StringName &p_name, const Variant &p_value) { |
| 39 | String str = p_name; |
| 40 | |
| 41 | if (!str.begins_with("slot/")) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | int idx = str.get_slicec('/', 1).to_int(); |
| 46 | String slot_property_name = str.get_slicec('/', 2); |
| 47 | |
| 48 | Slot slot; |
| 49 | if (slot_table.has(idx)) { |
| 50 | slot = slot_table[idx]; |
| 51 | } |
| 52 | |
| 53 | if (slot_property_name == "left_enabled") { |
| 54 | slot.enable_left = p_value; |
| 55 | } else if (slot_property_name == "left_type") { |
| 56 | slot.type_left = p_value; |
| 57 | } else if (slot_property_name == "left_icon") { |
| 58 | slot.custom_port_icon_left = p_value; |
| 59 | } else if (slot_property_name == "left_color") { |
| 60 | slot.color_left = p_value; |
| 61 | } else if (slot_property_name == "right_enabled") { |
| 62 | slot.enable_right = p_value; |
| 63 | } else if (slot_property_name == "right_type") { |
| 64 | slot.type_right = p_value; |
| 65 | } else if (slot_property_name == "right_color") { |
| 66 | slot.color_right = p_value; |
| 67 | } else if (slot_property_name == "right_icon") { |
| 68 | slot.custom_port_icon_right = p_value; |
| 69 | } else if (slot_property_name == "draw_stylebox") { |
| 70 | slot.draw_stylebox = p_value; |
| 71 | } else { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | set_slot(idx, |
| 76 | slot.enable_left, |
| 77 | slot.type_left, |
| 78 | slot.color_left, |
| 79 | slot.enable_right, |
| 80 | slot.type_right, |
| 81 | slot.color_right, |
| 82 | slot.custom_port_icon_left, |
| 83 | slot.custom_port_icon_right, |
| 84 | slot.draw_stylebox); |
| 85 | |
| 86 | queue_redraw(); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const { |
| 91 | String str = p_name; |
| 92 | |
| 93 | if (!str.begins_with("slot/")) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | int idx = str.get_slicec('/', 1).to_int(); |
| 98 | StringName slot_property_name = str.get_slicec('/', 2); |
| 99 | |
| 100 | Slot slot; |
| 101 | if (slot_table.has(idx)) { |
| 102 | slot = slot_table[idx]; |
| 103 | } |
| 104 | |
| 105 | if (slot_property_name == "left_enabled") { |
| 106 | r_ret = slot.enable_left; |
| 107 | } else if (slot_property_name == "left_type") { |
| 108 | r_ret = slot.type_left; |
| 109 | } else if (slot_property_name == "left_color") { |
| 110 | r_ret = slot.color_left; |
| 111 | } else if (slot_property_name == "left_icon") { |
| 112 | r_ret = slot.custom_port_icon_left; |
| 113 | } else if (slot_property_name == "right_enabled") { |
| 114 | r_ret = slot.enable_right; |
| 115 | } else if (slot_property_name == "right_type") { |
| 116 | r_ret = slot.type_right; |
| 117 | } else if (slot_property_name == "right_color") { |
| 118 | r_ret = slot.color_right; |
| 119 | } else if (slot_property_name == "right_icon") { |
| 120 | r_ret = slot.custom_port_icon_right; |
| 121 | } else if (slot_property_name == "draw_stylebox") { |
| 122 | r_ret = slot.draw_stylebox; |
| 123 | } else { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const { |
| 131 | int idx = 0; |
| 132 | for (int i = 0; i < get_child_count(false); i++) { |
| 133 | Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE); |
| 134 | if (!child) { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | String base = "slot/" + itos(idx) + "/"; |
| 139 | |
| 140 | p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled")); |
| 141 | p_list->push_back(PropertyInfo(Variant::INT, base + "left_type")); |
| 142 | p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color")); |
| 143 | p_list->push_back(PropertyInfo(Variant::OBJECT, base + "left_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL)); |
| 144 | p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled")); |
| 145 | p_list->push_back(PropertyInfo(Variant::INT, base + "right_type")); |
| 146 | p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color")); |
| 147 | p_list->push_back(PropertyInfo(Variant::OBJECT, base + "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL)); |
| 148 | p_list->push_back(PropertyInfo(Variant::BOOL, base + "draw_stylebox")); |
| 149 | idx++; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void GraphNode::_resort() { |
| 154 | Size2 new_size = get_size(); |
| 155 | Ref<StyleBox> sb_panel = theme_cache.panel; |
| 156 | Ref<StyleBox> sb_titlebar = theme_cache.titlebar; |
| 157 | |
| 158 | // Resort titlebar first. |
| 159 | Size2 titlebar_size = Size2(new_size.width, titlebar_hbox->get_size().height); |
| 160 | titlebar_size -= sb_titlebar->get_minimum_size(); |
| 161 | Rect2 titlebar_rect = Rect2(sb_titlebar->get_offset(), titlebar_size); |
| 162 | fit_child_in_rect(titlebar_hbox, titlebar_rect); |
| 163 | |
| 164 | // After resort, the children of the titlebar container may have changed their height (e.g. Label autowrap). |
| 165 | Size2i titlebar_min_size = titlebar_hbox->get_combined_minimum_size(); |
| 166 | |
| 167 | // First pass, determine minimum size AND amount of stretchable elements. |
| 168 | Ref<StyleBox> sb_slot = theme_cache.slot; |
| 169 | int separation = theme_cache.separation; |
| 170 | |
| 171 | int children_count = 0; |
| 172 | int stretch_min = 0; |
| 173 | int available_stretch_space = 0; |
| 174 | float stretch_ratio_total = 0; |
| 175 | HashMap<Control *, _MinSizeCache> min_size_cache; |
| 176 | |
| 177 | for (int i = 0; i < get_child_count(false); i++) { |
| 178 | Control *child = as_sortable_control(get_child(i, false)); |
| 179 | if (!child) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | Size2i size = child->get_combined_minimum_size() + (slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2()); |
| 184 | |
| 185 | stretch_min += size.height; |
| 186 | |
| 187 | _MinSizeCache msc; |
| 188 | msc.min_size = size.height; |
| 189 | msc.will_stretch = child->get_v_size_flags().has_flag(SIZE_EXPAND); |
| 190 | msc.final_size = msc.min_size; |
| 191 | min_size_cache[child] = msc; |
| 192 | |
| 193 | if (msc.will_stretch) { |
| 194 | available_stretch_space += msc.min_size; |
| 195 | stretch_ratio_total += child->get_stretch_ratio(); |
| 196 | } |
| 197 | |
| 198 | children_count++; |
| 199 | } |
| 200 | slot_count = children_count; |
| 201 | if (selected_slot >= slot_count) { |
| 202 | selected_slot = -1; |
| 203 | } |
| 204 | |
| 205 | int stretch_max = new_size.height - (children_count - 1) * separation; |
| 206 | int stretch_diff = stretch_max - stretch_min; |
| 207 | |
| 208 | // Avoid negative stretch space. |
| 209 | stretch_diff = MAX(stretch_diff, 0); |
| 210 | |
| 211 | available_stretch_space += stretch_diff - sb_panel->get_margin(SIDE_BOTTOM) - sb_panel->get_margin(SIDE_TOP) - titlebar_min_size.height - sb_titlebar->get_minimum_size().height; |
| 212 | |
| 213 | // Second pass, discard elements that can't be stretched, this will run while stretchable elements exist. |
| 214 | |
| 215 | while (stretch_ratio_total > 0) { |
| 216 | // First of all, don't even be here if no stretchable objects exist. |
| 217 | bool refit_successful = true; |
| 218 | |
| 219 | for (int i = 0; i < get_child_count(false); i++) { |
| 220 | Control *child = as_sortable_control(get_child(i, false)); |
| 221 | if (!child) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | ERR_FAIL_COND(!min_size_cache.has(child)); |
| 226 | _MinSizeCache &msc = min_size_cache[child]; |
| 227 | |
| 228 | if (msc.will_stretch) { |
| 229 | int final_pixel_size = available_stretch_space * child->get_stretch_ratio() / stretch_ratio_total; |
| 230 | if (final_pixel_size < msc.min_size) { |
| 231 | // If the available stretching area is too small for a Control, |
| 232 | // then remove it from stretching area. |
| 233 | msc.will_stretch = false; |
| 234 | stretch_ratio_total -= child->get_stretch_ratio(); |
| 235 | refit_successful = false; |
| 236 | available_stretch_space -= msc.min_size; |
| 237 | msc.final_size = msc.min_size; |
| 238 | break; |
| 239 | } else { |
| 240 | msc.final_size = final_pixel_size; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (refit_successful) { |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Final pass, draw and stretch elements. |
| 251 | |
| 252 | int ofs_y = sb_panel->get_margin(SIDE_TOP) + titlebar_min_size.height + sb_titlebar->get_minimum_size().height; |
| 253 | |
| 254 | slot_y_cache.clear(); |
| 255 | int width = new_size.width - sb_panel->get_minimum_size().width; |
| 256 | int valid_children_idx = 0; |
| 257 | for (int i = 0; i < get_child_count(false); i++) { |
| 258 | Control *child = as_sortable_control(get_child(i, false)); |
| 259 | if (!child) { |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | _MinSizeCache &msc = min_size_cache[child]; |
| 264 | |
| 265 | if (valid_children_idx > 0) { |
| 266 | ofs_y += separation; |
| 267 | } |
| 268 | |
| 269 | int from_y_pos = ofs_y; |
| 270 | int to_y_pos = ofs_y + msc.final_size; |
| 271 | |
| 272 | // Adjust so the last valid child always fits perfect, compensating for numerical imprecision. |
| 273 | if (msc.will_stretch && valid_children_idx == children_count - 1) { |
| 274 | to_y_pos = new_size.height - sb_panel->get_margin(SIDE_BOTTOM); |
| 275 | } |
| 276 | |
| 277 | int height = to_y_pos - from_y_pos; |
| 278 | float margin = sb_panel->get_margin(SIDE_LEFT) + (slot_table[i].draw_stylebox ? sb_slot->get_margin(SIDE_LEFT) : 0); |
| 279 | float final_width = width - (slot_table[i].draw_stylebox ? sb_slot->get_minimum_size().x : 0); |
| 280 | Rect2 rect(margin, from_y_pos, final_width, height); |
| 281 | fit_child_in_rect(child, rect); |
| 282 | |
| 283 | slot_y_cache.push_back(child->get_rect().position.y + child->get_rect().size.height * 0.5); |
| 284 | |
| 285 | ofs_y = to_y_pos; |
| 286 | valid_children_idx++; |
| 287 | } |
| 288 | |
| 289 | queue_accessibility_update(); |
| 290 | queue_redraw(); |
| 291 | port_pos_dirty = true; |
| 292 | emit_signal(SNAME("slot_sizes_changed")); |
| 293 | } |
| 294 | |
| 295 | void GraphNode::draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color) { |
| 296 | if (GDVIRTUAL_CALL(_draw_port, p_slot_index, p_pos, p_left, p_color)) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | Slot slot = slot_table[p_slot_index]; |
| 301 | Ref<Texture2D> port_icon = p_left ? slot.custom_port_icon_left : slot.custom_port_icon_right; |
| 302 | |
| 303 | Point2 icon_offset; |
| 304 | if (port_icon.is_null()) { |
| 305 | port_icon = theme_cache.port; |
| 306 | } |
| 307 | |
| 308 | icon_offset = -port_icon->get_size() * 0.5; |
| 309 | port_icon->draw(get_canvas_item(), p_pos + icon_offset, p_color); |
| 310 | } |
| 311 | |
| 312 | void GraphNode::_accessibility_action_slot(const Variant &p_data) { |
| 313 | CustomAccessibilityAction action = (CustomAccessibilityAction)p_data.operator int(); |
| 314 | switch (action) { |
| 315 | case ACTION_CONNECT_INPUT: { |
| 316 | if (slot_table.has(selected_slot)) { |
| 317 | const Slot &slot = slot_table[selected_slot]; |
| 318 | if (slot.enable_left) { |
| 319 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 320 | if (graph) { |
| 321 | for (int i = 0; i < left_port_cache.size(); i++) { |
| 322 | if (left_port_cache[i].slot_index == selected_slot) { |
| 323 | if (graph->is_keyboard_connecting()) { |
| 324 | graph->end_keyboard_connecting(this, i, -1); |
| 325 | } else { |
| 326 | graph->start_keyboard_connecting(this, i, -1); |
| 327 | } |
| 328 | queue_accessibility_update(); |
| 329 | queue_redraw(); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } break; |
| 337 | |
| 338 | case ACTION_CONNECT_OUTPUT: { |
| 339 | if (slot_table.has(selected_slot)) { |
| 340 | const Slot &slot = slot_table[selected_slot]; |
| 341 | if (slot.enable_right) { |
| 342 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 343 | if (graph) { |
| 344 | for (int i = 0; i < right_port_cache.size(); i++) { |
| 345 | if (right_port_cache[i].slot_index == selected_slot) { |
| 346 | if (graph->is_keyboard_connecting()) { |
| 347 | graph->end_keyboard_connecting(this, -1, i); |
| 348 | } else { |
| 349 | graph->start_keyboard_connecting(this, -1, i); |
| 350 | } |
| 351 | queue_accessibility_update(); |
| 352 | queue_redraw(); |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } break; |
| 360 | |
| 361 | case ACTION_FOLLOW_INPUT: { |
| 362 | if (slot_table.has(selected_slot)) { |
| 363 | const Slot &slot = slot_table[selected_slot]; |
| 364 | if (slot.enable_left) { |
| 365 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 366 | if (graph) { |
| 367 | for (int i = 0; i < left_port_cache.size(); i++) { |
| 368 | if (left_port_cache[i].slot_index == selected_slot) { |
| 369 | GraphNode *target = graph->get_input_connection_target(get_name(), i); |
| 370 | if (target) { |
| 371 | target->grab_focus(); |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } break; |
| 380 | |
| 381 | case ACTION_FOLLOW_OUTPUT: { |
| 382 | if (slot_table.has(selected_slot)) { |
| 383 | const Slot &slot = slot_table[selected_slot]; |
| 384 | if (slot.enable_right) { |
| 385 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 386 | if (graph) { |
| 387 | for (int i = 0; i < right_port_cache.size(); i++) { |
| 388 | if (right_port_cache[i].slot_index == selected_slot) { |
| 389 | GraphNode *target = graph->get_output_connection_target(get_name(), i); |
| 390 | if (target) { |
| 391 | target->grab_focus(); |
| 392 | break; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void GraphNode::gui_input(const Ref<InputEvent> &p_event) { |
| 404 | ERR_FAIL_COND(p_event.is_null()); |
| 405 | if (port_pos_dirty) { |
| 406 | _port_pos_update(); |
| 407 | } |
| 408 | |
| 409 | if (p_event->is_pressed() && slot_count > 0) { |
| 410 | bool ac_enabled = get_tree() && get_tree()->is_accessibility_enabled(); |
| 411 | if ((ac_enabled && slots_focus_mode == Control::FOCUS_ACCESSIBILITY) || slots_focus_mode == Control::FOCUS_ALL) { |
| 412 | if (p_event->is_action("ui_up", true)) { |
| 413 | selected_slot--; |
| 414 | if (selected_slot < 0) { |
| 415 | selected_slot = -1; |
| 416 | } else { |
| 417 | accept_event(); |
| 418 | } |
| 419 | } else if (p_event->is_action("ui_down", true)) { |
| 420 | selected_slot++; |
| 421 | if (selected_slot >= slot_count) { |
| 422 | selected_slot = -1; |
| 423 | } else { |
| 424 | accept_event(); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | if (p_event->is_action("ui_cancel", true)) { |
| 429 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 430 | if (graph && graph->is_keyboard_connecting()) { |
| 431 | graph->force_connection_drag_end(); |
| 432 | accept_event(); |
| 433 | } |
| 434 | } else if (p_event->is_action("ui_graph_delete", true)) { |
| 435 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 436 | if (graph && graph->is_keyboard_connecting()) { |
| 437 | graph->end_keyboard_connecting(this, -1, -1); |
| 438 | accept_event(); |
| 439 | } |
| 440 | } else if (p_event->is_action("ui_graph_follow_left", true)) { |
| 441 | if (slot_table.has(selected_slot)) { |
| 442 | const Slot &slot = slot_table[selected_slot]; |
| 443 | if (slot.enable_left) { |
| 444 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 445 | if (graph) { |
| 446 | for (int i = 0; i < left_port_cache.size(); i++) { |
| 447 | if (left_port_cache[i].slot_index == selected_slot) { |
| 448 | GraphNode *target = graph->get_input_connection_target(get_name(), i); |
| 449 | if (target) { |
| 450 | target->grab_focus(); |
| 451 | accept_event(); |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } else if (p_event->is_action("ui_graph_follow_right", true)) { |
| 460 | if (slot_table.has(selected_slot)) { |
| 461 | const Slot &slot = slot_table[selected_slot]; |
| 462 | if (slot.enable_right) { |
| 463 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 464 | if (graph) { |
| 465 | for (int i = 0; i < right_port_cache.size(); i++) { |
| 466 | if (right_port_cache[i].slot_index == selected_slot) { |
| 467 | GraphNode *target = graph->get_output_connection_target(get_name(), i); |
| 468 | if (target) { |
| 469 | target->grab_focus(); |
| 470 | accept_event(); |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } else if (p_event->is_action("ui_left", true)) { |
| 479 | if (slot_table.has(selected_slot)) { |
| 480 | const Slot &slot = slot_table[selected_slot]; |
| 481 | if (slot.enable_left) { |
| 482 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 483 | if (graph) { |
| 484 | for (int i = 0; i < left_port_cache.size(); i++) { |
| 485 | if (left_port_cache[i].slot_index == selected_slot) { |
| 486 | if (graph->is_keyboard_connecting()) { |
| 487 | graph->end_keyboard_connecting(this, i, -1); |
| 488 | } else { |
| 489 | graph->start_keyboard_connecting(this, i, -1); |
| 490 | } |
| 491 | accept_event(); |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } else if (p_event->is_action("ui_right", true)) { |
| 499 | if (slot_table.has(selected_slot)) { |
| 500 | const Slot &slot = slot_table[selected_slot]; |
| 501 | if (slot.enable_right) { |
| 502 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 503 | if (graph) { |
| 504 | for (int i = 0; i < right_port_cache.size(); i++) { |
| 505 | if (right_port_cache[i].slot_index == selected_slot) { |
| 506 | if (graph->is_keyboard_connecting()) { |
| 507 | graph->end_keyboard_connecting(this, -1, i); |
| 508 | } else { |
| 509 | graph->start_keyboard_connecting(this, -1, i); |
| 510 | } |
| 511 | accept_event(); |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } else if (p_event->is_action("ui_accept", true)) { |
| 519 | if (slot_table.has(selected_slot)) { |
| 520 | int idx = 0; |
| 521 | for (int i = 0; i < get_child_count(false); i++) { |
| 522 | Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE); |
| 523 | if (!child) { |
| 524 | continue; |
| 525 | } |
| 526 | if (idx == selected_slot) { |
| 527 | selected_slot = -1; |
| 528 | child->grab_focus(); |
| 529 | break; |
| 530 | } |
| 531 | idx++; |
| 532 | } |
| 533 | accept_event(); |
| 534 | } |
| 535 | } |
| 536 | queue_accessibility_update(); |
| 537 | queue_redraw(); |
| 538 | } |
| 539 | |
| 540 | GraphElement::gui_input(p_event); |
| 541 | } |
| 542 | |
| 543 | void GraphNode::_notification(int p_what) { |
| 544 | switch (p_what) { |
| 545 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 546 | RID ae = get_accessibility_element(); |
| 547 | ERR_FAIL_COND(ae.is_null()); |
| 548 | |
| 549 | String name = get_accessibility_name(); |
| 550 | if (name.is_empty()) { |
| 551 | name = get_name(); |
| 552 | } |
| 553 | name = vformat(ETR("graph node %s (%s)"), name, get_title()); |
| 554 | |
| 555 | if (slot_table.has(selected_slot)) { |
| 556 | GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent()); |
| 557 | Dictionary type_info; |
| 558 | if (graph) { |
| 559 | type_info = graph->get_type_names(); |
| 560 | } |
| 561 | const Slot &slot = slot_table[selected_slot]; |
| 562 | name += ", " + vformat(ETR("slot %d of %d"), selected_slot + 1, slot_count); |
| 563 | if (slot.enable_left) { |
| 564 | if (type_info.has(slot.type_left)) { |
| 565 | name += "," + vformat(ETR("input port, type: %s"), type_info[slot.type_left]); |
| 566 | } else { |
| 567 | name += "," + vformat(ETR("input port, type: %d"), slot.type_left); |
| 568 | } |
| 569 | if (graph) { |
| 570 | for (int i = 0; i < left_port_cache.size(); i++) { |
| 571 | if (left_port_cache[i].slot_index == selected_slot) { |
| 572 | String cd = graph->get_connections_description(get_name(), i); |
| 573 | if (cd.is_empty()) { |
| 574 | name += " " + ETR("no connections"); |
| 575 | } else { |
| 576 | name += " " + cd; |
| 577 | } |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | if (slot.enable_right) { |
| 584 | if (type_info.has(slot.type_right)) { |
| 585 | name += "," + vformat(ETR("output port, type: %s"), type_info[slot.type_right]); |
| 586 | } else { |
| 587 | name += "," + vformat(ETR("output port, type: %d"), slot.type_right); |
| 588 | } |
| 589 | if (graph) { |
| 590 | for (int i = 0; i < right_port_cache.size(); i++) { |
| 591 | if (right_port_cache[i].slot_index == selected_slot) { |
| 592 | String cd = graph->get_connections_description(get_name(), i); |
| 593 | if (cd.is_empty()) { |
| 594 | name += " " + ETR("no connections"); |
| 595 | } else { |
| 596 | name += " " + cd; |
| 597 | } |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | if (graph && graph->is_keyboard_connecting()) { |
| 604 | name += ", " + ETR("currently selecting target port"); |
| 605 | } |
| 606 | } else { |
| 607 | name += ", " + vformat(ETR("has %d slots"), slot_count); |
| 608 | } |
| 609 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST); |
| 610 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, name); |
| 611 | DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_CONNECT_INPUT, ETR("Edit Input Port Connection")); |
| 612 | DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_CONNECT_OUTPUT, ETR("Edit Output Port Connection")); |
| 613 | DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_FOLLOW_INPUT, ETR("Follow Input Port Connection")); |
| 614 | DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_FOLLOW_OUTPUT, ETR("Follow Output Port Connection")); |
| 615 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CUSTOM, callable_mp(this, &GraphNode::_accessibility_action_slot)); |
| 616 | } break; |
| 617 | case NOTIFICATION_FOCUS_EXIT: { |
| 618 | selected_slot = -1; |
| 619 | queue_redraw(); |
| 620 | } break; |
| 621 | case NOTIFICATION_DRAW: { |
| 622 | // Used for layout calculations. |
| 623 | Ref<StyleBox> sb_panel = theme_cache.panel; |
| 624 | Ref<StyleBox> sb_titlebar = theme_cache.titlebar; |
| 625 | // Used for drawing. |
| 626 | Ref<StyleBox> sb_to_draw_panel = selected ? theme_cache.panel_selected : theme_cache.panel; |
| 627 | Ref<StyleBox> sb_to_draw_titlebar = selected ? theme_cache.titlebar_selected : theme_cache.titlebar; |
| 628 | |
| 629 | Ref<StyleBox> sb_slot = theme_cache.slot; |
| 630 | Ref<StyleBox> sb_slot_selected = theme_cache.slot_selected; |
| 631 | |
| 632 | int port_h_offset = theme_cache.port_h_offset; |
| 633 | |
| 634 | Rect2 titlebar_rect(Point2(), titlebar_hbox->get_size() + sb_titlebar->get_minimum_size()); |
| 635 | Size2 body_size = get_size(); |
| 636 | titlebar_rect.size.width = body_size.width; |
| 637 | body_size.height -= titlebar_rect.size.height; |
| 638 | Rect2 body_rect(0, titlebar_rect.size.height, body_size.width, body_size.height); |
| 639 | |
| 640 | // Draw body (slots area) stylebox. |
| 641 | draw_style_box(sb_to_draw_panel, body_rect); |
| 642 | |
| 643 | // Draw title bar stylebox above. |
| 644 | draw_style_box(sb_to_draw_titlebar, titlebar_rect); |
| 645 | |
| 646 | int width = get_size().width - sb_panel->get_minimum_size().x; |
| 647 | |
| 648 | // Take the HboxContainer child into account. |
| 649 | if (get_child_count(false) > 0) { |
| 650 | for (const KeyValue<int, Slot> &E : slot_table) { |
| 651 | const int slot_index = E.key; |
| 652 | |
| 653 | if (slot_index < 0 || slot_index >= slot_y_cache.size()) { |
| 654 | continue; |
| 655 | } |
| 656 | |
| 657 | const Slot &slot = E.value; |
| 658 | const int slot_y = slot_y_cache[slot_index]; |
| 659 | |
| 660 | // Left port. |
| 661 | if (slot.enable_left) { |
| 662 | draw_port(slot_index, Point2i(port_h_offset, slot_y), true, slot.color_left); |
| 663 | } |
| 664 | |
| 665 | // Right port. |
| 666 | if (slot.enable_right) { |
| 667 | draw_port(slot_index, Point2i(get_size().x - port_h_offset, slot_y), false, slot.color_right); |
| 668 | } |
| 669 | |
| 670 | if (slot_index == selected_slot) { |
| 671 | Ref<Texture2D> port_icon = slot.custom_port_icon_left; |
| 672 | if (port_icon.is_null()) { |
| 673 | port_icon = theme_cache.port; |
| 674 | } |
| 675 | Size2i port_sz = port_icon->get_size() + sb_slot_selected->get_minimum_size(); |
| 676 | draw_style_box(sb_slot_selected, Rect2i(port_h_offset - port_sz.x * 0.5, slot_y - port_sz.y * 0.5, port_sz.x, port_sz.y)); |
| 677 | port_icon = slot.custom_port_icon_right; |
| 678 | if (port_icon.is_null()) { |
| 679 | port_icon = theme_cache.port; |
| 680 | } |
| 681 | port_sz = port_icon->get_size() + sb_slot_selected->get_minimum_size(); |
| 682 | draw_style_box(sb_slot_selected, Rect2i(get_size().x - port_h_offset - port_sz.x * 0.5, slot_y - port_sz.y * 0.5, port_sz.x, port_sz.y)); |
| 683 | } |
| 684 | |
| 685 | // Draw slot stylebox. |
| 686 | if (slot.draw_stylebox) { |
| 687 | Control *child = Object::cast_to<Control>(get_child(slot_index, false)); |
| 688 | if (child && child->is_visible_in_tree()) { |
| 689 | Rect2 child_rect = child->get_rect(); |
| 690 | child_rect.position.x = sb_panel->get_margin(SIDE_LEFT); |
| 691 | child_rect.size.width = width; |
| 692 | draw_style_box(sb_slot, child_rect); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | if (resizable) { |
| 699 | draw_texture(theme_cache.resizer, get_size() - theme_cache.resizer->get_size(), theme_cache.resizer_color); |
| 700 | } |
| 701 | } break; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | void GraphNode::set_slot(int p_slot_index, bool p_enable_left, int p_type_left, const Color &p_color_left, bool p_enable_right, int p_type_right, const Color &p_color_right, const Ref<Texture2D> &p_custom_left, const Ref<Texture2D> &p_custom_right, bool p_draw_stylebox) { |
| 706 | ERR_FAIL_COND_MSG(p_slot_index < 0, vformat("Cannot set slot with index (%d) lesser than zero.", p_slot_index)); |
| 707 | |
| 708 | if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) && |
| 709 | !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) && |
| 710 | p_custom_left.is_null() && p_custom_right.is_null()) { |
| 711 | slot_table.erase(p_slot_index); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | Slot slot; |
| 716 | slot.enable_left = p_enable_left; |
| 717 | slot.type_left = p_type_left; |
| 718 | slot.color_left = p_color_left; |
| 719 | slot.enable_right = p_enable_right; |
| 720 | slot.type_right = p_type_right; |
| 721 | slot.color_right = p_color_right; |
| 722 | slot.custom_port_icon_left = p_custom_left; |
| 723 | slot.custom_port_icon_right = p_custom_right; |
| 724 | slot.draw_stylebox = p_draw_stylebox; |
| 725 | slot_table[p_slot_index] = slot; |
| 726 | |
| 727 | queue_accessibility_update(); |
| 728 | queue_redraw(); |
| 729 | port_pos_dirty = true; |
| 730 | |
| 731 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 732 | } |
| 733 | |
| 734 | void GraphNode::clear_slot(int p_slot_index) { |
| 735 | slot_table.erase(p_slot_index); |
| 736 | |
| 737 | queue_accessibility_update(); |
| 738 | queue_redraw(); |
| 739 | port_pos_dirty = true; |
| 740 | } |
| 741 | |
| 742 | void GraphNode::clear_all_slots() { |
| 743 | slot_table.clear(); |
| 744 | |
| 745 | queue_accessibility_update(); |
| 746 | queue_redraw(); |
| 747 | port_pos_dirty = true; |
| 748 | } |
| 749 | |
| 750 | bool GraphNode::is_slot_enabled_left(int p_slot_index) const { |
| 751 | if (!slot_table.has(p_slot_index)) { |
| 752 | return false; |
| 753 | } |
| 754 | return slot_table[p_slot_index].enable_left; |
| 755 | } |
| 756 | |
| 757 | void GraphNode::set_slot_enabled_left(int p_slot_index, bool p_enable) { |
| 758 | ERR_FAIL_COND_MSG(p_slot_index < 0, vformat("Cannot set enable_left for the slot with index (%d) lesser than zero.", p_slot_index)); |
| 759 | |
| 760 | if (slot_table[p_slot_index].enable_left == p_enable) { |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | slot_table[p_slot_index].enable_left = p_enable; |
| 765 | |
| 766 | queue_accessibility_update(); |
| 767 | queue_redraw(); |
| 768 | port_pos_dirty = true; |
| 769 | |
| 770 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 771 | } |
| 772 | |
| 773 | void GraphNode::set_slot_type_left(int p_slot_index, int p_type) { |
| 774 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set type_left for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 775 | |
| 776 | if (slot_table[p_slot_index].type_left == p_type) { |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | slot_table[p_slot_index].type_left = p_type; |
| 781 | |
| 782 | queue_accessibility_update(); |
| 783 | queue_redraw(); |
| 784 | port_pos_dirty = true; |
| 785 | |
| 786 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 787 | } |
| 788 | |
| 789 | int GraphNode::get_slot_type_left(int p_slot_index) const { |
| 790 | if (!slot_table.has(p_slot_index)) { |
| 791 | return 0; |
| 792 | } |
| 793 | return slot_table[p_slot_index].type_left; |
| 794 | } |
| 795 | |
| 796 | void GraphNode::set_slot_color_left(int p_slot_index, const Color &p_color) { |
| 797 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set color_left for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 798 | |
| 799 | if (slot_table[p_slot_index].color_left == p_color) { |
| 800 | return; |
| 801 | } |
| 802 | |
| 803 | slot_table[p_slot_index].color_left = p_color; |
| 804 | queue_redraw(); |
| 805 | port_pos_dirty = true; |
| 806 | |
| 807 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 808 | } |
| 809 | |
| 810 | Color GraphNode::get_slot_color_left(int p_slot_index) const { |
| 811 | if (!slot_table.has(p_slot_index)) { |
| 812 | return Color(1, 1, 1, 1); |
| 813 | } |
| 814 | return slot_table[p_slot_index].color_left; |
| 815 | } |
| 816 | |
| 817 | void GraphNode::set_slot_custom_icon_left(int p_slot_index, const Ref<Texture2D> &p_custom_icon) { |
| 818 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set custom_port_icon_left for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 819 | |
| 820 | if (slot_table[p_slot_index].custom_port_icon_left == p_custom_icon) { |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | slot_table[p_slot_index].custom_port_icon_left = p_custom_icon; |
| 825 | queue_redraw(); |
| 826 | port_pos_dirty = true; |
| 827 | |
| 828 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 829 | } |
| 830 | |
| 831 | Ref<Texture2D> GraphNode::get_slot_custom_icon_left(int p_slot_index) const { |
| 832 | if (!slot_table.has(p_slot_index)) { |
| 833 | return Ref<Texture2D>(); |
| 834 | } |
| 835 | return slot_table[p_slot_index].custom_port_icon_left; |
| 836 | } |
| 837 | |
| 838 | void GraphNode::set_slot_metadata_left(int p_slot_index, const Variant &p_value) { |
| 839 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set left metadata for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 840 | slot_table[p_slot_index].metadata_left = p_value; |
| 841 | } |
| 842 | |
| 843 | Variant GraphNode::get_slot_metadata_left(int p_slot_index) const { |
| 844 | const Slot *slot = slot_table.getptr(p_slot_index); |
| 845 | if (slot == nullptr) { |
| 846 | return Variant(); |
| 847 | } |
| 848 | return slot->metadata_left; |
| 849 | } |
| 850 | |
| 851 | bool GraphNode::is_slot_enabled_right(int p_slot_index) const { |
| 852 | if (!slot_table.has(p_slot_index)) { |
| 853 | return false; |
| 854 | } |
| 855 | return slot_table[p_slot_index].enable_right; |
| 856 | } |
| 857 | |
| 858 | void GraphNode::set_slot_enabled_right(int p_slot_index, bool p_enable) { |
| 859 | ERR_FAIL_COND_MSG(p_slot_index < 0, vformat("Cannot set enable_right for the slot with index (%d) lesser than zero.", p_slot_index)); |
| 860 | |
| 861 | if (slot_table[p_slot_index].enable_right == p_enable) { |
| 862 | return; |
| 863 | } |
| 864 | |
| 865 | slot_table[p_slot_index].enable_right = p_enable; |
| 866 | |
| 867 | queue_accessibility_update(); |
| 868 | queue_redraw(); |
| 869 | port_pos_dirty = true; |
| 870 | |
| 871 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 872 | } |
| 873 | |
| 874 | void GraphNode::set_slot_type_right(int p_slot_index, int p_type) { |
| 875 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set type_right for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 876 | |
| 877 | if (slot_table[p_slot_index].type_right == p_type) { |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | slot_table[p_slot_index].type_right = p_type; |
| 882 | |
| 883 | queue_accessibility_update(); |
| 884 | queue_redraw(); |
| 885 | port_pos_dirty = true; |
| 886 | |
| 887 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 888 | } |
| 889 | |
| 890 | int GraphNode::get_slot_type_right(int p_slot_index) const { |
| 891 | if (!slot_table.has(p_slot_index)) { |
| 892 | return 0; |
| 893 | } |
| 894 | return slot_table[p_slot_index].type_right; |
| 895 | } |
| 896 | |
| 897 | void GraphNode::set_slot_color_right(int p_slot_index, const Color &p_color) { |
| 898 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set color_right for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 899 | |
| 900 | if (slot_table[p_slot_index].color_right == p_color) { |
| 901 | return; |
| 902 | } |
| 903 | |
| 904 | slot_table[p_slot_index].color_right = p_color; |
| 905 | queue_redraw(); |
| 906 | port_pos_dirty = true; |
| 907 | |
| 908 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 909 | } |
| 910 | |
| 911 | Color GraphNode::get_slot_color_right(int p_slot_index) const { |
| 912 | if (!slot_table.has(p_slot_index)) { |
| 913 | return Color(1, 1, 1, 1); |
| 914 | } |
| 915 | return slot_table[p_slot_index].color_right; |
| 916 | } |
| 917 | |
| 918 | void GraphNode::set_slot_custom_icon_right(int p_slot_index, const Ref<Texture2D> &p_custom_icon) { |
| 919 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set custom_port_icon_right for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 920 | |
| 921 | if (slot_table[p_slot_index].custom_port_icon_right == p_custom_icon) { |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | slot_table[p_slot_index].custom_port_icon_right = p_custom_icon; |
| 926 | queue_redraw(); |
| 927 | port_pos_dirty = true; |
| 928 | |
| 929 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 930 | } |
| 931 | |
| 932 | Ref<Texture2D> GraphNode::get_slot_custom_icon_right(int p_slot_index) const { |
| 933 | if (!slot_table.has(p_slot_index)) { |
| 934 | return Ref<Texture2D>(); |
| 935 | } |
| 936 | return slot_table[p_slot_index].custom_port_icon_right; |
| 937 | } |
| 938 | |
| 939 | void GraphNode::set_slot_metadata_right(int p_slot_index, const Variant &p_value) { |
| 940 | ERR_FAIL_COND_MSG(!slot_table.has(p_slot_index), vformat("Cannot set right metadata for the slot with index '%d' because it hasn't been enabled.", p_slot_index)); |
| 941 | slot_table[p_slot_index].metadata_right = p_value; |
| 942 | } |
| 943 | |
| 944 | Variant GraphNode::get_slot_metadata_right(int p_slot_index) const { |
| 945 | const Slot *slot = slot_table.getptr(p_slot_index); |
| 946 | if (slot == nullptr) { |
| 947 | return Variant(); |
| 948 | } |
| 949 | return slot->metadata_right; |
| 950 | } |
| 951 | |
| 952 | bool GraphNode::is_slot_draw_stylebox(int p_slot_index) const { |
| 953 | if (!slot_table.has(p_slot_index)) { |
| 954 | return false; |
| 955 | } |
| 956 | return slot_table[p_slot_index].draw_stylebox; |
| 957 | } |
| 958 | |
| 959 | void GraphNode::set_slot_draw_stylebox(int p_slot_index, bool p_enable) { |
| 960 | ERR_FAIL_COND_MSG(p_slot_index < 0, vformat("Cannot set draw_stylebox for the slot with p_index (%d) lesser than zero.", p_slot_index)); |
| 961 | |
| 962 | slot_table[p_slot_index].draw_stylebox = p_enable; |
| 963 | queue_redraw(); |
| 964 | port_pos_dirty = true; |
| 965 | |
| 966 | emit_signal(SNAME("slot_updated"), p_slot_index); |
| 967 | } |
| 968 | |
| 969 | void GraphNode::set_ignore_invalid_connection_type(bool p_ignore) { |
| 970 | ignore_invalid_connection_type = p_ignore; |
| 971 | } |
| 972 | |
| 973 | bool GraphNode::is_ignoring_valid_connection_type() const { |
| 974 | return ignore_invalid_connection_type; |
| 975 | } |
| 976 | |
| 977 | Size2 GraphNode::get_minimum_size() const { |
| 978 | Ref<StyleBox> sb_panel = theme_cache.panel; |
| 979 | Ref<StyleBox> sb_titlebar = theme_cache.titlebar; |
| 980 | Ref<StyleBox> sb_slot = theme_cache.slot; |
| 981 | |
| 982 | int separation = theme_cache.separation; |
| 983 | Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size(); |
| 984 | |
| 985 | for (int i = 0; i < get_child_count(false); i++) { |
| 986 | Control *child = as_sortable_control(get_child(i, false)); |
| 987 | if (!child) { |
| 988 | continue; |
| 989 | } |
| 990 | |
| 991 | Size2i size = child->get_combined_minimum_size(); |
| 992 | size.width += sb_panel->get_minimum_size().width; |
| 993 | if (slot_table.has(i)) { |
| 994 | size += slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2(); |
| 995 | } |
| 996 | |
| 997 | minsize.height += size.height; |
| 998 | minsize.width = MAX(minsize.width, size.width); |
| 999 | |
| 1000 | if (i > 0) { |
| 1001 | minsize.height += separation; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | minsize.height += sb_panel->get_minimum_size().height; |
| 1006 | |
| 1007 | return minsize; |
| 1008 | } |
| 1009 | |
| 1010 | void GraphNode::_port_pos_update() { |
| 1011 | int edgeofs = theme_cache.port_h_offset; |
| 1012 | int separation = theme_cache.separation; |
| 1013 | |
| 1014 | // This helps to immediately achieve the initial y "original point" of the slots, which the sum of the titlebar height and the top margin of the panel. |
| 1015 | int vertical_ofs = titlebar_hbox->get_size().height + theme_cache.titlebar->get_minimum_size().height + theme_cache.panel->get_margin(SIDE_TOP); |
| 1016 | |
| 1017 | left_port_cache.clear(); |
| 1018 | right_port_cache.clear(); |
| 1019 | |
| 1020 | slot_count = 0; // Reset the slot count, which is the index of the current slot. |
| 1021 | |
| 1022 | for (int i = 0; i < get_child_count(false); i++) { |
| 1023 | Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::VISIBLE_IN_TREE); |
| 1024 | if (!child) { |
| 1025 | continue; |
| 1026 | } |
| 1027 | |
| 1028 | Size2 size = child->get_size(); |
| 1029 | |
| 1030 | if (slot_table.has(slot_count)) { |
| 1031 | const Slot &slot = slot_table[slot_count]; |
| 1032 | |
| 1033 | int port_y; |
| 1034 | |
| 1035 | // Check if it is using resort layout (e.g. Shader Graph nodes slots). |
| 1036 | if (slot_y_cache.is_empty()) { |
| 1037 | port_y = vertical_ofs + size.height * 0.5; // The y centor is calculated from the widget position. |
| 1038 | } else { |
| 1039 | port_y = child->get_position().y + size.height * 0.5; // The y centor is calculated from the class object position. |
| 1040 | } |
| 1041 | |
| 1042 | if (slot.enable_left) { |
| 1043 | PortCache port_cache_left{ Point2i(edgeofs, port_y), slot_count, slot.type_left, slot.color_left }; |
| 1044 | left_port_cache.push_back(port_cache_left); |
| 1045 | } |
| 1046 | if (slot.enable_right) { |
| 1047 | PortCache port_cache_right{ Point2i(get_size().width - edgeofs, port_y), slot_count, slot.type_right, slot.color_right }; |
| 1048 | right_port_cache.push_back(port_cache_right); |
| 1049 | } |
| 1050 | } |
| 1051 | vertical_ofs += size.height + separation; // Add the height of the child and the separation to the vertical offset. |
| 1052 | slot_count++; // Go to the next slot |
| 1053 | } |
| 1054 | |
| 1055 | if (selected_slot >= slot_count) { |
| 1056 | selected_slot = -1; |
| 1057 | } |
| 1058 | |
| 1059 | port_pos_dirty = false; |
| 1060 | } |
| 1061 | |
| 1062 | int GraphNode::get_input_port_count() { |
| 1063 | if (port_pos_dirty) { |
| 1064 | _port_pos_update(); |
| 1065 | } |
| 1066 | |
| 1067 | return left_port_cache.size(); |
| 1068 | } |
| 1069 | |
| 1070 | int GraphNode::get_output_port_count() { |
| 1071 | if (port_pos_dirty) { |
| 1072 | _port_pos_update(); |
| 1073 | } |
| 1074 | |
| 1075 | return right_port_cache.size(); |
| 1076 | } |
| 1077 | |
| 1078 | Vector2 GraphNode::get_input_port_position(int p_port_idx) { |
| 1079 | if (port_pos_dirty) { |
| 1080 | _port_pos_update(); |
| 1081 | } |
| 1082 | |
| 1083 | ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Vector2()); |
| 1084 | Vector2 pos = left_port_cache[p_port_idx].pos; |
| 1085 | return pos; |
| 1086 | } |
| 1087 | |
| 1088 | int GraphNode::get_input_port_type(int p_port_idx) { |
| 1089 | if (port_pos_dirty) { |
| 1090 | _port_pos_update(); |
| 1091 | } |
| 1092 | |
| 1093 | ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), 0); |
| 1094 | return left_port_cache[p_port_idx].type; |
| 1095 | } |
| 1096 | |
| 1097 | Color GraphNode::get_input_port_color(int p_port_idx) { |
| 1098 | if (port_pos_dirty) { |
| 1099 | _port_pos_update(); |
| 1100 | } |
| 1101 | |
| 1102 | ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Color()); |
| 1103 | return left_port_cache[p_port_idx].color; |
| 1104 | } |
| 1105 | |
| 1106 | int GraphNode::get_input_port_slot(int p_port_idx) { |
| 1107 | if (port_pos_dirty) { |
| 1108 | _port_pos_update(); |
| 1109 | } |
| 1110 | |
| 1111 | ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), -1); |
| 1112 | return left_port_cache[p_port_idx].slot_index; |
| 1113 | } |
| 1114 | |
| 1115 | Vector2 GraphNode::get_output_port_position(int p_port_idx) { |
| 1116 | if (port_pos_dirty) { |
| 1117 | _port_pos_update(); |
| 1118 | } |
| 1119 | |
| 1120 | ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Vector2()); |
| 1121 | Vector2 pos = right_port_cache[p_port_idx].pos; |
| 1122 | return pos; |
| 1123 | } |
| 1124 | |
| 1125 | int GraphNode::get_output_port_type(int p_port_idx) { |
| 1126 | if (port_pos_dirty) { |
| 1127 | _port_pos_update(); |
| 1128 | } |
| 1129 | |
| 1130 | ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), 0); |
| 1131 | return right_port_cache[p_port_idx].type; |
| 1132 | } |
| 1133 | |
| 1134 | Color GraphNode::get_output_port_color(int p_port_idx) { |
| 1135 | if (port_pos_dirty) { |
| 1136 | _port_pos_update(); |
| 1137 | } |
| 1138 | |
| 1139 | ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Color()); |
| 1140 | return right_port_cache[p_port_idx].color; |
| 1141 | } |
| 1142 | |
| 1143 | int GraphNode::get_output_port_slot(int p_port_idx) { |
| 1144 | if (port_pos_dirty) { |
| 1145 | _port_pos_update(); |
| 1146 | } |
| 1147 | |
| 1148 | ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), -1); |
| 1149 | return right_port_cache[p_port_idx].slot_index; |
| 1150 | } |
| 1151 | |
| 1152 | String GraphNode::get_accessibility_container_name(const Node *p_node) const { |
| 1153 | int idx = 0; |
| 1154 | for (int i = 0; i < get_child_count(false); i++) { |
| 1155 | Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE); |
| 1156 | if (!child) { |
| 1157 | continue; |
| 1158 | } |
| 1159 | if (child == p_node) { |
| 1160 | String name = get_accessibility_name(); |
| 1161 | if (name.is_empty()) { |
| 1162 | name = get_name(); |
| 1163 | } |
| 1164 | return vformat(ETR(", in slot %d of graph node %s (%s)"), idx + 1, name, get_title()); |
| 1165 | } |
| 1166 | idx++; |
| 1167 | } |
| 1168 | return String(); |
| 1169 | } |
| 1170 | |
| 1171 | void GraphNode::set_title(const String &p_title) { |
| 1172 | if (title == p_title) { |
| 1173 | return; |
| 1174 | } |
| 1175 | title = p_title; |
| 1176 | if (title_label) { |
| 1177 | title_label->set_text(title); |
| 1178 | } |
| 1179 | update_minimum_size(); |
| 1180 | } |
| 1181 | |
| 1182 | String GraphNode::get_title() const { |
| 1183 | return title; |
| 1184 | } |
| 1185 | |
| 1186 | HBoxContainer *GraphNode::get_titlebar_hbox() { |
| 1187 | return titlebar_hbox; |
| 1188 | } |
| 1189 | |
| 1190 | Control::CursorShape GraphNode::get_cursor_shape(const Point2 &p_pos) const { |
| 1191 | if (resizable) { |
| 1192 | if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) { |
| 1193 | return CURSOR_FDIAGSIZE; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | return Control::get_cursor_shape(p_pos); |
| 1198 | } |
| 1199 | |
| 1200 | Vector<int> GraphNode::get_allowed_size_flags_horizontal() const { |
| 1201 | Vector<int> flags; |
| 1202 | flags.append(SIZE_FILL); |
| 1203 | flags.append(SIZE_SHRINK_BEGIN); |
| 1204 | flags.append(SIZE_SHRINK_CENTER); |
| 1205 | flags.append(SIZE_SHRINK_END); |
| 1206 | return flags; |
| 1207 | } |
| 1208 | |
| 1209 | Vector<int> GraphNode::get_allowed_size_flags_vertical() const { |
| 1210 | Vector<int> flags; |
| 1211 | flags.append(SIZE_FILL); |
| 1212 | flags.append(SIZE_EXPAND); |
| 1213 | flags.append(SIZE_SHRINK_BEGIN); |
| 1214 | flags.append(SIZE_SHRINK_CENTER); |
| 1215 | flags.append(SIZE_SHRINK_END); |
| 1216 | return flags; |
| 1217 | } |
| 1218 | |
| 1219 | void GraphNode::set_slots_focus_mode(Control::FocusMode p_focus_mode) { |
| 1220 | if (slots_focus_mode == p_focus_mode) { |
| 1221 | return; |
| 1222 | } |
| 1223 | ERR_FAIL_COND((int)p_focus_mode < 1 || (int)p_focus_mode > 3); |
| 1224 | |
| 1225 | slots_focus_mode = p_focus_mode; |
| 1226 | if (slots_focus_mode == Control::FOCUS_CLICK && selected_slot > -1) { |
| 1227 | selected_slot = -1; |
| 1228 | queue_redraw(); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | Control::FocusMode GraphNode::get_slots_focus_mode() const { |
| 1233 | return slots_focus_mode; |
| 1234 | } |
| 1235 | |
| 1236 | void GraphNode::_bind_methods() { |
| 1237 | ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title); |
| 1238 | ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title); |
| 1239 | |
| 1240 | ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphNode::get_titlebar_hbox); |
| 1241 | |
| 1242 | ClassDB::bind_method(D_METHOD("set_slot", "slot_index", "enable_left_port", "type_left", "color_left", "enable_right_port", "type_right", "color_right", "custom_icon_left", "custom_icon_right", "draw_stylebox"), &GraphNode::set_slot, DEFVAL(Ref<Texture2D>()), DEFVAL(Ref<Texture2D>()), DEFVAL(true)); |
| 1243 | ClassDB::bind_method(D_METHOD("clear_slot", "slot_index"), &GraphNode::clear_slot); |
| 1244 | ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots); |
| 1245 | |
| 1246 | ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "slot_index"), &GraphNode::is_slot_enabled_left); |
| 1247 | ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "slot_index", "enable"), &GraphNode::set_slot_enabled_left); |
| 1248 | |
| 1249 | ClassDB::bind_method(D_METHOD("set_slot_type_left", "slot_index", "type"), &GraphNode::set_slot_type_left); |
| 1250 | ClassDB::bind_method(D_METHOD("get_slot_type_left", "slot_index"), &GraphNode::get_slot_type_left); |
| 1251 | |
| 1252 | ClassDB::bind_method(D_METHOD("set_slot_color_left", "slot_index", "color"), &GraphNode::set_slot_color_left); |
| 1253 | ClassDB::bind_method(D_METHOD("get_slot_color_left", "slot_index"), &GraphNode::get_slot_color_left); |
| 1254 | |
| 1255 | ClassDB::bind_method(D_METHOD("set_slot_custom_icon_left", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_left); |
| 1256 | ClassDB::bind_method(D_METHOD("get_slot_custom_icon_left", "slot_index"), &GraphNode::get_slot_custom_icon_left); |
| 1257 | |
| 1258 | ClassDB::bind_method(D_METHOD("set_slot_metadata_left", "slot_index", "value"), &GraphNode::set_slot_metadata_left); |
| 1259 | ClassDB::bind_method(D_METHOD("get_slot_metadata_left", "slot_index"), &GraphNode::get_slot_metadata_left); |
| 1260 | |
| 1261 | ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "slot_index"), &GraphNode::is_slot_enabled_right); |
| 1262 | ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "slot_index", "enable"), &GraphNode::set_slot_enabled_right); |
| 1263 | |
| 1264 | ClassDB::bind_method(D_METHOD("set_slot_type_right", "slot_index", "type"), &GraphNode::set_slot_type_right); |
| 1265 | ClassDB::bind_method(D_METHOD("get_slot_type_right", "slot_index"), &GraphNode::get_slot_type_right); |
| 1266 | |
| 1267 | ClassDB::bind_method(D_METHOD("set_slot_color_right", "slot_index", "color"), &GraphNode::set_slot_color_right); |
| 1268 | ClassDB::bind_method(D_METHOD("get_slot_color_right", "slot_index"), &GraphNode::get_slot_color_right); |
| 1269 | |
| 1270 | ClassDB::bind_method(D_METHOD("set_slot_custom_icon_right", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_right); |
| 1271 | ClassDB::bind_method(D_METHOD("get_slot_custom_icon_right", "slot_index"), &GraphNode::get_slot_custom_icon_right); |
| 1272 | |
| 1273 | ClassDB::bind_method(D_METHOD("set_slot_metadata_right", "slot_index", "value"), &GraphNode::set_slot_metadata_right); |
| 1274 | ClassDB::bind_method(D_METHOD("get_slot_metadata_right", "slot_index"), &GraphNode::get_slot_metadata_right); |
| 1275 | |
| 1276 | ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "slot_index"), &GraphNode::is_slot_draw_stylebox); |
| 1277 | ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "slot_index", "enable"), &GraphNode::set_slot_draw_stylebox); |
| 1278 | |
| 1279 | ClassDB::bind_method(D_METHOD("set_ignore_invalid_connection_type", "ignore"), &GraphNode::set_ignore_invalid_connection_type); |
| 1280 | ClassDB::bind_method(D_METHOD("is_ignoring_valid_connection_type"), &GraphNode::is_ignoring_valid_connection_type); |
| 1281 | |
| 1282 | ClassDB::bind_method(D_METHOD("set_slots_focus_mode", "focus_mode"), &GraphNode::set_slots_focus_mode); |
| 1283 | ClassDB::bind_method(D_METHOD("get_slots_focus_mode"), &GraphNode::get_slots_focus_mode); |
| 1284 | |
| 1285 | ClassDB::bind_method(D_METHOD("get_input_port_count"), &GraphNode::get_input_port_count); |
| 1286 | ClassDB::bind_method(D_METHOD("get_input_port_position", "port_idx"), &GraphNode::get_input_port_position); |
| 1287 | ClassDB::bind_method(D_METHOD("get_input_port_type", "port_idx"), &GraphNode::get_input_port_type); |
| 1288 | ClassDB::bind_method(D_METHOD("get_input_port_color", "port_idx"), &GraphNode::get_input_port_color); |
| 1289 | ClassDB::bind_method(D_METHOD("get_input_port_slot", "port_idx"), &GraphNode::get_input_port_slot); |
| 1290 | |
| 1291 | ClassDB::bind_method(D_METHOD("get_output_port_count"), &GraphNode::get_output_port_count); |
| 1292 | ClassDB::bind_method(D_METHOD("get_output_port_position", "port_idx"), &GraphNode::get_output_port_position); |
| 1293 | ClassDB::bind_method(D_METHOD("get_output_port_type", "port_idx"), &GraphNode::get_output_port_type); |
| 1294 | ClassDB::bind_method(D_METHOD("get_output_port_color", "port_idx"), &GraphNode::get_output_port_color); |
| 1295 | ClassDB::bind_method(D_METHOD("get_output_port_slot", "port_idx"), &GraphNode::get_output_port_slot); |
| 1296 | |
| 1297 | GDVIRTUAL_BIND(_draw_port, "slot_index", "position", "left", "color") |
| 1298 | |
| 1299 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title"); |
| 1300 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_invalid_connection_type"), "set_ignore_invalid_connection_type", "is_ignoring_valid_connection_type"); |
| 1301 | ADD_PROPERTY(PropertyInfo(Variant::INT, "slots_focus_mode", PROPERTY_HINT_ENUM, "Click:1,All:2,Accessibility:3"), "set_slots_focus_mode", "get_slots_focus_mode"); |
| 1302 | |
| 1303 | ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "slot_index"))); |
| 1304 | ADD_SIGNAL(MethodInfo("slot_sizes_changed")); |
| 1305 | |
| 1306 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel); |
| 1307 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_selected); |
| 1308 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_focus); |
| 1309 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar); |
| 1310 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar_selected); |
| 1311 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot); |
| 1312 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot_selected); |
| 1313 | |
| 1314 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, separation); |
| 1315 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, port_h_offset); |
| 1316 | |
| 1317 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, port); |
| 1318 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, resizer); |
| 1319 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphNode, resizer_color); |
| 1320 | } |
| 1321 | |
| 1322 | GraphNode::GraphNode() { |
| 1323 | titlebar_hbox = memnew(HBoxContainer); |
| 1324 | titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1325 | add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT); |
| 1326 | |
| 1327 | title_label = memnew(Label); |
| 1328 | title_label->set_theme_type_variation("GraphNodeTitleLabel"); |
| 1329 | title_label->set_h_size_flags(SIZE_EXPAND_FILL); |
| 1330 | titlebar_hbox->add_child(title_label); |
| 1331 | |
| 1332 | set_mouse_filter(MOUSE_FILTER_STOP); |
| 1333 | set_focus_mode(FOCUS_ACCESSIBILITY); |
| 1334 | } |