graph_edit.cpp 122 KB

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