graph_edit.cpp 37 KB

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