2
0

graph_edit.cpp 122 KB

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