graph_node.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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/label.h"
  33. #include "scene/theme/theme_db.h"
  34. bool GraphNode::_set(const StringName &p_name, const Variant &p_value) {
  35. String str = p_name;
  36. if (!str.begins_with("slot/")) {
  37. return false;
  38. }
  39. int idx = str.get_slice("/", 1).to_int();
  40. String slot_property_name = str.get_slice("/", 2);
  41. Slot slot;
  42. if (slot_table.has(idx)) {
  43. slot = slot_table[idx];
  44. }
  45. if (slot_property_name == "left_enabled") {
  46. slot.enable_left = p_value;
  47. } else if (slot_property_name == "left_type") {
  48. slot.type_left = p_value;
  49. } else if (slot_property_name == "left_icon") {
  50. slot.custom_port_icon_left = p_value;
  51. } else if (slot_property_name == "left_color") {
  52. slot.color_left = p_value;
  53. } else if (slot_property_name == "right_enabled") {
  54. slot.enable_right = p_value;
  55. } else if (slot_property_name == "right_type") {
  56. slot.type_right = p_value;
  57. } else if (slot_property_name == "right_color") {
  58. slot.color_right = p_value;
  59. } else if (slot_property_name == "right_icon") {
  60. slot.custom_port_icon_right = p_value;
  61. } else if (slot_property_name == "draw_stylebox") {
  62. slot.draw_stylebox = p_value;
  63. } else {
  64. return false;
  65. }
  66. set_slot(idx,
  67. slot.enable_left,
  68. slot.type_left,
  69. slot.color_left,
  70. slot.enable_right,
  71. slot.type_right,
  72. slot.color_right,
  73. slot.custom_port_icon_left,
  74. slot.custom_port_icon_right,
  75. slot.draw_stylebox);
  76. queue_redraw();
  77. return true;
  78. }
  79. bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const {
  80. String str = p_name;
  81. if (!str.begins_with("slot/")) {
  82. return false;
  83. }
  84. int idx = str.get_slice("/", 1).to_int();
  85. StringName slot_property_name = str.get_slice("/", 2);
  86. Slot slot;
  87. if (slot_table.has(idx)) {
  88. slot = slot_table[idx];
  89. }
  90. if (slot_property_name == "left_enabled") {
  91. r_ret = slot.enable_left;
  92. } else if (slot_property_name == "left_type") {
  93. r_ret = slot.type_left;
  94. } else if (slot_property_name == "left_color") {
  95. r_ret = slot.color_left;
  96. } else if (slot_property_name == "left_icon") {
  97. r_ret = slot.custom_port_icon_left;
  98. } else if (slot_property_name == "right_enabled") {
  99. r_ret = slot.enable_right;
  100. } else if (slot_property_name == "right_type") {
  101. r_ret = slot.type_right;
  102. } else if (slot_property_name == "right_color") {
  103. r_ret = slot.color_right;
  104. } else if (slot_property_name == "right_icon") {
  105. r_ret = slot.custom_port_icon_right;
  106. } else if (slot_property_name == "draw_stylebox") {
  107. r_ret = slot.draw_stylebox;
  108. } else {
  109. return false;
  110. }
  111. return true;
  112. }
  113. void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const {
  114. int idx = 0;
  115. for (int i = 0; i < get_child_count(false); i++) {
  116. Control *child = as_sortable_control(get_child(i, false), SortableVisbilityMode::IGNORE);
  117. if (!child) {
  118. continue;
  119. }
  120. String base = "slot/" + itos(idx) + "/";
  121. p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled"));
  122. p_list->push_back(PropertyInfo(Variant::INT, base + "left_type"));
  123. p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color"));
  124. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "left_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  125. p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled"));
  126. p_list->push_back(PropertyInfo(Variant::INT, base + "right_type"));
  127. p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color"));
  128. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  129. p_list->push_back(PropertyInfo(Variant::BOOL, base + "draw_stylebox"));
  130. idx++;
  131. }
  132. }
  133. void GraphNode::_resort() {
  134. Size2 new_size = get_size();
  135. Ref<StyleBox> sb_panel = theme_cache.panel;
  136. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  137. // Resort titlebar first.
  138. Size2 titlebar_size = Size2(new_size.width, titlebar_hbox->get_size().height);
  139. titlebar_size -= sb_titlebar->get_minimum_size();
  140. Rect2 titlebar_rect = Rect2(sb_titlebar->get_offset(), titlebar_size);
  141. fit_child_in_rect(titlebar_hbox, titlebar_rect);
  142. // After resort, the children of the titlebar container may have changed their height (e.g. Label autowrap).
  143. Size2i titlebar_min_size = titlebar_hbox->get_combined_minimum_size();
  144. // First pass, determine minimum size AND amount of stretchable elements.
  145. Ref<StyleBox> sb_slot = theme_cache.slot;
  146. int separation = theme_cache.separation;
  147. int children_count = 0;
  148. int stretch_min = 0;
  149. int available_stretch_space = 0;
  150. float stretch_ratio_total = 0;
  151. HashMap<Control *, _MinSizeCache> min_size_cache;
  152. for (int i = 0; i < get_child_count(false); i++) {
  153. Control *child = as_sortable_control(get_child(i, false));
  154. if (!child) {
  155. continue;
  156. }
  157. Size2i size = child->get_combined_minimum_size() + (slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2());
  158. stretch_min += size.height;
  159. _MinSizeCache msc;
  160. msc.min_size = size.height;
  161. msc.will_stretch = child->get_v_size_flags().has_flag(SIZE_EXPAND);
  162. msc.final_size = msc.min_size;
  163. min_size_cache[child] = msc;
  164. if (msc.will_stretch) {
  165. available_stretch_space += msc.min_size;
  166. stretch_ratio_total += child->get_stretch_ratio();
  167. }
  168. children_count++;
  169. }
  170. if (children_count == 0) {
  171. return;
  172. }
  173. int stretch_max = new_size.height - (children_count - 1) * separation;
  174. int stretch_diff = stretch_max - stretch_min;
  175. // Avoid negative stretch space.
  176. stretch_diff = MAX(stretch_diff, 0);
  177. 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;
  178. // Second pass, discard elements that can't be stretched, this will run while stretchable elements exist.
  179. while (stretch_ratio_total > 0) {
  180. // First of all, don't even be here if no stretchable objects exist.
  181. bool refit_successful = true;
  182. for (int i = 0; i < get_child_count(false); i++) {
  183. Control *child = as_sortable_control(get_child(i, false));
  184. if (!child) {
  185. continue;
  186. }
  187. ERR_FAIL_COND(!min_size_cache.has(child));
  188. _MinSizeCache &msc = min_size_cache[child];
  189. if (msc.will_stretch) {
  190. int final_pixel_size = available_stretch_space * child->get_stretch_ratio() / stretch_ratio_total;
  191. if (final_pixel_size < msc.min_size) {
  192. // If the available stretching area is too small for a Control,
  193. // then remove it from stretching area.
  194. msc.will_stretch = false;
  195. stretch_ratio_total -= child->get_stretch_ratio();
  196. refit_successful = false;
  197. available_stretch_space -= msc.min_size;
  198. msc.final_size = msc.min_size;
  199. break;
  200. } else {
  201. msc.final_size = final_pixel_size;
  202. }
  203. }
  204. }
  205. if (refit_successful) {
  206. break;
  207. }
  208. }
  209. // Final pass, draw and stretch elements.
  210. int ofs_y = sb_panel->get_margin(SIDE_TOP) + titlebar_min_size.height + sb_titlebar->get_minimum_size().height;
  211. slot_y_cache.clear();
  212. int width = new_size.width - sb_panel->get_minimum_size().width;
  213. int valid_children_idx = 0;
  214. for (int i = 0; i < get_child_count(false); i++) {
  215. Control *child = as_sortable_control(get_child(i, false));
  216. if (!child) {
  217. continue;
  218. }
  219. _MinSizeCache &msc = min_size_cache[child];
  220. if (valid_children_idx > 0) {
  221. ofs_y += separation;
  222. }
  223. int from_y_pos = ofs_y;
  224. int to_y_pos = ofs_y + msc.final_size;
  225. // Adjust so the last valid child always fits perfect, compensating for numerical imprecision.
  226. if (msc.will_stretch && valid_children_idx == children_count - 1) {
  227. to_y_pos = new_size.height - sb_panel->get_margin(SIDE_BOTTOM);
  228. }
  229. int height = to_y_pos - from_y_pos;
  230. float margin = sb_panel->get_margin(SIDE_LEFT) + (slot_table[i].draw_stylebox ? sb_slot->get_margin(SIDE_LEFT) : 0);
  231. float final_width = width - (slot_table[i].draw_stylebox ? sb_slot->get_minimum_size().x : 0);
  232. Rect2 rect(margin, from_y_pos, final_width, height);
  233. fit_child_in_rect(child, rect);
  234. slot_y_cache.push_back(from_y_pos - sb_panel->get_margin(SIDE_TOP) + height * 0.5);
  235. ofs_y = to_y_pos;
  236. valid_children_idx++;
  237. }
  238. queue_redraw();
  239. port_pos_dirty = true;
  240. }
  241. void GraphNode::draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color) {
  242. if (GDVIRTUAL_CALL(_draw_port, p_slot_index, p_pos, p_left, p_color)) {
  243. return;
  244. }
  245. Slot slot = slot_table[p_slot_index];
  246. Ref<Texture2D> port_icon = p_left ? slot.custom_port_icon_left : slot.custom_port_icon_right;
  247. Point2 icon_offset;
  248. if (!port_icon.is_valid()) {
  249. port_icon = theme_cache.port;
  250. }
  251. icon_offset = -port_icon->get_size() * 0.5;
  252. port_icon->draw(get_canvas_item(), p_pos + icon_offset, p_color);
  253. }
  254. void GraphNode::_notification(int p_what) {
  255. switch (p_what) {
  256. case NOTIFICATION_DRAW: {
  257. // Used for layout calculations.
  258. Ref<StyleBox> sb_panel = theme_cache.panel;
  259. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  260. // Used for drawing.
  261. Ref<StyleBox> sb_to_draw_panel = selected ? theme_cache.panel_selected : theme_cache.panel;
  262. Ref<StyleBox> sb_to_draw_titlebar = selected ? theme_cache.titlebar_selected : theme_cache.titlebar;
  263. Ref<StyleBox> sb_slot = theme_cache.slot;
  264. int port_h_offset = theme_cache.port_h_offset;
  265. Rect2 titlebar_rect(Point2(), titlebar_hbox->get_size() + sb_titlebar->get_minimum_size());
  266. Size2 body_size = get_size();
  267. titlebar_rect.size.width = body_size.width;
  268. body_size.height -= titlebar_rect.size.height;
  269. Rect2 body_rect(0, titlebar_rect.size.height, body_size.width, body_size.height);
  270. // Draw body (slots area) stylebox.
  271. draw_style_box(sb_to_draw_panel, body_rect);
  272. // Draw title bar stylebox above.
  273. draw_style_box(sb_to_draw_titlebar, titlebar_rect);
  274. int width = get_size().width - sb_panel->get_minimum_size().x;
  275. // Take the HboxContainer child into account.
  276. if (get_child_count(false) > 0) {
  277. int slot_index = 0;
  278. for (const KeyValue<int, Slot> &E : slot_table) {
  279. if (E.key < 0 || E.key >= slot_y_cache.size()) {
  280. continue;
  281. }
  282. if (!slot_table.has(E.key)) {
  283. continue;
  284. }
  285. const Slot &slot = slot_table[E.key];
  286. // Left port.
  287. if (slot.enable_left) {
  288. draw_port(slot_index, Point2i(port_h_offset, slot_y_cache[E.key] + sb_panel->get_margin(SIDE_TOP)), true, slot.color_left);
  289. }
  290. // Right port.
  291. if (slot.enable_right) {
  292. draw_port(slot_index, Point2i(get_size().x - port_h_offset, slot_y_cache[E.key] + sb_panel->get_margin(SIDE_TOP)), false, slot.color_right);
  293. }
  294. // Draw slot stylebox.
  295. if (slot.draw_stylebox) {
  296. Control *child = Object::cast_to<Control>(get_child(E.key, false));
  297. if (!child || !child->is_visible_in_tree()) {
  298. continue;
  299. }
  300. Rect2 child_rect = child->get_rect();
  301. child_rect.position.x = sb_panel->get_margin(SIDE_LEFT);
  302. child_rect.size.width = width;
  303. draw_style_box(sb_slot, child_rect);
  304. }
  305. slot_index++;
  306. }
  307. }
  308. if (resizable) {
  309. draw_texture(theme_cache.resizer, get_size() - theme_cache.resizer->get_size(), theme_cache.resizer_color);
  310. }
  311. } break;
  312. }
  313. }
  314. 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) {
  315. ERR_FAIL_COND_MSG(p_slot_index < 0, vformat("Cannot set slot with index (%d) lesser than zero.", p_slot_index));
  316. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) &&
  317. !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) &&
  318. !p_custom_left.is_valid() && !p_custom_right.is_valid()) {
  319. slot_table.erase(p_slot_index);
  320. return;
  321. }
  322. Slot slot;
  323. slot.enable_left = p_enable_left;
  324. slot.type_left = p_type_left;
  325. slot.color_left = p_color_left;
  326. slot.enable_right = p_enable_right;
  327. slot.type_right = p_type_right;
  328. slot.color_right = p_color_right;
  329. slot.custom_port_icon_left = p_custom_left;
  330. slot.custom_port_icon_right = p_custom_right;
  331. slot.draw_stylebox = p_draw_stylebox;
  332. slot_table[p_slot_index] = slot;
  333. queue_redraw();
  334. port_pos_dirty = true;
  335. emit_signal(SNAME("slot_updated"), p_slot_index);
  336. }
  337. void GraphNode::clear_slot(int p_slot_index) {
  338. slot_table.erase(p_slot_index);
  339. queue_redraw();
  340. port_pos_dirty = true;
  341. }
  342. void GraphNode::clear_all_slots() {
  343. slot_table.clear();
  344. queue_redraw();
  345. port_pos_dirty = true;
  346. }
  347. bool GraphNode::is_slot_enabled_left(int p_slot_index) const {
  348. if (!slot_table.has(p_slot_index)) {
  349. return false;
  350. }
  351. return slot_table[p_slot_index].enable_left;
  352. }
  353. void GraphNode::set_slot_enabled_left(int p_slot_index, bool p_enable) {
  354. 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));
  355. if (slot_table[p_slot_index].enable_left == p_enable) {
  356. return;
  357. }
  358. slot_table[p_slot_index].enable_left = p_enable;
  359. queue_redraw();
  360. port_pos_dirty = true;
  361. emit_signal(SNAME("slot_updated"), p_slot_index);
  362. }
  363. void GraphNode::set_slot_type_left(int p_slot_index, int p_type) {
  364. 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));
  365. if (slot_table[p_slot_index].type_left == p_type) {
  366. return;
  367. }
  368. slot_table[p_slot_index].type_left = p_type;
  369. queue_redraw();
  370. port_pos_dirty = true;
  371. emit_signal(SNAME("slot_updated"), p_slot_index);
  372. }
  373. int GraphNode::get_slot_type_left(int p_slot_index) const {
  374. if (!slot_table.has(p_slot_index)) {
  375. return 0;
  376. }
  377. return slot_table[p_slot_index].type_left;
  378. }
  379. void GraphNode::set_slot_color_left(int p_slot_index, const Color &p_color) {
  380. 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));
  381. if (slot_table[p_slot_index].color_left == p_color) {
  382. return;
  383. }
  384. slot_table[p_slot_index].color_left = p_color;
  385. queue_redraw();
  386. port_pos_dirty = true;
  387. emit_signal(SNAME("slot_updated"), p_slot_index);
  388. }
  389. Color GraphNode::get_slot_color_left(int p_slot_index) const {
  390. if (!slot_table.has(p_slot_index)) {
  391. return Color(1, 1, 1, 1);
  392. }
  393. return slot_table[p_slot_index].color_left;
  394. }
  395. void GraphNode::set_slot_custom_icon_left(int p_slot_index, const Ref<Texture2D> &p_custom_icon) {
  396. 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));
  397. if (slot_table[p_slot_index].custom_port_icon_left == p_custom_icon) {
  398. return;
  399. }
  400. slot_table[p_slot_index].custom_port_icon_left = p_custom_icon;
  401. queue_redraw();
  402. port_pos_dirty = true;
  403. emit_signal(SNAME("slot_updated"), p_slot_index);
  404. }
  405. Ref<Texture2D> GraphNode::get_slot_custom_icon_left(int p_slot_index) const {
  406. if (!slot_table.has(p_slot_index)) {
  407. return Ref<Texture2D>();
  408. }
  409. return slot_table[p_slot_index].custom_port_icon_left;
  410. }
  411. bool GraphNode::is_slot_enabled_right(int p_slot_index) const {
  412. if (!slot_table.has(p_slot_index)) {
  413. return false;
  414. }
  415. return slot_table[p_slot_index].enable_right;
  416. }
  417. void GraphNode::set_slot_enabled_right(int p_slot_index, bool p_enable) {
  418. 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));
  419. if (slot_table[p_slot_index].enable_right == p_enable) {
  420. return;
  421. }
  422. slot_table[p_slot_index].enable_right = p_enable;
  423. queue_redraw();
  424. port_pos_dirty = true;
  425. emit_signal(SNAME("slot_updated"), p_slot_index);
  426. }
  427. void GraphNode::set_slot_type_right(int p_slot_index, int p_type) {
  428. 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));
  429. if (slot_table[p_slot_index].type_right == p_type) {
  430. return;
  431. }
  432. slot_table[p_slot_index].type_right = p_type;
  433. queue_redraw();
  434. port_pos_dirty = true;
  435. emit_signal(SNAME("slot_updated"), p_slot_index);
  436. }
  437. int GraphNode::get_slot_type_right(int p_slot_index) const {
  438. if (!slot_table.has(p_slot_index)) {
  439. return 0;
  440. }
  441. return slot_table[p_slot_index].type_right;
  442. }
  443. void GraphNode::set_slot_color_right(int p_slot_index, const Color &p_color) {
  444. 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));
  445. if (slot_table[p_slot_index].color_right == p_color) {
  446. return;
  447. }
  448. slot_table[p_slot_index].color_right = p_color;
  449. queue_redraw();
  450. port_pos_dirty = true;
  451. emit_signal(SNAME("slot_updated"), p_slot_index);
  452. }
  453. Color GraphNode::get_slot_color_right(int p_slot_index) const {
  454. if (!slot_table.has(p_slot_index)) {
  455. return Color(1, 1, 1, 1);
  456. }
  457. return slot_table[p_slot_index].color_right;
  458. }
  459. void GraphNode::set_slot_custom_icon_right(int p_slot_index, const Ref<Texture2D> &p_custom_icon) {
  460. 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));
  461. if (slot_table[p_slot_index].custom_port_icon_right == p_custom_icon) {
  462. return;
  463. }
  464. slot_table[p_slot_index].custom_port_icon_right = p_custom_icon;
  465. queue_redraw();
  466. port_pos_dirty = true;
  467. emit_signal(SNAME("slot_updated"), p_slot_index);
  468. }
  469. Ref<Texture2D> GraphNode::get_slot_custom_icon_right(int p_slot_index) const {
  470. if (!slot_table.has(p_slot_index)) {
  471. return Ref<Texture2D>();
  472. }
  473. return slot_table[p_slot_index].custom_port_icon_right;
  474. }
  475. bool GraphNode::is_slot_draw_stylebox(int p_slot_index) const {
  476. if (!slot_table.has(p_slot_index)) {
  477. return false;
  478. }
  479. return slot_table[p_slot_index].draw_stylebox;
  480. }
  481. void GraphNode::set_slot_draw_stylebox(int p_slot_index, bool p_enable) {
  482. 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));
  483. slot_table[p_slot_index].draw_stylebox = p_enable;
  484. queue_redraw();
  485. port_pos_dirty = true;
  486. emit_signal(SNAME("slot_updated"), p_slot_index);
  487. }
  488. void GraphNode::set_ignore_invalid_connection_type(bool p_ignore) {
  489. ignore_invalid_connection_type = p_ignore;
  490. }
  491. bool GraphNode::is_ignoring_valid_connection_type() const {
  492. return ignore_invalid_connection_type;
  493. }
  494. Size2 GraphNode::get_minimum_size() const {
  495. Ref<StyleBox> sb_panel = theme_cache.panel;
  496. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  497. Ref<StyleBox> sb_slot = theme_cache.slot;
  498. int separation = theme_cache.separation;
  499. Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
  500. for (int i = 0; i < get_child_count(false); i++) {
  501. Control *child = as_sortable_control(get_child(i, false));
  502. if (!child) {
  503. continue;
  504. }
  505. Size2i size = child->get_combined_minimum_size();
  506. size.width += sb_panel->get_minimum_size().width;
  507. if (slot_table.has(i)) {
  508. size += slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2();
  509. }
  510. minsize.height += size.height;
  511. minsize.width = MAX(minsize.width, size.width);
  512. if (i > 0) {
  513. minsize.height += separation;
  514. }
  515. }
  516. minsize.height += sb_panel->get_minimum_size().height;
  517. return minsize;
  518. }
  519. void GraphNode::_port_pos_update() {
  520. int edgeofs = theme_cache.port_h_offset;
  521. int separation = theme_cache.separation;
  522. Ref<StyleBox> sb_panel = theme_cache.panel;
  523. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  524. left_port_cache.clear();
  525. right_port_cache.clear();
  526. int vertical_ofs = titlebar_hbox->get_size().height + sb_titlebar->get_minimum_size().height + sb_panel->get_margin(SIDE_TOP);
  527. int slot_index = 0;
  528. for (int i = 0; i < get_child_count(false); i++) {
  529. Control *child = as_sortable_control(get_child(i, false), SortableVisbilityMode::IGNORE);
  530. if (!child) {
  531. continue;
  532. }
  533. Size2i size = child->get_rect().size;
  534. if (slot_table.has(slot_index)) {
  535. if (slot_table[slot_index].enable_left) {
  536. PortCache port_cache;
  537. port_cache.pos = Point2i(edgeofs, vertical_ofs + size.height / 2);
  538. port_cache.type = slot_table[slot_index].type_left;
  539. port_cache.color = slot_table[slot_index].color_left;
  540. port_cache.slot_index = slot_index;
  541. left_port_cache.push_back(port_cache);
  542. }
  543. if (slot_table[slot_index].enable_right) {
  544. PortCache port_cache;
  545. port_cache.pos = Point2i(get_size().width - edgeofs, vertical_ofs + size.height / 2);
  546. port_cache.type = slot_table[slot_index].type_right;
  547. port_cache.color = slot_table[slot_index].color_right;
  548. port_cache.slot_index = slot_index;
  549. right_port_cache.push_back(port_cache);
  550. }
  551. }
  552. vertical_ofs += separation;
  553. vertical_ofs += size.height;
  554. slot_index++;
  555. }
  556. port_pos_dirty = false;
  557. }
  558. int GraphNode::get_input_port_count() {
  559. if (port_pos_dirty) {
  560. _port_pos_update();
  561. }
  562. return left_port_cache.size();
  563. }
  564. int GraphNode::get_output_port_count() {
  565. if (port_pos_dirty) {
  566. _port_pos_update();
  567. }
  568. return right_port_cache.size();
  569. }
  570. Vector2 GraphNode::get_input_port_position(int p_port_idx) {
  571. if (port_pos_dirty) {
  572. _port_pos_update();
  573. }
  574. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Vector2());
  575. Vector2 pos = left_port_cache[p_port_idx].pos;
  576. return pos;
  577. }
  578. int GraphNode::get_input_port_type(int p_port_idx) {
  579. if (port_pos_dirty) {
  580. _port_pos_update();
  581. }
  582. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), 0);
  583. return left_port_cache[p_port_idx].type;
  584. }
  585. Color GraphNode::get_input_port_color(int p_port_idx) {
  586. if (port_pos_dirty) {
  587. _port_pos_update();
  588. }
  589. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Color());
  590. return left_port_cache[p_port_idx].color;
  591. }
  592. int GraphNode::get_input_port_slot(int p_port_idx) {
  593. if (port_pos_dirty) {
  594. _port_pos_update();
  595. }
  596. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), -1);
  597. return left_port_cache[p_port_idx].slot_index;
  598. }
  599. Vector2 GraphNode::get_output_port_position(int p_port_idx) {
  600. if (port_pos_dirty) {
  601. _port_pos_update();
  602. }
  603. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Vector2());
  604. Vector2 pos = right_port_cache[p_port_idx].pos;
  605. return pos;
  606. }
  607. int GraphNode::get_output_port_type(int p_port_idx) {
  608. if (port_pos_dirty) {
  609. _port_pos_update();
  610. }
  611. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), 0);
  612. return right_port_cache[p_port_idx].type;
  613. }
  614. Color GraphNode::get_output_port_color(int p_port_idx) {
  615. if (port_pos_dirty) {
  616. _port_pos_update();
  617. }
  618. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Color());
  619. return right_port_cache[p_port_idx].color;
  620. }
  621. int GraphNode::get_output_port_slot(int p_port_idx) {
  622. if (port_pos_dirty) {
  623. _port_pos_update();
  624. }
  625. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), -1);
  626. return right_port_cache[p_port_idx].slot_index;
  627. }
  628. void GraphNode::set_title(const String &p_title) {
  629. if (title == p_title) {
  630. return;
  631. }
  632. title = p_title;
  633. if (title_label) {
  634. title_label->set_text(title);
  635. }
  636. update_minimum_size();
  637. }
  638. String GraphNode::get_title() const {
  639. return title;
  640. }
  641. HBoxContainer *GraphNode::get_titlebar_hbox() {
  642. return titlebar_hbox;
  643. }
  644. Control::CursorShape GraphNode::get_cursor_shape(const Point2 &p_pos) const {
  645. if (resizable) {
  646. if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) {
  647. return CURSOR_FDIAGSIZE;
  648. }
  649. }
  650. return Control::get_cursor_shape(p_pos);
  651. }
  652. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  653. Vector<int> flags;
  654. flags.append(SIZE_FILL);
  655. flags.append(SIZE_SHRINK_BEGIN);
  656. flags.append(SIZE_SHRINK_CENTER);
  657. flags.append(SIZE_SHRINK_END);
  658. return flags;
  659. }
  660. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  661. Vector<int> flags;
  662. flags.append(SIZE_FILL);
  663. flags.append(SIZE_EXPAND);
  664. flags.append(SIZE_SHRINK_BEGIN);
  665. flags.append(SIZE_SHRINK_CENTER);
  666. flags.append(SIZE_SHRINK_END);
  667. return flags;
  668. }
  669. void GraphNode::_bind_methods() {
  670. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  671. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  672. ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphNode::get_titlebar_hbox);
  673. 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));
  674. ClassDB::bind_method(D_METHOD("clear_slot", "slot_index"), &GraphNode::clear_slot);
  675. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  676. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "slot_index"), &GraphNode::is_slot_enabled_left);
  677. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "slot_index", "enable"), &GraphNode::set_slot_enabled_left);
  678. ClassDB::bind_method(D_METHOD("set_slot_type_left", "slot_index", "type"), &GraphNode::set_slot_type_left);
  679. ClassDB::bind_method(D_METHOD("get_slot_type_left", "slot_index"), &GraphNode::get_slot_type_left);
  680. ClassDB::bind_method(D_METHOD("set_slot_color_left", "slot_index", "color"), &GraphNode::set_slot_color_left);
  681. ClassDB::bind_method(D_METHOD("get_slot_color_left", "slot_index"), &GraphNode::get_slot_color_left);
  682. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_left", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_left);
  683. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_left", "slot_index"), &GraphNode::get_slot_custom_icon_left);
  684. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "slot_index"), &GraphNode::is_slot_enabled_right);
  685. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "slot_index", "enable"), &GraphNode::set_slot_enabled_right);
  686. ClassDB::bind_method(D_METHOD("set_slot_type_right", "slot_index", "type"), &GraphNode::set_slot_type_right);
  687. ClassDB::bind_method(D_METHOD("get_slot_type_right", "slot_index"), &GraphNode::get_slot_type_right);
  688. ClassDB::bind_method(D_METHOD("set_slot_color_right", "slot_index", "color"), &GraphNode::set_slot_color_right);
  689. ClassDB::bind_method(D_METHOD("get_slot_color_right", "slot_index"), &GraphNode::get_slot_color_right);
  690. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_right", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_right);
  691. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_right", "slot_index"), &GraphNode::get_slot_custom_icon_right);
  692. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "slot_index"), &GraphNode::is_slot_draw_stylebox);
  693. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "slot_index", "enable"), &GraphNode::set_slot_draw_stylebox);
  694. ClassDB::bind_method(D_METHOD("set_ignore_invalid_connection_type", "ignore"), &GraphNode::set_ignore_invalid_connection_type);
  695. ClassDB::bind_method(D_METHOD("is_ignoring_valid_connection_type"), &GraphNode::is_ignoring_valid_connection_type);
  696. ClassDB::bind_method(D_METHOD("get_input_port_count"), &GraphNode::get_input_port_count);
  697. ClassDB::bind_method(D_METHOD("get_input_port_position", "port_idx"), &GraphNode::get_input_port_position);
  698. ClassDB::bind_method(D_METHOD("get_input_port_type", "port_idx"), &GraphNode::get_input_port_type);
  699. ClassDB::bind_method(D_METHOD("get_input_port_color", "port_idx"), &GraphNode::get_input_port_color);
  700. ClassDB::bind_method(D_METHOD("get_input_port_slot", "port_idx"), &GraphNode::get_input_port_slot);
  701. ClassDB::bind_method(D_METHOD("get_output_port_count"), &GraphNode::get_output_port_count);
  702. ClassDB::bind_method(D_METHOD("get_output_port_position", "port_idx"), &GraphNode::get_output_port_position);
  703. ClassDB::bind_method(D_METHOD("get_output_port_type", "port_idx"), &GraphNode::get_output_port_type);
  704. ClassDB::bind_method(D_METHOD("get_output_port_color", "port_idx"), &GraphNode::get_output_port_color);
  705. ClassDB::bind_method(D_METHOD("get_output_port_slot", "port_idx"), &GraphNode::get_output_port_slot);
  706. GDVIRTUAL_BIND(_draw_port, "slot_index", "position", "left", "color")
  707. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  708. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_invalid_connection_type"), "set_ignore_invalid_connection_type", "is_ignoring_valid_connection_type");
  709. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "slot_index")));
  710. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel);
  711. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_selected);
  712. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar);
  713. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar_selected);
  714. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot);
  715. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, separation);
  716. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, port_h_offset);
  717. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, port);
  718. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, resizer);
  719. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphNode, resizer_color);
  720. }
  721. GraphNode::GraphNode() {
  722. titlebar_hbox = memnew(HBoxContainer);
  723. titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  724. add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
  725. title_label = memnew(Label);
  726. title_label->set_theme_type_variation("GraphNodeTitleLabel");
  727. title_label->set_h_size_flags(SIZE_EXPAND_FILL);
  728. titlebar_hbox->add_child(title_label);
  729. set_mouse_filter(MOUSE_FILTER_STOP);
  730. }