graph_edit.cpp 33 KB

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