graph_edit.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. constexpr int MINIMAP_OFFSET = 12;
  40. constexpr int MINIMAP_PADDING = 5;
  41. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  42. return ge->_filter_input(p_point);
  43. }
  44. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  45. ge = p_edit;
  46. }
  47. void GraphEditMinimap::_bind_methods() {
  48. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEditMinimap::_gui_input);
  49. }
  50. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  51. ge = p_edit;
  52. graph_proportions = Vector2(1, 1);
  53. graph_padding = Vector2(0, 0);
  54. camera_position = Vector2(100, 50);
  55. camera_size = Vector2(200, 200);
  56. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  57. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  58. is_pressing = false;
  59. is_resizing = false;
  60. }
  61. void GraphEditMinimap::update_minimap() {
  62. Vector2 graph_offset = _get_graph_offset();
  63. Vector2 graph_size = _get_graph_size();
  64. camera_position = ge->get_scroll_ofs() - graph_offset;
  65. camera_size = ge->get_size();
  66. Vector2 render_size = _get_render_size();
  67. float target_ratio = render_size.x / render_size.y;
  68. float graph_ratio = graph_size.x / graph_size.y;
  69. graph_proportions = graph_size;
  70. graph_padding = Vector2(0, 0);
  71. if (graph_ratio > target_ratio) {
  72. graph_proportions.x = graph_size.x;
  73. graph_proportions.y = graph_size.x / target_ratio;
  74. graph_padding.y = Math::abs(graph_size.y - graph_proportions.y) / 2;
  75. } else {
  76. graph_proportions.x = graph_size.y * target_ratio;
  77. graph_proportions.y = graph_size.y;
  78. graph_padding.x = Math::abs(graph_size.x - graph_proportions.x) / 2;
  79. }
  80. // This centers minimap inside the minimap rectangle.
  81. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  82. }
  83. Rect2 GraphEditMinimap::get_camera_rect() {
  84. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  85. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  86. Vector2 camera_position = (camera_center - camera_viewport / 2);
  87. return Rect2(camera_position, camera_viewport);
  88. }
  89. Vector2 GraphEditMinimap::_get_render_size() {
  90. if (!is_inside_tree()) {
  91. return Vector2(0, 0);
  92. }
  93. return get_size() - 2 * minimap_padding;
  94. }
  95. Vector2 GraphEditMinimap::_get_graph_offset() {
  96. return Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  97. }
  98. Vector2 GraphEditMinimap::_get_graph_size() {
  99. 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());
  100. if (graph_size.x == 0) {
  101. graph_size.x = 1;
  102. }
  103. if (graph_size.y == 0) {
  104. graph_size.y = 1;
  105. }
  106. return graph_size;
  107. }
  108. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  109. Vector2 map_position = Vector2(0, 0);
  110. Vector2 render_size = _get_render_size();
  111. map_position.x = p_position.x * render_size.x / graph_proportions.x;
  112. map_position.y = p_position.y * render_size.y / graph_proportions.y;
  113. return map_position;
  114. }
  115. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  116. Vector2 graph_position = Vector2(0, 0);
  117. Vector2 render_size = _get_render_size();
  118. graph_position.x = p_position.x * graph_proportions.x / render_size.x;
  119. graph_position.y = p_position.y * graph_proportions.y / render_size.y;
  120. return graph_position;
  121. }
  122. void GraphEditMinimap::_gui_input(const Ref<InputEvent> &p_ev) {
  123. ERR_FAIL_COND(p_ev.is_null());
  124. if (!ge->is_minimap_enabled()) {
  125. return;
  126. }
  127. Ref<InputEventMouseButton> mb = p_ev;
  128. Ref<InputEventMouseMotion> mm = p_ev;
  129. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  130. if (mb->is_pressed()) {
  131. is_pressing = true;
  132. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  133. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  134. if (resizer_hitbox.has_point(mb->get_position())) {
  135. is_resizing = true;
  136. } else {
  137. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  138. _adjust_graph_scroll(click_position);
  139. }
  140. } else {
  141. is_pressing = false;
  142. is_resizing = false;
  143. }
  144. accept_event();
  145. } else if (mm.is_valid() && is_pressing) {
  146. if (is_resizing) {
  147. // Prevent setting minimap wider than GraphEdit
  148. Vector2 new_minimap_size;
  149. new_minimap_size.x = MIN(get_size().x - mm->get_relative().x, ge->get_size().x - 2.0 * minimap_padding.x);
  150. new_minimap_size.y = MIN(get_size().y - mm->get_relative().y, ge->get_size().y - 2.0 * minimap_padding.y);
  151. ge->set_minimap_size(new_minimap_size);
  152. update();
  153. } else {
  154. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  155. _adjust_graph_scroll(click_position);
  156. }
  157. accept_event();
  158. }
  159. }
  160. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  161. Vector2 graph_offset = _get_graph_offset();
  162. ge->set_scroll_ofs(p_offset + graph_offset - camera_size / 2);
  163. }
  164. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  165. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  166. return OK;
  167. }
  168. Connection c;
  169. c.from = p_from;
  170. c.from_port = p_from_port;
  171. c.to = p_to;
  172. c.to_port = p_to_port;
  173. c.activity = 0;
  174. connections.push_back(c);
  175. top_layer->update();
  176. minimap->update();
  177. update();
  178. connections_layer->update();
  179. return OK;
  180. }
  181. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  182. for (const Connection &E : connections) {
  183. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  190. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  191. 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) {
  192. connections.erase(E);
  193. top_layer->update();
  194. minimap->update();
  195. update();
  196. connections_layer->update();
  197. return;
  198. }
  199. }
  200. }
  201. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  202. *r_connections = connections;
  203. }
  204. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  205. setting_scroll_ofs = true;
  206. h_scroll->set_value(p_ofs.x);
  207. v_scroll->set_value(p_ofs.y);
  208. _update_scroll();
  209. setting_scroll_ofs = false;
  210. }
  211. Vector2 GraphEdit::get_scroll_ofs() const {
  212. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  213. }
  214. void GraphEdit::_scroll_moved(double) {
  215. if (!awaiting_scroll_offset_update) {
  216. call_deferred(SNAME("_update_scroll_offset"));
  217. awaiting_scroll_offset_update = true;
  218. }
  219. top_layer->update();
  220. minimap->update();
  221. update();
  222. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  223. emit_signal(SNAME("scroll_offset_changed"), get_scroll_ofs());
  224. }
  225. }
  226. void GraphEdit::_update_scroll_offset() {
  227. set_block_minimum_size_adjust(true);
  228. for (int i = 0; i < get_child_count(); i++) {
  229. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  230. if (!gn) {
  231. continue;
  232. }
  233. Point2 pos = gn->get_position_offset() * zoom;
  234. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  235. gn->set_position(pos);
  236. if (gn->get_scale() != Vector2(zoom, zoom)) {
  237. gn->set_scale(Vector2(zoom, zoom));
  238. }
  239. }
  240. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  241. set_block_minimum_size_adjust(false);
  242. awaiting_scroll_offset_update = false;
  243. }
  244. void GraphEdit::_update_scroll() {
  245. if (updating) {
  246. return;
  247. }
  248. updating = true;
  249. set_block_minimum_size_adjust(true);
  250. Rect2 screen;
  251. for (int i = 0; i < get_child_count(); i++) {
  252. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  253. if (!gn) {
  254. continue;
  255. }
  256. Rect2 r;
  257. r.position = gn->get_position_offset() * zoom;
  258. r.size = gn->get_size() * zoom;
  259. screen = screen.merge(r);
  260. }
  261. screen.position -= get_size();
  262. screen.size += get_size() * 2.0;
  263. h_scroll->set_min(screen.position.x);
  264. h_scroll->set_max(screen.position.x + screen.size.x);
  265. h_scroll->set_page(get_size().x);
  266. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  267. h_scroll->hide();
  268. } else {
  269. h_scroll->show();
  270. }
  271. v_scroll->set_min(screen.position.y);
  272. v_scroll->set_max(screen.position.y + screen.size.y);
  273. v_scroll->set_page(get_size().y);
  274. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  275. v_scroll->hide();
  276. } else {
  277. v_scroll->show();
  278. }
  279. Size2 hmin = h_scroll->get_combined_minimum_size();
  280. Size2 vmin = v_scroll->get_combined_minimum_size();
  281. // Avoid scrollbar overlapping.
  282. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  283. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  284. set_block_minimum_size_adjust(false);
  285. if (!awaiting_scroll_offset_update) {
  286. call_deferred(SNAME("_update_scroll_offset"));
  287. awaiting_scroll_offset_update = true;
  288. }
  289. updating = false;
  290. }
  291. void GraphEdit::_graph_node_raised(Node *p_gn) {
  292. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  293. ERR_FAIL_COND(!gn);
  294. if (gn->is_comment()) {
  295. move_child(gn, 0);
  296. } else {
  297. gn->raise();
  298. }
  299. int first_not_comment = 0;
  300. for (int i = 0; i < get_child_count(); i++) {
  301. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  302. if (gn2 && !gn2->is_comment()) {
  303. first_not_comment = i;
  304. break;
  305. }
  306. }
  307. move_child(connections_layer, first_not_comment);
  308. top_layer->raise();
  309. emit_signal(SNAME("node_selected"), p_gn);
  310. }
  311. void GraphEdit::_graph_node_moved(Node *p_gn) {
  312. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  313. ERR_FAIL_COND(!gn);
  314. top_layer->update();
  315. minimap->update();
  316. update();
  317. connections_layer->update();
  318. }
  319. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_gn) {
  320. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  321. ERR_FAIL_COND(!gn);
  322. top_layer->update();
  323. minimap->update();
  324. update();
  325. connections_layer->update();
  326. }
  327. void GraphEdit::add_child_notify(Node *p_child) {
  328. Control::add_child_notify(p_child);
  329. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  330. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  331. if (gn) {
  332. gn->set_scale(Vector2(zoom, zoom));
  333. gn->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved), varray(gn));
  334. gn->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated), varray(gn));
  335. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised), varray(gn));
  336. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  337. gn->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::update));
  338. _graph_node_moved(gn);
  339. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  340. }
  341. }
  342. void GraphEdit::remove_child_notify(Node *p_child) {
  343. Control::remove_child_notify(p_child);
  344. if (p_child == top_layer) {
  345. top_layer = nullptr;
  346. minimap = nullptr;
  347. } else if (p_child == connections_layer) {
  348. connections_layer = nullptr;
  349. }
  350. if (top_layer != nullptr && is_inside_tree()) {
  351. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  352. }
  353. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  354. if (gn) {
  355. gn->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  356. gn->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  357. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  358. // In case of the whole GraphEdit being destroyed these references can already be freed.
  359. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  360. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  361. }
  362. if (minimap != nullptr && minimap->is_inside_tree()) {
  363. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::update));
  364. }
  365. }
  366. }
  367. void GraphEdit::_notification(int p_what) {
  368. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  369. port_grab_distance_horizontal = get_theme_constant(SNAME("port_grab_distance_horizontal"));
  370. port_grab_distance_vertical = get_theme_constant(SNAME("port_grab_distance_vertical"));
  371. zoom_minus->set_icon(get_theme_icon(SNAME("minus")));
  372. zoom_reset->set_icon(get_theme_icon(SNAME("reset")));
  373. zoom_plus->set_icon(get_theme_icon(SNAME("more")));
  374. snap_button->set_icon(get_theme_icon(SNAME("snap")));
  375. minimap_button->set_icon(get_theme_icon(SNAME("minimap")));
  376. layout_button->set_icon(get_theme_icon(SNAME("layout")));
  377. }
  378. if (p_what == NOTIFICATION_READY) {
  379. Size2 hmin = h_scroll->get_combined_minimum_size();
  380. Size2 vmin = v_scroll->get_combined_minimum_size();
  381. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  382. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  383. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  384. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  385. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  386. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  387. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  388. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  389. }
  390. if (p_what == NOTIFICATION_DRAW) {
  391. draw_style_box(get_theme_stylebox(SNAME("bg")), Rect2(Point2(), get_size()));
  392. if (is_using_snap()) {
  393. //draw grid
  394. int snap = get_snap();
  395. Vector2 offset = get_scroll_ofs() / zoom;
  396. Size2 size = get_size() / zoom;
  397. Point2i from = (offset / float(snap)).floor();
  398. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  399. Color grid_minor = get_theme_color(SNAME("grid_minor"));
  400. Color grid_major = get_theme_color(SNAME("grid_major"));
  401. for (int i = from.x; i < from.x + len.x; i++) {
  402. Color color;
  403. if (ABS(i) % 10 == 0) {
  404. color = grid_major;
  405. } else {
  406. color = grid_minor;
  407. }
  408. float base_ofs = i * snap * zoom - offset.x * zoom;
  409. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  410. }
  411. for (int i = from.y; i < from.y + len.y; i++) {
  412. Color color;
  413. if (ABS(i) % 10 == 0) {
  414. color = grid_major;
  415. } else {
  416. color = grid_minor;
  417. }
  418. float base_ofs = i * snap * zoom - offset.y * zoom;
  419. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  420. }
  421. }
  422. }
  423. if (p_what == NOTIFICATION_RESIZED) {
  424. _update_scroll();
  425. top_layer->update();
  426. minimap->update();
  427. }
  428. }
  429. bool GraphEdit::_filter_input(const Point2 &p_point) {
  430. Ref<Texture2D> port = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  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 / zoom, p_point / zoom)) {
  439. return true;
  440. }
  441. }
  442. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  443. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  444. if (is_in_hot_zone(pos / zoom, p_point / zoom)) {
  445. return true;
  446. }
  447. }
  448. }
  449. return false;
  450. }
  451. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  452. Ref<InputEventMouseButton> mb = p_ev;
  453. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT && mb->is_pressed()) {
  454. connecting_valid = false;
  455. Ref<Texture2D> port = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  456. click_pos = mb->get_position() / zoom;
  457. for (int i = get_child_count() - 1; i >= 0; i--) {
  458. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  459. if (!gn) {
  460. continue;
  461. }
  462. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  463. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  464. if (is_in_hot_zone(pos / zoom, click_pos)) {
  465. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  466. //check disconnect
  467. for (const Connection &E : connections) {
  468. if (E.from == gn->get_name() && E.from_port == j) {
  469. Node *to = get_node(String(E.to));
  470. if (Object::cast_to<GraphNode>(to)) {
  471. connecting_from = E.to;
  472. connecting_index = E.to_port;
  473. connecting_out = false;
  474. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E.to_port);
  475. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E.to_port);
  476. connecting_target = false;
  477. connecting_to = pos;
  478. just_disconnected = true;
  479. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  480. to = get_node(String(connecting_from)); //maybe it was erased
  481. if (Object::cast_to<GraphNode>(to)) {
  482. connecting = true;
  483. }
  484. return;
  485. }
  486. }
  487. }
  488. }
  489. connecting = true;
  490. connecting_from = gn->get_name();
  491. connecting_index = j;
  492. connecting_out = true;
  493. connecting_type = gn->get_connection_output_type(j);
  494. connecting_color = gn->get_connection_output_color(j);
  495. connecting_target = false;
  496. connecting_to = pos;
  497. just_disconnected = false;
  498. return;
  499. }
  500. }
  501. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  502. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  503. if (is_in_hot_zone(pos / zoom, click_pos)) {
  504. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  505. //check disconnect
  506. for (const Connection &E : connections) {
  507. if (E.to == gn->get_name() && E.to_port == j) {
  508. Node *fr = get_node(String(E.from));
  509. if (Object::cast_to<GraphNode>(fr)) {
  510. connecting_from = E.from;
  511. connecting_index = E.from_port;
  512. connecting_out = true;
  513. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E.from_port);
  514. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E.from_port);
  515. connecting_target = false;
  516. connecting_to = pos;
  517. just_disconnected = true;
  518. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  519. fr = get_node(String(connecting_from)); //maybe it was erased
  520. if (Object::cast_to<GraphNode>(fr)) {
  521. connecting = true;
  522. }
  523. return;
  524. }
  525. }
  526. }
  527. }
  528. connecting = true;
  529. connecting_from = gn->get_name();
  530. connecting_index = j;
  531. connecting_out = false;
  532. connecting_type = gn->get_connection_input_type(j);
  533. connecting_color = gn->get_connection_input_color(j);
  534. connecting_target = false;
  535. connecting_to = pos;
  536. just_disconnected = false;
  537. return;
  538. }
  539. }
  540. }
  541. }
  542. Ref<InputEventMouseMotion> mm = p_ev;
  543. if (mm.is_valid() && connecting) {
  544. connecting_to = mm->get_position();
  545. connecting_target = false;
  546. top_layer->update();
  547. minimap->update();
  548. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > 20.0 * zoom;
  549. if (connecting_valid) {
  550. Ref<Texture2D> port = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  551. Vector2 mpos = mm->get_position() / zoom;
  552. for (int i = get_child_count() - 1; i >= 0; i--) {
  553. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  554. if (!gn) {
  555. continue;
  556. }
  557. if (!connecting_out) {
  558. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  559. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  560. int type = gn->get_connection_output_type(j);
  561. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos / zoom, mpos)) {
  562. connecting_target = true;
  563. connecting_to = pos;
  564. connecting_target_to = gn->get_name();
  565. connecting_target_index = j;
  566. return;
  567. }
  568. }
  569. } else {
  570. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  571. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  572. int type = gn->get_connection_input_type(j);
  573. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos / zoom, mpos)) {
  574. connecting_target = true;
  575. connecting_to = pos;
  576. connecting_target_to = gn->get_name();
  577. connecting_target_index = j;
  578. return;
  579. }
  580. }
  581. }
  582. }
  583. }
  584. }
  585. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT && !mb->is_pressed()) {
  586. if (connecting_valid) {
  587. if (connecting && connecting_target) {
  588. String from = connecting_from;
  589. int from_slot = connecting_index;
  590. String to = connecting_target_to;
  591. int to_slot = connecting_target_index;
  592. if (!connecting_out) {
  593. SWAP(from, to);
  594. SWAP(from_slot, to_slot);
  595. }
  596. emit_signal(SNAME("connection_request"), from, from_slot, to, to_slot);
  597. } else if (!just_disconnected) {
  598. String from = connecting_from;
  599. int from_slot = connecting_index;
  600. Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
  601. if (!connecting_out) {
  602. emit_signal(SNAME("connection_from_empty"), from, from_slot, ofs);
  603. } else {
  604. emit_signal(SNAME("connection_to_empty"), from, from_slot, ofs);
  605. }
  606. }
  607. }
  608. connecting = false;
  609. top_layer->update();
  610. minimap->update();
  611. update();
  612. connections_layer->update();
  613. }
  614. }
  615. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
  616. if (p_control->is_set_as_top_level() || !p_control->is_visible()) {
  617. return false;
  618. }
  619. if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  620. //test children
  621. for (int i = 0; i < p_control->get_child_count(); i++) {
  622. Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
  623. if (!subchild) {
  624. continue;
  625. }
  626. if (_check_clickable_control(subchild, pos - subchild->get_position())) {
  627. return true;
  628. }
  629. }
  630. return false;
  631. } else {
  632. return true;
  633. }
  634. }
  635. bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos) {
  636. 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)) {
  637. return false;
  638. }
  639. for (int i = 0; i < get_child_count(); i++) {
  640. Control *child = Object::cast_to<Control>(get_child(i));
  641. if (!child) {
  642. continue;
  643. }
  644. Rect2 rect = child->get_rect();
  645. // To prevent intersections with other nodes.
  646. rect.position *= zoom;
  647. rect.size *= zoom;
  648. if (rect.has_point(p_mouse_pos)) {
  649. //check sub-controls
  650. Vector2 subpos = p_mouse_pos - rect.position;
  651. for (int j = 0; j < child->get_child_count(); j++) {
  652. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  653. if (!subchild) {
  654. continue;
  655. }
  656. if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
  657. return false;
  658. }
  659. }
  660. }
  661. }
  662. return true;
  663. }
  664. template <class Vector2>
  665. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  666. /* Formula from Wikipedia article on Bezier curves. */
  667. real_t omt = (1.0 - t);
  668. real_t omt2 = omt * omt;
  669. real_t omt3 = omt2 * omt;
  670. real_t t2 = t * t;
  671. real_t t3 = t2 * t;
  672. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  673. }
  674. 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 {
  675. float mp = p_begin + (p_end - p_begin) * 0.5;
  676. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  677. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  678. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  679. Vector2 na = (mid - beg).normalized();
  680. Vector2 nb = (end - mid).normalized();
  681. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  682. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  683. points.push_back((beg + end) * 0.5);
  684. colors.push_back(p_color.lerp(p_to_color, mp));
  685. lines++;
  686. } else {
  687. _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);
  688. _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);
  689. }
  690. }
  691. 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, float p_bezier_ratio) {
  692. //cubic bezier code
  693. float diff = p_to.x - p_from.x;
  694. float cp_offset;
  695. int cp_len = get_theme_constant(SNAME("bezier_len_pos")) * p_bezier_ratio;
  696. int cp_neg_len = get_theme_constant(SNAME("bezier_len_neg")) * p_bezier_ratio;
  697. if (diff > 0) {
  698. cp_offset = MIN(cp_len, diff * 0.5);
  699. } else {
  700. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  701. }
  702. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  703. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  704. int lines = 0;
  705. Vector<Point2> points;
  706. Vector<Color> colors;
  707. points.push_back(p_from);
  708. colors.push_back(p_color);
  709. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
  710. points.push_back(p_to);
  711. colors.push_back(p_to_color);
  712. #ifdef TOOLS_ENABLED
  713. p_where->draw_polyline_colors(points, colors, Math::floor(p_width * EDSCALE), lines_antialiased);
  714. #else
  715. p_where->draw_polyline_colors(points, colors, p_width, lines_antialiased);
  716. #endif
  717. }
  718. void GraphEdit::_connections_layer_draw() {
  719. Color activity_color = get_theme_color(SNAME("activity"));
  720. //draw connections
  721. List<List<Connection>::Element *> to_erase;
  722. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  723. NodePath fromnp(E->get().from);
  724. Node *from = get_node(fromnp);
  725. if (!from) {
  726. to_erase.push_back(E);
  727. continue;
  728. }
  729. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  730. if (!gfrom) {
  731. to_erase.push_back(E);
  732. continue;
  733. }
  734. NodePath tonp(E->get().to);
  735. Node *to = get_node(tonp);
  736. if (!to) {
  737. to_erase.push_back(E);
  738. continue;
  739. }
  740. GraphNode *gto = Object::cast_to<GraphNode>(to);
  741. if (!gto) {
  742. to_erase.push_back(E);
  743. continue;
  744. }
  745. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_position_offset() * zoom;
  746. Color color = gfrom->get_connection_output_color(E->get().from_port);
  747. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_position_offset() * zoom;
  748. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  749. if (E->get().activity > 0) {
  750. color = color.lerp(activity_color, E->get().activity);
  751. tocolor = tocolor.lerp(activity_color, E->get().activity);
  752. }
  753. _draw_cos_line(connections_layer, frompos, topos, color, tocolor, lines_thickness);
  754. }
  755. while (to_erase.size()) {
  756. connections.erase(to_erase.front()->get());
  757. to_erase.pop_front();
  758. }
  759. }
  760. void GraphEdit::_top_layer_draw() {
  761. _update_scroll();
  762. if (connecting) {
  763. Node *fromn = get_node(connecting_from);
  764. ERR_FAIL_COND(!fromn);
  765. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  766. ERR_FAIL_COND(!from);
  767. Vector2 pos;
  768. if (connecting_out) {
  769. pos = from->get_connection_output_position(connecting_index);
  770. } else {
  771. pos = from->get_connection_input_position(connecting_index);
  772. }
  773. pos += from->get_position();
  774. Vector2 topos;
  775. topos = connecting_to;
  776. Color col = connecting_color;
  777. if (connecting_target) {
  778. col.r += 0.4;
  779. col.g += 0.4;
  780. col.b += 0.4;
  781. }
  782. if (!connecting_out) {
  783. SWAP(pos, topos);
  784. }
  785. _draw_cos_line(top_layer, pos, topos, col, col, lines_thickness);
  786. }
  787. if (box_selecting) {
  788. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_fill")));
  789. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_stroke")), false);
  790. }
  791. }
  792. void GraphEdit::_minimap_draw() {
  793. if (!is_minimap_enabled()) {
  794. return;
  795. }
  796. minimap->update_minimap();
  797. // Draw the minimap background.
  798. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  799. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("bg")), minimap_rect);
  800. Vector2 graph_offset = minimap->_get_graph_offset();
  801. Vector2 minimap_offset = minimap->minimap_offset;
  802. // Draw comment graph nodes.
  803. for (int i = get_child_count() - 1; i >= 0; i--) {
  804. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  805. if (!gn || !gn->is_comment()) {
  806. continue;
  807. }
  808. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  809. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  810. Rect2 node_rect = Rect2(node_position, node_size);
  811. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  812. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  813. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "commentfocus" : "comment");
  814. if (sbf.is_valid()) {
  815. Color node_color = sbf->get_bg_color();
  816. sb_minimap->set_bg_color(node_color);
  817. }
  818. minimap->draw_style_box(sb_minimap, node_rect);
  819. }
  820. // Draw regular graph nodes.
  821. for (int i = get_child_count() - 1; i >= 0; i--) {
  822. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  823. if (!gn || gn->is_comment()) {
  824. continue;
  825. }
  826. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  827. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  828. Rect2 node_rect = Rect2(node_position, node_size);
  829. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  830. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  831. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "selectedframe" : "frame");
  832. if (sbf.is_valid()) {
  833. Color node_color = sbf->get_border_color();
  834. sb_minimap->set_bg_color(node_color);
  835. }
  836. minimap->draw_style_box(sb_minimap, node_rect);
  837. }
  838. // Draw node connections.
  839. Color activity_color = get_theme_color(SNAME("activity"));
  840. for (const Connection &E : connections) {
  841. NodePath fromnp(E.from);
  842. Node *from = get_node(fromnp);
  843. if (!from) {
  844. continue;
  845. }
  846. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  847. if (!gfrom) {
  848. continue;
  849. }
  850. NodePath tonp(E.to);
  851. Node *to = get_node(tonp);
  852. if (!to) {
  853. continue;
  854. }
  855. GraphNode *gto = Object::cast_to<GraphNode>(to);
  856. if (!gto) {
  857. continue;
  858. }
  859. Vector2 from_slot_position = gfrom->get_position_offset() * zoom + gfrom->get_connection_output_position(E.from_port);
  860. Vector2 from_position = minimap->_convert_from_graph_position(from_slot_position - graph_offset) + minimap_offset;
  861. Color from_color = gfrom->get_connection_output_color(E.from_port);
  862. Vector2 to_slot_position = gto->get_position_offset() * zoom + gto->get_connection_input_position(E.to_port);
  863. Vector2 to_position = minimap->_convert_from_graph_position(to_slot_position - graph_offset) + minimap_offset;
  864. Color to_color = gto->get_connection_input_color(E.to_port);
  865. if (E.activity > 0) {
  866. from_color = from_color.lerp(activity_color, E.activity);
  867. to_color = to_color.lerp(activity_color, E.activity);
  868. }
  869. _draw_cos_line(minimap, from_position, to_position, from_color, to_color, 1.0, 0.5);
  870. }
  871. // Draw the "camera" viewport.
  872. Rect2 camera_rect = minimap->get_camera_rect();
  873. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("camera")), camera_rect);
  874. // Draw the resizer control.
  875. Ref<Texture2D> resizer = minimap->get_theme_icon(SNAME("resizer"));
  876. Color resizer_color = minimap->get_theme_color(SNAME("resizer_color"));
  877. minimap->draw_texture(resizer, Point2(), resizer_color);
  878. }
  879. void GraphEdit::set_selected(Node *p_child) {
  880. for (int i = get_child_count() - 1; i >= 0; i--) {
  881. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  882. if (!gn) {
  883. continue;
  884. }
  885. gn->set_selected(gn == p_child);
  886. }
  887. }
  888. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  889. ERR_FAIL_COND(p_ev.is_null());
  890. Ref<InputEventMouseMotion> mm = p_ev;
  891. if (mm.is_valid() && (mm->get_button_mask() & MOUSE_BUTTON_MASK_MIDDLE || (mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
  892. Vector2i relative = Input::get_singleton()->warp_mouse_motion(mm, get_global_rect());
  893. h_scroll->set_value(h_scroll->get_value() - relative.x);
  894. v_scroll->set_value(v_scroll->get_value() - relative.y);
  895. }
  896. if (mm.is_valid() && dragging) {
  897. if (!moving_selection) {
  898. emit_signal(SNAME("begin_node_move"));
  899. moving_selection = true;
  900. }
  901. just_selected = true;
  902. drag_accum += mm->get_relative();
  903. for (int i = get_child_count() - 1; i >= 0; i--) {
  904. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  905. if (gn && gn->is_selected()) {
  906. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  907. // Snapping can be toggled temporarily by holding down Ctrl.
  908. // This is done here as to not toggle the grid when holding down Ctrl.
  909. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  910. const int snap = get_snap();
  911. pos = pos.snapped(Vector2(snap, snap));
  912. }
  913. gn->set_position_offset(pos);
  914. }
  915. }
  916. }
  917. if (mm.is_valid() && box_selecting) {
  918. box_selecting_to = mm->get_position();
  919. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  920. MIN(box_selecting_from.y, box_selecting_to.y),
  921. ABS(box_selecting_from.x - box_selecting_to.x),
  922. ABS(box_selecting_from.y - box_selecting_to.y));
  923. for (int i = get_child_count() - 1; i >= 0; i--) {
  924. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  925. if (!gn) {
  926. continue;
  927. }
  928. Rect2 r = gn->get_rect();
  929. r.size *= zoom;
  930. bool in_box = r.intersects(box_selecting_rect);
  931. if (in_box) {
  932. if (!gn->is_selected() && box_selection_mode_additive) {
  933. emit_signal(SNAME("node_selected"), gn);
  934. } else if (gn->is_selected() && !box_selection_mode_additive) {
  935. emit_signal(SNAME("node_deselected"), gn);
  936. }
  937. gn->set_selected(box_selection_mode_additive);
  938. } else {
  939. bool select = (previous_selected.find(gn) != nullptr);
  940. if (gn->is_selected() && !select) {
  941. emit_signal(SNAME("node_deselected"), gn);
  942. } else if (!gn->is_selected() && select) {
  943. emit_signal(SNAME("node_selected"), gn);
  944. }
  945. gn->set_selected(select);
  946. }
  947. }
  948. top_layer->update();
  949. minimap->update();
  950. }
  951. Ref<InputEventMouseButton> b = p_ev;
  952. if (b.is_valid()) {
  953. if (b->get_button_index() == MOUSE_BUTTON_RIGHT && b->is_pressed()) {
  954. if (box_selecting) {
  955. box_selecting = false;
  956. for (int i = get_child_count() - 1; i >= 0; i--) {
  957. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  958. if (!gn) {
  959. continue;
  960. }
  961. bool select = (previous_selected.find(gn) != nullptr);
  962. if (gn->is_selected() && !select) {
  963. emit_signal(SNAME("node_deselected"), gn);
  964. } else if (!gn->is_selected() && select) {
  965. emit_signal(SNAME("node_selected"), gn);
  966. }
  967. gn->set_selected(select);
  968. }
  969. top_layer->update();
  970. minimap->update();
  971. } else {
  972. if (connecting) {
  973. connecting = false;
  974. top_layer->update();
  975. minimap->update();
  976. } else {
  977. emit_signal(SNAME("popup_request"), b->get_global_position());
  978. }
  979. }
  980. }
  981. if (b->get_button_index() == MOUSE_BUTTON_LEFT && !b->is_pressed() && dragging) {
  982. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  983. //deselect current node
  984. for (int i = get_child_count() - 1; i >= 0; i--) {
  985. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  986. if (gn) {
  987. Rect2 r = gn->get_rect();
  988. r.size *= zoom;
  989. if (r.has_point(b->get_position())) {
  990. emit_signal(SNAME("node_deselected"), gn);
  991. gn->set_selected(false);
  992. }
  993. }
  994. }
  995. }
  996. if (drag_accum != Vector2()) {
  997. for (int i = get_child_count() - 1; i >= 0; i--) {
  998. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  999. if (gn && gn->is_selected()) {
  1000. gn->set_drag(false);
  1001. }
  1002. }
  1003. }
  1004. if (moving_selection) {
  1005. emit_signal(SNAME("end_node_move"));
  1006. moving_selection = false;
  1007. }
  1008. dragging = false;
  1009. top_layer->update();
  1010. minimap->update();
  1011. update();
  1012. connections_layer->update();
  1013. }
  1014. if (b->get_button_index() == MOUSE_BUTTON_LEFT && b->is_pressed()) {
  1015. GraphNode *gn = nullptr;
  1016. for (int i = get_child_count() - 1; i >= 0; i--) {
  1017. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  1018. if (gn_selected) {
  1019. if (gn_selected->is_resizing()) {
  1020. continue;
  1021. }
  1022. if (gn_selected->has_point((b->get_position() - gn_selected->get_position()) / zoom)) {
  1023. gn = gn_selected;
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. if (gn) {
  1029. if (_filter_input(b->get_position())) {
  1030. return;
  1031. }
  1032. dragging = true;
  1033. drag_accum = Vector2();
  1034. just_selected = !gn->is_selected();
  1035. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  1036. for (int i = 0; i < get_child_count(); i++) {
  1037. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1038. if (o_gn) {
  1039. if (o_gn == gn) {
  1040. o_gn->set_selected(true);
  1041. } else {
  1042. if (o_gn->is_selected()) {
  1043. emit_signal(SNAME("node_deselected"), o_gn);
  1044. }
  1045. o_gn->set_selected(false);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. gn->set_selected(true);
  1051. for (int i = 0; i < get_child_count(); i++) {
  1052. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1053. if (!o_gn) {
  1054. continue;
  1055. }
  1056. if (o_gn->is_selected()) {
  1057. o_gn->set_drag(true);
  1058. }
  1059. }
  1060. } else {
  1061. if (_filter_input(b->get_position())) {
  1062. return;
  1063. }
  1064. if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  1065. return;
  1066. }
  1067. box_selecting = true;
  1068. box_selecting_from = b->get_position();
  1069. if (b->is_ctrl_pressed()) {
  1070. box_selection_mode_additive = true;
  1071. previous_selected.clear();
  1072. for (int i = get_child_count() - 1; i >= 0; i--) {
  1073. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1074. if (!gn2 || !gn2->is_selected()) {
  1075. continue;
  1076. }
  1077. previous_selected.push_back(gn2);
  1078. }
  1079. } else if (b->is_shift_pressed()) {
  1080. box_selection_mode_additive = false;
  1081. previous_selected.clear();
  1082. for (int i = get_child_count() - 1; i >= 0; i--) {
  1083. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1084. if (!gn2 || !gn2->is_selected()) {
  1085. continue;
  1086. }
  1087. previous_selected.push_back(gn2);
  1088. }
  1089. } else {
  1090. box_selection_mode_additive = true;
  1091. previous_selected.clear();
  1092. for (int i = get_child_count() - 1; i >= 0; i--) {
  1093. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1094. if (!gn2) {
  1095. continue;
  1096. }
  1097. if (gn2->is_selected()) {
  1098. emit_signal(SNAME("node_deselected"), gn2);
  1099. }
  1100. gn2->set_selected(false);
  1101. }
  1102. }
  1103. }
  1104. }
  1105. if (b->get_button_index() == MOUSE_BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  1106. box_selecting = false;
  1107. box_selecting_rect = Rect2();
  1108. previous_selected.clear();
  1109. top_layer->update();
  1110. minimap->update();
  1111. }
  1112. int scroll_direction = (b->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN) - (b->get_button_index() == MOUSE_BUTTON_WHEEL_UP);
  1113. if (scroll_direction != 0) {
  1114. if (b->is_ctrl_pressed()) {
  1115. if (b->is_shift_pressed()) {
  1116. // Horizontal scrolling.
  1117. h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1118. } else {
  1119. // Vertical scrolling.
  1120. v_scroll->set_value(v_scroll->get_value() + (v_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1121. }
  1122. } else {
  1123. // Zooming.
  1124. set_zoom_custom(scroll_direction < 0 ? zoom * zoom_step : zoom / zoom_step, b->get_position());
  1125. }
  1126. }
  1127. }
  1128. if (p_ev->is_pressed()) {
  1129. if (p_ev->is_action("ui_graph_duplicate")) {
  1130. emit_signal(SNAME("duplicate_nodes_request"));
  1131. accept_event();
  1132. } else if (p_ev->is_action("ui_copy")) {
  1133. emit_signal(SNAME("copy_nodes_request"));
  1134. accept_event();
  1135. } else if (p_ev->is_action("ui_paste")) {
  1136. emit_signal(SNAME("paste_nodes_request"));
  1137. accept_event();
  1138. } else if (p_ev->is_action("ui_graph_delete")) {
  1139. emit_signal(SNAME("delete_nodes_request"));
  1140. accept_event();
  1141. }
  1142. }
  1143. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  1144. if (magnify_gesture.is_valid()) {
  1145. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  1146. }
  1147. Ref<InputEventPanGesture> pan_gesture = p_ev;
  1148. if (pan_gesture.is_valid()) {
  1149. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  1150. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  1151. }
  1152. }
  1153. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1154. for (Connection &E : connections) {
  1155. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  1156. if (Math::is_equal_approx(E.activity, p_activity)) {
  1157. //update only if changed
  1158. top_layer->update();
  1159. minimap->update();
  1160. connections_layer->update();
  1161. }
  1162. E.activity = p_activity;
  1163. return;
  1164. }
  1165. }
  1166. }
  1167. void GraphEdit::clear_connections() {
  1168. connections.clear();
  1169. minimap->update();
  1170. update();
  1171. connections_layer->update();
  1172. }
  1173. void GraphEdit::set_zoom(float p_zoom) {
  1174. set_zoom_custom(p_zoom, get_size() / 2);
  1175. }
  1176. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1177. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1178. if (zoom == p_zoom) {
  1179. return;
  1180. }
  1181. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  1182. zoom = p_zoom;
  1183. top_layer->update();
  1184. zoom_minus->set_disabled(zoom == zoom_min);
  1185. zoom_plus->set_disabled(zoom == zoom_max);
  1186. _update_scroll();
  1187. minimap->update();
  1188. connections_layer->update();
  1189. if (is_visible_in_tree()) {
  1190. Vector2 ofs = sbofs * zoom - p_center;
  1191. h_scroll->set_value(ofs.x);
  1192. v_scroll->set_value(ofs.y);
  1193. }
  1194. _update_zoom_label();
  1195. update();
  1196. }
  1197. float GraphEdit::get_zoom() const {
  1198. return zoom;
  1199. }
  1200. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1201. p_zoom_step = abs(p_zoom_step);
  1202. if (zoom_step == p_zoom_step) {
  1203. return;
  1204. }
  1205. zoom_step = p_zoom_step;
  1206. }
  1207. float GraphEdit::get_zoom_step() const {
  1208. return zoom_step;
  1209. }
  1210. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1211. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1212. if (zoom_min == p_zoom_min) {
  1213. return;
  1214. }
  1215. zoom_min = p_zoom_min;
  1216. set_zoom(zoom);
  1217. }
  1218. float GraphEdit::get_zoom_min() const {
  1219. return zoom_min;
  1220. }
  1221. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1222. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1223. if (zoom_max == p_zoom_max) {
  1224. return;
  1225. }
  1226. zoom_max = p_zoom_max;
  1227. set_zoom(zoom);
  1228. }
  1229. float GraphEdit::get_zoom_max() const {
  1230. return zoom_max;
  1231. }
  1232. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1233. if (zoom_label->is_visible() == p_enable) {
  1234. return;
  1235. }
  1236. zoom_label->set_visible(p_enable);
  1237. }
  1238. bool GraphEdit::is_showing_zoom_label() const {
  1239. return zoom_label->is_visible();
  1240. }
  1241. void GraphEdit::set_right_disconnects(bool p_enable) {
  1242. right_disconnects = p_enable;
  1243. }
  1244. bool GraphEdit::is_right_disconnects_enabled() const {
  1245. return right_disconnects;
  1246. }
  1247. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1248. valid_right_disconnect_types.insert(p_type);
  1249. }
  1250. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1251. valid_right_disconnect_types.erase(p_type);
  1252. }
  1253. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1254. valid_left_disconnect_types.insert(p_type);
  1255. }
  1256. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1257. valid_left_disconnect_types.erase(p_type);
  1258. }
  1259. Array GraphEdit::_get_connection_list() const {
  1260. List<Connection> conns;
  1261. get_connection_list(&conns);
  1262. Array arr;
  1263. for (const Connection &E : conns) {
  1264. Dictionary d;
  1265. d["from"] = E.from;
  1266. d["from_port"] = E.from_port;
  1267. d["to"] = E.to;
  1268. d["to_port"] = E.to_port;
  1269. arr.push_back(d);
  1270. }
  1271. return arr;
  1272. }
  1273. void GraphEdit::_zoom_minus() {
  1274. set_zoom(zoom / zoom_step);
  1275. }
  1276. void GraphEdit::_zoom_reset() {
  1277. set_zoom(1);
  1278. }
  1279. void GraphEdit::_zoom_plus() {
  1280. set_zoom(zoom * zoom_step);
  1281. }
  1282. void GraphEdit::_update_zoom_label() {
  1283. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1284. String zoom_text = itos(zoom_percent) + "%";
  1285. zoom_label->set_text(zoom_text);
  1286. }
  1287. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1288. ConnType ct;
  1289. ct.type_a = p_type;
  1290. ct.type_b = p_with_type;
  1291. valid_connection_types.insert(ct);
  1292. }
  1293. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1294. ConnType ct;
  1295. ct.type_a = p_type;
  1296. ct.type_b = p_with_type;
  1297. valid_connection_types.erase(ct);
  1298. }
  1299. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1300. ConnType ct;
  1301. ct.type_a = p_type;
  1302. ct.type_b = p_with_type;
  1303. return valid_connection_types.has(ct);
  1304. }
  1305. void GraphEdit::set_use_snap(bool p_enable) {
  1306. snap_button->set_pressed(p_enable);
  1307. update();
  1308. }
  1309. bool GraphEdit::is_using_snap() const {
  1310. return snap_button->is_pressed();
  1311. }
  1312. int GraphEdit::get_snap() const {
  1313. return snap_amount->get_value();
  1314. }
  1315. void GraphEdit::set_snap(int p_snap) {
  1316. ERR_FAIL_COND(p_snap < 5);
  1317. snap_amount->set_value(p_snap);
  1318. update();
  1319. }
  1320. void GraphEdit::_snap_toggled() {
  1321. update();
  1322. }
  1323. void GraphEdit::_snap_value_changed(double) {
  1324. update();
  1325. }
  1326. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1327. minimap->set_size(p_size);
  1328. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1329. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1330. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1331. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1332. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1333. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1334. minimap->update();
  1335. }
  1336. Vector2 GraphEdit::get_minimap_size() const {
  1337. return minimap->get_size();
  1338. }
  1339. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1340. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1341. minimap->update();
  1342. }
  1343. float GraphEdit::get_minimap_opacity() const {
  1344. Color minimap_modulate = minimap->get_modulate();
  1345. return minimap_modulate.a;
  1346. }
  1347. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1348. minimap_button->set_pressed(p_enable);
  1349. minimap->update();
  1350. }
  1351. bool GraphEdit::is_minimap_enabled() const {
  1352. return minimap_button->is_pressed();
  1353. }
  1354. void GraphEdit::_minimap_toggled() {
  1355. if (is_minimap_enabled()) {
  1356. minimap->set_visible(true);
  1357. minimap->update();
  1358. } else {
  1359. minimap->set_visible(false);
  1360. }
  1361. }
  1362. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  1363. lines_thickness = p_thickness;
  1364. update();
  1365. }
  1366. float GraphEdit::get_connection_lines_thickness() const {
  1367. return lines_thickness;
  1368. }
  1369. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  1370. lines_antialiased = p_antialiased;
  1371. update();
  1372. }
  1373. bool GraphEdit::is_connection_lines_antialiased() const {
  1374. return lines_antialiased;
  1375. }
  1376. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1377. return zoom_hb;
  1378. }
  1379. int GraphEdit::_set_operations(SET_OPERATIONS p_operation, Set<StringName> &r_u, const Set<StringName> &r_v) {
  1380. switch (p_operation) {
  1381. case GraphEdit::IS_EQUAL: {
  1382. for (Set<StringName>::Element *E = r_u.front(); E; E = E->next()) {
  1383. if (!r_v.has(E->get()))
  1384. return 0;
  1385. }
  1386. return r_u.size() == r_v.size();
  1387. } break;
  1388. case GraphEdit::IS_SUBSET: {
  1389. if (r_u.size() == r_v.size() && !r_u.size()) {
  1390. return 1;
  1391. }
  1392. for (Set<StringName>::Element *E = r_u.front(); E; E = E->next()) {
  1393. if (!r_v.has(E->get()))
  1394. return 0;
  1395. }
  1396. return 1;
  1397. } break;
  1398. case GraphEdit::DIFFERENCE: {
  1399. for (Set<StringName>::Element *E = r_u.front(); E; E = E->next()) {
  1400. if (r_v.has(E->get())) {
  1401. r_u.erase(E->get());
  1402. }
  1403. }
  1404. return r_u.size();
  1405. } break;
  1406. case GraphEdit::UNION: {
  1407. for (Set<StringName>::Element *E = r_v.front(); E; E = E->next()) {
  1408. if (!r_u.has(E->get())) {
  1409. r_u.insert(E->get());
  1410. }
  1411. }
  1412. return r_v.size();
  1413. } break;
  1414. default:
  1415. break;
  1416. }
  1417. return -1;
  1418. }
  1419. HashMap<int, Vector<StringName>> GraphEdit::_layering(const Set<StringName> &r_selected_nodes, const HashMap<StringName, Set<StringName>> &r_upper_neighbours) {
  1420. HashMap<int, Vector<StringName>> l;
  1421. Set<StringName> p = r_selected_nodes, q = r_selected_nodes, u, z;
  1422. int current_layer = 0;
  1423. bool selected = false;
  1424. while (!_set_operations(GraphEdit::IS_EQUAL, q, u)) {
  1425. _set_operations(GraphEdit::DIFFERENCE, p, u);
  1426. for (const Set<StringName>::Element *E = p.front(); E; E = E->next()) {
  1427. Set<StringName> n = r_upper_neighbours[E->get()];
  1428. if (_set_operations(GraphEdit::IS_SUBSET, n, z)) {
  1429. Vector<StringName> t;
  1430. t.push_back(E->get());
  1431. if (!l.has(current_layer)) {
  1432. l.set(current_layer, Vector<StringName>{});
  1433. }
  1434. selected = true;
  1435. t.append_array(l[current_layer]);
  1436. l.set(current_layer, t);
  1437. Set<StringName> V;
  1438. V.insert(E->get());
  1439. _set_operations(GraphEdit::UNION, u, V);
  1440. }
  1441. }
  1442. if (!selected) {
  1443. current_layer++;
  1444. _set_operations(GraphEdit::UNION, z, u);
  1445. }
  1446. selected = false;
  1447. }
  1448. return l;
  1449. }
  1450. Vector<StringName> GraphEdit::_split(const Vector<StringName> &r_layer, const HashMap<StringName, Dictionary> &r_crossings) {
  1451. if (!r_layer.size()) {
  1452. return Vector<StringName>();
  1453. }
  1454. StringName p = r_layer[Math::random(0, r_layer.size() - 1)];
  1455. Vector<StringName> left;
  1456. Vector<StringName> right;
  1457. for (int i = 0; i < r_layer.size(); i++) {
  1458. if (p != r_layer[i]) {
  1459. StringName q = r_layer[i];
  1460. int cross_pq = r_crossings[p][q];
  1461. int cross_qp = r_crossings[q][p];
  1462. if (cross_pq > cross_qp) {
  1463. left.push_back(q);
  1464. } else {
  1465. right.push_back(q);
  1466. }
  1467. }
  1468. }
  1469. left.push_back(p);
  1470. left.append_array(right);
  1471. return left;
  1472. }
  1473. void GraphEdit::_horizontal_alignment(Dictionary &r_root, Dictionary &r_align, const HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, Set<StringName>> &r_upper_neighbours, const Set<StringName> &r_selected_nodes) {
  1474. for (const Set<StringName>::Element *E = r_selected_nodes.front(); E; E = E->next()) {
  1475. r_root[E->get()] = E->get();
  1476. r_align[E->get()] = E->get();
  1477. }
  1478. if (r_layers.size() == 1) {
  1479. return;
  1480. }
  1481. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1482. Vector<StringName> lower_layer = r_layers[i];
  1483. Vector<StringName> upper_layer = r_layers[i - 1];
  1484. int r = -1;
  1485. for (int j = 0; j < lower_layer.size(); j++) {
  1486. Vector<Pair<int, StringName>> up;
  1487. StringName current_node = lower_layer[j];
  1488. for (int k = 0; k < upper_layer.size(); k++) {
  1489. StringName adjacent_neighbour = upper_layer[k];
  1490. if (r_upper_neighbours[current_node].has(adjacent_neighbour)) {
  1491. up.push_back(Pair<int, StringName>(k, adjacent_neighbour));
  1492. }
  1493. }
  1494. int start = up.size() / 2;
  1495. int end = up.size() % 2 ? start : start + 1;
  1496. for (int p = start; p <= end; p++) {
  1497. StringName Align = r_align[current_node];
  1498. if (Align == current_node && r < up[p].first) {
  1499. r_align[up[p].second] = lower_layer[j];
  1500. r_root[current_node] = r_root[up[p].second];
  1501. r_align[current_node] = r_root[up[p].second];
  1502. r = up[p].first;
  1503. }
  1504. }
  1505. }
  1506. }
  1507. }
  1508. void GraphEdit::_crossing_minimisation(HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, Set<StringName>> &r_upper_neighbours) {
  1509. if (r_layers.size() == 1) {
  1510. return;
  1511. }
  1512. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1513. Vector<StringName> upper_layer = r_layers[i - 1];
  1514. Vector<StringName> lower_layer = r_layers[i];
  1515. HashMap<StringName, Dictionary> c;
  1516. for (int j = 0; j < lower_layer.size(); j++) {
  1517. StringName p = lower_layer[j];
  1518. Dictionary d;
  1519. for (int k = 0; k < lower_layer.size(); k++) {
  1520. unsigned int crossings = 0;
  1521. StringName q = lower_layer[k];
  1522. if (j != k) {
  1523. for (int h = 1; h < upper_layer.size(); h++) {
  1524. if (r_upper_neighbours[p].has(upper_layer[h])) {
  1525. for (int g = 0; g < h; g++) {
  1526. if (r_upper_neighbours[q].has(upper_layer[g])) {
  1527. crossings++;
  1528. }
  1529. }
  1530. }
  1531. }
  1532. }
  1533. d[q] = crossings;
  1534. }
  1535. c.set(p, d);
  1536. }
  1537. r_layers.set(i, _split(lower_layer, c));
  1538. }
  1539. }
  1540. void GraphEdit::_calculate_inner_shifts(Dictionary &r_inner_shifts, const Dictionary &r_root, const Dictionary &r_node_names, const Dictionary &r_align, const Set<StringName> &r_block_heads, const HashMap<StringName, Pair<int, int>> &r_port_info) {
  1541. for (const Set<StringName>::Element *E = r_block_heads.front(); E; E = E->next()) {
  1542. real_t left = 0;
  1543. StringName u = E->get();
  1544. StringName v = r_align[u];
  1545. while (u != v && (StringName)r_root[u] != v) {
  1546. String _connection = String(u) + " " + String(v);
  1547. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[u]);
  1548. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[v]);
  1549. Pair<int, int> ports = r_port_info[_connection];
  1550. int pfrom = ports.first;
  1551. int pto = ports.second;
  1552. Vector2 frompos = gfrom->get_connection_output_position(pfrom);
  1553. Vector2 topos = gto->get_connection_input_position(pto);
  1554. real_t s = (real_t)r_inner_shifts[u] + (frompos.y - topos.y) / zoom;
  1555. r_inner_shifts[v] = s;
  1556. left = MIN(left, s);
  1557. u = v;
  1558. v = (StringName)r_align[v];
  1559. }
  1560. u = E->get();
  1561. do {
  1562. r_inner_shifts[u] = (real_t)r_inner_shifts[u] - left;
  1563. u = (StringName)r_align[u];
  1564. } while (u != E->get());
  1565. }
  1566. }
  1567. float GraphEdit::_calculate_threshold(StringName p_v, StringName p_w, const Dictionary &r_node_names, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_inner_shift, real_t p_current_threshold, const HashMap<StringName, Vector2> &r_node_positions) {
  1568. #define MAX_ORDER 2147483647
  1569. #define ORDER(node, layers) \
  1570. for (unsigned int i = 0; i < layers.size(); i++) { \
  1571. int index = layers[i].find(node); \
  1572. if (index > 0) { \
  1573. order = index; \
  1574. break; \
  1575. } \
  1576. order = MAX_ORDER; \
  1577. }
  1578. int order = MAX_ORDER;
  1579. float threshold = p_current_threshold;
  1580. if (p_v == p_w) {
  1581. int min_order = MAX_ORDER;
  1582. Connection incoming;
  1583. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1584. if (E->get().to == p_w) {
  1585. ORDER(E->get().from, r_layers);
  1586. if (min_order > order) {
  1587. min_order = order;
  1588. incoming = E->get();
  1589. }
  1590. }
  1591. }
  1592. if (incoming.from != StringName()) {
  1593. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[incoming.from]);
  1594. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1595. Vector2 frompos = gfrom->get_connection_output_position(incoming.from_port);
  1596. Vector2 topos = gto->get_connection_input_position(incoming.to_port);
  1597. //If connected block node is selected, calculate thershold or add current block to list
  1598. if (gfrom->is_selected()) {
  1599. Vector2 connected_block_pos = r_node_positions[r_root[incoming.from]];
  1600. if (connected_block_pos.y != FLT_MAX) {
  1601. //Connected block is placed. Calculate threshold
  1602. threshold = connected_block_pos.y + (real_t)r_inner_shift[incoming.from] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1603. }
  1604. }
  1605. }
  1606. }
  1607. if (threshold == FLT_MIN && (StringName)r_align[p_w] == p_v) {
  1608. //This time, pick an outgoing edge and repeat as above!
  1609. int min_order = MAX_ORDER;
  1610. Connection outgoing;
  1611. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1612. if (E->get().from == p_w) {
  1613. ORDER(E->get().to, r_layers);
  1614. if (min_order > order) {
  1615. min_order = order;
  1616. outgoing = E->get();
  1617. }
  1618. }
  1619. }
  1620. if (outgoing.to != StringName()) {
  1621. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1622. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[outgoing.to]);
  1623. Vector2 frompos = gfrom->get_connection_output_position(outgoing.from_port);
  1624. Vector2 topos = gto->get_connection_input_position(outgoing.to_port);
  1625. //If connected block node is selected, calculate thershold or add current block to list
  1626. if (gto->is_selected()) {
  1627. Vector2 connected_block_pos = r_node_positions[r_root[outgoing.to]];
  1628. if (connected_block_pos.y != FLT_MAX) {
  1629. //Connected block is placed. Calculate threshold
  1630. threshold = connected_block_pos.y + (real_t)r_inner_shift[outgoing.to] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1631. }
  1632. }
  1633. }
  1634. }
  1635. #undef MAX_ORDER
  1636. #undef ORDER
  1637. return threshold;
  1638. }
  1639. void GraphEdit::_place_block(StringName p_v, float p_delta, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_node_name, const Dictionary &r_inner_shift, Dictionary &r_sink, Dictionary &r_shift, HashMap<StringName, Vector2> &r_node_positions) {
  1640. #define PRED(node, layers) \
  1641. for (unsigned int i = 0; i < layers.size(); i++) { \
  1642. int index = layers[i].find(node); \
  1643. if (index > 0) { \
  1644. predecessor = layers[i][index - 1]; \
  1645. break; \
  1646. } \
  1647. predecessor = StringName(); \
  1648. }
  1649. StringName predecessor;
  1650. StringName successor;
  1651. Vector2 pos = r_node_positions[p_v];
  1652. if (pos.y == FLT_MAX) {
  1653. pos.y = 0;
  1654. bool initial = false;
  1655. StringName w = p_v;
  1656. real_t threshold = FLT_MIN;
  1657. do {
  1658. PRED(w, r_layers);
  1659. if (predecessor != StringName()) {
  1660. StringName u = r_root[predecessor];
  1661. _place_block(u, p_delta, r_layers, r_root, r_align, r_node_name, r_inner_shift, r_sink, r_shift, r_node_positions);
  1662. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1663. if ((StringName)r_sink[p_v] == p_v) {
  1664. r_sink[p_v] = r_sink[u];
  1665. }
  1666. Vector2 predecessor_root_pos = r_node_positions[u];
  1667. Vector2 predecessor_node_size = Object::cast_to<GraphNode>(r_node_name[predecessor])->get_size();
  1668. if (r_sink[p_v] != r_sink[u]) {
  1669. real_t sc = pos.y + (real_t)r_inner_shift[w] - predecessor_root_pos.y - (real_t)r_inner_shift[predecessor] - predecessor_node_size.y - p_delta;
  1670. r_shift[r_sink[u]] = MIN(sc, (real_t)r_shift[r_sink[u]]);
  1671. } else {
  1672. real_t sb = predecessor_root_pos.y + (real_t)r_inner_shift[predecessor] + predecessor_node_size.y - (real_t)r_inner_shift[w] + p_delta;
  1673. sb = MAX(sb, threshold);
  1674. if (initial) {
  1675. pos.y = sb;
  1676. } else {
  1677. pos.y = MAX(pos.y, sb);
  1678. }
  1679. initial = false;
  1680. }
  1681. }
  1682. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1683. w = r_align[w];
  1684. } while (w != p_v);
  1685. r_node_positions.set(p_v, pos);
  1686. }
  1687. #undef PRED
  1688. }
  1689. void GraphEdit::arrange_nodes() {
  1690. if (!arranging_graph) {
  1691. arranging_graph = true;
  1692. } else {
  1693. return;
  1694. }
  1695. Dictionary node_names;
  1696. Set<StringName> selected_nodes;
  1697. for (int i = get_child_count() - 1; i >= 0; i--) {
  1698. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1699. if (!gn) {
  1700. continue;
  1701. }
  1702. node_names[gn->get_name()] = gn;
  1703. }
  1704. HashMap<StringName, Set<StringName>> upper_neighbours;
  1705. HashMap<StringName, Pair<int, int>> port_info;
  1706. Vector2 origin(FLT_MAX, FLT_MAX);
  1707. float gap_v = 100.0f;
  1708. float gap_h = 100.0f;
  1709. for (int i = get_child_count() - 1; i >= 0; i--) {
  1710. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1711. if (!gn) {
  1712. continue;
  1713. }
  1714. if (gn->is_selected()) {
  1715. selected_nodes.insert(gn->get_name());
  1716. origin = origin < gn->get_position_offset() ? origin : gn->get_position_offset();
  1717. Set<StringName> s;
  1718. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1719. GraphNode *p_from = Object::cast_to<GraphNode>(node_names[E->get().from]);
  1720. if (E->get().to == gn->get_name() && p_from->is_selected()) {
  1721. if (!s.has(p_from->get_name())) {
  1722. s.insert(p_from->get_name());
  1723. }
  1724. String s_connection = String(p_from->get_name()) + " " + String(E->get().to);
  1725. StringName _connection(s_connection);
  1726. Pair<int, int> ports(E->get().from_port, E->get().to_port);
  1727. if (port_info.has(_connection)) {
  1728. Pair<int, int> p_ports = port_info[_connection];
  1729. if (p_ports.first < ports.first) {
  1730. ports = p_ports;
  1731. }
  1732. }
  1733. port_info.set(_connection, ports);
  1734. }
  1735. }
  1736. upper_neighbours.set(gn->get_name(), s);
  1737. }
  1738. }
  1739. HashMap<int, Vector<StringName>> layers = _layering(selected_nodes, upper_neighbours);
  1740. _crossing_minimisation(layers, upper_neighbours);
  1741. Dictionary root, align, sink, shift;
  1742. _horizontal_alignment(root, align, layers, upper_neighbours, selected_nodes);
  1743. HashMap<StringName, Vector2> new_positions;
  1744. Vector2 default_position(FLT_MAX, FLT_MAX);
  1745. Dictionary inner_shift;
  1746. Set<StringName> block_heads;
  1747. for (const Set<StringName>::Element *E = selected_nodes.front(); E; E = E->next()) {
  1748. inner_shift[E->get()] = 0.0f;
  1749. sink[E->get()] = E->get();
  1750. shift[E->get()] = FLT_MAX;
  1751. new_positions.set(E->get(), default_position);
  1752. if ((StringName)root[E->get()] == E->get()) {
  1753. block_heads.insert(E->get());
  1754. }
  1755. }
  1756. _calculate_inner_shifts(inner_shift, root, node_names, align, block_heads, port_info);
  1757. for (const Set<StringName>::Element *E = block_heads.front(); E; E = E->next()) {
  1758. _place_block(E->get(), gap_v, layers, root, align, node_names, inner_shift, sink, shift, new_positions);
  1759. }
  1760. for (const Set<StringName>::Element *E = block_heads.front(); E; E = E->next()) {
  1761. StringName u = E->get();
  1762. StringName prev = u;
  1763. float start_from = origin.y + new_positions[E->get()].y;
  1764. do {
  1765. Vector2 cal_pos;
  1766. cal_pos.y = start_from + (real_t)inner_shift[u];
  1767. new_positions.set(u, cal_pos);
  1768. prev = u;
  1769. u = align[u];
  1770. } while (u != E->get());
  1771. }
  1772. //Compute horizontal co-ordinates individually for layers to get uniform gap
  1773. float start_from = origin.x;
  1774. float largest_node_size = 0.0f;
  1775. for (unsigned int i = 0; i < layers.size(); i++) {
  1776. Vector<StringName> layer = layers[i];
  1777. for (int j = 0; j < layer.size(); j++) {
  1778. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1779. largest_node_size = MAX(largest_node_size, current_node_size);
  1780. }
  1781. for (int j = 0; j < layer.size(); j++) {
  1782. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1783. Vector2 cal_pos = new_positions[layer[j]];
  1784. if (current_node_size == largest_node_size) {
  1785. cal_pos.x = start_from;
  1786. } else {
  1787. float current_node_start_pos;
  1788. if (current_node_size >= largest_node_size / 2) {
  1789. current_node_start_pos = start_from;
  1790. } else {
  1791. current_node_start_pos = start_from + largest_node_size - current_node_size;
  1792. }
  1793. cal_pos.x = current_node_start_pos;
  1794. }
  1795. new_positions.set(layer[j], cal_pos);
  1796. }
  1797. start_from += largest_node_size + gap_h;
  1798. largest_node_size = 0.0f;
  1799. }
  1800. emit_signal("begin_node_move");
  1801. for (const Set<StringName>::Element *E = selected_nodes.front(); E; E = E->next()) {
  1802. GraphNode *gn = Object::cast_to<GraphNode>(node_names[E->get()]);
  1803. gn->set_drag(true);
  1804. Vector2 pos = (new_positions[E->get()]);
  1805. if (is_using_snap()) {
  1806. const int snap = get_snap();
  1807. pos = pos.snapped(Vector2(snap, snap));
  1808. }
  1809. gn->set_position_offset(pos);
  1810. gn->set_drag(false);
  1811. }
  1812. emit_signal("end_node_move");
  1813. arranging_graph = false;
  1814. }
  1815. void GraphEdit::_bind_methods() {
  1816. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  1817. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  1818. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  1819. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1820. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1821. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1822. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1823. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  1824. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1825. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1826. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1827. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1828. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1829. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1830. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1831. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  1832. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1833. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1834. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1835. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1836. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1837. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1838. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1839. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1840. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1841. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1842. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1843. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1844. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1845. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  1846. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  1847. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  1848. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  1849. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  1850. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1851. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  1852. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1853. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1854. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1855. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1856. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1857. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  1858. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1859. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1860. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  1861. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1862. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1863. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  1864. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  1865. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1866. ADD_GROUP("Connection Lines", "connection_lines");
  1867. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  1868. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  1869. ADD_GROUP("Zoom", "");
  1870. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1871. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  1872. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  1873. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  1874. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  1875. ADD_GROUP("Minimap", "minimap");
  1876. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  1877. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size"), "set_minimap_size", "get_minimap_size");
  1878. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  1879. 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")));
  1880. 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")));
  1881. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1882. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1883. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1884. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1885. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1886. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1887. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1888. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1889. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  1890. ADD_SIGNAL(MethodInfo("begin_node_move"));
  1891. ADD_SIGNAL(MethodInfo("end_node_move"));
  1892. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  1893. }
  1894. GraphEdit::GraphEdit() {
  1895. set_focus_mode(FOCUS_ALL);
  1896. // Allow dezooming 8 times from the default zoom level.
  1897. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  1898. // but this is still useful for previewing and navigation.
  1899. zoom_min = (1 / Math::pow(zoom_step, 8));
  1900. // Allow zooming 4 times from the default zoom level.
  1901. zoom_max = (1 * Math::pow(zoom_step, 4));
  1902. top_layer = memnew(GraphEditFilter(this));
  1903. add_child(top_layer);
  1904. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1905. top_layer->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1906. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1907. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1908. connections_layer = memnew(Control);
  1909. add_child(connections_layer);
  1910. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1911. connections_layer->set_name("CLAYER");
  1912. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1913. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1914. h_scroll = memnew(HScrollBar);
  1915. h_scroll->set_name("_h_scroll");
  1916. top_layer->add_child(h_scroll);
  1917. v_scroll = memnew(VScrollBar);
  1918. v_scroll->set_name("_v_scroll");
  1919. top_layer->add_child(v_scroll);
  1920. //set large minmax so it can scroll even if not resized yet
  1921. h_scroll->set_min(-10000);
  1922. h_scroll->set_max(10000);
  1923. v_scroll->set_min(-10000);
  1924. v_scroll->set_max(10000);
  1925. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1926. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1927. zoom_hb = memnew(HBoxContainer);
  1928. top_layer->add_child(zoom_hb);
  1929. zoom_hb->set_position(Vector2(10, 10));
  1930. zoom_label = memnew(Label);
  1931. zoom_hb->add_child(zoom_label);
  1932. zoom_label->set_visible(false);
  1933. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1934. zoom_label->set_align(Label::ALIGN_CENTER);
  1935. #ifdef TOOLS_ENABLED
  1936. zoom_label->set_custom_minimum_size(Size2(48, 0) * EDSCALE);
  1937. #else
  1938. zoom_label->set_custom_minimum_size(Size2(48, 0));
  1939. #endif
  1940. _update_zoom_label();
  1941. zoom_minus = memnew(Button);
  1942. zoom_minus->set_flat(true);
  1943. zoom_hb->add_child(zoom_minus);
  1944. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1945. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1946. zoom_minus->set_focus_mode(FOCUS_NONE);
  1947. zoom_reset = memnew(Button);
  1948. zoom_reset->set_flat(true);
  1949. zoom_hb->add_child(zoom_reset);
  1950. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1951. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1952. zoom_reset->set_focus_mode(FOCUS_NONE);
  1953. zoom_plus = memnew(Button);
  1954. zoom_plus->set_flat(true);
  1955. zoom_hb->add_child(zoom_plus);
  1956. zoom_plus->set_tooltip(RTR("Zoom In"));
  1957. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1958. zoom_plus->set_focus_mode(FOCUS_NONE);
  1959. snap_button = memnew(Button);
  1960. snap_button->set_flat(true);
  1961. snap_button->set_toggle_mode(true);
  1962. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1963. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  1964. snap_button->set_pressed(true);
  1965. snap_button->set_focus_mode(FOCUS_NONE);
  1966. zoom_hb->add_child(snap_button);
  1967. snap_amount = memnew(SpinBox);
  1968. snap_amount->set_min(5);
  1969. snap_amount->set_max(100);
  1970. snap_amount->set_step(1);
  1971. snap_amount->set_value(20);
  1972. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  1973. zoom_hb->add_child(snap_amount);
  1974. minimap_button = memnew(Button);
  1975. minimap_button->set_flat(true);
  1976. minimap_button->set_toggle_mode(true);
  1977. minimap_button->set_tooltip(RTR("Enable grid minimap."));
  1978. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  1979. minimap_button->set_pressed(true);
  1980. minimap_button->set_focus_mode(FOCUS_NONE);
  1981. zoom_hb->add_child(minimap_button);
  1982. layout_button = memnew(Button);
  1983. layout_button->set_flat(true);
  1984. zoom_hb->add_child(layout_button);
  1985. layout_button->set_tooltip(RTR("Arrange nodes."));
  1986. layout_button->connect("pressed", callable_mp(this, &GraphEdit::arrange_nodes));
  1987. layout_button->set_focus_mode(FOCUS_NONE);
  1988. Vector2 minimap_size = Vector2(240, 160);
  1989. float minimap_opacity = 0.65;
  1990. minimap = memnew(GraphEditMinimap(this));
  1991. top_layer->add_child(minimap);
  1992. minimap->set_name("_minimap");
  1993. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  1994. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  1995. minimap->set_custom_minimum_size(Vector2(50, 50));
  1996. minimap->set_size(minimap_size);
  1997. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1998. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1999. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  2000. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2001. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2002. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  2003. set_clip_contents(true);
  2004. }