graph_node.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*************************************************************************/
  2. /* graph_node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/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 sucessively 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(p_idx < 0);
  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. int GraphNode::get_slot_type_left(int p_idx) const {
  361. if (!slot_info.has(p_idx)) {
  362. return 0;
  363. }
  364. return slot_info[p_idx].type_left;
  365. }
  366. Color GraphNode::get_slot_color_left(int p_idx) const {
  367. if (!slot_info.has(p_idx)) {
  368. return Color(1, 1, 1, 1);
  369. }
  370. return slot_info[p_idx].color_left;
  371. }
  372. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  373. if (!slot_info.has(p_idx)) {
  374. return false;
  375. }
  376. return slot_info[p_idx].enable_right;
  377. }
  378. int GraphNode::get_slot_type_right(int p_idx) const {
  379. if (!slot_info.has(p_idx)) {
  380. return 0;
  381. }
  382. return slot_info[p_idx].type_right;
  383. }
  384. Color GraphNode::get_slot_color_right(int p_idx) const {
  385. if (!slot_info.has(p_idx)) {
  386. return Color(1, 1, 1, 1);
  387. }
  388. return slot_info[p_idx].color_right;
  389. }
  390. Size2 GraphNode::get_minimum_size() const {
  391. Ref<Font> title_font = get_font("title_font");
  392. int sep = get_constant("separation");
  393. Ref<StyleBox> sb = get_stylebox("frame");
  394. bool first = true;
  395. Size2 minsize;
  396. minsize.x = title_font->get_string_size(title).x;
  397. if (show_close) {
  398. Ref<Texture> close = get_icon("close");
  399. minsize.x += sep + close->get_width();
  400. }
  401. for (int i = 0; i < get_child_count(); i++) {
  402. Control *c = Object::cast_to<Control>(get_child(i));
  403. if (!c) {
  404. continue;
  405. }
  406. if (c->is_set_as_toplevel()) {
  407. continue;
  408. }
  409. Size2i size = c->get_combined_minimum_size();
  410. minsize.y += size.y;
  411. minsize.x = MAX(minsize.x, size.x);
  412. if (first) {
  413. first = false;
  414. } else {
  415. minsize.y += sep;
  416. }
  417. }
  418. return minsize + sb->get_minimum_size();
  419. }
  420. void GraphNode::set_title(const String &p_title) {
  421. if (title == p_title) {
  422. return;
  423. }
  424. title = p_title;
  425. update();
  426. _change_notify("title");
  427. minimum_size_changed();
  428. }
  429. String GraphNode::get_title() const {
  430. return title;
  431. }
  432. void GraphNode::set_offset(const Vector2 &p_offset) {
  433. offset = p_offset;
  434. emit_signal("offset_changed");
  435. update();
  436. }
  437. Vector2 GraphNode::get_offset() const {
  438. return offset;
  439. }
  440. void GraphNode::set_selected(bool p_selected) {
  441. selected = p_selected;
  442. update();
  443. }
  444. bool GraphNode::is_selected() {
  445. return selected;
  446. }
  447. void GraphNode::set_drag(bool p_drag) {
  448. if (p_drag) {
  449. drag_from = get_offset();
  450. } else {
  451. emit_signal("dragged", drag_from, get_offset()); //useful for undo/redo
  452. }
  453. }
  454. Vector2 GraphNode::get_drag_from() {
  455. return drag_from;
  456. }
  457. void GraphNode::set_show_close_button(bool p_enable) {
  458. show_close = p_enable;
  459. update();
  460. }
  461. bool GraphNode::is_close_button_visible() const {
  462. return show_close;
  463. }
  464. void GraphNode::_connpos_update() {
  465. int edgeofs = get_constant("port_offset");
  466. int sep = get_constant("separation");
  467. Ref<StyleBox> sb = get_stylebox("frame");
  468. conn_input_cache.clear();
  469. conn_output_cache.clear();
  470. int vofs = 0;
  471. int idx = 0;
  472. for (int i = 0; i < get_child_count(); i++) {
  473. Control *c = Object::cast_to<Control>(get_child(i));
  474. if (!c) {
  475. continue;
  476. }
  477. if (c->is_set_as_toplevel()) {
  478. continue;
  479. }
  480. Size2i size = c->get_combined_minimum_size();
  481. int y = sb->get_margin(MARGIN_TOP) + vofs;
  482. int h = size.y;
  483. if (slot_info.has(idx)) {
  484. if (slot_info[idx].enable_left) {
  485. ConnCache cc;
  486. cc.pos = Point2i(edgeofs, y + h / 2);
  487. cc.type = slot_info[idx].type_left;
  488. cc.color = slot_info[idx].color_left;
  489. conn_input_cache.push_back(cc);
  490. }
  491. if (slot_info[idx].enable_right) {
  492. ConnCache cc;
  493. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  494. cc.type = slot_info[idx].type_right;
  495. cc.color = slot_info[idx].color_right;
  496. conn_output_cache.push_back(cc);
  497. }
  498. }
  499. vofs += sep;
  500. vofs += size.y;
  501. idx++;
  502. }
  503. connpos_dirty = false;
  504. }
  505. int GraphNode::get_connection_input_count() {
  506. if (connpos_dirty) {
  507. _connpos_update();
  508. }
  509. return conn_input_cache.size();
  510. }
  511. int GraphNode::get_connection_output_count() {
  512. if (connpos_dirty) {
  513. _connpos_update();
  514. }
  515. return conn_output_cache.size();
  516. }
  517. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  518. if (connpos_dirty) {
  519. _connpos_update();
  520. }
  521. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  522. Vector2 pos = conn_input_cache[p_idx].pos;
  523. pos.x *= get_scale().x;
  524. pos.y *= get_scale().y;
  525. return pos;
  526. }
  527. int GraphNode::get_connection_input_type(int p_idx) {
  528. if (connpos_dirty) {
  529. _connpos_update();
  530. }
  531. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  532. return conn_input_cache[p_idx].type;
  533. }
  534. Color GraphNode::get_connection_input_color(int p_idx) {
  535. if (connpos_dirty) {
  536. _connpos_update();
  537. }
  538. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  539. return conn_input_cache[p_idx].color;
  540. }
  541. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  542. if (connpos_dirty) {
  543. _connpos_update();
  544. }
  545. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  546. Vector2 pos = conn_output_cache[p_idx].pos;
  547. pos.x *= get_scale().x;
  548. pos.y *= get_scale().y;
  549. return pos;
  550. }
  551. int GraphNode::get_connection_output_type(int p_idx) {
  552. if (connpos_dirty) {
  553. _connpos_update();
  554. }
  555. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  556. return conn_output_cache[p_idx].type;
  557. }
  558. Color GraphNode::get_connection_output_color(int p_idx) {
  559. if (connpos_dirty) {
  560. _connpos_update();
  561. }
  562. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  563. return conn_output_cache[p_idx].color;
  564. }
  565. void GraphNode::_gui_input(const Ref<InputEvent> &p_ev) {
  566. Ref<InputEventMouseButton> mb = p_ev;
  567. if (mb.is_valid()) {
  568. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  569. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  570. Vector2 mpos = Vector2(mb->get_position().x, mb->get_position().y);
  571. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  572. //send focus to parent
  573. get_parent_control()->grab_focus();
  574. emit_signal("close_request");
  575. accept_event();
  576. return;
  577. }
  578. Ref<Texture> resizer = get_icon("resizer");
  579. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  580. resizing = true;
  581. resizing_from = mpos;
  582. resizing_from_size = get_size();
  583. accept_event();
  584. return;
  585. }
  586. emit_signal("raise_request");
  587. }
  588. if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  589. resizing = false;
  590. }
  591. }
  592. Ref<InputEventMouseMotion> mm = p_ev;
  593. if (resizing && mm.is_valid()) {
  594. Vector2 mpos = mm->get_position();
  595. Vector2 diff = mpos - resizing_from;
  596. emit_signal("resize_request", resizing_from_size + diff);
  597. }
  598. }
  599. void GraphNode::set_overlay(Overlay p_overlay) {
  600. overlay = p_overlay;
  601. update();
  602. }
  603. GraphNode::Overlay GraphNode::get_overlay() const {
  604. return overlay;
  605. }
  606. void GraphNode::set_comment(bool p_enable) {
  607. comment = p_enable;
  608. update();
  609. }
  610. bool GraphNode::is_comment() const {
  611. return comment;
  612. }
  613. void GraphNode::set_resizable(bool p_enable) {
  614. resizable = p_enable;
  615. update();
  616. }
  617. bool GraphNode::is_resizable() const {
  618. return resizable;
  619. }
  620. void GraphNode::_bind_methods() {
  621. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  622. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  623. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphNode::_gui_input);
  624. 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>()));
  625. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  626. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  627. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  628. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  629. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  630. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  631. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  632. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  633. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &GraphNode::set_offset);
  634. ClassDB::bind_method(D_METHOD("get_offset"), &GraphNode::get_offset);
  635. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  636. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  637. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  638. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  639. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  640. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  641. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  642. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  643. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  644. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  645. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  646. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  647. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  648. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  649. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  650. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  651. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  652. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  653. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  654. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  655. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  656. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  657. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  658. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  659. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  660. ADD_SIGNAL(MethodInfo("offset_changed"));
  661. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  662. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  663. ADD_SIGNAL(MethodInfo("raise_request"));
  664. ADD_SIGNAL(MethodInfo("close_request"));
  665. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  666. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  667. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  668. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  669. }
  670. GraphNode::GraphNode() {
  671. overlay = OVERLAY_DISABLED;
  672. show_close = false;
  673. connpos_dirty = true;
  674. set_mouse_filter(MOUSE_FILTER_STOP);
  675. comment = false;
  676. resizable = false;
  677. resizing = false;
  678. selected = false;
  679. }