graph_edit.cpp 35 KB

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