graph_node.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*************************************************************************/
  2. /* graph_node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "graph_edit.h"
  33. struct _MinSizeCache {
  34. int min_size;
  35. bool will_stretch;
  36. int final_size;
  37. };
  38. bool GraphNode::_set(const StringName &p_name, const Variant &p_value) {
  39. String str = p_name;
  40. if (!str.begins_with("slot/")) {
  41. return false;
  42. }
  43. int idx = str.get_slice("/", 1).to_int();
  44. String what = str.get_slice("/", 2);
  45. Slot si;
  46. if (slot_info.has(idx)) {
  47. si = slot_info[idx];
  48. }
  49. if (what == "left_enabled") {
  50. si.enable_left = p_value;
  51. } else if (what == "left_type") {
  52. si.type_left = p_value;
  53. } else if (what == "left_icon") {
  54. si.custom_slot_left = p_value;
  55. } else if (what == "left_color") {
  56. si.color_left = p_value;
  57. } else if (what == "right_enabled") {
  58. si.enable_right = p_value;
  59. } else if (what == "right_type") {
  60. si.type_right = p_value;
  61. } else if (what == "right_color") {
  62. si.color_right = p_value;
  63. } else if (what == "right_icon") {
  64. si.custom_slot_right = p_value;
  65. } else if (what == "draw_stylebox") {
  66. si.draw_stylebox = p_value;
  67. } else {
  68. return false;
  69. }
  70. set_slot(idx, si.enable_left, si.type_left, si.color_left, si.enable_right, si.type_right, si.color_right, si.custom_slot_left, si.custom_slot_right, si.draw_stylebox);
  71. queue_redraw();
  72. return true;
  73. }
  74. bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const {
  75. String str = p_name;
  76. if (!str.begins_with("slot/")) {
  77. return false;
  78. }
  79. int idx = str.get_slice("/", 1).to_int();
  80. String what = str.get_slice("/", 2);
  81. Slot si;
  82. if (slot_info.has(idx)) {
  83. si = slot_info[idx];
  84. }
  85. if (what == "left_enabled") {
  86. r_ret = si.enable_left;
  87. } else if (what == "left_type") {
  88. r_ret = si.type_left;
  89. } else if (what == "left_color") {
  90. r_ret = si.color_left;
  91. } else if (what == "left_icon") {
  92. r_ret = si.custom_slot_left;
  93. } else if (what == "right_enabled") {
  94. r_ret = si.enable_right;
  95. } else if (what == "right_type") {
  96. r_ret = si.type_right;
  97. } else if (what == "right_color") {
  98. r_ret = si.color_right;
  99. } else if (what == "right_icon") {
  100. r_ret = si.custom_slot_right;
  101. } else if (what == "draw_stylebox") {
  102. r_ret = si.draw_stylebox;
  103. } else {
  104. return false;
  105. }
  106. return true;
  107. }
  108. void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const {
  109. int idx = 0;
  110. for (int i = 0; i < get_child_count(); i++) {
  111. Control *c = Object::cast_to<Control>(get_child(i));
  112. if (!c || c->is_set_as_top_level()) {
  113. continue;
  114. }
  115. String base = "slot/" + itos(idx) + "/";
  116. p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled"));
  117. p_list->push_back(PropertyInfo(Variant::INT, base + "left_type"));
  118. p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color"));
  119. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "left_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  120. p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled"));
  121. p_list->push_back(PropertyInfo(Variant::INT, base + "right_type"));
  122. p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color"));
  123. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  124. p_list->push_back(PropertyInfo(Variant::BOOL, base + "draw_stylebox"));
  125. idx++;
  126. }
  127. }
  128. void GraphNode::_resort() {
  129. /** First pass, determine minimum size AND amount of stretchable elements */
  130. Size2i new_size = get_size();
  131. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  132. Ref<StyleBox> sb_slot = get_theme_stylebox(SNAME("slot"));
  133. int sep = get_theme_constant(SNAME("separation"));
  134. bool first = true;
  135. int children_count = 0;
  136. int stretch_min = 0;
  137. int stretch_avail = 0;
  138. float stretch_ratio_total = 0;
  139. HashMap<Control *, _MinSizeCache> min_size_cache;
  140. for (int i = 0; i < get_child_count(); i++) {
  141. Control *c = Object::cast_to<Control>(get_child(i));
  142. if (!c || !c->is_visible_in_tree()) {
  143. continue;
  144. }
  145. if (c->is_set_as_top_level()) {
  146. continue;
  147. }
  148. Size2i size = c->get_combined_minimum_size() + (slot_info[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2());
  149. _MinSizeCache msc;
  150. stretch_min += size.height;
  151. msc.min_size = size.height;
  152. msc.will_stretch = c->get_v_size_flags() & SIZE_EXPAND;
  153. if (msc.will_stretch) {
  154. stretch_avail += msc.min_size;
  155. stretch_ratio_total += c->get_stretch_ratio();
  156. }
  157. msc.final_size = msc.min_size;
  158. min_size_cache[c] = msc;
  159. children_count++;
  160. }
  161. if (children_count == 0) {
  162. return;
  163. }
  164. int stretch_max = new_size.height - (children_count - 1) * sep;
  165. int stretch_diff = stretch_max - stretch_min;
  166. if (stretch_diff < 0) {
  167. //avoid negative stretch space
  168. stretch_diff = 0;
  169. }
  170. stretch_avail += stretch_diff - sb->get_margin(SIDE_BOTTOM) - sb->get_margin(SIDE_TOP); //available stretch space.
  171. /** Second, pass successively to discard elements that can't be stretched, this will run while stretchable
  172. elements exist */
  173. while (stretch_ratio_total > 0) { // first of all, don't even be here if no stretchable objects exist
  174. bool refit_successful = true; //assume refit-test will go well
  175. for (int i = 0; i < get_child_count(); i++) {
  176. Control *c = Object::cast_to<Control>(get_child(i));
  177. if (!c || !c->is_visible_in_tree()) {
  178. continue;
  179. }
  180. if (c->is_set_as_top_level()) {
  181. continue;
  182. }
  183. ERR_FAIL_COND(!min_size_cache.has(c));
  184. _MinSizeCache &msc = min_size_cache[c];
  185. if (msc.will_stretch) { //wants to stretch
  186. //let's see if it can really stretch
  187. int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  188. if (final_pixel_size < msc.min_size) {
  189. //if available stretching area is too small for widget,
  190. //then remove it from stretching area
  191. msc.will_stretch = false;
  192. stretch_ratio_total -= c->get_stretch_ratio();
  193. refit_successful = false;
  194. stretch_avail -= msc.min_size;
  195. msc.final_size = msc.min_size;
  196. break;
  197. } else {
  198. msc.final_size = final_pixel_size;
  199. }
  200. }
  201. }
  202. if (refit_successful) { //uf refit went well, break
  203. break;
  204. }
  205. }
  206. /** Final pass, draw and stretch elements **/
  207. int ofs = sb->get_margin(SIDE_TOP);
  208. first = true;
  209. int idx = 0;
  210. cache_y.clear();
  211. int w = new_size.width - sb->get_minimum_size().x;
  212. for (int i = 0; i < get_child_count(); i++) {
  213. Control *c = Object::cast_to<Control>(get_child(i));
  214. if (!c || !c->is_visible_in_tree()) {
  215. continue;
  216. }
  217. if (c->is_set_as_top_level()) {
  218. continue;
  219. }
  220. _MinSizeCache &msc = min_size_cache[c];
  221. if (first) {
  222. first = false;
  223. } else {
  224. ofs += sep;
  225. }
  226. int from = ofs;
  227. int to = ofs + msc.final_size;
  228. if (msc.will_stretch && idx == children_count - 1) {
  229. //adjust so the last one always fits perfect
  230. //compensating for numerical imprecision
  231. to = new_size.height - sb->get_margin(SIDE_BOTTOM);
  232. }
  233. int size = to - from;
  234. float margin = sb->get_margin(SIDE_LEFT) + (slot_info[i].draw_stylebox ? sb_slot->get_margin(SIDE_LEFT) : 0);
  235. float width = w - (slot_info[i].draw_stylebox ? sb_slot->get_minimum_size().x : 0);
  236. Rect2 rect(margin, from, width, size);
  237. fit_child_in_rect(c, rect);
  238. cache_y.push_back(from - sb->get_margin(SIDE_TOP) + size * 0.5);
  239. ofs = to;
  240. idx++;
  241. }
  242. queue_redraw();
  243. connpos_dirty = true;
  244. }
  245. bool GraphNode::has_point(const Point2 &p_point) const {
  246. if (comment) {
  247. Ref<StyleBox> comment = get_theme_stylebox(SNAME("comment"));
  248. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  249. if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
  250. return true;
  251. }
  252. if (Rect2(0, 0, get_size().width, comment->get_margin(SIDE_TOP)).has_point(p_point)) {
  253. return true;
  254. }
  255. return false;
  256. } else {
  257. return Control::has_point(p_point);
  258. }
  259. }
  260. void GraphNode::_notification(int p_what) {
  261. switch (p_what) {
  262. case NOTIFICATION_DRAW: {
  263. Ref<StyleBox> sb;
  264. if (comment) {
  265. sb = get_theme_stylebox(selected ? SNAME("comment_focus") : SNAME("comment"));
  266. } else {
  267. sb = get_theme_stylebox(selected ? SNAME("selected_frame") : SNAME("frame"));
  268. }
  269. Ref<StyleBox> sb_slot = get_theme_stylebox(SNAME("slot"));
  270. Ref<Texture2D> port = get_theme_icon(SNAME("port"));
  271. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  272. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  273. int close_offset = get_theme_constant(SNAME("close_offset"));
  274. int close_h_offset = get_theme_constant(SNAME("close_h_offset"));
  275. Color close_color = get_theme_color(SNAME("close_color"));
  276. Color resizer_color = get_theme_color(SNAME("resizer_color"));
  277. int title_offset = get_theme_constant(SNAME("title_offset"));
  278. int title_h_offset = get_theme_constant(SNAME("title_h_offset"));
  279. Color title_color = get_theme_color(SNAME("title_color"));
  280. Point2i icofs = -port->get_size() * 0.5;
  281. int edgeofs = get_theme_constant(SNAME("port_offset"));
  282. icofs.y += sb->get_margin(SIDE_TOP);
  283. draw_style_box(sb, Rect2(Point2(), get_size()));
  284. switch (overlay) {
  285. case OVERLAY_DISABLED: {
  286. } break;
  287. case OVERLAY_BREAKPOINT: {
  288. draw_style_box(get_theme_stylebox(SNAME("breakpoint")), Rect2(Point2(), get_size()));
  289. } break;
  290. case OVERLAY_POSITION: {
  291. draw_style_box(get_theme_stylebox(SNAME("position")), Rect2(Point2(), get_size()));
  292. } break;
  293. }
  294. int w = get_size().width - sb->get_minimum_size().x;
  295. title_buf->draw(get_canvas_item(), Point2(sb->get_margin(SIDE_LEFT) + title_h_offset, -title_buf->get_size().y + title_offset), title_color);
  296. if (show_close) {
  297. Vector2 cpos = Point2(w + sb->get_margin(SIDE_LEFT) + close_h_offset - close->get_width(), -close->get_height() + close_offset);
  298. draw_texture(close, cpos, close_color);
  299. close_rect.position = cpos;
  300. close_rect.size = close->get_size();
  301. } else {
  302. close_rect = Rect2();
  303. }
  304. for (const KeyValue<int, Slot> &E : slot_info) {
  305. if (E.key < 0 || E.key >= cache_y.size()) {
  306. continue;
  307. }
  308. if (!slot_info.has(E.key)) {
  309. continue;
  310. }
  311. const Slot &s = slot_info[E.key];
  312. // Left port.
  313. if (s.enable_left) {
  314. Ref<Texture2D> p = port;
  315. if (s.custom_slot_left.is_valid()) {
  316. p = s.custom_slot_left;
  317. }
  318. p->draw(get_canvas_item(), icofs + Point2(edgeofs, cache_y[E.key]), s.color_left);
  319. }
  320. // Right port.
  321. if (s.enable_right) {
  322. Ref<Texture2D> p = port;
  323. if (s.custom_slot_right.is_valid()) {
  324. p = s.custom_slot_right;
  325. }
  326. p->draw(get_canvas_item(), icofs + Point2(get_size().x - edgeofs, cache_y[E.key]), s.color_right);
  327. }
  328. // Draw slot stylebox.
  329. if (s.draw_stylebox) {
  330. Control *c = Object::cast_to<Control>(get_child(E.key));
  331. Rect2 c_rect = c->get_rect();
  332. c_rect.position.x = sb->get_margin(SIDE_LEFT);
  333. c_rect.size.width = w;
  334. draw_style_box(sb_slot, c_rect);
  335. }
  336. }
  337. if (resizable) {
  338. draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
  339. }
  340. } break;
  341. case NOTIFICATION_SORT_CHILDREN: {
  342. _resort();
  343. } break;
  344. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  345. case NOTIFICATION_TRANSLATION_CHANGED:
  346. case NOTIFICATION_THEME_CHANGED: {
  347. _shape();
  348. update_minimum_size();
  349. queue_redraw();
  350. } break;
  351. }
  352. }
  353. void GraphNode::_shape() {
  354. Ref<Font> font = get_theme_font(SNAME("title_font"));
  355. int font_size = get_theme_font_size(SNAME("title_font_size"));
  356. title_buf->clear();
  357. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  358. title_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  359. } else {
  360. title_buf->set_direction((TextServer::Direction)text_direction);
  361. }
  362. title_buf->add_string(title, font, font_size, language);
  363. }
  364. #ifdef TOOLS_ENABLED
  365. void GraphNode::_edit_set_position(const Point2 &p_position) {
  366. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  367. if (graph) {
  368. Point2 offset = (p_position + graph->get_scroll_ofs()) * graph->get_zoom();
  369. set_position_offset(offset);
  370. }
  371. set_position(p_position);
  372. }
  373. #endif
  374. void GraphNode::_validate_property(PropertyInfo &p_property) const {
  375. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  376. if (graph) {
  377. if (p_property.name == "position") {
  378. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  379. }
  380. }
  381. }
  382. void GraphNode::set_slot(int p_idx, 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) {
  383. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set slot with p_idx (%d) lesser than zero.", p_idx));
  384. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) &&
  385. !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) &&
  386. !p_custom_left.is_valid() && !p_custom_right.is_valid()) {
  387. slot_info.erase(p_idx);
  388. return;
  389. }
  390. Slot s;
  391. s.enable_left = p_enable_left;
  392. s.type_left = p_type_left;
  393. s.color_left = p_color_left;
  394. s.enable_right = p_enable_right;
  395. s.type_right = p_type_right;
  396. s.color_right = p_color_right;
  397. s.custom_slot_left = p_custom_left;
  398. s.custom_slot_right = p_custom_right;
  399. s.draw_stylebox = p_draw_stylebox;
  400. slot_info[p_idx] = s;
  401. queue_redraw();
  402. connpos_dirty = true;
  403. emit_signal(SNAME("slot_updated"), p_idx);
  404. }
  405. void GraphNode::clear_slot(int p_idx) {
  406. slot_info.erase(p_idx);
  407. queue_redraw();
  408. connpos_dirty = true;
  409. }
  410. void GraphNode::clear_all_slots() {
  411. slot_info.clear();
  412. queue_redraw();
  413. connpos_dirty = true;
  414. }
  415. bool GraphNode::is_slot_enabled_left(int p_idx) const {
  416. if (!slot_info.has(p_idx)) {
  417. return false;
  418. }
  419. return slot_info[p_idx].enable_left;
  420. }
  421. void GraphNode::set_slot_enabled_left(int p_idx, bool p_enable_left) {
  422. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_left for the slot with p_idx (%d) lesser than zero.", p_idx));
  423. if (slot_info[p_idx].enable_left == p_enable_left) {
  424. return;
  425. }
  426. slot_info[p_idx].enable_left = p_enable_left;
  427. queue_redraw();
  428. connpos_dirty = true;
  429. emit_signal(SNAME("slot_updated"), p_idx);
  430. }
  431. void GraphNode::set_slot_type_left(int p_idx, int p_type_left) {
  432. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set type_left for the slot '%d' because it hasn't been enabled.", p_idx));
  433. if (slot_info[p_idx].type_left == p_type_left) {
  434. return;
  435. }
  436. slot_info[p_idx].type_left = p_type_left;
  437. queue_redraw();
  438. connpos_dirty = true;
  439. emit_signal(SNAME("slot_updated"), p_idx);
  440. }
  441. int GraphNode::get_slot_type_left(int p_idx) const {
  442. if (!slot_info.has(p_idx)) {
  443. return 0;
  444. }
  445. return slot_info[p_idx].type_left;
  446. }
  447. void GraphNode::set_slot_color_left(int p_idx, const Color &p_color_left) {
  448. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set color_left for the slot '%d' because it hasn't been enabled.", p_idx));
  449. if (slot_info[p_idx].color_left == p_color_left) {
  450. return;
  451. }
  452. slot_info[p_idx].color_left = p_color_left;
  453. queue_redraw();
  454. connpos_dirty = true;
  455. emit_signal(SNAME("slot_updated"), p_idx);
  456. }
  457. Color GraphNode::get_slot_color_left(int p_idx) const {
  458. if (!slot_info.has(p_idx)) {
  459. return Color(1, 1, 1, 1);
  460. }
  461. return slot_info[p_idx].color_left;
  462. }
  463. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  464. if (!slot_info.has(p_idx)) {
  465. return false;
  466. }
  467. return slot_info[p_idx].enable_right;
  468. }
  469. void GraphNode::set_slot_enabled_right(int p_idx, bool p_enable_right) {
  470. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_right for the slot with p_idx (%d) lesser than zero.", p_idx));
  471. if (slot_info[p_idx].enable_right == p_enable_right) {
  472. return;
  473. }
  474. slot_info[p_idx].enable_right = p_enable_right;
  475. queue_redraw();
  476. connpos_dirty = true;
  477. emit_signal(SNAME("slot_updated"), p_idx);
  478. }
  479. void GraphNode::set_slot_type_right(int p_idx, int p_type_right) {
  480. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set type_right for the slot '%d' because it hasn't been enabled.", p_idx));
  481. if (slot_info[p_idx].type_right == p_type_right) {
  482. return;
  483. }
  484. slot_info[p_idx].type_right = p_type_right;
  485. queue_redraw();
  486. connpos_dirty = true;
  487. emit_signal(SNAME("slot_updated"), p_idx);
  488. }
  489. int GraphNode::get_slot_type_right(int p_idx) const {
  490. if (!slot_info.has(p_idx)) {
  491. return 0;
  492. }
  493. return slot_info[p_idx].type_right;
  494. }
  495. void GraphNode::set_slot_color_right(int p_idx, const Color &p_color_right) {
  496. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set color_right for the slot '%d' because it hasn't been enabled.", p_idx));
  497. if (slot_info[p_idx].color_right == p_color_right) {
  498. return;
  499. }
  500. slot_info[p_idx].color_right = p_color_right;
  501. queue_redraw();
  502. connpos_dirty = true;
  503. emit_signal(SNAME("slot_updated"), p_idx);
  504. }
  505. Color GraphNode::get_slot_color_right(int p_idx) const {
  506. if (!slot_info.has(p_idx)) {
  507. return Color(1, 1, 1, 1);
  508. }
  509. return slot_info[p_idx].color_right;
  510. }
  511. bool GraphNode::is_slot_draw_stylebox(int p_idx) const {
  512. if (!slot_info.has(p_idx)) {
  513. return false;
  514. }
  515. return slot_info[p_idx].draw_stylebox;
  516. }
  517. void GraphNode::set_slot_draw_stylebox(int p_idx, bool p_enable) {
  518. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set draw_stylebox for the slot with p_idx (%d) lesser than zero.", p_idx));
  519. slot_info[p_idx].draw_stylebox = p_enable;
  520. queue_redraw();
  521. connpos_dirty = true;
  522. emit_signal(SNAME("slot_updated"), p_idx);
  523. }
  524. Size2 GraphNode::get_minimum_size() const {
  525. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  526. Ref<StyleBox> sb_slot = get_theme_stylebox(SNAME("slot"));
  527. int sep = get_theme_constant(SNAME("separation"));
  528. int title_h_offset = get_theme_constant(SNAME("title_h_offset"));
  529. bool first = true;
  530. Size2 minsize;
  531. minsize.x = title_buf->get_size().x + title_h_offset;
  532. if (show_close) {
  533. int close_h_offset = get_theme_constant(SNAME("close_h_offset"));
  534. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  535. //TODO: Remove this magic number after GraphNode rework.
  536. minsize.x += 12 + close->get_width() + close_h_offset;
  537. }
  538. for (int i = 0; i < get_child_count(); i++) {
  539. Control *c = Object::cast_to<Control>(get_child(i));
  540. if (!c) {
  541. continue;
  542. }
  543. if (c->is_set_as_top_level()) {
  544. continue;
  545. }
  546. Size2i size = c->get_combined_minimum_size();
  547. if (slot_info.has(i)) {
  548. size += slot_info[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2();
  549. }
  550. minsize.y += size.y;
  551. minsize.x = MAX(minsize.x, size.x);
  552. if (first) {
  553. first = false;
  554. } else {
  555. minsize.y += sep;
  556. }
  557. }
  558. return minsize + sb->get_minimum_size();
  559. }
  560. void GraphNode::set_title(const String &p_title) {
  561. if (title == p_title) {
  562. return;
  563. }
  564. title = p_title;
  565. _shape();
  566. queue_redraw();
  567. update_minimum_size();
  568. }
  569. String GraphNode::get_title() const {
  570. return title;
  571. }
  572. void GraphNode::set_text_direction(Control::TextDirection p_text_direction) {
  573. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  574. if (text_direction != p_text_direction) {
  575. text_direction = p_text_direction;
  576. _shape();
  577. queue_redraw();
  578. }
  579. }
  580. Control::TextDirection GraphNode::get_text_direction() const {
  581. return text_direction;
  582. }
  583. void GraphNode::set_language(const String &p_language) {
  584. if (language != p_language) {
  585. language = p_language;
  586. _shape();
  587. queue_redraw();
  588. }
  589. }
  590. String GraphNode::get_language() const {
  591. return language;
  592. }
  593. void GraphNode::set_position_offset(const Vector2 &p_offset) {
  594. if (position_offset == p_offset) {
  595. return;
  596. }
  597. position_offset = p_offset;
  598. emit_signal(SNAME("position_offset_changed"));
  599. queue_redraw();
  600. }
  601. Vector2 GraphNode::get_position_offset() const {
  602. return position_offset;
  603. }
  604. void GraphNode::set_selected(bool p_selected) {
  605. if (selected == p_selected) {
  606. return;
  607. }
  608. selected = p_selected;
  609. queue_redraw();
  610. }
  611. bool GraphNode::is_selected() {
  612. return selected;
  613. }
  614. void GraphNode::set_drag(bool p_drag) {
  615. if (p_drag) {
  616. drag_from = get_position_offset();
  617. } else {
  618. emit_signal(SNAME("dragged"), drag_from, get_position_offset()); //useful for undo/redo
  619. }
  620. }
  621. Vector2 GraphNode::get_drag_from() {
  622. return drag_from;
  623. }
  624. void GraphNode::set_show_close_button(bool p_enable) {
  625. if (show_close == p_enable) {
  626. return;
  627. }
  628. show_close = p_enable;
  629. queue_redraw();
  630. }
  631. bool GraphNode::is_close_button_visible() const {
  632. return show_close;
  633. }
  634. void GraphNode::_connpos_update() {
  635. int edgeofs = get_theme_constant(SNAME("port_offset"));
  636. int sep = get_theme_constant(SNAME("separation"));
  637. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  638. conn_input_cache.clear();
  639. conn_output_cache.clear();
  640. int vofs = 0;
  641. int idx = 0;
  642. for (int i = 0; i < get_child_count(); i++) {
  643. Control *c = Object::cast_to<Control>(get_child(i));
  644. if (!c) {
  645. continue;
  646. }
  647. if (c->is_set_as_top_level()) {
  648. continue;
  649. }
  650. Size2i size = c->get_rect().size;
  651. int y = sb->get_margin(SIDE_TOP) + vofs;
  652. int h = size.y;
  653. if (slot_info.has(idx)) {
  654. if (slot_info[idx].enable_left) {
  655. ConnCache cc;
  656. cc.pos = Point2i(edgeofs, y + h / 2);
  657. cc.type = slot_info[idx].type_left;
  658. cc.color = slot_info[idx].color_left;
  659. cc.height = size.height;
  660. conn_input_cache.push_back(cc);
  661. }
  662. if (slot_info[idx].enable_right) {
  663. ConnCache cc;
  664. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  665. cc.type = slot_info[idx].type_right;
  666. cc.color = slot_info[idx].color_right;
  667. cc.height = size.height;
  668. conn_output_cache.push_back(cc);
  669. }
  670. }
  671. vofs += sep;
  672. vofs += size.y;
  673. idx++;
  674. }
  675. connpos_dirty = false;
  676. }
  677. int GraphNode::get_connection_input_count() {
  678. if (connpos_dirty) {
  679. _connpos_update();
  680. }
  681. return conn_input_cache.size();
  682. }
  683. int GraphNode::get_connection_input_height(int p_idx) {
  684. if (connpos_dirty) {
  685. _connpos_update();
  686. }
  687. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  688. return conn_input_cache[p_idx].height;
  689. }
  690. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  691. if (connpos_dirty) {
  692. _connpos_update();
  693. }
  694. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  695. Vector2 pos = conn_input_cache[p_idx].pos;
  696. pos.x *= get_scale().x;
  697. pos.y *= get_scale().y;
  698. return pos;
  699. }
  700. int GraphNode::get_connection_input_type(int p_idx) {
  701. if (connpos_dirty) {
  702. _connpos_update();
  703. }
  704. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  705. return conn_input_cache[p_idx].type;
  706. }
  707. Color GraphNode::get_connection_input_color(int p_idx) {
  708. if (connpos_dirty) {
  709. _connpos_update();
  710. }
  711. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  712. return conn_input_cache[p_idx].color;
  713. }
  714. int GraphNode::get_connection_output_count() {
  715. if (connpos_dirty) {
  716. _connpos_update();
  717. }
  718. return conn_output_cache.size();
  719. }
  720. int GraphNode::get_connection_output_height(int p_idx) {
  721. if (connpos_dirty) {
  722. _connpos_update();
  723. }
  724. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  725. return conn_output_cache[p_idx].height;
  726. }
  727. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  728. if (connpos_dirty) {
  729. _connpos_update();
  730. }
  731. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  732. Vector2 pos = conn_output_cache[p_idx].pos;
  733. pos.x *= get_scale().x;
  734. pos.y *= get_scale().y;
  735. return pos;
  736. }
  737. int GraphNode::get_connection_output_type(int p_idx) {
  738. if (connpos_dirty) {
  739. _connpos_update();
  740. }
  741. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  742. return conn_output_cache[p_idx].type;
  743. }
  744. Color GraphNode::get_connection_output_color(int p_idx) {
  745. if (connpos_dirty) {
  746. _connpos_update();
  747. }
  748. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  749. return conn_output_cache[p_idx].color;
  750. }
  751. void GraphNode::gui_input(const Ref<InputEvent> &p_ev) {
  752. ERR_FAIL_COND(p_ev.is_null());
  753. Ref<InputEventMouseButton> mb = p_ev;
  754. if (mb.is_valid()) {
  755. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  756. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  757. Vector2 mpos = mb->get_position();
  758. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  759. //send focus to parent
  760. get_parent_control()->grab_focus();
  761. emit_signal(SNAME("close_request"));
  762. accept_event();
  763. return;
  764. }
  765. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  766. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  767. resizing = true;
  768. resizing_from = mpos;
  769. resizing_from_size = get_size();
  770. accept_event();
  771. return;
  772. }
  773. emit_signal(SNAME("raise_request"));
  774. }
  775. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  776. resizing = false;
  777. }
  778. }
  779. Ref<InputEventMouseMotion> mm = p_ev;
  780. if (resizing && mm.is_valid()) {
  781. Vector2 mpos = mm->get_position();
  782. Vector2 diff = mpos - resizing_from;
  783. emit_signal(SNAME("resize_request"), resizing_from_size + diff);
  784. }
  785. }
  786. void GraphNode::set_overlay(Overlay p_overlay) {
  787. if (overlay == p_overlay) {
  788. return;
  789. }
  790. overlay = p_overlay;
  791. queue_redraw();
  792. }
  793. GraphNode::Overlay GraphNode::get_overlay() const {
  794. return overlay;
  795. }
  796. void GraphNode::set_comment(bool p_enable) {
  797. if (comment == p_enable) {
  798. return;
  799. }
  800. comment = p_enable;
  801. queue_redraw();
  802. }
  803. bool GraphNode::is_comment() const {
  804. return comment;
  805. }
  806. void GraphNode::set_resizable(bool p_enable) {
  807. if (resizable == p_enable) {
  808. return;
  809. }
  810. resizable = p_enable;
  811. queue_redraw();
  812. }
  813. bool GraphNode::is_resizable() const {
  814. return resizable;
  815. }
  816. void GraphNode::set_draggable(bool p_draggable) {
  817. draggable = p_draggable;
  818. }
  819. bool GraphNode::is_draggable() {
  820. return draggable;
  821. }
  822. void GraphNode::set_selectable(bool p_selectable) {
  823. selectable = p_selectable;
  824. }
  825. bool GraphNode::is_selectable() {
  826. return selectable;
  827. }
  828. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  829. Vector<int> flags;
  830. flags.append(SIZE_FILL);
  831. flags.append(SIZE_SHRINK_BEGIN);
  832. flags.append(SIZE_SHRINK_CENTER);
  833. flags.append(SIZE_SHRINK_END);
  834. return flags;
  835. }
  836. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  837. Vector<int> flags;
  838. flags.append(SIZE_FILL);
  839. flags.append(SIZE_EXPAND);
  840. flags.append(SIZE_SHRINK_BEGIN);
  841. flags.append(SIZE_SHRINK_CENTER);
  842. flags.append(SIZE_SHRINK_END);
  843. return flags;
  844. }
  845. void GraphNode::_bind_methods() {
  846. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  847. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  848. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &GraphNode::set_text_direction);
  849. ClassDB::bind_method(D_METHOD("get_text_direction"), &GraphNode::get_text_direction);
  850. ClassDB::bind_method(D_METHOD("set_language", "language"), &GraphNode::set_language);
  851. ClassDB::bind_method(D_METHOD("get_language"), &GraphNode::get_language);
  852. ClassDB::bind_method(D_METHOD("set_slot", "idx", "enable_left", "type_left", "color_left", "enable_right", "type_right", "color_right", "custom_left", "custom_right", "enable"), &GraphNode::set_slot, DEFVAL(Ref<Texture2D>()), DEFVAL(Ref<Texture2D>()), DEFVAL(true));
  853. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  854. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  855. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  856. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "idx", "enable_left"), &GraphNode::set_slot_enabled_left);
  857. ClassDB::bind_method(D_METHOD("set_slot_type_left", "idx", "type_left"), &GraphNode::set_slot_type_left);
  858. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  859. ClassDB::bind_method(D_METHOD("set_slot_color_left", "idx", "color_left"), &GraphNode::set_slot_color_left);
  860. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  861. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  862. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "idx", "enable_right"), &GraphNode::set_slot_enabled_right);
  863. ClassDB::bind_method(D_METHOD("set_slot_type_right", "idx", "type_right"), &GraphNode::set_slot_type_right);
  864. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  865. ClassDB::bind_method(D_METHOD("set_slot_color_right", "idx", "color_right"), &GraphNode::set_slot_color_right);
  866. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  867. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "idx"), &GraphNode::is_slot_draw_stylebox);
  868. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "idx", "draw_stylebox"), &GraphNode::set_slot_draw_stylebox);
  869. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &GraphNode::set_position_offset);
  870. ClassDB::bind_method(D_METHOD("get_position_offset"), &GraphNode::get_position_offset);
  871. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  872. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  873. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  874. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  875. ClassDB::bind_method(D_METHOD("set_draggable", "draggable"), &GraphNode::set_draggable);
  876. ClassDB::bind_method(D_METHOD("is_draggable"), &GraphNode::is_draggable);
  877. ClassDB::bind_method(D_METHOD("set_selectable", "selectable"), &GraphNode::set_selectable);
  878. ClassDB::bind_method(D_METHOD("is_selectable"), &GraphNode::is_selectable);
  879. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  880. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  881. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  882. ClassDB::bind_method(D_METHOD("get_connection_input_height", "idx"), &GraphNode::get_connection_input_height);
  883. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  884. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  885. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  886. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  887. ClassDB::bind_method(D_METHOD("get_connection_output_height", "idx"), &GraphNode::get_connection_output_height);
  888. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  889. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  890. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  891. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  892. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  893. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  894. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  895. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  896. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_position_offset", "get_position_offset");
  897. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  898. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  899. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draggable"), "set_draggable", "is_draggable");
  900. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selectable"), "set_selectable", "is_selectable");
  901. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  902. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  903. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  904. ADD_GROUP("BiDi", "");
  905. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  906. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  907. ADD_GROUP("", "");
  908. ADD_SIGNAL(MethodInfo("position_offset_changed"));
  909. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  910. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  911. ADD_SIGNAL(MethodInfo("raise_request"));
  912. ADD_SIGNAL(MethodInfo("close_request"));
  913. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  914. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  915. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  916. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  917. }
  918. GraphNode::GraphNode() {
  919. title_buf.instantiate();
  920. set_mouse_filter(MOUSE_FILTER_STOP);
  921. }