graph_node.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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_right) {
  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. for (const KeyValue<int, Slot> &E : slot_table) {
  581. const int slot_index = E.key;
  582. if (slot_index < 0 || slot_index >= slot_y_cache.size()) {
  583. continue;
  584. }
  585. const Slot &slot = E.value;
  586. const int slot_y = slot_y_cache[slot_index];
  587. // Left port.
  588. if (slot.enable_left) {
  589. draw_port(slot_index, Point2i(port_h_offset, slot_y), true, slot.color_left);
  590. }
  591. // Right port.
  592. if (slot.enable_right) {
  593. draw_port(slot_index, Point2i(get_size().x - port_h_offset, slot_y), false, slot.color_right);
  594. }
  595. if (slot_index == selected_slot) {
  596. Ref<Texture2D> port_icon = slot.custom_port_icon_left;
  597. if (port_icon.is_null()) {
  598. port_icon = theme_cache.port;
  599. }
  600. Size2i port_sz = port_icon->get_size() + sb_slot_selected->get_minimum_size();
  601. 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));
  602. port_icon = slot.custom_port_icon_right;
  603. if (port_icon.is_null()) {
  604. port_icon = theme_cache.port;
  605. }
  606. port_sz = port_icon->get_size() + sb_slot_selected->get_minimum_size();
  607. 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));
  608. }
  609. // Draw slot stylebox.
  610. if (slot.draw_stylebox) {
  611. Control *child = Object::cast_to<Control>(get_child(slot_index, false));
  612. if (child && child->is_visible_in_tree()) {
  613. Rect2 child_rect = child->get_rect();
  614. child_rect.position.x = sb_panel->get_margin(SIDE_LEFT);
  615. child_rect.size.width = width;
  616. draw_style_box(sb_slot, child_rect);
  617. }
  618. }
  619. }
  620. }
  621. if (resizable) {
  622. draw_texture(theme_cache.resizer, get_size() - theme_cache.resizer->get_size(), theme_cache.resizer_color);
  623. }
  624. } break;
  625. }
  626. }
  627. 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) {
  628. ERR_FAIL_COND_MSG(p_slot_index < 0, vformat("Cannot set slot with index (%d) lesser than zero.", p_slot_index));
  629. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) &&
  630. !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) &&
  631. p_custom_left.is_null() && p_custom_right.is_null()) {
  632. slot_table.erase(p_slot_index);
  633. return;
  634. }
  635. Slot slot;
  636. slot.enable_left = p_enable_left;
  637. slot.type_left = p_type_left;
  638. slot.color_left = p_color_left;
  639. slot.enable_right = p_enable_right;
  640. slot.type_right = p_type_right;
  641. slot.color_right = p_color_right;
  642. slot.custom_port_icon_left = p_custom_left;
  643. slot.custom_port_icon_right = p_custom_right;
  644. slot.draw_stylebox = p_draw_stylebox;
  645. slot_table[p_slot_index] = slot;
  646. queue_accessibility_update();
  647. queue_redraw();
  648. port_pos_dirty = true;
  649. emit_signal(SNAME("slot_updated"), p_slot_index);
  650. }
  651. void GraphNode::clear_slot(int p_slot_index) {
  652. slot_table.erase(p_slot_index);
  653. queue_accessibility_update();
  654. queue_redraw();
  655. port_pos_dirty = true;
  656. }
  657. void GraphNode::clear_all_slots() {
  658. slot_table.clear();
  659. queue_accessibility_update();
  660. queue_redraw();
  661. port_pos_dirty = true;
  662. }
  663. bool GraphNode::is_slot_enabled_left(int p_slot_index) const {
  664. if (!slot_table.has(p_slot_index)) {
  665. return false;
  666. }
  667. return slot_table[p_slot_index].enable_left;
  668. }
  669. void GraphNode::set_slot_enabled_left(int p_slot_index, bool p_enable) {
  670. 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));
  671. if (slot_table[p_slot_index].enable_left == p_enable) {
  672. return;
  673. }
  674. slot_table[p_slot_index].enable_left = p_enable;
  675. queue_accessibility_update();
  676. queue_redraw();
  677. port_pos_dirty = true;
  678. emit_signal(SNAME("slot_updated"), p_slot_index);
  679. }
  680. void GraphNode::set_slot_type_left(int p_slot_index, int p_type) {
  681. 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));
  682. if (slot_table[p_slot_index].type_left == p_type) {
  683. return;
  684. }
  685. slot_table[p_slot_index].type_left = p_type;
  686. queue_accessibility_update();
  687. queue_redraw();
  688. port_pos_dirty = true;
  689. emit_signal(SNAME("slot_updated"), p_slot_index);
  690. }
  691. int GraphNode::get_slot_type_left(int p_slot_index) const {
  692. if (!slot_table.has(p_slot_index)) {
  693. return 0;
  694. }
  695. return slot_table[p_slot_index].type_left;
  696. }
  697. void GraphNode::set_slot_color_left(int p_slot_index, const Color &p_color) {
  698. 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));
  699. if (slot_table[p_slot_index].color_left == p_color) {
  700. return;
  701. }
  702. slot_table[p_slot_index].color_left = p_color;
  703. queue_redraw();
  704. port_pos_dirty = true;
  705. emit_signal(SNAME("slot_updated"), p_slot_index);
  706. }
  707. Color GraphNode::get_slot_color_left(int p_slot_index) const {
  708. if (!slot_table.has(p_slot_index)) {
  709. return Color(1, 1, 1, 1);
  710. }
  711. return slot_table[p_slot_index].color_left;
  712. }
  713. void GraphNode::set_slot_custom_icon_left(int p_slot_index, const Ref<Texture2D> &p_custom_icon) {
  714. 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));
  715. if (slot_table[p_slot_index].custom_port_icon_left == p_custom_icon) {
  716. return;
  717. }
  718. slot_table[p_slot_index].custom_port_icon_left = p_custom_icon;
  719. queue_redraw();
  720. port_pos_dirty = true;
  721. emit_signal(SNAME("slot_updated"), p_slot_index);
  722. }
  723. Ref<Texture2D> GraphNode::get_slot_custom_icon_left(int p_slot_index) const {
  724. if (!slot_table.has(p_slot_index)) {
  725. return Ref<Texture2D>();
  726. }
  727. return slot_table[p_slot_index].custom_port_icon_left;
  728. }
  729. void GraphNode::set_slot_metadata_left(int p_slot_index, const Variant &p_value) {
  730. 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));
  731. slot_table[p_slot_index].metadata_left = p_value;
  732. }
  733. Variant GraphNode::get_slot_metadata_left(int p_slot_index) const {
  734. const Slot *slot = slot_table.getptr(p_slot_index);
  735. if (slot == nullptr) {
  736. return Variant();
  737. }
  738. return slot->metadata_left;
  739. }
  740. bool GraphNode::is_slot_enabled_right(int p_slot_index) const {
  741. if (!slot_table.has(p_slot_index)) {
  742. return false;
  743. }
  744. return slot_table[p_slot_index].enable_right;
  745. }
  746. void GraphNode::set_slot_enabled_right(int p_slot_index, bool p_enable) {
  747. 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));
  748. if (slot_table[p_slot_index].enable_right == p_enable) {
  749. return;
  750. }
  751. slot_table[p_slot_index].enable_right = p_enable;
  752. queue_accessibility_update();
  753. queue_redraw();
  754. port_pos_dirty = true;
  755. emit_signal(SNAME("slot_updated"), p_slot_index);
  756. }
  757. void GraphNode::set_slot_type_right(int p_slot_index, int p_type) {
  758. 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));
  759. if (slot_table[p_slot_index].type_right == p_type) {
  760. return;
  761. }
  762. slot_table[p_slot_index].type_right = p_type;
  763. queue_accessibility_update();
  764. queue_redraw();
  765. port_pos_dirty = true;
  766. emit_signal(SNAME("slot_updated"), p_slot_index);
  767. }
  768. int GraphNode::get_slot_type_right(int p_slot_index) const {
  769. if (!slot_table.has(p_slot_index)) {
  770. return 0;
  771. }
  772. return slot_table[p_slot_index].type_right;
  773. }
  774. void GraphNode::set_slot_color_right(int p_slot_index, const Color &p_color) {
  775. 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));
  776. if (slot_table[p_slot_index].color_right == p_color) {
  777. return;
  778. }
  779. slot_table[p_slot_index].color_right = p_color;
  780. queue_redraw();
  781. port_pos_dirty = true;
  782. emit_signal(SNAME("slot_updated"), p_slot_index);
  783. }
  784. Color GraphNode::get_slot_color_right(int p_slot_index) const {
  785. if (!slot_table.has(p_slot_index)) {
  786. return Color(1, 1, 1, 1);
  787. }
  788. return slot_table[p_slot_index].color_right;
  789. }
  790. void GraphNode::set_slot_custom_icon_right(int p_slot_index, const Ref<Texture2D> &p_custom_icon) {
  791. 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));
  792. if (slot_table[p_slot_index].custom_port_icon_right == p_custom_icon) {
  793. return;
  794. }
  795. slot_table[p_slot_index].custom_port_icon_right = p_custom_icon;
  796. queue_redraw();
  797. port_pos_dirty = true;
  798. emit_signal(SNAME("slot_updated"), p_slot_index);
  799. }
  800. Ref<Texture2D> GraphNode::get_slot_custom_icon_right(int p_slot_index) const {
  801. if (!slot_table.has(p_slot_index)) {
  802. return Ref<Texture2D>();
  803. }
  804. return slot_table[p_slot_index].custom_port_icon_right;
  805. }
  806. void GraphNode::set_slot_metadata_right(int p_slot_index, const Variant &p_value) {
  807. 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));
  808. slot_table[p_slot_index].metadata_right = p_value;
  809. }
  810. Variant GraphNode::get_slot_metadata_right(int p_slot_index) const {
  811. const Slot *slot = slot_table.getptr(p_slot_index);
  812. if (slot == nullptr) {
  813. return Variant();
  814. }
  815. return slot->metadata_right;
  816. }
  817. bool GraphNode::is_slot_draw_stylebox(int p_slot_index) const {
  818. if (!slot_table.has(p_slot_index)) {
  819. return false;
  820. }
  821. return slot_table[p_slot_index].draw_stylebox;
  822. }
  823. void GraphNode::set_slot_draw_stylebox(int p_slot_index, bool p_enable) {
  824. 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));
  825. slot_table[p_slot_index].draw_stylebox = p_enable;
  826. queue_redraw();
  827. port_pos_dirty = true;
  828. emit_signal(SNAME("slot_updated"), p_slot_index);
  829. }
  830. void GraphNode::set_ignore_invalid_connection_type(bool p_ignore) {
  831. ignore_invalid_connection_type = p_ignore;
  832. }
  833. bool GraphNode::is_ignoring_valid_connection_type() const {
  834. return ignore_invalid_connection_type;
  835. }
  836. Size2 GraphNode::get_minimum_size() const {
  837. Ref<StyleBox> sb_panel = theme_cache.panel;
  838. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  839. Ref<StyleBox> sb_slot = theme_cache.slot;
  840. int separation = theme_cache.separation;
  841. Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
  842. for (int i = 0; i < get_child_count(false); i++) {
  843. Control *child = as_sortable_control(get_child(i, false));
  844. if (!child) {
  845. continue;
  846. }
  847. Size2i size = child->get_combined_minimum_size();
  848. size.width += sb_panel->get_minimum_size().width;
  849. if (slot_table.has(i)) {
  850. size += slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2();
  851. }
  852. minsize.height += size.height;
  853. minsize.width = MAX(minsize.width, size.width);
  854. if (i > 0) {
  855. minsize.height += separation;
  856. }
  857. }
  858. minsize.height += sb_panel->get_minimum_size().height;
  859. return minsize;
  860. }
  861. void GraphNode::_port_pos_update() {
  862. int edgeofs = theme_cache.port_h_offset;
  863. int separation = theme_cache.separation;
  864. // 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.
  865. int vertical_ofs = titlebar_hbox->get_size().height + theme_cache.titlebar->get_minimum_size().height + theme_cache.panel->get_margin(SIDE_TOP);
  866. left_port_cache.clear();
  867. right_port_cache.clear();
  868. slot_count = 0; // Reset the slot count, which is the index of the current slot.
  869. for (int i = 0; i < get_child_count(false); i++) {
  870. Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::VISIBLE_IN_TREE);
  871. if (!child) {
  872. continue;
  873. }
  874. Size2 size = child->get_size();
  875. if (slot_table.has(slot_count)) {
  876. const Slot &slot = slot_table[slot_count];
  877. int port_y;
  878. // Check if it is using resort layout (e.g. Shader Graph nodes slots).
  879. if (slot_y_cache.is_empty()) {
  880. port_y = vertical_ofs + size.height * 0.5; // The y centor is calculated from the widget position.
  881. } else {
  882. port_y = child->get_position().y + size.height * 0.5; // The y centor is calculated from the class object position.
  883. }
  884. if (slot.enable_left) {
  885. PortCache port_cache_left{ Point2i(edgeofs, port_y), slot_count, slot.type_left, slot.color_left };
  886. left_port_cache.push_back(port_cache_left);
  887. }
  888. if (slot.enable_right) {
  889. PortCache port_cache_right{ Point2i(get_size().width - edgeofs, port_y), slot_count, slot.type_right, slot.color_right };
  890. right_port_cache.push_back(port_cache_right);
  891. }
  892. }
  893. vertical_ofs += size.height + separation; // Add the height of the child and the separation to the vertical offset.
  894. slot_count++; // Go to the next slot
  895. }
  896. if (selected_slot >= slot_count) {
  897. selected_slot = -1;
  898. }
  899. port_pos_dirty = false;
  900. }
  901. int GraphNode::get_input_port_count() {
  902. if (port_pos_dirty) {
  903. _port_pos_update();
  904. }
  905. return left_port_cache.size();
  906. }
  907. int GraphNode::get_output_port_count() {
  908. if (port_pos_dirty) {
  909. _port_pos_update();
  910. }
  911. return right_port_cache.size();
  912. }
  913. Vector2 GraphNode::get_input_port_position(int p_port_idx) {
  914. if (port_pos_dirty) {
  915. _port_pos_update();
  916. }
  917. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Vector2());
  918. Vector2 pos = left_port_cache[p_port_idx].pos;
  919. return pos;
  920. }
  921. int GraphNode::get_input_port_type(int p_port_idx) {
  922. if (port_pos_dirty) {
  923. _port_pos_update();
  924. }
  925. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), 0);
  926. return left_port_cache[p_port_idx].type;
  927. }
  928. Color GraphNode::get_input_port_color(int p_port_idx) {
  929. if (port_pos_dirty) {
  930. _port_pos_update();
  931. }
  932. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Color());
  933. return left_port_cache[p_port_idx].color;
  934. }
  935. int GraphNode::get_input_port_slot(int p_port_idx) {
  936. if (port_pos_dirty) {
  937. _port_pos_update();
  938. }
  939. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), -1);
  940. return left_port_cache[p_port_idx].slot_index;
  941. }
  942. Vector2 GraphNode::get_output_port_position(int p_port_idx) {
  943. if (port_pos_dirty) {
  944. _port_pos_update();
  945. }
  946. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Vector2());
  947. Vector2 pos = right_port_cache[p_port_idx].pos;
  948. return pos;
  949. }
  950. int GraphNode::get_output_port_type(int p_port_idx) {
  951. if (port_pos_dirty) {
  952. _port_pos_update();
  953. }
  954. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), 0);
  955. return right_port_cache[p_port_idx].type;
  956. }
  957. Color GraphNode::get_output_port_color(int p_port_idx) {
  958. if (port_pos_dirty) {
  959. _port_pos_update();
  960. }
  961. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Color());
  962. return right_port_cache[p_port_idx].color;
  963. }
  964. int GraphNode::get_output_port_slot(int p_port_idx) {
  965. if (port_pos_dirty) {
  966. _port_pos_update();
  967. }
  968. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), -1);
  969. return right_port_cache[p_port_idx].slot_index;
  970. }
  971. String GraphNode::get_accessibility_container_name(const Node *p_node) const {
  972. int idx = 0;
  973. for (int i = 0; i < get_child_count(false); i++) {
  974. Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE);
  975. if (!child) {
  976. continue;
  977. }
  978. if (child == p_node) {
  979. String name = get_accessibility_name();
  980. if (name.is_empty()) {
  981. name = get_name();
  982. }
  983. return vformat(ETR(", in slot %d of graph node %s (%s)"), idx + 1, name, get_title());
  984. }
  985. idx++;
  986. }
  987. return String();
  988. }
  989. void GraphNode::set_title(const String &p_title) {
  990. if (title == p_title) {
  991. return;
  992. }
  993. title = p_title;
  994. if (title_label) {
  995. title_label->set_text(title);
  996. }
  997. update_minimum_size();
  998. }
  999. String GraphNode::get_title() const {
  1000. return title;
  1001. }
  1002. HBoxContainer *GraphNode::get_titlebar_hbox() {
  1003. return titlebar_hbox;
  1004. }
  1005. Control::CursorShape GraphNode::get_cursor_shape(const Point2 &p_pos) const {
  1006. if (resizable) {
  1007. if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) {
  1008. return CURSOR_FDIAGSIZE;
  1009. }
  1010. }
  1011. return Control::get_cursor_shape(p_pos);
  1012. }
  1013. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  1014. Vector<int> flags;
  1015. flags.append(SIZE_FILL);
  1016. flags.append(SIZE_SHRINK_BEGIN);
  1017. flags.append(SIZE_SHRINK_CENTER);
  1018. flags.append(SIZE_SHRINK_END);
  1019. return flags;
  1020. }
  1021. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  1022. Vector<int> flags;
  1023. flags.append(SIZE_FILL);
  1024. flags.append(SIZE_EXPAND);
  1025. flags.append(SIZE_SHRINK_BEGIN);
  1026. flags.append(SIZE_SHRINK_CENTER);
  1027. flags.append(SIZE_SHRINK_END);
  1028. return flags;
  1029. }
  1030. void GraphNode::set_slots_focus_mode(Control::FocusMode p_focus_mode) {
  1031. if (slots_focus_mode == p_focus_mode) {
  1032. return;
  1033. }
  1034. ERR_FAIL_COND((int)p_focus_mode < 1 || (int)p_focus_mode > 3);
  1035. slots_focus_mode = p_focus_mode;
  1036. if (slots_focus_mode == Control::FOCUS_CLICK && selected_slot > -1) {
  1037. selected_slot = -1;
  1038. queue_redraw();
  1039. }
  1040. }
  1041. Control::FocusMode GraphNode::get_slots_focus_mode() const {
  1042. return slots_focus_mode;
  1043. }
  1044. void GraphNode::_bind_methods() {
  1045. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  1046. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  1047. ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphNode::get_titlebar_hbox);
  1048. 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));
  1049. ClassDB::bind_method(D_METHOD("clear_slot", "slot_index"), &GraphNode::clear_slot);
  1050. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  1051. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "slot_index"), &GraphNode::is_slot_enabled_left);
  1052. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "slot_index", "enable"), &GraphNode::set_slot_enabled_left);
  1053. ClassDB::bind_method(D_METHOD("set_slot_type_left", "slot_index", "type"), &GraphNode::set_slot_type_left);
  1054. ClassDB::bind_method(D_METHOD("get_slot_type_left", "slot_index"), &GraphNode::get_slot_type_left);
  1055. ClassDB::bind_method(D_METHOD("set_slot_color_left", "slot_index", "color"), &GraphNode::set_slot_color_left);
  1056. ClassDB::bind_method(D_METHOD("get_slot_color_left", "slot_index"), &GraphNode::get_slot_color_left);
  1057. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_left", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_left);
  1058. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_left", "slot_index"), &GraphNode::get_slot_custom_icon_left);
  1059. ClassDB::bind_method(D_METHOD("set_slot_metadata_left", "slot_index", "value"), &GraphNode::set_slot_metadata_left);
  1060. ClassDB::bind_method(D_METHOD("get_slot_metadata_left", "slot_index"), &GraphNode::get_slot_metadata_left);
  1061. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "slot_index"), &GraphNode::is_slot_enabled_right);
  1062. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "slot_index", "enable"), &GraphNode::set_slot_enabled_right);
  1063. ClassDB::bind_method(D_METHOD("set_slot_type_right", "slot_index", "type"), &GraphNode::set_slot_type_right);
  1064. ClassDB::bind_method(D_METHOD("get_slot_type_right", "slot_index"), &GraphNode::get_slot_type_right);
  1065. ClassDB::bind_method(D_METHOD("set_slot_color_right", "slot_index", "color"), &GraphNode::set_slot_color_right);
  1066. ClassDB::bind_method(D_METHOD("get_slot_color_right", "slot_index"), &GraphNode::get_slot_color_right);
  1067. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_right", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_right);
  1068. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_right", "slot_index"), &GraphNode::get_slot_custom_icon_right);
  1069. ClassDB::bind_method(D_METHOD("set_slot_metadata_right", "slot_index", "value"), &GraphNode::set_slot_metadata_right);
  1070. ClassDB::bind_method(D_METHOD("get_slot_metadata_right", "slot_index"), &GraphNode::get_slot_metadata_right);
  1071. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "slot_index"), &GraphNode::is_slot_draw_stylebox);
  1072. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "slot_index", "enable"), &GraphNode::set_slot_draw_stylebox);
  1073. ClassDB::bind_method(D_METHOD("set_ignore_invalid_connection_type", "ignore"), &GraphNode::set_ignore_invalid_connection_type);
  1074. ClassDB::bind_method(D_METHOD("is_ignoring_valid_connection_type"), &GraphNode::is_ignoring_valid_connection_type);
  1075. ClassDB::bind_method(D_METHOD("set_slots_focus_mode", "focus_mode"), &GraphNode::set_slots_focus_mode);
  1076. ClassDB::bind_method(D_METHOD("get_slots_focus_mode"), &GraphNode::get_slots_focus_mode);
  1077. ClassDB::bind_method(D_METHOD("get_input_port_count"), &GraphNode::get_input_port_count);
  1078. ClassDB::bind_method(D_METHOD("get_input_port_position", "port_idx"), &GraphNode::get_input_port_position);
  1079. ClassDB::bind_method(D_METHOD("get_input_port_type", "port_idx"), &GraphNode::get_input_port_type);
  1080. ClassDB::bind_method(D_METHOD("get_input_port_color", "port_idx"), &GraphNode::get_input_port_color);
  1081. ClassDB::bind_method(D_METHOD("get_input_port_slot", "port_idx"), &GraphNode::get_input_port_slot);
  1082. ClassDB::bind_method(D_METHOD("get_output_port_count"), &GraphNode::get_output_port_count);
  1083. ClassDB::bind_method(D_METHOD("get_output_port_position", "port_idx"), &GraphNode::get_output_port_position);
  1084. ClassDB::bind_method(D_METHOD("get_output_port_type", "port_idx"), &GraphNode::get_output_port_type);
  1085. ClassDB::bind_method(D_METHOD("get_output_port_color", "port_idx"), &GraphNode::get_output_port_color);
  1086. ClassDB::bind_method(D_METHOD("get_output_port_slot", "port_idx"), &GraphNode::get_output_port_slot);
  1087. GDVIRTUAL_BIND(_draw_port, "slot_index", "position", "left", "color")
  1088. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  1089. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_invalid_connection_type"), "set_ignore_invalid_connection_type", "is_ignoring_valid_connection_type");
  1090. 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");
  1091. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "slot_index")));
  1092. ADD_SIGNAL(MethodInfo("slot_sizes_changed"));
  1093. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel);
  1094. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_selected);
  1095. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_focus);
  1096. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar);
  1097. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar_selected);
  1098. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot);
  1099. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot_selected);
  1100. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, separation);
  1101. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, port_h_offset);
  1102. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, port);
  1103. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, resizer);
  1104. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphNode, resizer_color);
  1105. }
  1106. GraphNode::GraphNode() {
  1107. titlebar_hbox = memnew(HBoxContainer);
  1108. titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1109. add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
  1110. title_label = memnew(Label);
  1111. title_label->set_theme_type_variation("GraphNodeTitleLabel");
  1112. title_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1113. titlebar_hbox->add_child(title_label);
  1114. set_mouse_filter(MOUSE_FILTER_STOP);
  1115. set_focus_mode(FOCUS_ACCESSIBILITY);
  1116. }