graph_edit.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "graph_edit.h"
  31. #include "core/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "scene/gui/box_container.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/editor_scale.h"
  36. #endif
  37. #define ZOOM_SCALE 1.2
  38. #define MIN_ZOOM (((1 / ZOOM_SCALE) / ZOOM_SCALE) / ZOOM_SCALE)
  39. #define MAX_ZOOM (1 * ZOOM_SCALE * ZOOM_SCALE * ZOOM_SCALE)
  40. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  41. return ge->_filter_input(p_point);
  42. }
  43. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  44. ge = p_edit;
  45. }
  46. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  47. if (is_node_connected(p_from, p_from_port, p_to, p_to_port))
  48. return OK;
  49. Connection c;
  50. c.from = p_from;
  51. c.from_port = p_from_port;
  52. c.to = p_to;
  53. c.to_port = p_to_port;
  54. c.activity = 0;
  55. connections.push_back(c);
  56. top_layer->update();
  57. update();
  58. connections_layer->update();
  59. return OK;
  60. }
  61. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  62. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  63. 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)
  64. return true;
  65. }
  66. return false;
  67. }
  68. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  69. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  70. 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) {
  71. connections.erase(E);
  72. top_layer->update();
  73. update();
  74. connections_layer->update();
  75. return;
  76. }
  77. }
  78. }
  79. bool GraphEdit::clips_input() const {
  80. return true;
  81. }
  82. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  83. *r_connections = connections;
  84. }
  85. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  86. setting_scroll_ofs = true;
  87. h_scroll->set_value(p_ofs.x);
  88. v_scroll->set_value(p_ofs.y);
  89. _update_scroll();
  90. setting_scroll_ofs = false;
  91. }
  92. Vector2 GraphEdit::get_scroll_ofs() const {
  93. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  94. }
  95. void GraphEdit::_scroll_moved(double) {
  96. if (!awaiting_scroll_offset_update) {
  97. call_deferred("_update_scroll_offset");
  98. awaiting_scroll_offset_update = true;
  99. }
  100. top_layer->update();
  101. update();
  102. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  103. emit_signal("scroll_offset_changed", get_scroll_ofs());
  104. }
  105. }
  106. void GraphEdit::_update_scroll_offset() {
  107. set_block_minimum_size_adjust(true);
  108. for (int i = 0; i < get_child_count(); i++) {
  109. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  110. if (!gn)
  111. continue;
  112. Point2 pos = gn->get_offset() * zoom;
  113. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  114. gn->set_position(pos);
  115. if (gn->get_scale() != Vector2(zoom, zoom)) {
  116. gn->set_scale(Vector2(zoom, zoom));
  117. }
  118. }
  119. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  120. set_block_minimum_size_adjust(false);
  121. awaiting_scroll_offset_update = false;
  122. }
  123. void GraphEdit::_update_scroll() {
  124. if (updating)
  125. return;
  126. updating = true;
  127. set_block_minimum_size_adjust(true);
  128. Rect2 screen;
  129. for (int i = 0; i < get_child_count(); i++) {
  130. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  131. if (!gn)
  132. continue;
  133. Rect2 r;
  134. r.position = gn->get_offset() * zoom;
  135. r.size = gn->get_size() * zoom;
  136. screen = screen.merge(r);
  137. }
  138. screen.position -= get_size();
  139. screen.size += get_size() * 2.0;
  140. h_scroll->set_min(screen.position.x);
  141. h_scroll->set_max(screen.position.x + screen.size.x);
  142. h_scroll->set_page(get_size().x);
  143. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page())
  144. h_scroll->hide();
  145. else
  146. h_scroll->show();
  147. v_scroll->set_min(screen.position.y);
  148. v_scroll->set_max(screen.position.y + screen.size.y);
  149. v_scroll->set_page(get_size().y);
  150. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page())
  151. v_scroll->hide();
  152. else
  153. v_scroll->show();
  154. set_block_minimum_size_adjust(false);
  155. if (!awaiting_scroll_offset_update) {
  156. call_deferred("_update_scroll_offset");
  157. awaiting_scroll_offset_update = true;
  158. }
  159. updating = false;
  160. }
  161. void GraphEdit::_graph_node_raised(Node *p_gn) {
  162. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  163. ERR_FAIL_COND(!gn);
  164. if (gn->is_comment()) {
  165. move_child(gn, 0);
  166. } else {
  167. gn->raise();
  168. }
  169. int first_not_comment = 0;
  170. for (int i = 0; i < get_child_count(); i++) {
  171. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  172. if (gn && !gn->is_comment()) {
  173. first_not_comment = i;
  174. break;
  175. }
  176. }
  177. move_child(connections_layer, first_not_comment);
  178. top_layer->raise();
  179. emit_signal("node_selected", p_gn);
  180. }
  181. void GraphEdit::_graph_node_moved(Node *p_gn) {
  182. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  183. ERR_FAIL_COND(!gn);
  184. top_layer->update();
  185. update();
  186. connections_layer->update();
  187. }
  188. void GraphEdit::add_child_notify(Node *p_child) {
  189. Control::add_child_notify(p_child);
  190. top_layer->call_deferred("raise"); //top layer always on top!
  191. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  192. if (gn) {
  193. gn->set_scale(Vector2(zoom, zoom));
  194. gn->connect("offset_changed", this, "_graph_node_moved", varray(gn));
  195. gn->connect("raise_request", this, "_graph_node_raised", varray(gn));
  196. gn->connect("item_rect_changed", connections_layer, "update");
  197. _graph_node_moved(gn);
  198. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  199. }
  200. }
  201. void GraphEdit::remove_child_notify(Node *p_child) {
  202. Control::remove_child_notify(p_child);
  203. top_layer->call_deferred("raise"); //top layer always on top!
  204. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  205. if (gn) {
  206. gn->disconnect("offset_changed", this, "_graph_node_moved");
  207. gn->disconnect("raise_request", this, "_graph_node_raised");
  208. }
  209. }
  210. void GraphEdit::_notification(int p_what) {
  211. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  212. port_grab_distance_horizontal = get_constant("port_grab_distance_horizontal");
  213. port_grab_distance_vertical = get_constant("port_grab_distance_vertical");
  214. }
  215. if (p_what == NOTIFICATION_READY) {
  216. Size2 hmin = h_scroll->get_combined_minimum_size();
  217. Size2 vmin = v_scroll->get_combined_minimum_size();
  218. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  219. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  220. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  221. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  222. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  223. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  224. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  225. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  226. zoom_minus->set_icon(get_icon("minus"));
  227. zoom_reset->set_icon(get_icon("reset"));
  228. zoom_plus->set_icon(get_icon("more"));
  229. snap_button->set_icon(get_icon("snap"));
  230. }
  231. if (p_what == NOTIFICATION_DRAW) {
  232. draw_style_box(get_stylebox("bg"), Rect2(Point2(), get_size()));
  233. if (is_using_snap()) {
  234. //draw grid
  235. int snap = get_snap();
  236. Vector2 offset = get_scroll_ofs() / zoom;
  237. Size2 size = get_size() / zoom;
  238. Point2i from = (offset / float(snap)).floor();
  239. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  240. Color grid_minor = get_color("grid_minor");
  241. Color grid_major = get_color("grid_major");
  242. for (int i = from.x; i < from.x + len.x; i++) {
  243. Color color;
  244. if (ABS(i) % 10 == 0)
  245. color = grid_major;
  246. else
  247. color = grid_minor;
  248. float base_ofs = i * snap * zoom - offset.x * zoom;
  249. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  250. }
  251. for (int i = from.y; i < from.y + len.y; i++) {
  252. Color color;
  253. if (ABS(i) % 10 == 0)
  254. color = grid_major;
  255. else
  256. color = grid_minor;
  257. float base_ofs = i * snap * zoom - offset.y * zoom;
  258. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  259. }
  260. }
  261. }
  262. if (p_what == NOTIFICATION_RESIZED) {
  263. _update_scroll();
  264. top_layer->update();
  265. }
  266. }
  267. bool GraphEdit::_filter_input(const Point2 &p_point) {
  268. Ref<Texture> port = get_icon("port", "GraphNode");
  269. for (int i = get_child_count() - 1; i >= 0; i--) {
  270. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  271. if (!gn)
  272. continue;
  273. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  274. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  275. if (is_in_hot_zone(pos, p_point))
  276. return true;
  277. }
  278. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  279. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  280. if (is_in_hot_zone(pos, p_point)) {
  281. return true;
  282. }
  283. }
  284. }
  285. return false;
  286. }
  287. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  288. Ref<InputEventMouseButton> mb = p_ev;
  289. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  290. Ref<Texture> port = get_icon("port", "GraphNode");
  291. Vector2 mpos(mb->get_position().x, mb->get_position().y);
  292. for (int i = get_child_count() - 1; i >= 0; i--) {
  293. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  294. if (!gn)
  295. continue;
  296. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  297. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  298. if (is_in_hot_zone(pos, mpos)) {
  299. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  300. //check disconnect
  301. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  302. if (E->get().from == gn->get_name() && E->get().from_port == j) {
  303. Node *to = get_node(String(E->get().to));
  304. if (Object::cast_to<GraphNode>(to)) {
  305. connecting_from = E->get().to;
  306. connecting_index = E->get().to_port;
  307. connecting_out = false;
  308. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
  309. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E->get().to_port);
  310. connecting_target = false;
  311. connecting_to = pos;
  312. just_disconnected = true;
  313. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  314. to = get_node(String(connecting_from)); //maybe it was erased
  315. if (Object::cast_to<GraphNode>(to)) {
  316. connecting = true;
  317. }
  318. return;
  319. }
  320. }
  321. }
  322. }
  323. connecting = true;
  324. connecting_from = gn->get_name();
  325. connecting_index = j;
  326. connecting_out = true;
  327. connecting_type = gn->get_connection_output_type(j);
  328. connecting_color = gn->get_connection_output_color(j);
  329. connecting_target = false;
  330. connecting_to = pos;
  331. just_disconnected = false;
  332. return;
  333. }
  334. }
  335. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  336. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  337. if (is_in_hot_zone(pos, mpos)) {
  338. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  339. //check disconnect
  340. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  341. if (E->get().to == gn->get_name() && E->get().to_port == j) {
  342. Node *fr = get_node(String(E->get().from));
  343. if (Object::cast_to<GraphNode>(fr)) {
  344. connecting_from = E->get().from;
  345. connecting_index = E->get().from_port;
  346. connecting_out = true;
  347. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
  348. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E->get().from_port);
  349. connecting_target = false;
  350. connecting_to = pos;
  351. just_disconnected = true;
  352. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  353. fr = get_node(String(connecting_from)); //maybe it was erased
  354. if (Object::cast_to<GraphNode>(fr)) {
  355. connecting = true;
  356. }
  357. return;
  358. }
  359. }
  360. }
  361. }
  362. connecting = true;
  363. connecting_from = gn->get_name();
  364. connecting_index = j;
  365. connecting_out = false;
  366. connecting_type = gn->get_connection_input_type(j);
  367. connecting_color = gn->get_connection_input_color(j);
  368. connecting_target = false;
  369. connecting_to = pos;
  370. just_disconnected = true;
  371. return;
  372. }
  373. }
  374. }
  375. }
  376. Ref<InputEventMouseMotion> mm = p_ev;
  377. if (mm.is_valid() && connecting) {
  378. connecting_to = mm->get_position();
  379. connecting_target = false;
  380. top_layer->update();
  381. Ref<Texture> port = get_icon("port", "GraphNode");
  382. Vector2 mpos = mm->get_position();
  383. for (int i = get_child_count() - 1; i >= 0; i--) {
  384. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  385. if (!gn)
  386. continue;
  387. if (!connecting_out) {
  388. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  389. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  390. int type = gn->get_connection_output_type(j);
  391. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  392. connecting_target = true;
  393. connecting_to = pos;
  394. connecting_target_to = gn->get_name();
  395. connecting_target_index = j;
  396. return;
  397. }
  398. }
  399. } else {
  400. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  401. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  402. int type = gn->get_connection_input_type(j);
  403. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  404. connecting_target = true;
  405. connecting_to = pos;
  406. connecting_target_to = gn->get_name();
  407. connecting_target_index = j;
  408. return;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  415. if (connecting && connecting_target) {
  416. String from = connecting_from;
  417. int from_slot = connecting_index;
  418. String to = connecting_target_to;
  419. int to_slot = connecting_target_index;
  420. if (!connecting_out) {
  421. SWAP(from, to);
  422. SWAP(from_slot, to_slot);
  423. }
  424. emit_signal("connection_request", from, from_slot, to, to_slot);
  425. } else if (!just_disconnected) {
  426. String from = connecting_from;
  427. int from_slot = connecting_index;
  428. Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
  429. emit_signal("connection_to_empty", from, from_slot, ofs);
  430. }
  431. connecting = false;
  432. top_layer->update();
  433. update();
  434. connections_layer->update();
  435. }
  436. }
  437. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
  438. if (p_control->is_set_as_toplevel() || !p_control->is_visible())
  439. return false;
  440. if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  441. //test children
  442. for (int i = 0; i < p_control->get_child_count(); i++) {
  443. Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
  444. if (!subchild)
  445. continue;
  446. if (_check_clickable_control(subchild, pos - subchild->get_position())) {
  447. return true;
  448. }
  449. }
  450. return false;
  451. } else {
  452. return true;
  453. }
  454. }
  455. bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos) {
  456. if (!Rect2(pos.x - port_grab_distance_horizontal, pos.y - port_grab_distance_vertical, port_grab_distance_horizontal * 2, port_grab_distance_vertical * 2).has_point(p_mouse_pos))
  457. return false;
  458. for (int i = 0; i < get_child_count(); i++) {
  459. Control *child = Object::cast_to<Control>(get_child(i));
  460. if (!child)
  461. continue;
  462. Rect2 rect = child->get_rect();
  463. if (rect.has_point(p_mouse_pos)) {
  464. //check sub-controls
  465. Vector2 subpos = p_mouse_pos - rect.position;
  466. for (int j = 0; j < child->get_child_count(); j++) {
  467. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  468. if (!subchild)
  469. continue;
  470. if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
  471. return false;
  472. }
  473. }
  474. }
  475. }
  476. return true;
  477. }
  478. template <class Vector2>
  479. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  480. /* Formula from Wikipedia article on Bezier curves. */
  481. real_t omt = (1.0 - t);
  482. real_t omt2 = omt * omt;
  483. real_t omt3 = omt2 * omt;
  484. real_t t2 = t * t;
  485. real_t t3 = t2 * t;
  486. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  487. }
  488. void GraphEdit::_bake_segment2d(Vector<Vector2> &points, Vector<Color> &colors, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const {
  489. float mp = p_begin + (p_end - p_begin) * 0.5;
  490. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  491. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  492. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  493. Vector2 na = (mid - beg).normalized();
  494. Vector2 nb = (end - mid).normalized();
  495. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  496. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  497. points.push_back((beg + end) * 0.5);
  498. colors.push_back(p_color.linear_interpolate(p_to_color, mp));
  499. lines++;
  500. } else {
  501. _bake_segment2d(points, colors, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  502. _bake_segment2d(points, colors, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  503. }
  504. }
  505. void GraphEdit::_draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color) {
  506. //cubic bezier code
  507. float diff = p_to.x - p_from.x;
  508. float cp_offset;
  509. int cp_len = get_constant("bezier_len_pos");
  510. int cp_neg_len = get_constant("bezier_len_neg");
  511. if (diff > 0) {
  512. cp_offset = MIN(cp_len, diff * 0.5);
  513. } else {
  514. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  515. }
  516. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  517. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  518. int lines = 0;
  519. Vector<Point2> points;
  520. Vector<Color> colors;
  521. points.push_back(p_from);
  522. colors.push_back(p_color);
  523. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
  524. points.push_back(p_to);
  525. colors.push_back(p_to_color);
  526. #ifdef TOOLS_ENABLED
  527. p_where->draw_polyline_colors(points, colors, Math::floor(2 * EDSCALE), true);
  528. #else
  529. p_where->draw_polyline_colors(points, colors, 2, true);
  530. #endif
  531. }
  532. void GraphEdit::_connections_layer_draw() {
  533. Color activity_color = get_color("activity");
  534. //draw connections
  535. List<List<Connection>::Element *> to_erase;
  536. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  537. NodePath fromnp(E->get().from);
  538. Node *from = get_node(fromnp);
  539. if (!from) {
  540. to_erase.push_back(E);
  541. continue;
  542. }
  543. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  544. if (!gfrom) {
  545. to_erase.push_back(E);
  546. continue;
  547. }
  548. NodePath tonp(E->get().to);
  549. Node *to = get_node(tonp);
  550. if (!to) {
  551. to_erase.push_back(E);
  552. continue;
  553. }
  554. GraphNode *gto = Object::cast_to<GraphNode>(to);
  555. if (!gto) {
  556. to_erase.push_back(E);
  557. continue;
  558. }
  559. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_offset() * zoom;
  560. Color color = gfrom->get_connection_output_color(E->get().from_port);
  561. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_offset() * zoom;
  562. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  563. if (E->get().activity > 0) {
  564. color = color.linear_interpolate(activity_color, E->get().activity);
  565. tocolor = tocolor.linear_interpolate(activity_color, E->get().activity);
  566. }
  567. _draw_cos_line(connections_layer, frompos, topos, color, tocolor);
  568. }
  569. while (to_erase.size()) {
  570. connections.erase(to_erase.front()->get());
  571. to_erase.pop_front();
  572. }
  573. }
  574. void GraphEdit::_top_layer_draw() {
  575. _update_scroll();
  576. if (connecting) {
  577. Node *fromn = get_node(connecting_from);
  578. ERR_FAIL_COND(!fromn);
  579. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  580. ERR_FAIL_COND(!from);
  581. Vector2 pos;
  582. if (connecting_out)
  583. pos = from->get_connection_output_position(connecting_index);
  584. else
  585. pos = from->get_connection_input_position(connecting_index);
  586. pos += from->get_position();
  587. Vector2 topos;
  588. topos = connecting_to;
  589. Color col = connecting_color;
  590. if (connecting_target) {
  591. col.r += 0.4;
  592. col.g += 0.4;
  593. col.b += 0.4;
  594. }
  595. if (!connecting_out) {
  596. SWAP(pos, topos);
  597. }
  598. _draw_cos_line(top_layer, pos, topos, col, col);
  599. }
  600. if (box_selecting)
  601. top_layer->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3));
  602. }
  603. void GraphEdit::set_selected(Node *p_child) {
  604. for (int i = get_child_count() - 1; i >= 0; i--) {
  605. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  606. if (!gn)
  607. continue;
  608. gn->set_selected(gn == p_child);
  609. }
  610. }
  611. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  612. Ref<InputEventMouseMotion> mm = p_ev;
  613. if (mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_MIDDLE || (mm->get_button_mask() & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
  614. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  615. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  616. }
  617. if (mm.is_valid() && dragging) {
  618. just_selected = true;
  619. // TODO: Remove local mouse pos hack if/when InputEventMouseMotion is fixed to support floats
  620. //drag_accum+=Vector2(mm->get_relative().x,mm->get_relative().y);
  621. drag_accum = get_local_mouse_position() - drag_origin;
  622. for (int i = get_child_count() - 1; i >= 0; i--) {
  623. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  624. if (gn && gn->is_selected()) {
  625. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  626. if (is_using_snap()) {
  627. int snap = get_snap();
  628. pos = pos.snapped(Vector2(snap, snap));
  629. }
  630. gn->set_offset(pos);
  631. }
  632. }
  633. }
  634. if (mm.is_valid() && box_selecting) {
  635. box_selecting_to = get_local_mouse_position();
  636. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  637. MIN(box_selecting_from.y, box_selecting_to.y),
  638. ABS(box_selecting_from.x - box_selecting_to.x),
  639. ABS(box_selecting_from.y - box_selecting_to.y));
  640. for (int i = get_child_count() - 1; i >= 0; i--) {
  641. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  642. if (!gn)
  643. continue;
  644. Rect2 r = gn->get_rect();
  645. r.size *= zoom;
  646. bool in_box = r.intersects(box_selecting_rect);
  647. if (in_box)
  648. gn->set_selected(box_selection_mode_aditive);
  649. else
  650. gn->set_selected(previus_selected.find(gn) != NULL);
  651. }
  652. top_layer->update();
  653. }
  654. Ref<InputEventMouseButton> b = p_ev;
  655. if (b.is_valid()) {
  656. if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
  657. if (box_selecting) {
  658. box_selecting = false;
  659. for (int i = get_child_count() - 1; i >= 0; i--) {
  660. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  661. if (!gn)
  662. continue;
  663. gn->set_selected(previus_selected.find(gn) != NULL);
  664. }
  665. top_layer->update();
  666. } else {
  667. if (connecting) {
  668. connecting = false;
  669. top_layer->update();
  670. } else {
  671. emit_signal("popup_request", b->get_global_position());
  672. }
  673. }
  674. }
  675. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
  676. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  677. //deselect current node
  678. for (int i = get_child_count() - 1; i >= 0; i--) {
  679. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  680. if (gn) {
  681. Rect2 r = gn->get_rect();
  682. r.size *= zoom;
  683. if (r.has_point(get_local_mouse_position()))
  684. gn->set_selected(false);
  685. }
  686. }
  687. }
  688. if (drag_accum != Vector2()) {
  689. emit_signal("_begin_node_move");
  690. for (int i = get_child_count() - 1; i >= 0; i--) {
  691. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  692. if (gn && gn->is_selected())
  693. gn->set_drag(false);
  694. }
  695. emit_signal("_end_node_move");
  696. }
  697. dragging = false;
  698. top_layer->update();
  699. update();
  700. connections_layer->update();
  701. }
  702. if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {
  703. GraphNode *gn = NULL;
  704. for (int i = get_child_count() - 1; i >= 0; i--) {
  705. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  706. if (gn_selected) {
  707. if (gn_selected->is_resizing())
  708. continue;
  709. if (gn_selected->has_point(gn_selected->get_local_mouse_position())) {
  710. gn = gn_selected;
  711. break;
  712. }
  713. }
  714. }
  715. if (gn) {
  716. if (_filter_input(b->get_position()))
  717. return;
  718. dragging = true;
  719. drag_accum = Vector2();
  720. drag_origin = get_local_mouse_position();
  721. just_selected = !gn->is_selected();
  722. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  723. for (int i = 0; i < get_child_count(); i++) {
  724. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  725. if (o_gn)
  726. o_gn->set_selected(o_gn == gn);
  727. }
  728. }
  729. gn->set_selected(true);
  730. for (int i = 0; i < get_child_count(); i++) {
  731. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  732. if (!o_gn)
  733. continue;
  734. if (o_gn->is_selected())
  735. o_gn->set_drag(true);
  736. }
  737. } else {
  738. if (_filter_input(b->get_position()))
  739. return;
  740. if (Input::get_singleton()->is_key_pressed(KEY_SPACE))
  741. return;
  742. box_selecting = true;
  743. box_selecting_from = get_local_mouse_position();
  744. if (b->get_control()) {
  745. box_selection_mode_aditive = true;
  746. previus_selected.clear();
  747. for (int i = get_child_count() - 1; i >= 0; i--) {
  748. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  749. if (!gn || !gn->is_selected())
  750. continue;
  751. previus_selected.push_back(gn);
  752. }
  753. } else if (b->get_shift()) {
  754. box_selection_mode_aditive = false;
  755. previus_selected.clear();
  756. for (int i = get_child_count() - 1; i >= 0; i--) {
  757. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  758. if (!gn || !gn->is_selected())
  759. continue;
  760. previus_selected.push_back(gn);
  761. }
  762. } else {
  763. box_selection_mode_aditive = true;
  764. previus_selected.clear();
  765. for (int i = get_child_count() - 1; i >= 0; i--) {
  766. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  767. if (!gn)
  768. continue;
  769. gn->set_selected(false);
  770. }
  771. }
  772. }
  773. }
  774. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  775. box_selecting = false;
  776. previus_selected.clear();
  777. top_layer->update();
  778. }
  779. if (b->get_button_index() == BUTTON_WHEEL_UP && b->is_pressed()) {
  780. //too difficult to get right
  781. //set_zoom(zoom*ZOOM_SCALE);
  782. }
  783. if (b->get_button_index() == BUTTON_WHEEL_DOWN && b->is_pressed()) {
  784. //too difficult to get right
  785. //set_zoom(zoom/ZOOM_SCALE);
  786. }
  787. if (b->get_button_index() == BUTTON_WHEEL_UP && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  788. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  789. }
  790. if (b->get_button_index() == BUTTON_WHEEL_DOWN && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  791. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  792. }
  793. if (b->get_button_index() == BUTTON_WHEEL_RIGHT || (b->get_button_index() == BUTTON_WHEEL_DOWN && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  794. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b->get_factor() / 8);
  795. }
  796. if (b->get_button_index() == BUTTON_WHEEL_LEFT || (b->get_button_index() == BUTTON_WHEEL_UP && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  797. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b->get_factor() / 8);
  798. }
  799. }
  800. Ref<InputEventKey> k = p_ev;
  801. if (k.is_valid() && k->get_scancode() == KEY_D && k->is_pressed() && k->get_command()) {
  802. emit_signal("duplicate_nodes_request");
  803. accept_event();
  804. }
  805. if (k.is_valid() && k->get_scancode() == KEY_DELETE && k->is_pressed()) {
  806. emit_signal("delete_nodes_request");
  807. accept_event();
  808. }
  809. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  810. if (magnify_gesture.is_valid()) {
  811. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  812. }
  813. Ref<InputEventPanGesture> pan_gesture = p_ev;
  814. if (pan_gesture.is_valid()) {
  815. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  816. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  817. }
  818. }
  819. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  820. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  821. 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) {
  822. if (ABS(E->get().activity - p_activity) < CMP_EPSILON) {
  823. //update only if changed
  824. top_layer->update();
  825. connections_layer->update();
  826. }
  827. E->get().activity = p_activity;
  828. return;
  829. }
  830. }
  831. }
  832. void GraphEdit::clear_connections() {
  833. connections.clear();
  834. update();
  835. connections_layer->update();
  836. }
  837. void GraphEdit::set_zoom(float p_zoom) {
  838. set_zoom_custom(p_zoom, get_size() / 2);
  839. }
  840. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  841. p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
  842. if (zoom == p_zoom)
  843. return;
  844. zoom_minus->set_disabled(zoom == MIN_ZOOM);
  845. zoom_plus->set_disabled(zoom == MAX_ZOOM);
  846. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  847. zoom = p_zoom;
  848. top_layer->update();
  849. _update_scroll();
  850. connections_layer->update();
  851. if (is_visible_in_tree()) {
  852. Vector2 ofs = sbofs * zoom - p_center;
  853. h_scroll->set_value(ofs.x);
  854. v_scroll->set_value(ofs.y);
  855. }
  856. update();
  857. }
  858. float GraphEdit::get_zoom() const {
  859. return zoom;
  860. }
  861. void GraphEdit::set_right_disconnects(bool p_enable) {
  862. right_disconnects = p_enable;
  863. }
  864. bool GraphEdit::is_right_disconnects_enabled() const {
  865. return right_disconnects;
  866. }
  867. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  868. valid_right_disconnect_types.insert(p_type);
  869. }
  870. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  871. valid_right_disconnect_types.erase(p_type);
  872. }
  873. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  874. valid_left_disconnect_types.insert(p_type);
  875. }
  876. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  877. valid_left_disconnect_types.erase(p_type);
  878. }
  879. Array GraphEdit::_get_connection_list() const {
  880. List<Connection> conns;
  881. get_connection_list(&conns);
  882. Array arr;
  883. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  884. Dictionary d;
  885. d["from"] = E->get().from;
  886. d["from_port"] = E->get().from_port;
  887. d["to"] = E->get().to;
  888. d["to_port"] = E->get().to_port;
  889. arr.push_back(d);
  890. }
  891. return arr;
  892. }
  893. void GraphEdit::_zoom_minus() {
  894. set_zoom(zoom / ZOOM_SCALE);
  895. }
  896. void GraphEdit::_zoom_reset() {
  897. set_zoom(1);
  898. }
  899. void GraphEdit::_zoom_plus() {
  900. set_zoom(zoom * ZOOM_SCALE);
  901. }
  902. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  903. ConnType ct;
  904. ct.type_a = p_type;
  905. ct.type_b = p_with_type;
  906. valid_connection_types.insert(ct);
  907. }
  908. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  909. ConnType ct;
  910. ct.type_a = p_type;
  911. ct.type_b = p_with_type;
  912. valid_connection_types.erase(ct);
  913. }
  914. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  915. ConnType ct;
  916. ct.type_a = p_type;
  917. ct.type_b = p_with_type;
  918. return valid_connection_types.has(ct);
  919. }
  920. void GraphEdit::set_use_snap(bool p_enable) {
  921. snap_button->set_pressed(p_enable);
  922. update();
  923. }
  924. bool GraphEdit::is_using_snap() const {
  925. return snap_button->is_pressed();
  926. }
  927. int GraphEdit::get_snap() const {
  928. return snap_amount->get_value();
  929. }
  930. void GraphEdit::set_snap(int p_snap) {
  931. ERR_FAIL_COND(p_snap < 5);
  932. snap_amount->set_value(p_snap);
  933. update();
  934. }
  935. void GraphEdit::_snap_toggled() {
  936. update();
  937. }
  938. void GraphEdit::_snap_value_changed(double) {
  939. update();
  940. }
  941. HBoxContainer *GraphEdit::get_zoom_hbox() {
  942. return zoom_hb;
  943. }
  944. void GraphEdit::_bind_methods() {
  945. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  946. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  947. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  948. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  949. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  950. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  951. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  952. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  953. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  954. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  955. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  956. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  957. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  958. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  959. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  960. ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
  961. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  962. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  963. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  964. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  965. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  966. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  967. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  968. ClassDB::bind_method(D_METHOD("_graph_node_moved"), &GraphEdit::_graph_node_moved);
  969. ClassDB::bind_method(D_METHOD("_graph_node_raised"), &GraphEdit::_graph_node_raised);
  970. ClassDB::bind_method(D_METHOD("_top_layer_input"), &GraphEdit::_top_layer_input);
  971. ClassDB::bind_method(D_METHOD("_top_layer_draw"), &GraphEdit::_top_layer_draw);
  972. ClassDB::bind_method(D_METHOD("_scroll_moved"), &GraphEdit::_scroll_moved);
  973. ClassDB::bind_method(D_METHOD("_zoom_minus"), &GraphEdit::_zoom_minus);
  974. ClassDB::bind_method(D_METHOD("_zoom_reset"), &GraphEdit::_zoom_reset);
  975. ClassDB::bind_method(D_METHOD("_zoom_plus"), &GraphEdit::_zoom_plus);
  976. ClassDB::bind_method(D_METHOD("_snap_toggled"), &GraphEdit::_snap_toggled);
  977. ClassDB::bind_method(D_METHOD("_snap_value_changed"), &GraphEdit::_snap_value_changed);
  978. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  979. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  980. ClassDB::bind_method(D_METHOD("_connections_layer_draw"), &GraphEdit::_connections_layer_draw);
  981. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  982. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  983. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  984. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  985. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  986. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  987. ADD_PROPERTY(PropertyInfo(Variant::REAL, "zoom"), "set_zoom", "get_zoom");
  988. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  989. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  990. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  991. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  992. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  993. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  994. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  995. ADD_SIGNAL(MethodInfo("_begin_node_move"));
  996. ADD_SIGNAL(MethodInfo("_end_node_move"));
  997. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  998. }
  999. GraphEdit::GraphEdit() {
  1000. set_focus_mode(FOCUS_ALL);
  1001. awaiting_scroll_offset_update = false;
  1002. top_layer = NULL;
  1003. top_layer = memnew(GraphEditFilter(this));
  1004. add_child(top_layer);
  1005. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1006. top_layer->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1007. top_layer->connect("draw", this, "_top_layer_draw");
  1008. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1009. top_layer->connect("gui_input", this, "_top_layer_input");
  1010. connections_layer = memnew(Control);
  1011. add_child(connections_layer);
  1012. connections_layer->connect("draw", this, "_connections_layer_draw");
  1013. connections_layer->set_name("CLAYER");
  1014. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1015. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1016. h_scroll = memnew(HScrollBar);
  1017. h_scroll->set_name("_h_scroll");
  1018. top_layer->add_child(h_scroll);
  1019. v_scroll = memnew(VScrollBar);
  1020. v_scroll->set_name("_v_scroll");
  1021. top_layer->add_child(v_scroll);
  1022. updating = false;
  1023. connecting = false;
  1024. right_disconnects = false;
  1025. box_selecting = false;
  1026. dragging = false;
  1027. //set large minmax so it can scroll even if not resized yet
  1028. h_scroll->set_min(-10000);
  1029. h_scroll->set_max(10000);
  1030. v_scroll->set_min(-10000);
  1031. v_scroll->set_max(10000);
  1032. h_scroll->connect("value_changed", this, "_scroll_moved");
  1033. v_scroll->connect("value_changed", this, "_scroll_moved");
  1034. zoom = 1;
  1035. zoom_hb = memnew(HBoxContainer);
  1036. top_layer->add_child(zoom_hb);
  1037. zoom_hb->set_position(Vector2(10, 10));
  1038. zoom_minus = memnew(ToolButton);
  1039. zoom_hb->add_child(zoom_minus);
  1040. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1041. zoom_minus->connect("pressed", this, "_zoom_minus");
  1042. zoom_minus->set_focus_mode(FOCUS_NONE);
  1043. zoom_reset = memnew(ToolButton);
  1044. zoom_hb->add_child(zoom_reset);
  1045. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1046. zoom_reset->connect("pressed", this, "_zoom_reset");
  1047. zoom_reset->set_focus_mode(FOCUS_NONE);
  1048. zoom_plus = memnew(ToolButton);
  1049. zoom_hb->add_child(zoom_plus);
  1050. zoom_plus->set_tooltip(RTR("Zoom In"));
  1051. zoom_plus->connect("pressed", this, "_zoom_plus");
  1052. zoom_plus->set_focus_mode(FOCUS_NONE);
  1053. snap_button = memnew(ToolButton);
  1054. snap_button->set_toggle_mode(true);
  1055. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1056. snap_button->connect("pressed", this, "_snap_toggled");
  1057. snap_button->set_pressed(true);
  1058. snap_button->set_focus_mode(FOCUS_NONE);
  1059. zoom_hb->add_child(snap_button);
  1060. snap_amount = memnew(SpinBox);
  1061. snap_amount->set_min(5);
  1062. snap_amount->set_max(100);
  1063. snap_amount->set_step(1);
  1064. snap_amount->set_value(20);
  1065. snap_amount->connect("value_changed", this, "_snap_value_changed");
  1066. zoom_hb->add_child(snap_amount);
  1067. setting_scroll_ofs = false;
  1068. just_disconnected = false;
  1069. set_clip_contents(true);
  1070. }