graph_node.cpp 44 KB

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