graph_edit.cpp 35 KB

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