graph_edit.cpp 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  1. /**************************************************************************/
  2. /* graph_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "graph_edit.compat.inc"
  32. #include "core/input/input.h"
  33. #include "core/math/geometry_2d.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/os/keyboard.h"
  36. #include "scene/2d/line_2d.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/button.h"
  39. #include "scene/gui/graph_edit_arranger.h"
  40. #include "scene/gui/label.h"
  41. #include "scene/gui/panel_container.h"
  42. #include "scene/gui/scroll_bar.h"
  43. #include "scene/gui/spin_box.h"
  44. #include "scene/gui/view_panner.h"
  45. #include "scene/resources/style_box_flat.h"
  46. #include "scene/theme/theme_db.h"
  47. constexpr int MINIMAP_OFFSET = 12;
  48. constexpr int MINIMAP_PADDING = 5;
  49. constexpr int MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION = 20;
  50. constexpr int MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES = 5;
  51. constexpr int GRID_MINOR_STEPS_PER_MAJOR_LINE = 10;
  52. constexpr int GRID_MINOR_STEPS_PER_MAJOR_DOT = 5;
  53. constexpr int GRID_MIN_SNAPPING_DISTANCE = 2;
  54. constexpr int GRID_MAX_SNAPPING_DISTANCE = 100;
  55. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  56. return ge->_filter_input(p_point);
  57. }
  58. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  59. ge = p_edit;
  60. }
  61. Control::CursorShape GraphEditMinimap::get_cursor_shape(const Point2 &p_pos) const {
  62. if (is_resizing || (p_pos.x < theme_cache.resizer->get_width() && p_pos.y < theme_cache.resizer->get_height())) {
  63. return CURSOR_FDIAGSIZE;
  64. }
  65. return Control::get_cursor_shape(p_pos);
  66. }
  67. void GraphEditMinimap::update_minimap() {
  68. Vector2 graph_offset = _get_graph_offset();
  69. Vector2 graph_size = _get_graph_size();
  70. camera_position = ge->get_scroll_offset() - graph_offset;
  71. camera_size = ge->get_size();
  72. Vector2 render_size = _get_render_size();
  73. float target_ratio = render_size.width / render_size.height;
  74. float graph_ratio = graph_size.width / graph_size.height;
  75. graph_proportions = graph_size;
  76. graph_padding = Vector2(0, 0);
  77. if (graph_ratio > target_ratio) {
  78. graph_proportions.width = graph_size.width;
  79. graph_proportions.height = graph_size.width / target_ratio;
  80. graph_padding.y = Math::abs(graph_size.height - graph_proportions.y) / 2;
  81. } else {
  82. graph_proportions.width = graph_size.height * target_ratio;
  83. graph_proportions.height = graph_size.height;
  84. graph_padding.x = Math::abs(graph_size.width - graph_proportions.x) / 2;
  85. }
  86. // This centers minimap inside the minimap rectangle.
  87. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  88. }
  89. Rect2 GraphEditMinimap::get_camera_rect() {
  90. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  91. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  92. Vector2 camera_pos = (camera_center - camera_viewport / 2);
  93. return Rect2(camera_pos, camera_viewport);
  94. }
  95. Vector2 GraphEditMinimap::_get_render_size() {
  96. if (!is_inside_tree()) {
  97. return Vector2(0, 0);
  98. }
  99. return get_size() - 2 * minimap_padding;
  100. }
  101. Vector2 GraphEditMinimap::_get_graph_offset() {
  102. return Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  103. }
  104. Vector2 GraphEditMinimap::_get_graph_size() {
  105. Vector2 graph_size = Vector2(ge->h_scrollbar->get_max(), ge->v_scrollbar->get_max()) - Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  106. if (graph_size.width == 0) {
  107. graph_size.width = 1;
  108. }
  109. if (graph_size.height == 0) {
  110. graph_size.height = 1;
  111. }
  112. return graph_size;
  113. }
  114. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  115. Vector2 map_position = Vector2(0, 0);
  116. Vector2 render_size = _get_render_size();
  117. map_position.x = p_position.x * render_size.width / graph_proportions.x;
  118. map_position.y = p_position.y * render_size.height / graph_proportions.y;
  119. return map_position;
  120. }
  121. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  122. Vector2 graph_position = Vector2(0, 0);
  123. Vector2 render_size = _get_render_size();
  124. graph_position.x = p_position.x * graph_proportions.x / render_size.width;
  125. graph_position.y = p_position.y * graph_proportions.y / render_size.height;
  126. return graph_position;
  127. }
  128. void GraphEditMinimap::gui_input(const Ref<InputEvent> &p_ev) {
  129. ERR_FAIL_COND(p_ev.is_null());
  130. if (!ge->is_minimap_enabled()) {
  131. return;
  132. }
  133. Ref<InputEventMouseButton> mb = p_ev;
  134. Ref<InputEventMouseMotion> mm = p_ev;
  135. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  136. if (mb->is_pressed()) {
  137. is_pressing = true;
  138. Rect2 resizer_hitbox = Rect2(Point2(), theme_cache.resizer->get_size());
  139. if (resizer_hitbox.has_point(mb->get_position())) {
  140. is_resizing = true;
  141. } else {
  142. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  143. _adjust_graph_scroll(click_position);
  144. }
  145. } else {
  146. is_pressing = false;
  147. is_resizing = false;
  148. }
  149. accept_event();
  150. } else if (mm.is_valid() && is_pressing) {
  151. if (is_resizing) {
  152. // Prevent setting minimap wider than GraphEdit.
  153. Vector2 new_minimap_size;
  154. new_minimap_size = (get_size() - mm->get_relative()).min(ge->get_size() - 2.0 * minimap_padding);
  155. ge->set_minimap_size(new_minimap_size);
  156. queue_redraw();
  157. } else {
  158. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  159. _adjust_graph_scroll(click_position);
  160. }
  161. accept_event();
  162. }
  163. }
  164. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  165. Vector2 graph_offset = _get_graph_offset();
  166. ge->set_scroll_offset(p_offset + graph_offset - camera_size / 2);
  167. }
  168. void GraphEditMinimap::_bind_methods() {
  169. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, panel);
  170. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, node_style, "node");
  171. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, camera_style, "camera");
  172. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEditMinimap, resizer);
  173. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEditMinimap, resizer_color);
  174. }
  175. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  176. ge = p_edit;
  177. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  178. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  179. }
  180. Ref<Shader> GraphEdit::default_connections_shader;
  181. void GraphEdit::init_shaders() {
  182. default_connections_shader.instantiate();
  183. default_connections_shader->set_code(R"(
  184. // Connection lines shader.
  185. shader_type canvas_item;
  186. render_mode blend_mix;
  187. uniform vec4 rim_color : source_color;
  188. uniform int from_type;
  189. uniform int to_type;
  190. uniform float line_width;
  191. void fragment(){
  192. float fake_aa_width = 1.5/line_width;
  193. float rim_width = 1.5/line_width;
  194. float dist = abs(UV.y - 0.5);
  195. float alpha = smoothstep(0.5, 0.5-fake_aa_width, dist);
  196. vec4 final_color = mix(rim_color, COLOR, smoothstep(0.5-rim_width, 0.5-fake_aa_width-rim_width, dist));
  197. COLOR = vec4(final_color.rgb, final_color.a*alpha);
  198. }
  199. )");
  200. }
  201. void GraphEdit::finish_shaders() {
  202. default_connections_shader.unref();
  203. }
  204. Control::CursorShape GraphEdit::get_cursor_shape(const Point2 &p_pos) const {
  205. if (moving_selection) {
  206. return CURSOR_MOVE;
  207. }
  208. return Control::get_cursor_shape(p_pos);
  209. }
  210. PackedStringArray GraphEdit::get_configuration_warnings() const {
  211. PackedStringArray warnings = Control::get_configuration_warnings();
  212. warnings.push_back(RTR("Please be aware that GraphEdit and GraphNode will undergo extensive refactoring in a future 4.x version involving compatibility-breaking API changes."));
  213. return warnings;
  214. }
  215. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  216. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  217. return OK;
  218. }
  219. Ref<Connection> c;
  220. c.instantiate();
  221. c->from_node = p_from;
  222. c->from_port = p_from_port;
  223. c->to_node = p_to;
  224. c->to_port = p_to_port;
  225. c->activity = 0;
  226. connections.push_back(c);
  227. connection_map[p_from].push_back(c);
  228. connection_map[p_to].push_back(c);
  229. Line2D *line = memnew(Line2D);
  230. line->set_texture_mode(Line2D::LineTextureMode::LINE_TEXTURE_STRETCH);
  231. Ref<ShaderMaterial> line_material;
  232. line_material.instantiate();
  233. line_material->set_shader(connections_shader);
  234. float line_width = _get_shader_line_width();
  235. line_material->set_shader_parameter("line_width", line_width);
  236. line_material->set_shader_parameter("from_type", c->from_port);
  237. line_material->set_shader_parameter("to_type", c->to_port);
  238. Ref<StyleBoxFlat> bg_panel = theme_cache.panel;
  239. Color connection_line_rim_color = bg_panel.is_valid() ? bg_panel->get_bg_color() : Color(0.0, 0.0, 0.0, 0.0);
  240. line_material->set_shader_parameter("rim_color", connection_line_rim_color);
  241. line->set_material(line_material);
  242. connections_layer->add_child(line);
  243. c->_cache.line = line;
  244. minimap->queue_redraw();
  245. queue_redraw();
  246. connections_layer->queue_redraw();
  247. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  248. return OK;
  249. }
  250. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  251. for (const Ref<Connection> &conn : connection_map[p_from]) {
  252. if (conn->from_node == p_from && conn->from_port == p_from_port && conn->to_node == p_to && conn->to_port == p_to_port) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  259. for (const List<Ref<Connection>>::Element *E = connections.front(); E; E = E->next()) {
  260. if (E->get()->from_node == p_from && E->get()->from_port == p_from_port && E->get()->to_node == p_to && E->get()->to_port == p_to_port) {
  261. connection_map[p_from].erase(E->get());
  262. connection_map[p_to].erase(E->get());
  263. E->get()->_cache.line->queue_free();
  264. connections.erase(E);
  265. minimap->queue_redraw();
  266. queue_redraw();
  267. connections_layer->queue_redraw();
  268. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  269. return;
  270. }
  271. }
  272. }
  273. const List<Ref<GraphEdit::Connection>> &GraphEdit::get_connection_list() const {
  274. return connections;
  275. }
  276. void GraphEdit::set_scroll_offset(const Vector2 &p_offset) {
  277. setting_scroll_offset = true;
  278. h_scrollbar->set_value(p_offset.x);
  279. v_scrollbar->set_value(p_offset.y);
  280. _update_scroll();
  281. setting_scroll_offset = false;
  282. }
  283. Vector2 GraphEdit::get_scroll_offset() const {
  284. return Vector2(h_scrollbar->get_value(), v_scrollbar->get_value());
  285. }
  286. void GraphEdit::_scroll_moved(double) {
  287. if (!awaiting_scroll_offset_update) {
  288. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  289. awaiting_scroll_offset_update = true;
  290. }
  291. minimap->queue_redraw();
  292. queue_redraw();
  293. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  294. }
  295. void GraphEdit::_update_scroll_offset() {
  296. set_block_minimum_size_adjust(true);
  297. for (int i = 0; i < get_child_count(); i++) {
  298. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  299. if (!graph_element) {
  300. continue;
  301. }
  302. Point2 pos = graph_element->get_position_offset() * zoom;
  303. pos -= Point2(h_scrollbar->get_value(), v_scrollbar->get_value());
  304. graph_element->set_position(pos);
  305. if (graph_element->get_scale() != Vector2(zoom, zoom)) {
  306. graph_element->set_scale(Vector2(zoom, zoom));
  307. }
  308. }
  309. connections_layer->set_position(-Point2(h_scrollbar->get_value(), v_scrollbar->get_value()));
  310. set_block_minimum_size_adjust(false);
  311. awaiting_scroll_offset_update = false;
  312. // In Godot, signals on value change are avoided by convention.
  313. if (!setting_scroll_offset) {
  314. emit_signal(SNAME("scroll_offset_changed"), get_scroll_offset());
  315. }
  316. }
  317. void GraphEdit::_update_scroll() {
  318. if (updating) {
  319. return;
  320. }
  321. updating = true;
  322. set_block_minimum_size_adjust(true);
  323. Rect2 screen_rect;
  324. for (int i = 0; i < get_child_count(); i++) {
  325. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  326. if (!graph_element) {
  327. continue;
  328. }
  329. Rect2 node_rect;
  330. node_rect.position = graph_element->get_position_offset() * zoom;
  331. node_rect.size = graph_element->get_size() * zoom;
  332. screen_rect = screen_rect.merge(node_rect);
  333. }
  334. screen_rect.position -= get_size();
  335. screen_rect.size += get_size() * 2.0;
  336. h_scrollbar->set_min(screen_rect.position.x);
  337. h_scrollbar->set_max(screen_rect.position.x + screen_rect.size.width);
  338. h_scrollbar->set_page(get_size().x);
  339. if (h_scrollbar->get_max() - h_scrollbar->get_min() <= h_scrollbar->get_page()) {
  340. h_scrollbar->hide();
  341. } else {
  342. h_scrollbar->show();
  343. }
  344. v_scrollbar->set_min(screen_rect.position.y);
  345. v_scrollbar->set_max(screen_rect.position.y + screen_rect.size.height);
  346. v_scrollbar->set_page(get_size().height);
  347. if (v_scrollbar->get_max() - v_scrollbar->get_min() <= v_scrollbar->get_page()) {
  348. v_scrollbar->hide();
  349. } else {
  350. v_scrollbar->show();
  351. }
  352. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  353. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  354. // Avoid scrollbar overlapping.
  355. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scrollbar->is_visible() ? -vmin.width : 0);
  356. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scrollbar->is_visible() ? -hmin.height : 0);
  357. set_block_minimum_size_adjust(false);
  358. if (!awaiting_scroll_offset_update) {
  359. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  360. awaiting_scroll_offset_update = true;
  361. }
  362. updating = false;
  363. }
  364. void GraphEdit::_ensure_node_order_from(Node *p_node) {
  365. GraphElement *graph_node = Object::cast_to<GraphElement>(p_node);
  366. ERR_FAIL_NULL(graph_node);
  367. GraphFrame *frame = Object::cast_to<GraphFrame>(p_node);
  368. // Move a non-frame node directly to the front.
  369. if (!frame) {
  370. graph_node->move_to_front();
  371. return;
  372. }
  373. // Reorder the frames behind the connection layer.
  374. List<GraphFrame *> attached_nodes_to_move;
  375. attached_nodes_to_move.push_back(frame);
  376. while (!attached_nodes_to_move.is_empty()) {
  377. GraphFrame *attached_frame = attached_nodes_to_move.front()->get();
  378. attached_nodes_to_move.pop_front();
  379. // Move the frame to the front of the background node index range.
  380. attached_frame->get_parent()->call_deferred("move_child", attached_frame, background_nodes_separator_idx - 1);
  381. if (!frame_attached_nodes.has(attached_frame->get_name())) {
  382. continue;
  383. }
  384. for (const StringName &attached_node_name : frame_attached_nodes.get(attached_frame->get_name())) {
  385. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  386. GraphFrame *attached_child_frame_node = Object::cast_to<GraphFrame>(attached_node);
  387. if (attached_child_frame_node && (attached_child_frame_node != frame)) {
  388. attached_nodes_to_move.push_back(attached_child_frame_node);
  389. }
  390. }
  391. }
  392. }
  393. void GraphEdit::_graph_element_selected(Node *p_node) {
  394. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  395. ERR_FAIL_NULL(graph_element);
  396. emit_signal(SNAME("node_selected"), graph_element);
  397. }
  398. void GraphEdit::_graph_element_deselected(Node *p_node) {
  399. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  400. ERR_FAIL_NULL(graph_element);
  401. emit_signal(SNAME("node_deselected"), graph_element);
  402. }
  403. void GraphEdit::_graph_element_resize_request(const Vector2 &p_new_minsize, Node *p_node) {
  404. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  405. ERR_FAIL_NULL(graph_element);
  406. // Snap the new size to the grid if snapping is enabled.
  407. Vector2 new_size = p_new_minsize;
  408. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  409. new_size = new_size.snappedf(snapping_distance);
  410. }
  411. // Disallow resizing the frame to a size smaller than the minimum size of the attached nodes.
  412. GraphFrame *frame = Object::cast_to<GraphFrame>(graph_element);
  413. if (frame && !frame->is_autoshrink_enabled()) {
  414. Rect2 frame_rect = _compute_shrinked_frame_rect(frame);
  415. Vector2 computed_min_size = (frame_rect.position + frame_rect.size) - frame->get_position_offset();
  416. frame->set_size(new_size.max(computed_min_size));
  417. } else {
  418. graph_element->set_size(new_size);
  419. }
  420. // Update all parent frames recursively bottom-up.
  421. if (linked_parent_map.has(graph_element->get_name())) {
  422. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[graph_element->get_name()])));
  423. if (parent_frame) {
  424. _update_graph_frame(parent_frame);
  425. }
  426. }
  427. }
  428. void GraphEdit::_graph_frame_autoshrink_changed(const Vector2 &p_new_minsize, GraphFrame *p_frame) {
  429. _update_graph_frame(p_frame);
  430. minimap->queue_redraw();
  431. queue_redraw();
  432. connections_layer->queue_redraw();
  433. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  434. }
  435. void GraphEdit::_graph_element_moved(Node *p_node) {
  436. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  437. ERR_FAIL_NULL(graph_element);
  438. minimap->queue_redraw();
  439. queue_redraw();
  440. connections_layer->queue_redraw();
  441. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  442. }
  443. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) {
  444. GraphNode *graph_node = Object::cast_to<GraphNode>(p_node);
  445. ERR_FAIL_NULL(graph_node);
  446. minimap->queue_redraw();
  447. queue_redraw();
  448. connections_layer->queue_redraw();
  449. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  450. }
  451. void GraphEdit::_graph_node_rect_changed(GraphNode *p_node) {
  452. // Only invalidate the cache when zooming or the node is moved/resized in graph space.
  453. if (panner->is_panning()) {
  454. return;
  455. }
  456. for (Ref<Connection> &c : connection_map[p_node->get_name()]) {
  457. c->_cache.dirty = true;
  458. }
  459. connections_layer->queue_redraw();
  460. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  461. // Update all parent frames recursively bottom-up.
  462. if (linked_parent_map.has(p_node->get_name())) {
  463. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[p_node->get_name()])));
  464. if (parent_frame) {
  465. _update_graph_frame(parent_frame);
  466. }
  467. }
  468. }
  469. void GraphEdit::_ensure_node_order_from_root(const StringName &p_node) {
  470. // Find the root frame node of the frame tree starting from p_node.
  471. GraphElement *root_frame = Object::cast_to<GraphElement>(get_node(NodePath(p_node)));
  472. ERR_FAIL_NULL(root_frame);
  473. while (linked_parent_map.has(root_frame->get_name())) {
  474. root_frame = Object::cast_to<GraphElement>(get_node(NodePath(linked_parent_map[root_frame->get_name()])));
  475. }
  476. _ensure_node_order_from(root_frame);
  477. }
  478. void GraphEdit::add_child_notify(Node *p_child) {
  479. Control::add_child_notify(p_child);
  480. // Keep the top layer always on top!
  481. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  482. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  483. if (graph_element) {
  484. graph_element->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved).bind(graph_element));
  485. graph_element->connect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected).bind(graph_element));
  486. graph_element->connect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected).bind(graph_element));
  487. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  488. if (graph_node) {
  489. graph_node->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(graph_element));
  490. graph_node->connect(SceneStringName(item_rect_changed), callable_mp(this, &GraphEdit::_graph_node_rect_changed).bind(graph_node));
  491. _ensure_node_order_from(graph_node);
  492. }
  493. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(graph_element);
  494. if (graph_frame) {
  495. background_nodes_separator_idx++;
  496. callable_mp((Node *)this, &Node::move_child).call_deferred(graph_frame, 0);
  497. callable_mp((Node *)this, &Node::move_child).call_deferred(connections_layer, background_nodes_separator_idx);
  498. _update_graph_frame(graph_frame);
  499. graph_frame->connect("autoshrink_changed", callable_mp(this, &GraphEdit::_graph_frame_autoshrink_changed).bind(graph_element));
  500. }
  501. graph_element->connect("raise_request", callable_mp(this, &GraphEdit::_ensure_node_order_from).bind(graph_element));
  502. graph_element->connect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resize_request).bind(graph_element));
  503. graph_element->connect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  504. graph_element->connect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  505. graph_element->set_scale(Vector2(zoom, zoom));
  506. _graph_element_moved(graph_element);
  507. graph_element->set_mouse_filter(MOUSE_FILTER_PASS);
  508. }
  509. }
  510. void GraphEdit::remove_child_notify(Node *p_child) {
  511. Control::remove_child_notify(p_child);
  512. if (p_child == top_layer) {
  513. top_layer = nullptr;
  514. minimap = nullptr;
  515. } else if (p_child == connections_layer) {
  516. connections_layer = nullptr;
  517. }
  518. if (top_layer != nullptr && is_inside_tree()) {
  519. // Keep the top layer always on top!
  520. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  521. }
  522. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  523. if (graph_element) {
  524. graph_element->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved));
  525. graph_element->disconnect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected));
  526. graph_element->disconnect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected));
  527. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  528. if (graph_node) {
  529. graph_node->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  530. graph_node->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &GraphEdit::_graph_node_rect_changed));
  531. // Invalidate all adjacent connections, so that they are removed before the next redraw.
  532. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  533. conn->_cache.dirty = true;
  534. }
  535. connections_layer->queue_redraw();
  536. }
  537. GraphFrame *frame = Object::cast_to<GraphFrame>(graph_element);
  538. if (frame) {
  539. background_nodes_separator_idx--;
  540. graph_element->disconnect("autoshrink_changed", callable_mp(this, &GraphEdit::_graph_frame_autoshrink_changed));
  541. }
  542. if (linked_parent_map.has(graph_element->get_name())) {
  543. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[graph_element->get_name()])));
  544. if (parent_frame) {
  545. if (frame_attached_nodes.has(parent_frame->get_name())) {
  546. frame_attached_nodes.get(parent_frame->get_name()).erase(graph_element->get_name());
  547. }
  548. linked_parent_map.erase(graph_element->get_name());
  549. _update_graph_frame(parent_frame);
  550. }
  551. }
  552. if (frame_attached_nodes.has(graph_element->get_name())) {
  553. for (const StringName &attached_node_name : frame_attached_nodes.get(graph_element->get_name())) {
  554. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  555. if (attached_node) {
  556. linked_parent_map.erase(attached_node->get_name());
  557. }
  558. }
  559. frame_attached_nodes.erase(graph_element->get_name());
  560. }
  561. graph_element->disconnect("raise_request", callable_mp(this, &GraphEdit::_ensure_node_order_from));
  562. graph_element->disconnect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resize_request));
  563. // In case of the whole GraphEdit being destroyed these references can already be freed.
  564. if (minimap != nullptr && minimap->is_inside_tree()) {
  565. graph_element->disconnect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  566. }
  567. }
  568. }
  569. void GraphEdit::_update_theme_item_cache() {
  570. Control::_update_theme_item_cache();
  571. theme_cache.base_scale = get_theme_default_base_scale();
  572. }
  573. void GraphEdit::_notification(int p_what) {
  574. switch (p_what) {
  575. case NOTIFICATION_THEME_CHANGED: {
  576. zoom_minus_button->set_icon(theme_cache.zoom_out);
  577. zoom_reset_button->set_icon(theme_cache.zoom_reset);
  578. zoom_plus_button->set_icon(theme_cache.zoom_in);
  579. toggle_snapping_button->set_icon(theme_cache.snapping_toggle);
  580. toggle_grid_button->set_icon(theme_cache.grid_toggle);
  581. minimap_button->set_icon(theme_cache.minimap_toggle);
  582. arrange_button->set_icon(theme_cache.layout);
  583. zoom_label->set_custom_minimum_size(Size2(48, 0) * theme_cache.base_scale);
  584. menu_panel->add_theme_style_override("panel", theme_cache.menu_panel);
  585. } break;
  586. case NOTIFICATION_READY: {
  587. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  588. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  589. h_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  590. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  591. h_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  592. h_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  593. v_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  594. v_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  595. v_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  596. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  597. } break;
  598. case NOTIFICATION_DRAW: {
  599. // Draw background fill.
  600. draw_style_box(theme_cache.panel, Rect2(Point2(), get_size()));
  601. // Draw background grid.
  602. if (show_grid) {
  603. _draw_grid();
  604. }
  605. } break;
  606. case NOTIFICATION_RESIZED: {
  607. _update_scroll();
  608. minimap->queue_redraw();
  609. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  610. } break;
  611. }
  612. }
  613. Rect2 GraphEdit::_compute_shrinked_frame_rect(const GraphFrame *p_frame) {
  614. Vector2 min_point{ FLT_MAX, FLT_MAX };
  615. Vector2 max_point{ -FLT_MAX, -FLT_MAX };
  616. if (!frame_attached_nodes.has(p_frame->get_name())) {
  617. return Rect2(p_frame->get_position_offset(), Size2());
  618. }
  619. int autoshrink_margin = p_frame->get_autoshrink_margin();
  620. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  621. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node_or_null(NodePath(attached_node_name)));
  622. if (!attached_node || attached_node == p_frame) {
  623. if (!attached_node) {
  624. frame_attached_nodes.get(p_frame->get_name()).erase(attached_node_name);
  625. }
  626. continue;
  627. }
  628. Vector2 node_pos = attached_node->get_position_offset();
  629. Vector2 size = attached_node->get_size();
  630. min_point = min_point.min(node_pos);
  631. max_point = max_point.max(node_pos + size);
  632. }
  633. // It's sufficient to check only one value here.
  634. if (min_point.x == FLT_MAX) {
  635. return Rect2(p_frame->get_position_offset(), Size2());
  636. }
  637. min_point -= Size2(autoshrink_margin, autoshrink_margin);
  638. max_point += Size2(autoshrink_margin, autoshrink_margin);
  639. return Rect2(min_point, max_point - min_point);
  640. }
  641. void GraphEdit::_update_graph_frame(GraphFrame *p_frame) {
  642. Rect2 frame_rect = _compute_shrinked_frame_rect(p_frame);
  643. Vector2 min_point = frame_rect.position;
  644. Vector2 max_point = frame_rect.position + frame_rect.size;
  645. // Only update the size if there are attached nodes.
  646. if (frame_attached_nodes.has(p_frame->get_name()) && frame_attached_nodes.get(p_frame->get_name()).size() > 0) {
  647. if (!p_frame->is_autoshrink_enabled()) {
  648. Vector2 old_offset = p_frame->get_position_offset();
  649. min_point = min_point.min(old_offset);
  650. max_point = max_point.max(old_offset + p_frame->get_size());
  651. }
  652. Rect2 old_rect = p_frame->get_rect();
  653. p_frame->set_position_offset(min_point);
  654. p_frame->set_size(max_point - min_point);
  655. // Emit the signal only if the frame rect has changed.
  656. if (old_rect != p_frame->get_rect()) {
  657. emit_signal(SNAME("frame_rect_changed"), p_frame, p_frame->get_rect());
  658. }
  659. }
  660. // Update all parent frames recursively bottom-up.
  661. if (linked_parent_map.has(p_frame->get_name())) {
  662. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[p_frame->get_name()])));
  663. if (parent_frame) {
  664. _update_graph_frame(parent_frame);
  665. }
  666. }
  667. }
  668. void GraphEdit::_set_drag_frame_attached_nodes(GraphFrame *p_frame, bool p_drag) {
  669. if (!frame_attached_nodes.has(p_frame->get_name())) {
  670. return;
  671. }
  672. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  673. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  674. attached_node->set_drag(p_drag);
  675. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(attached_node);
  676. if (graph_frame) {
  677. _set_drag_frame_attached_nodes(graph_frame, p_drag);
  678. }
  679. }
  680. }
  681. void GraphEdit::_set_position_of_frame_attached_nodes(GraphFrame *p_frame, const Vector2 &p_pos) {
  682. if (!frame_attached_nodes.has(p_frame->get_name())) {
  683. return;
  684. }
  685. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  686. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node_or_null(NodePath(attached_node_name)));
  687. if (!attached_node) {
  688. continue;
  689. }
  690. Vector2 pos = (attached_node->get_drag_from() * zoom + drag_accum) / zoom;
  691. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  692. pos = pos.snappedf(snapping_distance);
  693. }
  694. // Recursively move graph frames.
  695. attached_node->set_position_offset(pos);
  696. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(attached_node);
  697. if (graph_frame) {
  698. _set_position_of_frame_attached_nodes(graph_frame, p_pos);
  699. }
  700. }
  701. }
  702. bool GraphEdit::_filter_input(const Point2 &p_point) {
  703. for (int i = get_child_count() - 1; i >= 0; i--) {
  704. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  705. if (!graph_node || !graph_node->is_visible_in_tree()) {
  706. continue;
  707. }
  708. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  709. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  710. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  711. // Determine slot height.
  712. int slot_index = graph_node->get_input_port_slot(j);
  713. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  714. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  715. if (is_in_input_hotzone(graph_node, j, p_point / zoom, port_size)) {
  716. return true;
  717. }
  718. }
  719. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  720. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  721. // Determine slot height.
  722. int slot_index = graph_node->get_output_port_slot(j);
  723. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  724. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  725. if (is_in_output_hotzone(graph_node, j, p_point / zoom, port_size)) {
  726. return true;
  727. }
  728. }
  729. // This prevents interactions with a port hotzone that is behind another node.
  730. Rect2 graph_node_rect = Rect2(graph_node->get_position(), graph_node->get_size() * zoom);
  731. if (graph_node_rect.has_point(click_pos * zoom)) {
  732. break;
  733. }
  734. }
  735. return false;
  736. }
  737. void GraphEdit::_top_connection_layer_input(const Ref<InputEvent> &p_ev) {
  738. Ref<InputEventMouseButton> mb = p_ev;
  739. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  740. connecting_valid = false;
  741. click_pos = mb->get_position() / zoom;
  742. for (int i = get_child_count() - 1; i >= 0; i--) {
  743. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  744. if (!graph_node || !graph_node->is_visible_in_tree()) {
  745. continue;
  746. }
  747. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  748. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  749. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  750. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  751. // Determine slot height.
  752. int slot_index = graph_node->get_output_port_slot(j);
  753. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  754. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  755. if (is_in_output_hotzone(graph_node, j, click_pos, port_size)) {
  756. if (valid_left_disconnect_types.has(graph_node->get_output_port_type(j))) {
  757. // Check disconnect.
  758. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  759. if (conn->from_node == graph_node->get_name() && conn->from_port == j) {
  760. Node *to = get_node(NodePath(conn->to_node));
  761. if (Object::cast_to<GraphNode>(to)) {
  762. connecting_from_node = conn->to_node;
  763. connecting_from_port_index = conn->to_port;
  764. connecting_from_output = false;
  765. connecting_type = Object::cast_to<GraphNode>(to)->get_input_port_type(conn->to_port);
  766. connecting_color = Object::cast_to<GraphNode>(to)->get_input_port_color(conn->to_port);
  767. connecting_target_valid = false;
  768. connecting_to_point = pos;
  769. if (connecting_type >= 0) {
  770. just_disconnected = true;
  771. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  772. to = get_node(NodePath(connecting_from_node)); // Maybe it was erased.
  773. if (Object::cast_to<GraphNode>(to)) {
  774. connecting = true;
  775. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  776. }
  777. }
  778. return;
  779. }
  780. }
  781. }
  782. }
  783. connecting_from_node = graph_node->get_name();
  784. connecting_from_port_index = j;
  785. connecting_from_output = true;
  786. connecting_type = graph_node->get_output_port_type(j);
  787. connecting_color = graph_node->get_output_port_color(j);
  788. connecting_target_valid = false;
  789. connecting_to_point = pos;
  790. if (connecting_type >= 0) {
  791. connecting = true;
  792. just_disconnected = false;
  793. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  794. }
  795. return;
  796. }
  797. }
  798. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  799. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  800. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  801. // Determine slot height.
  802. int slot_index = graph_node->get_input_port_slot(j);
  803. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  804. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  805. if (is_in_input_hotzone(graph_node, j, click_pos, port_size)) {
  806. if (right_disconnects || valid_right_disconnect_types.has(graph_node->get_input_port_type(j))) {
  807. // Check disconnect.
  808. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  809. if (conn->to_node == graph_node->get_name() && conn->to_port == j) {
  810. Node *fr = get_node(NodePath(conn->from_node));
  811. if (Object::cast_to<GraphNode>(fr)) {
  812. connecting_from_node = conn->from_node;
  813. connecting_from_port_index = conn->from_port;
  814. connecting_from_output = true;
  815. connecting_type = Object::cast_to<GraphNode>(fr)->get_output_port_type(conn->from_port);
  816. connecting_color = Object::cast_to<GraphNode>(fr)->get_output_port_color(conn->from_port);
  817. connecting_target_valid = false;
  818. connecting_to_point = pos;
  819. just_disconnected = true;
  820. if (connecting_type >= 0) {
  821. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  822. fr = get_node(NodePath(connecting_from_node));
  823. if (Object::cast_to<GraphNode>(fr)) {
  824. connecting = true;
  825. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  826. }
  827. }
  828. return;
  829. }
  830. }
  831. }
  832. }
  833. connecting_from_node = graph_node->get_name();
  834. connecting_from_port_index = j;
  835. connecting_from_output = false;
  836. connecting_type = graph_node->get_input_port_type(j);
  837. connecting_color = graph_node->get_input_port_color(j);
  838. connecting_target_valid = false;
  839. connecting_to_point = pos;
  840. if (connecting_type >= 0) {
  841. connecting = true;
  842. just_disconnected = false;
  843. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  844. }
  845. return;
  846. }
  847. }
  848. // This prevents interactions with a port hotzone that is behind another node.
  849. Rect2 graph_node_rect = Rect2(graph_node->get_position(), graph_node->get_size() * zoom);
  850. if (graph_node_rect.has_point(click_pos * zoom)) {
  851. break;
  852. }
  853. }
  854. }
  855. Ref<InputEventMouseMotion> mm = p_ev;
  856. if (mm.is_valid() && connecting) {
  857. connecting_to_point = mm->get_position();
  858. minimap->queue_redraw();
  859. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  860. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to_point / zoom) > MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION;
  861. if (connecting_valid) {
  862. Vector2 mpos = mm->get_position() / zoom;
  863. for (int i = get_child_count() - 1; i >= 0; i--) {
  864. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  865. if (!graph_node || !graph_node->is_visible_in_tree()) {
  866. continue;
  867. }
  868. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  869. if (!connecting_from_output) {
  870. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  871. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  872. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  873. // Determine slot height.
  874. int slot_index = graph_node->get_output_port_slot(j);
  875. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  876. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  877. int type = graph_node->get_output_port_type(j);
  878. if ((type == connecting_type || graph_node->is_ignoring_valid_connection_type() ||
  879. valid_connection_types.has(ConnectionType(type, connecting_type))) &&
  880. is_in_output_hotzone(graph_node, j, mpos, port_size)) {
  881. if (!is_node_hover_valid(graph_node->get_name(), j, connecting_from_node, connecting_from_port_index)) {
  882. continue;
  883. }
  884. connecting_target_valid = true;
  885. connecting_to_point = pos;
  886. connecting_target_node = graph_node->get_name();
  887. connecting_target_port_index = j;
  888. return;
  889. }
  890. }
  891. connecting_target_valid = false;
  892. } else {
  893. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  894. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  895. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  896. // Determine slot height.
  897. int slot_index = graph_node->get_input_port_slot(j);
  898. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  899. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  900. int type = graph_node->get_input_port_type(j);
  901. if ((type == connecting_type || graph_node->is_ignoring_valid_connection_type() || valid_connection_types.has(ConnectionType(connecting_type, type))) &&
  902. is_in_input_hotzone(graph_node, j, mpos, port_size)) {
  903. if (!is_node_hover_valid(connecting_from_node, connecting_from_port_index, graph_node->get_name(), j)) {
  904. continue;
  905. }
  906. connecting_target_valid = true;
  907. connecting_to_point = pos;
  908. connecting_target_node = graph_node->get_name();
  909. connecting_target_port_index = j;
  910. return;
  911. }
  912. }
  913. connecting_target_valid = false;
  914. }
  915. }
  916. }
  917. }
  918. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  919. if (connecting_valid) {
  920. if (connecting && connecting_target_valid) {
  921. if (connecting_from_output) {
  922. emit_signal(SNAME("connection_request"), connecting_from_node, connecting_from_port_index, connecting_target_node, connecting_target_port_index);
  923. } else {
  924. emit_signal(SNAME("connection_request"), connecting_target_node, connecting_target_port_index, connecting_from_node, connecting_from_port_index);
  925. }
  926. } else if (!just_disconnected) {
  927. if (connecting_from_output) {
  928. emit_signal(SNAME("connection_to_empty"), connecting_from_node, connecting_from_port_index, mb->get_position());
  929. } else {
  930. emit_signal(SNAME("connection_from_empty"), connecting_from_node, connecting_from_port_index, mb->get_position());
  931. }
  932. }
  933. } else {
  934. set_selected(get_node_or_null(NodePath(connecting_from_node)));
  935. }
  936. if (connecting) {
  937. force_connection_drag_end();
  938. }
  939. }
  940. }
  941. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) {
  942. if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) {
  943. return false;
  944. }
  945. Rect2 control_rect = p_control->get_rect();
  946. control_rect.position *= zoom;
  947. control_rect.size *= zoom;
  948. control_rect.position += p_offset;
  949. if (!control_rect.has_point(mpos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  950. // Test children.
  951. for (int i = 0; i < p_control->get_child_count(); i++) {
  952. Control *child_rect = Object::cast_to<Control>(p_control->get_child(i));
  953. if (!child_rect) {
  954. continue;
  955. }
  956. if (_check_clickable_control(child_rect, mpos, control_rect.position)) {
  957. return true;
  958. }
  959. }
  960. return false;
  961. } else {
  962. return true;
  963. }
  964. }
  965. bool GraphEdit::is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  966. bool success;
  967. if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  968. return success;
  969. } else {
  970. Vector2 pos = p_graph_node->get_input_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  971. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true);
  972. }
  973. }
  974. bool GraphEdit::is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  975. if (p_graph_node->is_resizable()) {
  976. Ref<Texture2D> resizer = p_graph_node->theme_cache.resizer;
  977. Rect2 resizer_rect = Rect2(p_graph_node->get_position() / zoom + p_graph_node->get_size() - resizer->get_size(), resizer->get_size());
  978. if (resizer_rect.has_point(p_mouse_pos)) {
  979. return false;
  980. }
  981. }
  982. bool success;
  983. if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  984. return success;
  985. } else {
  986. Vector2 pos = p_graph_node->get_output_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  987. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false);
  988. }
  989. }
  990. bool GraphEdit::is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  991. Rect2 hotzone = Rect2(
  992. p_pos.x - (p_left ? theme_cache.port_hotzone_outer_extent : theme_cache.port_hotzone_inner_extent),
  993. p_pos.y - p_port_size.height / 2.0,
  994. theme_cache.port_hotzone_inner_extent + theme_cache.port_hotzone_outer_extent,
  995. p_port_size.height);
  996. if (!hotzone.has_point(p_mouse_pos)) {
  997. return false;
  998. }
  999. for (int i = 0; i < get_child_count(); i++) {
  1000. GraphNode *child = Object::cast_to<GraphNode>(get_child(i));
  1001. if (!child) {
  1002. continue;
  1003. }
  1004. Rect2 child_rect = child->get_rect();
  1005. if (child_rect.has_point(p_mouse_pos * zoom)) {
  1006. for (int j = 0; j < child->get_child_count(); j++) {
  1007. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  1008. if (!subchild) {
  1009. continue;
  1010. }
  1011. if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) {
  1012. return false;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. return true;
  1018. }
  1019. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) const {
  1020. Vector<Vector2> ret;
  1021. if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) {
  1022. return ret;
  1023. }
  1024. float x_diff = (p_to.x - p_from.x);
  1025. float cp_offset = x_diff * lines_curvature;
  1026. if (x_diff < 0) {
  1027. cp_offset *= -1;
  1028. }
  1029. Curve2D curve;
  1030. curve.add_point(p_from);
  1031. curve.set_point_out(0, Vector2(cp_offset, 0));
  1032. curve.add_point(p_to);
  1033. curve.set_point_in(1, Vector2(-cp_offset, 0));
  1034. if (lines_curvature > 0) {
  1035. return curve.tessellate(MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES, 2.0);
  1036. } else {
  1037. return curve.tessellate(1);
  1038. }
  1039. }
  1040. Ref<GraphEdit::Connection> GraphEdit::get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance) const {
  1041. Vector2 transformed_point = p_point + get_scroll_offset();
  1042. Ref<GraphEdit::Connection> closest_connection;
  1043. float closest_distance = p_max_distance;
  1044. for (const Ref<Connection> &c : connections) {
  1045. if (c->_cache.aabb.distance_to(transformed_point) > p_max_distance) {
  1046. continue;
  1047. }
  1048. Vector<Vector2> points = get_connection_line(c->_cache.from_pos * zoom, c->_cache.to_pos * zoom);
  1049. for (int i = 0; i < points.size() - 1; i++) {
  1050. float distance = Geometry2D::get_distance_to_segment(transformed_point, &points[i]);
  1051. if (distance <= lines_thickness * 0.5 + p_max_distance && distance < closest_distance) {
  1052. closest_connection = c;
  1053. closest_distance = distance;
  1054. }
  1055. }
  1056. }
  1057. return closest_connection;
  1058. }
  1059. List<Ref<GraphEdit::Connection>> GraphEdit::get_connections_intersecting_with_rect(const Rect2 &p_rect) const {
  1060. Rect2 transformed_rect = p_rect;
  1061. transformed_rect.position += get_scroll_offset();
  1062. List<Ref<Connection>> intersecting_connections;
  1063. for (const Ref<Connection> &c : connections) {
  1064. if (!c->_cache.aabb.intersects(transformed_rect)) {
  1065. continue;
  1066. }
  1067. Vector<Vector2> points = get_connection_line(c->_cache.from_pos * zoom, c->_cache.to_pos * zoom);
  1068. for (int i = 0; i < points.size() - 1; i++) {
  1069. if (Geometry2D::segment_intersects_rect(points[i], points[i + 1], transformed_rect)) {
  1070. intersecting_connections.push_back(c);
  1071. break;
  1072. }
  1073. }
  1074. }
  1075. return intersecting_connections;
  1076. }
  1077. void GraphEdit::_draw_minimap_connection_line(const Vector2 &p_from_graph_position, const Vector2 &p_to_graph_position, const Color &p_from_color, const Color &p_to_color) {
  1078. Vector<Vector2> points = get_connection_line(p_from_graph_position, p_to_graph_position);
  1079. ERR_FAIL_COND_MSG(points.size() < 2, "\"_get_connection_line()\" returned an invalid line.");
  1080. // Convert to minimap points.
  1081. for (Vector2 &point : points) {
  1082. point = minimap->_convert_from_graph_position(point) + minimap->minimap_offset;
  1083. }
  1084. // Setup polyline colors.
  1085. LocalVector<Color> colors;
  1086. colors.reserve(points.size());
  1087. const Vector2 &from = points[0];
  1088. const Vector2 &to = points[points.size() - 1];
  1089. float length_inv = 1.0 / (from).distance_to(to);
  1090. for (const Vector2 &point : points) {
  1091. float normalized_curve_position = from.distance_to(point) * length_inv;
  1092. colors.push_back(p_from_color.lerp(p_to_color, normalized_curve_position));
  1093. }
  1094. minimap->draw_polyline_colors(points, colors, 0.5, lines_antialiased);
  1095. }
  1096. void GraphEdit::_update_connections() {
  1097. // Collect all dead connections and remove them.
  1098. List<List<Ref<Connection>>::Element *> dead_connections;
  1099. for (List<Ref<Connection>>::Element *E = connections.front(); E; E = E->next()) {
  1100. Ref<Connection> &c = E->get();
  1101. if (c->_cache.dirty) {
  1102. Node *from = get_node_or_null(NodePath(c->from_node));
  1103. GraphNode *gnode_from = Object::cast_to<GraphNode>(from);
  1104. if (!gnode_from) {
  1105. dead_connections.push_back(E);
  1106. continue;
  1107. }
  1108. Node *to = get_node_or_null(NodePath(c->to_node));
  1109. GraphNode *gnode_to = Object::cast_to<GraphNode>(to);
  1110. if (!gnode_to) {
  1111. dead_connections.push_back(E);
  1112. continue;
  1113. }
  1114. const Vector2 from_pos = gnode_from->get_output_port_position(c->from_port) + gnode_from->get_position_offset();
  1115. const Vector2 to_pos = gnode_to->get_input_port_position(c->to_port) + gnode_to->get_position_offset();
  1116. const Color from_color = gnode_from->get_output_port_color(c->from_port);
  1117. const Color to_color = gnode_to->get_input_port_color(c->to_port);
  1118. const int from_type = gnode_from->get_output_port_type(c->from_port);
  1119. const int to_type = gnode_to->get_input_port_type(c->to_port);
  1120. c->_cache.from_pos = from_pos;
  1121. c->_cache.to_pos = to_pos;
  1122. c->_cache.from_color = from_color;
  1123. c->_cache.to_color = to_color;
  1124. PackedVector2Array line_points = get_connection_line(from_pos * zoom, to_pos * zoom);
  1125. c->_cache.line->set_points(line_points);
  1126. Ref<ShaderMaterial> line_material = c->_cache.line->get_material();
  1127. if (line_material.is_null()) {
  1128. line_material.instantiate();
  1129. c->_cache.line->set_material(line_material);
  1130. }
  1131. float line_width = _get_shader_line_width();
  1132. line_material->set_shader_parameter("line_width", line_width);
  1133. line_material->set_shader_parameter("from_type", from_type);
  1134. line_material->set_shader_parameter("to_type", to_type);
  1135. line_material->set_shader_parameter("rim_color", theme_cache.connection_rim_color);
  1136. // Compute bounding box of the line, including the line width.
  1137. c->_cache.aabb = Rect2(line_points[0], Vector2());
  1138. for (int i = 0; i < line_points.size(); i++) {
  1139. c->_cache.aabb.expand_to(line_points[i]);
  1140. }
  1141. c->_cache.aabb.grow_by(lines_thickness * 0.5);
  1142. c->_cache.dirty = false;
  1143. }
  1144. // Skip updating/drawing connections that are not visible.
  1145. Rect2 viewport_rect = get_viewport_rect();
  1146. viewport_rect.position += get_scroll_offset();
  1147. if (!c->_cache.aabb.intersects(viewport_rect)) {
  1148. continue;
  1149. }
  1150. Color from_color = c->_cache.from_color;
  1151. Color to_color = c->_cache.to_color;
  1152. if (c->activity > 0) {
  1153. from_color = from_color.lerp(theme_cache.activity_color, c->activity);
  1154. to_color = to_color.lerp(theme_cache.activity_color, c->activity);
  1155. }
  1156. if (c == hovered_connection) {
  1157. from_color = from_color.blend(theme_cache.connection_hover_tint_color);
  1158. to_color = to_color.blend(theme_cache.connection_hover_tint_color);
  1159. }
  1160. // Update Line2D node.
  1161. Ref<Gradient> line_gradient = memnew(Gradient);
  1162. float line_width = _get_shader_line_width();
  1163. c->_cache.line->set_width(line_width);
  1164. line_gradient->set_color(0, from_color);
  1165. line_gradient->set_color(1, to_color);
  1166. c->_cache.line->set_gradient(line_gradient);
  1167. }
  1168. for (const List<Ref<Connection>>::Element *E : dead_connections) {
  1169. List<Ref<Connection>> &connections_from = connection_map[E->get()->from_node];
  1170. List<Ref<Connection>> &connections_to = connection_map[E->get()->to_node];
  1171. connections_from.erase(E->get());
  1172. connections_to.erase(E->get());
  1173. E->get()->_cache.line->queue_free();
  1174. connections.erase(E->get());
  1175. }
  1176. }
  1177. void GraphEdit::_top_layer_draw() {
  1178. if (!box_selecting) {
  1179. return;
  1180. }
  1181. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_fill);
  1182. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_stroke, false);
  1183. }
  1184. void GraphEdit::_update_top_connection_layer() {
  1185. _update_scroll();
  1186. if (!connecting) {
  1187. dragged_connection_line->clear_points();
  1188. return;
  1189. }
  1190. GraphNode *graph_node_from = Object::cast_to<GraphNode>(get_node_or_null(NodePath(connecting_from_node)));
  1191. ERR_FAIL_NULL(graph_node_from);
  1192. Vector2 from_pos = graph_node_from->get_position() / zoom;
  1193. Vector2 to_pos = connecting_to_point / zoom;
  1194. int from_type;
  1195. int to_type = connecting_type;
  1196. Color from_color;
  1197. Color to_color = connecting_color;
  1198. if (connecting_from_output) {
  1199. from_pos += graph_node_from->get_output_port_position(connecting_from_port_index);
  1200. from_type = graph_node_from->get_output_port_type(connecting_from_port_index);
  1201. from_color = graph_node_from->get_output_port_color(connecting_from_port_index);
  1202. } else {
  1203. from_pos += graph_node_from->get_input_port_position(connecting_from_port_index);
  1204. from_type = graph_node_from->get_input_port_type(connecting_from_port_index);
  1205. from_color = graph_node_from->get_input_port_color(connecting_from_port_index);
  1206. }
  1207. if (connecting_target_valid) {
  1208. GraphNode *graph_node_to = Object::cast_to<GraphNode>(get_node_or_null(NodePath(connecting_target_node)));
  1209. ERR_FAIL_NULL(graph_node_to);
  1210. if (connecting_from_output) {
  1211. to_type = graph_node_to->get_input_port_type(connecting_target_port_index);
  1212. to_color = graph_node_to->get_input_port_color(connecting_target_port_index);
  1213. } else {
  1214. to_type = graph_node_to->get_output_port_type(connecting_target_port_index);
  1215. to_color = graph_node_to->get_output_port_color(connecting_target_port_index);
  1216. }
  1217. // Highlight the line to the mouse cursor when it's over a valid target port.
  1218. from_color = from_color.blend(theme_cache.connection_valid_target_tint_color);
  1219. to_color = to_color.blend(theme_cache.connection_valid_target_tint_color);
  1220. }
  1221. if (!connecting_from_output) {
  1222. SWAP(from_pos, to_pos);
  1223. SWAP(from_type, to_type);
  1224. SWAP(from_color, to_color);
  1225. }
  1226. PackedVector2Array line_points = get_connection_line(from_pos * zoom, to_pos * zoom);
  1227. dragged_connection_line->set_points(line_points);
  1228. Ref<ShaderMaterial> line_material = dragged_connection_line->get_material();
  1229. if (line_material.is_null()) {
  1230. line_material.instantiate();
  1231. line_material->set_shader(connections_shader);
  1232. dragged_connection_line->set_material(line_material);
  1233. }
  1234. float line_width = _get_shader_line_width();
  1235. line_material->set_shader_parameter("line_width", line_width);
  1236. line_material->set_shader_parameter("from_type", from_type);
  1237. line_material->set_shader_parameter("to_type", to_type);
  1238. line_material->set_shader_parameter("rim_color", theme_cache.connection_rim_color);
  1239. Ref<Gradient> line_gradient = memnew(Gradient);
  1240. dragged_connection_line->set_width(line_width);
  1241. line_gradient->set_color(0, from_color);
  1242. line_gradient->set_color(1, to_color);
  1243. dragged_connection_line->set_gradient(line_gradient);
  1244. }
  1245. void GraphEdit::_minimap_draw() {
  1246. if (!is_minimap_enabled()) {
  1247. return;
  1248. }
  1249. minimap->update_minimap();
  1250. // Draw the minimap background.
  1251. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  1252. minimap->draw_style_box(minimap->theme_cache.panel, minimap_rect);
  1253. Vector2 graph_offset = minimap->_get_graph_offset();
  1254. Vector2 minimap_offset = minimap->minimap_offset;
  1255. // Draw frame nodes.
  1256. for (int i = get_child_count() - 1; i >= 0; i--) {
  1257. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(get_child(i));
  1258. if (!graph_frame || !graph_frame->is_visible()) {
  1259. continue;
  1260. }
  1261. Vector2 node_position = minimap->_convert_from_graph_position(graph_frame->get_position_offset() * zoom - graph_offset) + minimap_offset;
  1262. Vector2 node_size = minimap->_convert_from_graph_position(graph_frame->get_size() * zoom);
  1263. Rect2 node_rect = Rect2(node_position, node_size);
  1264. Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate();
  1265. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  1266. Ref<StyleBoxFlat> sb_frame = graph_frame->get_theme_stylebox(graph_frame->is_selected() ? SNAME("panel_selected") : SNAME("panel"));
  1267. if (sb_frame.is_valid()) {
  1268. Color node_color = sb_frame->get_bg_color();
  1269. if (graph_frame->is_tint_color_enabled()) {
  1270. node_color = graph_frame->get_tint_color();
  1271. }
  1272. sb_minimap->set_bg_color(node_color);
  1273. }
  1274. minimap->draw_style_box(sb_minimap, node_rect);
  1275. }
  1276. // Draw regular graph nodes.
  1277. for (int i = get_child_count() - 1; i >= 0; i--) {
  1278. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  1279. if (!graph_node || !graph_node->is_visible()) {
  1280. continue;
  1281. }
  1282. Vector2 node_position = minimap->_convert_from_graph_position(graph_node->get_position_offset() * zoom - graph_offset) + minimap_offset;
  1283. Vector2 node_size = minimap->_convert_from_graph_position(graph_node->get_size() * zoom);
  1284. Rect2 node_rect = Rect2(node_position, node_size);
  1285. Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate();
  1286. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  1287. Ref<StyleBoxFlat> sb_frame = graph_node->is_selected() ? graph_node->theme_cache.panel_selected : graph_node->theme_cache.panel;
  1288. if (sb_frame.is_valid()) {
  1289. Color node_color = sb_frame->get_bg_color();
  1290. sb_minimap->set_bg_color(node_color);
  1291. }
  1292. minimap->draw_style_box(sb_minimap, node_rect);
  1293. }
  1294. // Draw node connections.
  1295. for (const Ref<Connection> &c : connections) {
  1296. Vector2 from_graph_position = c->_cache.from_pos * zoom - graph_offset;
  1297. Vector2 to_graph_position = c->_cache.to_pos * zoom - graph_offset;
  1298. Color from_color = c->_cache.from_color;
  1299. Color to_color = c->_cache.to_color;
  1300. if (c->activity > 0) {
  1301. from_color = from_color.lerp(theme_cache.activity_color, c->activity);
  1302. to_color = to_color.lerp(theme_cache.activity_color, c->activity);
  1303. }
  1304. _draw_minimap_connection_line(from_graph_position, to_graph_position, from_color, to_color);
  1305. }
  1306. // Draw the "camera" viewport.
  1307. Rect2 camera_rect = minimap->get_camera_rect();
  1308. minimap->draw_style_box(minimap->theme_cache.camera_style, camera_rect);
  1309. // Draw the resizer control.
  1310. Ref<Texture2D> resizer = minimap->theme_cache.resizer;
  1311. Color resizer_color = minimap->theme_cache.resizer_color;
  1312. minimap->draw_texture(resizer, Point2(), resizer_color);
  1313. }
  1314. void GraphEdit::_draw_grid() {
  1315. Vector2 offset = get_scroll_offset() / zoom;
  1316. Size2 size = get_size() / zoom;
  1317. Point2i from_pos = (offset / float(snapping_distance)).floor();
  1318. Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1);
  1319. switch (grid_pattern) {
  1320. case GRID_PATTERN_LINES: {
  1321. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  1322. Color color;
  1323. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  1324. color = theme_cache.grid_major;
  1325. } else {
  1326. color = theme_cache.grid_minor;
  1327. }
  1328. float base_offset = i * snapping_distance * zoom - offset.x * zoom;
  1329. draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color);
  1330. }
  1331. for (int i = from_pos.y; i < from_pos.y + len.y; i++) {
  1332. Color color;
  1333. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  1334. color = theme_cache.grid_major;
  1335. } else {
  1336. color = theme_cache.grid_minor;
  1337. }
  1338. float base_offset = i * snapping_distance * zoom - offset.y * zoom;
  1339. draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color);
  1340. }
  1341. } break;
  1342. case GRID_PATTERN_DOTS: {
  1343. Color transparent_grid_minor = theme_cache.grid_minor;
  1344. transparent_grid_minor.a *= CLAMP(1.0 * (zoom - 0.4), 0, 1);
  1345. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  1346. for (int j = from_pos.y; j < from_pos.y + len.y; j++) {
  1347. Color color = transparent_grid_minor;
  1348. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0 && ABS(j) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0) {
  1349. color = theme_cache.grid_major;
  1350. }
  1351. if (color.a == 0) {
  1352. continue;
  1353. }
  1354. float base_offset_x = i * snapping_distance * zoom - offset.x * zoom;
  1355. float base_offset_y = j * snapping_distance * zoom - offset.y * zoom;
  1356. draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), color);
  1357. }
  1358. }
  1359. } break;
  1360. }
  1361. }
  1362. void GraphEdit::set_selected(Node *p_child) {
  1363. for (int i = get_child_count() - 1; i >= 0; i--) {
  1364. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1365. if (!graph_element) {
  1366. continue;
  1367. }
  1368. graph_element->set_selected(graph_element == p_child);
  1369. }
  1370. }
  1371. void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
  1372. ERR_FAIL_COND(p_ev.is_null());
  1373. if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) {
  1374. return;
  1375. }
  1376. // Highlight the connection close to the mouse cursor.
  1377. Ref<InputEventMouseMotion> mm = p_ev;
  1378. if (mm.is_valid()) {
  1379. Ref<Connection> new_highlighted_connection = get_closest_connection_at_point(mm->get_position());
  1380. if (new_highlighted_connection != hovered_connection) {
  1381. connections_layer->queue_redraw();
  1382. }
  1383. hovered_connection = new_highlighted_connection;
  1384. }
  1385. // Logic for moving graph controls via mouse drag.
  1386. if (mm.is_valid() && dragging) {
  1387. if (!moving_selection) {
  1388. emit_signal(SNAME("begin_node_move"));
  1389. moving_selection = true;
  1390. }
  1391. just_selected = true;
  1392. drag_accum += mm->get_relative();
  1393. for (int i = get_child_count() - 1; i >= 0; i--) {
  1394. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1395. if (graph_element && graph_element->is_selected() && graph_element->is_draggable()) {
  1396. Vector2 pos = (graph_element->get_drag_from() * zoom + drag_accum) / zoom;
  1397. // Snapping can be toggled temporarily by holding down Ctrl.
  1398. // This is done here as to not toggle the grid when holding down Ctrl.
  1399. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1400. pos = pos.snappedf(snapping_distance);
  1401. }
  1402. graph_element->set_position_offset(pos);
  1403. if (linked_parent_map.has(graph_element->get_name())) {
  1404. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[graph_element->get_name()])));
  1405. if (parent_frame) {
  1406. _update_graph_frame(parent_frame);
  1407. }
  1408. }
  1409. // Update all frame transforms recursively.
  1410. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(get_child(i));
  1411. if (graph_frame) {
  1412. _set_position_of_frame_attached_nodes(graph_frame, drag_accum);
  1413. }
  1414. }
  1415. }
  1416. }
  1417. // Box selection logic.
  1418. if (mm.is_valid() && box_selecting) {
  1419. box_selecting_to = mm->get_position();
  1420. box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());
  1421. for (int i = get_child_count() - 1; i >= 0; i--) {
  1422. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1423. if (!graph_element) {
  1424. continue;
  1425. }
  1426. // Only select frames when the box selection is fully enclosing them.
  1427. bool is_frame = Object::cast_to<GraphFrame>(graph_element);
  1428. Rect2 r = graph_element->get_rect();
  1429. bool should_be_selected = is_frame ? box_selecting_rect.encloses(r) : box_selecting_rect.intersects(r);
  1430. if (should_be_selected) {
  1431. graph_element->set_selected(box_selection_mode_additive);
  1432. } else {
  1433. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  1434. }
  1435. }
  1436. top_layer->queue_redraw();
  1437. minimap->queue_redraw();
  1438. }
  1439. Ref<InputEventMouseButton> mb = p_ev;
  1440. if (mb.is_valid()) {
  1441. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  1442. if (box_selecting) {
  1443. box_selecting = false;
  1444. for (int i = get_child_count() - 1; i >= 0; i--) {
  1445. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1446. if (!graph_element) {
  1447. continue;
  1448. }
  1449. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  1450. }
  1451. top_layer->queue_redraw();
  1452. minimap->queue_redraw();
  1453. } else {
  1454. if (connecting) {
  1455. force_connection_drag_end();
  1456. } else {
  1457. emit_signal(SNAME("popup_request"), mb->get_position());
  1458. }
  1459. }
  1460. }
  1461. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && dragging) {
  1462. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1463. // Deselect current node.
  1464. for (int i = get_child_count() - 1; i >= 0; i--) {
  1465. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1466. if (graph_element) {
  1467. Rect2 r = graph_element->get_rect();
  1468. if (r.has_point(mb->get_position())) {
  1469. graph_element->set_selected(false);
  1470. }
  1471. }
  1472. }
  1473. }
  1474. if (drag_accum != Vector2()) {
  1475. for (int i = get_child_count() - 1; i >= 0; i--) {
  1476. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1477. if (graph_element && graph_element->is_selected()) {
  1478. graph_element->set_drag(false);
  1479. GraphFrame *frame = Object::cast_to<GraphFrame>(get_child(i));
  1480. if (frame) {
  1481. _set_drag_frame_attached_nodes(frame, false);
  1482. }
  1483. }
  1484. }
  1485. }
  1486. if (moving_selection) {
  1487. emit_signal(SNAME("end_node_move"));
  1488. moving_selection = false;
  1489. Vector<GraphElement *> dragged_nodes;
  1490. for (int i = get_child_count() - 1; i >= 0; i--) {
  1491. GraphElement *moved_node = Object::cast_to<GraphElement>(get_child(i));
  1492. if (moved_node && moved_node->is_selected() && moved_node->is_draggable()) {
  1493. dragged_nodes.push_back(moved_node);
  1494. }
  1495. }
  1496. GraphFrame *frame_dropped_on = nullptr;
  1497. // Find frame on which the node(s) is/were dropped.
  1498. // Count down to find the topmost frame.
  1499. for (int i = get_child_count() - 1; i >= 0; i--) {
  1500. GraphFrame *frame = Object::cast_to<GraphFrame>(get_child(i));
  1501. if (!frame || frame->is_resizing()) {
  1502. continue;
  1503. }
  1504. Rect2 frame_rect = frame->get_rect();
  1505. if (frame_rect.has_point(mb->get_position()) && !dragged_nodes.has(frame)) {
  1506. frame_dropped_on = frame;
  1507. break;
  1508. }
  1509. }
  1510. if (frame_dropped_on) {
  1511. dragged_nodes.erase(frame_dropped_on);
  1512. TypedArray<StringName> dragged_node_names;
  1513. for (GraphElement *moved_node : dragged_nodes) {
  1514. if (!linked_parent_map.has(moved_node->get_name())) {
  1515. dragged_node_names.push_back(moved_node->get_name());
  1516. }
  1517. }
  1518. if (dragged_node_names.size() > 0) {
  1519. emit_signal(SNAME("graph_elements_linked_to_frame_request"), dragged_node_names, frame_dropped_on->get_name());
  1520. }
  1521. }
  1522. }
  1523. dragging = false;
  1524. minimap->queue_redraw();
  1525. queue_redraw();
  1526. connections_layer->queue_redraw();
  1527. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1528. }
  1529. // Node selection logic.
  1530. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  1531. GraphElement *graph_element = nullptr;
  1532. // Find node which was clicked on.
  1533. for (int i = get_child_count() - 1; i >= 0; i--) {
  1534. GraphElement *selected_element = Object::cast_to<GraphElement>(get_child(i));
  1535. if (!selected_element) {
  1536. continue;
  1537. }
  1538. if (selected_element->is_resizing()) {
  1539. continue;
  1540. }
  1541. if (selected_element->has_point((mb->get_position() - selected_element->get_position()) / zoom)) {
  1542. graph_element = selected_element;
  1543. break;
  1544. }
  1545. }
  1546. if (graph_element) {
  1547. if (_filter_input(mb->get_position())) {
  1548. return;
  1549. }
  1550. // Left-clicked on a node, select it.
  1551. dragging = true;
  1552. drag_accum = Vector2();
  1553. just_selected = !graph_element->is_selected();
  1554. if (!graph_element->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1555. for (int i = 0; i < get_child_count(); i++) {
  1556. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1557. if (!child_element) {
  1558. continue;
  1559. }
  1560. child_element->set_selected(child_element == graph_element);
  1561. }
  1562. }
  1563. graph_element->set_selected(true);
  1564. for (int i = 0; i < get_child_count(); i++) {
  1565. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1566. if (!child_element) {
  1567. continue;
  1568. }
  1569. if (child_element->is_selected()) {
  1570. child_element->set_drag(true);
  1571. GraphFrame *frame_node = Object::cast_to<GraphFrame>(get_child(i));
  1572. if (frame_node) {
  1573. _ensure_node_order_from(frame_node);
  1574. _set_drag_frame_attached_nodes(frame_node, true);
  1575. }
  1576. }
  1577. }
  1578. } else {
  1579. if (_filter_input(mb->get_position())) {
  1580. return;
  1581. }
  1582. if (panner->is_panning()) {
  1583. return;
  1584. }
  1585. // Left-clicked on empty space, start box select.
  1586. box_selecting = true;
  1587. box_selecting_from = mb->get_position();
  1588. if (mb->is_command_or_control_pressed()) {
  1589. box_selection_mode_additive = true;
  1590. prev_selected.clear();
  1591. for (int i = get_child_count() - 1; i >= 0; i--) {
  1592. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1593. if (!child_element || !child_element->is_selected()) {
  1594. continue;
  1595. }
  1596. prev_selected.push_back(child_element);
  1597. }
  1598. } else if (mb->is_shift_pressed()) {
  1599. box_selection_mode_additive = false;
  1600. prev_selected.clear();
  1601. for (int i = get_child_count() - 1; i >= 0; i--) {
  1602. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1603. if (!child_element || !child_element->is_selected()) {
  1604. continue;
  1605. }
  1606. prev_selected.push_back(child_element);
  1607. }
  1608. } else {
  1609. box_selection_mode_additive = true;
  1610. prev_selected.clear();
  1611. for (int i = get_child_count() - 1; i >= 0; i--) {
  1612. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1613. if (!child_element) {
  1614. continue;
  1615. }
  1616. child_element->set_selected(false);
  1617. }
  1618. }
  1619. }
  1620. }
  1621. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
  1622. // Box selection ended. Nodes were selected during mouse movement.
  1623. box_selecting = false;
  1624. box_selecting_rect = Rect2();
  1625. prev_selected.clear();
  1626. top_layer->queue_redraw();
  1627. minimap->queue_redraw();
  1628. }
  1629. }
  1630. if (p_ev->is_pressed()) {
  1631. if (p_ev->is_action("ui_graph_duplicate", true)) {
  1632. emit_signal(SNAME("duplicate_nodes_request"));
  1633. accept_event();
  1634. } else if (p_ev->is_action("ui_copy", true)) {
  1635. emit_signal(SNAME("copy_nodes_request"));
  1636. accept_event();
  1637. } else if (p_ev->is_action("ui_paste", true)) {
  1638. emit_signal(SNAME("paste_nodes_request"));
  1639. accept_event();
  1640. } else if (p_ev->is_action("ui_graph_delete", true)) {
  1641. TypedArray<StringName> nodes;
  1642. for (int i = 0; i < get_child_count(); i++) {
  1643. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1644. if (!graph_element) {
  1645. continue;
  1646. }
  1647. if (graph_element->is_selected()) {
  1648. nodes.push_back(graph_element->get_name());
  1649. }
  1650. }
  1651. emit_signal(SNAME("delete_nodes_request"), nodes);
  1652. accept_event();
  1653. }
  1654. }
  1655. }
  1656. void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1657. h_scrollbar->set_value(h_scrollbar->get_value() - p_scroll_vec.x);
  1658. v_scrollbar->set_value(v_scrollbar->get_value() - p_scroll_vec.y);
  1659. connections_layer->queue_redraw();
  1660. }
  1661. void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1662. // We need to invalidate all connections since we don't know whether
  1663. // the user is zooming/panning at the same time.
  1664. _invalidate_connection_line_cache();
  1665. set_zoom_custom(zoom * p_zoom_factor, p_origin);
  1666. }
  1667. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1668. for (Ref<Connection> &c : connection_map[p_from]) {
  1669. if (c->from_node == p_from && c->from_port == p_from_port && c->to_node == p_to && c->to_port == p_to_port) {
  1670. if (!Math::is_equal_approx(c->activity, p_activity)) {
  1671. // Update only if changed.
  1672. minimap->queue_redraw();
  1673. c->_cache.dirty = true;
  1674. connections_layer->queue_redraw();
  1675. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1676. }
  1677. c->activity = p_activity;
  1678. return;
  1679. }
  1680. }
  1681. }
  1682. void GraphEdit::reset_all_connection_activity() {
  1683. bool changed = false;
  1684. for (Ref<Connection> &conn : connections) {
  1685. if (conn->activity > 0) {
  1686. changed = true;
  1687. conn->_cache.dirty = true;
  1688. }
  1689. conn->activity = 0;
  1690. }
  1691. if (changed) {
  1692. connections_layer->queue_redraw();
  1693. }
  1694. }
  1695. void GraphEdit::clear_connections() {
  1696. for (Ref<Connection> &c : connections) {
  1697. c->_cache.line->queue_free();
  1698. }
  1699. connections.clear();
  1700. connection_map.clear();
  1701. minimap->queue_redraw();
  1702. queue_redraw();
  1703. connections_layer->queue_redraw();
  1704. }
  1705. void GraphEdit::force_connection_drag_end() {
  1706. ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
  1707. connecting = false;
  1708. connecting_valid = false;
  1709. minimap->queue_redraw();
  1710. queue_redraw();
  1711. connections_layer->queue_redraw();
  1712. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1713. emit_signal(SNAME("connection_drag_ended"));
  1714. }
  1715. bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) {
  1716. bool valid = true;
  1717. GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid);
  1718. return valid;
  1719. }
  1720. void GraphEdit::set_panning_scheme(PanningScheme p_scheme) {
  1721. panning_scheme = p_scheme;
  1722. panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme);
  1723. }
  1724. GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const {
  1725. return panning_scheme;
  1726. }
  1727. void GraphEdit::set_zoom(float p_zoom) {
  1728. set_zoom_custom(p_zoom, get_size() / 2);
  1729. }
  1730. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1731. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1732. if (zoom == p_zoom) {
  1733. return;
  1734. }
  1735. Vector2 scrollbar_offset = (Vector2(h_scrollbar->get_value(), v_scrollbar->get_value()) + p_center) / zoom;
  1736. zoom = p_zoom;
  1737. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1738. zoom_minus_button->set_disabled(zoom == zoom_min);
  1739. zoom_plus_button->set_disabled(zoom == zoom_max);
  1740. _update_scroll();
  1741. minimap->queue_redraw();
  1742. connections_layer->queue_redraw();
  1743. if (is_visible_in_tree()) {
  1744. Vector2 offset = scrollbar_offset * zoom - p_center;
  1745. h_scrollbar->set_value(offset.x);
  1746. v_scrollbar->set_value(offset.y);
  1747. }
  1748. _update_zoom_label();
  1749. queue_redraw();
  1750. }
  1751. float GraphEdit::get_zoom() const {
  1752. return zoom;
  1753. }
  1754. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1755. p_zoom_step = abs(p_zoom_step);
  1756. ERR_FAIL_COND(!isfinite(p_zoom_step));
  1757. if (zoom_step == p_zoom_step) {
  1758. return;
  1759. }
  1760. zoom_step = p_zoom_step;
  1761. panner->set_scroll_zoom_factor(zoom_step);
  1762. }
  1763. float GraphEdit::get_zoom_step() const {
  1764. return zoom_step;
  1765. }
  1766. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1767. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1768. if (zoom_min == p_zoom_min) {
  1769. return;
  1770. }
  1771. zoom_min = p_zoom_min;
  1772. set_zoom(zoom);
  1773. }
  1774. float GraphEdit::get_zoom_min() const {
  1775. return zoom_min;
  1776. }
  1777. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1778. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1779. if (zoom_max == p_zoom_max) {
  1780. return;
  1781. }
  1782. zoom_max = p_zoom_max;
  1783. set_zoom(zoom);
  1784. }
  1785. float GraphEdit::get_zoom_max() const {
  1786. return zoom_max;
  1787. }
  1788. void GraphEdit::set_right_disconnects(bool p_enable) {
  1789. right_disconnects = p_enable;
  1790. }
  1791. bool GraphEdit::is_right_disconnects_enabled() const {
  1792. return right_disconnects;
  1793. }
  1794. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1795. valid_right_disconnect_types.insert(p_type);
  1796. }
  1797. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1798. valid_right_disconnect_types.erase(p_type);
  1799. }
  1800. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1801. valid_left_disconnect_types.insert(p_type);
  1802. }
  1803. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1804. valid_left_disconnect_types.erase(p_type);
  1805. }
  1806. TypedArray<Dictionary> GraphEdit::_get_connection_list() const {
  1807. List<Ref<Connection>> conns = get_connection_list();
  1808. TypedArray<Dictionary> arr;
  1809. for (const Ref<Connection> &conn : conns) {
  1810. Dictionary d;
  1811. d["from_node"] = conn->from_node;
  1812. d["from_port"] = conn->from_port;
  1813. d["to_node"] = conn->to_node;
  1814. d["to_port"] = conn->to_port;
  1815. arr.push_back(d);
  1816. }
  1817. return arr;
  1818. }
  1819. Dictionary GraphEdit::_get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance) const {
  1820. Dictionary ret;
  1821. Ref<Connection> c = get_closest_connection_at_point(p_point, p_max_distance);
  1822. if (c.is_valid()) {
  1823. ret["from_node"] = c->from_node;
  1824. ret["from_port"] = c->from_port;
  1825. ret["to_node"] = c->to_node;
  1826. ret["to_port"] = c->to_port;
  1827. }
  1828. return ret;
  1829. }
  1830. TypedArray<Dictionary> GraphEdit::_get_connections_intersecting_with_rect(const Rect2 &p_rect) const {
  1831. List<Ref<Connection>> intersecting_connections = get_connections_intersecting_with_rect(p_rect);
  1832. TypedArray<Dictionary> arr;
  1833. for (const Ref<Connection> &conn : intersecting_connections) {
  1834. Dictionary d;
  1835. d["from_node"] = conn->from_node;
  1836. d["from_port"] = conn->from_port;
  1837. d["to_node"] = conn->to_node;
  1838. d["to_port"] = conn->to_port;
  1839. arr.push_back(d);
  1840. }
  1841. return arr;
  1842. }
  1843. void GraphEdit::_zoom_minus() {
  1844. set_zoom(zoom / zoom_step);
  1845. }
  1846. void GraphEdit::_zoom_reset() {
  1847. set_zoom(1);
  1848. }
  1849. void GraphEdit::_zoom_plus() {
  1850. set_zoom(zoom * zoom_step);
  1851. }
  1852. void GraphEdit::_update_zoom_label() {
  1853. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1854. String zoom_text = itos(zoom_percent) + "%";
  1855. zoom_label->set_text(zoom_text);
  1856. }
  1857. void GraphEdit::_invalidate_connection_line_cache() {
  1858. for (Ref<Connection> &c : connections) {
  1859. c->_cache.dirty = true;
  1860. }
  1861. }
  1862. float GraphEdit::_get_shader_line_width() {
  1863. return lines_thickness * theme_cache.base_scale + 4.0;
  1864. }
  1865. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1866. ConnectionType ct(p_type, p_with_type);
  1867. valid_connection_types.insert(ct);
  1868. }
  1869. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1870. ConnectionType ct(p_type, p_with_type);
  1871. valid_connection_types.erase(ct);
  1872. }
  1873. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1874. ConnectionType ct(p_type, p_with_type);
  1875. return valid_connection_types.has(ct);
  1876. }
  1877. void GraphEdit::attach_graph_element_to_frame(const StringName &p_graph_element, const StringName &p_parent_frame) {
  1878. GraphFrame *frame = Object::cast_to<GraphFrame>(get_node(NodePath(p_parent_frame)));
  1879. ERR_FAIL_NULL_MSG(frame, "Frame does not exist or is not of type GraphFrame.");
  1880. GraphElement *graph_element = Object::cast_to<GraphElement>(get_node(NodePath(p_graph_element)));
  1881. ERR_FAIL_NULL_MSG(graph_element, "Graph element to attach does not exist or is not of type GraphElement.");
  1882. ERR_FAIL_COND_MSG(frame == graph_element, "Cannot attach a frame to itself.");
  1883. linked_parent_map.insert(p_graph_element, p_parent_frame);
  1884. frame_attached_nodes[p_parent_frame].insert(p_graph_element);
  1885. _ensure_node_order_from_root(p_graph_element);
  1886. _update_graph_frame(frame);
  1887. }
  1888. void GraphEdit::detach_graph_element_from_frame(const StringName &p_graph_element) {
  1889. if (!linked_parent_map.has(p_graph_element)) {
  1890. return;
  1891. }
  1892. GraphFrame *frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[p_graph_element])));
  1893. ERR_FAIL_NULL_MSG(frame, "Frame does not exist or is not of type GraphFrame.");
  1894. GraphElement *graph_element = Object::cast_to<GraphElement>(get_node(NodePath(p_graph_element)));
  1895. ERR_FAIL_NULL_MSG(graph_element, "Graph element to detach does not exist or is not of type GraphElement.");
  1896. frame_attached_nodes.get(frame->get_name()).erase(p_graph_element);
  1897. linked_parent_map.erase(p_graph_element);
  1898. _update_graph_frame(frame);
  1899. }
  1900. GraphFrame *GraphEdit::get_element_frame(const StringName &p_attached_graph_element) {
  1901. if (!linked_parent_map.has(p_attached_graph_element)) {
  1902. return nullptr;
  1903. }
  1904. Node *parent = get_node_or_null(NodePath(linked_parent_map[p_attached_graph_element]));
  1905. return Object::cast_to<GraphFrame>(parent);
  1906. }
  1907. TypedArray<StringName> GraphEdit::get_attached_nodes_of_frame(const StringName &p_graph_frame) {
  1908. if (!frame_attached_nodes.has(p_graph_frame)) {
  1909. return TypedArray<StringName>();
  1910. }
  1911. TypedArray<StringName> attached_nodes;
  1912. for (const StringName &node : frame_attached_nodes.get(p_graph_frame)) {
  1913. attached_nodes.push_back(node);
  1914. }
  1915. return attached_nodes;
  1916. }
  1917. void GraphEdit::set_snapping_enabled(bool p_enable) {
  1918. if (snapping_enabled == p_enable) {
  1919. return;
  1920. }
  1921. snapping_enabled = p_enable;
  1922. toggle_snapping_button->set_pressed(p_enable);
  1923. queue_redraw();
  1924. }
  1925. bool GraphEdit::is_snapping_enabled() const {
  1926. return snapping_enabled;
  1927. }
  1928. void GraphEdit::set_snapping_distance(int p_snapping_distance) {
  1929. ERR_FAIL_COND_MSG(p_snapping_distance < GRID_MIN_SNAPPING_DISTANCE || p_snapping_distance > GRID_MAX_SNAPPING_DISTANCE,
  1930. vformat("GraphEdit's snapping distance must be between %d and %d (inclusive)", GRID_MIN_SNAPPING_DISTANCE, GRID_MAX_SNAPPING_DISTANCE));
  1931. snapping_distance = p_snapping_distance;
  1932. snapping_distance_spinbox->set_value(p_snapping_distance);
  1933. queue_redraw();
  1934. }
  1935. int GraphEdit::get_snapping_distance() const {
  1936. return snapping_distance;
  1937. }
  1938. void GraphEdit::set_show_grid(bool p_show) {
  1939. if (show_grid == p_show) {
  1940. return;
  1941. }
  1942. show_grid = p_show;
  1943. toggle_grid_button->set_pressed(p_show);
  1944. queue_redraw();
  1945. }
  1946. bool GraphEdit::is_showing_grid() const {
  1947. return show_grid;
  1948. }
  1949. void GraphEdit::set_grid_pattern(GridPattern p_pattern) {
  1950. if (grid_pattern == p_pattern) {
  1951. return;
  1952. }
  1953. grid_pattern = p_pattern;
  1954. queue_redraw();
  1955. }
  1956. GraphEdit::GridPattern GraphEdit::get_grid_pattern() const {
  1957. return grid_pattern;
  1958. }
  1959. void GraphEdit::_snapping_toggled() {
  1960. snapping_enabled = toggle_snapping_button->is_pressed();
  1961. }
  1962. void GraphEdit::_snapping_distance_changed(double) {
  1963. snapping_distance = snapping_distance_spinbox->get_value();
  1964. queue_redraw();
  1965. }
  1966. void GraphEdit::_show_grid_toggled() {
  1967. show_grid = toggle_grid_button->is_pressed();
  1968. queue_redraw();
  1969. }
  1970. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1971. minimap->set_size(p_size);
  1972. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1973. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1974. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  1975. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  1976. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1977. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1978. minimap->queue_redraw();
  1979. }
  1980. Vector2 GraphEdit::get_minimap_size() const {
  1981. return minimap->get_size();
  1982. }
  1983. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1984. if (minimap->get_modulate().a == p_opacity) {
  1985. return;
  1986. }
  1987. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1988. minimap->queue_redraw();
  1989. }
  1990. float GraphEdit::get_minimap_opacity() const {
  1991. Color minimap_modulate = minimap->get_modulate();
  1992. return minimap_modulate.a;
  1993. }
  1994. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1995. if (minimap_button->is_pressed() == p_enable) {
  1996. return;
  1997. }
  1998. minimap_button->set_pressed(p_enable);
  1999. _minimap_toggled();
  2000. minimap->queue_redraw();
  2001. }
  2002. bool GraphEdit::is_minimap_enabled() const {
  2003. return minimap_button->is_pressed();
  2004. }
  2005. void GraphEdit::set_show_menu(bool p_hidden) {
  2006. show_menu = p_hidden;
  2007. menu_panel->set_visible(show_menu);
  2008. }
  2009. bool GraphEdit::is_showing_menu() const {
  2010. return show_menu;
  2011. }
  2012. void GraphEdit::set_show_zoom_label(bool p_hidden) {
  2013. show_zoom_label = p_hidden;
  2014. zoom_label->set_visible(show_zoom_label);
  2015. }
  2016. bool GraphEdit::is_showing_zoom_label() const {
  2017. return show_zoom_label;
  2018. }
  2019. void GraphEdit::set_show_zoom_buttons(bool p_hidden) {
  2020. show_zoom_buttons = p_hidden;
  2021. zoom_minus_button->set_visible(show_zoom_buttons);
  2022. zoom_reset_button->set_visible(show_zoom_buttons);
  2023. zoom_plus_button->set_visible(show_zoom_buttons);
  2024. }
  2025. bool GraphEdit::is_showing_zoom_buttons() const {
  2026. return show_zoom_buttons;
  2027. }
  2028. void GraphEdit::set_show_grid_buttons(bool p_hidden) {
  2029. show_grid_buttons = p_hidden;
  2030. toggle_grid_button->set_visible(show_grid_buttons);
  2031. toggle_snapping_button->set_visible(show_grid_buttons);
  2032. snapping_distance_spinbox->set_visible(show_grid_buttons);
  2033. }
  2034. bool GraphEdit::is_showing_grid_buttons() const {
  2035. return show_grid_buttons;
  2036. }
  2037. void GraphEdit::set_show_minimap_button(bool p_hidden) {
  2038. show_minimap_button = p_hidden;
  2039. minimap_button->set_visible(show_minimap_button);
  2040. }
  2041. bool GraphEdit::is_showing_minimap_button() const {
  2042. return show_minimap_button;
  2043. }
  2044. void GraphEdit::set_show_arrange_button(bool p_hidden) {
  2045. show_arrange_button = p_hidden;
  2046. arrange_button->set_visible(show_arrange_button);
  2047. }
  2048. bool GraphEdit::is_showing_arrange_button() const {
  2049. return show_arrange_button;
  2050. }
  2051. void GraphEdit::override_connections_shader(const Ref<Shader> &p_shader) {
  2052. connections_shader = p_shader;
  2053. _invalidate_connection_line_cache();
  2054. connections_layer->queue_redraw();
  2055. minimap->queue_redraw();
  2056. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  2057. }
  2058. void GraphEdit::_minimap_toggled() {
  2059. if (is_minimap_enabled()) {
  2060. minimap->set_visible(true);
  2061. minimap->queue_redraw();
  2062. } else {
  2063. minimap->set_visible(false);
  2064. }
  2065. }
  2066. void GraphEdit::set_connection_lines_curvature(float p_curvature) {
  2067. lines_curvature = p_curvature;
  2068. _invalidate_connection_line_cache();
  2069. connections_layer->queue_redraw();
  2070. queue_redraw();
  2071. }
  2072. float GraphEdit::get_connection_lines_curvature() const {
  2073. return lines_curvature;
  2074. }
  2075. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  2076. ERR_FAIL_COND_MSG(p_thickness < 0, "Connection lines thickness must be greater than or equal to 0.");
  2077. if (lines_thickness == p_thickness) {
  2078. return;
  2079. }
  2080. lines_thickness = p_thickness;
  2081. _invalidate_connection_line_cache();
  2082. connections_layer->queue_redraw();
  2083. queue_redraw();
  2084. }
  2085. float GraphEdit::get_connection_lines_thickness() const {
  2086. return lines_thickness;
  2087. }
  2088. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  2089. if (lines_antialiased == p_antialiased) {
  2090. return;
  2091. }
  2092. lines_antialiased = p_antialiased;
  2093. _invalidate_connection_line_cache();
  2094. connections_layer->queue_redraw();
  2095. queue_redraw();
  2096. }
  2097. bool GraphEdit::is_connection_lines_antialiased() const {
  2098. return lines_antialiased;
  2099. }
  2100. HBoxContainer *GraphEdit::get_menu_hbox() {
  2101. return menu_hbox;
  2102. }
  2103. Ref<ViewPanner> GraphEdit::get_panner() {
  2104. return panner;
  2105. }
  2106. void GraphEdit::set_warped_panning(bool p_warped) {
  2107. warped_panning = p_warped;
  2108. }
  2109. void GraphEdit::arrange_nodes() {
  2110. arranger->arrange_nodes();
  2111. }
  2112. void GraphEdit::_bind_methods() {
  2113. ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::connect_node);
  2114. ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
  2115. ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
  2116. ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
  2117. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  2118. ClassDB::bind_method(D_METHOD("get_closest_connection_at_point", "point", "max_distance"), &GraphEdit::_get_closest_connection_at_point, DEFVAL(4.0));
  2119. ClassDB::bind_method(D_METHOD("get_connections_intersecting_with_rect", "rect"), &GraphEdit::_get_connections_intersecting_with_rect);
  2120. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  2121. ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end);
  2122. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &GraphEdit::get_scroll_offset);
  2123. ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &GraphEdit::set_scroll_offset);
  2124. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  2125. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  2126. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  2127. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  2128. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  2129. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  2130. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  2131. ClassDB::bind_method(D_METHOD("get_connection_line", "from_node", "to_node"), &GraphEdit::get_connection_line);
  2132. ClassDB::bind_method(D_METHOD("attach_graph_element_to_frame", "element", "frame"), &GraphEdit::attach_graph_element_to_frame);
  2133. ClassDB::bind_method(D_METHOD("detach_graph_element_from_frame", "element"), &GraphEdit::detach_graph_element_from_frame);
  2134. ClassDB::bind_method(D_METHOD("get_element_frame", "element"), &GraphEdit::get_element_frame);
  2135. ClassDB::bind_method(D_METHOD("get_attached_nodes_of_frame", "frame"), &GraphEdit::get_attached_nodes_of_frame);
  2136. ClassDB::bind_method(D_METHOD("set_panning_scheme", "scheme"), &GraphEdit::set_panning_scheme);
  2137. ClassDB::bind_method(D_METHOD("get_panning_scheme"), &GraphEdit::get_panning_scheme);
  2138. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  2139. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  2140. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  2141. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  2142. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  2143. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  2144. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  2145. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  2146. ClassDB::bind_method(D_METHOD("set_show_grid", "enable"), &GraphEdit::set_show_grid);
  2147. ClassDB::bind_method(D_METHOD("is_showing_grid"), &GraphEdit::is_showing_grid);
  2148. ClassDB::bind_method(D_METHOD("set_grid_pattern", "pattern"), &GraphEdit::set_grid_pattern);
  2149. ClassDB::bind_method(D_METHOD("get_grid_pattern"), &GraphEdit::get_grid_pattern);
  2150. ClassDB::bind_method(D_METHOD("set_snapping_enabled", "enable"), &GraphEdit::set_snapping_enabled);
  2151. ClassDB::bind_method(D_METHOD("is_snapping_enabled"), &GraphEdit::is_snapping_enabled);
  2152. ClassDB::bind_method(D_METHOD("set_snapping_distance", "pixels"), &GraphEdit::set_snapping_distance);
  2153. ClassDB::bind_method(D_METHOD("get_snapping_distance"), &GraphEdit::get_snapping_distance);
  2154. ClassDB::bind_method(D_METHOD("set_connection_lines_curvature", "curvature"), &GraphEdit::set_connection_lines_curvature);
  2155. ClassDB::bind_method(D_METHOD("get_connection_lines_curvature"), &GraphEdit::get_connection_lines_curvature);
  2156. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  2157. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  2158. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  2159. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  2160. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  2161. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  2162. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  2163. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  2164. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  2165. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  2166. ClassDB::bind_method(D_METHOD("set_show_menu", "hidden"), &GraphEdit::set_show_menu);
  2167. ClassDB::bind_method(D_METHOD("is_showing_menu"), &GraphEdit::is_showing_menu);
  2168. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  2169. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  2170. ClassDB::bind_method(D_METHOD("set_show_grid_buttons", "hidden"), &GraphEdit::set_show_grid_buttons);
  2171. ClassDB::bind_method(D_METHOD("is_showing_grid_buttons"), &GraphEdit::is_showing_grid_buttons);
  2172. ClassDB::bind_method(D_METHOD("set_show_zoom_buttons", "hidden"), &GraphEdit::set_show_zoom_buttons);
  2173. ClassDB::bind_method(D_METHOD("is_showing_zoom_buttons"), &GraphEdit::is_showing_zoom_buttons);
  2174. ClassDB::bind_method(D_METHOD("set_show_minimap_button", "hidden"), &GraphEdit::set_show_minimap_button);
  2175. ClassDB::bind_method(D_METHOD("is_showing_minimap_button"), &GraphEdit::is_showing_minimap_button);
  2176. ClassDB::bind_method(D_METHOD("set_show_arrange_button", "hidden"), &GraphEdit::set_show_arrange_button);
  2177. ClassDB::bind_method(D_METHOD("is_showing_arrange_button"), &GraphEdit::is_showing_arrange_button);
  2178. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  2179. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  2180. GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node", "in_port", "mouse_position");
  2181. GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node", "in_port", "mouse_position");
  2182. ClassDB::bind_method(D_METHOD("get_menu_hbox"), &GraphEdit::get_menu_hbox);
  2183. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  2184. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  2185. GDVIRTUAL_BIND(_get_connection_line, "from_position", "to_position")
  2186. GDVIRTUAL_BIND(_is_node_hover_valid, "from_node", "from_port", "to_node", "to_port");
  2187. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
  2188. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid"), "set_show_grid", "is_showing_grid");
  2189. ADD_PROPERTY(PropertyInfo(Variant::INT, "grid_pattern", PROPERTY_HINT_ENUM, "Lines,Dots"), "set_grid_pattern", "get_grid_pattern");
  2190. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snapping_enabled"), "set_snapping_enabled", "is_snapping_enabled");
  2191. ADD_PROPERTY(PropertyInfo(Variant::INT, "snapping_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snapping_distance", "get_snapping_distance");
  2192. ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
  2193. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  2194. ADD_GROUP("Connection Lines", "connection_lines");
  2195. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
  2196. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness", PROPERTY_HINT_RANGE, "0,100,0.1,suffix:px"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  2197. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  2198. ADD_GROUP("Zoom", "");
  2199. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  2200. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  2201. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  2202. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  2203. ADD_GROUP("Minimap", "minimap_");
  2204. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  2205. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_size", "get_minimap_size");
  2206. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  2207. ADD_GROUP("Toolbar Menu", "");
  2208. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_menu"), "set_show_menu", "is_showing_menu");
  2209. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  2210. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_buttons"), "set_show_zoom_buttons", "is_showing_zoom_buttons");
  2211. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid_buttons"), "set_show_grid_buttons", "is_showing_grid_buttons");
  2212. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_minimap_button"), "set_show_minimap_button", "is_showing_minimap_button");
  2213. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_arrange_button"), "set_show_arrange_button", "is_showing_arrange_button");
  2214. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  2215. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  2216. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2217. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2218. ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output")));
  2219. ADD_SIGNAL(MethodInfo("connection_drag_ended"));
  2220. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  2221. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  2222. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  2223. ADD_SIGNAL(MethodInfo("delete_nodes_request", PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_ARRAY_TYPE, "StringName")));
  2224. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2225. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2226. ADD_SIGNAL(MethodInfo("frame_rect_changed", PropertyInfo(Variant::OBJECT, "frame", PROPERTY_HINT_RESOURCE_TYPE, "GraphFrame"), PropertyInfo(Variant::VECTOR2, "new_rect")));
  2227. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "at_position")));
  2228. ADD_SIGNAL(MethodInfo("begin_node_move"));
  2229. ADD_SIGNAL(MethodInfo("end_node_move"));
  2230. ADD_SIGNAL(MethodInfo("graph_elements_linked_to_frame_request", PropertyInfo(Variant::ARRAY, "elements"), PropertyInfo(Variant::STRING_NAME, "frame")));
  2231. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "offset")));
  2232. BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
  2233. BIND_ENUM_CONSTANT(SCROLL_PANS);
  2234. BIND_ENUM_CONSTANT(GRID_PATTERN_LINES);
  2235. BIND_ENUM_CONSTANT(GRID_PATTERN_DOTS);
  2236. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, panel);
  2237. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_major);
  2238. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_minor);
  2239. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, GraphEdit, activity_color, "activity");
  2240. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_hover_tint_color);
  2241. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_valid_target_tint_color);
  2242. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_rim_color);
  2243. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_fill);
  2244. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_stroke);
  2245. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, menu_panel);
  2246. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_in);
  2247. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_out);
  2248. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_reset);
  2249. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, snapping_toggle);
  2250. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, grid_toggle);
  2251. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, minimap_toggle);
  2252. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, layout);
  2253. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_inner_extent);
  2254. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_outer_extent);
  2255. }
  2256. GraphEdit::GraphEdit() {
  2257. set_focus_mode(FOCUS_ALL);
  2258. // Allow dezooming 8 times from the default zoom level.
  2259. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  2260. // but this is still useful for previewing and navigation.
  2261. zoom_min = (1 / Math::pow(zoom_step, 8));
  2262. // Allow zooming 4 times from the default zoom level.
  2263. zoom_max = (1 * Math::pow(zoom_step, 4));
  2264. panner.instantiate();
  2265. panner->set_callbacks(callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback));
  2266. top_layer = memnew(Control);
  2267. add_child(top_layer, false, INTERNAL_MODE_BACK);
  2268. top_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2269. top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2270. top_layer->connect(SceneStringName(draw), callable_mp(this, &GraphEdit::_top_layer_draw));
  2271. top_layer->connect(SceneStringName(focus_exited), callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  2272. connections_layer = memnew(Control);
  2273. add_child(connections_layer, false);
  2274. connections_layer->connect(SceneStringName(draw), callable_mp(this, &GraphEdit::_update_connections));
  2275. connections_layer->set_name("_connection_layer");
  2276. connections_layer->set_disable_visibility_clip(true); // Necessary, so it can draw freely and be offset.
  2277. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2278. top_connection_layer = memnew(GraphEditFilter(this));
  2279. add_child(top_connection_layer, false, INTERNAL_MODE_BACK);
  2280. connections_shader = default_connections_shader;
  2281. top_connection_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  2282. top_connection_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2283. top_connection_layer->connect(SceneStringName(gui_input), callable_mp(this, &GraphEdit::_top_connection_layer_input));
  2284. dragged_connection_line = memnew(Line2D);
  2285. dragged_connection_line->set_texture_mode(Line2D::LINE_TEXTURE_STRETCH);
  2286. top_connection_layer->add_child(dragged_connection_line);
  2287. h_scrollbar = memnew(HScrollBar);
  2288. h_scrollbar->set_name("_h_scroll");
  2289. top_layer->add_child(h_scrollbar);
  2290. v_scrollbar = memnew(VScrollBar);
  2291. v_scrollbar->set_name("_v_scroll");
  2292. top_layer->add_child(v_scrollbar);
  2293. // Set large minmax so it can scroll even if not resized yet.
  2294. h_scrollbar->set_min(-10000);
  2295. h_scrollbar->set_max(10000);
  2296. v_scrollbar->set_min(-10000);
  2297. v_scrollbar->set_max(10000);
  2298. h_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2299. v_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2300. // Toolbar menu.
  2301. menu_panel = memnew(PanelContainer);
  2302. menu_panel->set_visible(show_menu);
  2303. top_layer->add_child(menu_panel);
  2304. menu_panel->set_position(Vector2(10, 10));
  2305. menu_hbox = memnew(HBoxContainer);
  2306. menu_panel->add_child(menu_hbox);
  2307. // Zoom label and controls.
  2308. zoom_label = memnew(Label);
  2309. zoom_label->set_visible(show_zoom_label);
  2310. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  2311. zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2312. zoom_label->set_custom_minimum_size(Size2(48, 0));
  2313. menu_hbox->add_child(zoom_label);
  2314. _update_zoom_label();
  2315. zoom_minus_button = memnew(Button);
  2316. zoom_minus_button->set_theme_type_variation("FlatButton");
  2317. zoom_minus_button->set_visible(show_zoom_buttons);
  2318. zoom_minus_button->set_tooltip_text(ETR("Zoom Out"));
  2319. zoom_minus_button->set_focus_mode(FOCUS_NONE);
  2320. menu_hbox->add_child(zoom_minus_button);
  2321. zoom_minus_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_zoom_minus));
  2322. zoom_reset_button = memnew(Button);
  2323. zoom_reset_button->set_theme_type_variation("FlatButton");
  2324. zoom_reset_button->set_visible(show_zoom_buttons);
  2325. zoom_reset_button->set_tooltip_text(ETR("Zoom Reset"));
  2326. zoom_reset_button->set_focus_mode(FOCUS_NONE);
  2327. menu_hbox->add_child(zoom_reset_button);
  2328. zoom_reset_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_zoom_reset));
  2329. zoom_plus_button = memnew(Button);
  2330. zoom_plus_button->set_theme_type_variation("FlatButton");
  2331. zoom_plus_button->set_visible(show_zoom_buttons);
  2332. zoom_plus_button->set_tooltip_text(ETR("Zoom In"));
  2333. zoom_plus_button->set_focus_mode(FOCUS_NONE);
  2334. menu_hbox->add_child(zoom_plus_button);
  2335. zoom_plus_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_zoom_plus));
  2336. // Grid controls.
  2337. toggle_grid_button = memnew(Button);
  2338. toggle_grid_button->set_theme_type_variation("FlatButton");
  2339. toggle_grid_button->set_visible(show_grid_buttons);
  2340. toggle_grid_button->set_toggle_mode(true);
  2341. toggle_grid_button->set_pressed(true);
  2342. toggle_grid_button->set_tooltip_text(ETR("Toggle the visual grid."));
  2343. toggle_grid_button->set_focus_mode(FOCUS_NONE);
  2344. menu_hbox->add_child(toggle_grid_button);
  2345. toggle_grid_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_show_grid_toggled));
  2346. toggle_snapping_button = memnew(Button);
  2347. toggle_snapping_button->set_theme_type_variation("FlatButton");
  2348. toggle_snapping_button->set_visible(show_grid_buttons);
  2349. toggle_snapping_button->set_toggle_mode(true);
  2350. toggle_snapping_button->set_tooltip_text(ETR("Toggle snapping to the grid."));
  2351. toggle_snapping_button->set_pressed(snapping_enabled);
  2352. toggle_snapping_button->set_focus_mode(FOCUS_NONE);
  2353. menu_hbox->add_child(toggle_snapping_button);
  2354. toggle_snapping_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_snapping_toggled));
  2355. snapping_distance_spinbox = memnew(SpinBox);
  2356. snapping_distance_spinbox->set_visible(show_grid_buttons);
  2357. snapping_distance_spinbox->set_min(GRID_MIN_SNAPPING_DISTANCE);
  2358. snapping_distance_spinbox->set_max(GRID_MAX_SNAPPING_DISTANCE);
  2359. snapping_distance_spinbox->set_step(1);
  2360. snapping_distance_spinbox->set_value(snapping_distance);
  2361. snapping_distance_spinbox->set_tooltip_text(ETR("Change the snapping distance."));
  2362. menu_hbox->add_child(snapping_distance_spinbox);
  2363. snapping_distance_spinbox->connect("value_changed", callable_mp(this, &GraphEdit::_snapping_distance_changed));
  2364. // Extra controls.
  2365. minimap_button = memnew(Button);
  2366. minimap_button->set_theme_type_variation("FlatButton");
  2367. minimap_button->set_visible(show_minimap_button);
  2368. minimap_button->set_toggle_mode(true);
  2369. minimap_button->set_tooltip_text(ETR("Toggle the graph minimap."));
  2370. minimap_button->set_pressed(show_grid);
  2371. minimap_button->set_focus_mode(FOCUS_NONE);
  2372. menu_hbox->add_child(minimap_button);
  2373. minimap_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_minimap_toggled));
  2374. arrange_button = memnew(Button);
  2375. arrange_button->set_theme_type_variation("FlatButton");
  2376. arrange_button->set_visible(show_arrange_button);
  2377. arrange_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::arrange_nodes));
  2378. arrange_button->set_focus_mode(FOCUS_NONE);
  2379. menu_hbox->add_child(arrange_button);
  2380. arrange_button->set_tooltip_text(ETR("Automatically arrange selected nodes."));
  2381. // Minimap.
  2382. const Vector2 minimap_size = Vector2(240, 160);
  2383. const float minimap_opacity = 0.65;
  2384. minimap = memnew(GraphEditMinimap(this));
  2385. top_layer->add_child(minimap);
  2386. minimap->set_name("_minimap");
  2387. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  2388. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  2389. minimap->set_custom_minimum_size(Vector2(50, 50));
  2390. minimap->set_size(minimap_size);
  2391. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  2392. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  2393. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  2394. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2395. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2396. minimap->connect(SceneStringName(draw), callable_mp(this, &GraphEdit::_minimap_draw));
  2397. set_clip_contents(true);
  2398. arranger = Ref<GraphEditArranger>(memnew(GraphEditArranger(this)));
  2399. }