graph_node.cpp 33 KB

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