graph_node.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. 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. Size2 GraphNode::get_minimum_size() const {
  489. Ref<StyleBox> sb_panel = theme_cache.panel;
  490. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  491. Ref<StyleBox> sb_slot = theme_cache.slot;
  492. int separation = theme_cache.separation;
  493. Size2 minsize = titlebar_hbox->get_minimum_size() + sb_titlebar->get_minimum_size();
  494. for (int i = 0; i < get_child_count(false); i++) {
  495. Control *child = Object::cast_to<Control>(get_child(i, false));
  496. if (!child || !child->is_visible() || child->is_set_as_top_level()) {
  497. continue;
  498. }
  499. Size2i size = child->get_combined_minimum_size();
  500. size.width += sb_panel->get_minimum_size().width;
  501. if (slot_table.has(i)) {
  502. size += slot_table[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2();
  503. }
  504. minsize.height += size.height;
  505. minsize.width = MAX(minsize.width, size.width);
  506. if (i > 0) {
  507. minsize.height += separation;
  508. }
  509. }
  510. minsize.height += sb_panel->get_minimum_size().height;
  511. return minsize;
  512. }
  513. void GraphNode::_port_pos_update() {
  514. int edgeofs = theme_cache.port_h_offset;
  515. int separation = theme_cache.separation;
  516. Ref<StyleBox> sb_panel = theme_cache.panel;
  517. Ref<StyleBox> sb_titlebar = theme_cache.titlebar;
  518. left_port_cache.clear();
  519. right_port_cache.clear();
  520. int vertical_ofs = titlebar_hbox->get_size().height + sb_titlebar->get_minimum_size().height + sb_panel->get_margin(SIDE_TOP);
  521. int slot_index = 0;
  522. for (int i = 0; i < get_child_count(false); i++) {
  523. Control *child = Object::cast_to<Control>(get_child(i, false));
  524. if (!child || child->is_set_as_top_level()) {
  525. continue;
  526. }
  527. Size2i size = child->get_rect().size;
  528. if (slot_table.has(slot_index)) {
  529. if (slot_table[slot_index].enable_left) {
  530. PortCache port_cache;
  531. port_cache.pos = Point2i(edgeofs, vertical_ofs + size.height / 2);
  532. port_cache.type = slot_table[slot_index].type_left;
  533. port_cache.color = slot_table[slot_index].color_left;
  534. port_cache.slot_index = slot_index;
  535. left_port_cache.push_back(port_cache);
  536. }
  537. if (slot_table[slot_index].enable_right) {
  538. PortCache port_cache;
  539. port_cache.pos = Point2i(get_size().width - edgeofs, vertical_ofs + size.height / 2);
  540. port_cache.type = slot_table[slot_index].type_right;
  541. port_cache.color = slot_table[slot_index].color_right;
  542. port_cache.slot_index = slot_index;
  543. right_port_cache.push_back(port_cache);
  544. }
  545. }
  546. vertical_ofs += separation;
  547. vertical_ofs += size.height;
  548. slot_index++;
  549. }
  550. port_pos_dirty = false;
  551. }
  552. int GraphNode::get_input_port_count() {
  553. if (port_pos_dirty) {
  554. _port_pos_update();
  555. }
  556. return left_port_cache.size();
  557. }
  558. int GraphNode::get_output_port_count() {
  559. if (port_pos_dirty) {
  560. _port_pos_update();
  561. }
  562. return right_port_cache.size();
  563. }
  564. Vector2 GraphNode::get_input_port_position(int p_port_idx) {
  565. if (port_pos_dirty) {
  566. _port_pos_update();
  567. }
  568. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), Vector2());
  569. Vector2 pos = left_port_cache[p_port_idx].pos;
  570. return pos;
  571. }
  572. int GraphNode::get_input_port_type(int p_port_idx) {
  573. if (port_pos_dirty) {
  574. _port_pos_update();
  575. }
  576. ERR_FAIL_INDEX_V(p_port_idx, left_port_cache.size(), 0);
  577. return left_port_cache[p_port_idx].type;
  578. }
  579. Color GraphNode::get_input_port_color(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(), Color());
  584. return left_port_cache[p_port_idx].color;
  585. }
  586. int GraphNode::get_input_port_slot(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(), -1);
  591. return left_port_cache[p_port_idx].slot_index;
  592. }
  593. Vector2 GraphNode::get_output_port_position(int p_port_idx) {
  594. if (port_pos_dirty) {
  595. _port_pos_update();
  596. }
  597. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), Vector2());
  598. Vector2 pos = right_port_cache[p_port_idx].pos;
  599. return pos;
  600. }
  601. int GraphNode::get_output_port_type(int p_port_idx) {
  602. if (port_pos_dirty) {
  603. _port_pos_update();
  604. }
  605. ERR_FAIL_INDEX_V(p_port_idx, right_port_cache.size(), 0);
  606. return right_port_cache[p_port_idx].type;
  607. }
  608. Color GraphNode::get_output_port_color(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(), Color());
  613. return right_port_cache[p_port_idx].color;
  614. }
  615. int GraphNode::get_output_port_slot(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(), -1);
  620. return right_port_cache[p_port_idx].slot_index;
  621. }
  622. void GraphNode::set_title(const String &p_title) {
  623. if (title == p_title) {
  624. return;
  625. }
  626. title = p_title;
  627. if (title_label) {
  628. title_label->set_text(title);
  629. }
  630. update_minimum_size();
  631. }
  632. String GraphNode::get_title() const {
  633. return title;
  634. }
  635. HBoxContainer *GraphNode::get_titlebar_hbox() {
  636. return titlebar_hbox;
  637. }
  638. Control::CursorShape GraphNode::get_cursor_shape(const Point2 &p_pos) const {
  639. if (resizable) {
  640. if (resizing || (p_pos.x > get_size().x - theme_cache.resizer->get_width() && p_pos.y > get_size().y - theme_cache.resizer->get_height())) {
  641. return CURSOR_FDIAGSIZE;
  642. }
  643. }
  644. return Control::get_cursor_shape(p_pos);
  645. }
  646. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  647. Vector<int> flags;
  648. flags.append(SIZE_FILL);
  649. flags.append(SIZE_SHRINK_BEGIN);
  650. flags.append(SIZE_SHRINK_CENTER);
  651. flags.append(SIZE_SHRINK_END);
  652. return flags;
  653. }
  654. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  655. Vector<int> flags;
  656. flags.append(SIZE_FILL);
  657. flags.append(SIZE_EXPAND);
  658. flags.append(SIZE_SHRINK_BEGIN);
  659. flags.append(SIZE_SHRINK_CENTER);
  660. flags.append(SIZE_SHRINK_END);
  661. return flags;
  662. }
  663. void GraphNode::_bind_methods() {
  664. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  665. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  666. ClassDB::bind_method(D_METHOD("get_titlebar_hbox"), &GraphNode::get_titlebar_hbox);
  667. 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));
  668. ClassDB::bind_method(D_METHOD("clear_slot", "slot_index"), &GraphNode::clear_slot);
  669. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  670. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "slot_index"), &GraphNode::is_slot_enabled_left);
  671. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "slot_index", "enable"), &GraphNode::set_slot_enabled_left);
  672. ClassDB::bind_method(D_METHOD("set_slot_type_left", "slot_index", "type"), &GraphNode::set_slot_type_left);
  673. ClassDB::bind_method(D_METHOD("get_slot_type_left", "slot_index"), &GraphNode::get_slot_type_left);
  674. ClassDB::bind_method(D_METHOD("set_slot_color_left", "slot_index", "color"), &GraphNode::set_slot_color_left);
  675. ClassDB::bind_method(D_METHOD("get_slot_color_left", "slot_index"), &GraphNode::get_slot_color_left);
  676. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_left", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_left);
  677. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_left", "slot_index"), &GraphNode::get_slot_custom_icon_left);
  678. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "slot_index"), &GraphNode::is_slot_enabled_right);
  679. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "slot_index", "enable"), &GraphNode::set_slot_enabled_right);
  680. ClassDB::bind_method(D_METHOD("set_slot_type_right", "slot_index", "type"), &GraphNode::set_slot_type_right);
  681. ClassDB::bind_method(D_METHOD("get_slot_type_right", "slot_index"), &GraphNode::get_slot_type_right);
  682. ClassDB::bind_method(D_METHOD("set_slot_color_right", "slot_index", "color"), &GraphNode::set_slot_color_right);
  683. ClassDB::bind_method(D_METHOD("get_slot_color_right", "slot_index"), &GraphNode::get_slot_color_right);
  684. ClassDB::bind_method(D_METHOD("set_slot_custom_icon_right", "slot_index", "custom_icon"), &GraphNode::set_slot_custom_icon_right);
  685. ClassDB::bind_method(D_METHOD("get_slot_custom_icon_right", "slot_index"), &GraphNode::get_slot_custom_icon_right);
  686. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "slot_index"), &GraphNode::is_slot_draw_stylebox);
  687. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "slot_index", "enable"), &GraphNode::set_slot_draw_stylebox);
  688. ClassDB::bind_method(D_METHOD("get_input_port_count"), &GraphNode::get_input_port_count);
  689. ClassDB::bind_method(D_METHOD("get_input_port_position", "port_idx"), &GraphNode::get_input_port_position);
  690. ClassDB::bind_method(D_METHOD("get_input_port_type", "port_idx"), &GraphNode::get_input_port_type);
  691. ClassDB::bind_method(D_METHOD("get_input_port_color", "port_idx"), &GraphNode::get_input_port_color);
  692. ClassDB::bind_method(D_METHOD("get_input_port_slot", "port_idx"), &GraphNode::get_input_port_slot);
  693. ClassDB::bind_method(D_METHOD("get_output_port_count"), &GraphNode::get_output_port_count);
  694. ClassDB::bind_method(D_METHOD("get_output_port_position", "port_idx"), &GraphNode::get_output_port_position);
  695. ClassDB::bind_method(D_METHOD("get_output_port_type", "port_idx"), &GraphNode::get_output_port_type);
  696. ClassDB::bind_method(D_METHOD("get_output_port_color", "port_idx"), &GraphNode::get_output_port_color);
  697. ClassDB::bind_method(D_METHOD("get_output_port_slot", "port_idx"), &GraphNode::get_output_port_slot);
  698. GDVIRTUAL_BIND(_draw_port, "slot_index", "position", "left", "color")
  699. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  700. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "slot_index")));
  701. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel);
  702. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, panel_selected);
  703. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar);
  704. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, titlebar_selected);
  705. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphNode, slot);
  706. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, separation);
  707. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphNode, port_h_offset);
  708. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, port);
  709. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphNode, resizer);
  710. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphNode, resizer_color);
  711. }
  712. GraphNode::GraphNode() {
  713. titlebar_hbox = memnew(HBoxContainer);
  714. titlebar_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  715. add_child(titlebar_hbox, false, INTERNAL_MODE_FRONT);
  716. title_label = memnew(Label);
  717. title_label->set_theme_type_variation("GraphNodeTitleLabel");
  718. title_label->set_h_size_flags(SIZE_EXPAND_FILL);
  719. titlebar_hbox->add_child(title_label);
  720. set_mouse_filter(MOUSE_FILTER_STOP);
  721. }