graph_edit.cpp 44 KB

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