graph_edit.cpp 43 KB

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