2
0

graph_edit.cpp 37 KB

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