graph_node.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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. if (p_event->is_action("ui_up", true)) {
  352. selected_slot--;
  353. if (selected_slot < 0) {
  354. selected_slot = -1;
  355. } else {
  356. accept_event();
  357. }
  358. } else if (p_event->is_action("ui_down", true)) {
  359. selected_slot++;
  360. if (selected_slot >= slot_count) {
  361. selected_slot = -1;
  362. } else {
  363. accept_event();
  364. }
  365. } else if (p_event->is_action("ui_cancel", true)) {
  366. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  367. if (graph && graph->is_keyboard_connecting()) {
  368. graph->force_connection_drag_end();
  369. accept_event();
  370. }
  371. } else if (p_event->is_action("ui_graph_delete", true)) {
  372. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  373. if (graph && graph->is_keyboard_connecting()) {
  374. graph->end_keyboard_connecting(this, -1, -1);
  375. accept_event();
  376. }
  377. } else if (p_event->is_action("ui_graph_follow_left", true)) {
  378. if (slot_table.has(selected_slot)) {
  379. const Slot &slot = slot_table[selected_slot];
  380. if (slot.enable_left) {
  381. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  382. if (graph) {
  383. for (int i = 0; i < left_port_cache.size(); i++) {
  384. if (left_port_cache[i].slot_index == selected_slot) {
  385. GraphNode *target = graph->get_input_connection_target(get_name(), i);
  386. if (target) {
  387. target->grab_focus();
  388. accept_event();
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. } else if (p_event->is_action("ui_graph_follow_right", true)) {
  397. if (slot_table.has(selected_slot)) {
  398. const Slot &slot = slot_table[selected_slot];
  399. if (slot.enable_right) {
  400. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  401. if (graph) {
  402. for (int i = 0; i < right_port_cache.size(); i++) {
  403. if (right_port_cache[i].slot_index == selected_slot) {
  404. GraphNode *target = graph->get_output_connection_target(get_name(), i);
  405. if (target) {
  406. target->grab_focus();
  407. accept_event();
  408. break;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. } else if (p_event->is_action("ui_left", true)) {
  416. if (slot_table.has(selected_slot)) {
  417. const Slot &slot = slot_table[selected_slot];
  418. if (slot.enable_left) {
  419. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  420. if (graph) {
  421. for (int i = 0; i < left_port_cache.size(); i++) {
  422. if (left_port_cache[i].slot_index == selected_slot) {
  423. if (graph->is_keyboard_connecting()) {
  424. graph->end_keyboard_connecting(this, i, -1);
  425. } else {
  426. graph->start_keyboard_connecting(this, i, -1);
  427. }
  428. accept_event();
  429. break;
  430. }
  431. }
  432. }
  433. }
  434. }
  435. } else if (p_event->is_action("ui_right", true)) {
  436. if (slot_table.has(selected_slot)) {
  437. const Slot &slot = slot_table[selected_slot];
  438. if (slot.enable_right) {
  439. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  440. if (graph) {
  441. for (int i = 0; i < right_port_cache.size(); i++) {
  442. if (right_port_cache[i].slot_index == selected_slot) {
  443. if (graph->is_keyboard_connecting()) {
  444. graph->end_keyboard_connecting(this, -1, i);
  445. } else {
  446. graph->start_keyboard_connecting(this, -1, i);
  447. }
  448. accept_event();
  449. break;
  450. }
  451. }
  452. }
  453. }
  454. }
  455. } else if (p_event->is_action("ui_accept", true)) {
  456. if (slot_table.has(selected_slot)) {
  457. int idx = 0;
  458. for (int i = 0; i < get_child_count(false); i++) {
  459. Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE);
  460. if (!child) {
  461. continue;
  462. }
  463. if (idx == selected_slot) {
  464. selected_slot = -1;
  465. child->grab_focus();
  466. break;
  467. }
  468. idx++;
  469. }
  470. accept_event();
  471. }
  472. }
  473. queue_accessibility_update();
  474. queue_redraw();
  475. }
  476. GraphElement::gui_input(p_event);
  477. }
  478. void GraphNode::_notification(int p_what) {
  479. switch (p_what) {
  480. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  481. RID ae = get_accessibility_element();
  482. ERR_FAIL_COND(ae.is_null());
  483. String name = get_accessibility_name();
  484. if (name.is_empty()) {
  485. name = get_name();
  486. }
  487. name = vformat(ETR("graph node %s (%s)"), name, get_title());
  488. if (slot_table.has(selected_slot)) {
  489. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  490. Dictionary type_info;
  491. if (graph) {
  492. type_info = graph->get_type_names();
  493. }
  494. const Slot &slot = slot_table[selected_slot];
  495. name += ", " + vformat(ETR("slot %d of %d"), selected_slot + 1, slot_count);
  496. if (slot.enable_left) {
  497. if (type_info.has(slot.type_left)) {
  498. name += "," + vformat(ETR("input port, type: %s"), type_info[slot.type_left]);
  499. } else {
  500. name += "," + vformat(ETR("input port, type: %d"), slot.type_left);
  501. }
  502. if (graph) {
  503. for (int i = 0; i < left_port_cache.size(); i++) {
  504. if (left_port_cache[i].slot_index == selected_slot) {
  505. String cd = graph->get_connections_description(get_name(), i);
  506. if (cd.is_empty()) {
  507. name += " " + ETR("no connections");
  508. } else {
  509. name += " " + cd;
  510. }
  511. break;
  512. }
  513. }
  514. }
  515. }
  516. if (slot.enable_left) {
  517. if (type_info.has(slot.type_right)) {
  518. name += "," + vformat(ETR("output port, type: %s"), type_info[slot.type_right]);
  519. } else {
  520. name += "," + vformat(ETR("output port, type: %d"), slot.type_right);
  521. }
  522. if (graph) {
  523. for (int i = 0; i < right_port_cache.size(); i++) {
  524. if (right_port_cache[i].slot_index == selected_slot) {
  525. String cd = graph->get_connections_description(get_name(), i);
  526. if (cd.is_empty()) {
  527. name += " " + ETR("no connections");
  528. } else {
  529. name += " " + cd;
  530. }
  531. break;
  532. }
  533. }
  534. }
  535. }
  536. if (graph && graph->is_keyboard_connecting()) {
  537. name += ", " + ETR("currently selecting target port");
  538. }
  539. } else {
  540. name += ", " + vformat(ETR("has %d slots"), slot_count);
  541. }
  542. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST);
  543. DisplayServer::get_singleton()->accessibility_update_set_name(ae, name);
  544. DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_CONNECT_INPUT, ETR("Edit Input Port Connection"));
  545. DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_CONNECT_OUTPUT, ETR("Edit Output Port Connection"));
  546. DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_FOLLOW_INPUT, ETR("Follow Input Port Connection"));
  547. DisplayServer::get_singleton()->accessibility_update_add_custom_action(ae, CustomAccessibilityAction::ACTION_FOLLOW_OUTPUT, ETR("Follow Output Port Connection"));
  548. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CUSTOM, callable_mp(this, &GraphNode::_accessibility_action_slot));
  549. } break;
  550. case NOTIFICATION_FOCUS_EXIT: {
  551. selected_slot = -1;
  552. queue_redraw();
  553. } break;
  554. case NOTIFICATION_DRAW: {
  555. // Used for layout calculations.
  556. Ref<StyleBox> sb_panel = theme_cache.panel;
  557. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  558. // Used for drawing.
  559. Ref<StyleBox> sb_to_draw_panel = selected ? theme_cache.panel_selected : theme_cache.panel;
  560. Ref<StyleBox> sb_to_draw_titlebar = selected ? theme_cache.titlebar_selected : theme_cache.titlebar;
  561. Ref<StyleBox> sb_slot = theme_cache.slot;
  562. Ref<StyleBox> sb_slot_selected = theme_cache.slot_selected;
  563. int port_h_offset = theme_cache.port_h_offset;
  564. Rect2 titlebar_rect(Point2(), titlebar_hbox->get_size() + sb_titlebar->get_minimum_size());
  565. Size2 body_size = get_size();
  566. titlebar_rect.size.width = body_size.width;
  567. body_size.height -= titlebar_rect.size.height;
  568. Rect2 body_rect(0, titlebar_rect.size.height, body_size.width, body_size.height);
  569. // Draw body (slots area) stylebox.
  570. draw_style_box(sb_to_draw_panel, body_rect);
  571. // Draw title bar stylebox above.
  572. draw_style_box(sb_to_draw_titlebar, titlebar_rect);
  573. int width = get_size().width - sb_panel->get_minimum_size().x;
  574. // Take the HboxContainer child into account.
  575. if (get_child_count(false) > 0) {
  576. int slot_index = 0;
  577. for (const KeyValue<int, Slot> &E : slot_table) {
  578. if (E.key < 0 || E.key >= slot_y_cache.size()) {
  579. continue;
  580. }
  581. if (!slot_table.has(E.key)) {
  582. continue;
  583. }
  584. const Slot &slot = slot_table[E.key];
  585. // Left port.
  586. if (slot.enable_left) {
  587. draw_port(slot_index, Point2i(port_h_offset, slot_y_cache[E.key]), true, slot.color_left);
  588. }
  589. // Right port.
  590. if (slot.enable_right) {
  591. draw_port(slot_index, Point2i(get_size().x - port_h_offset, slot_y_cache[E.key]), false, slot.color_right);
  592. }
  593. if (slot_index == selected_slot) {
  594. Size2i port_sz = theme_cache.port->get_size();
  595. draw_style_box(sb_slot_selected, Rect2i(port_h_offset - port_sz.x, slot_y_cache[E.key] + sb_panel->get_margin(SIDE_TOP) - port_sz.y, port_sz.x * 2, port_sz.y * 2));
  596. draw_style_box(sb_slot_selected, Rect2i(get_size().x - port_h_offset - port_sz.x, slot_y_cache[E.key] + sb_panel->get_margin(SIDE_TOP) - port_sz.y, port_sz.x * 2, port_sz.y * 2));
  597. }
  598. // Draw slot stylebox.
  599. if (slot.draw_stylebox) {
  600. Control *child = Object::cast_to<Control>(get_child(E.key, false));
  601. if (!child || !child->is_visible_in_tree()) {
  602. continue;
  603. }
  604. Rect2 child_rect = child->get_rect();
  605. child_rect.position.x = sb_panel->get_margin(SIDE_LEFT);
  606. child_rect.size.width = width;
  607. draw_style_box(sb_slot, child_rect);
  608. }
  609. slot_index++;
  610. }
  611. }
  612. if (resizable) {
  613. draw_texture(theme_cache.resizer, get_size() - theme_cache.resizer->get_size(), theme_cache.resizer_color);
  614. }
  615. } break;
  616. }
  617. }
  618. 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) {
  619. ERR_FAIL_COND_MSG(p_slot_index < 0, vformat("Cannot set slot with index (%d) lesser than zero.", p_slot_index));
  620. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) &&
  621. !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) &&
  622. p_custom_left.is_null() && p_custom_right.is_null()) {
  623. slot_table.erase(p_slot_index);
  624. return;
  625. }
  626. Slot slot;
  627. slot.enable_left = p_enable_left;
  628. slot.type_left = p_type_left;
  629. slot.color_left = p_color_left;
  630. slot.enable_right = p_enable_right;
  631. slot.type_right = p_type_right;
  632. slot.color_right = p_color_right;
  633. slot.custom_port_icon_left = p_custom_left;
  634. slot.custom_port_icon_right = p_custom_right;
  635. slot.draw_stylebox = p_draw_stylebox;
  636. slot_table[p_slot_index] = slot;
  637. queue_accessibility_update();
  638. queue_redraw();
  639. port_pos_dirty = true;
  640. emit_signal(SNAME("slot_updated"), p_slot_index);
  641. }
  642. void GraphNode::clear_slot(int p_slot_index) {
  643. slot_table.erase(p_slot_index);
  644. queue_accessibility_update();
  645. queue_redraw();
  646. port_pos_dirty = true;
  647. }
  648. void GraphNode::clear_all_slots() {
  649. slot_table.clear();
  650. queue_accessibility_update();
  651. queue_redraw();
  652. port_pos_dirty = true;
  653. }
  654. bool GraphNode::is_slot_enabled_left(int p_slot_index) const {
  655. if (!slot_table.has(p_slot_index)) {
  656. return false;
  657. }
  658. return slot_table[p_slot_index].enable_left;
  659. }
  660. void GraphNode::set_slot_enabled_left(int p_slot_index, bool p_enable) {
  661. 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));
  662. if (slot_table[p_slot_index].enable_left == p_enable) {
  663. return;
  664. }
  665. slot_table[p_slot_index].enable_left = p_enable;
  666. queue_accessibility_update();
  667. queue_redraw();
  668. port_pos_dirty = true;
  669. emit_signal(SNAME("slot_updated"), p_slot_index);
  670. }
  671. void GraphNode::set_slot_type_left(int p_slot_index, int p_type) {
  672. 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));
  673. if (slot_table[p_slot_index].type_left == p_type) {
  674. return;
  675. }
  676. slot_table[p_slot_index].type_left = p_type;
  677. queue_accessibility_update();
  678. queue_redraw();
  679. port_pos_dirty = true;
  680. emit_signal(SNAME("slot_updated"), p_slot_index);
  681. }
  682. int GraphNode::get_slot_type_left(int p_slot_index) const {
  683. if (!slot_table.has(p_slot_index)) {
  684. return 0;
  685. }
  686. return slot_table[p_slot_index].type_left;
  687. }
  688. void GraphNode::set_slot_color_left(int p_slot_index, const Color &p_color) {
  689. 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));
  690. if (slot_table[p_slot_index].color_left == p_color) {
  691. return;
  692. }
  693. slot_table[p_slot_index].color_left = p_color;
  694. queue_redraw();
  695. port_pos_dirty = true;
  696. emit_signal(SNAME("slot_updated"), p_slot_index);
  697. }
  698. Color GraphNode::get_slot_color_left(int p_slot_index) const {
  699. if (!slot_table.has(p_slot_index)) {
  700. return Color(1, 1, 1, 1);
  701. }
  702. return slot_table[p_slot_index].color_left;
  703. }
  704. void GraphNode::set_slot_custom_icon_left(int p_slot_index, const Ref<Texture2D> &p_custom_icon) {
  705. 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));
  706. if (slot_table[p_slot_index].custom_port_icon_left == p_custom_icon) {
  707. return;
  708. }
  709. slot_table[p_slot_index].custom_port_icon_left = p_custom_icon;
  710. queue_redraw();
  711. port_pos_dirty = true;
  712. emit_signal(SNAME("slot_updated"), p_slot_index);
  713. }
  714. Ref<Texture2D> GraphNode::get_slot_custom_icon_left(int p_slot_index) const {
  715. if (!slot_table.has(p_slot_index)) {
  716. return Ref<Texture2D>();
  717. }
  718. return slot_table[p_slot_index].custom_port_icon_left;
  719. }
  720. bool GraphNode::is_slot_enabled_right(int p_slot_index) const {
  721. if (!slot_table.has(p_slot_index)) {
  722. return false;
  723. }
  724. return slot_table[p_slot_index].enable_right;
  725. }
  726. void GraphNode::set_slot_enabled_right(int p_slot_index, bool p_enable) {
  727. 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));
  728. if (slot_table[p_slot_index].enable_right == p_enable) {
  729. return;
  730. }
  731. slot_table[p_slot_index].enable_right = p_enable;
  732. queue_accessibility_update();
  733. queue_redraw();
  734. port_pos_dirty = true;
  735. emit_signal(SNAME("slot_updated"), p_slot_index);
  736. }
  737. void GraphNode::set_slot_type_right(int p_slot_index, int p_type) {
  738. 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));
  739. if (slot_table[p_slot_index].type_right == p_type) {
  740. return;
  741. }
  742. slot_table[p_slot_index].type_right = p_type;
  743. queue_accessibility_update();
  744. queue_redraw();
  745. port_pos_dirty = true;
  746. emit_signal(SNAME("slot_updated"), p_slot_index);
  747. }
  748. int GraphNode::get_slot_type_right(int p_slot_index) const {
  749. if (!slot_table.has(p_slot_index)) {
  750. return 0;
  751. }
  752. return slot_table[p_slot_index].type_right;
  753. }
  754. void GraphNode::set_slot_color_right(int p_slot_index, const Color &p_color) {
  755. 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));
  756. if (slot_table[p_slot_index].color_right == p_color) {
  757. return;
  758. }
  759. slot_table[p_slot_index].color_right = p_color;
  760. queue_redraw();
  761. port_pos_dirty = true;
  762. emit_signal(SNAME("slot_updated"), p_slot_index);
  763. }
  764. Color GraphNode::get_slot_color_right(int p_slot_index) const {
  765. if (!slot_table.has(p_slot_index)) {
  766. return Color(1, 1, 1, 1);
  767. }
  768. return slot_table[p_slot_index].color_right;
  769. }
  770. void GraphNode::set_slot_custom_icon_right(int p_slot_index, const Ref<Texture2D> &p_custom_icon) {
  771. 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));
  772. if (slot_table[p_slot_index].custom_port_icon_right == p_custom_icon) {
  773. return;
  774. }
  775. slot_table[p_slot_index].custom_port_icon_right = p_custom_icon;
  776. queue_redraw();
  777. port_pos_dirty = true;
  778. emit_signal(SNAME("slot_updated"), p_slot_index);
  779. }
  780. Ref<Texture2D> GraphNode::get_slot_custom_icon_right(int p_slot_index) const {
  781. if (!slot_table.has(p_slot_index)) {
  782. return Ref<Texture2D>();
  783. }
  784. return slot_table[p_slot_index].custom_port_icon_right;
  785. }
  786. bool GraphNode::is_slot_draw_stylebox(int p_slot_index) const {
  787. if (!slot_table.has(p_slot_index)) {
  788. return false;
  789. }
  790. return slot_table[p_slot_index].draw_stylebox;
  791. }
  792. void GraphNode::set_slot_draw_stylebox(int p_slot_index, bool p_enable) {
  793. 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));
  794. slot_table[p_slot_index].draw_stylebox = p_enable;
  795. queue_redraw();
  796. port_pos_dirty = true;
  797. emit_signal(SNAME("slot_updated"), p_slot_index);
  798. }
  799. void GraphNode::set_ignore_invalid_connection_type(bool p_ignore) {
  800. ignore_invalid_connection_type = p_ignore;
  801. }
  802. bool GraphNode::is_ignoring_valid_connection_type() const {
  803. return ignore_invalid_connection_type;
  804. }
  805. Size2 GraphNode::get_minimum_size() const {
  806. Ref<StyleBox> sb_panel = theme_cache.panel;
  807. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  808. Ref<StyleBox> sb_slot = theme_cache.slot;
  809. int separation = theme_cache.separation;
  810. Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
  811. for (int i = 0; i < get_child_count(false); i++) {
  812. Control *child = as_sortable_control(get_child(i, false));
  813. if (!child) {
  814. continue;
  815. }
  816. Size2i size = child->get_combined_minimum_size();
  817. size.width += sb_panel->get_minimum_size().width;
  818. if (slot_table.has(i)) {
  819. size += slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2();
  820. }
  821. minsize.height += size.height;
  822. minsize.width = MAX(minsize.width, size.width);
  823. if (i > 0) {
  824. minsize.height += separation;
  825. }
  826. }
  827. minsize.height += sb_panel->get_minimum_size().height;
  828. return minsize;
  829. }
  830. void GraphNode::_port_pos_update() {
  831. int edgeofs = theme_cache.port_h_offset;
  832. int separation = theme_cache.separation;
  833. // 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.
  834. int vertical_ofs = titlebar_hbox->get_size().height + theme_cache.titlebar->get_minimum_size().height + theme_cache.panel->get_margin(SIDE_TOP);
  835. left_port_cache.clear();
  836. right_port_cache.clear();
  837. slot_count = 0; // Reset the slot count, which is the index of the current slot.
  838. for (int i = 0; i < get_child_count(false); i++) {
  839. Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::VISIBLE_IN_TREE);
  840. if (!child) {
  841. continue;
  842. }
  843. Size2 size = child->get_size();
  844. if (slot_table.has(slot_count)) {
  845. const Slot &slot = slot_table[slot_count];
  846. int port_y;
  847. // Check if it is using resort layout (e.g. Shader Graph nodes slots).
  848. if (slot_y_cache.is_empty()) {
  849. port_y = vertical_ofs + size.height * 0.5; // The y centor is calculated from the widget position.
  850. } else {
  851. port_y = child->get_position().y + size.height * 0.5; // The y centor is calculated from the class object position.
  852. }
  853. if (slot.enable_left) {
  854. PortCache port_cache_left{ Point2i(edgeofs, port_y), slot_count, slot.type_left, slot.color_left };
  855. left_port_cache.push_back(port_cache_left);
  856. }
  857. if (slot.enable_right) {
  858. PortCache port_cache_right{ Point2i(get_size().width - edgeofs, port_y), slot_count, slot.type_right, slot.color_right };
  859. right_port_cache.push_back(port_cache_right);
  860. }
  861. }
  862. vertical_ofs += size.height + separation; // Add the height of the child and the separation to the vertical offset.
  863. slot_count++; // Go to the next slot
  864. }
  865. if (selected_slot >= slot_count) {
  866. selected_slot = -1;
  867. }
  868. port_pos_dirty = false;
  869. }
  870. int GraphNode::get_input_port_count() {
  871. if (port_pos_dirty) {
  872. _port_pos_update();
  873. }
  874. return left_port_cache.size();
  875. }
  876. int GraphNode::get_output_port_count() {
  877. if (port_pos_dirty) {
  878. _port_pos_update();
  879. }
  880. return right_port_cache.size();
  881. }
  882. Vector2 GraphNode::get_input_port_position(int p_port_idx) {
  883. if (port_pos_dirty) {
  884. _port_pos_update();
  885. }
  886. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Vector2());
  887. Vector2 pos = left_port_cache[p_port_idx].pos;
  888. return pos;
  889. }
  890. int GraphNode::get_input_port_type(int p_port_idx) {
  891. if (port_pos_dirty) {
  892. _port_pos_update();
  893. }
  894. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), 0);
  895. return left_port_cache[p_port_idx].type;
  896. }
  897. Color GraphNode::get_input_port_color(int p_port_idx) {
  898. if (port_pos_dirty) {
  899. _port_pos_update();
  900. }
  901. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Color());
  902. return left_port_cache[p_port_idx].color;
  903. }
  904. int GraphNode::get_input_port_slot(int p_port_idx) {
  905. if (port_pos_dirty) {
  906. _port_pos_update();
  907. }
  908. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), -1);
  909. return left_port_cache[p_port_idx].slot_index;
  910. }
  911. Vector2 GraphNode::get_output_port_position(int p_port_idx) {
  912. if (port_pos_dirty) {
  913. _port_pos_update();
  914. }
  915. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Vector2());
  916. Vector2 pos = right_port_cache[p_port_idx].pos;
  917. return pos;
  918. }
  919. int GraphNode::get_output_port_type(int p_port_idx) {
  920. if (port_pos_dirty) {
  921. _port_pos_update();
  922. }
  923. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), 0);
  924. return right_port_cache[p_port_idx].type;
  925. }
  926. Color GraphNode::get_output_port_color(int p_port_idx) {
  927. if (port_pos_dirty) {
  928. _port_pos_update();
  929. }
  930. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Color());
  931. return right_port_cache[p_port_idx].color;
  932. }
  933. int GraphNode::get_output_port_slot(int p_port_idx) {
  934. if (port_pos_dirty) {
  935. _port_pos_update();
  936. }
  937. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), -1);
  938. return right_port_cache[p_port_idx].slot_index;
  939. }
  940. String GraphNode::get_accessibility_container_name(const Node *p_node) const {
  941. int idx = 0;
  942. for (int i = 0; i < get_child_count(false); i++) {
  943. Control *child = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE);
  944. if (!child) {
  945. continue;
  946. }
  947. if (child == p_node) {
  948. String name = get_accessibility_name();
  949. if (name.is_empty()) {
  950. name = get_name();
  951. }
  952. return vformat(ETR(", in slot %d of graph node %s (%s)"), idx + 1, name, get_title());
  953. }
  954. idx++;
  955. }
  956. return String();
  957. }
  958. void GraphNode::set_title(const String &p_title) {
  959. if (title == p_title) {
  960. return;
  961. }
  962. title = p_title;
  963. if (title_label) {
  964. title_label->set_text(title);
  965. }
  966. update_minimum_size();
  967. }
  968. String GraphNode::get_title() const {
  969. return title;
  970. }
  971. HBoxContainer *GraphNode::get_titlebar_hbox() {
  972. return titlebar_hbox;
  973. }
  974. Control::CursorShape GraphNode::get_cursor_shape(const Point2 &p_pos) const {
  975. if (resizable) {
  976. if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) {
  977. return CURSOR_FDIAGSIZE;
  978. }
  979. }
  980. return Control::get_cursor_shape(p_pos);
  981. }
  982. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  983. Vector<int> flags;
  984. flags.append(SIZE_FILL);
  985. flags.append(SIZE_SHRINK_BEGIN);
  986. flags.append(SIZE_SHRINK_CENTER);
  987. flags.append(SIZE_SHRINK_END);
  988. return flags;
  989. }
  990. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  991. Vector<int> flags;
  992. flags.append(SIZE_FILL);
  993. flags.append(SIZE_EXPAND);
  994. flags.append(SIZE_SHRINK_BEGIN);
  995. flags.append(SIZE_SHRINK_CENTER);
  996. flags.append(SIZE_SHRINK_END);
  997. return flags;
  998. }
  999. void GraphNode::_bind_methods() {
  1000. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  1001. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  1002. ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphNode::get_titlebar_hbox);
  1003. 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));
  1004. ClassDB::bind_method(D_METHOD("clear_slot", "slot_index"), &GraphNode::clear_slot);
  1005. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  1006. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "slot_index"), &GraphNode::is_slot_enabled_left);
  1007. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "slot_index", "enable"), &GraphNode::set_slot_enabled_left);
  1008. ClassDB::bind_method(D_METHOD("set_slot_type_left", "slot_index", "type"), &GraphNode::set_slot_type_left);
  1009. ClassDB::bind_method(D_METHOD("get_slot_type_left", "slot_index"), &GraphNode::get_slot_type_left);
  1010. ClassDB::bind_method(D_METHOD("set_slot_color_left", "slot_index", "color"), &GraphNode::set_slot_color_left);
  1011. ClassDB::bind_method(D_METHOD("get_slot_color_left", "slot_index"), &GraphNode::get_slot_color_left);
  1012. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_left", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_left);
  1013. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_left", "slot_index"), &GraphNode::get_slot_custom_icon_left);
  1014. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "slot_index"), &GraphNode::is_slot_enabled_right);
  1015. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "slot_index", "enable"), &GraphNode::set_slot_enabled_right);
  1016. ClassDB::bind_method(D_METHOD("set_slot_type_right", "slot_index", "type"), &GraphNode::set_slot_type_right);
  1017. ClassDB::bind_method(D_METHOD("get_slot_type_right", "slot_index"), &GraphNode::get_slot_type_right);
  1018. ClassDB::bind_method(D_METHOD("set_slot_color_right", "slot_index", "color"), &GraphNode::set_slot_color_right);
  1019. ClassDB::bind_method(D_METHOD("get_slot_color_right", "slot_index"), &GraphNode::get_slot_color_right);
  1020. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_right", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_right);
  1021. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_right", "slot_index"), &GraphNode::get_slot_custom_icon_right);
  1022. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "slot_index"), &GraphNode::is_slot_draw_stylebox);
  1023. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "slot_index", "enable"), &GraphNode::set_slot_draw_stylebox);
  1024. ClassDB::bind_method(D_METHOD("set_ignore_invalid_connection_type", "ignore"), &GraphNode::set_ignore_invalid_connection_type);
  1025. ClassDB::bind_method(D_METHOD("is_ignoring_valid_connection_type"), &GraphNode::is_ignoring_valid_connection_type);
  1026. ClassDB::bind_method(D_METHOD("get_input_port_count"), &GraphNode::get_input_port_count);
  1027. ClassDB::bind_method(D_METHOD("get_input_port_position", "port_idx"), &GraphNode::get_input_port_position);
  1028. ClassDB::bind_method(D_METHOD("get_input_port_type", "port_idx"), &GraphNode::get_input_port_type);
  1029. ClassDB::bind_method(D_METHOD("get_input_port_color", "port_idx"), &GraphNode::get_input_port_color);
  1030. ClassDB::bind_method(D_METHOD("get_input_port_slot", "port_idx"), &GraphNode::get_input_port_slot);
  1031. ClassDB::bind_method(D_METHOD("get_output_port_count"), &GraphNode::get_output_port_count);
  1032. ClassDB::bind_method(D_METHOD("get_output_port_position", "port_idx"), &GraphNode::get_output_port_position);
  1033. ClassDB::bind_method(D_METHOD("get_output_port_type", "port_idx"), &GraphNode::get_output_port_type);
  1034. ClassDB::bind_method(D_METHOD("get_output_port_color", "port_idx"), &GraphNode::get_output_port_color);
  1035. ClassDB::bind_method(D_METHOD("get_output_port_slot", "port_idx"), &GraphNode::get_output_port_slot);
  1036. GDVIRTUAL_BIND(_draw_port, "slot_index", "position", "left", "color")
  1037. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  1038. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_invalid_connection_type"), "set_ignore_invalid_connection_type", "is_ignoring_valid_connection_type");
  1039. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "slot_index")));
  1040. ADD_SIGNAL(MethodInfo("slot_sizes_changed"));
  1041. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel);
  1042. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_selected);
  1043. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_focus);
  1044. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar);
  1045. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar_selected);
  1046. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot);
  1047. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot_selected);
  1048. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, separation);
  1049. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, port_h_offset);
  1050. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, port);
  1051. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, resizer);
  1052. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphNode, resizer_color);
  1053. }
  1054. GraphNode::GraphNode() {
  1055. titlebar_hbox = memnew(HBoxContainer);
  1056. titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1057. add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
  1058. title_label = memnew(Label);
  1059. title_label->set_theme_type_variation("GraphNodeTitleLabel");
  1060. title_label->set_h_size_flags(SIZE_EXPAND_FILL);
  1061. titlebar_hbox->add_child(title_label);
  1062. set_mouse_filter(MOUSE_FILTER_STOP);
  1063. set_focus_mode(FOCUS_ACCESSIBILITY);
  1064. }