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