graph_edit.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://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 = Object::cast_to<GraphNode>(get_child(i));
  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 = Object::cast_to<GraphNode>(get_child(i));
  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 = Object::cast_to<GraphNode>(p_gn);
  159. ERR_FAIL_COND(!gn);
  160. if (gn->is_comment()) {
  161. move_child(gn, 0);
  162. } else {
  163. gn->raise();
  164. }
  165. int first_not_comment = 0;
  166. for (int i = 0; i < get_child_count(); i++) {
  167. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  168. if (gn && !gn->is_comment()) {
  169. first_not_comment = i;
  170. break;
  171. }
  172. }
  173. move_child(connections_layer, first_not_comment);
  174. top_layer->raise();
  175. emit_signal("node_selected", p_gn);
  176. }
  177. void GraphEdit::_graph_node_moved(Node *p_gn) {
  178. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  179. ERR_FAIL_COND(!gn);
  180. top_layer->update();
  181. update();
  182. connections_layer->update();
  183. }
  184. void GraphEdit::add_child_notify(Node *p_child) {
  185. Control::add_child_notify(p_child);
  186. top_layer->call_deferred("raise"); //top layer always on top!
  187. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  188. if (gn) {
  189. gn->set_scale(Vector2(zoom, zoom));
  190. gn->connect("offset_changed", this, "_graph_node_moved", varray(gn));
  191. gn->connect("raise_request", this, "_graph_node_raised", varray(gn));
  192. gn->connect("item_rect_changed", connections_layer, "update");
  193. _graph_node_moved(gn);
  194. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  195. }
  196. }
  197. void GraphEdit::remove_child_notify(Node *p_child) {
  198. Control::remove_child_notify(p_child);
  199. top_layer->call_deferred("raise"); //top layer always on top!
  200. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  201. if (gn) {
  202. gn->disconnect("offset_changed", this, "_graph_node_moved");
  203. gn->disconnect("raise_request", this, "_graph_node_raised");
  204. }
  205. }
  206. void GraphEdit::_notification(int p_what) {
  207. if (p_what == NOTIFICATION_READY) {
  208. Size2 hmin = h_scroll->get_combined_minimum_size();
  209. Size2 vmin = v_scroll->get_combined_minimum_size();
  210. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  211. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  212. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  213. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  214. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  215. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  216. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  217. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  218. zoom_minus->set_icon(get_icon("minus"));
  219. zoom_reset->set_icon(get_icon("reset"));
  220. zoom_plus->set_icon(get_icon("more"));
  221. snap_button->set_icon(get_icon("snap"));
  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 = Object::cast_to<GraphNode>(get_child(i));
  265. if (!gn)
  266. continue;
  267. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  268. Vector2 pos = gn->get_connection_output_position(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_position(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 = Object::cast_to<GraphNode>(get_child(i));
  290. if (!gn)
  291. continue;
  292. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  293. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  294. if (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 (Object::cast_to<GraphNode>(to)) {
  301. connecting_from = E->get().to;
  302. connecting_index = E->get().to_port;
  303. connecting_out = false;
  304. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
  305. connecting_color = Object::cast_to<GraphNode>(to)->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 (Object::cast_to<GraphNode>(to)) {
  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_position(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 (Object::cast_to<GraphNode>(fr)) {
  340. connecting_from = E->get().from;
  341. connecting_index = E->get().from_port;
  342. connecting_out = true;
  343. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
  344. connecting_color = Object::cast_to<GraphNode>(fr)->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 (Object::cast_to<GraphNode>(fr)) {
  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 = 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))) && 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_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))) && 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(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 {
  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. points.push_back((beg + end) * 0.5);
  454. colors.push_back(p_color.linear_interpolate(p_to_color, mp));
  455. lines++;
  456. } else {
  457. _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);
  458. _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);
  459. }
  460. }
  461. 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) {
  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 = MIN(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. Vector<Point2> points;
  476. Vector<Color> colors;
  477. points.push_back(p_from);
  478. colors.push_back(p_color);
  479. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 8, p_color, p_to_color, lines);
  480. points.push_back(p_to);
  481. colors.push_back(p_to_color);
  482. p_where->draw_polyline_colors(points, colors, 2, true);
  483. }
  484. void GraphEdit::_connections_layer_draw() {
  485. //draw connections
  486. List<List<Connection>::Element *> to_erase;
  487. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  488. NodePath fromnp(E->get().from);
  489. Node *from = get_node(fromnp);
  490. if (!from) {
  491. to_erase.push_back(E);
  492. continue;
  493. }
  494. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  495. if (!gfrom) {
  496. to_erase.push_back(E);
  497. continue;
  498. }
  499. NodePath tonp(E->get().to);
  500. Node *to = get_node(tonp);
  501. if (!to) {
  502. to_erase.push_back(E);
  503. continue;
  504. }
  505. GraphNode *gto = Object::cast_to<GraphNode>(to);
  506. if (!gto) {
  507. to_erase.push_back(E);
  508. continue;
  509. }
  510. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_offset() * zoom;
  511. Color color = gfrom->get_connection_output_color(E->get().from_port);
  512. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_offset() * zoom;
  513. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  514. _draw_cos_line(connections_layer, frompos, topos, color, tocolor);
  515. }
  516. while (to_erase.size()) {
  517. connections.erase(to_erase.front()->get());
  518. to_erase.pop_front();
  519. }
  520. }
  521. void GraphEdit::_top_layer_draw() {
  522. _update_scroll();
  523. if (connecting) {
  524. Node *fromn = get_node(connecting_from);
  525. ERR_FAIL_COND(!fromn);
  526. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  527. ERR_FAIL_COND(!from);
  528. Vector2 pos;
  529. if (connecting_out)
  530. pos = from->get_connection_output_position(connecting_index);
  531. else
  532. pos = from->get_connection_input_position(connecting_index);
  533. pos += from->get_position();
  534. Vector2 topos;
  535. topos = connecting_to;
  536. Color col = connecting_color;
  537. if (connecting_target) {
  538. col.r += 0.4;
  539. col.g += 0.4;
  540. col.b += 0.4;
  541. }
  542. if (!connecting_out) {
  543. SWAP(pos, topos);
  544. }
  545. _draw_cos_line(top_layer, pos, topos, col, col);
  546. }
  547. if (box_selecting)
  548. top_layer->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3));
  549. }
  550. void GraphEdit::set_selected(Node *p_child) {
  551. for (int i = get_child_count() - 1; i >= 0; i--) {
  552. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  553. if (!gn)
  554. continue;
  555. gn->set_selected(gn == p_child);
  556. }
  557. }
  558. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  559. Ref<InputEventMouseMotion> mm = p_ev;
  560. 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)))) {
  561. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  562. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  563. }
  564. if (mm.is_valid() && dragging) {
  565. just_selected = true;
  566. // TODO: Remove local mouse pos hack if/when InputEventMouseMotion is fixed to support floats
  567. //drag_accum+=Vector2(mm->get_relative().x,mm->get_relative().y);
  568. drag_accum = get_local_mouse_position() - drag_origin;
  569. for (int i = get_child_count() - 1; i >= 0; i--) {
  570. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  571. if (gn && gn->is_selected()) {
  572. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  573. if (is_using_snap()) {
  574. int snap = get_snap();
  575. pos = pos.snapped(Vector2(snap, snap));
  576. }
  577. gn->set_offset(pos);
  578. }
  579. }
  580. }
  581. if (mm.is_valid() && box_selecting) {
  582. box_selecting_to = get_local_mouse_position();
  583. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  584. MIN(box_selecting_from.y, box_selecting_to.y),
  585. ABS(box_selecting_from.x - box_selecting_to.x),
  586. ABS(box_selecting_from.y - box_selecting_to.y));
  587. for (int i = get_child_count() - 1; i >= 0; i--) {
  588. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  589. if (!gn)
  590. continue;
  591. Rect2 r = gn->get_rect();
  592. r.size *= zoom;
  593. bool in_box = r.intersects(box_selecting_rect);
  594. if (in_box)
  595. gn->set_selected(box_selection_mode_aditive);
  596. else
  597. gn->set_selected(previus_selected.find(gn) != NULL);
  598. }
  599. top_layer->update();
  600. }
  601. Ref<InputEventMouseButton> b = p_ev;
  602. if (b.is_valid()) {
  603. if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
  604. if (box_selecting) {
  605. box_selecting = false;
  606. for (int i = get_child_count() - 1; i >= 0; i--) {
  607. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  608. if (!gn)
  609. continue;
  610. gn->set_selected(previus_selected.find(gn) != NULL);
  611. }
  612. top_layer->update();
  613. } else {
  614. if (connecting) {
  615. connecting = false;
  616. top_layer->update();
  617. } else {
  618. emit_signal("popup_request", b->get_global_position());
  619. }
  620. }
  621. }
  622. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
  623. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  624. //deselect current node
  625. for (int i = get_child_count() - 1; i >= 0; i--) {
  626. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  627. if (gn) {
  628. Rect2 r = gn->get_rect();
  629. r.size *= zoom;
  630. if (r.has_point(get_local_mouse_position()))
  631. gn->set_selected(false);
  632. }
  633. }
  634. }
  635. if (drag_accum != Vector2()) {
  636. emit_signal("_begin_node_move");
  637. for (int i = get_child_count() - 1; i >= 0; i--) {
  638. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  639. if (gn && gn->is_selected())
  640. gn->set_drag(false);
  641. }
  642. emit_signal("_end_node_move");
  643. }
  644. dragging = false;
  645. top_layer->update();
  646. update();
  647. connections_layer->update();
  648. }
  649. if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {
  650. GraphNode *gn = NULL;
  651. for (int i = get_child_count() - 1; i >= 0; i--) {
  652. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  653. if (gn_selected) {
  654. if (gn_selected->is_resizing())
  655. continue;
  656. if (gn_selected->has_point(gn_selected->get_local_mouse_position())) {
  657. gn = gn_selected;
  658. break;
  659. }
  660. }
  661. }
  662. if (gn) {
  663. if (_filter_input(b->get_position()))
  664. return;
  665. dragging = true;
  666. drag_accum = Vector2();
  667. drag_origin = get_local_mouse_position();
  668. just_selected = !gn->is_selected();
  669. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  670. for (int i = 0; i < get_child_count(); i++) {
  671. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  672. if (o_gn)
  673. o_gn->set_selected(o_gn == gn);
  674. }
  675. }
  676. gn->set_selected(true);
  677. for (int i = 0; i < get_child_count(); i++) {
  678. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  679. if (!o_gn)
  680. continue;
  681. if (o_gn->is_selected())
  682. o_gn->set_drag(true);
  683. }
  684. } else {
  685. if (_filter_input(b->get_position()))
  686. return;
  687. if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
  688. return;
  689. box_selecting = true;
  690. box_selecting_from = get_local_mouse_position();
  691. if (b->get_control()) {
  692. box_selection_mode_aditive = true;
  693. previus_selected.clear();
  694. for (int i = get_child_count() - 1; i >= 0; i--) {
  695. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  696. if (!gn || !gn->is_selected())
  697. continue;
  698. previus_selected.push_back(gn);
  699. }
  700. } else if (b->get_shift()) {
  701. box_selection_mode_aditive = false;
  702. previus_selected.clear();
  703. for (int i = get_child_count() - 1; i >= 0; i--) {
  704. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  705. if (!gn || !gn->is_selected())
  706. continue;
  707. previus_selected.push_back(gn);
  708. }
  709. } else {
  710. box_selection_mode_aditive = true;
  711. previus_selected.clear();
  712. for (int i = get_child_count() - 1; i >= 0; i--) {
  713. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  714. if (!gn)
  715. continue;
  716. gn->set_selected(false);
  717. }
  718. }
  719. }
  720. }
  721. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  722. box_selecting = false;
  723. previus_selected.clear();
  724. top_layer->update();
  725. }
  726. if (b->get_button_index() == BUTTON_WHEEL_UP && b->is_pressed()) {
  727. //too difficult to get right
  728. //set_zoom(zoom*ZOOM_SCALE);
  729. }
  730. if (b->get_button_index() == BUTTON_WHEEL_DOWN && b->is_pressed()) {
  731. //too difficult to get right
  732. //set_zoom(zoom/ZOOM_SCALE);
  733. }
  734. if (b->get_button_index() == BUTTON_WHEEL_UP && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  735. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  736. }
  737. if (b->get_button_index() == BUTTON_WHEEL_DOWN && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  738. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  739. }
  740. if (b->get_button_index() == BUTTON_WHEEL_RIGHT || (b->get_button_index() == BUTTON_WHEEL_DOWN && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  741. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b->get_factor() / 8);
  742. }
  743. if (b->get_button_index() == BUTTON_WHEEL_LEFT || (b->get_button_index() == BUTTON_WHEEL_UP && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  744. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b->get_factor() / 8);
  745. }
  746. }
  747. Ref<InputEventKey> k = p_ev;
  748. if (k.is_valid() && k->get_scancode() == KEY_D && k->is_pressed() && k->get_command()) {
  749. emit_signal("duplicate_nodes_request");
  750. accept_event();
  751. }
  752. if (k.is_valid() && k->get_scancode() == KEY_DELETE && k->is_pressed()) {
  753. emit_signal("delete_nodes_request");
  754. accept_event();
  755. }
  756. }
  757. void GraphEdit::clear_connections() {
  758. connections.clear();
  759. update();
  760. connections_layer->update();
  761. }
  762. void GraphEdit::set_zoom(float p_zoom) {
  763. p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
  764. if (zoom == p_zoom)
  765. return;
  766. zoom_minus->set_disabled(zoom == MIN_ZOOM);
  767. zoom_plus->set_disabled(zoom == MAX_ZOOM);
  768. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + get_size() / 2) / zoom;
  769. zoom = p_zoom;
  770. top_layer->update();
  771. _update_scroll();
  772. connections_layer->update();
  773. if (is_visible_in_tree()) {
  774. Vector2 ofs = sbofs * zoom - get_size() / 2;
  775. h_scroll->set_value(ofs.x);
  776. v_scroll->set_value(ofs.y);
  777. }
  778. update();
  779. }
  780. float GraphEdit::get_zoom() const {
  781. return zoom;
  782. }
  783. void GraphEdit::set_right_disconnects(bool p_enable) {
  784. right_disconnects = p_enable;
  785. }
  786. bool GraphEdit::is_right_disconnects_enabled() const {
  787. return right_disconnects;
  788. }
  789. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  790. valid_right_disconnect_types.insert(p_type);
  791. }
  792. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  793. valid_right_disconnect_types.erase(p_type);
  794. }
  795. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  796. valid_left_disconnect_types.insert(p_type);
  797. }
  798. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  799. valid_left_disconnect_types.erase(p_type);
  800. }
  801. Array GraphEdit::_get_connection_list() const {
  802. List<Connection> conns;
  803. get_connection_list(&conns);
  804. Array arr;
  805. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  806. Dictionary d;
  807. d["from"] = E->get().from;
  808. d["from_port"] = E->get().from_port;
  809. d["to"] = E->get().to;
  810. d["to_port"] = E->get().to_port;
  811. arr.push_back(d);
  812. }
  813. return arr;
  814. }
  815. void GraphEdit::_zoom_minus() {
  816. set_zoom(zoom / ZOOM_SCALE);
  817. }
  818. void GraphEdit::_zoom_reset() {
  819. set_zoom(1);
  820. }
  821. void GraphEdit::_zoom_plus() {
  822. set_zoom(zoom * ZOOM_SCALE);
  823. }
  824. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  825. ConnType ct;
  826. ct.type_a = p_type;
  827. ct.type_b = p_with_type;
  828. valid_connection_types.insert(ct);
  829. }
  830. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  831. ConnType ct;
  832. ct.type_a = p_type;
  833. ct.type_b = p_with_type;
  834. valid_connection_types.erase(ct);
  835. }
  836. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  837. ConnType ct;
  838. ct.type_a = p_type;
  839. ct.type_b = p_with_type;
  840. return valid_connection_types.has(ct);
  841. }
  842. void GraphEdit::set_use_snap(bool p_enable) {
  843. snap_button->set_pressed(p_enable);
  844. update();
  845. }
  846. bool GraphEdit::is_using_snap() const {
  847. return snap_button->is_pressed();
  848. }
  849. int GraphEdit::get_snap() const {
  850. return snap_amount->get_value();
  851. }
  852. void GraphEdit::set_snap(int p_snap) {
  853. ERR_FAIL_COND(p_snap < 5);
  854. snap_amount->set_value(p_snap);
  855. update();
  856. }
  857. void GraphEdit::_snap_toggled() {
  858. update();
  859. }
  860. void GraphEdit::_snap_value_changed(double) {
  861. update();
  862. }
  863. void GraphEdit::_bind_methods() {
  864. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  865. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  866. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  867. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  868. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  869. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  870. ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
  871. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  872. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  873. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  874. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  875. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  876. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  877. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  878. ClassDB::bind_method(D_METHOD("_graph_node_moved"), &GraphEdit::_graph_node_moved);
  879. ClassDB::bind_method(D_METHOD("_graph_node_raised"), &GraphEdit::_graph_node_raised);
  880. ClassDB::bind_method(D_METHOD("_top_layer_input"), &GraphEdit::_top_layer_input);
  881. ClassDB::bind_method(D_METHOD("_top_layer_draw"), &GraphEdit::_top_layer_draw);
  882. ClassDB::bind_method(D_METHOD("_scroll_moved"), &GraphEdit::_scroll_moved);
  883. ClassDB::bind_method(D_METHOD("_zoom_minus"), &GraphEdit::_zoom_minus);
  884. ClassDB::bind_method(D_METHOD("_zoom_reset"), &GraphEdit::_zoom_reset);
  885. ClassDB::bind_method(D_METHOD("_zoom_plus"), &GraphEdit::_zoom_plus);
  886. ClassDB::bind_method(D_METHOD("_snap_toggled"), &GraphEdit::_snap_toggled);
  887. ClassDB::bind_method(D_METHOD("_snap_value_changed"), &GraphEdit::_snap_value_changed);
  888. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  889. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  890. ClassDB::bind_method(D_METHOD("_connections_layer_draw"), &GraphEdit::_connections_layer_draw);
  891. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  892. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  893. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  894. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "p_position")));
  895. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  896. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node")));
  897. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  898. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  899. ADD_SIGNAL(MethodInfo("_begin_node_move"));
  900. ADD_SIGNAL(MethodInfo("_end_node_move"));
  901. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  902. }
  903. GraphEdit::GraphEdit() {
  904. set_focus_mode(FOCUS_ALL);
  905. awaiting_scroll_offset_update = false;
  906. top_layer = NULL;
  907. top_layer = memnew(GraphEditFilter(this));
  908. add_child(top_layer);
  909. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  910. top_layer->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  911. top_layer->connect("draw", this, "_top_layer_draw");
  912. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  913. top_layer->connect("gui_input", this, "_top_layer_input");
  914. connections_layer = memnew(Control);
  915. add_child(connections_layer);
  916. connections_layer->connect("draw", this, "_connections_layer_draw");
  917. connections_layer->set_name("CLAYER");
  918. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offseted
  919. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  920. h_scroll = memnew(HScrollBar);
  921. h_scroll->set_name("_h_scroll");
  922. top_layer->add_child(h_scroll);
  923. v_scroll = memnew(VScrollBar);
  924. v_scroll->set_name("_v_scroll");
  925. top_layer->add_child(v_scroll);
  926. updating = false;
  927. connecting = false;
  928. right_disconnects = false;
  929. box_selecting = false;
  930. dragging = false;
  931. //set large minmax so it can scroll even if not resized yet
  932. h_scroll->set_min(-10000);
  933. h_scroll->set_max(10000);
  934. v_scroll->set_min(-10000);
  935. v_scroll->set_max(10000);
  936. h_scroll->connect("value_changed", this, "_scroll_moved");
  937. v_scroll->connect("value_changed", this, "_scroll_moved");
  938. zoom = 1;
  939. HBoxContainer *zoom_hb = memnew(HBoxContainer);
  940. top_layer->add_child(zoom_hb);
  941. zoom_hb->set_position(Vector2(10, 10));
  942. zoom_minus = memnew(ToolButton);
  943. zoom_hb->add_child(zoom_minus);
  944. zoom_minus->connect("pressed", this, "_zoom_minus");
  945. zoom_minus->set_focus_mode(FOCUS_NONE);
  946. zoom_reset = memnew(ToolButton);
  947. zoom_hb->add_child(zoom_reset);
  948. zoom_reset->connect("pressed", this, "_zoom_reset");
  949. zoom_reset->set_focus_mode(FOCUS_NONE);
  950. zoom_plus = memnew(ToolButton);
  951. zoom_hb->add_child(zoom_plus);
  952. zoom_plus->connect("pressed", this, "_zoom_plus");
  953. zoom_plus->set_focus_mode(FOCUS_NONE);
  954. snap_button = memnew(ToolButton);
  955. snap_button->set_toggle_mode(true);
  956. snap_button->connect("pressed", this, "_snap_toggled");
  957. snap_button->set_pressed(true);
  958. snap_button->set_focus_mode(FOCUS_NONE);
  959. zoom_hb->add_child(snap_button);
  960. snap_amount = memnew(SpinBox);
  961. snap_amount->set_min(5);
  962. snap_amount->set_max(100);
  963. snap_amount->set_step(1);
  964. snap_amount->set_value(20);
  965. snap_amount->connect("value_changed", this, "_snap_value_changed");
  966. zoom_hb->add_child(snap_amount);
  967. setting_scroll_ofs = false;
  968. just_disconected = false;
  969. set_clip_contents(true);
  970. }