graph_edit.cpp 21 KB

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