graph_node.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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/method_bind_ext.gen.inc"
  32. struct _MinSizeCache {
  33. int min_size;
  34. bool will_stretch;
  35. int final_size;
  36. };
  37. bool GraphNode::_set(const StringName &p_name, const Variant &p_value) {
  38. if (!p_name.operator String().begins_with("slot/")) {
  39. return false;
  40. }
  41. int idx = p_name.operator String().get_slice("/", 1).to_int();
  42. String what = p_name.operator String().get_slice("/", 2);
  43. Slot si;
  44. if (slot_info.has(idx)) {
  45. si = slot_info[idx];
  46. }
  47. if (what == "left_enabled") {
  48. si.enable_left = p_value;
  49. } else if (what == "left_type") {
  50. si.type_left = p_value;
  51. } else if (what == "left_color") {
  52. si.color_left = p_value;
  53. } else if (what == "right_enabled") {
  54. si.enable_right = p_value;
  55. } else if (what == "right_type") {
  56. si.type_right = p_value;
  57. } else if (what == "right_color") {
  58. si.color_right = p_value;
  59. } else {
  60. return false;
  61. }
  62. set_slot(idx, si.enable_left, si.type_left, si.color_left, si.enable_right, si.type_right, si.color_right);
  63. update();
  64. return true;
  65. }
  66. bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const {
  67. if (!p_name.operator String().begins_with("slot/")) {
  68. return false;
  69. }
  70. int idx = p_name.operator String().get_slice("/", 1).to_int();
  71. String what = p_name.operator String().get_slice("/", 2);
  72. Slot si;
  73. if (slot_info.has(idx)) {
  74. si = slot_info[idx];
  75. }
  76. if (what == "left_enabled") {
  77. r_ret = si.enable_left;
  78. } else if (what == "left_type") {
  79. r_ret = si.type_left;
  80. } else if (what == "left_color") {
  81. r_ret = si.color_left;
  82. } else if (what == "right_enabled") {
  83. r_ret = si.enable_right;
  84. } else if (what == "right_type") {
  85. r_ret = si.type_right;
  86. } else if (what == "right_color") {
  87. r_ret = si.color_right;
  88. } else {
  89. return false;
  90. }
  91. return true;
  92. }
  93. void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const {
  94. int idx = 0;
  95. for (int i = 0; i < get_child_count(); i++) {
  96. Control *c = Object::cast_to<Control>(get_child(i));
  97. if (!c || c->is_set_as_toplevel()) {
  98. continue;
  99. }
  100. String base = "slot/" + itos(idx) + "/";
  101. p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled"));
  102. p_list->push_back(PropertyInfo(Variant::INT, base + "left_type"));
  103. p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color"));
  104. p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled"));
  105. p_list->push_back(PropertyInfo(Variant::INT, base + "right_type"));
  106. p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color"));
  107. idx++;
  108. }
  109. }
  110. void GraphNode::_resort() {
  111. /** First pass, determine minimum size AND amount of stretchable elements */
  112. Size2 new_size = get_size();
  113. Ref<StyleBox> sb = get_stylebox("frame");
  114. int sep = get_constant("separation");
  115. bool first = true;
  116. int children_count = 0;
  117. int stretch_min = 0;
  118. int stretch_avail = 0;
  119. float stretch_ratio_total = 0;
  120. Map<Control *, _MinSizeCache> min_size_cache;
  121. for (int i = 0; i < get_child_count(); i++) {
  122. Control *c = Object::cast_to<Control>(get_child(i));
  123. if (!c || !c->is_visible_in_tree()) {
  124. continue;
  125. }
  126. if (c->is_set_as_toplevel()) {
  127. continue;
  128. }
  129. Size2i size = c->get_combined_minimum_size();
  130. _MinSizeCache msc;
  131. stretch_min += size.height;
  132. msc.min_size = size.height;
  133. msc.will_stretch = c->get_v_size_flags() & SIZE_EXPAND;
  134. if (msc.will_stretch) {
  135. stretch_avail += msc.min_size;
  136. stretch_ratio_total += c->get_stretch_ratio();
  137. }
  138. msc.final_size = msc.min_size;
  139. min_size_cache[c] = msc;
  140. children_count++;
  141. }
  142. if (children_count == 0) {
  143. return;
  144. }
  145. int stretch_max = new_size.height - (children_count - 1) * sep;
  146. int stretch_diff = stretch_max - stretch_min;
  147. if (stretch_diff < 0) {
  148. //avoid negative stretch space
  149. stretch_diff = 0;
  150. }
  151. stretch_avail += stretch_diff - sb->get_margin(MARGIN_BOTTOM) - sb->get_margin(MARGIN_TOP); //available stretch space.
  152. /** Second, pass successively to discard elements that can't be stretched, this will run while stretchable
  153. elements exist */
  154. while (stretch_ratio_total > 0) { // first of all, don't even be here if no stretchable objects exist
  155. bool refit_successful = true; //assume refit-test will go well
  156. for (int i = 0; i < get_child_count(); i++) {
  157. Control *c = Object::cast_to<Control>(get_child(i));
  158. if (!c || !c->is_visible_in_tree()) {
  159. continue;
  160. }
  161. if (c->is_set_as_toplevel()) {
  162. continue;
  163. }
  164. ERR_FAIL_COND(!min_size_cache.has(c));
  165. _MinSizeCache &msc = min_size_cache[c];
  166. if (msc.will_stretch) { //wants to stretch
  167. //let's see if it can really stretch
  168. int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  169. if (final_pixel_size < msc.min_size) {
  170. //if available stretching area is too small for widget,
  171. //then remove it from stretching area
  172. msc.will_stretch = false;
  173. stretch_ratio_total -= c->get_stretch_ratio();
  174. refit_successful = false;
  175. stretch_avail -= msc.min_size;
  176. msc.final_size = msc.min_size;
  177. break;
  178. } else {
  179. msc.final_size = final_pixel_size;
  180. }
  181. }
  182. }
  183. if (refit_successful) { //uf refit went well, break
  184. break;
  185. }
  186. }
  187. /** Final pass, draw and stretch elements **/
  188. int ofs = sb->get_margin(MARGIN_TOP);
  189. first = true;
  190. int idx = 0;
  191. cache_y.clear();
  192. int w = new_size.width - sb->get_minimum_size().x;
  193. for (int i = 0; i < get_child_count(); i++) {
  194. Control *c = Object::cast_to<Control>(get_child(i));
  195. if (!c || !c->is_visible_in_tree()) {
  196. continue;
  197. }
  198. if (c->is_set_as_toplevel()) {
  199. continue;
  200. }
  201. _MinSizeCache &msc = min_size_cache[c];
  202. if (first) {
  203. first = false;
  204. } else {
  205. ofs += sep;
  206. }
  207. int from = ofs;
  208. int to = ofs + msc.final_size;
  209. if (msc.will_stretch && idx == children_count - 1) {
  210. //adjust so the last one always fits perfect
  211. //compensating for numerical imprecision
  212. to = new_size.height - sb->get_margin(MARGIN_BOTTOM);
  213. }
  214. int size = to - from;
  215. Rect2 rect(sb->get_margin(MARGIN_LEFT), from, w, size);
  216. fit_child_in_rect(c, rect);
  217. cache_y.push_back(from - sb->get_margin(MARGIN_TOP) + size * 0.5);
  218. ofs = to;
  219. idx++;
  220. }
  221. update();
  222. connpos_dirty = true;
  223. }
  224. bool GraphNode::has_point(const Point2 &p_point) const {
  225. if (comment) {
  226. Ref<StyleBox> comment = get_stylebox("comment");
  227. Ref<Texture> resizer = get_icon("resizer");
  228. if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
  229. return true;
  230. }
  231. if (Rect2(0, 0, get_size().width, comment->get_margin(MARGIN_TOP)).has_point(p_point)) {
  232. return true;
  233. }
  234. return false;
  235. } else {
  236. return Control::has_point(p_point);
  237. }
  238. }
  239. void GraphNode::_notification(int p_what) {
  240. switch (p_what) {
  241. case NOTIFICATION_DRAW: {
  242. Ref<StyleBox> sb;
  243. if (comment) {
  244. sb = get_stylebox(selected ? "commentfocus" : "comment");
  245. } else {
  246. sb = get_stylebox(selected ? "selectedframe" : "frame");
  247. }
  248. //sb=sb->duplicate();
  249. //sb->call("set_modulate",modulate);
  250. Ref<Texture> port = get_icon("port");
  251. Ref<Texture> close = get_icon("close");
  252. Ref<Texture> resizer = get_icon("resizer");
  253. int close_offset = get_constant("close_offset");
  254. int close_h_offset = get_constant("close_h_offset");
  255. Color close_color = get_color("close_color");
  256. Color resizer_color = get_color("resizer_color");
  257. Ref<Font> title_font = get_font("title_font");
  258. int title_offset = get_constant("title_offset");
  259. int title_h_offset = get_constant("title_h_offset");
  260. Color title_color = get_color("title_color");
  261. Point2i icofs = -port->get_size() * 0.5;
  262. int edgeofs = get_constant("port_offset");
  263. icofs.y += sb->get_margin(MARGIN_TOP);
  264. draw_style_box(sb, Rect2(Point2(), get_size()));
  265. switch (overlay) {
  266. case OVERLAY_DISABLED: {
  267. } break;
  268. case OVERLAY_BREAKPOINT: {
  269. draw_style_box(get_stylebox("breakpoint"), Rect2(Point2(), get_size()));
  270. } break;
  271. case OVERLAY_POSITION: {
  272. draw_style_box(get_stylebox("position"), Rect2(Point2(), get_size()));
  273. } break;
  274. }
  275. int w = get_size().width - sb->get_minimum_size().x;
  276. if (show_close) {
  277. w -= close->get_width();
  278. }
  279. draw_string(title_font, Point2(sb->get_margin(MARGIN_LEFT) + title_h_offset, -title_font->get_height() + title_font->get_ascent() + title_offset), title, title_color, w);
  280. if (show_close) {
  281. Vector2 cpos = Point2(w + sb->get_margin(MARGIN_LEFT) + close_h_offset, -close->get_height() + close_offset);
  282. draw_texture(close, cpos, close_color);
  283. close_rect.position = cpos;
  284. close_rect.size = close->get_size();
  285. } else {
  286. close_rect = Rect2();
  287. }
  288. for (Map<int, Slot>::Element *E = slot_info.front(); E; E = E->next()) {
  289. if (E->key() < 0 || E->key() >= cache_y.size()) {
  290. continue;
  291. }
  292. if (!slot_info.has(E->key())) {
  293. continue;
  294. }
  295. const Slot &s = slot_info[E->key()];
  296. //left
  297. if (s.enable_left) {
  298. Ref<Texture> p = port;
  299. if (s.custom_slot_left.is_valid()) {
  300. p = s.custom_slot_left;
  301. }
  302. p->draw(get_canvas_item(), icofs + Point2(edgeofs, cache_y[E->key()]), s.color_left);
  303. }
  304. if (s.enable_right) {
  305. Ref<Texture> p = port;
  306. if (s.custom_slot_right.is_valid()) {
  307. p = s.custom_slot_right;
  308. }
  309. p->draw(get_canvas_item(), icofs + Point2(get_size().x - edgeofs, cache_y[E->key()]), s.color_right);
  310. }
  311. }
  312. if (resizable) {
  313. draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
  314. }
  315. } break;
  316. case NOTIFICATION_SORT_CHILDREN: {
  317. _resort();
  318. } break;
  319. case NOTIFICATION_THEME_CHANGED: {
  320. minimum_size_changed();
  321. } break;
  322. }
  323. }
  324. 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<Texture> &p_custom_left, const Ref<Texture> &p_custom_right) {
  325. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set slot with p_idx (%d) lesser than zero.", p_idx));
  326. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) && !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1)) {
  327. slot_info.erase(p_idx);
  328. return;
  329. }
  330. Slot s;
  331. s.enable_left = p_enable_left;
  332. s.type_left = p_type_left;
  333. s.color_left = p_color_left;
  334. s.enable_right = p_enable_right;
  335. s.type_right = p_type_right;
  336. s.color_right = p_color_right;
  337. s.custom_slot_left = p_custom_left;
  338. s.custom_slot_right = p_custom_right;
  339. slot_info[p_idx] = s;
  340. update();
  341. connpos_dirty = true;
  342. emit_signal("slot_updated", p_idx);
  343. }
  344. void GraphNode::clear_slot(int p_idx) {
  345. slot_info.erase(p_idx);
  346. update();
  347. connpos_dirty = true;
  348. }
  349. void GraphNode::clear_all_slots() {
  350. slot_info.clear();
  351. update();
  352. connpos_dirty = true;
  353. }
  354. bool GraphNode::is_slot_enabled_left(int p_idx) const {
  355. if (!slot_info.has(p_idx)) {
  356. return false;
  357. }
  358. return slot_info[p_idx].enable_left;
  359. }
  360. void GraphNode::set_slot_enabled_left(int p_idx, bool p_enable_left) {
  361. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_left for the slot with p_idx (%d) lesser than zero.", p_idx));
  362. slot_info[p_idx].enable_left = p_enable_left;
  363. update();
  364. connpos_dirty = true;
  365. emit_signal("slot_updated", p_idx);
  366. }
  367. void GraphNode::set_slot_type_left(int p_idx, int p_type_left) {
  368. 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));
  369. slot_info[p_idx].type_left = p_type_left;
  370. update();
  371. connpos_dirty = true;
  372. emit_signal("slot_updated", p_idx);
  373. }
  374. int GraphNode::get_slot_type_left(int p_idx) const {
  375. if (!slot_info.has(p_idx)) {
  376. return 0;
  377. }
  378. return slot_info[p_idx].type_left;
  379. }
  380. void GraphNode::set_slot_color_left(int p_idx, const Color &p_color_left) {
  381. 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));
  382. slot_info[p_idx].color_left = p_color_left;
  383. update();
  384. connpos_dirty = true;
  385. emit_signal("slot_updated", p_idx);
  386. }
  387. Color GraphNode::get_slot_color_left(int p_idx) const {
  388. if (!slot_info.has(p_idx)) {
  389. return Color(1, 1, 1, 1);
  390. }
  391. return slot_info[p_idx].color_left;
  392. }
  393. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  394. if (!slot_info.has(p_idx)) {
  395. return false;
  396. }
  397. return slot_info[p_idx].enable_right;
  398. }
  399. void GraphNode::set_slot_enabled_right(int p_idx, bool p_enable_right) {
  400. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_right for the slot with p_idx (%d) lesser than zero.", p_idx));
  401. slot_info[p_idx].enable_right = p_enable_right;
  402. update();
  403. connpos_dirty = true;
  404. emit_signal("slot_updated", p_idx);
  405. }
  406. void GraphNode::set_slot_type_right(int p_idx, int p_type_right) {
  407. 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));
  408. slot_info[p_idx].type_right = p_type_right;
  409. update();
  410. connpos_dirty = true;
  411. emit_signal("slot_updated", p_idx);
  412. }
  413. int GraphNode::get_slot_type_right(int p_idx) const {
  414. if (!slot_info.has(p_idx)) {
  415. return 0;
  416. }
  417. return slot_info[p_idx].type_right;
  418. }
  419. void GraphNode::set_slot_color_right(int p_idx, const Color &p_color_right) {
  420. 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));
  421. slot_info[p_idx].color_right = p_color_right;
  422. update();
  423. connpos_dirty = true;
  424. emit_signal("slot_updated", p_idx);
  425. }
  426. Color GraphNode::get_slot_color_right(int p_idx) const {
  427. if (!slot_info.has(p_idx)) {
  428. return Color(1, 1, 1, 1);
  429. }
  430. return slot_info[p_idx].color_right;
  431. }
  432. Size2 GraphNode::get_minimum_size() const {
  433. Ref<Font> title_font = get_font("title_font");
  434. int sep = get_constant("separation");
  435. Ref<StyleBox> sb = get_stylebox("frame");
  436. bool first = true;
  437. Size2 minsize;
  438. minsize.x = title_font->get_string_size(title).x;
  439. if (show_close) {
  440. Ref<Texture> close = get_icon("close");
  441. minsize.x += sep + close->get_width();
  442. }
  443. for (int i = 0; i < get_child_count(); i++) {
  444. Control *c = Object::cast_to<Control>(get_child(i));
  445. if (!c) {
  446. continue;
  447. }
  448. if (c->is_set_as_toplevel()) {
  449. continue;
  450. }
  451. Size2i size = c->get_combined_minimum_size();
  452. minsize.y += size.y;
  453. minsize.x = MAX(minsize.x, size.x);
  454. if (first) {
  455. first = false;
  456. } else {
  457. minsize.y += sep;
  458. }
  459. }
  460. return minsize + sb->get_minimum_size();
  461. }
  462. void GraphNode::set_title(const String &p_title) {
  463. if (title == p_title) {
  464. return;
  465. }
  466. title = p_title;
  467. update();
  468. _change_notify("title");
  469. minimum_size_changed();
  470. }
  471. String GraphNode::get_title() const {
  472. return title;
  473. }
  474. void GraphNode::set_offset(const Vector2 &p_offset) {
  475. offset = p_offset;
  476. emit_signal("offset_changed");
  477. update();
  478. }
  479. Vector2 GraphNode::get_offset() const {
  480. return offset;
  481. }
  482. void GraphNode::set_selected(bool p_selected) {
  483. selected = p_selected;
  484. update();
  485. }
  486. bool GraphNode::is_selected() {
  487. return selected;
  488. }
  489. void GraphNode::set_drag(bool p_drag) {
  490. if (p_drag) {
  491. drag_from = get_offset();
  492. } else {
  493. emit_signal("dragged", drag_from, get_offset()); //useful for undo/redo
  494. }
  495. }
  496. Vector2 GraphNode::get_drag_from() {
  497. return drag_from;
  498. }
  499. void GraphNode::set_show_close_button(bool p_enable) {
  500. show_close = p_enable;
  501. update();
  502. }
  503. bool GraphNode::is_close_button_visible() const {
  504. return show_close;
  505. }
  506. void GraphNode::_connpos_update() {
  507. int edgeofs = get_constant("port_offset");
  508. int sep = get_constant("separation");
  509. Ref<StyleBox> sb = get_stylebox("frame");
  510. conn_input_cache.clear();
  511. conn_output_cache.clear();
  512. int vofs = 0;
  513. int idx = 0;
  514. for (int i = 0; i < get_child_count(); i++) {
  515. Control *c = Object::cast_to<Control>(get_child(i));
  516. if (!c) {
  517. continue;
  518. }
  519. if (c->is_set_as_toplevel()) {
  520. continue;
  521. }
  522. Size2i size = c->get_rect().size;
  523. int y = sb->get_margin(MARGIN_TOP) + vofs;
  524. int h = size.y;
  525. if (slot_info.has(idx)) {
  526. if (slot_info[idx].enable_left) {
  527. ConnCache cc;
  528. cc.pos = Point2i(edgeofs, y + h / 2);
  529. cc.type = slot_info[idx].type_left;
  530. cc.color = slot_info[idx].color_left;
  531. conn_input_cache.push_back(cc);
  532. }
  533. if (slot_info[idx].enable_right) {
  534. ConnCache cc;
  535. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  536. cc.type = slot_info[idx].type_right;
  537. cc.color = slot_info[idx].color_right;
  538. conn_output_cache.push_back(cc);
  539. }
  540. }
  541. vofs += sep;
  542. vofs += size.y;
  543. idx++;
  544. }
  545. connpos_dirty = false;
  546. }
  547. int GraphNode::get_connection_input_count() {
  548. if (connpos_dirty) {
  549. _connpos_update();
  550. }
  551. return conn_input_cache.size();
  552. }
  553. int GraphNode::get_connection_output_count() {
  554. if (connpos_dirty) {
  555. _connpos_update();
  556. }
  557. return conn_output_cache.size();
  558. }
  559. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  560. if (connpos_dirty) {
  561. _connpos_update();
  562. }
  563. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  564. Vector2 pos = conn_input_cache[p_idx].pos;
  565. pos.x *= get_scale().x;
  566. pos.y *= get_scale().y;
  567. return pos;
  568. }
  569. int GraphNode::get_connection_input_type(int p_idx) {
  570. if (connpos_dirty) {
  571. _connpos_update();
  572. }
  573. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  574. return conn_input_cache[p_idx].type;
  575. }
  576. Color GraphNode::get_connection_input_color(int p_idx) {
  577. if (connpos_dirty) {
  578. _connpos_update();
  579. }
  580. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  581. return conn_input_cache[p_idx].color;
  582. }
  583. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  584. if (connpos_dirty) {
  585. _connpos_update();
  586. }
  587. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  588. Vector2 pos = conn_output_cache[p_idx].pos;
  589. pos.x *= get_scale().x;
  590. pos.y *= get_scale().y;
  591. return pos;
  592. }
  593. int GraphNode::get_connection_output_type(int p_idx) {
  594. if (connpos_dirty) {
  595. _connpos_update();
  596. }
  597. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  598. return conn_output_cache[p_idx].type;
  599. }
  600. Color GraphNode::get_connection_output_color(int p_idx) {
  601. if (connpos_dirty) {
  602. _connpos_update();
  603. }
  604. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  605. return conn_output_cache[p_idx].color;
  606. }
  607. void GraphNode::_gui_input(const Ref<InputEvent> &p_ev) {
  608. Ref<InputEventMouseButton> mb = p_ev;
  609. if (mb.is_valid()) {
  610. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  611. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  612. Vector2 mpos = Vector2(mb->get_position().x, mb->get_position().y);
  613. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  614. //send focus to parent
  615. get_parent_control()->grab_focus();
  616. emit_signal("close_request");
  617. accept_event();
  618. return;
  619. }
  620. Ref<Texture> resizer = get_icon("resizer");
  621. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  622. resizing = true;
  623. resizing_from = mpos;
  624. resizing_from_size = get_size();
  625. accept_event();
  626. return;
  627. }
  628. emit_signal("raise_request");
  629. }
  630. if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  631. resizing = false;
  632. }
  633. }
  634. Ref<InputEventMouseMotion> mm = p_ev;
  635. if (resizing && mm.is_valid()) {
  636. Vector2 mpos = mm->get_position();
  637. Vector2 diff = mpos - resizing_from;
  638. emit_signal("resize_request", resizing_from_size + diff);
  639. }
  640. }
  641. void GraphNode::set_overlay(Overlay p_overlay) {
  642. overlay = p_overlay;
  643. update();
  644. }
  645. GraphNode::Overlay GraphNode::get_overlay() const {
  646. return overlay;
  647. }
  648. void GraphNode::set_comment(bool p_enable) {
  649. comment = p_enable;
  650. update();
  651. }
  652. bool GraphNode::is_comment() const {
  653. return comment;
  654. }
  655. void GraphNode::set_resizable(bool p_enable) {
  656. resizable = p_enable;
  657. update();
  658. }
  659. bool GraphNode::is_resizable() const {
  660. return resizable;
  661. }
  662. void GraphNode::_bind_methods() {
  663. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  664. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  665. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphNode::_gui_input);
  666. ClassDB::bind_method(D_METHOD("set_slot", "idx", "enable_left", "type_left", "color_left", "enable_right", "type_right", "color_right", "custom_left", "custom_right"), &GraphNode::set_slot, DEFVAL(Ref<Texture>()), DEFVAL(Ref<Texture>()));
  667. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  668. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  669. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  670. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "idx", "enable_left"), &GraphNode::set_slot_enabled_left);
  671. ClassDB::bind_method(D_METHOD("set_slot_type_left", "idx", "type_left"), &GraphNode::set_slot_type_left);
  672. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  673. ClassDB::bind_method(D_METHOD("set_slot_color_left", "idx", "color_left"), &GraphNode::set_slot_color_left);
  674. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  675. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  676. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "idx", "enable_right"), &GraphNode::set_slot_enabled_right);
  677. ClassDB::bind_method(D_METHOD("set_slot_type_right", "idx", "type_right"), &GraphNode::set_slot_type_right);
  678. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  679. ClassDB::bind_method(D_METHOD("set_slot_color_right", "idx", "color_right"), &GraphNode::set_slot_color_right);
  680. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  681. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &GraphNode::set_offset);
  682. ClassDB::bind_method(D_METHOD("get_offset"), &GraphNode::get_offset);
  683. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  684. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  685. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  686. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  687. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  688. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  689. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  690. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  691. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  692. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  693. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  694. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  695. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  696. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  697. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  698. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  699. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  700. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  701. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  702. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  703. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  704. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  705. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  706. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  707. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  708. ADD_SIGNAL(MethodInfo("offset_changed"));
  709. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  710. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  711. ADD_SIGNAL(MethodInfo("raise_request"));
  712. ADD_SIGNAL(MethodInfo("close_request"));
  713. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  714. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  715. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  716. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  717. }
  718. GraphNode::GraphNode() {
  719. overlay = OVERLAY_DISABLED;
  720. show_close = false;
  721. connpos_dirty = true;
  722. set_mouse_filter(MOUSE_FILTER_STOP);
  723. comment = false;
  724. resizable = false;
  725. resizing = false;
  726. selected = false;
  727. }