graph_node.cpp 31 KB

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