graph_edit.cpp 40 KB

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