graph_edit.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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.pos = gn->get_offset() * zoom;
  131. r.size = gn->get_size() * zoom;
  132. screen = screen.merge(r);
  133. }
  134. screen.pos -= get_size();
  135. screen.size += get_size() * 2.0;
  136. h_scroll->set_min(screen.pos.x);
  137. h_scroll->set_max(screen.pos.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.pos.y);
  144. v_scroll->set_max(screen.pos.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 InputEvent &p_ev) {
  282. float grab_r_extend = 2.0;
  283. if (p_ev.type == InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index == BUTTON_LEFT && p_ev.mouse_button.pressed) {
  284. Ref<Texture> port = get_icon("port", "GraphNode");
  285. Vector2 mpos(p_ev.mouse_button.x, p_ev.mouse_button.y);
  286. float grab_r = port->get_width() * 0.5 * grab_r_extend;
  287. for (int i = get_child_count() - 1; i >= 0; i--) {
  288. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  289. if (!gn)
  290. continue;
  291. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  292. Vector2 pos = gn->get_connection_output_pos(j) + gn->get_position();
  293. if (pos.distance_to(mpos) < grab_r) {
  294. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  295. //check disconnect
  296. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  297. if (E->get().from == gn->get_name() && E->get().from_port == j) {
  298. Node *to = get_node(String(E->get().to));
  299. if (to && to->cast_to<GraphNode>()) {
  300. connecting_from = E->get().to;
  301. connecting_index = E->get().to_port;
  302. connecting_out = false;
  303. connecting_type = to->cast_to<GraphNode>()->get_connection_input_type(E->get().to_port);
  304. connecting_color = to->cast_to<GraphNode>()->get_connection_input_color(E->get().to_port);
  305. connecting_target = false;
  306. connecting_to = pos;
  307. just_disconected = true;
  308. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  309. to = get_node(String(connecting_from)); //maybe it was erased
  310. if (to && to->cast_to<GraphNode>()) {
  311. connecting = true;
  312. }
  313. return;
  314. }
  315. }
  316. }
  317. }
  318. connecting = true;
  319. connecting_from = gn->get_name();
  320. connecting_index = j;
  321. connecting_out = true;
  322. connecting_type = gn->get_connection_output_type(j);
  323. connecting_color = gn->get_connection_output_color(j);
  324. connecting_target = false;
  325. connecting_to = pos;
  326. just_disconected = false;
  327. return;
  328. }
  329. }
  330. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  331. Vector2 pos = gn->get_connection_input_pos(j) + gn->get_position();
  332. if (pos.distance_to(mpos) < grab_r) {
  333. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  334. //check disconnect
  335. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  336. if (E->get().to == gn->get_name() && E->get().to_port == j) {
  337. Node *fr = get_node(String(E->get().from));
  338. if (fr && fr->cast_to<GraphNode>()) {
  339. connecting_from = E->get().from;
  340. connecting_index = E->get().from_port;
  341. connecting_out = true;
  342. connecting_type = fr->cast_to<GraphNode>()->get_connection_output_type(E->get().from_port);
  343. connecting_color = fr->cast_to<GraphNode>()->get_connection_output_color(E->get().from_port);
  344. connecting_target = false;
  345. connecting_to = pos;
  346. just_disconected = true;
  347. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  348. fr = get_node(String(connecting_from)); //maybe it was erased
  349. if (fr && fr->cast_to<GraphNode>()) {
  350. connecting = true;
  351. }
  352. return;
  353. }
  354. }
  355. }
  356. }
  357. connecting = true;
  358. connecting_from = gn->get_name();
  359. connecting_index = j;
  360. connecting_out = false;
  361. connecting_type = gn->get_connection_input_type(j);
  362. connecting_color = gn->get_connection_input_color(j);
  363. connecting_target = false;
  364. connecting_to = pos;
  365. just_disconected = true;
  366. return;
  367. }
  368. }
  369. }
  370. }
  371. if (p_ev.type == InputEvent::MOUSE_MOTION && connecting) {
  372. connecting_to = Vector2(p_ev.mouse_motion.x, p_ev.mouse_motion.y);
  373. connecting_target = false;
  374. top_layer->update();
  375. Ref<Texture> port = get_icon("port", "GraphNode");
  376. Vector2 mpos(p_ev.mouse_button.x, p_ev.mouse_button.y);
  377. float grab_r = port->get_width() * 0.5 * grab_r_extend;
  378. for (int i = get_child_count() - 1; i >= 0; i--) {
  379. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  380. if (!gn)
  381. continue;
  382. if (!connecting_out) {
  383. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  384. Vector2 pos = gn->get_connection_output_pos(j) + gn->get_position();
  385. int type = gn->get_connection_output_type(j);
  386. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && pos.distance_to(mpos) < grab_r) {
  387. connecting_target = true;
  388. connecting_to = pos;
  389. connecting_target_to = gn->get_name();
  390. connecting_target_index = j;
  391. return;
  392. }
  393. }
  394. } else {
  395. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  396. Vector2 pos = gn->get_connection_input_pos(j) + gn->get_position();
  397. int type = gn->get_connection_input_type(j);
  398. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && pos.distance_to(mpos) < grab_r) {
  399. connecting_target = true;
  400. connecting_to = pos;
  401. connecting_target_to = gn->get_name();
  402. connecting_target_index = j;
  403. return;
  404. }
  405. }
  406. }
  407. }
  408. }
  409. if (p_ev.type == InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index == BUTTON_LEFT && !p_ev.mouse_button.pressed) {
  410. if (connecting && connecting_target) {
  411. String from = connecting_from;
  412. int from_slot = connecting_index;
  413. String to = connecting_target_to;
  414. int to_slot = connecting_target_index;
  415. if (!connecting_out) {
  416. SWAP(from, to);
  417. SWAP(from_slot, to_slot);
  418. }
  419. emit_signal("connection_request", from, from_slot, to, to_slot);
  420. } else if (!just_disconected) {
  421. String from = connecting_from;
  422. int from_slot = connecting_index;
  423. Vector2 ofs = Vector2(p_ev.mouse_button.x, p_ev.mouse_button.y);
  424. emit_signal("connection_to_empty", from, from_slot, ofs);
  425. }
  426. connecting = false;
  427. top_layer->update();
  428. update();
  429. connections_layer->update();
  430. }
  431. }
  432. template <class Vector2>
  433. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  434. /* Formula from Wikipedia article on Bezier curves. */
  435. real_t omt = (1.0 - t);
  436. real_t omt2 = omt * omt;
  437. real_t omt3 = omt2 * omt;
  438. real_t t2 = t * t;
  439. real_t t3 = t2 * t;
  440. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  441. }
  442. 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 {
  443. float mp = p_begin + (p_end - p_begin) * 0.5;
  444. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  445. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  446. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  447. Vector2 na = (mid - beg).normalized();
  448. Vector2 nb = (end - mid).normalized();
  449. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  450. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  451. p_where->draw_line(beg, end, p_color.linear_interpolate(p_to_color, mp), 2, true);
  452. lines++;
  453. } else {
  454. _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);
  455. _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);
  456. }
  457. }
  458. 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) {
  459. #if 1
  460. //cubic bezier code
  461. float diff = p_to.x - p_from.x;
  462. float cp_offset;
  463. int cp_len = get_constant("bezier_len_pos");
  464. int cp_neg_len = get_constant("bezier_len_neg");
  465. if (diff > 0) {
  466. cp_offset = MAX(cp_len, diff * 0.5);
  467. } else {
  468. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  469. }
  470. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  471. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  472. int lines = 0;
  473. _bake_segment2d(p_where, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 8, p_color, p_to_color, lines);
  474. #else
  475. static const int steps = 20;
  476. //old cosine code
  477. Rect2 r;
  478. r.pos = p_from;
  479. r.expand_to(p_to);
  480. Vector2 sign = Vector2((p_from.x < p_to.x) ? 1 : -1, (p_from.y < p_to.y) ? 1 : -1);
  481. bool flip = sign.x * sign.y < 0;
  482. Vector2 prev;
  483. for (int i = 0; i <= steps; i++) {
  484. float d = i / float(steps);
  485. float c = -Math::cos(d * Math_PI) * 0.5 + 0.5;
  486. if (flip)
  487. c = 1.0 - c;
  488. Vector2 p = r.pos + Vector2(d * r.size.width, c * r.size.height);
  489. if (i > 0) {
  490. p_where->draw_line(prev, p, p_color.linear_interpolate(p_to_color, d), 2);
  491. }
  492. prev = p;
  493. }
  494. #endif
  495. }
  496. void GraphEdit::_connections_layer_draw() {
  497. {
  498. //draw connections
  499. List<List<Connection>::Element *> to_erase;
  500. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  501. NodePath fromnp(E->get().from);
  502. Node *from = get_node(fromnp);
  503. if (!from) {
  504. to_erase.push_back(E);
  505. continue;
  506. }
  507. GraphNode *gfrom = from->cast_to<GraphNode>();
  508. if (!gfrom) {
  509. to_erase.push_back(E);
  510. continue;
  511. }
  512. NodePath tonp(E->get().to);
  513. Node *to = get_node(tonp);
  514. if (!to) {
  515. to_erase.push_back(E);
  516. continue;
  517. }
  518. GraphNode *gto = to->cast_to<GraphNode>();
  519. if (!gto) {
  520. to_erase.push_back(E);
  521. continue;
  522. }
  523. Vector2 frompos = gfrom->get_connection_output_pos(E->get().from_port) + gfrom->get_offset() * zoom;
  524. Color color = gfrom->get_connection_output_color(E->get().from_port);
  525. Vector2 topos = gto->get_connection_input_pos(E->get().to_port) + gto->get_offset() * zoom;
  526. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  527. _draw_cos_line(connections_layer, frompos, topos, color, tocolor);
  528. }
  529. while (to_erase.size()) {
  530. connections.erase(to_erase.front()->get());
  531. to_erase.pop_front();
  532. }
  533. }
  534. }
  535. void GraphEdit::_top_layer_draw() {
  536. _update_scroll();
  537. if (connecting) {
  538. Node *fromn = get_node(connecting_from);
  539. ERR_FAIL_COND(!fromn);
  540. GraphNode *from = fromn->cast_to<GraphNode>();
  541. ERR_FAIL_COND(!from);
  542. Vector2 pos;
  543. if (connecting_out)
  544. pos = from->get_connection_output_pos(connecting_index);
  545. else
  546. pos = from->get_connection_input_pos(connecting_index);
  547. pos += from->get_position();
  548. Vector2 topos;
  549. topos = connecting_to;
  550. Color col = connecting_color;
  551. if (connecting_target) {
  552. col.r += 0.4;
  553. col.g += 0.4;
  554. col.b += 0.4;
  555. }
  556. if (!connecting_out) {
  557. SWAP(pos, topos);
  558. }
  559. _draw_cos_line(top_layer, pos, topos, col, col);
  560. }
  561. if (box_selecting)
  562. top_layer->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3));
  563. }
  564. void GraphEdit::set_selected(Node *p_child) {
  565. for (int i = get_child_count() - 1; i >= 0; i--) {
  566. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  567. if (!gn)
  568. continue;
  569. gn->set_selected(gn == p_child);
  570. }
  571. }
  572. void GraphEdit::_gui_input(const InputEvent &p_ev) {
  573. if (p_ev.type == InputEvent::MOUSE_MOTION && (p_ev.mouse_motion.button_mask & BUTTON_MASK_MIDDLE || (p_ev.mouse_motion.button_mask & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
  574. h_scroll->set_value(h_scroll->get_value() - p_ev.mouse_motion.relative_x);
  575. v_scroll->set_value(v_scroll->get_value() - p_ev.mouse_motion.relative_y);
  576. }
  577. if (p_ev.type == InputEvent::MOUSE_MOTION && dragging) {
  578. just_selected = true;
  579. // TODO: Remove local mouse pos hack if/when InputEventMouseMotion is fixed to support floats
  580. //drag_accum+=Vector2(p_ev.mouse_motion.relative_x,p_ev.mouse_motion.relative_y);
  581. drag_accum = get_local_mouse_pos() - drag_origin;
  582. for (int i = get_child_count() - 1; i >= 0; i--) {
  583. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  584. if (gn && gn->is_selected()) {
  585. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  586. if (is_using_snap()) {
  587. int snap = get_snap();
  588. pos = pos.snapped(Vector2(snap, snap));
  589. }
  590. gn->set_offset(pos);
  591. }
  592. }
  593. }
  594. if (p_ev.type == InputEvent::MOUSE_MOTION && box_selecting) {
  595. box_selecting_to = get_local_mouse_pos();
  596. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  597. MIN(box_selecting_from.y, box_selecting_to.y),
  598. ABS(box_selecting_from.x - box_selecting_to.x),
  599. ABS(box_selecting_from.y - box_selecting_to.y));
  600. for (int i = get_child_count() - 1; i >= 0; i--) {
  601. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  602. if (!gn)
  603. continue;
  604. Rect2 r = gn->get_rect();
  605. r.size *= zoom;
  606. bool in_box = r.intersects(box_selecting_rect);
  607. if (in_box)
  608. gn->set_selected(box_selection_mode_aditive);
  609. else
  610. gn->set_selected(previus_selected.find(gn) != NULL);
  611. }
  612. top_layer->update();
  613. }
  614. if (p_ev.type == InputEvent::MOUSE_BUTTON) {
  615. const InputEventMouseButton &b = p_ev.mouse_button;
  616. if (b.button_index == BUTTON_RIGHT && b.pressed) {
  617. if (box_selecting) {
  618. box_selecting = false;
  619. for (int i = get_child_count() - 1; i >= 0; i--) {
  620. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  621. if (!gn)
  622. continue;
  623. gn->set_selected(previus_selected.find(gn) != NULL);
  624. }
  625. top_layer->update();
  626. } else {
  627. if (connecting) {
  628. connecting = false;
  629. top_layer->update();
  630. } else {
  631. emit_signal("popup_request", Vector2(b.global_x, b.global_y));
  632. }
  633. }
  634. }
  635. if (b.button_index == BUTTON_LEFT && !b.pressed && dragging) {
  636. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  637. //deselect current node
  638. for (int i = get_child_count() - 1; i >= 0; i--) {
  639. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  640. if (gn) {
  641. Rect2 r = gn->get_rect();
  642. r.size *= zoom;
  643. if (r.has_point(get_local_mouse_pos()))
  644. gn->set_selected(false);
  645. }
  646. }
  647. }
  648. if (drag_accum != Vector2()) {
  649. emit_signal("_begin_node_move");
  650. for (int i = get_child_count() - 1; i >= 0; i--) {
  651. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  652. if (gn && gn->is_selected())
  653. gn->set_drag(false);
  654. }
  655. emit_signal("_end_node_move");
  656. }
  657. dragging = false;
  658. top_layer->update();
  659. update();
  660. connections_layer->update();
  661. }
  662. if (b.button_index == BUTTON_LEFT && b.pressed) {
  663. GraphNode *gn = NULL;
  664. GraphNode *gn_selected = NULL;
  665. for (int i = get_child_count() - 1; i >= 0; i--) {
  666. gn_selected = get_child(i)->cast_to<GraphNode>();
  667. if (gn_selected) {
  668. if (gn_selected->is_resizing())
  669. continue;
  670. Rect2 r = gn_selected->get_rect();
  671. r.size *= zoom;
  672. if (r.has_point(get_local_mouse_pos()))
  673. gn = gn_selected;
  674. break;
  675. }
  676. }
  677. if (gn) {
  678. if (_filter_input(Vector2(b.x, b.y)))
  679. return;
  680. dragging = true;
  681. drag_accum = Vector2();
  682. drag_origin = get_local_mouse_pos();
  683. just_selected = !gn->is_selected();
  684. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  685. for (int i = 0; i < get_child_count(); i++) {
  686. GraphNode *o_gn = get_child(i)->cast_to<GraphNode>();
  687. if (o_gn)
  688. o_gn->set_selected(o_gn == gn);
  689. }
  690. }
  691. gn->set_selected(true);
  692. for (int i = 0; i < get_child_count(); i++) {
  693. GraphNode *o_gn = get_child(i)->cast_to<GraphNode>();
  694. if (!o_gn)
  695. continue;
  696. if (o_gn->is_selected())
  697. o_gn->set_drag(true);
  698. }
  699. } else {
  700. if (_filter_input(Vector2(b.x, b.y)))
  701. return;
  702. if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
  703. return;
  704. box_selecting = true;
  705. box_selecting_from = get_local_mouse_pos();
  706. if (b.mod.control) {
  707. box_selection_mode_aditive = true;
  708. previus_selected.clear();
  709. for (int i = get_child_count() - 1; i >= 0; i--) {
  710. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  711. if (!gn || !gn->is_selected())
  712. continue;
  713. previus_selected.push_back(gn);
  714. }
  715. } else if (b.mod.shift) {
  716. box_selection_mode_aditive = false;
  717. previus_selected.clear();
  718. for (int i = get_child_count() - 1; i >= 0; i--) {
  719. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  720. if (!gn || !gn->is_selected())
  721. continue;
  722. previus_selected.push_back(gn);
  723. }
  724. } else {
  725. box_selection_mode_aditive = true;
  726. previus_selected.clear();
  727. for (int i = get_child_count() - 1; i >= 0; i--) {
  728. GraphNode *gn = get_child(i)->cast_to<GraphNode>();
  729. if (!gn)
  730. continue;
  731. gn->set_selected(false);
  732. }
  733. }
  734. }
  735. }
  736. if (b.button_index == BUTTON_LEFT && !b.pressed && box_selecting) {
  737. box_selecting = false;
  738. previus_selected.clear();
  739. top_layer->update();
  740. }
  741. if (b.button_index == BUTTON_WHEEL_UP && b.pressed) {
  742. //too difficult to get right
  743. //set_zoom(zoom*ZOOM_SCALE);
  744. }
  745. if (b.button_index == BUTTON_WHEEL_DOWN && b.pressed) {
  746. //too difficult to get right
  747. //set_zoom(zoom/ZOOM_SCALE);
  748. }
  749. }
  750. if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_D && p_ev.key.pressed && p_ev.key.mod.command) {
  751. emit_signal("duplicate_nodes_request");
  752. accept_event();
  753. }
  754. if (p_ev.type == InputEvent::KEY && p_ev.key.scancode == KEY_DELETE && p_ev.key.pressed) {
  755. emit_signal("delete_nodes_request");
  756. accept_event();
  757. }
  758. }
  759. void GraphEdit::clear_connections() {
  760. connections.clear();
  761. update();
  762. connections_layer->update();
  763. }
  764. void GraphEdit::set_zoom(float p_zoom) {
  765. p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
  766. if (zoom == p_zoom)
  767. return;
  768. zoom_minus->set_disabled(zoom == MIN_ZOOM);
  769. zoom_plus->set_disabled(zoom == MAX_ZOOM);
  770. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + get_size() / 2) / zoom;
  771. zoom = p_zoom;
  772. top_layer->update();
  773. _update_scroll();
  774. connections_layer->update();
  775. if (is_visible_in_tree()) {
  776. Vector2 ofs = sbofs * zoom - get_size() / 2;
  777. h_scroll->set_value(ofs.x);
  778. v_scroll->set_value(ofs.y);
  779. }
  780. update();
  781. }
  782. float GraphEdit::get_zoom() const {
  783. return zoom;
  784. }
  785. void GraphEdit::set_right_disconnects(bool p_enable) {
  786. right_disconnects = p_enable;
  787. }
  788. bool GraphEdit::is_right_disconnects_enabled() const {
  789. return right_disconnects;
  790. }
  791. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  792. valid_right_disconnect_types.insert(p_type);
  793. }
  794. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  795. valid_right_disconnect_types.erase(p_type);
  796. }
  797. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  798. valid_left_disconnect_types.insert(p_type);
  799. }
  800. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  801. valid_left_disconnect_types.erase(p_type);
  802. }
  803. Array GraphEdit::_get_connection_list() const {
  804. List<Connection> conns;
  805. get_connection_list(&conns);
  806. Array arr;
  807. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  808. Dictionary d;
  809. d["from"] = E->get().from;
  810. d["from_port"] = E->get().from_port;
  811. d["to"] = E->get().to;
  812. d["to_port"] = E->get().to_port;
  813. arr.push_back(d);
  814. }
  815. return arr;
  816. }
  817. void GraphEdit::_zoom_minus() {
  818. set_zoom(zoom / ZOOM_SCALE);
  819. }
  820. void GraphEdit::_zoom_reset() {
  821. set_zoom(1);
  822. }
  823. void GraphEdit::_zoom_plus() {
  824. set_zoom(zoom * ZOOM_SCALE);
  825. }
  826. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  827. ConnType ct;
  828. ct.type_a = p_type;
  829. ct.type_b = p_with_type;
  830. valid_connection_types.insert(ct);
  831. }
  832. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  833. ConnType ct;
  834. ct.type_a = p_type;
  835. ct.type_b = p_with_type;
  836. valid_connection_types.erase(ct);
  837. }
  838. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  839. ConnType ct;
  840. ct.type_a = p_type;
  841. ct.type_b = p_with_type;
  842. return valid_connection_types.has(ct);
  843. }
  844. void GraphEdit::set_use_snap(bool p_enable) {
  845. snap_button->set_pressed(p_enable);
  846. update();
  847. }
  848. bool GraphEdit::is_using_snap() const {
  849. return snap_button->is_pressed();
  850. }
  851. int GraphEdit::get_snap() const {
  852. return snap_amount->get_value();
  853. }
  854. void GraphEdit::set_snap(int p_snap) {
  855. ERR_FAIL_COND(p_snap < 5);
  856. snap_amount->set_value(p_snap);
  857. update();
  858. }
  859. void GraphEdit::_snap_toggled() {
  860. update();
  861. }
  862. void GraphEdit::_snap_value_changed(double) {
  863. update();
  864. }
  865. void GraphEdit::_bind_methods() {
  866. ClassDB::bind_method(D_METHOD("connect_node:Error", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  867. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  868. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  869. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  870. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  871. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  872. ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
  873. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  874. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  875. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  876. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  877. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  878. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  879. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  880. ClassDB::bind_method(D_METHOD("_graph_node_moved"), &GraphEdit::_graph_node_moved);
  881. ClassDB::bind_method(D_METHOD("_graph_node_raised"), &GraphEdit::_graph_node_raised);
  882. ClassDB::bind_method(D_METHOD("_top_layer_input"), &GraphEdit::_top_layer_input);
  883. ClassDB::bind_method(D_METHOD("_top_layer_draw"), &GraphEdit::_top_layer_draw);
  884. ClassDB::bind_method(D_METHOD("_scroll_moved"), &GraphEdit::_scroll_moved);
  885. ClassDB::bind_method(D_METHOD("_zoom_minus"), &GraphEdit::_zoom_minus);
  886. ClassDB::bind_method(D_METHOD("_zoom_reset"), &GraphEdit::_zoom_reset);
  887. ClassDB::bind_method(D_METHOD("_zoom_plus"), &GraphEdit::_zoom_plus);
  888. ClassDB::bind_method(D_METHOD("_snap_toggled"), &GraphEdit::_snap_toggled);
  889. ClassDB::bind_method(D_METHOD("_snap_value_changed"), &GraphEdit::_snap_value_changed);
  890. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  891. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  892. ClassDB::bind_method(D_METHOD("_connections_layer_draw"), &GraphEdit::_connections_layer_draw);
  893. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  894. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  895. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  896. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "p_position")));
  897. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  898. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node")));
  899. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_pos")));
  900. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  901. ADD_SIGNAL(MethodInfo("_begin_node_move"));
  902. ADD_SIGNAL(MethodInfo("_end_node_move"));
  903. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  904. }
  905. GraphEdit::GraphEdit() {
  906. set_focus_mode(FOCUS_ALL);
  907. awaiting_scroll_offset_update = false;
  908. top_layer = NULL;
  909. top_layer = memnew(GraphEditFilter(this));
  910. add_child(top_layer);
  911. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  912. top_layer->set_area_as_parent_rect();
  913. top_layer->connect("draw", this, "_top_layer_draw");
  914. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  915. top_layer->connect("gui_input", this, "_top_layer_input");
  916. connections_layer = memnew(Control);
  917. add_child(connections_layer);
  918. connections_layer->connect("draw", this, "_connections_layer_draw");
  919. connections_layer->set_name("CLAYER");
  920. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offseted
  921. h_scroll = memnew(HScrollBar);
  922. h_scroll->set_name("_h_scroll");
  923. top_layer->add_child(h_scroll);
  924. v_scroll = memnew(VScrollBar);
  925. v_scroll->set_name("_v_scroll");
  926. top_layer->add_child(v_scroll);
  927. updating = false;
  928. connecting = false;
  929. right_disconnects = false;
  930. box_selecting = false;
  931. dragging = false;
  932. //set large minmax so it can scroll even if not resized yet
  933. h_scroll->set_min(-10000);
  934. h_scroll->set_max(10000);
  935. v_scroll->set_min(-10000);
  936. v_scroll->set_max(10000);
  937. h_scroll->connect("value_changed", this, "_scroll_moved");
  938. v_scroll->connect("value_changed", this, "_scroll_moved");
  939. zoom = 1;
  940. HBoxContainer *zoom_hb = memnew(HBoxContainer);
  941. top_layer->add_child(zoom_hb);
  942. zoom_hb->set_position(Vector2(10, 10));
  943. zoom_minus = memnew(ToolButton);
  944. zoom_hb->add_child(zoom_minus);
  945. zoom_minus->connect("pressed", this, "_zoom_minus");
  946. zoom_minus->set_focus_mode(FOCUS_NONE);
  947. zoom_reset = memnew(ToolButton);
  948. zoom_hb->add_child(zoom_reset);
  949. zoom_reset->connect("pressed", this, "_zoom_reset");
  950. zoom_reset->set_focus_mode(FOCUS_NONE);
  951. zoom_plus = memnew(ToolButton);
  952. zoom_hb->add_child(zoom_plus);
  953. zoom_plus->connect("pressed", this, "_zoom_plus");
  954. zoom_plus->set_focus_mode(FOCUS_NONE);
  955. snap_button = memnew(ToolButton);
  956. snap_button->set_toggle_mode(true);
  957. snap_button->connect("pressed", this, "_snap_toggled");
  958. snap_button->set_pressed(true);
  959. snap_button->set_focus_mode(FOCUS_NONE);
  960. zoom_hb->add_child(snap_button);
  961. snap_amount = memnew(SpinBox);
  962. snap_amount->set_min(5);
  963. snap_amount->set_max(100);
  964. snap_amount->set_step(1);
  965. snap_amount->set_value(20);
  966. snap_amount->connect("value_changed", this, "_snap_value_changed");
  967. zoom_hb->add_child(snap_amount);
  968. setting_scroll_ofs = false;
  969. just_disconected = false;
  970. set_clip_contents(true);
  971. }