graph_edit.cpp 35 KB

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