graph_edit.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_edit.h"
  31. #include "core/input/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "scene/gui/box_container.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/editor_scale.h"
  36. #endif
  37. #define ZOOM_SCALE 1.2
  38. #define MIN_ZOOM (((1 / ZOOM_SCALE) / ZOOM_SCALE) / ZOOM_SCALE)
  39. #define MAX_ZOOM (1 * ZOOM_SCALE * ZOOM_SCALE * ZOOM_SCALE)
  40. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  41. return ge->_filter_input(p_point);
  42. }
  43. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  44. ge = p_edit;
  45. }
  46. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  47. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  48. return OK;
  49. }
  50. Connection c;
  51. c.from = p_from;
  52. c.from_port = p_from_port;
  53. c.to = p_to;
  54. c.to_port = p_to_port;
  55. c.activity = 0;
  56. connections.push_back(c);
  57. top_layer->update();
  58. update();
  59. connections_layer->update();
  60. return OK;
  61. }
  62. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  63. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  64. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  71. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  72. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  73. connections.erase(E);
  74. top_layer->update();
  75. update();
  76. connections_layer->update();
  77. return;
  78. }
  79. }
  80. }
  81. bool GraphEdit::clips_input() const {
  82. return true;
  83. }
  84. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  85. *r_connections = connections;
  86. }
  87. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  88. setting_scroll_ofs = true;
  89. h_scroll->set_value(p_ofs.x);
  90. v_scroll->set_value(p_ofs.y);
  91. _update_scroll();
  92. setting_scroll_ofs = false;
  93. }
  94. Vector2 GraphEdit::get_scroll_ofs() const {
  95. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  96. }
  97. void GraphEdit::_scroll_moved(double) {
  98. if (!awaiting_scroll_offset_update) {
  99. call_deferred("_update_scroll_offset");
  100. awaiting_scroll_offset_update = true;
  101. }
  102. top_layer->update();
  103. update();
  104. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  105. emit_signal("scroll_offset_changed", get_scroll_ofs());
  106. }
  107. }
  108. void GraphEdit::_update_scroll_offset() {
  109. set_block_minimum_size_adjust(true);
  110. for (int i = 0; i < get_child_count(); i++) {
  111. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  112. if (!gn) {
  113. continue;
  114. }
  115. Point2 pos = gn->get_offset() * zoom;
  116. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  117. gn->set_position(pos);
  118. if (gn->get_scale() != Vector2(zoom, zoom)) {
  119. gn->set_scale(Vector2(zoom, zoom));
  120. }
  121. }
  122. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  123. set_block_minimum_size_adjust(false);
  124. awaiting_scroll_offset_update = false;
  125. }
  126. void GraphEdit::_update_scroll() {
  127. if (updating) {
  128. return;
  129. }
  130. updating = true;
  131. set_block_minimum_size_adjust(true);
  132. Rect2 screen;
  133. for (int i = 0; i < get_child_count(); i++) {
  134. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  135. if (!gn) {
  136. continue;
  137. }
  138. Rect2 r;
  139. r.position = gn->get_offset() * zoom;
  140. r.size = gn->get_size() * zoom;
  141. screen = screen.merge(r);
  142. }
  143. screen.position -= get_size();
  144. screen.size += get_size() * 2.0;
  145. h_scroll->set_min(screen.position.x);
  146. h_scroll->set_max(screen.position.x + screen.size.x);
  147. h_scroll->set_page(get_size().x);
  148. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  149. h_scroll->hide();
  150. } else {
  151. h_scroll->show();
  152. }
  153. v_scroll->set_min(screen.position.y);
  154. v_scroll->set_max(screen.position.y + screen.size.y);
  155. v_scroll->set_page(get_size().y);
  156. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  157. v_scroll->hide();
  158. } else {
  159. v_scroll->show();
  160. }
  161. Size2 hmin = h_scroll->get_combined_minimum_size();
  162. Size2 vmin = v_scroll->get_combined_minimum_size();
  163. // Avoid scrollbar overlapping.
  164. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  165. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  166. set_block_minimum_size_adjust(false);
  167. if (!awaiting_scroll_offset_update) {
  168. call_deferred("_update_scroll_offset");
  169. awaiting_scroll_offset_update = true;
  170. }
  171. updating = false;
  172. }
  173. void GraphEdit::_graph_node_raised(Node *p_gn) {
  174. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  175. ERR_FAIL_COND(!gn);
  176. if (gn->is_comment()) {
  177. move_child(gn, 0);
  178. } else {
  179. gn->raise();
  180. }
  181. int first_not_comment = 0;
  182. for (int i = 0; i < get_child_count(); i++) {
  183. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  184. if (gn2 && !gn2->is_comment()) {
  185. first_not_comment = i;
  186. break;
  187. }
  188. }
  189. move_child(connections_layer, first_not_comment);
  190. top_layer->raise();
  191. emit_signal("node_selected", p_gn);
  192. }
  193. void GraphEdit::_graph_node_moved(Node *p_gn) {
  194. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  195. ERR_FAIL_COND(!gn);
  196. top_layer->update();
  197. update();
  198. connections_layer->update();
  199. }
  200. void GraphEdit::add_child_notify(Node *p_child) {
  201. Control::add_child_notify(p_child);
  202. top_layer->call_deferred("raise"); //top layer always on top!
  203. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  204. if (gn) {
  205. gn->set_scale(Vector2(zoom, zoom));
  206. gn->connect("offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved), varray(gn));
  207. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised), varray(gn));
  208. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  209. _graph_node_moved(gn);
  210. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  211. }
  212. }
  213. void GraphEdit::remove_child_notify(Node *p_child) {
  214. Control::remove_child_notify(p_child);
  215. if (is_inside_tree()) {
  216. top_layer->call_deferred("raise"); //top layer always on top!
  217. }
  218. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  219. if (gn) {
  220. gn->disconnect("offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  221. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  222. }
  223. }
  224. void GraphEdit::_notification(int p_what) {
  225. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  226. port_grab_distance_horizontal = get_theme_constant("port_grab_distance_horizontal");
  227. port_grab_distance_vertical = get_theme_constant("port_grab_distance_vertical");
  228. zoom_minus->set_icon(get_theme_icon("minus"));
  229. zoom_reset->set_icon(get_theme_icon("reset"));
  230. zoom_plus->set_icon(get_theme_icon("more"));
  231. snap_button->set_icon(get_theme_icon("snap"));
  232. }
  233. if (p_what == NOTIFICATION_READY) {
  234. Size2 hmin = h_scroll->get_combined_minimum_size();
  235. Size2 vmin = v_scroll->get_combined_minimum_size();
  236. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  237. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  238. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  239. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  240. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  241. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  242. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  243. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  244. }
  245. if (p_what == NOTIFICATION_DRAW) {
  246. draw_style_box(get_theme_stylebox("bg"), Rect2(Point2(), get_size()));
  247. if (is_using_snap()) {
  248. //draw grid
  249. int snap = get_snap();
  250. Vector2 offset = get_scroll_ofs() / zoom;
  251. Size2 size = get_size() / zoom;
  252. Point2i from = (offset / float(snap)).floor();
  253. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  254. Color grid_minor = get_theme_color("grid_minor");
  255. Color grid_major = get_theme_color("grid_major");
  256. for (int i = from.x; i < from.x + len.x; i++) {
  257. Color color;
  258. if (ABS(i) % 10 == 0) {
  259. color = grid_major;
  260. } else {
  261. color = grid_minor;
  262. }
  263. float base_ofs = i * snap * zoom - offset.x * zoom;
  264. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  265. }
  266. for (int i = from.y; i < from.y + len.y; i++) {
  267. Color color;
  268. if (ABS(i) % 10 == 0) {
  269. color = grid_major;
  270. } else {
  271. color = grid_minor;
  272. }
  273. float base_ofs = i * snap * zoom - offset.y * zoom;
  274. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  275. }
  276. }
  277. }
  278. if (p_what == NOTIFICATION_RESIZED) {
  279. _update_scroll();
  280. top_layer->update();
  281. }
  282. }
  283. bool GraphEdit::_filter_input(const Point2 &p_point) {
  284. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  285. for (int i = get_child_count() - 1; i >= 0; i--) {
  286. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  287. if (!gn) {
  288. continue;
  289. }
  290. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  291. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  292. if (is_in_hot_zone(pos, p_point)) {
  293. return true;
  294. }
  295. }
  296. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  297. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  298. if (is_in_hot_zone(pos, p_point)) {
  299. return true;
  300. }
  301. }
  302. }
  303. return false;
  304. }
  305. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  306. Ref<InputEventMouseButton> mb = p_ev;
  307. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  308. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  309. Vector2 mpos(mb->get_position().x, mb->get_position().y);
  310. for (int i = get_child_count() - 1; i >= 0; i--) {
  311. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  312. if (!gn) {
  313. continue;
  314. }
  315. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  316. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  317. if (is_in_hot_zone(pos, mpos)) {
  318. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  319. //check disconnect
  320. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  321. if (E->get().from == gn->get_name() && E->get().from_port == j) {
  322. Node *to = get_node(String(E->get().to));
  323. if (Object::cast_to<GraphNode>(to)) {
  324. connecting_from = E->get().to;
  325. connecting_index = E->get().to_port;
  326. connecting_out = false;
  327. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
  328. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E->get().to_port);
  329. connecting_target = false;
  330. connecting_to = pos;
  331. just_disconnected = true;
  332. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  333. to = get_node(String(connecting_from)); //maybe it was erased
  334. if (Object::cast_to<GraphNode>(to)) {
  335. connecting = true;
  336. }
  337. return;
  338. }
  339. }
  340. }
  341. }
  342. connecting = true;
  343. connecting_from = gn->get_name();
  344. connecting_index = j;
  345. connecting_out = true;
  346. connecting_type = gn->get_connection_output_type(j);
  347. connecting_color = gn->get_connection_output_color(j);
  348. connecting_target = false;
  349. connecting_to = pos;
  350. just_disconnected = false;
  351. return;
  352. }
  353. }
  354. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  355. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  356. if (is_in_hot_zone(pos, mpos)) {
  357. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  358. //check disconnect
  359. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  360. if (E->get().to == gn->get_name() && E->get().to_port == j) {
  361. Node *fr = get_node(String(E->get().from));
  362. if (Object::cast_to<GraphNode>(fr)) {
  363. connecting_from = E->get().from;
  364. connecting_index = E->get().from_port;
  365. connecting_out = true;
  366. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
  367. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E->get().from_port);
  368. connecting_target = false;
  369. connecting_to = pos;
  370. just_disconnected = true;
  371. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  372. fr = get_node(String(connecting_from)); //maybe it was erased
  373. if (Object::cast_to<GraphNode>(fr)) {
  374. connecting = true;
  375. }
  376. return;
  377. }
  378. }
  379. }
  380. }
  381. connecting = true;
  382. connecting_from = gn->get_name();
  383. connecting_index = j;
  384. connecting_out = false;
  385. connecting_type = gn->get_connection_input_type(j);
  386. connecting_color = gn->get_connection_input_color(j);
  387. connecting_target = false;
  388. connecting_to = pos;
  389. just_disconnected = false;
  390. return;
  391. }
  392. }
  393. }
  394. }
  395. Ref<InputEventMouseMotion> mm = p_ev;
  396. if (mm.is_valid() && connecting) {
  397. connecting_to = mm->get_position();
  398. connecting_target = false;
  399. top_layer->update();
  400. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  401. Vector2 mpos = mm->get_position();
  402. for (int i = get_child_count() - 1; i >= 0; i--) {
  403. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  404. if (!gn) {
  405. continue;
  406. }
  407. if (!connecting_out) {
  408. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  409. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  410. int type = gn->get_connection_output_type(j);
  411. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  412. connecting_target = true;
  413. connecting_to = pos;
  414. connecting_target_to = gn->get_name();
  415. connecting_target_index = j;
  416. return;
  417. }
  418. }
  419. } else {
  420. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  421. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  422. int type = gn->get_connection_input_type(j);
  423. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  424. connecting_target = true;
  425. connecting_to = pos;
  426. connecting_target_to = gn->get_name();
  427. connecting_target_index = j;
  428. return;
  429. }
  430. }
  431. }
  432. }
  433. }
  434. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  435. if (connecting && connecting_target) {
  436. String from = connecting_from;
  437. int from_slot = connecting_index;
  438. String to = connecting_target_to;
  439. int to_slot = connecting_target_index;
  440. if (!connecting_out) {
  441. SWAP(from, to);
  442. SWAP(from_slot, to_slot);
  443. }
  444. emit_signal("connection_request", from, from_slot, to, to_slot);
  445. } else if (!just_disconnected) {
  446. String from = connecting_from;
  447. int from_slot = connecting_index;
  448. Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
  449. if (!connecting_out) {
  450. emit_signal("connection_from_empty", from, from_slot, ofs);
  451. } else {
  452. emit_signal("connection_to_empty", from, from_slot, ofs);
  453. }
  454. }
  455. connecting = false;
  456. top_layer->update();
  457. update();
  458. connections_layer->update();
  459. }
  460. }
  461. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
  462. if (p_control->is_set_as_toplevel() || !p_control->is_visible()) {
  463. return false;
  464. }
  465. if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  466. //test children
  467. for (int i = 0; i < p_control->get_child_count(); i++) {
  468. Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
  469. if (!subchild) {
  470. continue;
  471. }
  472. if (_check_clickable_control(subchild, pos - subchild->get_position())) {
  473. return true;
  474. }
  475. }
  476. return false;
  477. } else {
  478. return true;
  479. }
  480. }
  481. bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos) {
  482. if (!Rect2(pos.x - port_grab_distance_horizontal, pos.y - port_grab_distance_vertical, port_grab_distance_horizontal * 2, port_grab_distance_vertical * 2).has_point(p_mouse_pos)) {
  483. return false;
  484. }
  485. for (int i = 0; i < get_child_count(); i++) {
  486. Control *child = Object::cast_to<Control>(get_child(i));
  487. if (!child) {
  488. continue;
  489. }
  490. Rect2 rect = child->get_rect();
  491. if (rect.has_point(p_mouse_pos)) {
  492. //check sub-controls
  493. Vector2 subpos = p_mouse_pos - rect.position;
  494. for (int j = 0; j < child->get_child_count(); j++) {
  495. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  496. if (!subchild) {
  497. continue;
  498. }
  499. if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
  500. return false;
  501. }
  502. }
  503. }
  504. }
  505. return true;
  506. }
  507. template <class Vector2>
  508. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  509. /* Formula from Wikipedia article on Bezier curves. */
  510. real_t omt = (1.0 - t);
  511. real_t omt2 = omt * omt;
  512. real_t omt3 = omt2 * omt;
  513. real_t t2 = t * t;
  514. real_t t3 = t2 * t;
  515. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  516. }
  517. void GraphEdit::_bake_segment2d(Vector<Vector2> &points, Vector<Color> &colors, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const {
  518. float mp = p_begin + (p_end - p_begin) * 0.5;
  519. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  520. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  521. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  522. Vector2 na = (mid - beg).normalized();
  523. Vector2 nb = (end - mid).normalized();
  524. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  525. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  526. points.push_back((beg + end) * 0.5);
  527. colors.push_back(p_color.lerp(p_to_color, mp));
  528. lines++;
  529. } else {
  530. _bake_segment2d(points, colors, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  531. _bake_segment2d(points, colors, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  532. }
  533. }
  534. void GraphEdit::_draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color) {
  535. //cubic bezier code
  536. float diff = p_to.x - p_from.x;
  537. float cp_offset;
  538. int cp_len = get_theme_constant("bezier_len_pos");
  539. int cp_neg_len = get_theme_constant("bezier_len_neg");
  540. if (diff > 0) {
  541. cp_offset = MIN(cp_len, diff * 0.5);
  542. } else {
  543. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  544. }
  545. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  546. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  547. int lines = 0;
  548. Vector<Point2> points;
  549. Vector<Color> colors;
  550. points.push_back(p_from);
  551. colors.push_back(p_color);
  552. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
  553. points.push_back(p_to);
  554. colors.push_back(p_to_color);
  555. #ifdef TOOLS_ENABLED
  556. p_where->draw_polyline_colors(points, colors, Math::floor(2 * EDSCALE));
  557. #else
  558. p_where->draw_polyline_colors(points, colors, 2);
  559. #endif
  560. }
  561. void GraphEdit::_connections_layer_draw() {
  562. Color activity_color = get_theme_color("activity");
  563. //draw connections
  564. List<List<Connection>::Element *> to_erase;
  565. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  566. NodePath fromnp(E->get().from);
  567. Node *from = get_node(fromnp);
  568. if (!from) {
  569. to_erase.push_back(E);
  570. continue;
  571. }
  572. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  573. if (!gfrom) {
  574. to_erase.push_back(E);
  575. continue;
  576. }
  577. NodePath tonp(E->get().to);
  578. Node *to = get_node(tonp);
  579. if (!to) {
  580. to_erase.push_back(E);
  581. continue;
  582. }
  583. GraphNode *gto = Object::cast_to<GraphNode>(to);
  584. if (!gto) {
  585. to_erase.push_back(E);
  586. continue;
  587. }
  588. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_offset() * zoom;
  589. Color color = gfrom->get_connection_output_color(E->get().from_port);
  590. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_offset() * zoom;
  591. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  592. if (E->get().activity > 0) {
  593. color = color.lerp(activity_color, E->get().activity);
  594. tocolor = tocolor.lerp(activity_color, E->get().activity);
  595. }
  596. _draw_cos_line(connections_layer, frompos, topos, color, tocolor);
  597. }
  598. while (to_erase.size()) {
  599. connections.erase(to_erase.front()->get());
  600. to_erase.pop_front();
  601. }
  602. }
  603. void GraphEdit::_top_layer_draw() {
  604. _update_scroll();
  605. if (connecting) {
  606. Node *fromn = get_node(connecting_from);
  607. ERR_FAIL_COND(!fromn);
  608. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  609. ERR_FAIL_COND(!from);
  610. Vector2 pos;
  611. if (connecting_out) {
  612. pos = from->get_connection_output_position(connecting_index);
  613. } else {
  614. pos = from->get_connection_input_position(connecting_index);
  615. }
  616. pos += from->get_position();
  617. Vector2 topos;
  618. topos = connecting_to;
  619. Color col = connecting_color;
  620. if (connecting_target) {
  621. col.r += 0.4;
  622. col.g += 0.4;
  623. col.b += 0.4;
  624. }
  625. if (!connecting_out) {
  626. SWAP(pos, topos);
  627. }
  628. _draw_cos_line(top_layer, pos, topos, col, col);
  629. }
  630. if (box_selecting) {
  631. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_fill"));
  632. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_stroke"), false);
  633. }
  634. }
  635. void GraphEdit::set_selected(Node *p_child) {
  636. for (int i = get_child_count() - 1; i >= 0; i--) {
  637. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  638. if (!gn) {
  639. continue;
  640. }
  641. gn->set_selected(gn == p_child);
  642. }
  643. }
  644. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  645. Ref<InputEventMouseMotion> mm = p_ev;
  646. if (mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_MIDDLE || (mm->get_button_mask() & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
  647. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  648. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  649. }
  650. if (mm.is_valid() && dragging) {
  651. just_selected = true;
  652. drag_accum += mm->get_relative();
  653. for (int i = get_child_count() - 1; i >= 0; i--) {
  654. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  655. if (gn && gn->is_selected()) {
  656. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  657. // Snapping can be toggled temporarily by holding down Ctrl.
  658. // This is done here as to not toggle the grid when holding down Ctrl.
  659. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  660. const int snap = get_snap();
  661. pos = pos.snapped(Vector2(snap, snap));
  662. }
  663. gn->set_offset(pos);
  664. }
  665. }
  666. }
  667. if (mm.is_valid() && box_selecting) {
  668. box_selecting_to = mm->get_position();
  669. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  670. MIN(box_selecting_from.y, box_selecting_to.y),
  671. ABS(box_selecting_from.x - box_selecting_to.x),
  672. ABS(box_selecting_from.y - box_selecting_to.y));
  673. for (int i = get_child_count() - 1; i >= 0; i--) {
  674. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  675. if (!gn) {
  676. continue;
  677. }
  678. Rect2 r = gn->get_rect();
  679. r.size *= zoom;
  680. bool in_box = r.intersects(box_selecting_rect);
  681. if (in_box) {
  682. gn->set_selected(box_selection_mode_additive);
  683. } else {
  684. gn->set_selected(previus_selected.find(gn) != nullptr);
  685. }
  686. }
  687. top_layer->update();
  688. }
  689. Ref<InputEventMouseButton> b = p_ev;
  690. if (b.is_valid()) {
  691. if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
  692. if (box_selecting) {
  693. box_selecting = false;
  694. for (int i = get_child_count() - 1; i >= 0; i--) {
  695. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  696. if (!gn) {
  697. continue;
  698. }
  699. gn->set_selected(previus_selected.find(gn) != nullptr);
  700. }
  701. top_layer->update();
  702. } else {
  703. if (connecting) {
  704. connecting = false;
  705. top_layer->update();
  706. } else {
  707. emit_signal("popup_request", b->get_global_position());
  708. }
  709. }
  710. }
  711. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
  712. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  713. //deselect current node
  714. for (int i = get_child_count() - 1; i >= 0; i--) {
  715. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  716. if (gn) {
  717. Rect2 r = gn->get_rect();
  718. r.size *= zoom;
  719. if (r.has_point(b->get_position())) {
  720. gn->set_selected(false);
  721. }
  722. }
  723. }
  724. }
  725. if (drag_accum != Vector2()) {
  726. emit_signal("_begin_node_move");
  727. for (int i = get_child_count() - 1; i >= 0; i--) {
  728. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  729. if (gn && gn->is_selected()) {
  730. gn->set_drag(false);
  731. }
  732. }
  733. emit_signal("_end_node_move");
  734. }
  735. dragging = false;
  736. top_layer->update();
  737. update();
  738. connections_layer->update();
  739. }
  740. if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {
  741. GraphNode *gn = nullptr;
  742. for (int i = get_child_count() - 1; i >= 0; i--) {
  743. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  744. if (gn_selected) {
  745. if (gn_selected->is_resizing()) {
  746. continue;
  747. }
  748. if (gn_selected->has_point(b->get_position() - gn_selected->get_position())) {
  749. gn = gn_selected;
  750. break;
  751. }
  752. }
  753. }
  754. if (gn) {
  755. if (_filter_input(b->get_position())) {
  756. return;
  757. }
  758. dragging = true;
  759. drag_accum = Vector2();
  760. just_selected = !gn->is_selected();
  761. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  762. for (int i = 0; i < get_child_count(); i++) {
  763. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  764. if (o_gn) {
  765. if (o_gn == gn) {
  766. o_gn->set_selected(true);
  767. } else {
  768. if (o_gn->is_selected()) {
  769. emit_signal("node_unselected", o_gn);
  770. }
  771. o_gn->set_selected(false);
  772. }
  773. }
  774. }
  775. }
  776. gn->set_selected(true);
  777. for (int i = 0; i < get_child_count(); i++) {
  778. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  779. if (!o_gn) {
  780. continue;
  781. }
  782. if (o_gn->is_selected()) {
  783. o_gn->set_drag(true);
  784. }
  785. }
  786. } else {
  787. if (_filter_input(b->get_position())) {
  788. return;
  789. }
  790. if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  791. return;
  792. }
  793. box_selecting = true;
  794. box_selecting_from = b->get_position();
  795. if (b->get_control()) {
  796. box_selection_mode_additive = true;
  797. previus_selected.clear();
  798. for (int i = get_child_count() - 1; i >= 0; i--) {
  799. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  800. if (!gn2 || !gn2->is_selected()) {
  801. continue;
  802. }
  803. previus_selected.push_back(gn2);
  804. }
  805. } else if (b->get_shift()) {
  806. box_selection_mode_additive = false;
  807. previus_selected.clear();
  808. for (int i = get_child_count() - 1; i >= 0; i--) {
  809. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  810. if (!gn2 || !gn2->is_selected()) {
  811. continue;
  812. }
  813. previus_selected.push_back(gn2);
  814. }
  815. } else {
  816. box_selection_mode_additive = true;
  817. previus_selected.clear();
  818. for (int i = get_child_count() - 1; i >= 0; i--) {
  819. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  820. if (!gn2) {
  821. continue;
  822. }
  823. if (gn2->is_selected()) {
  824. emit_signal("node_unselected", gn2);
  825. }
  826. gn2->set_selected(false);
  827. }
  828. }
  829. }
  830. }
  831. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  832. box_selecting = false;
  833. previus_selected.clear();
  834. top_layer->update();
  835. }
  836. if (b->get_button_index() == BUTTON_WHEEL_UP && b->is_pressed()) {
  837. //too difficult to get right
  838. //set_zoom(zoom*ZOOM_SCALE);
  839. }
  840. if (b->get_button_index() == BUTTON_WHEEL_DOWN && b->is_pressed()) {
  841. //too difficult to get right
  842. //set_zoom(zoom/ZOOM_SCALE);
  843. }
  844. if (b->get_button_index() == BUTTON_WHEEL_UP && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  845. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  846. }
  847. if (b->get_button_index() == BUTTON_WHEEL_DOWN && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  848. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  849. }
  850. if (b->get_button_index() == BUTTON_WHEEL_RIGHT || (b->get_button_index() == BUTTON_WHEEL_DOWN && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  851. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b->get_factor() / 8);
  852. }
  853. if (b->get_button_index() == BUTTON_WHEEL_LEFT || (b->get_button_index() == BUTTON_WHEEL_UP && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  854. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b->get_factor() / 8);
  855. }
  856. }
  857. Ref<InputEventKey> k = p_ev;
  858. if (k.is_valid()) {
  859. if (k->get_keycode() == KEY_D && k->is_pressed() && k->get_command()) {
  860. emit_signal("duplicate_nodes_request");
  861. accept_event();
  862. }
  863. if (k->get_keycode() == KEY_C && k->is_pressed() && k->get_command()) {
  864. emit_signal("copy_nodes_request");
  865. accept_event();
  866. }
  867. if (k->get_keycode() == KEY_V && k->is_pressed() && k->get_command()) {
  868. emit_signal("paste_nodes_request");
  869. accept_event();
  870. }
  871. if (k->get_keycode() == KEY_DELETE && k->is_pressed()) {
  872. emit_signal("delete_nodes_request");
  873. accept_event();
  874. }
  875. }
  876. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  877. if (magnify_gesture.is_valid()) {
  878. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  879. }
  880. Ref<InputEventPanGesture> pan_gesture = p_ev;
  881. if (pan_gesture.is_valid()) {
  882. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  883. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  884. }
  885. }
  886. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  887. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  888. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  889. if (Math::is_equal_approx(E->get().activity, p_activity)) {
  890. //update only if changed
  891. top_layer->update();
  892. connections_layer->update();
  893. }
  894. E->get().activity = p_activity;
  895. return;
  896. }
  897. }
  898. }
  899. void GraphEdit::clear_connections() {
  900. connections.clear();
  901. update();
  902. connections_layer->update();
  903. }
  904. void GraphEdit::set_zoom(float p_zoom) {
  905. set_zoom_custom(p_zoom, get_size() / 2);
  906. }
  907. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  908. p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
  909. if (zoom == p_zoom) {
  910. return;
  911. }
  912. zoom_minus->set_disabled(zoom == MIN_ZOOM);
  913. zoom_plus->set_disabled(zoom == MAX_ZOOM);
  914. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  915. zoom = p_zoom;
  916. top_layer->update();
  917. _update_scroll();
  918. connections_layer->update();
  919. if (is_visible_in_tree()) {
  920. Vector2 ofs = sbofs * zoom - p_center;
  921. h_scroll->set_value(ofs.x);
  922. v_scroll->set_value(ofs.y);
  923. }
  924. update();
  925. }
  926. float GraphEdit::get_zoom() const {
  927. return zoom;
  928. }
  929. void GraphEdit::set_right_disconnects(bool p_enable) {
  930. right_disconnects = p_enable;
  931. }
  932. bool GraphEdit::is_right_disconnects_enabled() const {
  933. return right_disconnects;
  934. }
  935. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  936. valid_right_disconnect_types.insert(p_type);
  937. }
  938. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  939. valid_right_disconnect_types.erase(p_type);
  940. }
  941. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  942. valid_left_disconnect_types.insert(p_type);
  943. }
  944. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  945. valid_left_disconnect_types.erase(p_type);
  946. }
  947. Array GraphEdit::_get_connection_list() const {
  948. List<Connection> conns;
  949. get_connection_list(&conns);
  950. Array arr;
  951. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  952. Dictionary d;
  953. d["from"] = E->get().from;
  954. d["from_port"] = E->get().from_port;
  955. d["to"] = E->get().to;
  956. d["to_port"] = E->get().to_port;
  957. arr.push_back(d);
  958. }
  959. return arr;
  960. }
  961. void GraphEdit::_zoom_minus() {
  962. set_zoom(zoom / ZOOM_SCALE);
  963. }
  964. void GraphEdit::_zoom_reset() {
  965. set_zoom(1);
  966. }
  967. void GraphEdit::_zoom_plus() {
  968. set_zoom(zoom * ZOOM_SCALE);
  969. }
  970. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  971. ConnType ct;
  972. ct.type_a = p_type;
  973. ct.type_b = p_with_type;
  974. valid_connection_types.insert(ct);
  975. }
  976. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  977. ConnType ct;
  978. ct.type_a = p_type;
  979. ct.type_b = p_with_type;
  980. valid_connection_types.erase(ct);
  981. }
  982. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  983. ConnType ct;
  984. ct.type_a = p_type;
  985. ct.type_b = p_with_type;
  986. return valid_connection_types.has(ct);
  987. }
  988. void GraphEdit::set_use_snap(bool p_enable) {
  989. snap_button->set_pressed(p_enable);
  990. update();
  991. }
  992. bool GraphEdit::is_using_snap() const {
  993. return snap_button->is_pressed();
  994. }
  995. int GraphEdit::get_snap() const {
  996. return snap_amount->get_value();
  997. }
  998. void GraphEdit::set_snap(int p_snap) {
  999. ERR_FAIL_COND(p_snap < 5);
  1000. snap_amount->set_value(p_snap);
  1001. update();
  1002. }
  1003. void GraphEdit::_snap_toggled() {
  1004. update();
  1005. }
  1006. void GraphEdit::_snap_value_changed(double) {
  1007. update();
  1008. }
  1009. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1010. return zoom_hb;
  1011. }
  1012. void GraphEdit::_bind_methods() {
  1013. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  1014. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  1015. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  1016. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1017. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1018. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1019. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1020. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  1021. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1022. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1023. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1024. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1025. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1026. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1027. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1028. ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
  1029. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1030. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1031. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1032. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1033. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1034. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1035. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1036. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  1037. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1038. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1039. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1040. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1041. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  1042. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  1043. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1044. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1045. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1046. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1047. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1048. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1049. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1050. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1051. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1052. ADD_SIGNAL(MethodInfo("node_unselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1053. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1054. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1055. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  1056. ADD_SIGNAL(MethodInfo("_begin_node_move"));
  1057. ADD_SIGNAL(MethodInfo("_end_node_move"));
  1058. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  1059. }
  1060. GraphEdit::GraphEdit() {
  1061. set_focus_mode(FOCUS_ALL);
  1062. awaiting_scroll_offset_update = false;
  1063. top_layer = nullptr;
  1064. top_layer = memnew(GraphEditFilter(this));
  1065. add_child(top_layer);
  1066. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1067. top_layer->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1068. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1069. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1070. connections_layer = memnew(Control);
  1071. add_child(connections_layer);
  1072. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1073. connections_layer->set_name("CLAYER");
  1074. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1075. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1076. h_scroll = memnew(HScrollBar);
  1077. h_scroll->set_name("_h_scroll");
  1078. top_layer->add_child(h_scroll);
  1079. v_scroll = memnew(VScrollBar);
  1080. v_scroll->set_name("_v_scroll");
  1081. top_layer->add_child(v_scroll);
  1082. updating = false;
  1083. connecting = false;
  1084. right_disconnects = false;
  1085. box_selecting = false;
  1086. dragging = false;
  1087. //set large minmax so it can scroll even if not resized yet
  1088. h_scroll->set_min(-10000);
  1089. h_scroll->set_max(10000);
  1090. v_scroll->set_min(-10000);
  1091. v_scroll->set_max(10000);
  1092. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1093. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1094. zoom = 1;
  1095. zoom_hb = memnew(HBoxContainer);
  1096. top_layer->add_child(zoom_hb);
  1097. zoom_hb->set_position(Vector2(10, 10));
  1098. zoom_minus = memnew(ToolButton);
  1099. zoom_hb->add_child(zoom_minus);
  1100. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1101. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1102. zoom_minus->set_focus_mode(FOCUS_NONE);
  1103. zoom_reset = memnew(ToolButton);
  1104. zoom_hb->add_child(zoom_reset);
  1105. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1106. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1107. zoom_reset->set_focus_mode(FOCUS_NONE);
  1108. zoom_plus = memnew(ToolButton);
  1109. zoom_hb->add_child(zoom_plus);
  1110. zoom_plus->set_tooltip(RTR("Zoom In"));
  1111. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1112. zoom_plus->set_focus_mode(FOCUS_NONE);
  1113. snap_button = memnew(ToolButton);
  1114. snap_button->set_toggle_mode(true);
  1115. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1116. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  1117. snap_button->set_pressed(true);
  1118. snap_button->set_focus_mode(FOCUS_NONE);
  1119. zoom_hb->add_child(snap_button);
  1120. snap_amount = memnew(SpinBox);
  1121. snap_amount->set_min(5);
  1122. snap_amount->set_max(100);
  1123. snap_amount->set_step(1);
  1124. snap_amount->set_value(20);
  1125. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  1126. zoom_hb->add_child(snap_amount);
  1127. setting_scroll_ofs = false;
  1128. just_disconnected = false;
  1129. set_clip_contents(true);
  1130. }