graph_edit.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/input/input.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/keyboard.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/button.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_scale.h"
  38. #endif
  39. #define ZOOM_SCALE 1.2
  40. #define MIN_ZOOM (((1 / ZOOM_SCALE) / ZOOM_SCALE) / ZOOM_SCALE)
  41. #define MAX_ZOOM (1 * ZOOM_SCALE * ZOOM_SCALE * ZOOM_SCALE)
  42. #define MINIMAP_OFFSET 12
  43. #define MINIMAP_PADDING 5
  44. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  45. return ge->_filter_input(p_point);
  46. }
  47. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  48. ge = p_edit;
  49. }
  50. void GraphEditMinimap::_bind_methods() {
  51. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEditMinimap::_gui_input);
  52. }
  53. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  54. ge = p_edit;
  55. graph_proportions = Vector2(1, 1);
  56. graph_padding = Vector2(0, 0);
  57. camera_position = Vector2(100, 50);
  58. camera_size = Vector2(200, 200);
  59. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  60. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  61. is_pressing = false;
  62. is_resizing = false;
  63. }
  64. void GraphEditMinimap::update_minimap() {
  65. Vector2 graph_offset = _get_graph_offset();
  66. Vector2 graph_size = _get_graph_size();
  67. camera_position = ge->get_scroll_ofs() - graph_offset;
  68. camera_size = ge->get_size();
  69. Vector2 render_size = _get_render_size();
  70. float target_ratio = render_size.x / render_size.y;
  71. float graph_ratio = graph_size.x / graph_size.y;
  72. graph_proportions = graph_size;
  73. graph_padding = Vector2(0, 0);
  74. if (graph_ratio > target_ratio) {
  75. graph_proportions.x = graph_size.x;
  76. graph_proportions.y = graph_size.x / target_ratio;
  77. graph_padding.y = Math::abs(graph_size.y - graph_proportions.y) / 2;
  78. } else {
  79. graph_proportions.x = graph_size.y * target_ratio;
  80. graph_proportions.y = graph_size.y;
  81. graph_padding.x = Math::abs(graph_size.x - graph_proportions.x) / 2;
  82. }
  83. // This centers minimap inside the minimap rectangle.
  84. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  85. }
  86. Rect2 GraphEditMinimap::get_camera_rect() {
  87. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  88. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  89. Vector2 camera_position = (camera_center - camera_viewport / 2);
  90. return Rect2(camera_position, camera_viewport);
  91. }
  92. Vector2 GraphEditMinimap::_get_render_size() {
  93. if (!is_inside_tree()) {
  94. return Vector2(0, 0);
  95. }
  96. return get_size() - 2 * minimap_padding;
  97. }
  98. Vector2 GraphEditMinimap::_get_graph_offset() {
  99. return Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  100. }
  101. Vector2 GraphEditMinimap::_get_graph_size() {
  102. Vector2 graph_size = Vector2(ge->h_scroll->get_max(), ge->v_scroll->get_max()) - Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  103. if (graph_size.x == 0) {
  104. graph_size.x = 1;
  105. }
  106. if (graph_size.y == 0) {
  107. graph_size.y = 1;
  108. }
  109. return graph_size;
  110. }
  111. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  112. Vector2 map_position = Vector2(0, 0);
  113. Vector2 render_size = _get_render_size();
  114. map_position.x = p_position.x * render_size.x / graph_proportions.x;
  115. map_position.y = p_position.y * render_size.y / graph_proportions.y;
  116. return map_position;
  117. }
  118. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  119. Vector2 graph_position = Vector2(0, 0);
  120. Vector2 render_size = _get_render_size();
  121. graph_position.x = p_position.x * graph_proportions.x / render_size.x;
  122. graph_position.y = p_position.y * graph_proportions.y / render_size.y;
  123. return graph_position;
  124. }
  125. void GraphEditMinimap::_gui_input(const Ref<InputEvent> &p_ev) {
  126. Ref<InputEventMouseButton> mb = p_ev;
  127. Ref<InputEventMouseMotion> mm = p_ev;
  128. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  129. if (mb->is_pressed()) {
  130. is_pressing = true;
  131. Ref<Texture2D> resizer = get_theme_icon("resizer");
  132. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  133. if (resizer_hitbox.has_point(mb->get_position())) {
  134. is_resizing = true;
  135. } else {
  136. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  137. _adjust_graph_scroll(click_position);
  138. }
  139. } else {
  140. is_pressing = false;
  141. is_resizing = false;
  142. }
  143. accept_event();
  144. } else if (mm.is_valid() && is_pressing) {
  145. if (is_resizing) {
  146. ge->set_minimap_size(ge->get_minimap_size() - mm->get_relative());
  147. update();
  148. } else {
  149. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  150. _adjust_graph_scroll(click_position);
  151. }
  152. accept_event();
  153. }
  154. }
  155. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  156. Vector2 graph_offset = _get_graph_offset();
  157. ge->set_scroll_ofs(p_offset + graph_offset - camera_size / 2);
  158. }
  159. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  160. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  161. return OK;
  162. }
  163. Connection c;
  164. c.from = p_from;
  165. c.from_port = p_from_port;
  166. c.to = p_to;
  167. c.to_port = p_to_port;
  168. c.activity = 0;
  169. connections.push_back(c);
  170. top_layer->update();
  171. minimap->update();
  172. update();
  173. connections_layer->update();
  174. return OK;
  175. }
  176. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  177. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  178. 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) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  185. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  186. 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) {
  187. connections.erase(E);
  188. top_layer->update();
  189. minimap->update();
  190. update();
  191. connections_layer->update();
  192. return;
  193. }
  194. }
  195. }
  196. bool GraphEdit::clips_input() const {
  197. return true;
  198. }
  199. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  200. *r_connections = connections;
  201. }
  202. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  203. setting_scroll_ofs = true;
  204. h_scroll->set_value(p_ofs.x);
  205. v_scroll->set_value(p_ofs.y);
  206. _update_scroll();
  207. setting_scroll_ofs = false;
  208. }
  209. Vector2 GraphEdit::get_scroll_ofs() const {
  210. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  211. }
  212. void GraphEdit::_scroll_moved(double) {
  213. if (!awaiting_scroll_offset_update) {
  214. call_deferred("_update_scroll_offset");
  215. awaiting_scroll_offset_update = true;
  216. }
  217. top_layer->update();
  218. minimap->update();
  219. update();
  220. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  221. emit_signal("scroll_offset_changed", get_scroll_ofs());
  222. }
  223. }
  224. void GraphEdit::_update_scroll_offset() {
  225. set_block_minimum_size_adjust(true);
  226. for (int i = 0; i < get_child_count(); i++) {
  227. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  228. if (!gn) {
  229. continue;
  230. }
  231. Point2 pos = gn->get_offset() * zoom;
  232. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  233. gn->set_position(pos);
  234. if (gn->get_scale() != Vector2(zoom, zoom)) {
  235. gn->set_scale(Vector2(zoom, zoom));
  236. }
  237. }
  238. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  239. set_block_minimum_size_adjust(false);
  240. awaiting_scroll_offset_update = false;
  241. }
  242. void GraphEdit::_update_scroll() {
  243. if (updating) {
  244. return;
  245. }
  246. updating = true;
  247. set_block_minimum_size_adjust(true);
  248. Rect2 screen;
  249. for (int i = 0; i < get_child_count(); i++) {
  250. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  251. if (!gn) {
  252. continue;
  253. }
  254. Rect2 r;
  255. r.position = gn->get_offset() * zoom;
  256. r.size = gn->get_size() * zoom;
  257. screen = screen.merge(r);
  258. }
  259. screen.position -= get_size();
  260. screen.size += get_size() * 2.0;
  261. h_scroll->set_min(screen.position.x);
  262. h_scroll->set_max(screen.position.x + screen.size.x);
  263. h_scroll->set_page(get_size().x);
  264. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  265. h_scroll->hide();
  266. } else {
  267. h_scroll->show();
  268. }
  269. v_scroll->set_min(screen.position.y);
  270. v_scroll->set_max(screen.position.y + screen.size.y);
  271. v_scroll->set_page(get_size().y);
  272. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  273. v_scroll->hide();
  274. } else {
  275. v_scroll->show();
  276. }
  277. Size2 hmin = h_scroll->get_combined_minimum_size();
  278. Size2 vmin = v_scroll->get_combined_minimum_size();
  279. // Avoid scrollbar overlapping.
  280. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  281. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  282. set_block_minimum_size_adjust(false);
  283. if (!awaiting_scroll_offset_update) {
  284. call_deferred("_update_scroll_offset");
  285. awaiting_scroll_offset_update = true;
  286. }
  287. updating = false;
  288. }
  289. void GraphEdit::_graph_node_raised(Node *p_gn) {
  290. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  291. ERR_FAIL_COND(!gn);
  292. if (gn->is_comment()) {
  293. move_child(gn, 0);
  294. } else {
  295. gn->raise();
  296. }
  297. int first_not_comment = 0;
  298. for (int i = 0; i < get_child_count(); i++) {
  299. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  300. if (gn2 && !gn2->is_comment()) {
  301. first_not_comment = i;
  302. break;
  303. }
  304. }
  305. move_child(connections_layer, first_not_comment);
  306. top_layer->raise();
  307. emit_signal("node_selected", p_gn);
  308. }
  309. void GraphEdit::_graph_node_moved(Node *p_gn) {
  310. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  311. ERR_FAIL_COND(!gn);
  312. top_layer->update();
  313. minimap->update();
  314. update();
  315. connections_layer->update();
  316. }
  317. void GraphEdit::add_child_notify(Node *p_child) {
  318. Control::add_child_notify(p_child);
  319. top_layer->call_deferred("raise"); // Top layer always on top!
  320. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  321. if (gn) {
  322. gn->set_scale(Vector2(zoom, zoom));
  323. gn->connect("offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved), varray(gn));
  324. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised), varray(gn));
  325. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  326. _graph_node_moved(gn);
  327. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  328. }
  329. }
  330. void GraphEdit::remove_child_notify(Node *p_child) {
  331. Control::remove_child_notify(p_child);
  332. if (is_inside_tree()) {
  333. top_layer->call_deferred("raise"); // Top layer always on top!
  334. }
  335. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  336. if (gn) {
  337. gn->disconnect("offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  338. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  339. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  340. }
  341. }
  342. void GraphEdit::_notification(int p_what) {
  343. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  344. port_grab_distance_horizontal = get_theme_constant("port_grab_distance_horizontal");
  345. port_grab_distance_vertical = get_theme_constant("port_grab_distance_vertical");
  346. zoom_minus->set_icon(get_theme_icon("minus"));
  347. zoom_reset->set_icon(get_theme_icon("reset"));
  348. zoom_plus->set_icon(get_theme_icon("more"));
  349. snap_button->set_icon(get_theme_icon("snap"));
  350. minimap_button->set_icon(get_theme_icon("minimap"));
  351. }
  352. if (p_what == NOTIFICATION_READY) {
  353. Size2 hmin = h_scroll->get_combined_minimum_size();
  354. Size2 vmin = v_scroll->get_combined_minimum_size();
  355. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  356. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  357. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  358. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  359. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  360. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  361. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  362. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  363. }
  364. if (p_what == NOTIFICATION_DRAW) {
  365. draw_style_box(get_theme_stylebox("bg"), Rect2(Point2(), get_size()));
  366. if (is_using_snap()) {
  367. //draw grid
  368. int snap = get_snap();
  369. Vector2 offset = get_scroll_ofs() / zoom;
  370. Size2 size = get_size() / zoom;
  371. Point2i from = (offset / float(snap)).floor();
  372. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  373. Color grid_minor = get_theme_color("grid_minor");
  374. Color grid_major = get_theme_color("grid_major");
  375. for (int i = from.x; i < from.x + len.x; i++) {
  376. Color color;
  377. if (ABS(i) % 10 == 0) {
  378. color = grid_major;
  379. } else {
  380. color = grid_minor;
  381. }
  382. float base_ofs = i * snap * zoom - offset.x * zoom;
  383. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  384. }
  385. for (int i = from.y; i < from.y + len.y; i++) {
  386. Color color;
  387. if (ABS(i) % 10 == 0) {
  388. color = grid_major;
  389. } else {
  390. color = grid_minor;
  391. }
  392. float base_ofs = i * snap * zoom - offset.y * zoom;
  393. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  394. }
  395. }
  396. }
  397. if (p_what == NOTIFICATION_RESIZED) {
  398. _update_scroll();
  399. top_layer->update();
  400. minimap->update();
  401. }
  402. }
  403. bool GraphEdit::_filter_input(const Point2 &p_point) {
  404. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  405. for (int i = get_child_count() - 1; i >= 0; i--) {
  406. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  407. if (!gn) {
  408. continue;
  409. }
  410. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  411. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  412. if (is_in_hot_zone(pos, p_point)) {
  413. return true;
  414. }
  415. }
  416. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  417. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  418. if (is_in_hot_zone(pos, p_point)) {
  419. return true;
  420. }
  421. }
  422. }
  423. return false;
  424. }
  425. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  426. Ref<InputEventMouseButton> mb = p_ev;
  427. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  428. connecting_valid = false;
  429. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  430. click_pos = mb->get_position();
  431. for (int i = get_child_count() - 1; i >= 0; i--) {
  432. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  433. if (!gn) {
  434. continue;
  435. }
  436. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  437. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  438. if (is_in_hot_zone(pos, click_pos)) {
  439. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  440. //check disconnect
  441. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  442. if (E->get().from == gn->get_name() && E->get().from_port == j) {
  443. Node *to = get_node(String(E->get().to));
  444. if (Object::cast_to<GraphNode>(to)) {
  445. connecting_from = E->get().to;
  446. connecting_index = E->get().to_port;
  447. connecting_out = false;
  448. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
  449. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E->get().to_port);
  450. connecting_target = false;
  451. connecting_to = pos;
  452. just_disconnected = true;
  453. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  454. to = get_node(String(connecting_from)); //maybe it was erased
  455. if (Object::cast_to<GraphNode>(to)) {
  456. connecting = true;
  457. }
  458. return;
  459. }
  460. }
  461. }
  462. }
  463. connecting = true;
  464. connecting_from = gn->get_name();
  465. connecting_index = j;
  466. connecting_out = true;
  467. connecting_type = gn->get_connection_output_type(j);
  468. connecting_color = gn->get_connection_output_color(j);
  469. connecting_target = false;
  470. connecting_to = pos;
  471. just_disconnected = false;
  472. return;
  473. }
  474. }
  475. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  476. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  477. if (is_in_hot_zone(pos, click_pos)) {
  478. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  479. //check disconnect
  480. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  481. if (E->get().to == gn->get_name() && E->get().to_port == j) {
  482. Node *fr = get_node(String(E->get().from));
  483. if (Object::cast_to<GraphNode>(fr)) {
  484. connecting_from = E->get().from;
  485. connecting_index = E->get().from_port;
  486. connecting_out = true;
  487. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
  488. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E->get().from_port);
  489. connecting_target = false;
  490. connecting_to = pos;
  491. just_disconnected = true;
  492. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  493. fr = get_node(String(connecting_from)); //maybe it was erased
  494. if (Object::cast_to<GraphNode>(fr)) {
  495. connecting = true;
  496. }
  497. return;
  498. }
  499. }
  500. }
  501. }
  502. connecting = true;
  503. connecting_from = gn->get_name();
  504. connecting_index = j;
  505. connecting_out = false;
  506. connecting_type = gn->get_connection_input_type(j);
  507. connecting_color = gn->get_connection_input_color(j);
  508. connecting_target = false;
  509. connecting_to = pos;
  510. just_disconnected = false;
  511. return;
  512. }
  513. }
  514. }
  515. }
  516. Ref<InputEventMouseMotion> mm = p_ev;
  517. if (mm.is_valid() && connecting) {
  518. connecting_to = mm->get_position();
  519. connecting_target = false;
  520. top_layer->update();
  521. minimap->update();
  522. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to) > 20.0 * zoom;
  523. if (connecting_valid) {
  524. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  525. Vector2 mpos = mm->get_position();
  526. for (int i = get_child_count() - 1; i >= 0; i--) {
  527. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  528. if (!gn) {
  529. continue;
  530. }
  531. if (!connecting_out) {
  532. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  533. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  534. int type = gn->get_connection_output_type(j);
  535. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  536. connecting_target = true;
  537. connecting_to = pos;
  538. connecting_target_to = gn->get_name();
  539. connecting_target_index = j;
  540. return;
  541. }
  542. }
  543. } else {
  544. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  545. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  546. int type = gn->get_connection_input_type(j);
  547. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  548. connecting_target = true;
  549. connecting_to = pos;
  550. connecting_target_to = gn->get_name();
  551. connecting_target_index = j;
  552. return;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. }
  559. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  560. if (connecting_valid) {
  561. if (connecting && connecting_target) {
  562. String from = connecting_from;
  563. int from_slot = connecting_index;
  564. String to = connecting_target_to;
  565. int to_slot = connecting_target_index;
  566. if (!connecting_out) {
  567. SWAP(from, to);
  568. SWAP(from_slot, to_slot);
  569. }
  570. emit_signal("connection_request", from, from_slot, to, to_slot);
  571. } else if (!just_disconnected) {
  572. String from = connecting_from;
  573. int from_slot = connecting_index;
  574. Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
  575. if (!connecting_out) {
  576. emit_signal("connection_from_empty", from, from_slot, ofs);
  577. } else {
  578. emit_signal("connection_to_empty", from, from_slot, ofs);
  579. }
  580. }
  581. }
  582. connecting = false;
  583. top_layer->update();
  584. minimap->update();
  585. update();
  586. connections_layer->update();
  587. }
  588. }
  589. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
  590. if (p_control->is_set_as_top_level() || !p_control->is_visible()) {
  591. return false;
  592. }
  593. if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  594. //test children
  595. for (int i = 0; i < p_control->get_child_count(); i++) {
  596. Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
  597. if (!subchild) {
  598. continue;
  599. }
  600. if (_check_clickable_control(subchild, pos - subchild->get_position())) {
  601. return true;
  602. }
  603. }
  604. return false;
  605. } else {
  606. return true;
  607. }
  608. }
  609. bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos) {
  610. 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)) {
  611. return false;
  612. }
  613. for (int i = 0; i < get_child_count(); i++) {
  614. Control *child = Object::cast_to<Control>(get_child(i));
  615. if (!child) {
  616. continue;
  617. }
  618. Rect2 rect = child->get_rect();
  619. if (rect.has_point(p_mouse_pos)) {
  620. //check sub-controls
  621. Vector2 subpos = p_mouse_pos - rect.position;
  622. for (int j = 0; j < child->get_child_count(); j++) {
  623. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  624. if (!subchild) {
  625. continue;
  626. }
  627. if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
  628. return false;
  629. }
  630. }
  631. }
  632. }
  633. return true;
  634. }
  635. template <class Vector2>
  636. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  637. /* Formula from Wikipedia article on Bezier curves. */
  638. real_t omt = (1.0 - t);
  639. real_t omt2 = omt * omt;
  640. real_t omt3 = omt2 * omt;
  641. real_t t2 = t * t;
  642. real_t t3 = t2 * t;
  643. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  644. }
  645. 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 {
  646. float mp = p_begin + (p_end - p_begin) * 0.5;
  647. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  648. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  649. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  650. Vector2 na = (mid - beg).normalized();
  651. Vector2 nb = (end - mid).normalized();
  652. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  653. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  654. points.push_back((beg + end) * 0.5);
  655. colors.push_back(p_color.lerp(p_to_color, mp));
  656. lines++;
  657. } else {
  658. _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);
  659. _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);
  660. }
  661. }
  662. 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, float p_bezier_ratio) {
  663. //cubic bezier code
  664. float diff = p_to.x - p_from.x;
  665. float cp_offset;
  666. int cp_len = get_theme_constant("bezier_len_pos") * p_bezier_ratio;
  667. int cp_neg_len = get_theme_constant("bezier_len_neg") * p_bezier_ratio;
  668. if (diff > 0) {
  669. cp_offset = MIN(cp_len, diff * 0.5);
  670. } else {
  671. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  672. }
  673. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  674. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  675. int lines = 0;
  676. Vector<Point2> points;
  677. Vector<Color> colors;
  678. points.push_back(p_from);
  679. colors.push_back(p_color);
  680. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
  681. points.push_back(p_to);
  682. colors.push_back(p_to_color);
  683. #ifdef TOOLS_ENABLED
  684. p_where->draw_polyline_colors(points, colors, Math::floor(2 * EDSCALE), true);
  685. #else
  686. p_where->draw_polyline_colors(points, colors, 2, true);
  687. #endif
  688. }
  689. void GraphEdit::_connections_layer_draw() {
  690. Color activity_color = get_theme_color("activity");
  691. //draw connections
  692. List<List<Connection>::Element *> to_erase;
  693. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  694. NodePath fromnp(E->get().from);
  695. Node *from = get_node(fromnp);
  696. if (!from) {
  697. to_erase.push_back(E);
  698. continue;
  699. }
  700. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  701. if (!gfrom) {
  702. to_erase.push_back(E);
  703. continue;
  704. }
  705. NodePath tonp(E->get().to);
  706. Node *to = get_node(tonp);
  707. if (!to) {
  708. to_erase.push_back(E);
  709. continue;
  710. }
  711. GraphNode *gto = Object::cast_to<GraphNode>(to);
  712. if (!gto) {
  713. to_erase.push_back(E);
  714. continue;
  715. }
  716. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_offset() * zoom;
  717. Color color = gfrom->get_connection_output_color(E->get().from_port);
  718. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_offset() * zoom;
  719. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  720. if (E->get().activity > 0) {
  721. color = color.lerp(activity_color, E->get().activity);
  722. tocolor = tocolor.lerp(activity_color, E->get().activity);
  723. }
  724. _draw_cos_line(connections_layer, frompos, topos, color, tocolor, 1.0);
  725. }
  726. while (to_erase.size()) {
  727. connections.erase(to_erase.front()->get());
  728. to_erase.pop_front();
  729. }
  730. }
  731. void GraphEdit::_top_layer_draw() {
  732. _update_scroll();
  733. if (connecting) {
  734. Node *fromn = get_node(connecting_from);
  735. ERR_FAIL_COND(!fromn);
  736. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  737. ERR_FAIL_COND(!from);
  738. Vector2 pos;
  739. if (connecting_out) {
  740. pos = from->get_connection_output_position(connecting_index);
  741. } else {
  742. pos = from->get_connection_input_position(connecting_index);
  743. }
  744. pos += from->get_position();
  745. Vector2 topos;
  746. topos = connecting_to;
  747. Color col = connecting_color;
  748. if (connecting_target) {
  749. col.r += 0.4;
  750. col.g += 0.4;
  751. col.b += 0.4;
  752. }
  753. if (!connecting_out) {
  754. SWAP(pos, topos);
  755. }
  756. _draw_cos_line(top_layer, pos, topos, col, col, 1.0);
  757. }
  758. if (box_selecting) {
  759. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_fill"));
  760. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_stroke"), false);
  761. }
  762. }
  763. void GraphEdit::_minimap_draw() {
  764. if (!is_minimap_enabled()) {
  765. return;
  766. }
  767. minimap->update_minimap();
  768. // Draw the minimap background.
  769. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  770. minimap->draw_style_box(minimap->get_theme_stylebox("bg"), minimap_rect);
  771. Vector2 graph_offset = minimap->_get_graph_offset();
  772. Vector2 minimap_offset = minimap->minimap_offset;
  773. // Draw comment graph nodes.
  774. for (int i = get_child_count() - 1; i >= 0; i--) {
  775. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  776. if (!gn || !gn->is_comment()) {
  777. continue;
  778. }
  779. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_offset() * zoom - graph_offset) + minimap_offset;
  780. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  781. Rect2 node_rect = Rect2(node_position, node_size);
  782. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox("node")->duplicate();
  783. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  784. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "commentfocus" : "comment");
  785. if (sbf.is_valid()) {
  786. Color node_color = sbf->get_bg_color();
  787. sb_minimap->set_bg_color(node_color);
  788. }
  789. minimap->draw_style_box(sb_minimap, node_rect);
  790. }
  791. // Draw regular graph nodes.
  792. for (int i = get_child_count() - 1; i >= 0; i--) {
  793. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  794. if (!gn || gn->is_comment()) {
  795. continue;
  796. }
  797. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_offset() * zoom - graph_offset) + minimap_offset;
  798. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  799. Rect2 node_rect = Rect2(node_position, node_size);
  800. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox("node")->duplicate();
  801. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  802. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "selectedframe" : "frame");
  803. if (sbf.is_valid()) {
  804. Color node_color = sbf->get_border_color();
  805. sb_minimap->set_bg_color(node_color);
  806. }
  807. minimap->draw_style_box(sb_minimap, node_rect);
  808. }
  809. // Draw node connections.
  810. Color activity_color = get_theme_color("activity");
  811. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  812. NodePath fromnp(E->get().from);
  813. Node *from = get_node(fromnp);
  814. if (!from) {
  815. continue;
  816. }
  817. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  818. if (!gfrom) {
  819. continue;
  820. }
  821. NodePath tonp(E->get().to);
  822. Node *to = get_node(tonp);
  823. if (!to) {
  824. continue;
  825. }
  826. GraphNode *gto = Object::cast_to<GraphNode>(to);
  827. if (!gto) {
  828. continue;
  829. }
  830. Vector2 from_slot_position = gfrom->get_offset() + gfrom->get_connection_output_position(E->get().from_port);
  831. Vector2 from_position = minimap->_convert_from_graph_position(from_slot_position * zoom - graph_offset) + minimap_offset;
  832. Color from_color = gfrom->get_connection_output_color(E->get().from_port);
  833. Vector2 to_slot_position = gto->get_offset() + gto->get_connection_input_position(E->get().to_port);
  834. Vector2 to_position = minimap->_convert_from_graph_position(to_slot_position * zoom - graph_offset) + minimap_offset;
  835. Color to_color = gto->get_connection_input_color(E->get().to_port);
  836. if (E->get().activity > 0) {
  837. from_color = from_color.lerp(activity_color, E->get().activity);
  838. to_color = to_color.lerp(activity_color, E->get().activity);
  839. }
  840. _draw_cos_line(minimap, from_position, to_position, from_color, to_color, 0.5);
  841. }
  842. // Draw the "camera" viewport.
  843. Rect2 camera_rect = minimap->get_camera_rect();
  844. minimap->draw_style_box(minimap->get_theme_stylebox("camera"), camera_rect);
  845. // Draw the resizer control.
  846. Ref<Texture2D> resizer = minimap->get_theme_icon("resizer");
  847. Color resizer_color = minimap->get_theme_color("resizer_color");
  848. minimap->draw_texture(resizer, Point2(), resizer_color);
  849. }
  850. void GraphEdit::set_selected(Node *p_child) {
  851. for (int i = get_child_count() - 1; i >= 0; i--) {
  852. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  853. if (!gn) {
  854. continue;
  855. }
  856. gn->set_selected(gn == p_child);
  857. }
  858. }
  859. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  860. Ref<InputEventMouseMotion> mm = p_ev;
  861. 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)))) {
  862. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  863. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  864. }
  865. if (mm.is_valid() && dragging) {
  866. if (!moving_selection) {
  867. emit_signal("begin_node_move");
  868. moving_selection = true;
  869. }
  870. just_selected = true;
  871. drag_accum += mm->get_relative();
  872. for (int i = get_child_count() - 1; i >= 0; i--) {
  873. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  874. if (gn && gn->is_selected()) {
  875. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  876. // Snapping can be toggled temporarily by holding down Ctrl.
  877. // This is done here as to not toggle the grid when holding down Ctrl.
  878. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  879. const int snap = get_snap();
  880. pos = pos.snapped(Vector2(snap, snap));
  881. }
  882. gn->set_offset(pos);
  883. }
  884. }
  885. }
  886. if (mm.is_valid() && box_selecting) {
  887. box_selecting_to = mm->get_position();
  888. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  889. MIN(box_selecting_from.y, box_selecting_to.y),
  890. ABS(box_selecting_from.x - box_selecting_to.x),
  891. ABS(box_selecting_from.y - box_selecting_to.y));
  892. for (int i = get_child_count() - 1; i >= 0; i--) {
  893. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  894. if (!gn) {
  895. continue;
  896. }
  897. Rect2 r = gn->get_rect();
  898. r.size *= zoom;
  899. bool in_box = r.intersects(box_selecting_rect);
  900. if (in_box) {
  901. if (!gn->is_selected() && box_selection_mode_additive) {
  902. emit_signal("node_selected", gn);
  903. } else if (gn->is_selected() && !box_selection_mode_additive) {
  904. emit_signal("node_unselected", gn);
  905. }
  906. gn->set_selected(box_selection_mode_additive);
  907. } else {
  908. bool select = (previus_selected.find(gn) != nullptr);
  909. if (gn->is_selected() && !select) {
  910. emit_signal("node_unselected", gn);
  911. } else if (!gn->is_selected() && select) {
  912. emit_signal("node_selected", gn);
  913. }
  914. gn->set_selected(select);
  915. }
  916. }
  917. top_layer->update();
  918. minimap->update();
  919. }
  920. Ref<InputEventMouseButton> b = p_ev;
  921. if (b.is_valid()) {
  922. if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
  923. if (box_selecting) {
  924. box_selecting = false;
  925. for (int i = get_child_count() - 1; i >= 0; i--) {
  926. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  927. if (!gn) {
  928. continue;
  929. }
  930. bool select = (previus_selected.find(gn) != nullptr);
  931. if (gn->is_selected() && !select) {
  932. emit_signal("node_unselected", gn);
  933. } else if (!gn->is_selected() && select) {
  934. emit_signal("node_selected", gn);
  935. }
  936. gn->set_selected(select);
  937. }
  938. top_layer->update();
  939. minimap->update();
  940. } else {
  941. if (connecting) {
  942. connecting = false;
  943. top_layer->update();
  944. minimap->update();
  945. } else {
  946. emit_signal("popup_request", b->get_global_position());
  947. }
  948. }
  949. }
  950. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
  951. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  952. //deselect current node
  953. for (int i = get_child_count() - 1; i >= 0; i--) {
  954. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  955. if (gn) {
  956. Rect2 r = gn->get_rect();
  957. r.size *= zoom;
  958. if (r.has_point(b->get_position())) {
  959. emit_signal("node_unselected", gn);
  960. gn->set_selected(false);
  961. }
  962. }
  963. }
  964. }
  965. if (drag_accum != Vector2()) {
  966. for (int i = get_child_count() - 1; i >= 0; i--) {
  967. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  968. if (gn && gn->is_selected()) {
  969. gn->set_drag(false);
  970. }
  971. }
  972. }
  973. if (moving_selection) {
  974. emit_signal("end_node_move");
  975. moving_selection = false;
  976. }
  977. dragging = false;
  978. top_layer->update();
  979. minimap->update();
  980. update();
  981. connections_layer->update();
  982. }
  983. if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {
  984. GraphNode *gn = nullptr;
  985. for (int i = get_child_count() - 1; i >= 0; i--) {
  986. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  987. if (gn_selected) {
  988. if (gn_selected->is_resizing()) {
  989. continue;
  990. }
  991. if (gn_selected->has_point(b->get_position() - gn_selected->get_position())) {
  992. gn = gn_selected;
  993. break;
  994. }
  995. }
  996. }
  997. if (gn) {
  998. if (_filter_input(b->get_position())) {
  999. return;
  1000. }
  1001. dragging = true;
  1002. drag_accum = Vector2();
  1003. just_selected = !gn->is_selected();
  1004. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  1005. for (int i = 0; i < get_child_count(); i++) {
  1006. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1007. if (o_gn) {
  1008. if (o_gn == gn) {
  1009. o_gn->set_selected(true);
  1010. } else {
  1011. if (o_gn->is_selected()) {
  1012. emit_signal("node_unselected", o_gn);
  1013. }
  1014. o_gn->set_selected(false);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. gn->set_selected(true);
  1020. for (int i = 0; i < get_child_count(); i++) {
  1021. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1022. if (!o_gn) {
  1023. continue;
  1024. }
  1025. if (o_gn->is_selected()) {
  1026. o_gn->set_drag(true);
  1027. }
  1028. }
  1029. } else {
  1030. if (_filter_input(b->get_position())) {
  1031. return;
  1032. }
  1033. if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  1034. return;
  1035. }
  1036. box_selecting = true;
  1037. box_selecting_from = b->get_position();
  1038. if (b->get_control()) {
  1039. box_selection_mode_additive = true;
  1040. previus_selected.clear();
  1041. for (int i = get_child_count() - 1; i >= 0; i--) {
  1042. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1043. if (!gn2 || !gn2->is_selected()) {
  1044. continue;
  1045. }
  1046. previus_selected.push_back(gn2);
  1047. }
  1048. } else if (b->get_shift()) {
  1049. box_selection_mode_additive = false;
  1050. previus_selected.clear();
  1051. for (int i = get_child_count() - 1; i >= 0; i--) {
  1052. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1053. if (!gn2 || !gn2->is_selected()) {
  1054. continue;
  1055. }
  1056. previus_selected.push_back(gn2);
  1057. }
  1058. } else {
  1059. box_selection_mode_additive = true;
  1060. previus_selected.clear();
  1061. for (int i = get_child_count() - 1; i >= 0; i--) {
  1062. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1063. if (!gn2) {
  1064. continue;
  1065. }
  1066. if (gn2->is_selected()) {
  1067. emit_signal("node_unselected", gn2);
  1068. }
  1069. gn2->set_selected(false);
  1070. }
  1071. }
  1072. }
  1073. }
  1074. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  1075. box_selecting = false;
  1076. previus_selected.clear();
  1077. top_layer->update();
  1078. minimap->update();
  1079. }
  1080. if (b->get_button_index() == BUTTON_WHEEL_UP && b->is_pressed()) {
  1081. //too difficult to get right
  1082. //set_zoom(zoom*ZOOM_SCALE);
  1083. }
  1084. if (b->get_button_index() == BUTTON_WHEEL_DOWN && b->is_pressed()) {
  1085. //too difficult to get right
  1086. //set_zoom(zoom/ZOOM_SCALE);
  1087. }
  1088. if (b->get_button_index() == BUTTON_WHEEL_UP && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1089. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  1090. }
  1091. if (b->get_button_index() == BUTTON_WHEEL_DOWN && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  1092. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  1093. }
  1094. if (b->get_button_index() == BUTTON_WHEEL_RIGHT || (b->get_button_index() == BUTTON_WHEEL_DOWN && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  1095. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b->get_factor() / 8);
  1096. }
  1097. if (b->get_button_index() == BUTTON_WHEEL_LEFT || (b->get_button_index() == BUTTON_WHEEL_UP && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  1098. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b->get_factor() / 8);
  1099. }
  1100. }
  1101. Ref<InputEventKey> k = p_ev;
  1102. if (k.is_valid()) {
  1103. if (k->get_keycode() == KEY_D && k->is_pressed() && k->get_command()) {
  1104. emit_signal("duplicate_nodes_request");
  1105. accept_event();
  1106. }
  1107. if (k->get_keycode() == KEY_C && k->is_pressed() && k->get_command()) {
  1108. emit_signal("copy_nodes_request");
  1109. accept_event();
  1110. }
  1111. if (k->get_keycode() == KEY_V && k->is_pressed() && k->get_command()) {
  1112. emit_signal("paste_nodes_request");
  1113. accept_event();
  1114. }
  1115. if (k->get_keycode() == KEY_DELETE && k->is_pressed()) {
  1116. emit_signal("delete_nodes_request");
  1117. accept_event();
  1118. }
  1119. }
  1120. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  1121. if (magnify_gesture.is_valid()) {
  1122. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  1123. }
  1124. Ref<InputEventPanGesture> pan_gesture = p_ev;
  1125. if (pan_gesture.is_valid()) {
  1126. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  1127. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  1128. }
  1129. }
  1130. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1131. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1132. 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) {
  1133. if (Math::is_equal_approx(E->get().activity, p_activity)) {
  1134. //update only if changed
  1135. top_layer->update();
  1136. minimap->update();
  1137. connections_layer->update();
  1138. }
  1139. E->get().activity = p_activity;
  1140. return;
  1141. }
  1142. }
  1143. }
  1144. void GraphEdit::clear_connections() {
  1145. connections.clear();
  1146. minimap->update();
  1147. update();
  1148. connections_layer->update();
  1149. }
  1150. void GraphEdit::set_zoom(float p_zoom) {
  1151. set_zoom_custom(p_zoom, get_size() / 2);
  1152. }
  1153. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1154. p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
  1155. if (zoom == p_zoom) {
  1156. return;
  1157. }
  1158. zoom_minus->set_disabled(zoom == MIN_ZOOM);
  1159. zoom_plus->set_disabled(zoom == MAX_ZOOM);
  1160. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  1161. zoom = p_zoom;
  1162. top_layer->update();
  1163. _update_scroll();
  1164. minimap->update();
  1165. connections_layer->update();
  1166. if (is_visible_in_tree()) {
  1167. Vector2 ofs = sbofs * zoom - p_center;
  1168. h_scroll->set_value(ofs.x);
  1169. v_scroll->set_value(ofs.y);
  1170. }
  1171. update();
  1172. }
  1173. float GraphEdit::get_zoom() const {
  1174. return zoom;
  1175. }
  1176. void GraphEdit::set_right_disconnects(bool p_enable) {
  1177. right_disconnects = p_enable;
  1178. }
  1179. bool GraphEdit::is_right_disconnects_enabled() const {
  1180. return right_disconnects;
  1181. }
  1182. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1183. valid_right_disconnect_types.insert(p_type);
  1184. }
  1185. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1186. valid_right_disconnect_types.erase(p_type);
  1187. }
  1188. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1189. valid_left_disconnect_types.insert(p_type);
  1190. }
  1191. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1192. valid_left_disconnect_types.erase(p_type);
  1193. }
  1194. Array GraphEdit::_get_connection_list() const {
  1195. List<Connection> conns;
  1196. get_connection_list(&conns);
  1197. Array arr;
  1198. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  1199. Dictionary d;
  1200. d["from"] = E->get().from;
  1201. d["from_port"] = E->get().from_port;
  1202. d["to"] = E->get().to;
  1203. d["to_port"] = E->get().to_port;
  1204. arr.push_back(d);
  1205. }
  1206. return arr;
  1207. }
  1208. void GraphEdit::_zoom_minus() {
  1209. set_zoom(zoom / ZOOM_SCALE);
  1210. }
  1211. void GraphEdit::_zoom_reset() {
  1212. set_zoom(1);
  1213. }
  1214. void GraphEdit::_zoom_plus() {
  1215. set_zoom(zoom * ZOOM_SCALE);
  1216. }
  1217. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1218. ConnType ct;
  1219. ct.type_a = p_type;
  1220. ct.type_b = p_with_type;
  1221. valid_connection_types.insert(ct);
  1222. }
  1223. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1224. ConnType ct;
  1225. ct.type_a = p_type;
  1226. ct.type_b = p_with_type;
  1227. valid_connection_types.erase(ct);
  1228. }
  1229. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1230. ConnType ct;
  1231. ct.type_a = p_type;
  1232. ct.type_b = p_with_type;
  1233. return valid_connection_types.has(ct);
  1234. }
  1235. void GraphEdit::set_use_snap(bool p_enable) {
  1236. snap_button->set_pressed(p_enable);
  1237. update();
  1238. }
  1239. bool GraphEdit::is_using_snap() const {
  1240. return snap_button->is_pressed();
  1241. }
  1242. int GraphEdit::get_snap() const {
  1243. return snap_amount->get_value();
  1244. }
  1245. void GraphEdit::set_snap(int p_snap) {
  1246. ERR_FAIL_COND(p_snap < 5);
  1247. snap_amount->set_value(p_snap);
  1248. update();
  1249. }
  1250. void GraphEdit::_snap_toggled() {
  1251. update();
  1252. }
  1253. void GraphEdit::_snap_value_changed(double) {
  1254. update();
  1255. }
  1256. // _minimap_toggled
  1257. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1258. minimap->set_size(p_size);
  1259. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1260. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1261. minimap->set_margin(Margin::MARGIN_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1262. minimap->set_margin(Margin::MARGIN_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1263. minimap->set_margin(Margin::MARGIN_RIGHT, -MINIMAP_OFFSET);
  1264. minimap->set_margin(Margin::MARGIN_BOTTOM, -MINIMAP_OFFSET);
  1265. minimap->update();
  1266. }
  1267. Vector2 GraphEdit::get_minimap_size() const {
  1268. return minimap->get_size();
  1269. }
  1270. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1271. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1272. minimap->update();
  1273. }
  1274. float GraphEdit::get_minimap_opacity() const {
  1275. Color minimap_modulate = minimap->get_modulate();
  1276. return minimap_modulate.a;
  1277. }
  1278. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1279. minimap_button->set_pressed(p_enable);
  1280. minimap->update();
  1281. }
  1282. bool GraphEdit::is_minimap_enabled() const {
  1283. return minimap_button->is_pressed();
  1284. }
  1285. void GraphEdit::_minimap_toggled() {
  1286. minimap->update();
  1287. }
  1288. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1289. return zoom_hb;
  1290. }
  1291. void GraphEdit::_bind_methods() {
  1292. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  1293. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  1294. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  1295. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1296. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1297. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1298. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1299. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  1300. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1301. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1302. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1303. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1304. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1305. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1306. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1307. ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
  1308. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1309. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1310. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1311. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1312. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1313. ClassDB::bind_method(D_METHOD("set_minimap_size", "p_size"), &GraphEdit::set_minimap_size);
  1314. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1315. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "p_opacity"), &GraphEdit::set_minimap_opacity);
  1316. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1317. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1318. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1319. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1320. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1321. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  1322. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1323. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1324. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1325. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1326. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  1327. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  1328. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1329. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1330. ADD_GROUP("Minimap", "minimap");
  1331. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  1332. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size"), "set_minimap_size", "get_minimap_size");
  1333. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  1334. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1335. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1336. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1337. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1338. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1339. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1340. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1341. ADD_SIGNAL(MethodInfo("node_unselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1342. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1343. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1344. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  1345. ADD_SIGNAL(MethodInfo("begin_node_move"));
  1346. ADD_SIGNAL(MethodInfo("end_node_move"));
  1347. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  1348. }
  1349. GraphEdit::GraphEdit() {
  1350. set_focus_mode(FOCUS_ALL);
  1351. awaiting_scroll_offset_update = false;
  1352. top_layer = nullptr;
  1353. top_layer = memnew(GraphEditFilter(this));
  1354. add_child(top_layer);
  1355. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1356. top_layer->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1357. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1358. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1359. connections_layer = memnew(Control);
  1360. add_child(connections_layer);
  1361. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1362. connections_layer->set_name("CLAYER");
  1363. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1364. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1365. h_scroll = memnew(HScrollBar);
  1366. h_scroll->set_name("_h_scroll");
  1367. top_layer->add_child(h_scroll);
  1368. v_scroll = memnew(VScrollBar);
  1369. v_scroll->set_name("_v_scroll");
  1370. top_layer->add_child(v_scroll);
  1371. updating = false;
  1372. connecting = false;
  1373. right_disconnects = false;
  1374. box_selecting = false;
  1375. dragging = false;
  1376. //set large minmax so it can scroll even if not resized yet
  1377. h_scroll->set_min(-10000);
  1378. h_scroll->set_max(10000);
  1379. v_scroll->set_min(-10000);
  1380. v_scroll->set_max(10000);
  1381. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1382. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1383. zoom = 1;
  1384. zoom_hb = memnew(HBoxContainer);
  1385. top_layer->add_child(zoom_hb);
  1386. zoom_hb->set_position(Vector2(10, 10));
  1387. zoom_minus = memnew(Button);
  1388. zoom_minus->set_flat(true);
  1389. zoom_hb->add_child(zoom_minus);
  1390. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1391. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1392. zoom_minus->set_focus_mode(FOCUS_NONE);
  1393. zoom_reset = memnew(Button);
  1394. zoom_reset->set_flat(true);
  1395. zoom_hb->add_child(zoom_reset);
  1396. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1397. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1398. zoom_reset->set_focus_mode(FOCUS_NONE);
  1399. zoom_plus = memnew(Button);
  1400. zoom_plus->set_flat(true);
  1401. zoom_hb->add_child(zoom_plus);
  1402. zoom_plus->set_tooltip(RTR("Zoom In"));
  1403. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1404. zoom_plus->set_focus_mode(FOCUS_NONE);
  1405. snap_button = memnew(Button);
  1406. snap_button->set_flat(true);
  1407. snap_button->set_toggle_mode(true);
  1408. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1409. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  1410. snap_button->set_pressed(true);
  1411. snap_button->set_focus_mode(FOCUS_NONE);
  1412. zoom_hb->add_child(snap_button);
  1413. snap_amount = memnew(SpinBox);
  1414. snap_amount->set_min(5);
  1415. snap_amount->set_max(100);
  1416. snap_amount->set_step(1);
  1417. snap_amount->set_value(20);
  1418. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  1419. zoom_hb->add_child(snap_amount);
  1420. minimap_button = memnew(Button);
  1421. minimap_button->set_flat(true);
  1422. minimap_button->set_toggle_mode(true);
  1423. minimap_button->set_tooltip(RTR("Enable grid minimap."));
  1424. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  1425. minimap_button->set_pressed(true);
  1426. minimap_button->set_focus_mode(FOCUS_NONE);
  1427. zoom_hb->add_child(minimap_button);
  1428. Vector2 minimap_size = Vector2(240, 160);
  1429. float minimap_opacity = 0.45;
  1430. minimap = memnew(GraphEditMinimap(this));
  1431. top_layer->add_child(minimap);
  1432. minimap->set_name("_minimap");
  1433. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  1434. minimap->set_mouse_filter(MOUSE_FILTER_STOP);
  1435. minimap->set_custom_minimum_size(Vector2(50, 50));
  1436. minimap->set_size(minimap_size);
  1437. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1438. minimap->set_margin(Margin::MARGIN_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1439. minimap->set_margin(Margin::MARGIN_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1440. minimap->set_margin(Margin::MARGIN_RIGHT, -MINIMAP_OFFSET);
  1441. minimap->set_margin(Margin::MARGIN_BOTTOM, -MINIMAP_OFFSET);
  1442. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  1443. setting_scroll_ofs = false;
  1444. just_disconnected = false;
  1445. set_clip_contents(true);
  1446. }