graph_node.cpp 35 KB

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