2
0

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