graph_edit.cpp 22 KB

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