graph_edit.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. #include "graph_edit.h"
  2. #include "os/input.h"
  3. #include "os/keyboard.h"
  4. bool GraphEditFilter::has_point(const Point2& p_point) const {
  5. return ge->_filter_input(p_point);
  6. }
  7. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  8. ge=p_edit;
  9. }
  10. Error GraphEdit::connect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port) {
  11. if (is_node_connected(p_from,p_from_port,p_to,p_to_port))
  12. return OK;
  13. Connection c;
  14. c.from=p_from;
  15. c.from_port=p_from_port;
  16. c.to=p_to;
  17. c.to_port=p_to_port;
  18. connections.push_back(c);
  19. top_layer->update();
  20. return OK;
  21. }
  22. bool GraphEdit::is_node_connected(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port) {
  23. for(List<Connection>::Element *E=connections.front();E;E=E->next()) {
  24. 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)
  25. return true;
  26. }
  27. return false;
  28. }
  29. void GraphEdit::disconnect_node(const StringName& p_from, int p_from_port,const StringName& p_to,int p_to_port){
  30. for(List<Connection>::Element *E=connections.front();E;E=E->next()) {
  31. 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) {
  32. connections.erase(E);
  33. top_layer->update();
  34. return;
  35. }
  36. }
  37. }
  38. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  39. *r_connections=connections;
  40. }
  41. Vector2 GraphEdit::get_scroll_ofs() const{
  42. return Vector2(h_scroll->get_val(),v_scroll->get_val());
  43. }
  44. void GraphEdit::_scroll_moved(double) {
  45. _update_scroll_offset();
  46. top_layer->update();
  47. }
  48. void GraphEdit::_update_scroll_offset() {
  49. for(int i=0;i<get_child_count();i++) {
  50. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  51. if (!gn)
  52. continue;
  53. Point2 pos=gn->get_offset();
  54. pos-=Point2(h_scroll->get_val(),v_scroll->get_val());
  55. gn->set_pos(pos);
  56. }
  57. }
  58. void GraphEdit::_update_scroll() {
  59. if (updating)
  60. return;
  61. updating=true;
  62. Rect2 screen;
  63. for(int i=0;i<get_child_count();i++) {
  64. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  65. if (!gn)
  66. continue;
  67. Rect2 r;
  68. r.pos=gn->get_offset();
  69. r.size=gn->get_size();
  70. screen = screen.merge(r);
  71. }
  72. screen.pos-=get_size();
  73. screen.size+=get_size()*2.0;
  74. h_scroll->set_min(screen.pos.x);
  75. h_scroll->set_max(screen.pos.x+screen.size.x);
  76. h_scroll->set_page(get_size().x);
  77. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page())
  78. h_scroll->hide();
  79. else
  80. h_scroll->show();
  81. v_scroll->set_min(screen.pos.y);
  82. v_scroll->set_max(screen.pos.y+screen.size.y);
  83. v_scroll->set_page(get_size().y);
  84. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page())
  85. v_scroll->hide();
  86. else
  87. v_scroll->show();
  88. _update_scroll_offset();
  89. updating=false;
  90. }
  91. void GraphEdit::_graph_node_raised(Node* p_gn) {
  92. GraphNode *gn=p_gn->cast_to<GraphNode>();
  93. ERR_FAIL_COND(!gn);
  94. gn->raise();
  95. top_layer->raise();
  96. }
  97. void GraphEdit::_graph_node_moved(Node *p_gn) {
  98. GraphNode *gn=p_gn->cast_to<GraphNode>();
  99. ERR_FAIL_COND(!gn);
  100. top_layer->update();
  101. }
  102. void GraphEdit::add_child_notify(Node *p_child) {
  103. top_layer->call_deferred("raise"); //top layer always on top!
  104. GraphNode *gn = p_child->cast_to<GraphNode>();
  105. if (gn) {
  106. gn->set_scale(Vector2(zoom,zoom));
  107. gn->connect("offset_changed",this,"_graph_node_moved",varray(gn));
  108. gn->connect("raise_request",this,"_graph_node_raised",varray(gn));
  109. _graph_node_moved(gn);
  110. gn->set_stop_mouse(false);
  111. }
  112. }
  113. void GraphEdit::remove_child_notify(Node *p_child) {
  114. top_layer->call_deferred("raise"); //top layer always on top!
  115. GraphNode *gn = p_child->cast_to<GraphNode>();
  116. if (gn) {
  117. gn->disconnect("offset_changed",this,"_graph_node_moved");
  118. gn->disconnect("raise_request",this,"_graph_node_raised");
  119. }
  120. }
  121. void GraphEdit::_notification(int p_what) {
  122. if (p_what==NOTIFICATION_READY) {
  123. Size2 hmin = h_scroll->get_combined_minimum_size();
  124. Size2 vmin = v_scroll->get_combined_minimum_size();
  125. v_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,vmin.width);
  126. v_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
  127. v_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,0);
  128. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
  129. h_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,0);
  130. h_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
  131. h_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,hmin.height);
  132. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
  133. }
  134. if (p_what==NOTIFICATION_DRAW) {
  135. VS::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
  136. }
  137. if (p_what==NOTIFICATION_RESIZED) {
  138. _update_scroll();
  139. top_layer->update();
  140. }
  141. }
  142. bool GraphEdit::_filter_input(const Point2& p_point) {
  143. Ref<Texture> port =get_icon("port","GraphNode");
  144. float grab_r=port->get_width()*0.5;
  145. for(int i=get_child_count()-1;i>=0;i--) {
  146. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  147. if (!gn)
  148. continue;
  149. for(int j=0;j<gn->get_connection_output_count();j++) {
  150. Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos();
  151. if (pos.distance_to(p_point)<grab_r)
  152. return true;
  153. }
  154. for(int j=0;j<gn->get_connection_input_count();j++) {
  155. Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos();
  156. if (pos.distance_to(p_point)<grab_r)
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. void GraphEdit::_top_layer_input(const InputEvent& p_ev) {
  163. if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index==BUTTON_LEFT && p_ev.mouse_button.pressed) {
  164. Ref<Texture> port =get_icon("port","GraphNode");
  165. Vector2 mpos(p_ev.mouse_button.x,p_ev.mouse_button.y);
  166. float grab_r=port->get_width()*0.5;
  167. for(int i=get_child_count()-1;i>=0;i--) {
  168. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  169. if (!gn)
  170. continue;
  171. for(int j=0;j<gn->get_connection_output_count();j++) {
  172. Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos();
  173. if (pos.distance_to(mpos)<grab_r) {
  174. connecting=true;
  175. connecting_from=gn->get_name();
  176. connecting_index=j;
  177. connecting_out=true;
  178. connecting_type=gn->get_connection_output_type(j);
  179. connecting_color=gn->get_connection_output_color(j);
  180. connecting_target=false;
  181. connecting_to=pos;
  182. return;
  183. }
  184. }
  185. for(int j=0;j<gn->get_connection_input_count();j++) {
  186. Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos();
  187. if (pos.distance_to(mpos)<grab_r) {
  188. if (right_disconnects) {
  189. //check disconnect
  190. for (List<Connection>::Element*E=connections.front();E;E=E->next()) {
  191. if (E->get().to==gn->get_name() && E->get().to_port==j) {
  192. Node*fr = get_node(String(E->get().from));
  193. if (fr && fr->cast_to<GraphNode>()) {
  194. connecting_from=E->get().from;
  195. connecting_index=E->get().from_port;
  196. connecting_out=true;
  197. connecting_type=fr->cast_to<GraphNode>()->get_connection_output_type(E->get().from_port);
  198. connecting_color=fr->cast_to<GraphNode>()->get_connection_output_color(E->get().from_port);
  199. connecting_target=false;
  200. connecting_to=pos;
  201. emit_signal("disconnection_request",E->get().from,E->get().from_port,E->get().to,E->get().to_port);
  202. fr = get_node(String(connecting_from)); //maybe it was erased
  203. if (fr && fr->cast_to<GraphNode>()) {
  204. connecting=true;
  205. }
  206. return;
  207. }
  208. }
  209. }
  210. }
  211. connecting=true;
  212. connecting_from=gn->get_name();
  213. connecting_index=j;
  214. connecting_out=false;
  215. connecting_type=gn->get_connection_input_type(j);
  216. connecting_color=gn->get_connection_input_color(j);
  217. connecting_target=false;
  218. connecting_to=pos;
  219. return;
  220. }
  221. }
  222. }
  223. }
  224. if (p_ev.type==InputEvent::MOUSE_MOTION && connecting) {
  225. connecting_to=Vector2(p_ev.mouse_motion.x,p_ev.mouse_motion.y);
  226. connecting_target=false;
  227. top_layer->update();
  228. Ref<Texture> port =get_icon("port","GraphNode");
  229. Vector2 mpos(p_ev.mouse_button.x,p_ev.mouse_button.y);
  230. float grab_r=port->get_width()*0.5;
  231. for(int i=get_child_count()-1;i>=0;i--) {
  232. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  233. if (!gn)
  234. continue;
  235. if (!connecting_out) {
  236. for(int j=0;j<gn->get_connection_output_count();j++) {
  237. Vector2 pos = gn->get_connection_output_pos(j)+gn->get_pos();
  238. int type =gn->get_connection_output_type(j);
  239. if (type==connecting_type && pos.distance_to(mpos)<grab_r) {
  240. connecting_target=true;
  241. connecting_to=pos;
  242. connecting_target_to=gn->get_name();
  243. connecting_target_index=j;
  244. return;
  245. }
  246. }
  247. } else {
  248. for(int j=0;j<gn->get_connection_input_count();j++) {
  249. Vector2 pos = gn->get_connection_input_pos(j)+gn->get_pos();
  250. int type =gn->get_connection_input_type(j);
  251. if (type==connecting_type && pos.distance_to(mpos)<grab_r) {
  252. connecting_target=true;
  253. connecting_to=pos;
  254. connecting_target_to=gn->get_name();
  255. connecting_target_index=j;
  256. return;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. if (p_ev.type==InputEvent::MOUSE_BUTTON && p_ev.mouse_button.button_index==BUTTON_LEFT && !p_ev.mouse_button.pressed) {
  263. if (connecting && connecting_target) {
  264. String from = connecting_from;
  265. int from_slot = connecting_index;
  266. String to =connecting_target_to;
  267. int to_slot = connecting_target_index;
  268. if (!connecting_out) {
  269. SWAP(from,to);
  270. SWAP(from_slot,to_slot);
  271. }
  272. emit_signal("connection_request",from,from_slot,to,to_slot);
  273. }
  274. connecting=false;
  275. top_layer->update();
  276. }
  277. }
  278. void GraphEdit::_draw_cos_line(const Vector2& p_from, const Vector2& p_to,const Color& p_color) {
  279. static const int steps = 20;
  280. Rect2 r;
  281. r.pos=p_from;
  282. r.expand_to(p_to);
  283. Vector2 sign=Vector2((p_from.x < p_to.x) ? 1 : -1,(p_from.y < p_to.y) ? 1 : -1);
  284. bool flip = sign.x * sign.y < 0;
  285. Vector2 prev;
  286. for(int i=0;i<=steps;i++) {
  287. float d = i/float(steps);
  288. float c=-Math::cos(d*Math_PI) * 0.5+0.5;
  289. if (flip)
  290. c=1.0-c;
  291. Vector2 p = r.pos+Vector2(d*r.size.width,c*r.size.height);
  292. if (i>0) {
  293. top_layer->draw_line(prev,p,p_color,2);
  294. }
  295. prev=p;
  296. }
  297. }
  298. void GraphEdit::_top_layer_draw() {
  299. _update_scroll();
  300. if (connecting) {
  301. Node *fromn = get_node(connecting_from);
  302. ERR_FAIL_COND(!fromn);
  303. GraphNode *from = fromn->cast_to<GraphNode>();
  304. ERR_FAIL_COND(!from);
  305. Vector2 pos;
  306. if (connecting_out)
  307. pos=from->get_connection_output_pos(connecting_index);
  308. else
  309. pos=from->get_connection_input_pos(connecting_index);
  310. pos+=from->get_pos();
  311. Vector2 topos;
  312. topos=connecting_to;
  313. Color col=connecting_color;
  314. if (connecting_target) {
  315. col.r+=0.4;
  316. col.g+=0.4;
  317. col.b+=0.4;
  318. }
  319. _draw_cos_line(pos,topos,col);
  320. }
  321. List<List<Connection>::Element* > to_erase;
  322. for(List<Connection>::Element *E=connections.front();E;E=E->next()) {
  323. NodePath fromnp(E->get().from);
  324. Node * from = get_node(fromnp);
  325. if (!from) {
  326. to_erase.push_back(E);
  327. continue;
  328. }
  329. GraphNode *gfrom = from->cast_to<GraphNode>();
  330. if (!gfrom) {
  331. to_erase.push_back(E);
  332. continue;
  333. }
  334. NodePath tonp(E->get().to);
  335. Node * to = get_node(tonp);
  336. if (!to) {
  337. to_erase.push_back(E);
  338. continue;
  339. }
  340. GraphNode *gto = to->cast_to<GraphNode>();
  341. if (!gto) {
  342. to_erase.push_back(E);
  343. continue;
  344. }
  345. Vector2 frompos=gfrom->get_connection_output_pos(E->get().from_port)+gfrom->get_pos();
  346. Color color = gfrom->get_connection_output_color(E->get().from_port);
  347. Vector2 topos=gto->get_connection_input_pos(E->get().to_port)+gto->get_pos();
  348. _draw_cos_line(frompos,topos,color);
  349. }
  350. while(to_erase.size()) {
  351. connections.erase(to_erase.front()->get());
  352. to_erase.pop_front();
  353. }
  354. if (box_selecting)
  355. top_layer->draw_rect(box_selecting_rect,Color(0.7,0.7,1.0,0.3));
  356. }
  357. void GraphEdit::_input_event(const InputEvent& p_ev) {
  358. 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)))) {
  359. h_scroll->set_val( h_scroll->get_val() - p_ev.mouse_motion.relative_x );
  360. v_scroll->set_val( v_scroll->get_val() - p_ev.mouse_motion.relative_y );
  361. }
  362. if (p_ev.type==InputEvent::MOUSE_MOTION && dragging) {
  363. just_selected=true;
  364. drag_accum+=Vector2(p_ev.mouse_motion.relative_x,p_ev.mouse_motion.relative_y);
  365. for(int i=get_child_count()-1;i>=0;i--) {
  366. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  367. if (gn && gn->is_selected())
  368. gn->set_offset(gn->get_drag_from()+drag_accum);
  369. }
  370. }
  371. if (p_ev.type==InputEvent::MOUSE_MOTION && box_selecting) {
  372. box_selecting_to = get_local_mouse_pos();
  373. box_selecting_rect = Rect2(MIN(box_selecting_from.x,box_selecting_to.x),
  374. MIN(box_selecting_from.y,box_selecting_to.y),
  375. ABS(box_selecting_from.x-box_selecting_to.x),
  376. ABS(box_selecting_from.y-box_selecting_to.y));
  377. for(int i=get_child_count()-1;i>=0;i--) {
  378. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  379. if (!gn)
  380. continue;
  381. Rect2 r = gn->get_rect();
  382. r.size*=zoom;
  383. bool in_box = r.intersects(box_selecting_rect);
  384. if (in_box)
  385. gn->set_selected(box_selection_mode_aditive);
  386. else
  387. gn->set_selected(previus_selected.find(gn)!=NULL);
  388. }
  389. top_layer->update();
  390. }
  391. if (p_ev.type==InputEvent::MOUSE_BUTTON) {
  392. const InputEventMouseButton &b=p_ev.mouse_button;
  393. if (b.button_index==BUTTON_RIGHT && b.pressed)
  394. {
  395. if (box_selecting) {
  396. box_selecting = false;
  397. for(int i=get_child_count()-1;i>=0;i--) {
  398. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  399. if (!gn)
  400. continue;
  401. gn->set_selected(previus_selected.find(gn)!=NULL);
  402. }
  403. top_layer->update();
  404. } else {
  405. if (connecting) {
  406. connecting = false;
  407. top_layer->update();
  408. } else {
  409. emit_signal("popup_request", Vector2(b.global_x, b.global_y));
  410. }
  411. }
  412. }
  413. if (b.button_index==BUTTON_LEFT && !b.pressed && dragging) {
  414. if (!just_selected && drag_accum==Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  415. //deselect current node
  416. for(int i=get_child_count()-1;i>=0;i--) {
  417. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  418. if (gn) {
  419. Rect2 r = gn->get_rect();
  420. r.size*=zoom;
  421. if (r.has_point(get_local_mouse_pos()))
  422. gn->set_selected(false);
  423. }
  424. }
  425. }
  426. if (drag_accum!=Vector2()) {
  427. emit_signal("_begin_node_move");
  428. for(int i=get_child_count()-1;i>=0;i--) {
  429. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  430. if (gn && gn->is_selected())
  431. gn->set_drag(false);
  432. }
  433. emit_signal("_end_node_move");
  434. }
  435. dragging = false;
  436. top_layer->update();
  437. }
  438. if (b.button_index==BUTTON_LEFT && b.pressed) {
  439. GraphNode *gn;
  440. for(int i=get_child_count()-1;i>=0;i--) {
  441. gn=get_child(i)->cast_to<GraphNode>();
  442. if (gn) {
  443. Rect2 r = gn->get_rect();
  444. r.size*=zoom;
  445. if (r.has_point(get_local_mouse_pos()))
  446. break;
  447. }
  448. }
  449. if (gn) {
  450. if (_filter_input(Vector2(b.x,b.y)))
  451. return;
  452. dragging = true;
  453. drag_accum = Vector2();
  454. just_selected = !gn->is_selected();
  455. if(!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  456. for (int i = 0; i < get_child_count(); i++) {
  457. GraphNode *o_gn = get_child(i)->cast_to<GraphNode>();
  458. if (o_gn)
  459. o_gn->set_selected(o_gn == gn);
  460. }
  461. }
  462. gn->set_selected(true);
  463. for (int i = 0; i < get_child_count(); i++) {
  464. GraphNode *o_gn = get_child(i)->cast_to<GraphNode>();
  465. if (!o_gn)
  466. continue;
  467. if (o_gn->is_selected())
  468. o_gn->set_drag(true);
  469. }
  470. } else {
  471. if (_filter_input(Vector2(b.x,b.y)))
  472. return;
  473. box_selecting = true;
  474. box_selecting_from = get_local_mouse_pos();
  475. if (b.mod.control) {
  476. box_selection_mode_aditive = true;
  477. previus_selected.clear();
  478. for(int i=get_child_count()-1;i>=0;i--) {
  479. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  480. if (!gn || !gn->is_selected())
  481. continue;
  482. previus_selected.push_back(gn);
  483. }
  484. } else if (b.mod.shift) {
  485. box_selection_mode_aditive = false;
  486. previus_selected.clear();
  487. for(int i=get_child_count()-1;i>=0;i--) {
  488. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  489. if (!gn || !gn->is_selected())
  490. continue;
  491. previus_selected.push_back(gn);
  492. }
  493. } else {
  494. box_selection_mode_aditive = true;
  495. previus_selected.clear();
  496. for(int i=get_child_count()-1;i>=0;i--) {
  497. GraphNode *gn=get_child(i)->cast_to<GraphNode>();
  498. if (!gn)
  499. continue;
  500. gn->set_selected(false);
  501. }
  502. }
  503. }
  504. }
  505. if (b.button_index==BUTTON_LEFT && !b.pressed && box_selecting) {
  506. box_selecting = false;
  507. previus_selected.clear();
  508. top_layer->update();
  509. }
  510. if (b.button_index==BUTTON_WHEEL_UP && b.pressed) {
  511. set_zoom(zoom/0.9);
  512. }
  513. if (b.button_index==BUTTON_WHEEL_DOWN && b.pressed) {
  514. set_zoom(zoom*0.9);
  515. }
  516. }
  517. if (p_ev.type==InputEvent::KEY && p_ev.key.scancode==KEY_D && p_ev.key.pressed && p_ev.key.mod.command) {
  518. emit_signal("duplicate_nodes_request");
  519. accept_event();
  520. }
  521. if (p_ev.type==InputEvent::KEY && p_ev.key.scancode==KEY_DELETE && p_ev.key.pressed) {
  522. emit_signal("delete_nodes_request");
  523. accept_event();
  524. }
  525. }
  526. void GraphEdit::clear_connections() {
  527. connections.clear();
  528. update();
  529. }
  530. void GraphEdit::set_zoom(float p_zoom) {
  531. if (p_zoom<0.01) p_zoom=0.01;
  532. if (p_zoom>4) p_zoom=4;
  533. if (zoom == p_zoom)
  534. return;
  535. float prev_zoom = zoom;
  536. zoom = p_zoom;
  537. for (int i = 0; i < get_child_count(); i++) {
  538. GraphNode *child = get_child(i)->cast_to<GraphNode>();
  539. if (!child)
  540. continue;
  541. Point2 ofs = child->get_offset() / prev_zoom * zoom;
  542. child->set_scale(Vector2(zoom,zoom));
  543. child->set_offset(ofs);
  544. }
  545. update();
  546. }
  547. float GraphEdit::get_zoom() const {
  548. return zoom;
  549. }
  550. void GraphEdit::set_right_disconnects(bool p_enable) {
  551. right_disconnects=p_enable;
  552. }
  553. bool GraphEdit::is_right_disconnects_enabled() const{
  554. return right_disconnects;
  555. }
  556. Array GraphEdit::_get_connection_list() const {
  557. List<Connection> conns;
  558. get_connection_list(&conns);
  559. Array arr;
  560. for(List<Connection>::Element *E=conns.front();E;E=E->next()) {
  561. Dictionary d;
  562. d["from"]=E->get().from;
  563. d["from_port"]=E->get().from_port;
  564. d["to"]=E->get().to;
  565. d["to_port"]=E->get().to_port;
  566. arr.push_back(d);
  567. }
  568. return arr;
  569. }
  570. void GraphEdit::_bind_methods() {
  571. ObjectTypeDB::bind_method(_MD("connect_node:Error","from","from_port","to","to_port"),&GraphEdit::connect_node);
  572. ObjectTypeDB::bind_method(_MD("is_node_connected","from","from_port","to","to_port"),&GraphEdit::is_node_connected);
  573. ObjectTypeDB::bind_method(_MD("disconnect_node","from","from_port","to","to_port"),&GraphEdit::disconnect_node);
  574. ObjectTypeDB::bind_method(_MD("get_connection_list"),&GraphEdit::_get_connection_list);
  575. ObjectTypeDB::bind_method(_MD("get_scroll_ofs"),&GraphEdit::get_scroll_ofs);
  576. ObjectTypeDB::bind_method(_MD("set_zoom","p_zoom"),&GraphEdit::set_zoom);
  577. ObjectTypeDB::bind_method(_MD("get_zoom"),&GraphEdit::get_zoom);
  578. ObjectTypeDB::bind_method(_MD("set_right_disconnects","enable"),&GraphEdit::set_right_disconnects);
  579. ObjectTypeDB::bind_method(_MD("is_right_disconnects_enabled"),&GraphEdit::is_right_disconnects_enabled);
  580. ObjectTypeDB::bind_method(_MD("_graph_node_moved"),&GraphEdit::_graph_node_moved);
  581. ObjectTypeDB::bind_method(_MD("_graph_node_raised"),&GraphEdit::_graph_node_raised);
  582. ObjectTypeDB::bind_method(_MD("_top_layer_input"),&GraphEdit::_top_layer_input);
  583. ObjectTypeDB::bind_method(_MD("_top_layer_draw"),&GraphEdit::_top_layer_draw);
  584. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&GraphEdit::_scroll_moved);
  585. ObjectTypeDB::bind_method(_MD("_input_event"),&GraphEdit::_input_event);
  586. ADD_SIGNAL(MethodInfo("connection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot")));
  587. ADD_SIGNAL(MethodInfo("disconnection_request",PropertyInfo(Variant::STRING,"from"),PropertyInfo(Variant::INT,"from_slot"),PropertyInfo(Variant::STRING,"to"),PropertyInfo(Variant::INT,"to_slot")));
  588. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2,"p_position")));
  589. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  590. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  591. ADD_SIGNAL(MethodInfo("_begin_node_move"));
  592. ADD_SIGNAL(MethodInfo("_end_node_move"));
  593. }
  594. GraphEdit::GraphEdit() {
  595. set_focus_mode(FOCUS_ALL);
  596. top_layer=NULL;
  597. top_layer=memnew(GraphEditFilter(this));
  598. add_child(top_layer);
  599. top_layer->set_stop_mouse(false);
  600. top_layer->set_area_as_parent_rect();
  601. top_layer->connect("draw",this,"_top_layer_draw");
  602. top_layer->set_stop_mouse(false);
  603. top_layer->connect("input_event",this,"_top_layer_input");
  604. h_scroll = memnew(HScrollBar);
  605. h_scroll->set_name("_h_scroll");
  606. top_layer->add_child(h_scroll);
  607. v_scroll = memnew(VScrollBar);
  608. v_scroll->set_name("_v_scroll");
  609. top_layer->add_child(v_scroll);
  610. updating=false;
  611. connecting=false;
  612. right_disconnects=false;
  613. box_selecting = false;
  614. dragging = false;
  615. h_scroll->connect("value_changed", this,"_scroll_moved");
  616. v_scroll->connect("value_changed", this,"_scroll_moved");
  617. zoom = 1;
  618. }