graph_node.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 = Object::cast_to<Control>(get_child(i, false));
  118. if (!child || child->is_set_as_top_level()) {
  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 = Object::cast_to<Control>(get_child(i, false));
  155. if (!child || !child->is_visible_in_tree() || child->is_set_as_top_level()) {
  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);
  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 = Object::cast_to<Control>(get_child(i, false));
  185. if (!child || !child->is_visible_in_tree() || child->is_set_as_top_level()) {
  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 = Object::cast_to<Control>(get_child(i, false));
  217. if (!child || !child->is_visible_in_tree() || child->is_set_as_top_level()) {
  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. if (get_child_count() > 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. bool GraphNode::is_slot_enabled_right(int p_slot_index) const {
  396. if (!slot_table.has(p_slot_index)) {
  397. return false;
  398. }
  399. return slot_table[p_slot_index].enable_right;
  400. }
  401. void GraphNode::set_slot_enabled_right(int p_slot_index, bool p_enable) {
  402. 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));
  403. if (slot_table[p_slot_index].enable_right == p_enable) {
  404. return;
  405. }
  406. slot_table[p_slot_index].enable_right = p_enable;
  407. queue_redraw();
  408. port_pos_dirty = true;
  409. emit_signal(SNAME("slot_updated"), p_slot_index);
  410. }
  411. void GraphNode::set_slot_type_right(int p_slot_index, int p_type) {
  412. 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));
  413. if (slot_table[p_slot_index].type_right == p_type) {
  414. return;
  415. }
  416. slot_table[p_slot_index].type_right = p_type;
  417. queue_redraw();
  418. port_pos_dirty = true;
  419. emit_signal(SNAME("slot_updated"), p_slot_index);
  420. }
  421. int GraphNode::get_slot_type_right(int p_slot_index) const {
  422. if (!slot_table.has(p_slot_index)) {
  423. return 0;
  424. }
  425. return slot_table[p_slot_index].type_right;
  426. }
  427. void GraphNode::set_slot_color_right(int p_slot_index, const Color &p_color) {
  428. 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));
  429. if (slot_table[p_slot_index].color_right == p_color) {
  430. return;
  431. }
  432. slot_table[p_slot_index].color_right = p_color;
  433. queue_redraw();
  434. port_pos_dirty = true;
  435. emit_signal(SNAME("slot_updated"), p_slot_index);
  436. }
  437. Color GraphNode::get_slot_color_right(int p_slot_index) const {
  438. if (!slot_table.has(p_slot_index)) {
  439. return Color(1, 1, 1, 1);
  440. }
  441. return slot_table[p_slot_index].color_right;
  442. }
  443. bool GraphNode::is_slot_draw_stylebox(int p_slot_index) const {
  444. if (!slot_table.has(p_slot_index)) {
  445. return false;
  446. }
  447. return slot_table[p_slot_index].draw_stylebox;
  448. }
  449. void GraphNode::set_slot_draw_stylebox(int p_slot_index, bool p_enable) {
  450. 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));
  451. slot_table[p_slot_index].draw_stylebox = p_enable;
  452. queue_redraw();
  453. port_pos_dirty = true;
  454. emit_signal(SNAME("slot_updated"), p_slot_index);
  455. }
  456. Size2 GraphNode::get_minimum_size() const {
  457. Ref<StyleBox> sb_panel = theme_cache.panel;
  458. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  459. Ref<StyleBox> sb_slot = theme_cache.slot;
  460. int separation = theme_cache.separation;
  461. Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
  462. for (int i = 0; i < get_child_count(false); i++) {
  463. Control *child = Object::cast_to<Control>(get_child(i, false));
  464. if (!child || !child->is_visible() || child->is_set_as_top_level()) {
  465. continue;
  466. }
  467. Size2i size = child->get_combined_minimum_size();
  468. size.width += sb_panel->get_minimum_size().width;
  469. if (slot_table.has(i)) {
  470. size += slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2();
  471. }
  472. minsize.height += size.height;
  473. minsize.width = MAX(minsize.width, size.width);
  474. if (i > 0) {
  475. minsize.height += separation;
  476. }
  477. }
  478. minsize.height += sb_panel->get_minimum_size().height;
  479. return minsize;
  480. }
  481. void GraphNode::_port_pos_update() {
  482. int edgeofs = theme_cache.port_h_offset;
  483. int separation = theme_cache.separation;
  484. Ref<StyleBox> sb_panel = theme_cache.panel;
  485. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  486. left_port_cache.clear();
  487. right_port_cache.clear();
  488. int vertical_ofs = titlebar_hbox->get_size().height + sb_titlebar->get_minimum_size().height + sb_panel->get_margin(SIDE_TOP);
  489. for (int i = 0; i < get_child_count(false); i++) {
  490. Control *child = Object::cast_to<Control>(get_child(i, false));
  491. if (!child || child->is_set_as_top_level()) {
  492. continue;
  493. }
  494. Size2i size = child->get_rect().size;
  495. if (slot_table.has(i)) {
  496. if (slot_table[i].enable_left) {
  497. PortCache port_cache;
  498. port_cache.pos = Point2i(edgeofs, vertical_ofs + size.height / 2);
  499. port_cache.type = slot_table[i].type_left;
  500. port_cache.color = slot_table[i].color_left;
  501. port_cache.slot_index = child->get_index(false);
  502. left_port_cache.push_back(port_cache);
  503. }
  504. if (slot_table[i].enable_right) {
  505. PortCache port_cache;
  506. port_cache.pos = Point2i(get_size().width - edgeofs, vertical_ofs + size.height / 2);
  507. port_cache.type = slot_table[i].type_right;
  508. port_cache.color = slot_table[i].color_right;
  509. port_cache.slot_index = child->get_index(false);
  510. right_port_cache.push_back(port_cache);
  511. }
  512. }
  513. vertical_ofs += separation;
  514. vertical_ofs += size.height;
  515. }
  516. port_pos_dirty = false;
  517. }
  518. int GraphNode::get_input_port_count() {
  519. if (port_pos_dirty) {
  520. _port_pos_update();
  521. }
  522. return left_port_cache.size();
  523. }
  524. int GraphNode::get_output_port_count() {
  525. if (port_pos_dirty) {
  526. _port_pos_update();
  527. }
  528. return right_port_cache.size();
  529. }
  530. Vector2 GraphNode::get_input_port_position(int p_port_idx) {
  531. if (port_pos_dirty) {
  532. _port_pos_update();
  533. }
  534. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Vector2());
  535. Vector2 pos = left_port_cache[p_port_idx].pos;
  536. return pos;
  537. }
  538. int GraphNode::get_input_port_type(int p_port_idx) {
  539. if (port_pos_dirty) {
  540. _port_pos_update();
  541. }
  542. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), 0);
  543. return left_port_cache[p_port_idx].type;
  544. }
  545. Color GraphNode::get_input_port_color(int p_port_idx) {
  546. if (port_pos_dirty) {
  547. _port_pos_update();
  548. }
  549. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Color());
  550. return left_port_cache[p_port_idx].color;
  551. }
  552. int GraphNode::get_input_port_slot(int p_port_idx) {
  553. if (port_pos_dirty) {
  554. _port_pos_update();
  555. }
  556. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), -1);
  557. return left_port_cache[p_port_idx].slot_index;
  558. }
  559. Vector2 GraphNode::get_output_port_position(int p_port_idx) {
  560. if (port_pos_dirty) {
  561. _port_pos_update();
  562. }
  563. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Vector2());
  564. Vector2 pos = right_port_cache[p_port_idx].pos;
  565. return pos;
  566. }
  567. int GraphNode::get_output_port_type(int p_port_idx) {
  568. if (port_pos_dirty) {
  569. _port_pos_update();
  570. }
  571. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), 0);
  572. return right_port_cache[p_port_idx].type;
  573. }
  574. Color GraphNode::get_output_port_color(int p_port_idx) {
  575. if (port_pos_dirty) {
  576. _port_pos_update();
  577. }
  578. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Color());
  579. return right_port_cache[p_port_idx].color;
  580. }
  581. int GraphNode::get_output_port_slot(int p_port_idx) {
  582. if (port_pos_dirty) {
  583. _port_pos_update();
  584. }
  585. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), -1);
  586. return right_port_cache[p_port_idx].slot_index;
  587. }
  588. void GraphNode::set_title(const String &p_title) {
  589. if (title == p_title) {
  590. return;
  591. }
  592. title = p_title;
  593. if (title_label) {
  594. title_label->set_text(title);
  595. }
  596. update_minimum_size();
  597. }
  598. String GraphNode::get_title() const {
  599. return title;
  600. }
  601. HBoxContainer *GraphNode::get_titlebar_hbox() {
  602. return titlebar_hbox;
  603. }
  604. Control::CursorShape GraphNode::get_cursor_shape(const Point2 &p_pos) const {
  605. if (resizable) {
  606. if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) {
  607. return CURSOR_FDIAGSIZE;
  608. }
  609. }
  610. return Control::get_cursor_shape(p_pos);
  611. }
  612. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  613. Vector<int> flags;
  614. flags.append(SIZE_FILL);
  615. flags.append(SIZE_SHRINK_BEGIN);
  616. flags.append(SIZE_SHRINK_CENTER);
  617. flags.append(SIZE_SHRINK_END);
  618. return flags;
  619. }
  620. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  621. Vector<int> flags;
  622. flags.append(SIZE_FILL);
  623. flags.append(SIZE_EXPAND);
  624. flags.append(SIZE_SHRINK_BEGIN);
  625. flags.append(SIZE_SHRINK_CENTER);
  626. flags.append(SIZE_SHRINK_END);
  627. return flags;
  628. }
  629. void GraphNode::_bind_methods() {
  630. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  631. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  632. ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphNode::get_titlebar_hbox);
  633. 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));
  634. ClassDB::bind_method(D_METHOD("clear_slot", "slot_index"), &GraphNode::clear_slot);
  635. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  636. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "slot_index"), &GraphNode::is_slot_enabled_left);
  637. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "slot_index", "enable"), &GraphNode::set_slot_enabled_left);
  638. ClassDB::bind_method(D_METHOD("set_slot_type_left", "slot_index", "type"), &GraphNode::set_slot_type_left);
  639. ClassDB::bind_method(D_METHOD("get_slot_type_left", "slot_index"), &GraphNode::get_slot_type_left);
  640. ClassDB::bind_method(D_METHOD("set_slot_color_left", "slot_index", "color"), &GraphNode::set_slot_color_left);
  641. ClassDB::bind_method(D_METHOD("get_slot_color_left", "slot_index"), &GraphNode::get_slot_color_left);
  642. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "slot_index"), &GraphNode::is_slot_enabled_right);
  643. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "slot_index", "enable"), &GraphNode::set_slot_enabled_right);
  644. ClassDB::bind_method(D_METHOD("set_slot_type_right", "slot_index", "type"), &GraphNode::set_slot_type_right);
  645. ClassDB::bind_method(D_METHOD("get_slot_type_right", "slot_index"), &GraphNode::get_slot_type_right);
  646. ClassDB::bind_method(D_METHOD("set_slot_color_right", "slot_index", "color"), &GraphNode::set_slot_color_right);
  647. ClassDB::bind_method(D_METHOD("get_slot_color_right", "slot_index"), &GraphNode::get_slot_color_right);
  648. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "slot_index"), &GraphNode::is_slot_draw_stylebox);
  649. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "slot_index", "enable"), &GraphNode::set_slot_draw_stylebox);
  650. ClassDB::bind_method(D_METHOD("get_input_port_count"), &GraphNode::get_input_port_count);
  651. ClassDB::bind_method(D_METHOD("get_input_port_position", "port_idx"), &GraphNode::get_input_port_position);
  652. ClassDB::bind_method(D_METHOD("get_input_port_type", "port_idx"), &GraphNode::get_input_port_type);
  653. ClassDB::bind_method(D_METHOD("get_input_port_color", "port_idx"), &GraphNode::get_input_port_color);
  654. ClassDB::bind_method(D_METHOD("get_input_port_slot", "port_idx"), &GraphNode::get_input_port_slot);
  655. ClassDB::bind_method(D_METHOD("get_output_port_count"), &GraphNode::get_output_port_count);
  656. ClassDB::bind_method(D_METHOD("get_output_port_position", "port_idx"), &GraphNode::get_output_port_position);
  657. ClassDB::bind_method(D_METHOD("get_output_port_type", "port_idx"), &GraphNode::get_output_port_type);
  658. ClassDB::bind_method(D_METHOD("get_output_port_color", "port_idx"), &GraphNode::get_output_port_color);
  659. ClassDB::bind_method(D_METHOD("get_output_port_slot", "port_idx"), &GraphNode::get_output_port_slot);
  660. GDVIRTUAL_BIND(_draw_port, "slot_index", "position", "left", "color")
  661. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  662. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "slot_index")));
  663. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel);
  664. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_selected);
  665. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar);
  666. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar_selected);
  667. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot);
  668. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, separation);
  669. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, port_h_offset);
  670. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, port);
  671. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, resizer);
  672. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphNode, resizer_color);
  673. }
  674. GraphNode::GraphNode() {
  675. titlebar_hbox = memnew(HBoxContainer);
  676. titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  677. add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
  678. title_label = memnew(Label);
  679. title_label->set_theme_type_variation("GraphNodeTitleLabel");
  680. title_label->set_h_size_flags(SIZE_EXPAND_FILL);
  681. titlebar_hbox->add_child(title_label);
  682. set_mouse_filter(MOUSE_FILTER_STOP);
  683. }