graph_edit.cpp 19 KB

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