control.cpp 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467
  1. /**************************************************************************/
  2. /* control.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 "control.h"
  31. #include "control.compat.inc"
  32. #include "container.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/input/input_map.h"
  35. #include "core/os/os.h"
  36. #include "core/string/string_builder.h"
  37. #include "scene/gui/scroll_container.h"
  38. #include "scene/main/canvas_layer.h"
  39. #include "scene/main/window.h"
  40. #include "scene/theme/theme_db.h"
  41. #include "scene/theme/theme_owner.h"
  42. #include "servers/rendering/rendering_server.h"
  43. #include "servers/text/text_server.h"
  44. #ifdef TOOLS_ENABLED
  45. #include "editor/scene/gui/control_editor_plugin.h"
  46. #endif // TOOLS_ENABLED
  47. // Editor plugin interoperability.
  48. // TODO: Decouple controls from their editor plugin and get rid of this.
  49. #ifdef TOOLS_ENABLED
  50. Dictionary Control::_edit_get_state() const {
  51. Dictionary s;
  52. s["rotation"] = get_rotation();
  53. s["scale"] = get_scale();
  54. s["pivot"] = get_pivot_offset();
  55. s["pivot_ratio"] = get_pivot_offset_ratio();
  56. Array anchors = { get_anchor(SIDE_LEFT), get_anchor(SIDE_TOP), get_anchor(SIDE_RIGHT), get_anchor(SIDE_BOTTOM) };
  57. s["anchors"] = anchors;
  58. Array offsets = { get_offset(SIDE_LEFT), get_offset(SIDE_TOP), get_offset(SIDE_RIGHT), get_offset(SIDE_BOTTOM) };
  59. s["offsets"] = offsets;
  60. s["layout_mode"] = _get_layout_mode();
  61. s["anchors_layout_preset"] = _get_anchors_layout_preset();
  62. return s;
  63. }
  64. void Control::_edit_set_state(const Dictionary &p_state) {
  65. ERR_FAIL_COND(p_state.is_empty() ||
  66. !p_state.has("rotation") || !p_state.has("scale") ||
  67. !p_state.has("pivot") || !p_state.has("pivot_ratio") || !p_state.has("anchors") || !p_state.has("offsets") ||
  68. !p_state.has("layout_mode") || !p_state.has("anchors_layout_preset"));
  69. Dictionary state = p_state;
  70. set_rotation(state["rotation"]);
  71. set_scale(state["scale"]);
  72. set_pivot_offset(state["pivot"]);
  73. set_pivot_offset_ratio(state["pivot_ratio"]);
  74. Array anchors = state["anchors"];
  75. // If anchors are not in their default position, force the anchor layout mode in place of position.
  76. LayoutMode _layout = (LayoutMode)(int)state["layout_mode"];
  77. if (_layout == LayoutMode::LAYOUT_MODE_POSITION) {
  78. bool anchors_mode = ((real_t)anchors[0] != 0.0 || (real_t)anchors[1] != 0.0 || (real_t)anchors[2] != 0.0 || (real_t)anchors[3] != 0.0);
  79. if (anchors_mode) {
  80. _layout = LayoutMode::LAYOUT_MODE_ANCHORS;
  81. }
  82. }
  83. _set_layout_mode(_layout);
  84. if (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED) {
  85. _set_anchors_layout_preset((int)state["anchors_layout_preset"]);
  86. }
  87. data.anchor[SIDE_LEFT] = anchors[0];
  88. data.anchor[SIDE_TOP] = anchors[1];
  89. data.anchor[SIDE_RIGHT] = anchors[2];
  90. data.anchor[SIDE_BOTTOM] = anchors[3];
  91. Array offsets = state["offsets"];
  92. data.offset[SIDE_LEFT] = offsets[0];
  93. data.offset[SIDE_TOP] = offsets[1];
  94. data.offset[SIDE_RIGHT] = offsets[2];
  95. data.offset[SIDE_BOTTOM] = offsets[3];
  96. _size_changed();
  97. }
  98. void Control::_edit_set_position(const Point2 &p_position) {
  99. ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
  100. set_position(p_position, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled() && get_parent_control());
  101. }
  102. Point2 Control::_edit_get_position() const {
  103. return get_position();
  104. }
  105. void Control::_edit_set_scale(const Size2 &p_scale) {
  106. set_scale(p_scale);
  107. }
  108. Size2 Control::_edit_get_scale() const {
  109. return data.scale;
  110. }
  111. void Control::_edit_set_rect(const Rect2 &p_edit_rect) {
  112. ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
  113. set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
  114. set_size(p_edit_rect.size, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
  115. }
  116. void Control::_edit_set_rotation(real_t p_rotation) {
  117. set_rotation(p_rotation);
  118. }
  119. real_t Control::_edit_get_rotation() const {
  120. return get_rotation();
  121. }
  122. bool Control::_edit_use_rotation() const {
  123. return true;
  124. }
  125. void Control::_edit_set_pivot(const Point2 &p_pivot) {
  126. Vector2 delta_pivot = p_pivot - get_pivot_offset();
  127. Vector2 move = Vector2((std::cos(data.rotation) - 1.0) * delta_pivot.x - std::sin(data.rotation) * delta_pivot.y, std::sin(data.rotation) * delta_pivot.x + (std::cos(data.rotation) - 1.0) * delta_pivot.y);
  128. set_position(get_position() + move);
  129. set_pivot_offset(p_pivot);
  130. set_pivot_offset_ratio(Vector2());
  131. }
  132. Point2 Control::_edit_get_pivot() const {
  133. return get_combined_pivot_offset();
  134. }
  135. bool Control::_edit_use_pivot() const {
  136. return true;
  137. }
  138. Size2 Control::_edit_get_minimum_size() const {
  139. return get_combined_minimum_size();
  140. }
  141. #endif // TOOLS_ENABLED
  142. #ifdef DEBUG_ENABLED
  143. Rect2 Control::_edit_get_rect() const {
  144. return Rect2(Point2(), get_size());
  145. }
  146. bool Control::_edit_use_rect() const {
  147. return true;
  148. }
  149. #endif // DEBUG_ENABLED
  150. void Control::reparent(RequiredParam<Node> p_parent, bool p_keep_global_transform) {
  151. ERR_MAIN_THREAD_GUARD;
  152. if (p_keep_global_transform) {
  153. Transform2D temp = get_global_transform();
  154. Node::reparent(p_parent);
  155. set_global_position(temp.get_origin());
  156. } else {
  157. Node::reparent(p_parent);
  158. }
  159. }
  160. // Editor integration.
  161. int Control::root_layout_direction = 0;
  162. void Control::set_root_layout_direction(int p_root_dir) {
  163. root_layout_direction = p_root_dir;
  164. }
  165. #ifdef TOOLS_ENABLED
  166. void Control::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  167. ERR_READ_THREAD_GUARD;
  168. if (p_idx == 0) {
  169. const String pf = p_function;
  170. Theme::DataType type = Theme::DATA_TYPE_MAX;
  171. if (pf == "add_theme_color_override" || pf == "has_theme_color" || pf == "has_theme_color_override" || pf == "get_theme_color" || pf == "remove_theme_color_override") {
  172. type = Theme::DATA_TYPE_COLOR;
  173. } else if (pf == "add_theme_constant_override" || pf == "has_theme_constant" || pf == "has_theme_constant_override" || pf == "get_theme_constant" || pf == "remove_theme_constant_override") {
  174. type = Theme::DATA_TYPE_CONSTANT;
  175. } else if (pf == "add_theme_font_override" || pf == "has_theme_font" || pf == "has_theme_font_override" || pf == "get_theme_font" || pf == "remove_theme_font_override") {
  176. type = Theme::DATA_TYPE_FONT;
  177. } else if (pf == "add_theme_font_size_override" || pf == "has_theme_font_size" || pf == "has_theme_font_size_override" || pf == "get_theme_font_size" || pf == "remove_theme_font_size_override") {
  178. type = Theme::DATA_TYPE_FONT_SIZE;
  179. } else if (pf == "add_theme_icon_override" || pf == "has_theme_icon" || pf == "has_theme_icon_override" || pf == "get_theme_icon" || pf == "remove_theme_icon_override") {
  180. type = Theme::DATA_TYPE_ICON;
  181. } else if (pf == "add_theme_stylebox_override" || pf == "has_theme_stylebox" || pf == "has_theme_stylebox_override" || pf == "get_theme_stylebox" || pf == "remove_theme_stylebox_override") {
  182. type = Theme::DATA_TYPE_STYLEBOX;
  183. }
  184. if (type != Theme::DATA_TYPE_MAX) {
  185. List<ThemeDB::ThemeItemBind> theme_items;
  186. ThemeDB::get_singleton()->get_class_items(get_class_name(), &theme_items, true, type);
  187. List<StringName> sn;
  188. for (const ThemeDB::ThemeItemBind &E : theme_items) {
  189. if (E.data_type == type) {
  190. sn.push_back(E.item_name);
  191. }
  192. }
  193. sn.sort_custom<StringName::AlphCompare>();
  194. for (const StringName &name : sn) {
  195. r_options->push_back(String(name).quote());
  196. }
  197. }
  198. }
  199. CanvasItem::get_argument_options(p_function, p_idx, r_options);
  200. }
  201. #endif // TOOLS_ENABLED
  202. PackedStringArray Control::get_configuration_warnings() const {
  203. ERR_READ_THREAD_GUARD_V(PackedStringArray());
  204. PackedStringArray warnings = CanvasItem::get_configuration_warnings();
  205. if (data.mouse_filter == MOUSE_FILTER_IGNORE && !data.tooltip.is_empty()) {
  206. warnings.push_back(RTR("The Hint Tooltip won't be displayed as the control's Mouse Filter is set to \"Ignore\". To solve this, set the Mouse Filter to \"Stop\" or \"Pass\"."));
  207. }
  208. return warnings;
  209. }
  210. PackedStringArray Control::get_accessibility_configuration_warnings() const {
  211. ERR_READ_THREAD_GUARD_V(PackedStringArray());
  212. PackedStringArray warnings = Node::get_accessibility_configuration_warnings();
  213. String ac_name = get_accessibility_name().strip_edges();
  214. if (ac_name.is_empty()) {
  215. warnings.push_back(RTR("Accessibility Name must not be empty, or contain only spaces."));
  216. }
  217. if (ac_name.contains(get_class_name())) {
  218. warnings.push_back(RTR("Accessibility Name must not include Node class name."));
  219. }
  220. for (int i = 0; i < ac_name.length(); i++) {
  221. if (is_control(ac_name[i])) {
  222. warnings.push_back(RTR("Accessibility Name must not include control character."));
  223. break;
  224. }
  225. }
  226. return warnings;
  227. }
  228. bool Control::is_text_field() const {
  229. ERR_READ_THREAD_GUARD_V(false);
  230. return false;
  231. }
  232. // Dynamic properties.
  233. String Control::properties_managed_by_container[] = {
  234. "offset_left",
  235. "offset_top",
  236. "offset_right",
  237. "offset_bottom",
  238. "anchor_left",
  239. "anchor_top",
  240. "anchor_right",
  241. "anchor_bottom",
  242. "position",
  243. "rotation",
  244. "scale",
  245. "size"
  246. };
  247. bool Control::_set(const StringName &p_name, const Variant &p_value) {
  248. ERR_MAIN_THREAD_GUARD_V(false);
  249. String name = p_name;
  250. if (!name.begins_with("theme_override")) {
  251. return false;
  252. }
  253. if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
  254. if (name.begins_with("theme_override_icons/")) {
  255. String dname = name.get_slicec('/', 1);
  256. if (data.theme_icon_override.has(dname)) {
  257. data.theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  258. }
  259. data.theme_icon_override.erase(dname);
  260. _notify_theme_override_changed();
  261. } else if (name.begins_with("theme_override_styles/")) {
  262. String dname = name.get_slicec('/', 1);
  263. if (data.theme_style_override.has(dname)) {
  264. data.theme_style_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  265. }
  266. data.theme_style_override.erase(dname);
  267. _notify_theme_override_changed();
  268. } else if (name.begins_with("theme_override_fonts/")) {
  269. String dname = name.get_slicec('/', 1);
  270. if (data.theme_font_override.has(dname)) {
  271. data.theme_font_override[dname]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  272. }
  273. data.theme_font_override.erase(dname);
  274. _notify_theme_override_changed();
  275. } else if (name.begins_with("theme_override_font_sizes/")) {
  276. String dname = name.get_slicec('/', 1);
  277. data.theme_font_size_override.erase(dname);
  278. _notify_theme_override_changed();
  279. } else if (name.begins_with("theme_override_colors/")) {
  280. String dname = name.get_slicec('/', 1);
  281. data.theme_color_override.erase(dname);
  282. _notify_theme_override_changed();
  283. } else if (name.begins_with("theme_override_constants/")) {
  284. String dname = name.get_slicec('/', 1);
  285. data.theme_constant_override.erase(dname);
  286. _notify_theme_override_changed();
  287. } else {
  288. return false;
  289. }
  290. } else {
  291. if (name.begins_with("theme_override_icons/")) {
  292. String dname = name.get_slicec('/', 1);
  293. add_theme_icon_override(dname, p_value);
  294. } else if (name.begins_with("theme_override_styles/")) {
  295. String dname = name.get_slicec('/', 1);
  296. add_theme_style_override(dname, p_value);
  297. } else if (name.begins_with("theme_override_fonts/")) {
  298. String dname = name.get_slicec('/', 1);
  299. add_theme_font_override(dname, p_value);
  300. } else if (name.begins_with("theme_override_font_sizes/")) {
  301. String dname = name.get_slicec('/', 1);
  302. add_theme_font_size_override(dname, p_value);
  303. } else if (name.begins_with("theme_override_colors/")) {
  304. String dname = name.get_slicec('/', 1);
  305. add_theme_color_override(dname, p_value);
  306. } else if (name.begins_with("theme_override_constants/")) {
  307. String dname = name.get_slicec('/', 1);
  308. add_theme_constant_override(dname, p_value);
  309. } else {
  310. return false;
  311. }
  312. }
  313. return true;
  314. }
  315. bool Control::_get(const StringName &p_name, Variant &r_ret) const {
  316. ERR_MAIN_THREAD_GUARD_V(false);
  317. String sname = p_name;
  318. if (!sname.begins_with("theme_override")) {
  319. return false;
  320. }
  321. if (sname.begins_with("theme_override_icons/")) {
  322. String name = sname.get_slicec('/', 1);
  323. r_ret = data.theme_icon_override.has(name) ? Variant(data.theme_icon_override[name]) : Variant();
  324. } else if (sname.begins_with("theme_override_styles/")) {
  325. String name = sname.get_slicec('/', 1);
  326. r_ret = data.theme_style_override.has(name) ? Variant(data.theme_style_override[name]) : Variant();
  327. } else if (sname.begins_with("theme_override_fonts/")) {
  328. String name = sname.get_slicec('/', 1);
  329. r_ret = data.theme_font_override.has(name) ? Variant(data.theme_font_override[name]) : Variant();
  330. } else if (sname.begins_with("theme_override_font_sizes/")) {
  331. String name = sname.get_slicec('/', 1);
  332. r_ret = data.theme_font_size_override.has(name) ? Variant(data.theme_font_size_override[name]) : Variant();
  333. } else if (sname.begins_with("theme_override_colors/")) {
  334. String name = sname.get_slicec('/', 1);
  335. r_ret = data.theme_color_override.has(name) ? Variant(data.theme_color_override[name]) : Variant();
  336. } else if (sname.begins_with("theme_override_constants/")) {
  337. String name = sname.get_slicec('/', 1);
  338. r_ret = data.theme_constant_override.has(name) ? Variant(data.theme_constant_override[name]) : Variant();
  339. } else {
  340. return false;
  341. }
  342. return true;
  343. }
  344. void Control::_get_property_list(List<PropertyInfo> *p_list) const {
  345. ERR_MAIN_THREAD_GUARD;
  346. List<ThemeDB::ThemeItemBind> theme_items;
  347. ThemeDB::get_singleton()->get_class_items(get_class_name(), &theme_items, true);
  348. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
  349. for (const ThemeDB::ThemeItemBind &E : theme_items) {
  350. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  351. switch (E.data_type) {
  352. case Theme::DATA_TYPE_COLOR: {
  353. if (data.theme_color_override.has(E.item_name)) {
  354. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  355. }
  356. p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E.item_name, PROPERTY_HINT_NONE, "", usage));
  357. } break;
  358. case Theme::DATA_TYPE_CONSTANT: {
  359. if (data.theme_constant_override.has(E.item_name)) {
  360. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  361. }
  362. p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E.item_name, PROPERTY_HINT_RANGE, "-16384,16384", usage));
  363. } break;
  364. case Theme::DATA_TYPE_FONT: {
  365. if (data.theme_font_override.has(E.item_name)) {
  366. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  367. }
  368. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E.item_name, PROPERTY_HINT_RESOURCE_TYPE, "Font", usage));
  369. } break;
  370. case Theme::DATA_TYPE_FONT_SIZE: {
  371. if (data.theme_font_size_override.has(E.item_name)) {
  372. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  373. }
  374. p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_font_sizes") + String("/") + E.item_name, PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px", usage));
  375. } break;
  376. case Theme::DATA_TYPE_ICON: {
  377. if (data.theme_icon_override.has(E.item_name)) {
  378. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  379. }
  380. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E.item_name, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", usage));
  381. } break;
  382. case Theme::DATA_TYPE_STYLEBOX: {
  383. if (data.theme_style_override.has(E.item_name)) {
  384. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  385. }
  386. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E.item_name, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", usage));
  387. } break;
  388. default: {
  389. // Silences warning.
  390. } break;
  391. }
  392. }
  393. }
  394. void Control::_validate_property(PropertyInfo &p_property) const {
  395. // Update theme type variation options.
  396. if (Engine::get_singleton()->is_editor_hint() && p_property.name == "theme_type_variation") {
  397. List<StringName> names;
  398. ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
  399. // Iterate to find all themes.
  400. Control *tmp_control = Object::cast_to<Control>(get_parent());
  401. Window *tmp_window = Object::cast_to<Window>(get_parent());
  402. while (tmp_control || tmp_window) {
  403. // We go up and any non Control/Window will break the chain.
  404. if (tmp_control) {
  405. if (tmp_control->get_theme().is_valid()) {
  406. tmp_control->get_theme()->get_type_variation_list(get_class_name(), &names);
  407. }
  408. tmp_window = Object::cast_to<Window>(tmp_control->get_parent());
  409. tmp_control = Object::cast_to<Control>(tmp_control->get_parent());
  410. } else { // Window.
  411. if (tmp_window->get_theme().is_valid()) {
  412. tmp_window->get_theme()->get_type_variation_list(get_class_name(), &names);
  413. }
  414. tmp_control = Object::cast_to<Control>(tmp_window->get_parent());
  415. tmp_window = Object::cast_to<Window>(tmp_window->get_parent());
  416. }
  417. }
  418. if (get_theme().is_valid()) {
  419. get_theme()->get_type_variation_list(get_class_name(), &names);
  420. }
  421. if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
  422. ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
  423. }
  424. names.sort_custom<StringName::AlphCompare>();
  425. Vector<StringName> unique_names;
  426. String hint_string;
  427. for (const StringName &E : names) {
  428. // Skip duplicate values.
  429. if (unique_names.has(E)) {
  430. continue;
  431. }
  432. hint_string += String(E) + ",";
  433. unique_names.append(E);
  434. }
  435. p_property.hint_string = hint_string;
  436. }
  437. if (Engine::get_singleton()->is_editor_hint() && p_property.name == "mouse_force_pass_scroll_events") {
  438. // Disable force pass if the control is not stopping the event.
  439. if (data.mouse_filter != MOUSE_FILTER_STOP) {
  440. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  441. }
  442. }
  443. if (Engine::get_singleton()->is_editor_hint() && p_property.name == "scale") {
  444. p_property.hint = PROPERTY_HINT_LINK;
  445. }
  446. // Validate which positioning properties should be displayed depending on the parent and the layout mode.
  447. Control *parent_control = get_parent_control();
  448. if (Engine::get_singleton()->is_editor_hint() && !parent_control) {
  449. // If there is no parent control, display both anchor and container options.
  450. // Set the layout mode to be disabled with the proper value.
  451. if (p_property.name == "layout_mode") {
  452. p_property.hint_string = "Position,Anchors,Container,Uncontrolled";
  453. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  454. }
  455. // Use the layout mode to display or hide advanced anchoring properties.
  456. bool use_custom_anchors = _get_anchors_layout_preset() == -1; // Custom "preset".
  457. if (!use_custom_anchors && (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_"))) {
  458. p_property.usage ^= PROPERTY_USAGE_EDITOR;
  459. }
  460. } else if (Object::cast_to<Container>(parent_control)) {
  461. // If the parent is a container, display only container-related properties.
  462. if (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_") || p_property.name == "anchors_preset") {
  463. p_property.usage ^= PROPERTY_USAGE_DEFAULT;
  464. } else if (p_property.name == "position" || p_property.name == "rotation" || p_property.name == "scale" || p_property.name == "size" || p_property.name == "pivot_offset" || p_property.name == "pivot_offset_ratio") {
  465. p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY;
  466. } else if (Engine::get_singleton()->is_editor_hint() && p_property.name == "layout_mode") {
  467. // Set the layout mode to be disabled with the proper value.
  468. p_property.hint_string = "Position,Anchors,Container,Uncontrolled";
  469. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  470. } else if (Engine::get_singleton()->is_editor_hint() && (p_property.name == "size_flags_horizontal" || p_property.name == "size_flags_vertical")) {
  471. // Filter allowed size flags based on the parent container configuration.
  472. Container *parent_container = Object::cast_to<Container>(parent_control);
  473. Vector<int> size_flags;
  474. if (p_property.name == "size_flags_horizontal") {
  475. size_flags = parent_container->get_allowed_size_flags_horizontal();
  476. } else if (p_property.name == "size_flags_vertical") {
  477. size_flags = parent_container->get_allowed_size_flags_vertical();
  478. }
  479. // Enforce the order of the options, regardless of what the container provided.
  480. String hint_string;
  481. if (size_flags.has(SIZE_FILL)) {
  482. hint_string += "Fill:1";
  483. }
  484. if (size_flags.has(SIZE_EXPAND)) {
  485. if (!hint_string.is_empty()) {
  486. hint_string += ",";
  487. }
  488. hint_string += "Expand:2";
  489. }
  490. if (size_flags.has(SIZE_SHRINK_CENTER)) {
  491. if (!hint_string.is_empty()) {
  492. hint_string += ",";
  493. }
  494. hint_string += "Shrink Center:4";
  495. }
  496. if (size_flags.has(SIZE_SHRINK_END)) {
  497. if (!hint_string.is_empty()) {
  498. hint_string += ",";
  499. }
  500. hint_string += "Shrink End:8";
  501. }
  502. if (hint_string.is_empty()) {
  503. p_property.hint_string = "";
  504. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  505. } else {
  506. p_property.hint_string = hint_string;
  507. }
  508. }
  509. } else if (Engine::get_singleton()->is_editor_hint()) {
  510. // If the parent is a non-container control, display only anchoring-related properties.
  511. if (p_property.name.begins_with("size_flags_")) {
  512. p_property.usage ^= PROPERTY_USAGE_EDITOR;
  513. } else if (p_property.name == "layout_mode") {
  514. // Set the layout mode to be enabled with proper options.
  515. p_property.hint_string = "Position,Anchors";
  516. }
  517. // Use the layout mode to display or hide advanced anchoring properties.
  518. LayoutMode _layout = _get_layout_mode();
  519. bool use_anchors = (_layout == LayoutMode::LAYOUT_MODE_ANCHORS || _layout == LayoutMode::LAYOUT_MODE_UNCONTROLLED);
  520. if (!use_anchors && p_property.name == "anchors_preset") {
  521. p_property.usage ^= PROPERTY_USAGE_EDITOR;
  522. }
  523. bool use_custom_anchors = use_anchors && _get_anchors_layout_preset() == -1; // Custom "preset".
  524. if (!use_custom_anchors && (p_property.name.begins_with("anchor_") || p_property.name.begins_with("offset_") || p_property.name.begins_with("grow_"))) {
  525. p_property.usage ^= PROPERTY_USAGE_EDITOR;
  526. }
  527. }
  528. if (!Engine::get_singleton()->is_editor_hint()) {
  529. return;
  530. }
  531. // Disable the property if it's managed by the parent container.
  532. if (!Object::cast_to<Container>(parent_control)) {
  533. return;
  534. }
  535. bool property_is_managed_by_container = false;
  536. for (unsigned i = 0; i < properties_managed_by_container_count; i++) {
  537. property_is_managed_by_container = properties_managed_by_container[i] == p_property.name;
  538. if (property_is_managed_by_container) {
  539. break;
  540. }
  541. }
  542. if (property_is_managed_by_container) {
  543. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  544. }
  545. }
  546. bool Control::_property_can_revert(const StringName &p_name) const {
  547. if (p_name == "layout_mode" || p_name == "anchors_preset") {
  548. return true;
  549. }
  550. return false;
  551. }
  552. bool Control::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  553. if (p_name == "layout_mode") {
  554. r_property = _get_default_layout_mode();
  555. return true;
  556. } else if (p_name == "anchors_preset") {
  557. r_property = LayoutPreset::PRESET_TOP_LEFT;
  558. return true;
  559. }
  560. return false;
  561. }
  562. // Global relations.
  563. Control *Control::get_parent_control() const {
  564. ERR_READ_THREAD_GUARD_V(nullptr);
  565. return data.parent_control;
  566. }
  567. Window *Control::get_parent_window() const {
  568. ERR_READ_THREAD_GUARD_V(nullptr);
  569. return data.parent_window;
  570. }
  571. Control *Control::get_root_parent_control() const {
  572. ERR_READ_THREAD_GUARD_V(nullptr);
  573. const CanvasItem *ci = this;
  574. const Control *root = this;
  575. while (ci) {
  576. const Control *c = Object::cast_to<Control>(ci);
  577. if (c) {
  578. root = c;
  579. }
  580. ci = ci->get_parent_item();
  581. }
  582. return const_cast<Control *>(root);
  583. }
  584. Rect2 Control::get_parent_anchorable_rect() const {
  585. ERR_READ_THREAD_GUARD_V(Rect2());
  586. if (!is_inside_tree()) {
  587. return Rect2();
  588. }
  589. Rect2 parent_rect;
  590. if (data.parent_canvas_item) {
  591. parent_rect = data.parent_canvas_item->get_anchorable_rect();
  592. } else {
  593. #ifdef TOOLS_ENABLED
  594. Node *edited_scene_root = get_tree()->get_edited_scene_root();
  595. Node *scene_root_parent = edited_scene_root ? edited_scene_root->get_parent() : nullptr;
  596. if (scene_root_parent && get_viewport() == scene_root_parent->get_viewport()) {
  597. parent_rect.size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
  598. } else {
  599. parent_rect = get_viewport()->get_visible_rect();
  600. }
  601. #else
  602. parent_rect = get_viewport()->get_visible_rect();
  603. #endif // TOOLS_ENABLED
  604. }
  605. return parent_rect;
  606. }
  607. Size2 Control::get_parent_area_size() const {
  608. ERR_READ_THREAD_GUARD_V(Size2());
  609. return get_parent_anchorable_rect().size;
  610. }
  611. // Positioning and sizing.
  612. Transform2D Control::_get_internal_transform() const {
  613. // T(pivot_offset) * R(rotation) * S(scale) * T(-pivot_offset)
  614. Transform2D xform(data.rotation, data.scale, 0.0f, get_combined_pivot_offset());
  615. xform.translate_local(-get_combined_pivot_offset());
  616. return xform;
  617. }
  618. void Control::_update_canvas_item_transform() {
  619. Transform2D xform = _get_internal_transform();
  620. xform[2] += get_position();
  621. // We use a little workaround to avoid flickering when moving the pivot with _edit_set_pivot()
  622. if (is_inside_tree() && Math::abs(Math::sin(data.rotation * 4.0f)) < 0.00001f && get_viewport()->is_snap_controls_to_pixels_enabled()) {
  623. xform[2] = (xform[2] + Vector2(0.5, 0.5)).floor();
  624. }
  625. RenderingServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), xform);
  626. }
  627. Transform2D Control::get_transform() const {
  628. ERR_READ_THREAD_GUARD_V(Transform2D());
  629. Transform2D xform = _get_internal_transform();
  630. xform[2] += get_position();
  631. return xform;
  632. }
  633. void Control::_top_level_changed_on_parent() {
  634. // Update root control status.
  635. _notification(NOTIFICATION_EXIT_CANVAS);
  636. _notification(NOTIFICATION_ENTER_CANVAS);
  637. }
  638. /// Anchors and offsets.
  639. void Control::_set_anchor(Side p_side, real_t p_anchor) {
  640. set_anchor(p_side, p_anchor);
  641. }
  642. void Control::set_anchor(Side p_side, real_t p_anchor, bool p_keep_offset, bool p_push_opposite_anchor) {
  643. ERR_MAIN_THREAD_GUARD;
  644. ERR_FAIL_INDEX((int)p_side, 4);
  645. Rect2 parent_rect = get_parent_anchorable_rect();
  646. real_t parent_range = (p_side == SIDE_LEFT || p_side == SIDE_RIGHT) ? parent_rect.size.x : parent_rect.size.y;
  647. real_t previous_pos = data.offset[p_side] + data.anchor[p_side] * parent_range;
  648. real_t previous_opposite_pos = data.offset[(p_side + 2) % 4] + data.anchor[(p_side + 2) % 4] * parent_range;
  649. data.anchor[p_side] = p_anchor;
  650. if (((p_side == SIDE_LEFT || p_side == SIDE_TOP) && data.anchor[p_side] > data.anchor[(p_side + 2) % 4]) ||
  651. ((p_side == SIDE_RIGHT || p_side == SIDE_BOTTOM) && data.anchor[p_side] < data.anchor[(p_side + 2) % 4])) {
  652. if (p_push_opposite_anchor) {
  653. data.anchor[(p_side + 2) % 4] = data.anchor[p_side];
  654. } else {
  655. data.anchor[p_side] = data.anchor[(p_side + 2) % 4];
  656. }
  657. }
  658. if (!p_keep_offset) {
  659. data.offset[p_side] = previous_pos - data.anchor[p_side] * parent_range;
  660. if (p_push_opposite_anchor) {
  661. data.offset[(p_side + 2) % 4] = previous_opposite_pos - data.anchor[(p_side + 2) % 4] * parent_range;
  662. }
  663. }
  664. if (is_inside_tree()) {
  665. _size_changed();
  666. }
  667. queue_redraw();
  668. }
  669. real_t Control::get_anchor(Side p_side) const {
  670. ERR_READ_THREAD_GUARD_V(0);
  671. ERR_FAIL_INDEX_V(int(p_side), 4, 0.0);
  672. return data.anchor[p_side];
  673. }
  674. void Control::set_offset(Side p_side, real_t p_value) {
  675. ERR_MAIN_THREAD_GUARD;
  676. ERR_FAIL_INDEX((int)p_side, 4);
  677. if (data.offset[p_side] == p_value) {
  678. return;
  679. }
  680. data.offset[p_side] = p_value;
  681. _size_changed();
  682. }
  683. real_t Control::get_offset(Side p_side) const {
  684. ERR_READ_THREAD_GUARD_V(0);
  685. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  686. return data.offset[p_side];
  687. }
  688. void Control::set_anchor_and_offset(Side p_side, real_t p_anchor, real_t p_pos, bool p_push_opposite_anchor) {
  689. ERR_MAIN_THREAD_GUARD;
  690. set_anchor(p_side, p_anchor, false, p_push_opposite_anchor);
  691. set_offset(p_side, p_pos);
  692. }
  693. void Control::set_begin(const Point2 &p_point) {
  694. ERR_MAIN_THREAD_GUARD;
  695. ERR_FAIL_COND(!std::isfinite(p_point.x) || !std::isfinite(p_point.y));
  696. if (data.offset[0] == p_point.x && data.offset[1] == p_point.y) {
  697. return;
  698. }
  699. data.offset[0] = p_point.x;
  700. data.offset[1] = p_point.y;
  701. _size_changed();
  702. }
  703. Point2 Control::get_begin() const {
  704. ERR_READ_THREAD_GUARD_V(Vector2());
  705. return Point2(data.offset[0], data.offset[1]);
  706. }
  707. void Control::set_end(const Point2 &p_point) {
  708. ERR_MAIN_THREAD_GUARD;
  709. if (data.offset[2] == p_point.x && data.offset[3] == p_point.y) {
  710. return;
  711. }
  712. data.offset[2] = p_point.x;
  713. data.offset[3] = p_point.y;
  714. _size_changed();
  715. }
  716. Point2 Control::get_end() const {
  717. ERR_READ_THREAD_GUARD_V(Point2());
  718. return Point2(data.offset[2], data.offset[3]);
  719. }
  720. void Control::set_h_grow_direction(GrowDirection p_direction) {
  721. ERR_MAIN_THREAD_GUARD;
  722. if (data.h_grow == p_direction) {
  723. return;
  724. }
  725. ERR_FAIL_INDEX((int)p_direction, 3);
  726. data.h_grow = p_direction;
  727. _size_changed();
  728. }
  729. Control::GrowDirection Control::get_h_grow_direction() const {
  730. ERR_READ_THREAD_GUARD_V(GROW_DIRECTION_BEGIN);
  731. return data.h_grow;
  732. }
  733. void Control::set_v_grow_direction(GrowDirection p_direction) {
  734. ERR_MAIN_THREAD_GUARD;
  735. if (data.v_grow == p_direction) {
  736. return;
  737. }
  738. ERR_FAIL_INDEX((int)p_direction, 3);
  739. data.v_grow = p_direction;
  740. _size_changed();
  741. }
  742. Control::GrowDirection Control::get_v_grow_direction() const {
  743. ERR_READ_THREAD_GUARD_V(GROW_DIRECTION_BEGIN);
  744. return data.v_grow;
  745. }
  746. void Control::_compute_anchors(Rect2 p_rect, const real_t p_offsets[4], real_t (&r_anchors)[4]) {
  747. Size2 parent_rect_size = get_parent_anchorable_rect().size;
  748. ERR_FAIL_COND(parent_rect_size.x == 0.0);
  749. ERR_FAIL_COND(parent_rect_size.y == 0.0);
  750. real_t x = p_rect.position.x;
  751. if (is_layout_rtl()) {
  752. x = parent_rect_size.x - x - p_rect.size.x;
  753. }
  754. r_anchors[0] = (x - p_offsets[0]) / parent_rect_size.x;
  755. r_anchors[1] = (p_rect.position.y - p_offsets[1]) / parent_rect_size.y;
  756. r_anchors[2] = (x + p_rect.size.x - p_offsets[2]) / parent_rect_size.x;
  757. r_anchors[3] = (p_rect.position.y + p_rect.size.y - p_offsets[3]) / parent_rect_size.y;
  758. }
  759. void Control::_compute_offsets(Rect2 p_rect, const real_t p_anchors[4], real_t (&r_offsets)[4]) {
  760. Size2 parent_rect_size = get_parent_anchorable_rect().size;
  761. real_t x = p_rect.position.x;
  762. if (is_layout_rtl()) {
  763. x = parent_rect_size.x - x - p_rect.size.x;
  764. }
  765. r_offsets[0] = x - (p_anchors[0] * parent_rect_size.x);
  766. r_offsets[1] = p_rect.position.y - (p_anchors[1] * parent_rect_size.y);
  767. r_offsets[2] = x + p_rect.size.x - (p_anchors[2] * parent_rect_size.x);
  768. r_offsets[3] = p_rect.position.y + p_rect.size.y - (p_anchors[3] * parent_rect_size.y);
  769. }
  770. /// Presets and layout modes.
  771. void Control::_set_layout_mode(LayoutMode p_mode) {
  772. bool list_changed = false;
  773. if (data.stored_layout_mode != p_mode) {
  774. list_changed = true;
  775. data.stored_layout_mode = p_mode;
  776. }
  777. if (data.stored_layout_mode == LayoutMode::LAYOUT_MODE_POSITION) {
  778. data.stored_use_custom_anchors = false;
  779. set_anchors_and_offsets_preset(LayoutPreset::PRESET_TOP_LEFT, LayoutPresetMode::PRESET_MODE_KEEP_SIZE);
  780. set_grow_direction_preset(LayoutPreset::PRESET_TOP_LEFT);
  781. }
  782. if (list_changed) {
  783. notify_property_list_changed();
  784. }
  785. }
  786. void Control::_update_layout_mode() {
  787. LayoutMode computed_layout = _get_layout_mode();
  788. if (data.stored_layout_mode != computed_layout) {
  789. data.stored_layout_mode = computed_layout;
  790. notify_property_list_changed();
  791. }
  792. }
  793. Control::LayoutMode Control::_get_layout_mode() const {
  794. Control *parent_control = get_parent_control();
  795. // In these modes the property is read-only.
  796. if (!parent_control) {
  797. return LayoutMode::LAYOUT_MODE_UNCONTROLLED;
  798. } else if (Object::cast_to<Container>(parent_control)) {
  799. return LayoutMode::LAYOUT_MODE_CONTAINER;
  800. }
  801. // If anchors are not in the top-left position, this is definitely in anchors mode.
  802. if (_get_anchors_layout_preset() != (int)LayoutPreset::PRESET_TOP_LEFT) {
  803. return LayoutMode::LAYOUT_MODE_ANCHORS;
  804. }
  805. // Only position/anchors modes are valid for non-container control parent.
  806. if (data.stored_layout_mode == LayoutMode::LAYOUT_MODE_POSITION || data.stored_layout_mode == LayoutMode::LAYOUT_MODE_ANCHORS) {
  807. return data.stored_layout_mode;
  808. }
  809. // Otherwise fallback to position mode.
  810. return LayoutMode::LAYOUT_MODE_POSITION;
  811. }
  812. Control::LayoutMode Control::_get_default_layout_mode() const {
  813. Control *parent_control = get_parent_control();
  814. // In these modes the property is read-only.
  815. if (!parent_control) {
  816. return LayoutMode::LAYOUT_MODE_UNCONTROLLED;
  817. } else if (Object::cast_to<Container>(parent_control)) {
  818. return LayoutMode::LAYOUT_MODE_CONTAINER;
  819. }
  820. // Otherwise fallback to the position mode.
  821. return LayoutMode::LAYOUT_MODE_POSITION;
  822. }
  823. void Control::_set_anchors_layout_preset(int p_preset) {
  824. if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_UNCONTROLLED && data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
  825. // In other modes the anchor preset is non-operational and shouldn't be set to anything.
  826. return;
  827. }
  828. if (p_preset == -1) {
  829. if (!data.stored_use_custom_anchors) {
  830. data.stored_use_custom_anchors = true;
  831. notify_property_list_changed();
  832. }
  833. return; // Keep settings as is.
  834. }
  835. bool list_changed = false;
  836. if (data.stored_use_custom_anchors) {
  837. list_changed = true;
  838. data.stored_use_custom_anchors = false;
  839. }
  840. LayoutPreset preset = (LayoutPreset)p_preset;
  841. // Set correct anchors.
  842. set_anchors_preset(preset);
  843. // Select correct preset mode.
  844. switch (preset) {
  845. case PRESET_TOP_LEFT:
  846. case PRESET_TOP_RIGHT:
  847. case PRESET_BOTTOM_LEFT:
  848. case PRESET_BOTTOM_RIGHT:
  849. case PRESET_CENTER_LEFT:
  850. case PRESET_CENTER_TOP:
  851. case PRESET_CENTER_RIGHT:
  852. case PRESET_CENTER_BOTTOM:
  853. case PRESET_CENTER:
  854. set_offsets_preset(preset, LayoutPresetMode::PRESET_MODE_KEEP_SIZE);
  855. break;
  856. case PRESET_LEFT_WIDE:
  857. case PRESET_TOP_WIDE:
  858. case PRESET_RIGHT_WIDE:
  859. case PRESET_BOTTOM_WIDE:
  860. case PRESET_VCENTER_WIDE:
  861. case PRESET_HCENTER_WIDE:
  862. case PRESET_FULL_RECT:
  863. set_offsets_preset(preset, LayoutPresetMode::PRESET_MODE_MINSIZE);
  864. break;
  865. }
  866. // Select correct grow directions.
  867. set_grow_direction_preset(preset);
  868. if (list_changed) {
  869. notify_property_list_changed();
  870. }
  871. }
  872. int Control::_get_anchors_layout_preset() const {
  873. // If this is a layout mode that doesn't rely on anchors, avoid excessive checks.
  874. if (data.stored_layout_mode != LayoutMode::LAYOUT_MODE_UNCONTROLLED && data.stored_layout_mode != LayoutMode::LAYOUT_MODE_ANCHORS) {
  875. return LayoutPreset::PRESET_TOP_LEFT;
  876. }
  877. // If the custom preset was selected by user, use it.
  878. if (data.stored_use_custom_anchors) {
  879. return -1;
  880. }
  881. // Check anchors to determine if the current state matches a preset, or not.
  882. float left = get_anchor(SIDE_LEFT);
  883. float right = get_anchor(SIDE_RIGHT);
  884. float top = get_anchor(SIDE_TOP);
  885. float bottom = get_anchor(SIDE_BOTTOM);
  886. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
  887. return (int)LayoutPreset::PRESET_TOP_LEFT;
  888. }
  889. if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
  890. return (int)LayoutPreset::PRESET_TOP_RIGHT;
  891. }
  892. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
  893. return (int)LayoutPreset::PRESET_BOTTOM_LEFT;
  894. }
  895. if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
  896. return (int)LayoutPreset::PRESET_BOTTOM_RIGHT;
  897. }
  898. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == 0.5 && bottom == 0.5) {
  899. return (int)LayoutPreset::PRESET_CENTER_LEFT;
  900. }
  901. if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == 0.5 && bottom == 0.5) {
  902. return (int)LayoutPreset::PRESET_CENTER_RIGHT;
  903. }
  904. if (left == 0.5 && right == 0.5 && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
  905. return (int)LayoutPreset::PRESET_CENTER_TOP;
  906. }
  907. if (left == 0.5 && right == 0.5 && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
  908. return (int)LayoutPreset::PRESET_CENTER_BOTTOM;
  909. }
  910. if (left == 0.5 && right == 0.5 && top == 0.5 && bottom == 0.5) {
  911. return (int)LayoutPreset::PRESET_CENTER;
  912. }
  913. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_BEGIN && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
  914. return (int)LayoutPreset::PRESET_LEFT_WIDE;
  915. }
  916. if (left == (float)ANCHOR_END && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
  917. return (int)LayoutPreset::PRESET_RIGHT_WIDE;
  918. }
  919. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_BEGIN) {
  920. return (int)LayoutPreset::PRESET_TOP_WIDE;
  921. }
  922. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == (float)ANCHOR_END && bottom == (float)ANCHOR_END) {
  923. return (int)LayoutPreset::PRESET_BOTTOM_WIDE;
  924. }
  925. if (left == 0.5 && right == 0.5 && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
  926. return (int)LayoutPreset::PRESET_VCENTER_WIDE;
  927. }
  928. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == 0.5 && bottom == 0.5) {
  929. return (int)LayoutPreset::PRESET_HCENTER_WIDE;
  930. }
  931. if (left == (float)ANCHOR_BEGIN && right == (float)ANCHOR_END && top == (float)ANCHOR_BEGIN && bottom == (float)ANCHOR_END) {
  932. return (int)LayoutPreset::PRESET_FULL_RECT;
  933. }
  934. // Does not match any preset, return "Custom".
  935. return -1;
  936. }
  937. void Control::set_anchors_preset(LayoutPreset p_preset, bool p_keep_offsets) {
  938. ERR_MAIN_THREAD_GUARD;
  939. ERR_FAIL_INDEX((int)p_preset, 16);
  940. //Left
  941. switch (p_preset) {
  942. case PRESET_TOP_LEFT:
  943. case PRESET_BOTTOM_LEFT:
  944. case PRESET_CENTER_LEFT:
  945. case PRESET_TOP_WIDE:
  946. case PRESET_BOTTOM_WIDE:
  947. case PRESET_LEFT_WIDE:
  948. case PRESET_HCENTER_WIDE:
  949. case PRESET_FULL_RECT:
  950. set_anchor(SIDE_LEFT, ANCHOR_BEGIN, p_keep_offsets);
  951. break;
  952. case PRESET_CENTER_TOP:
  953. case PRESET_CENTER_BOTTOM:
  954. case PRESET_CENTER:
  955. case PRESET_VCENTER_WIDE:
  956. set_anchor(SIDE_LEFT, 0.5, p_keep_offsets);
  957. break;
  958. case PRESET_TOP_RIGHT:
  959. case PRESET_BOTTOM_RIGHT:
  960. case PRESET_CENTER_RIGHT:
  961. case PRESET_RIGHT_WIDE:
  962. set_anchor(SIDE_LEFT, ANCHOR_END, p_keep_offsets);
  963. break;
  964. }
  965. // Top
  966. switch (p_preset) {
  967. case PRESET_TOP_LEFT:
  968. case PRESET_TOP_RIGHT:
  969. case PRESET_CENTER_TOP:
  970. case PRESET_LEFT_WIDE:
  971. case PRESET_RIGHT_WIDE:
  972. case PRESET_TOP_WIDE:
  973. case PRESET_VCENTER_WIDE:
  974. case PRESET_FULL_RECT:
  975. set_anchor(SIDE_TOP, ANCHOR_BEGIN, p_keep_offsets);
  976. break;
  977. case PRESET_CENTER_LEFT:
  978. case PRESET_CENTER_RIGHT:
  979. case PRESET_CENTER:
  980. case PRESET_HCENTER_WIDE:
  981. set_anchor(SIDE_TOP, 0.5, p_keep_offsets);
  982. break;
  983. case PRESET_BOTTOM_LEFT:
  984. case PRESET_BOTTOM_RIGHT:
  985. case PRESET_CENTER_BOTTOM:
  986. case PRESET_BOTTOM_WIDE:
  987. set_anchor(SIDE_TOP, ANCHOR_END, p_keep_offsets);
  988. break;
  989. }
  990. // Right
  991. switch (p_preset) {
  992. case PRESET_TOP_LEFT:
  993. case PRESET_BOTTOM_LEFT:
  994. case PRESET_CENTER_LEFT:
  995. case PRESET_LEFT_WIDE:
  996. set_anchor(SIDE_RIGHT, ANCHOR_BEGIN, p_keep_offsets);
  997. break;
  998. case PRESET_CENTER_TOP:
  999. case PRESET_CENTER_BOTTOM:
  1000. case PRESET_CENTER:
  1001. case PRESET_VCENTER_WIDE:
  1002. set_anchor(SIDE_RIGHT, 0.5, p_keep_offsets);
  1003. break;
  1004. case PRESET_TOP_RIGHT:
  1005. case PRESET_BOTTOM_RIGHT:
  1006. case PRESET_CENTER_RIGHT:
  1007. case PRESET_TOP_WIDE:
  1008. case PRESET_RIGHT_WIDE:
  1009. case PRESET_BOTTOM_WIDE:
  1010. case PRESET_HCENTER_WIDE:
  1011. case PRESET_FULL_RECT:
  1012. set_anchor(SIDE_RIGHT, ANCHOR_END, p_keep_offsets);
  1013. break;
  1014. }
  1015. // Bottom
  1016. switch (p_preset) {
  1017. case PRESET_TOP_LEFT:
  1018. case PRESET_TOP_RIGHT:
  1019. case PRESET_CENTER_TOP:
  1020. case PRESET_TOP_WIDE:
  1021. set_anchor(SIDE_BOTTOM, ANCHOR_BEGIN, p_keep_offsets);
  1022. break;
  1023. case PRESET_CENTER_LEFT:
  1024. case PRESET_CENTER_RIGHT:
  1025. case PRESET_CENTER:
  1026. case PRESET_HCENTER_WIDE:
  1027. set_anchor(SIDE_BOTTOM, 0.5, p_keep_offsets);
  1028. break;
  1029. case PRESET_BOTTOM_LEFT:
  1030. case PRESET_BOTTOM_RIGHT:
  1031. case PRESET_CENTER_BOTTOM:
  1032. case PRESET_LEFT_WIDE:
  1033. case PRESET_RIGHT_WIDE:
  1034. case PRESET_BOTTOM_WIDE:
  1035. case PRESET_VCENTER_WIDE:
  1036. case PRESET_FULL_RECT:
  1037. set_anchor(SIDE_BOTTOM, ANCHOR_END, p_keep_offsets);
  1038. break;
  1039. }
  1040. }
  1041. void Control::set_offsets_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
  1042. ERR_MAIN_THREAD_GUARD;
  1043. ERR_FAIL_INDEX((int)p_preset, 16);
  1044. ERR_FAIL_INDEX((int)p_resize_mode, 4);
  1045. // Calculate the size if the node is not resized
  1046. Size2 min_size = get_minimum_size();
  1047. Size2 new_size = get_size();
  1048. if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_HEIGHT) {
  1049. new_size.x = min_size.x;
  1050. }
  1051. if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_WIDTH) {
  1052. new_size.y = min_size.y;
  1053. }
  1054. Rect2 parent_rect = get_parent_anchorable_rect();
  1055. real_t x = parent_rect.size.x;
  1056. if (is_layout_rtl()) {
  1057. x = parent_rect.size.x - x - new_size.x;
  1058. }
  1059. //Left
  1060. switch (p_preset) {
  1061. case PRESET_TOP_LEFT:
  1062. case PRESET_BOTTOM_LEFT:
  1063. case PRESET_CENTER_LEFT:
  1064. case PRESET_TOP_WIDE:
  1065. case PRESET_BOTTOM_WIDE:
  1066. case PRESET_LEFT_WIDE:
  1067. case PRESET_HCENTER_WIDE:
  1068. case PRESET_FULL_RECT:
  1069. data.offset[0] = x * (0.0 - data.anchor[0]) + p_margin + parent_rect.position.x;
  1070. break;
  1071. case PRESET_CENTER_TOP:
  1072. case PRESET_CENTER_BOTTOM:
  1073. case PRESET_CENTER:
  1074. case PRESET_VCENTER_WIDE:
  1075. data.offset[0] = x * (0.5 - data.anchor[0]) - new_size.x / 2 + parent_rect.position.x;
  1076. break;
  1077. case PRESET_TOP_RIGHT:
  1078. case PRESET_BOTTOM_RIGHT:
  1079. case PRESET_CENTER_RIGHT:
  1080. case PRESET_RIGHT_WIDE:
  1081. data.offset[0] = x * (1.0 - data.anchor[0]) - new_size.x - p_margin + parent_rect.position.x;
  1082. break;
  1083. }
  1084. // Top
  1085. switch (p_preset) {
  1086. case PRESET_TOP_LEFT:
  1087. case PRESET_TOP_RIGHT:
  1088. case PRESET_CENTER_TOP:
  1089. case PRESET_LEFT_WIDE:
  1090. case PRESET_RIGHT_WIDE:
  1091. case PRESET_TOP_WIDE:
  1092. case PRESET_VCENTER_WIDE:
  1093. case PRESET_FULL_RECT:
  1094. data.offset[1] = parent_rect.size.y * (0.0 - data.anchor[1]) + p_margin + parent_rect.position.y;
  1095. break;
  1096. case PRESET_CENTER_LEFT:
  1097. case PRESET_CENTER_RIGHT:
  1098. case PRESET_CENTER:
  1099. case PRESET_HCENTER_WIDE:
  1100. data.offset[1] = parent_rect.size.y * (0.5 - data.anchor[1]) - new_size.y / 2 + parent_rect.position.y;
  1101. break;
  1102. case PRESET_BOTTOM_LEFT:
  1103. case PRESET_BOTTOM_RIGHT:
  1104. case PRESET_CENTER_BOTTOM:
  1105. case PRESET_BOTTOM_WIDE:
  1106. data.offset[1] = parent_rect.size.y * (1.0 - data.anchor[1]) - new_size.y - p_margin + parent_rect.position.y;
  1107. break;
  1108. }
  1109. // Right
  1110. switch (p_preset) {
  1111. case PRESET_TOP_LEFT:
  1112. case PRESET_BOTTOM_LEFT:
  1113. case PRESET_CENTER_LEFT:
  1114. case PRESET_LEFT_WIDE:
  1115. data.offset[2] = x * (0.0 - data.anchor[2]) + new_size.x + p_margin + parent_rect.position.x;
  1116. break;
  1117. case PRESET_CENTER_TOP:
  1118. case PRESET_CENTER_BOTTOM:
  1119. case PRESET_CENTER:
  1120. case PRESET_VCENTER_WIDE:
  1121. data.offset[2] = x * (0.5 - data.anchor[2]) + new_size.x / 2 + parent_rect.position.x;
  1122. break;
  1123. case PRESET_TOP_RIGHT:
  1124. case PRESET_BOTTOM_RIGHT:
  1125. case PRESET_CENTER_RIGHT:
  1126. case PRESET_TOP_WIDE:
  1127. case PRESET_RIGHT_WIDE:
  1128. case PRESET_BOTTOM_WIDE:
  1129. case PRESET_HCENTER_WIDE:
  1130. case PRESET_FULL_RECT:
  1131. data.offset[2] = x * (1.0 - data.anchor[2]) - p_margin + parent_rect.position.x;
  1132. break;
  1133. }
  1134. // Bottom
  1135. switch (p_preset) {
  1136. case PRESET_TOP_LEFT:
  1137. case PRESET_TOP_RIGHT:
  1138. case PRESET_CENTER_TOP:
  1139. case PRESET_TOP_WIDE:
  1140. data.offset[3] = parent_rect.size.y * (0.0 - data.anchor[3]) + new_size.y + p_margin + parent_rect.position.y;
  1141. break;
  1142. case PRESET_CENTER_LEFT:
  1143. case PRESET_CENTER_RIGHT:
  1144. case PRESET_CENTER:
  1145. case PRESET_HCENTER_WIDE:
  1146. data.offset[3] = parent_rect.size.y * (0.5 - data.anchor[3]) + new_size.y / 2 + parent_rect.position.y;
  1147. break;
  1148. case PRESET_BOTTOM_LEFT:
  1149. case PRESET_BOTTOM_RIGHT:
  1150. case PRESET_CENTER_BOTTOM:
  1151. case PRESET_LEFT_WIDE:
  1152. case PRESET_RIGHT_WIDE:
  1153. case PRESET_BOTTOM_WIDE:
  1154. case PRESET_VCENTER_WIDE:
  1155. case PRESET_FULL_RECT:
  1156. data.offset[3] = parent_rect.size.y * (1.0 - data.anchor[3]) - p_margin + parent_rect.position.y;
  1157. break;
  1158. }
  1159. _size_changed();
  1160. }
  1161. void Control::set_anchors_and_offsets_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
  1162. ERR_MAIN_THREAD_GUARD;
  1163. set_anchors_preset(p_preset);
  1164. set_offsets_preset(p_preset, p_resize_mode, p_margin);
  1165. }
  1166. void Control::set_grow_direction_preset(LayoutPreset p_preset) {
  1167. ERR_MAIN_THREAD_GUARD;
  1168. // Select correct horizontal grow direction.
  1169. switch (p_preset) {
  1170. case PRESET_TOP_LEFT:
  1171. case PRESET_BOTTOM_LEFT:
  1172. case PRESET_CENTER_LEFT:
  1173. case PRESET_LEFT_WIDE:
  1174. set_h_grow_direction(GrowDirection::GROW_DIRECTION_END);
  1175. break;
  1176. case PRESET_TOP_RIGHT:
  1177. case PRESET_BOTTOM_RIGHT:
  1178. case PRESET_CENTER_RIGHT:
  1179. case PRESET_RIGHT_WIDE:
  1180. set_h_grow_direction(GrowDirection::GROW_DIRECTION_BEGIN);
  1181. break;
  1182. case PRESET_CENTER_TOP:
  1183. case PRESET_CENTER_BOTTOM:
  1184. case PRESET_CENTER:
  1185. case PRESET_TOP_WIDE:
  1186. case PRESET_BOTTOM_WIDE:
  1187. case PRESET_VCENTER_WIDE:
  1188. case PRESET_HCENTER_WIDE:
  1189. case PRESET_FULL_RECT:
  1190. set_h_grow_direction(GrowDirection::GROW_DIRECTION_BOTH);
  1191. break;
  1192. }
  1193. // Select correct vertical grow direction.
  1194. switch (p_preset) {
  1195. case PRESET_TOP_LEFT:
  1196. case PRESET_TOP_RIGHT:
  1197. case PRESET_CENTER_TOP:
  1198. case PRESET_TOP_WIDE:
  1199. set_v_grow_direction(GrowDirection::GROW_DIRECTION_END);
  1200. break;
  1201. case PRESET_BOTTOM_LEFT:
  1202. case PRESET_BOTTOM_RIGHT:
  1203. case PRESET_CENTER_BOTTOM:
  1204. case PRESET_BOTTOM_WIDE:
  1205. set_v_grow_direction(GrowDirection::GROW_DIRECTION_BEGIN);
  1206. break;
  1207. case PRESET_CENTER_LEFT:
  1208. case PRESET_CENTER_RIGHT:
  1209. case PRESET_CENTER:
  1210. case PRESET_LEFT_WIDE:
  1211. case PRESET_RIGHT_WIDE:
  1212. case PRESET_VCENTER_WIDE:
  1213. case PRESET_HCENTER_WIDE:
  1214. case PRESET_FULL_RECT:
  1215. set_v_grow_direction(GrowDirection::GROW_DIRECTION_BOTH);
  1216. break;
  1217. }
  1218. }
  1219. /// Manual positioning.
  1220. void Control::_set_position(const Point2 &p_point) {
  1221. set_position(p_point);
  1222. }
  1223. void Control::set_position(const Point2 &p_point, bool p_keep_offsets) {
  1224. ERR_MAIN_THREAD_GUARD;
  1225. #ifdef TOOLS_ENABLED
  1226. // Can't compute anchors, set position directly and return immediately.
  1227. if (saving && !is_inside_tree()) {
  1228. data.pos_cache = p_point;
  1229. return;
  1230. }
  1231. #endif // TOOLS_ENABLED
  1232. if (p_keep_offsets) {
  1233. _compute_anchors(Rect2(p_point, data.size_cache), data.offset, data.anchor);
  1234. } else {
  1235. _compute_offsets(Rect2(p_point, data.size_cache), data.anchor, data.offset);
  1236. }
  1237. _size_changed();
  1238. }
  1239. Size2 Control::get_position() const {
  1240. ERR_READ_THREAD_GUARD_V(Size2());
  1241. return data.pos_cache;
  1242. }
  1243. void Control::_set_global_position(const Point2 &p_point) {
  1244. set_global_position(p_point);
  1245. }
  1246. void Control::set_global_position(const Point2 &p_point, bool p_keep_offsets) {
  1247. ERR_MAIN_THREAD_GUARD;
  1248. // (parent_global_transform * T(new_position) * internal_transform).origin == new_global_position
  1249. // (T(new_position) * internal_transform).origin == new_position_in_parent_space
  1250. // new_position == new_position_in_parent_space - internal_transform.origin
  1251. Point2 position_in_parent_space = data.parent_canvas_item ? data.parent_canvas_item->get_global_transform().affine_inverse().xform(p_point) : p_point;
  1252. set_position(position_in_parent_space - _get_internal_transform().get_origin(), p_keep_offsets);
  1253. }
  1254. Point2 Control::get_global_position() const {
  1255. ERR_READ_THREAD_GUARD_V(Point2());
  1256. return get_global_transform().get_origin();
  1257. }
  1258. Point2 Control::get_screen_position() const {
  1259. ERR_READ_THREAD_GUARD_V(Point2());
  1260. ERR_FAIL_COND_V(!is_inside_tree(), Point2());
  1261. return get_screen_transform().get_origin();
  1262. }
  1263. void Control::_set_size(const Size2 &p_size) {
  1264. #ifdef DEBUG_ENABLED
  1265. if (data.size_warning && (data.anchor[SIDE_LEFT] != data.anchor[SIDE_RIGHT] || data.anchor[SIDE_TOP] != data.anchor[SIDE_BOTTOM])) {
  1266. WARN_PRINT("Nodes with non-equal opposite anchors will have their size overridden after _ready(). \nIf you want to set size, change the anchors or consider using set_deferred().");
  1267. }
  1268. #endif // DEBUG_ENABLED
  1269. set_size(p_size);
  1270. }
  1271. void Control::set_size(const Size2 &p_size, bool p_keep_offsets) {
  1272. ERR_MAIN_THREAD_GUARD;
  1273. ERR_FAIL_COND(!std::isfinite(p_size.x) || !std::isfinite(p_size.y));
  1274. Size2 new_size = p_size;
  1275. Size2 min = get_combined_minimum_size();
  1276. if (new_size.x < min.x) {
  1277. new_size.x = min.x;
  1278. }
  1279. if (new_size.y < min.y) {
  1280. new_size.y = min.y;
  1281. }
  1282. #ifdef TOOLS_ENABLED
  1283. // Can't compute anchors, set size directly and return immediately.
  1284. if (saving && !is_inside_tree()) {
  1285. data.size_cache = new_size;
  1286. return;
  1287. }
  1288. #endif // TOOLS_ENABLED
  1289. if (p_keep_offsets) {
  1290. _compute_anchors(Rect2(data.pos_cache, new_size), data.offset, data.anchor);
  1291. } else {
  1292. _compute_offsets(Rect2(data.pos_cache, new_size), data.anchor, data.offset);
  1293. }
  1294. _size_changed();
  1295. }
  1296. Size2 Control::get_size() const {
  1297. ERR_READ_THREAD_GUARD_V(Size2());
  1298. return data.size_cache;
  1299. }
  1300. void Control::reset_size() {
  1301. ERR_MAIN_THREAD_GUARD;
  1302. set_size(Size2());
  1303. }
  1304. void Control::set_rect(const Rect2 &p_rect) {
  1305. ERR_MAIN_THREAD_GUARD;
  1306. for (int i = 0; i < 4; i++) {
  1307. data.anchor[i] = ANCHOR_BEGIN;
  1308. }
  1309. _compute_offsets(p_rect, data.anchor, data.offset);
  1310. if (is_inside_tree()) {
  1311. _size_changed();
  1312. }
  1313. }
  1314. Rect2 Control::get_rect() const {
  1315. ERR_READ_THREAD_GUARD_V(Rect2());
  1316. Transform2D xform = get_transform();
  1317. return Rect2(xform.get_origin(), xform.get_scale() * get_size());
  1318. }
  1319. Rect2 Control::get_global_rect() const {
  1320. ERR_READ_THREAD_GUARD_V(Rect2());
  1321. Transform2D xform = get_global_transform();
  1322. return Rect2(xform.get_origin(), xform.get_scale() * get_size());
  1323. }
  1324. Rect2 Control::get_screen_rect() const {
  1325. ERR_READ_THREAD_GUARD_V(Rect2());
  1326. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  1327. Transform2D xform = get_screen_transform();
  1328. return Rect2(xform.get_origin(), xform.get_scale() * get_size());
  1329. }
  1330. Rect2 Control::get_anchorable_rect() const {
  1331. ERR_READ_THREAD_GUARD_V(Rect2());
  1332. return Rect2(Point2(), get_size());
  1333. }
  1334. void Control::set_scale(const Vector2 &p_scale) {
  1335. ERR_MAIN_THREAD_GUARD;
  1336. if (data.scale == p_scale) {
  1337. return;
  1338. }
  1339. data.scale = p_scale;
  1340. // Avoid having 0 scale values, can lead to errors in physics and rendering.
  1341. if (data.scale.x == 0) {
  1342. data.scale.x = CMP_EPSILON;
  1343. }
  1344. if (data.scale.y == 0) {
  1345. data.scale.y = CMP_EPSILON;
  1346. }
  1347. queue_redraw();
  1348. _notify_transform();
  1349. queue_accessibility_update();
  1350. }
  1351. Vector2 Control::get_scale() const {
  1352. ERR_READ_THREAD_GUARD_V(Vector2());
  1353. return data.scale;
  1354. }
  1355. void Control::set_rotation(real_t p_radians) {
  1356. ERR_MAIN_THREAD_GUARD;
  1357. if (data.rotation == p_radians) {
  1358. return;
  1359. }
  1360. data.rotation = p_radians;
  1361. queue_redraw();
  1362. _notify_transform();
  1363. queue_accessibility_update();
  1364. }
  1365. void Control::set_rotation_degrees(real_t p_degrees) {
  1366. ERR_MAIN_THREAD_GUARD;
  1367. set_rotation(Math::deg_to_rad(p_degrees));
  1368. }
  1369. real_t Control::get_rotation() const {
  1370. ERR_READ_THREAD_GUARD_V(0);
  1371. return data.rotation;
  1372. }
  1373. real_t Control::get_rotation_degrees() const {
  1374. ERR_READ_THREAD_GUARD_V(0);
  1375. return Math::rad_to_deg(get_rotation());
  1376. }
  1377. void Control::set_pivot_offset_ratio(const Vector2 &p_ratio) {
  1378. ERR_MAIN_THREAD_GUARD;
  1379. if (data.pivot_offset_ratio == p_ratio) {
  1380. return;
  1381. }
  1382. data.pivot_offset_ratio = p_ratio;
  1383. queue_redraw();
  1384. _notify_transform();
  1385. queue_accessibility_update();
  1386. }
  1387. Vector2 Control::get_pivot_offset_ratio() const {
  1388. ERR_READ_THREAD_GUARD_V(Vector2());
  1389. return data.pivot_offset_ratio;
  1390. }
  1391. void Control::set_pivot_offset(const Vector2 &p_pivot) {
  1392. ERR_MAIN_THREAD_GUARD;
  1393. if (data.pivot_offset == p_pivot) {
  1394. return;
  1395. }
  1396. data.pivot_offset = p_pivot;
  1397. queue_redraw();
  1398. _notify_transform();
  1399. queue_accessibility_update();
  1400. }
  1401. Vector2 Control::get_pivot_offset() const {
  1402. ERR_READ_THREAD_GUARD_V(Vector2());
  1403. return data.pivot_offset;
  1404. }
  1405. Vector2 Control::get_combined_pivot_offset() const {
  1406. ERR_READ_THREAD_GUARD_V(Vector2());
  1407. return data.pivot_offset + data.pivot_offset_ratio * get_size();
  1408. }
  1409. /// Sizes.
  1410. void Control::_update_minimum_size() {
  1411. if (!is_inside_tree()) {
  1412. data.updating_last_minimum_size = false;
  1413. return;
  1414. }
  1415. Size2 minsize = get_combined_minimum_size();
  1416. data.updating_last_minimum_size = false;
  1417. if (minsize != data.last_minimum_size) {
  1418. data.last_minimum_size = minsize;
  1419. _size_changed();
  1420. emit_signal(SceneStringName(minimum_size_changed));
  1421. }
  1422. }
  1423. void Control::update_minimum_size() {
  1424. ERR_MAIN_THREAD_GUARD;
  1425. if (!is_inside_tree() || data.block_minimum_size_adjust) {
  1426. return;
  1427. }
  1428. // Invalidate cache upwards.
  1429. Control *invalidate = this;
  1430. while (invalidate && invalidate->data.minimum_size_valid) {
  1431. invalidate->data.minimum_size_valid = false;
  1432. if (invalidate->is_set_as_top_level()) {
  1433. break; // Do not go further up.
  1434. }
  1435. Window *parent_window = invalidate->get_parent_window();
  1436. if (parent_window && parent_window->is_wrapping_controls()) {
  1437. parent_window->child_controls_changed();
  1438. break; // Stop on a window as well.
  1439. }
  1440. invalidate = invalidate->get_parent_control();
  1441. }
  1442. if (!is_visible_in_tree()) {
  1443. return;
  1444. }
  1445. if (data.updating_last_minimum_size) {
  1446. return;
  1447. }
  1448. data.updating_last_minimum_size = true;
  1449. callable_mp(this, &Control::_update_minimum_size).call_deferred();
  1450. }
  1451. void Control::set_block_minimum_size_adjust(bool p_block) {
  1452. ERR_MAIN_THREAD_GUARD;
  1453. data.block_minimum_size_adjust = p_block;
  1454. }
  1455. Size2 Control::get_minimum_size() const {
  1456. ERR_READ_THREAD_GUARD_V(Size2());
  1457. Vector2 ms;
  1458. GDVIRTUAL_CALL(_get_minimum_size, ms);
  1459. return ms;
  1460. }
  1461. void Control::set_custom_minimum_size(const Size2 &p_custom) {
  1462. ERR_MAIN_THREAD_GUARD;
  1463. if (p_custom == data.custom_minimum_size) {
  1464. return;
  1465. }
  1466. if (!std::isfinite(p_custom.x) || !std::isfinite(p_custom.y)) {
  1467. // Prevent infinite loop.
  1468. return;
  1469. }
  1470. data.custom_minimum_size = p_custom;
  1471. update_minimum_size();
  1472. update_configuration_warnings();
  1473. }
  1474. Size2 Control::get_custom_minimum_size() const {
  1475. ERR_READ_THREAD_GUARD_V(Size2());
  1476. return data.custom_minimum_size;
  1477. }
  1478. void Control::_update_minimum_size_cache() const {
  1479. Size2 minsize = get_minimum_size();
  1480. minsize = minsize.max(data.custom_minimum_size);
  1481. data.minimum_size_cache = minsize;
  1482. data.minimum_size_valid = true;
  1483. }
  1484. Size2 Control::get_combined_minimum_size() const {
  1485. ERR_READ_THREAD_GUARD_V(Size2());
  1486. if (!data.minimum_size_valid) {
  1487. _update_minimum_size_cache();
  1488. }
  1489. return data.minimum_size_cache;
  1490. }
  1491. void Control::_size_changed() {
  1492. Rect2 parent_rect = get_parent_anchorable_rect();
  1493. real_t edge_pos[4];
  1494. for (int i = 0; i < 4; i++) {
  1495. real_t area = parent_rect.size[i & 1];
  1496. edge_pos[i] = data.offset[i] + (data.anchor[i] * area);
  1497. }
  1498. Point2 new_pos_cache = Point2(edge_pos[0], edge_pos[1]);
  1499. Size2 new_size_cache = Point2(edge_pos[2], edge_pos[3]) - new_pos_cache;
  1500. Size2 minimum_size = get_combined_minimum_size();
  1501. if (minimum_size.width > new_size_cache.width) {
  1502. if (data.h_grow == GROW_DIRECTION_BEGIN) {
  1503. new_pos_cache.x += new_size_cache.width - minimum_size.width;
  1504. } else if (data.h_grow == GROW_DIRECTION_BOTH) {
  1505. new_pos_cache.x += 0.5 * (new_size_cache.width - minimum_size.width);
  1506. }
  1507. new_size_cache.width = minimum_size.width;
  1508. }
  1509. if (is_layout_rtl()) {
  1510. new_pos_cache.x = parent_rect.size.x + 2 * parent_rect.position.x - new_pos_cache.x - new_size_cache.x;
  1511. }
  1512. if (minimum_size.height > new_size_cache.height) {
  1513. if (data.v_grow == GROW_DIRECTION_BEGIN) {
  1514. new_pos_cache.y += new_size_cache.height - minimum_size.height;
  1515. } else if (data.v_grow == GROW_DIRECTION_BOTH) {
  1516. new_pos_cache.y += 0.5 * (new_size_cache.height - minimum_size.height);
  1517. }
  1518. new_size_cache.height = minimum_size.height;
  1519. }
  1520. bool pos_changed = new_pos_cache != data.pos_cache;
  1521. bool size_changed = new_size_cache != data.size_cache;
  1522. // Below helps in getting rid of floating point errors for signaling resized.
  1523. bool approx_pos_changed = !new_pos_cache.is_equal_approx(data.pos_cache);
  1524. bool approx_size_changed = !new_size_cache.is_equal_approx(data.size_cache);
  1525. if (pos_changed) {
  1526. data.pos_cache = new_pos_cache;
  1527. }
  1528. if (size_changed) {
  1529. data.size_cache = new_size_cache;
  1530. }
  1531. if (is_inside_tree()) {
  1532. if (approx_pos_changed || approx_size_changed) {
  1533. // Ensure global transform is marked as dirty before `NOTIFICATION_RESIZED` / `item_rect_changed` signal
  1534. // so an up to date global transform could be obtained when handling these.
  1535. _notify_transform();
  1536. item_rect_changed(approx_size_changed);
  1537. if (approx_size_changed) {
  1538. notification(NOTIFICATION_RESIZED);
  1539. }
  1540. }
  1541. if (pos_changed && !approx_size_changed) {
  1542. _update_canvas_item_transform();
  1543. }
  1544. queue_accessibility_update();
  1545. } else if (pos_changed) {
  1546. _notify_transform();
  1547. }
  1548. }
  1549. void Control::_clear_size_warning() {
  1550. data.size_warning = false;
  1551. }
  1552. // Container sizing.
  1553. void Control::set_h_size_flags(BitField<SizeFlags> p_flags) {
  1554. ERR_MAIN_THREAD_GUARD;
  1555. if ((int)data.h_size_flags == (int)p_flags) {
  1556. return;
  1557. }
  1558. data.h_size_flags = p_flags;
  1559. emit_signal(SceneStringName(size_flags_changed));
  1560. }
  1561. BitField<Control::SizeFlags> Control::get_h_size_flags() const {
  1562. ERR_READ_THREAD_GUARD_V(SIZE_EXPAND_FILL);
  1563. return data.h_size_flags;
  1564. }
  1565. void Control::set_v_size_flags(BitField<SizeFlags> p_flags) {
  1566. ERR_MAIN_THREAD_GUARD;
  1567. if ((int)data.v_size_flags == (int)p_flags) {
  1568. return;
  1569. }
  1570. data.v_size_flags = p_flags;
  1571. emit_signal(SceneStringName(size_flags_changed));
  1572. }
  1573. BitField<Control::SizeFlags> Control::get_v_size_flags() const {
  1574. ERR_READ_THREAD_GUARD_V(SIZE_EXPAND_FILL);
  1575. return data.v_size_flags;
  1576. }
  1577. void Control::set_stretch_ratio(real_t p_ratio) {
  1578. ERR_MAIN_THREAD_GUARD;
  1579. if (data.expand == p_ratio) {
  1580. return;
  1581. }
  1582. data.expand = p_ratio;
  1583. emit_signal(SceneStringName(size_flags_changed));
  1584. }
  1585. real_t Control::get_stretch_ratio() const {
  1586. ERR_READ_THREAD_GUARD_V(0);
  1587. return data.expand;
  1588. }
  1589. // Input events.
  1590. void Control::_call_gui_input(const Ref<InputEvent> &p_event) {
  1591. if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
  1592. emit_signal(SceneStringName(gui_input), p_event); // Signal should be first, so it's possible to override an event (and then accept it).
  1593. }
  1594. if (!is_inside_tree() || get_viewport()->is_input_handled()) {
  1595. return; // Input was handled, abort.
  1596. }
  1597. if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
  1598. GDVIRTUAL_CALL(_gui_input, p_event);
  1599. }
  1600. if (!is_inside_tree() || get_viewport()->is_input_handled()) {
  1601. return; // Input was handled, abort.
  1602. }
  1603. gui_input(p_event);
  1604. }
  1605. void Control::gui_input(const Ref<InputEvent> &p_event) {
  1606. }
  1607. void Control::accept_event() {
  1608. ERR_MAIN_THREAD_GUARD;
  1609. if (is_inside_tree()) {
  1610. get_viewport()->_gui_accept_event();
  1611. }
  1612. }
  1613. bool Control::has_point(const Point2 &p_point) const {
  1614. ERR_READ_THREAD_GUARD_V(false);
  1615. bool ret;
  1616. if (GDVIRTUAL_CALL(_has_point, p_point, ret)) {
  1617. return ret;
  1618. }
  1619. return Rect2(Point2(), get_size()).has_point(p_point);
  1620. }
  1621. void Control::set_mouse_filter(MouseFilter p_filter) {
  1622. ERR_MAIN_THREAD_GUARD;
  1623. ERR_FAIL_INDEX(p_filter, 3);
  1624. if (data.mouse_filter == p_filter) {
  1625. return;
  1626. }
  1627. data.mouse_filter = p_filter;
  1628. notify_property_list_changed();
  1629. update_configuration_warnings();
  1630. if (get_viewport()) {
  1631. get_viewport()->_gui_update_mouse_over();
  1632. }
  1633. }
  1634. Control::MouseFilter Control::get_mouse_filter() const {
  1635. ERR_READ_THREAD_GUARD_V(MOUSE_FILTER_IGNORE);
  1636. return data.mouse_filter;
  1637. }
  1638. Control::MouseFilter Control::get_mouse_filter_with_override() const {
  1639. ERR_READ_THREAD_GUARD_V(MOUSE_FILTER_IGNORE);
  1640. if (!_is_mouse_filter_enabled()) {
  1641. return MOUSE_FILTER_IGNORE;
  1642. }
  1643. return data.mouse_filter;
  1644. }
  1645. void Control::set_mouse_behavior_recursive(MouseBehaviorRecursive p_mouse_behavior_recursive) {
  1646. ERR_MAIN_THREAD_GUARD;
  1647. ERR_FAIL_INDEX(p_mouse_behavior_recursive, 3);
  1648. if (data.mouse_behavior_recursive == p_mouse_behavior_recursive) {
  1649. return;
  1650. }
  1651. data.mouse_behavior_recursive = p_mouse_behavior_recursive;
  1652. _update_mouse_behavior_recursive();
  1653. }
  1654. Control::MouseBehaviorRecursive Control::get_mouse_behavior_recursive() const {
  1655. ERR_READ_THREAD_GUARD_V(MOUSE_BEHAVIOR_INHERITED);
  1656. return data.mouse_behavior_recursive;
  1657. }
  1658. bool Control::_is_mouse_filter_enabled() const {
  1659. ERR_READ_THREAD_GUARD_V(false);
  1660. if (data.mouse_behavior_recursive == MOUSE_BEHAVIOR_INHERITED) {
  1661. if (data.parent_control) {
  1662. return data.parent_mouse_behavior_recursive_enabled;
  1663. }
  1664. return true;
  1665. }
  1666. return data.mouse_behavior_recursive == MOUSE_BEHAVIOR_ENABLED;
  1667. }
  1668. void Control::_update_mouse_behavior_recursive() {
  1669. if (data.mouse_behavior_recursive == MOUSE_BEHAVIOR_INHERITED) {
  1670. if (data.parent_control) {
  1671. _propagate_mouse_behavior_recursive_recursively(data.parent_control->_is_mouse_filter_enabled(), false);
  1672. } else {
  1673. _propagate_mouse_behavior_recursive_recursively(true, false);
  1674. }
  1675. } else {
  1676. _propagate_mouse_behavior_recursive_recursively(data.mouse_behavior_recursive == MOUSE_BEHAVIOR_ENABLED, false);
  1677. }
  1678. if (get_viewport()) {
  1679. get_viewport()->_gui_update_mouse_over();
  1680. }
  1681. }
  1682. void Control::_propagate_mouse_behavior_recursive_recursively(bool p_enabled, bool p_skip_non_inherited) {
  1683. if (p_skip_non_inherited && data.mouse_behavior_recursive != MOUSE_BEHAVIOR_INHERITED) {
  1684. return;
  1685. }
  1686. data.parent_mouse_behavior_recursive_enabled = p_enabled;
  1687. for (int i = 0; i < get_child_count(); i++) {
  1688. Control *control = Object::cast_to<Control>(get_child(i));
  1689. if (control) {
  1690. control->_propagate_mouse_behavior_recursive_recursively(p_enabled, true);
  1691. }
  1692. }
  1693. }
  1694. void Control::set_force_pass_scroll_events(bool p_force_pass_scroll_events) {
  1695. ERR_MAIN_THREAD_GUARD;
  1696. data.force_pass_scroll_events = p_force_pass_scroll_events;
  1697. }
  1698. bool Control::is_force_pass_scroll_events() const {
  1699. ERR_READ_THREAD_GUARD_V(false);
  1700. return data.force_pass_scroll_events;
  1701. }
  1702. void Control::warp_mouse(const Point2 &p_position) {
  1703. ERR_MAIN_THREAD_GUARD;
  1704. ERR_FAIL_COND(!is_inside_tree());
  1705. get_viewport()->warp_mouse(get_global_transform_with_canvas().xform(p_position));
  1706. }
  1707. void Control::set_shortcut_context(const Node *p_node) {
  1708. ERR_MAIN_THREAD_GUARD;
  1709. if (p_node != nullptr) {
  1710. data.shortcut_context = p_node->get_instance_id();
  1711. } else {
  1712. data.shortcut_context = ObjectID();
  1713. }
  1714. }
  1715. Node *Control::get_shortcut_context() const {
  1716. ERR_READ_THREAD_GUARD_V(nullptr);
  1717. Object *ctx_obj = ObjectDB::get_instance(data.shortcut_context);
  1718. Node *ctx_node = Object::cast_to<Node>(ctx_obj);
  1719. return ctx_node;
  1720. }
  1721. bool Control::is_focus_owner_in_shortcut_context() const {
  1722. ERR_READ_THREAD_GUARD_V(false);
  1723. if (data.shortcut_context == ObjectID()) {
  1724. // No context, therefore global - always "in" context.
  1725. return true;
  1726. }
  1727. const Node *ctx_node = get_shortcut_context();
  1728. const Control *vp_focus = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  1729. // If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
  1730. return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
  1731. }
  1732. // Drag and drop handling.
  1733. void Control::set_drag_forwarding(const Callable &p_drag, const Callable &p_can_drop, const Callable &p_drop) {
  1734. ERR_MAIN_THREAD_GUARD;
  1735. data.forward_drag = p_drag;
  1736. data.forward_can_drop = p_can_drop;
  1737. data.forward_drop = p_drop;
  1738. }
  1739. Variant Control::get_drag_data(const Point2 &p_point) {
  1740. ERR_READ_THREAD_GUARD_V(Variant());
  1741. Variant ret;
  1742. if (data.forward_drag.is_valid()) {
  1743. Variant p = p_point;
  1744. const Variant *vp[1] = { &p };
  1745. Callable::CallError ce;
  1746. data.forward_drag.callp((const Variant **)vp, 1, ret, ce);
  1747. if (ce.error != Callable::CallError::CALL_OK) {
  1748. ERR_FAIL_V_MSG(Variant(), "Error calling forwarded method from 'get_drag_data': " + Variant::get_callable_error_text(data.forward_drag, (const Variant **)vp, 1, ce) + ".");
  1749. }
  1750. return ret;
  1751. }
  1752. GDVIRTUAL_CALL(_get_drag_data, p_point, ret);
  1753. return ret;
  1754. }
  1755. bool Control::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  1756. ERR_READ_THREAD_GUARD_V(false);
  1757. if (data.forward_can_drop.is_valid()) {
  1758. Variant ret;
  1759. Variant p = p_point;
  1760. const Variant *vp[2] = { &p, &p_data };
  1761. Callable::CallError ce;
  1762. data.forward_can_drop.callp((const Variant **)vp, 2, ret, ce);
  1763. if (ce.error != Callable::CallError::CALL_OK) {
  1764. ERR_FAIL_V_MSG(Variant(), "Error calling forwarded method from 'can_drop_data': " + Variant::get_callable_error_text(data.forward_can_drop, (const Variant **)vp, 2, ce) + ".");
  1765. }
  1766. return ret;
  1767. }
  1768. bool ret = false;
  1769. GDVIRTUAL_CALL(_can_drop_data, p_point, p_data, ret);
  1770. return ret;
  1771. }
  1772. void Control::drop_data(const Point2 &p_point, const Variant &p_data) {
  1773. ERR_READ_THREAD_GUARD;
  1774. if (data.forward_drop.is_valid()) {
  1775. Variant ret;
  1776. Variant p = p_point;
  1777. const Variant *vp[2] = { &p, &p_data };
  1778. Callable::CallError ce;
  1779. data.forward_drop.callp((const Variant **)vp, 2, ret, ce);
  1780. if (ce.error != Callable::CallError::CALL_OK) {
  1781. ERR_FAIL_MSG("Error calling forwarded method from 'drop_data': " + Variant::get_callable_error_text(data.forward_drop, (const Variant **)vp, 2, ce) + ".");
  1782. }
  1783. return;
  1784. }
  1785. GDVIRTUAL_CALL(_drop_data, p_point, p_data);
  1786. }
  1787. void Control::force_drag(const Variant &p_data, Control *p_control) {
  1788. ERR_MAIN_THREAD_GUARD;
  1789. ERR_FAIL_COND(!is_inside_tree());
  1790. ERR_FAIL_COND(p_data.get_type() == Variant::NIL);
  1791. Viewport *vp = get_viewport();
  1792. vp->_gui_force_drag_start();
  1793. vp->_gui_force_drag(this, p_data, p_control);
  1794. }
  1795. void Control::accessibility_drag() {
  1796. ERR_MAIN_THREAD_GUARD;
  1797. ERR_FAIL_COND(!is_inside_tree());
  1798. Viewport *vp = get_viewport();
  1799. vp->_gui_force_drag_start();
  1800. Variant dnd_data = get_drag_data(Vector2(Math::INF, Math::INF));
  1801. if (dnd_data.get_type() != Variant::NIL) {
  1802. Window *w = Window::get_from_id(get_window()->get_window_id());
  1803. if (w) {
  1804. w->accessibility_announcement(vformat(RTR("%s grabbed. Select target and use %s to drop, use %s to cancel."), vp->gui_get_drag_description(), InputMap::get_singleton()->get_action_description("ui_accessibility_drag_and_drop"), InputMap::get_singleton()->get_action_description("ui_cancel")));
  1805. }
  1806. vp->_gui_force_drag(this, dnd_data, nullptr);
  1807. queue_accessibility_update();
  1808. } else {
  1809. vp->_gui_force_drag_cancel();
  1810. }
  1811. }
  1812. void Control::accessibility_drop() {
  1813. ERR_MAIN_THREAD_GUARD;
  1814. ERR_FAIL_COND(!is_inside_tree());
  1815. ERR_FAIL_COND(!get_viewport()->gui_is_dragging());
  1816. get_viewport()->gui_perform_drop_at(Vector2(Math::INF, Math::INF), this);
  1817. queue_accessibility_update();
  1818. }
  1819. String Control::get_accessibility_container_name(const Node *p_node) const {
  1820. String ret;
  1821. if (GDVIRTUAL_CALL(_get_accessibility_container_name, p_node, ret)) {
  1822. } else if (data.parent_control) {
  1823. ret = data.parent_control->get_accessibility_container_name(this);
  1824. }
  1825. return ret;
  1826. }
  1827. void Control::set_accessibility_name(const String &p_name) {
  1828. ERR_THREAD_GUARD
  1829. if (data.accessibility_name != p_name) {
  1830. data.accessibility_name = p_name;
  1831. queue_accessibility_update();
  1832. update_configuration_warnings();
  1833. }
  1834. }
  1835. String Control::get_accessibility_name() const {
  1836. return tr(data.accessibility_name);
  1837. }
  1838. void Control::set_accessibility_description(const String &p_description) {
  1839. ERR_THREAD_GUARD
  1840. if (data.accessibility_description != p_description) {
  1841. data.accessibility_description = p_description;
  1842. queue_accessibility_update();
  1843. }
  1844. }
  1845. String Control::get_accessibility_description() const {
  1846. return tr(data.accessibility_description);
  1847. }
  1848. void Control::set_accessibility_live(DisplayServer::AccessibilityLiveMode p_mode) {
  1849. ERR_THREAD_GUARD
  1850. if (data.accessibility_live != p_mode) {
  1851. data.accessibility_live = p_mode;
  1852. queue_accessibility_update();
  1853. }
  1854. }
  1855. DisplayServer::AccessibilityLiveMode Control::get_accessibility_live() const {
  1856. return data.accessibility_live;
  1857. }
  1858. void Control::set_accessibility_controls_nodes(const TypedArray<NodePath> &p_node_path) {
  1859. ERR_MAIN_THREAD_GUARD;
  1860. if (data.accessibility_controls_nodes != p_node_path) {
  1861. data.accessibility_controls_nodes = p_node_path;
  1862. queue_accessibility_update();
  1863. }
  1864. }
  1865. TypedArray<NodePath> Control::get_accessibility_controls_nodes() const {
  1866. return data.accessibility_controls_nodes;
  1867. }
  1868. void Control::set_accessibility_described_by_nodes(const TypedArray<NodePath> &p_node_path) {
  1869. ERR_MAIN_THREAD_GUARD;
  1870. if (data.accessibility_described_by_nodes != p_node_path) {
  1871. data.accessibility_described_by_nodes = p_node_path;
  1872. queue_accessibility_update();
  1873. }
  1874. }
  1875. TypedArray<NodePath> Control::get_accessibility_described_by_nodes() const {
  1876. return data.accessibility_described_by_nodes;
  1877. }
  1878. void Control::set_accessibility_labeled_by_nodes(const TypedArray<NodePath> &p_node_path) {
  1879. ERR_MAIN_THREAD_GUARD;
  1880. if (data.accessibility_labeled_by_nodes != p_node_path) {
  1881. data.accessibility_labeled_by_nodes = p_node_path;
  1882. queue_accessibility_update();
  1883. }
  1884. }
  1885. TypedArray<NodePath> Control::get_accessibility_labeled_by_nodes() const {
  1886. return data.accessibility_labeled_by_nodes;
  1887. }
  1888. void Control::set_accessibility_flow_to_nodes(const TypedArray<NodePath> &p_node_path) {
  1889. ERR_MAIN_THREAD_GUARD;
  1890. if (data.accessibility_flow_to_nodes != p_node_path) {
  1891. data.accessibility_flow_to_nodes = p_node_path;
  1892. queue_accessibility_update();
  1893. }
  1894. }
  1895. TypedArray<NodePath> Control::get_accessibility_flow_to_nodes() const {
  1896. return data.accessibility_flow_to_nodes;
  1897. }
  1898. void Control::set_drag_preview(Control *p_control) {
  1899. ERR_MAIN_THREAD_GUARD;
  1900. ERR_FAIL_COND(!is_inside_tree());
  1901. ERR_FAIL_COND(!get_viewport()->gui_is_dragging());
  1902. get_viewport()->_gui_set_drag_preview(this, p_control);
  1903. }
  1904. bool Control::is_drag_successful() const {
  1905. ERR_READ_THREAD_GUARD_V(false);
  1906. return is_inside_tree() && get_viewport()->gui_is_drag_successful();
  1907. }
  1908. // Focus.
  1909. void Control::set_focus_mode(FocusMode p_focus_mode) {
  1910. ERR_MAIN_THREAD_GUARD;
  1911. ERR_FAIL_INDEX((int)p_focus_mode, 4);
  1912. if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode != FOCUS_NONE && has_focus()) {
  1913. release_focus();
  1914. }
  1915. if (data.focus_mode == p_focus_mode) {
  1916. return;
  1917. }
  1918. data.focus_mode = p_focus_mode;
  1919. queue_accessibility_update();
  1920. }
  1921. Control::FocusMode Control::get_focus_mode() const {
  1922. ERR_READ_THREAD_GUARD_V(FOCUS_NONE);
  1923. return data.focus_mode;
  1924. }
  1925. Control::FocusMode Control::get_focus_mode_with_override() const {
  1926. ERR_READ_THREAD_GUARD_V(FOCUS_NONE);
  1927. if (!_is_focus_mode_enabled()) {
  1928. return FOCUS_NONE;
  1929. }
  1930. return data.focus_mode;
  1931. }
  1932. void Control::set_focus_behavior_recursive(FocusBehaviorRecursive p_focus_behavior_recursive) {
  1933. ERR_MAIN_THREAD_GUARD;
  1934. ERR_FAIL_INDEX((int)p_focus_behavior_recursive, 3);
  1935. if (data.focus_behavior_recursive == p_focus_behavior_recursive) {
  1936. return;
  1937. }
  1938. data.focus_behavior_recursive = p_focus_behavior_recursive;
  1939. _update_focus_behavior_recursive();
  1940. queue_accessibility_update();
  1941. }
  1942. Control::FocusBehaviorRecursive Control::get_focus_behavior_recursive() const {
  1943. ERR_READ_THREAD_GUARD_V(FOCUS_BEHAVIOR_INHERITED);
  1944. return data.focus_behavior_recursive;
  1945. }
  1946. bool Control::_is_focusable() const {
  1947. bool ac_enabled = is_inside_tree() && get_tree()->is_accessibility_enabled();
  1948. return (is_visible_in_tree() && ((get_focus_mode_with_override() == FOCUS_ALL) || (get_focus_mode_with_override() == FOCUS_CLICK) || (ac_enabled && get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)));
  1949. }
  1950. bool Control::_is_focus_mode_enabled() const {
  1951. if (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED) {
  1952. if (data.parent_control) {
  1953. return data.parent_focus_behavior_recursive_enabled;
  1954. }
  1955. return true;
  1956. }
  1957. return data.focus_behavior_recursive == FOCUS_BEHAVIOR_ENABLED;
  1958. }
  1959. void Control::_update_focus_behavior_recursive() {
  1960. if (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED) {
  1961. Control *parent = get_parent_control();
  1962. if (parent) {
  1963. _propagate_focus_behavior_recursive_recursively(parent->_is_focus_mode_enabled(), false);
  1964. } else {
  1965. _propagate_focus_behavior_recursive_recursively(true, false);
  1966. }
  1967. } else {
  1968. _propagate_focus_behavior_recursive_recursively(data.focus_behavior_recursive == FOCUS_BEHAVIOR_ENABLED, false);
  1969. }
  1970. }
  1971. void Control::_propagate_focus_behavior_recursive_recursively(bool p_enabled, bool p_skip_non_inherited) {
  1972. if (is_inside_tree() && (data.focus_behavior_recursive == FOCUS_BEHAVIOR_DISABLED || (data.focus_behavior_recursive == FOCUS_BEHAVIOR_INHERITED && !p_enabled)) && has_focus()) {
  1973. release_focus();
  1974. }
  1975. if (p_skip_non_inherited && data.focus_behavior_recursive != FOCUS_BEHAVIOR_INHERITED) {
  1976. return;
  1977. }
  1978. data.parent_focus_behavior_recursive_enabled = p_enabled;
  1979. for (int i = 0; i < get_child_count(); i++) {
  1980. Control *control = Object::cast_to<Control>(get_child(i));
  1981. if (control) {
  1982. control->_propagate_focus_behavior_recursive_recursively(p_enabled, true);
  1983. }
  1984. }
  1985. }
  1986. bool Control::has_focus(bool p_ignore_hidden_focus) const {
  1987. ERR_READ_THREAD_GUARD_V(false);
  1988. return is_inside_tree() && get_viewport()->_gui_control_has_focus(this, p_ignore_hidden_focus);
  1989. }
  1990. void Control::grab_focus(bool p_hide_focus) {
  1991. ERR_MAIN_THREAD_GUARD;
  1992. ERR_FAIL_COND(!is_inside_tree());
  1993. if (get_focus_mode_with_override() == FOCUS_ACCESSIBILITY) {
  1994. if (!get_tree()->is_accessibility_enabled()) {
  1995. WARN_PRINT("This control can grab focus only when screen reader is active. Use set_focus_mode() and set_focus_behavior_recursive() to allow a control to get focus. Use get_tree().is_accessibility_enabled() to check screen-reader state.");
  1996. return;
  1997. }
  1998. }
  1999. if (get_focus_mode_with_override() == FOCUS_NONE) {
  2000. WARN_PRINT("This control can't grab focus. Use set_focus_mode() and set_focus_behavior_recursive() to allow a control to get focus.");
  2001. return;
  2002. }
  2003. get_viewport()->_gui_control_grab_focus(this, p_hide_focus);
  2004. }
  2005. void Control::grab_click_focus() {
  2006. ERR_MAIN_THREAD_GUARD;
  2007. ERR_FAIL_COND(!is_inside_tree());
  2008. get_viewport()->_gui_grab_click_focus(this);
  2009. }
  2010. void Control::release_focus() {
  2011. ERR_MAIN_THREAD_GUARD;
  2012. ERR_FAIL_COND(!is_inside_tree());
  2013. if (!has_focus()) {
  2014. return;
  2015. }
  2016. get_viewport()->gui_release_focus();
  2017. }
  2018. static Control *_next_control(Control *p_from) {
  2019. if (p_from->is_set_as_top_level()) {
  2020. return nullptr; // Can't go above.
  2021. }
  2022. Control *parent = p_from->get_parent_control();
  2023. if (!parent) {
  2024. return nullptr;
  2025. }
  2026. int next = p_from->get_index();
  2027. ERR_FAIL_INDEX_V(next, parent->get_child_count(), nullptr);
  2028. for (int i = (next + 1); i < parent->get_child_count(); i++) {
  2029. Control *c = Object::cast_to<Control>(parent->get_child(i));
  2030. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  2031. continue;
  2032. }
  2033. return c;
  2034. }
  2035. // No next in parent, try the same in parent.
  2036. return _next_control(parent);
  2037. }
  2038. Control *Control::find_next_valid_focus() const {
  2039. ERR_READ_THREAD_GUARD_V(nullptr);
  2040. // If the focus property is manually overwritten, attempt to use it.
  2041. if (!data.focus_next.is_empty()) {
  2042. Node *n = get_node_or_null(data.focus_next);
  2043. ERR_FAIL_NULL_V_MSG(n, nullptr, "Next focus node path is invalid: '" + String(data.focus_next) + "'.");
  2044. Control *c = Object::cast_to<Control>(n);
  2045. ERR_FAIL_NULL_V_MSG(c, nullptr, "Next focus node is not a control: '" + n->get_name() + "'.");
  2046. if (c->_is_focusable()) {
  2047. return c;
  2048. }
  2049. }
  2050. Control *from = const_cast<Control *>(this);
  2051. HashSet<Control *> checked;
  2052. bool ac_enabled = get_tree() && get_tree()->is_accessibility_enabled();
  2053. // Index of the current `Control` subtree within the containing `Window`.
  2054. int window_next = -1;
  2055. checked.insert(from);
  2056. while (true) {
  2057. // Find next child.
  2058. Control *next_child = nullptr;
  2059. for (int i = 0; i < from->get_child_count(); i++) {
  2060. Control *c = Object::cast_to<Control>(from->get_child(i));
  2061. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  2062. continue;
  2063. }
  2064. next_child = c;
  2065. break;
  2066. }
  2067. if (!next_child) {
  2068. next_child = _next_control(from);
  2069. if (!next_child) { // Nothing else. Go up and find either window or subwindow.
  2070. next_child = const_cast<Control *>(this);
  2071. while (next_child) {
  2072. if (next_child->is_set_as_top_level()) {
  2073. break;
  2074. }
  2075. if (next_child->data.RI) {
  2076. break;
  2077. }
  2078. next_child = next_child->data.parent_control;
  2079. }
  2080. Window *win = next_child == nullptr ? nullptr : next_child->data.parent_window;
  2081. if (win) { // Cycle through `Control` subtrees of the parent window
  2082. if (window_next == -1) {
  2083. window_next = next_child->get_index();
  2084. ERR_FAIL_INDEX_V(window_next, win->get_child_count(), nullptr);
  2085. }
  2086. for (int i = 1; i < win->get_child_count() + 1; i++) {
  2087. int next = Math::wrapi(window_next + i, 0, win->get_child_count());
  2088. Control *c = Object::cast_to<Control>(win->get_child(next));
  2089. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  2090. continue;
  2091. }
  2092. window_next = next;
  2093. next_child = c;
  2094. break;
  2095. }
  2096. }
  2097. }
  2098. }
  2099. if (!next_child) {
  2100. break;
  2101. }
  2102. if ((next_child->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && next_child->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) {
  2103. return next_child;
  2104. }
  2105. if (checked.has(next_child)) {
  2106. return nullptr; // Stuck in a loop with no next control.
  2107. }
  2108. checked.insert(next_child);
  2109. from = next_child; // Try to find the next control with focus mode FOCUS_ALL.
  2110. }
  2111. return nullptr;
  2112. }
  2113. static Control *_prev_control(Control *p_from) {
  2114. for (int i = p_from->get_child_count() - 1; i >= 0; i--) {
  2115. Control *c = Object::cast_to<Control>(p_from->get_child(i));
  2116. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  2117. continue;
  2118. }
  2119. // Find the last child as prev, try the same in the last child.
  2120. return _prev_control(c);
  2121. }
  2122. return p_from; // Not found in the children, return itself.
  2123. }
  2124. Control *Control::find_prev_valid_focus() const {
  2125. ERR_READ_THREAD_GUARD_V(nullptr);
  2126. // If the focus property is manually overwritten, attempt to use it.
  2127. if (!data.focus_prev.is_empty()) {
  2128. Node *n = get_node_or_null(data.focus_prev);
  2129. ERR_FAIL_NULL_V_MSG(n, nullptr, "Previous focus node path is invalid: '" + String(data.focus_prev) + "'.");
  2130. Control *c = Object::cast_to<Control>(n);
  2131. ERR_FAIL_NULL_V_MSG(c, nullptr, "Previous focus node is not a control: '" + n->get_name() + "'.");
  2132. if (c->_is_focusable()) {
  2133. return c;
  2134. }
  2135. }
  2136. Control *from = const_cast<Control *>(this);
  2137. HashSet<Control *> checked;
  2138. bool ac_enabled = get_tree() && get_tree()->is_accessibility_enabled();
  2139. // Index of the current `Control` subtree within the containing `Window`.
  2140. int window_prev = -1;
  2141. checked.insert(from);
  2142. while (true) {
  2143. // Find prev child.
  2144. Control *prev_child = nullptr;
  2145. if (from->is_set_as_top_level() || !from->data.parent_control) {
  2146. // Find last of the children.
  2147. Window *win = from->data.parent_window;
  2148. if (win) { // Cycle through `Control` subtrees of the parent window
  2149. if (window_prev == -1) {
  2150. window_prev = from->get_index();
  2151. ERR_FAIL_INDEX_V(window_prev, win->get_child_count(), nullptr);
  2152. }
  2153. for (int i = 1; i < win->get_child_count() + 1; i++) {
  2154. int prev = Math::wrapi(window_prev - i, 0, win->get_child_count());
  2155. Control *c = Object::cast_to<Control>(win->get_child(prev));
  2156. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  2157. continue;
  2158. }
  2159. window_prev = prev;
  2160. prev_child = _prev_control(c);
  2161. break;
  2162. }
  2163. }
  2164. if (!prev_child) {
  2165. prev_child = _prev_control(from); // Wrap start here.
  2166. }
  2167. } else {
  2168. for (int i = (from->get_index() - 1); i >= 0; i--) {
  2169. Control *c = Object::cast_to<Control>(from->get_parent()->get_child(i));
  2170. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  2171. continue;
  2172. }
  2173. prev_child = c;
  2174. break;
  2175. }
  2176. if (!prev_child) {
  2177. prev_child = from->data.parent_control;
  2178. } else {
  2179. prev_child = _prev_control(prev_child);
  2180. }
  2181. }
  2182. if ((prev_child->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && prev_child->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) {
  2183. return prev_child;
  2184. }
  2185. if (checked.has(prev_child)) {
  2186. return nullptr; // Stuck in a loop with no prev control.
  2187. }
  2188. checked.insert(prev_child);
  2189. from = prev_child; // Try to find the prev control with focus mode FOCUS_ALL.
  2190. }
  2191. return nullptr;
  2192. }
  2193. void Control::set_focus_neighbor(Side p_side, const NodePath &p_neighbor) {
  2194. ERR_MAIN_THREAD_GUARD;
  2195. ERR_FAIL_INDEX((int)p_side, 4);
  2196. data.focus_neighbor[p_side] = p_neighbor;
  2197. }
  2198. NodePath Control::get_focus_neighbor(Side p_side) const {
  2199. ERR_READ_THREAD_GUARD_V(NodePath());
  2200. ERR_FAIL_INDEX_V((int)p_side, 4, NodePath());
  2201. return data.focus_neighbor[p_side];
  2202. }
  2203. void Control::set_focus_next(const NodePath &p_next) {
  2204. ERR_MAIN_THREAD_GUARD;
  2205. data.focus_next = p_next;
  2206. }
  2207. NodePath Control::get_focus_next() const {
  2208. ERR_READ_THREAD_GUARD_V(NodePath());
  2209. return data.focus_next;
  2210. }
  2211. void Control::set_focus_previous(const NodePath &p_prev) {
  2212. ERR_MAIN_THREAD_GUARD;
  2213. data.focus_prev = p_prev;
  2214. }
  2215. NodePath Control::get_focus_previous() const {
  2216. ERR_READ_THREAD_GUARD_V(NodePath());
  2217. return data.focus_prev;
  2218. }
  2219. #define MAX_NEIGHBOR_SEARCH_COUNT 512
  2220. Control *Control::_get_focus_neighbor(Side p_side, int p_count) {
  2221. ERR_FAIL_INDEX_V((int)p_side, 4, nullptr);
  2222. if (p_count >= MAX_NEIGHBOR_SEARCH_COUNT) {
  2223. return nullptr;
  2224. }
  2225. if (!data.focus_neighbor[p_side].is_empty()) {
  2226. Node *n = get_node_or_null(data.focus_neighbor[p_side]);
  2227. ERR_FAIL_NULL_V_MSG(n, nullptr, "Neighbor focus node path is invalid: '" + String(data.focus_neighbor[p_side]) + "'.");
  2228. Control *c = Object::cast_to<Control>(n);
  2229. ERR_FAIL_NULL_V_MSG(c, nullptr, "Neighbor focus node is not a control: '" + n->get_name() + "'.");
  2230. if (c->_is_focusable()) {
  2231. return c;
  2232. }
  2233. c = c->_get_focus_neighbor(p_side, p_count + 1);
  2234. return c;
  2235. }
  2236. real_t square_of_dist = 1e14;
  2237. Control *result = nullptr;
  2238. const Vector2 dir[4] = {
  2239. Vector2(-1, 0),
  2240. Vector2(0, -1),
  2241. Vector2(1, 0),
  2242. Vector2(0, 1)
  2243. };
  2244. Vector2 vdir = dir[p_side];
  2245. Rect2 r = get_global_rect();
  2246. real_t begin_d = vdir.dot(r.get_position());
  2247. real_t end_d = vdir.dot(r.get_end());
  2248. real_t maxd = MAX(begin_d, end_d);
  2249. Rect2 clamp = Rect2(-1e7, -1e7, 2e7, 2e7);
  2250. Rect2 result_rect;
  2251. Node *base = this;
  2252. while (base) {
  2253. ScrollContainer *sc = Object::cast_to<ScrollContainer>(base);
  2254. if (sc) {
  2255. Rect2 sc_r = sc->get_global_rect();
  2256. bool follow_focus = sc->is_following_focus();
  2257. if (result && !follow_focus && !sc_r.intersects(result_rect)) {
  2258. result = nullptr; // Skip invisible control.
  2259. }
  2260. if (result == nullptr) {
  2261. real_t sc_begin_d = vdir.dot(sc_r.get_position());
  2262. real_t sc_end_d = vdir.dot(sc_r.get_end());
  2263. real_t sc_maxd = sc_begin_d;
  2264. real_t sc_mind = sc_end_d;
  2265. if (sc_begin_d < sc_end_d) {
  2266. sc_maxd = sc_end_d;
  2267. sc_mind = sc_begin_d;
  2268. }
  2269. if (!follow_focus && maxd < sc_mind) {
  2270. // Reposition to find visible control.
  2271. maxd = sc_mind;
  2272. r.set_position(r.get_position() + (sc_mind - maxd) * vdir);
  2273. }
  2274. if (follow_focus || sc_maxd > maxd) {
  2275. _window_find_focus_neighbor(vdir, base, r, clamp, maxd, square_of_dist, &result);
  2276. }
  2277. if (result == nullptr) {
  2278. // Reposition to search upwards.
  2279. maxd = sc_maxd;
  2280. r.set_position(r.get_position() + (sc_maxd - maxd) * vdir);
  2281. } else {
  2282. result_rect = result->get_global_rect();
  2283. if (follow_focus) {
  2284. real_t r_begin_d = vdir.dot(result_rect.get_position());
  2285. real_t r_end_d = vdir.dot(result_rect.get_end());
  2286. real_t r_maxd = r_begin_d;
  2287. real_t r_mind = r_end_d;
  2288. if (r_begin_d < r_end_d) {
  2289. r_maxd = r_end_d;
  2290. r_mind = r_begin_d;
  2291. }
  2292. if (r_maxd > sc_maxd) {
  2293. result_rect.set_position(result_rect.get_position() + (sc_maxd - r_maxd) * vdir);
  2294. } else if (r_mind < sc_mind) {
  2295. result_rect.set_position(result_rect.get_position() + (sc_mind - r_mind) * vdir);
  2296. }
  2297. }
  2298. }
  2299. }
  2300. }
  2301. Control *c = Object::cast_to<Control>(base);
  2302. if (c) {
  2303. if (c->data.RI) {
  2304. break;
  2305. }
  2306. }
  2307. base = base->get_parent();
  2308. }
  2309. if (result) {
  2310. return result;
  2311. }
  2312. if (!base) {
  2313. return nullptr;
  2314. }
  2315. _window_find_focus_neighbor(vdir, base, r, clamp, maxd, square_of_dist, &result);
  2316. return result;
  2317. }
  2318. Control *Control::find_valid_focus_neighbor(Side p_side) const {
  2319. return const_cast<Control *>(this)->_get_focus_neighbor(p_side);
  2320. }
  2321. void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, const Rect2 &p_rect, const Rect2 &p_clamp, real_t p_min, real_t &r_closest_dist_squared, Control **r_closest) {
  2322. if (Object::cast_to<Viewport>(p_at)) {
  2323. return; // Bye.
  2324. }
  2325. bool ac_enabled = get_tree() && get_tree()->is_accessibility_enabled();
  2326. Control *c = Object::cast_to<Control>(p_at);
  2327. Container *container = Object::cast_to<Container>(p_at);
  2328. bool in_container = container ? container->is_ancestor_of(this) : false;
  2329. if (c && c != this && ((c->get_focus_mode_with_override() == FOCUS_ALL) || (ac_enabled && c->get_focus_mode_with_override() == FOCUS_ACCESSIBILITY)) && !in_container && p_clamp.intersects(c->get_global_rect())) {
  2330. Rect2 r_c = c->get_global_rect();
  2331. r_c = r_c.intersection(p_clamp);
  2332. real_t begin_d = p_dir.dot(r_c.get_position());
  2333. real_t end_d = p_dir.dot(r_c.get_end());
  2334. real_t max = MAX(begin_d, end_d);
  2335. // Use max to allow navigation to overlapping controls (for ScrollContainer case).
  2336. if (max > (p_min + CMP_EPSILON)) {
  2337. // Calculate the shortest distance. (No shear transform)
  2338. // Flip along axis(es) so that C falls in the first quadrant of c (as origin) for easy calculation.
  2339. // The same transformation would put the direction vector in the positive direction (+x or +y).
  2340. // | -------------
  2341. // | | | |
  2342. // | |-----C-----|
  2343. // ----|---a | | |
  2344. // | | | b------------
  2345. // -|---c---|----------------------->
  2346. // | | |
  2347. // ----|----
  2348. // cC = ca + ab + bC
  2349. // The shortest distance is the vector ab's length or its positive projection length.
  2350. Vector2 cC_origin = r_c.get_center() - p_rect.get_center();
  2351. Vector2 cC = cC_origin.abs(); // Converted to fall in the first quadrant of c.
  2352. Vector2 ab = cC - 0.5 * r_c.get_size() - 0.5 * p_rect.get_size();
  2353. real_t min_d_squared = 0.0;
  2354. if (ab.x > 0.0) {
  2355. min_d_squared += ab.x * ab.x;
  2356. }
  2357. if (ab.y > 0.0) {
  2358. min_d_squared += ab.y * ab.y;
  2359. }
  2360. if (min_d_squared < r_closest_dist_squared || *r_closest == nullptr) {
  2361. r_closest_dist_squared = min_d_squared;
  2362. *r_closest = c;
  2363. } else if (min_d_squared == r_closest_dist_squared) {
  2364. // Tie-breaking aims to address situations where a potential focus neighbor's bounding rect
  2365. // is right next to the currently focused control (e.g. in BoxContainer with
  2366. // separation overridden to 0). This needs specific handling so that the correct
  2367. // focus neighbor is selected.
  2368. Point2 p_center = p_rect.get_center();
  2369. Control *closest = *r_closest;
  2370. Point2 closest_center = closest->get_global_rect().get_center();
  2371. // Tie-break in favor of the control most aligned with p_dir.
  2372. if (Math::abs(p_dir.cross(cC_origin)) < Math::abs(p_dir.cross(closest_center - p_center))) {
  2373. *r_closest = c;
  2374. }
  2375. }
  2376. }
  2377. }
  2378. ScrollContainer *sc = Object::cast_to<ScrollContainer>(c);
  2379. Rect2 intersection = p_clamp;
  2380. if (sc) {
  2381. if (!sc->is_following_focus() || !in_container) {
  2382. intersection = p_clamp.intersection(sc->get_global_rect());
  2383. if (!intersection.has_area()) {
  2384. return;
  2385. }
  2386. }
  2387. }
  2388. for (int i = 0; i < p_at->get_child_count(); i++) {
  2389. Node *child = p_at->get_child(i);
  2390. Control *childc = Object::cast_to<Control>(child);
  2391. if (childc) {
  2392. if (childc->data.RI) {
  2393. continue; // Subwindow, ignore.
  2394. }
  2395. if (!childc->is_visible_in_tree()) {
  2396. continue; // Skip invisible node trees.
  2397. }
  2398. if (Object::cast_to<ScrollContainer>(childc) && childc->is_ancestor_of(this)) {
  2399. continue; // Already searched in it, skip it.
  2400. }
  2401. }
  2402. _window_find_focus_neighbor(p_dir, child, p_rect, intersection, p_min, r_closest_dist_squared, r_closest);
  2403. }
  2404. }
  2405. // Rendering.
  2406. void Control::set_default_cursor_shape(CursorShape p_shape) {
  2407. ERR_MAIN_THREAD_GUARD;
  2408. ERR_FAIL_INDEX(int(p_shape), CURSOR_MAX);
  2409. if (data.default_cursor == p_shape) {
  2410. return;
  2411. }
  2412. data.default_cursor = p_shape;
  2413. if (!is_inside_tree()) {
  2414. return;
  2415. }
  2416. if (!get_global_rect().has_point(get_global_mouse_position())) {
  2417. return;
  2418. }
  2419. // Display the new cursor shape instantly.
  2420. get_viewport()->update_mouse_cursor_state();
  2421. }
  2422. Control::CursorShape Control::get_default_cursor_shape() const {
  2423. ERR_READ_THREAD_GUARD_V(CURSOR_ARROW);
  2424. return data.default_cursor;
  2425. }
  2426. Control::CursorShape Control::get_cursor_shape(const Point2 &p_pos) const {
  2427. ERR_READ_THREAD_GUARD_V(CURSOR_ARROW);
  2428. return data.default_cursor;
  2429. }
  2430. void Control::set_disable_visibility_clip(bool p_ignore) {
  2431. ERR_MAIN_THREAD_GUARD;
  2432. if (data.disable_visibility_clip == p_ignore) {
  2433. return;
  2434. }
  2435. data.disable_visibility_clip = p_ignore;
  2436. queue_redraw();
  2437. }
  2438. bool Control::is_visibility_clip_disabled() const {
  2439. ERR_READ_THREAD_GUARD_V(false);
  2440. return data.disable_visibility_clip;
  2441. }
  2442. void Control::set_clip_contents(bool p_clip) {
  2443. ERR_MAIN_THREAD_GUARD;
  2444. if (data.clip_contents == p_clip) {
  2445. return;
  2446. }
  2447. data.clip_contents = p_clip;
  2448. queue_redraw();
  2449. }
  2450. bool Control::is_clipping_contents() {
  2451. ERR_READ_THREAD_GUARD_V(false);
  2452. return data.clip_contents;
  2453. }
  2454. // Theming.
  2455. void Control::_theme_changed() {
  2456. if (is_inside_tree()) {
  2457. data.theme_owner->propagate_theme_changed(this, this, true, false);
  2458. }
  2459. }
  2460. void Control::_notify_theme_override_changed() {
  2461. if (!data.bulk_theme_override && is_inside_tree()) {
  2462. notification(NOTIFICATION_THEME_CHANGED);
  2463. }
  2464. }
  2465. void Control::_invalidate_theme_cache() {
  2466. data.theme_icon_cache.clear();
  2467. data.theme_style_cache.clear();
  2468. data.theme_font_cache.clear();
  2469. data.theme_font_size_cache.clear();
  2470. data.theme_color_cache.clear();
  2471. data.theme_constant_cache.clear();
  2472. }
  2473. void Control::_update_theme_item_cache() {
  2474. ThemeDB::get_singleton()->update_class_instance_items(this);
  2475. }
  2476. void Control::set_theme_owner_node(Node *p_node) {
  2477. ERR_MAIN_THREAD_GUARD;
  2478. data.theme_owner->set_owner_node(p_node);
  2479. }
  2480. Node *Control::get_theme_owner_node() const {
  2481. ERR_READ_THREAD_GUARD_V(nullptr);
  2482. return data.theme_owner->get_owner_node();
  2483. }
  2484. bool Control::has_theme_owner_node() const {
  2485. ERR_READ_THREAD_GUARD_V(false);
  2486. return data.theme_owner->has_owner_node();
  2487. }
  2488. void Control::set_theme_context(ThemeContext *p_context, bool p_propagate) {
  2489. ERR_MAIN_THREAD_GUARD;
  2490. data.theme_owner->set_owner_context(p_context, p_propagate);
  2491. }
  2492. void Control::set_theme(const Ref<Theme> &p_theme) {
  2493. ERR_MAIN_THREAD_GUARD;
  2494. if (data.theme == p_theme) {
  2495. return;
  2496. }
  2497. if (data.theme.is_valid()) {
  2498. data.theme->disconnect_changed(callable_mp(this, &Control::_theme_changed));
  2499. }
  2500. data.theme = p_theme;
  2501. if (data.theme.is_valid()) {
  2502. data.theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
  2503. data.theme->connect_changed(callable_mp(this, &Control::_theme_changed), CONNECT_DEFERRED);
  2504. return;
  2505. }
  2506. Control *parent_c = Object::cast_to<Control>(get_parent());
  2507. if (parent_c && parent_c->has_theme_owner_node()) {
  2508. data.theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
  2509. return;
  2510. }
  2511. Window *parent_w = cast_to<Window>(get_parent());
  2512. if (parent_w && parent_w->has_theme_owner_node()) {
  2513. data.theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
  2514. return;
  2515. }
  2516. data.theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
  2517. }
  2518. Ref<Theme> Control::get_theme() const {
  2519. ERR_READ_THREAD_GUARD_V(Ref<Theme>());
  2520. return data.theme;
  2521. }
  2522. void Control::set_theme_type_variation(const StringName &p_theme_type) {
  2523. ERR_MAIN_THREAD_GUARD;
  2524. if (data.theme_type_variation == p_theme_type) {
  2525. return;
  2526. }
  2527. data.theme_type_variation = p_theme_type;
  2528. if (is_inside_tree()) {
  2529. notification(NOTIFICATION_THEME_CHANGED);
  2530. }
  2531. }
  2532. StringName Control::get_theme_type_variation() const {
  2533. ERR_READ_THREAD_GUARD_V(StringName());
  2534. return data.theme_type_variation;
  2535. }
  2536. /// Theme property lookup.
  2537. Ref<Texture2D> Control::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  2538. ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
  2539. if (!data.initialized) {
  2540. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2541. }
  2542. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2543. const Ref<Texture2D> *tex = data.theme_icon_override.getptr(p_name);
  2544. if (tex) {
  2545. return *tex;
  2546. }
  2547. }
  2548. if (data.theme_icon_cache.has(p_theme_type) && data.theme_icon_cache[p_theme_type].has(p_name)) {
  2549. return data.theme_icon_cache[p_theme_type][p_name];
  2550. }
  2551. Vector<StringName> theme_types;
  2552. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2553. Ref<Texture2D> icon = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  2554. data.theme_icon_cache[p_theme_type][p_name] = icon;
  2555. return icon;
  2556. }
  2557. Ref<StyleBox> Control::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  2558. ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
  2559. if (!data.initialized) {
  2560. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2561. }
  2562. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2563. const Ref<StyleBox> *style = data.theme_style_override.getptr(p_name);
  2564. if (style) {
  2565. return *style;
  2566. }
  2567. }
  2568. if (data.theme_style_cache.has(p_theme_type) && data.theme_style_cache[p_theme_type].has(p_name)) {
  2569. return data.theme_style_cache[p_theme_type][p_name];
  2570. }
  2571. Vector<StringName> theme_types;
  2572. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2573. Ref<StyleBox> style = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  2574. data.theme_style_cache[p_theme_type][p_name] = style;
  2575. return style;
  2576. }
  2577. Ref<Font> Control::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  2578. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  2579. if (!data.initialized) {
  2580. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2581. }
  2582. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2583. const Ref<Font> *font = data.theme_font_override.getptr(p_name);
  2584. if (font) {
  2585. return *font;
  2586. }
  2587. }
  2588. if (data.theme_font_cache.has(p_theme_type) && data.theme_font_cache[p_theme_type].has(p_name)) {
  2589. return data.theme_font_cache[p_theme_type][p_name];
  2590. }
  2591. Vector<StringName> theme_types;
  2592. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2593. Ref<Font> font = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  2594. data.theme_font_cache[p_theme_type][p_name] = font;
  2595. return font;
  2596. }
  2597. int Control::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  2598. ERR_READ_THREAD_GUARD_V(0);
  2599. if (!data.initialized) {
  2600. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2601. }
  2602. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2603. const int *font_size = data.theme_font_size_override.getptr(p_name);
  2604. if (font_size && (*font_size) > 0) {
  2605. return *font_size;
  2606. }
  2607. }
  2608. if (data.theme_font_size_cache.has(p_theme_type) && data.theme_font_size_cache[p_theme_type].has(p_name)) {
  2609. return data.theme_font_size_cache[p_theme_type][p_name];
  2610. }
  2611. Vector<StringName> theme_types;
  2612. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2613. int font_size = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  2614. data.theme_font_size_cache[p_theme_type][p_name] = font_size;
  2615. return font_size;
  2616. }
  2617. Color Control::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  2618. ERR_READ_THREAD_GUARD_V(Color());
  2619. if (!data.initialized) {
  2620. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2621. }
  2622. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2623. const Color *color = data.theme_color_override.getptr(p_name);
  2624. if (color) {
  2625. return *color;
  2626. }
  2627. }
  2628. if (data.theme_color_cache.has(p_theme_type) && data.theme_color_cache[p_theme_type].has(p_name)) {
  2629. return data.theme_color_cache[p_theme_type][p_name];
  2630. }
  2631. Vector<StringName> theme_types;
  2632. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2633. Color color = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  2634. data.theme_color_cache[p_theme_type][p_name] = color;
  2635. return color;
  2636. }
  2637. int Control::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  2638. ERR_READ_THREAD_GUARD_V(0);
  2639. if (!data.initialized) {
  2640. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2641. }
  2642. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2643. const int *constant = data.theme_constant_override.getptr(p_name);
  2644. if (constant) {
  2645. return *constant;
  2646. }
  2647. }
  2648. if (data.theme_constant_cache.has(p_theme_type) && data.theme_constant_cache[p_theme_type].has(p_name)) {
  2649. return data.theme_constant_cache[p_theme_type][p_name];
  2650. }
  2651. Vector<StringName> theme_types;
  2652. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2653. int constant = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  2654. data.theme_constant_cache[p_theme_type][p_name] = constant;
  2655. return constant;
  2656. }
  2657. Variant Control::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  2658. switch (p_data_type) {
  2659. case Theme::DATA_TYPE_COLOR:
  2660. return get_theme_color(p_name, p_theme_type);
  2661. case Theme::DATA_TYPE_CONSTANT:
  2662. return get_theme_constant(p_name, p_theme_type);
  2663. case Theme::DATA_TYPE_FONT:
  2664. return get_theme_font(p_name, p_theme_type);
  2665. case Theme::DATA_TYPE_FONT_SIZE:
  2666. return get_theme_font_size(p_name, p_theme_type);
  2667. case Theme::DATA_TYPE_ICON:
  2668. return get_theme_icon(p_name, p_theme_type);
  2669. case Theme::DATA_TYPE_STYLEBOX:
  2670. return get_theme_stylebox(p_name, p_theme_type);
  2671. case Theme::DATA_TYPE_MAX:
  2672. break; // Can't happen, but silences warning.
  2673. }
  2674. return Variant();
  2675. }
  2676. Variant Control::get_used_theme_item(const String &p_full_name, const StringName &p_theme_type) const {
  2677. if (p_full_name.begins_with("theme_override_icons/")) {
  2678. String name = p_full_name.substr(strlen("theme_override_icons/"));
  2679. if (has_theme_icon(name)) { // Exclude cached and default ones.
  2680. return get_theme_icon(name);
  2681. }
  2682. } else if (p_full_name.begins_with("theme_override_styles/")) {
  2683. String name = p_full_name.substr(strlen("theme_override_styles/"));
  2684. if (has_theme_stylebox(name)) {
  2685. return get_theme_stylebox(name);
  2686. }
  2687. } else if (p_full_name.begins_with("theme_override_fonts/")) {
  2688. String name = p_full_name.substr(strlen("theme_override_fonts/"));
  2689. if (has_theme_font(name)) {
  2690. return get_theme_font(name);
  2691. }
  2692. } else if (p_full_name.begins_with("theme_override_font_sizes/")) {
  2693. String name = p_full_name.substr(strlen("theme_override_font_sizes/"));
  2694. if (has_theme_font_size(name)) {
  2695. return get_theme_font_size(name);
  2696. }
  2697. } else if (p_full_name.begins_with("theme_override_colors/")) {
  2698. String name = p_full_name.substr(strlen("theme_override_colors/"));
  2699. if (has_theme_color(name)) {
  2700. return get_theme_color(name);
  2701. }
  2702. } else if (p_full_name.begins_with("theme_override_constants/")) {
  2703. String name = p_full_name.substr(strlen("theme_override_constants/"));
  2704. if (has_theme_constant(name)) {
  2705. return get_theme_constant(name);
  2706. }
  2707. } else {
  2708. ERR_FAIL_V_MSG(Variant(), vformat("The property %s is not a theme item.", p_full_name));
  2709. }
  2710. return Variant();
  2711. }
  2712. #ifdef TOOLS_ENABLED
  2713. Ref<Texture2D> Control::get_editor_theme_icon(const StringName &p_name) const {
  2714. return get_theme_icon(p_name, SNAME("EditorIcons"));
  2715. }
  2716. #endif // TOOLS_ENABLED
  2717. bool Control::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  2718. ERR_READ_THREAD_GUARD_V(false);
  2719. if (!data.initialized) {
  2720. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2721. }
  2722. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2723. if (has_theme_icon_override(p_name)) {
  2724. return true;
  2725. }
  2726. }
  2727. Vector<StringName> theme_types;
  2728. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2729. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  2730. }
  2731. bool Control::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  2732. ERR_READ_THREAD_GUARD_V(false);
  2733. if (!data.initialized) {
  2734. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2735. }
  2736. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2737. if (has_theme_stylebox_override(p_name)) {
  2738. return true;
  2739. }
  2740. }
  2741. Vector<StringName> theme_types;
  2742. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2743. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  2744. }
  2745. bool Control::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  2746. ERR_READ_THREAD_GUARD_V(false);
  2747. if (!data.initialized) {
  2748. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2749. }
  2750. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2751. if (has_theme_font_override(p_name)) {
  2752. return true;
  2753. }
  2754. }
  2755. Vector<StringName> theme_types;
  2756. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2757. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  2758. }
  2759. bool Control::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  2760. ERR_READ_THREAD_GUARD_V(false);
  2761. if (!data.initialized) {
  2762. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2763. }
  2764. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2765. if (has_theme_font_size_override(p_name)) {
  2766. return true;
  2767. }
  2768. }
  2769. Vector<StringName> theme_types;
  2770. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2771. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  2772. }
  2773. bool Control::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  2774. ERR_READ_THREAD_GUARD_V(false);
  2775. if (!data.initialized) {
  2776. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2777. }
  2778. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2779. if (has_theme_color_override(p_name)) {
  2780. return true;
  2781. }
  2782. }
  2783. Vector<StringName> theme_types;
  2784. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2785. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  2786. }
  2787. bool Control::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  2788. ERR_READ_THREAD_GUARD_V(false);
  2789. if (!data.initialized) {
  2790. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2791. }
  2792. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2793. if (has_theme_constant_override(p_name)) {
  2794. return true;
  2795. }
  2796. }
  2797. Vector<StringName> theme_types;
  2798. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2799. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  2800. }
  2801. /// Local property overrides.
  2802. void Control::add_theme_icon_override(const StringName &p_name, RequiredParam<Texture2D> rp_icon) {
  2803. ERR_MAIN_THREAD_GUARD;
  2804. EXTRACT_PARAM_OR_FAIL(p_icon, rp_icon);
  2805. if (data.theme_icon_override.has(p_name)) {
  2806. data.theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2807. }
  2808. data.theme_icon_override[p_name] = p_icon;
  2809. data.theme_icon_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2810. _notify_theme_override_changed();
  2811. }
  2812. void Control::add_theme_style_override(const StringName &p_name, RequiredParam<StyleBox> rp_style) {
  2813. ERR_MAIN_THREAD_GUARD;
  2814. EXTRACT_PARAM_OR_FAIL(p_style, rp_style);
  2815. if (data.theme_style_override.has(p_name)) {
  2816. data.theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2817. }
  2818. data.theme_style_override[p_name] = p_style;
  2819. data.theme_style_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2820. _notify_theme_override_changed();
  2821. }
  2822. void Control::add_theme_font_override(const StringName &p_name, RequiredParam<Font> rp_font) {
  2823. ERR_MAIN_THREAD_GUARD;
  2824. EXTRACT_PARAM_OR_FAIL(p_font, rp_font);
  2825. if (data.theme_font_override.has(p_name)) {
  2826. data.theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2827. }
  2828. data.theme_font_override[p_name] = p_font;
  2829. data.theme_font_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2830. _notify_theme_override_changed();
  2831. }
  2832. void Control::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
  2833. ERR_MAIN_THREAD_GUARD;
  2834. data.theme_font_size_override[p_name] = p_font_size;
  2835. _notify_theme_override_changed();
  2836. }
  2837. void Control::add_theme_color_override(const StringName &p_name, const Color &p_color) {
  2838. ERR_MAIN_THREAD_GUARD;
  2839. data.theme_color_override[p_name] = p_color;
  2840. _notify_theme_override_changed();
  2841. }
  2842. void Control::add_theme_constant_override(const StringName &p_name, int p_constant) {
  2843. ERR_MAIN_THREAD_GUARD;
  2844. data.theme_constant_override[p_name] = p_constant;
  2845. _notify_theme_override_changed();
  2846. }
  2847. void Control::remove_theme_icon_override(const StringName &p_name) {
  2848. ERR_MAIN_THREAD_GUARD;
  2849. if (data.theme_icon_override.has(p_name)) {
  2850. data.theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2851. }
  2852. data.theme_icon_override.erase(p_name);
  2853. _notify_theme_override_changed();
  2854. }
  2855. void Control::remove_theme_style_override(const StringName &p_name) {
  2856. ERR_MAIN_THREAD_GUARD;
  2857. if (data.theme_style_override.has(p_name)) {
  2858. data.theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2859. }
  2860. data.theme_style_override.erase(p_name);
  2861. _notify_theme_override_changed();
  2862. }
  2863. void Control::remove_theme_font_override(const StringName &p_name) {
  2864. ERR_MAIN_THREAD_GUARD;
  2865. if (data.theme_font_override.has(p_name)) {
  2866. data.theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2867. }
  2868. data.theme_font_override.erase(p_name);
  2869. _notify_theme_override_changed();
  2870. }
  2871. void Control::remove_theme_font_size_override(const StringName &p_name) {
  2872. ERR_MAIN_THREAD_GUARD;
  2873. data.theme_font_size_override.erase(p_name);
  2874. _notify_theme_override_changed();
  2875. }
  2876. void Control::remove_theme_color_override(const StringName &p_name) {
  2877. ERR_MAIN_THREAD_GUARD;
  2878. data.theme_color_override.erase(p_name);
  2879. _notify_theme_override_changed();
  2880. }
  2881. void Control::remove_theme_constant_override(const StringName &p_name) {
  2882. ERR_MAIN_THREAD_GUARD;
  2883. data.theme_constant_override.erase(p_name);
  2884. _notify_theme_override_changed();
  2885. }
  2886. bool Control::has_theme_icon_override(const StringName &p_name) const {
  2887. ERR_READ_THREAD_GUARD_V(false);
  2888. const Ref<Texture2D> *tex = data.theme_icon_override.getptr(p_name);
  2889. return tex != nullptr;
  2890. }
  2891. bool Control::has_theme_stylebox_override(const StringName &p_name) const {
  2892. ERR_READ_THREAD_GUARD_V(false);
  2893. const Ref<StyleBox> *style = data.theme_style_override.getptr(p_name);
  2894. return style != nullptr;
  2895. }
  2896. bool Control::has_theme_font_override(const StringName &p_name) const {
  2897. ERR_READ_THREAD_GUARD_V(false);
  2898. const Ref<Font> *font = data.theme_font_override.getptr(p_name);
  2899. return font != nullptr;
  2900. }
  2901. bool Control::has_theme_font_size_override(const StringName &p_name) const {
  2902. ERR_READ_THREAD_GUARD_V(false);
  2903. const int *font_size = data.theme_font_size_override.getptr(p_name);
  2904. return font_size != nullptr;
  2905. }
  2906. bool Control::has_theme_color_override(const StringName &p_name) const {
  2907. ERR_READ_THREAD_GUARD_V(false);
  2908. const Color *color = data.theme_color_override.getptr(p_name);
  2909. return color != nullptr;
  2910. }
  2911. bool Control::has_theme_constant_override(const StringName &p_name) const {
  2912. ERR_READ_THREAD_GUARD_V(false);
  2913. const int *constant = data.theme_constant_override.getptr(p_name);
  2914. return constant != nullptr;
  2915. }
  2916. /// Default theme properties.
  2917. float Control::get_theme_default_base_scale() const {
  2918. ERR_READ_THREAD_GUARD_V(0);
  2919. return data.theme_owner->get_theme_default_base_scale();
  2920. }
  2921. Ref<Font> Control::get_theme_default_font() const {
  2922. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  2923. return data.theme_owner->get_theme_default_font();
  2924. }
  2925. int Control::get_theme_default_font_size() const {
  2926. ERR_READ_THREAD_GUARD_V(0);
  2927. return data.theme_owner->get_theme_default_font_size();
  2928. }
  2929. /// Bulk actions.
  2930. void Control::begin_bulk_theme_override() {
  2931. ERR_MAIN_THREAD_GUARD;
  2932. data.bulk_theme_override = true;
  2933. }
  2934. void Control::end_bulk_theme_override() {
  2935. ERR_MAIN_THREAD_GUARD;
  2936. ERR_FAIL_COND(!data.bulk_theme_override);
  2937. data.bulk_theme_override = false;
  2938. _notify_theme_override_changed();
  2939. }
  2940. // Internationalization.
  2941. TypedArray<Vector3i> Control::structured_text_parser(TextServer::StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const {
  2942. ERR_READ_THREAD_GUARD_V(TypedArray<Vector3i>());
  2943. if (p_parser_type == TextServer::STRUCTURED_TEXT_CUSTOM) {
  2944. TypedArray<Vector3i> ret;
  2945. GDVIRTUAL_CALL(_structured_text_parser, p_args, p_text, ret);
  2946. return ret;
  2947. } else {
  2948. return TS->parse_structured_text(p_parser_type, p_args, p_text);
  2949. }
  2950. }
  2951. void Control::set_layout_direction(Control::LayoutDirection p_direction) {
  2952. ERR_MAIN_THREAD_GUARD;
  2953. if (data.layout_dir == p_direction) {
  2954. return;
  2955. }
  2956. ERR_FAIL_INDEX(p_direction, LAYOUT_DIRECTION_MAX);
  2957. data.layout_dir = p_direction;
  2958. propagate_notification(NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  2959. }
  2960. Control::LayoutDirection Control::get_layout_direction() const {
  2961. ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
  2962. return data.layout_dir;
  2963. }
  2964. bool Control::is_layout_rtl() const {
  2965. ERR_READ_THREAD_GUARD_V(false);
  2966. if (data.is_rtl_dirty) {
  2967. data.is_rtl_dirty = false;
  2968. if (data.layout_dir == LAYOUT_DIRECTION_INHERITED) {
  2969. #ifdef TOOLS_ENABLED
  2970. if (is_part_of_edited_scene() && GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
  2971. data.is_rtl = true;
  2972. return data.is_rtl;
  2973. }
  2974. if (is_inside_tree()) {
  2975. Node *edited_scene_root = get_tree()->get_edited_scene_root();
  2976. if (edited_scene_root == this) {
  2977. int proj_root_layout_direction = GLOBAL_GET_CACHED(int, "internationalization/rendering/root_node_layout_direction");
  2978. if (proj_root_layout_direction == 1) {
  2979. data.is_rtl = false;
  2980. } else if (proj_root_layout_direction == 2) {
  2981. data.is_rtl = true;
  2982. } else if (proj_root_layout_direction == 3) {
  2983. data.is_rtl = TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
  2984. } else {
  2985. data.is_rtl = TS->is_locale_right_to_left(_get_locale());
  2986. }
  2987. return data.is_rtl;
  2988. }
  2989. }
  2990. #else
  2991. if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
  2992. data.is_rtl = true;
  2993. return data.is_rtl;
  2994. }
  2995. #endif // TOOLS_ENABLED
  2996. const StringName domain_name = get_translation_domain();
  2997. Node *parent_node = get_parent();
  2998. while (parent_node && domain_name == parent_node->get_translation_domain()) {
  2999. Control *parent_control = Object::cast_to<Control>(parent_node);
  3000. if (parent_control) {
  3001. data.is_rtl = parent_control->is_layout_rtl();
  3002. return data.is_rtl;
  3003. }
  3004. Window *parent_window = Object::cast_to<Window>(parent_node);
  3005. if (parent_window) {
  3006. data.is_rtl = parent_window->is_layout_rtl();
  3007. return data.is_rtl;
  3008. }
  3009. parent_node = parent_node->get_parent();
  3010. }
  3011. if (root_layout_direction == 1) {
  3012. data.is_rtl = false;
  3013. } else if (root_layout_direction == 2) {
  3014. data.is_rtl = true;
  3015. } else if (root_layout_direction == 3) {
  3016. String locale = OS::get_singleton()->get_locale();
  3017. data.is_rtl = TS->is_locale_right_to_left(locale);
  3018. } else {
  3019. data.is_rtl = TS->is_locale_right_to_left(_get_locale());
  3020. }
  3021. } else if (data.layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
  3022. if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
  3023. data.is_rtl = true;
  3024. } else {
  3025. data.is_rtl = TS->is_locale_right_to_left(_get_locale());
  3026. }
  3027. } else if (data.layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
  3028. if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
  3029. data.is_rtl = true;
  3030. } else {
  3031. data.is_rtl = TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
  3032. }
  3033. } else {
  3034. data.is_rtl = (data.layout_dir == LAYOUT_DIRECTION_RTL);
  3035. }
  3036. }
  3037. return data.is_rtl;
  3038. }
  3039. void Control::set_localize_numeral_system(bool p_enable) {
  3040. ERR_MAIN_THREAD_GUARD;
  3041. if (p_enable == data.localize_numeral_system) {
  3042. return;
  3043. }
  3044. data.localize_numeral_system = p_enable;
  3045. notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  3046. }
  3047. bool Control::is_localizing_numeral_system() const {
  3048. ERR_READ_THREAD_GUARD_V(false);
  3049. return data.localize_numeral_system;
  3050. }
  3051. #ifndef DISABLE_DEPRECATED
  3052. void Control::set_auto_translate(bool p_enable) {
  3053. ERR_MAIN_THREAD_GUARD;
  3054. set_auto_translate_mode(p_enable ? AUTO_TRANSLATE_MODE_ALWAYS : AUTO_TRANSLATE_MODE_DISABLED);
  3055. }
  3056. bool Control::is_auto_translating() const {
  3057. ERR_READ_THREAD_GUARD_V(false);
  3058. return can_auto_translate();
  3059. }
  3060. #endif // DISABLE_DEPRECATED
  3061. void Control::set_tooltip_auto_translate_mode(AutoTranslateMode p_mode) {
  3062. ERR_MAIN_THREAD_GUARD;
  3063. data.tooltip_auto_translate_mode = p_mode;
  3064. }
  3065. Node::AutoTranslateMode Control::get_tooltip_auto_translate_mode() const {
  3066. ERR_READ_THREAD_GUARD_V(AUTO_TRANSLATE_MODE_INHERIT);
  3067. return data.tooltip_auto_translate_mode;
  3068. }
  3069. // Extra properties.
  3070. void Control::set_tooltip_text(const String &p_hint) {
  3071. ERR_MAIN_THREAD_GUARD;
  3072. data.tooltip = p_hint;
  3073. update_configuration_warnings();
  3074. }
  3075. String Control::get_tooltip_text() const {
  3076. ERR_READ_THREAD_GUARD_V(String());
  3077. return data.tooltip;
  3078. }
  3079. String Control::get_tooltip(const Point2 &p_pos) const {
  3080. ERR_READ_THREAD_GUARD_V(String());
  3081. String ret;
  3082. if (GDVIRTUAL_CALL(_get_tooltip, p_pos, ret)) {
  3083. return ret;
  3084. }
  3085. return data.tooltip;
  3086. }
  3087. String Control::accessibility_get_contextual_info() const {
  3088. ERR_READ_THREAD_GUARD_V(String());
  3089. String ret;
  3090. GDVIRTUAL_CALL(_accessibility_get_contextual_info, ret);
  3091. return ret;
  3092. }
  3093. Control *Control::make_custom_tooltip(const String &p_text) const {
  3094. ERR_READ_THREAD_GUARD_V(nullptr);
  3095. Object *ret = nullptr;
  3096. GDVIRTUAL_CALL(_make_custom_tooltip, p_text, ret);
  3097. return Object::cast_to<Control>(ret);
  3098. }
  3099. // Base object overrides.
  3100. void Control::_accessibility_action_foucs(const Variant &p_data) {
  3101. grab_focus();
  3102. }
  3103. void Control::_accessibility_action_blur(const Variant &p_data) {
  3104. release_focus();
  3105. }
  3106. void Control::_accessibility_action_show_tooltip(const Variant &p_data) {
  3107. Viewport *vp = get_viewport();
  3108. if (vp) {
  3109. vp->show_tooltip(this);
  3110. }
  3111. }
  3112. void Control::_accessibility_action_hide_tooltip(const Variant &p_data) {
  3113. Viewport *vp = get_viewport();
  3114. if (vp) {
  3115. vp->cancel_tooltip();
  3116. }
  3117. }
  3118. void Control::_accessibility_action_scroll_into_view(const Variant &p_data) {
  3119. ScrollContainer *sc = Object::cast_to<ScrollContainer>(get_parent());
  3120. if (sc) {
  3121. sc->ensure_control_visible(this);
  3122. }
  3123. }
  3124. void Control::_notification(int p_notification) {
  3125. ERR_MAIN_THREAD_GUARD;
  3126. switch (p_notification) {
  3127. #ifdef TOOLS_ENABLED
  3128. case NOTIFICATION_EDITOR_PRE_SAVE: {
  3129. saving = true;
  3130. } break;
  3131. case NOTIFICATION_EDITOR_POST_SAVE: {
  3132. saving = false;
  3133. } break;
  3134. #endif // TOOLS_ENABLED
  3135. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  3136. RID ae = get_accessibility_element();
  3137. ERR_FAIL_COND(ae.is_null());
  3138. // Base info.
  3139. if (get_parent_control()) {
  3140. String container_info = get_parent_control()->get_accessibility_container_name(this);
  3141. DisplayServer::get_singleton()->accessibility_update_set_name(ae, container_info.is_empty() ? get_accessibility_name() : get_accessibility_name() + " " + container_info);
  3142. } else {
  3143. DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_accessibility_name());
  3144. }
  3145. DisplayServer::get_singleton()->accessibility_update_set_description(ae, get_accessibility_description());
  3146. DisplayServer::get_singleton()->accessibility_update_set_live(ae, get_accessibility_live());
  3147. DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform());
  3148. DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Vector2(), data.size_cache));
  3149. DisplayServer::get_singleton()->accessibility_update_set_tooltip(ae, data.tooltip);
  3150. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_CLIPS_CHILDREN, data.clip_contents);
  3151. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_TOUCH_PASSTHROUGH, data.mouse_filter == MOUSE_FILTER_PASS);
  3152. if (_is_focusable()) {
  3153. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Control::_accessibility_action_foucs));
  3154. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &Control::_accessibility_action_blur));
  3155. }
  3156. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_TOOLTIP, callable_mp(this, &Control::_accessibility_action_show_tooltip));
  3157. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_HIDE_TOOLTIP, callable_mp(this, &Control::_accessibility_action_hide_tooltip));
  3158. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &Control::_accessibility_action_scroll_into_view));
  3159. if (is_inside_tree() && get_viewport()->gui_is_dragging()) {
  3160. if (can_drop_data(Vector2(Math::INF, Math::INF), get_viewport()->gui_get_drag_data())) {
  3161. DisplayServer::get_singleton()->accessibility_update_set_extra_info(ae, vformat(RTR("%s can be dropped here. Use %s to drop, use %s to cancel."), get_viewport()->gui_get_drag_description(), InputMap::get_singleton()->get_action_description("ui_accessibility_drag_and_drop"), InputMap::get_singleton()->get_action_description("ui_cancel")));
  3162. } else {
  3163. DisplayServer::get_singleton()->accessibility_update_set_extra_info(ae, vformat(RTR("%s can not be dropped here. Use %s to cancel."), get_viewport()->gui_get_drag_description(), InputMap::get_singleton()->get_action_description("ui_cancel")));
  3164. }
  3165. }
  3166. // Related nodes.
  3167. for (int i = 0; i < data.accessibility_controls_nodes.size(); i++) {
  3168. const NodePath &np = data.accessibility_controls_nodes[i];
  3169. if (!np.is_empty()) {
  3170. Node *n = get_node(np);
  3171. if (n && !n->is_part_of_edited_scene()) {
  3172. DisplayServer::get_singleton()->accessibility_update_add_related_controls(ae, n->get_accessibility_element());
  3173. }
  3174. }
  3175. }
  3176. for (int i = 0; i < data.accessibility_described_by_nodes.size(); i++) {
  3177. const NodePath &np = data.accessibility_described_by_nodes[i];
  3178. if (!np.is_empty()) {
  3179. Node *n = get_node(np);
  3180. if (n && !n->is_part_of_edited_scene()) {
  3181. DisplayServer::get_singleton()->accessibility_update_add_related_described_by(ae, n->get_accessibility_element());
  3182. }
  3183. }
  3184. }
  3185. for (int i = 0; i < data.accessibility_labeled_by_nodes.size(); i++) {
  3186. const NodePath &np = data.accessibility_labeled_by_nodes[i];
  3187. if (!np.is_empty()) {
  3188. Node *n = get_node(np);
  3189. if (n && !n->is_part_of_edited_scene()) {
  3190. DisplayServer::get_singleton()->accessibility_update_add_related_labeled_by(ae, n->get_accessibility_element());
  3191. }
  3192. }
  3193. }
  3194. for (int i = 0; i < data.accessibility_flow_to_nodes.size(); i++) {
  3195. const NodePath &np = data.accessibility_flow_to_nodes[i];
  3196. if (!np.is_empty()) {
  3197. Node *n = get_node(np);
  3198. if (n && !n->is_part_of_edited_scene()) {
  3199. DisplayServer::get_singleton()->accessibility_update_add_related_flow_to(ae, n->get_accessibility_element());
  3200. }
  3201. }
  3202. }
  3203. } break;
  3204. case NOTIFICATION_POSTINITIALIZE: {
  3205. data.initialized = true;
  3206. _invalidate_theme_cache();
  3207. _update_theme_item_cache();
  3208. } break;
  3209. case NOTIFICATION_PARENTED: {
  3210. Node *parent_node = get_parent();
  3211. data.parent_control = Object::cast_to<Control>(parent_node);
  3212. data.parent_window = Object::cast_to<Window>(parent_node);
  3213. data.theme_owner->assign_theme_on_parented(this);
  3214. _update_layout_mode();
  3215. _update_focus_behavior_recursive();
  3216. _update_mouse_behavior_recursive();
  3217. } break;
  3218. case NOTIFICATION_UNPARENTED: {
  3219. data.parent_control = nullptr;
  3220. data.parent_window = nullptr;
  3221. data.theme_owner->clear_theme_on_unparented(this);
  3222. } break;
  3223. case NOTIFICATION_ENTER_TREE: {
  3224. // Emits NOTIFICATION_THEME_CHANGED internally.
  3225. set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
  3226. } break;
  3227. case NOTIFICATION_POST_ENTER_TREE: {
  3228. data.is_rtl_dirty = true;
  3229. update_minimum_size();
  3230. _size_changed();
  3231. } break;
  3232. case NOTIFICATION_EXIT_TREE: {
  3233. set_theme_context(nullptr, false);
  3234. release_focus();
  3235. get_viewport()->_gui_remove_control(this);
  3236. } break;
  3237. case NOTIFICATION_READY: {
  3238. #ifdef DEBUG_ENABLED
  3239. connect(SceneStringName(ready), callable_mp(this, &Control::_clear_size_warning), CONNECT_DEFERRED | CONNECT_ONE_SHOT);
  3240. #endif // DEBUG_ENABLED
  3241. } break;
  3242. case NOTIFICATION_ENTER_CANVAS: {
  3243. data.is_rtl_dirty = true;
  3244. CanvasItem *node = this;
  3245. bool has_parent_control = false;
  3246. while (!node->is_set_as_top_level()) {
  3247. CanvasItem *parent = Object::cast_to<CanvasItem>(node->get_parent());
  3248. if (!parent) {
  3249. break;
  3250. }
  3251. Control *parent_control = Object::cast_to<Control>(parent);
  3252. if (parent_control) {
  3253. has_parent_control = true;
  3254. break;
  3255. }
  3256. node = parent;
  3257. }
  3258. if (has_parent_control) {
  3259. // Do nothing, has a parent control.
  3260. } else {
  3261. // Is a regular root control or top_level.
  3262. Viewport *viewport = get_viewport();
  3263. ERR_FAIL_NULL(viewport);
  3264. data.RI = viewport->_gui_add_root_control(this);
  3265. get_parent()->connect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty), CONNECT_REFERENCE_COUNTED);
  3266. }
  3267. data.parent_canvas_item = get_parent_item();
  3268. if (data.parent_canvas_item) {
  3269. data.parent_canvas_item->connect(SceneStringName(item_rect_changed), callable_mp(this, &Control::_size_changed));
  3270. } else {
  3271. // Connect viewport.
  3272. Viewport *viewport = get_viewport();
  3273. ERR_FAIL_NULL(viewport);
  3274. viewport->connect("size_changed", callable_mp(this, &Control::_size_changed));
  3275. }
  3276. } break;
  3277. case NOTIFICATION_EXIT_CANVAS: {
  3278. if (data.parent_canvas_item) {
  3279. data.parent_canvas_item->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &Control::_size_changed));
  3280. data.parent_canvas_item = nullptr;
  3281. } else {
  3282. // Disconnect viewport.
  3283. Viewport *viewport = get_viewport();
  3284. ERR_FAIL_NULL(viewport);
  3285. viewport->disconnect("size_changed", callable_mp(this, &Control::_size_changed));
  3286. }
  3287. if (data.RI) {
  3288. get_viewport()->_gui_remove_root_control(data.RI);
  3289. data.RI = nullptr;
  3290. get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty));
  3291. }
  3292. data.parent_canvas_item = nullptr;
  3293. data.is_rtl_dirty = true;
  3294. } break;
  3295. case NOTIFICATION_CHILD_ORDER_CHANGED: {
  3296. // Some parents need to know the order of the children to draw (like TabContainer),
  3297. // so we update them just in case.
  3298. queue_redraw();
  3299. } break;
  3300. case NOTIFICATION_RESIZED: {
  3301. emit_signal(SceneStringName(resized));
  3302. } break;
  3303. case NOTIFICATION_DRAW: {
  3304. _update_canvas_item_transform();
  3305. RenderingServer::get_singleton()->canvas_item_set_custom_rect(get_canvas_item(), !data.disable_visibility_clip, Rect2(Point2(), get_size()));
  3306. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), data.clip_contents);
  3307. } break;
  3308. case NOTIFICATION_FOCUS_ENTER: {
  3309. emit_signal(SceneStringName(focus_entered));
  3310. queue_redraw();
  3311. } break;
  3312. case NOTIFICATION_FOCUS_EXIT: {
  3313. emit_signal(SceneStringName(focus_exited));
  3314. queue_redraw();
  3315. } break;
  3316. case NOTIFICATION_THEME_CHANGED: {
  3317. emit_signal(SceneStringName(theme_changed));
  3318. _invalidate_theme_cache();
  3319. _update_theme_item_cache();
  3320. queue_redraw();
  3321. update_minimum_size();
  3322. _size_changed();
  3323. } break;
  3324. case NOTIFICATION_VISIBILITY_CHANGED: {
  3325. if (!is_visible_in_tree()) {
  3326. if (get_viewport() != nullptr) {
  3327. get_viewport()->_gui_hide_control(this);
  3328. }
  3329. } else {
  3330. update_minimum_size();
  3331. _size_changed();
  3332. }
  3333. } break;
  3334. case NOTIFICATION_TRANSLATION_CHANGED:
  3335. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  3336. if (is_inside_tree()) {
  3337. data.is_rtl_dirty = true;
  3338. _invalidate_theme_cache();
  3339. _update_theme_item_cache();
  3340. queue_redraw();
  3341. update_minimum_size();
  3342. _size_changed();
  3343. }
  3344. } break;
  3345. }
  3346. }
  3347. void Control::_bind_methods() {
  3348. ClassDB::bind_method(D_METHOD("accept_event"), &Control::accept_event);
  3349. ClassDB::bind_method(D_METHOD("get_minimum_size"), &Control::get_minimum_size);
  3350. ClassDB::bind_method(D_METHOD("get_combined_minimum_size"), &Control::get_combined_minimum_size);
  3351. ClassDB::bind_method(D_METHOD("_set_layout_mode", "mode"), &Control::_set_layout_mode);
  3352. ClassDB::bind_method(D_METHOD("_get_layout_mode"), &Control::_get_layout_mode);
  3353. ClassDB::bind_method(D_METHOD("_set_anchors_layout_preset", "preset"), &Control::_set_anchors_layout_preset);
  3354. ClassDB::bind_method(D_METHOD("_get_anchors_layout_preset"), &Control::_get_anchors_layout_preset);
  3355. ClassDB::bind_method(D_METHOD("set_anchors_preset", "preset", "keep_offsets"), &Control::set_anchors_preset, DEFVAL(false));
  3356. ClassDB::bind_method(D_METHOD("set_offsets_preset", "preset", "resize_mode", "margin"), &Control::set_offsets_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
  3357. ClassDB::bind_method(D_METHOD("set_anchors_and_offsets_preset", "preset", "resize_mode", "margin"), &Control::set_anchors_and_offsets_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
  3358. ClassDB::bind_method(D_METHOD("_set_anchor", "side", "anchor"), &Control::_set_anchor);
  3359. ClassDB::bind_method(D_METHOD("set_anchor", "side", "anchor", "keep_offset", "push_opposite_anchor"), &Control::set_anchor, DEFVAL(false), DEFVAL(true));
  3360. ClassDB::bind_method(D_METHOD("get_anchor", "side"), &Control::get_anchor);
  3361. ClassDB::bind_method(D_METHOD("set_offset", "side", "offset"), &Control::set_offset);
  3362. ClassDB::bind_method(D_METHOD("get_offset", "offset"), &Control::get_offset);
  3363. ClassDB::bind_method(D_METHOD("set_anchor_and_offset", "side", "anchor", "offset", "push_opposite_anchor"), &Control::set_anchor_and_offset, DEFVAL(false));
  3364. ClassDB::bind_method(D_METHOD("set_begin", "position"), &Control::set_begin);
  3365. ClassDB::bind_method(D_METHOD("set_end", "position"), &Control::set_end);
  3366. ClassDB::bind_method(D_METHOD("set_position", "position", "keep_offsets"), &Control::set_position, DEFVAL(false));
  3367. ClassDB::bind_method(D_METHOD("_set_position", "position"), &Control::_set_position);
  3368. ClassDB::bind_method(D_METHOD("set_size", "size", "keep_offsets"), &Control::set_size, DEFVAL(false));
  3369. ClassDB::bind_method(D_METHOD("reset_size"), &Control::reset_size);
  3370. ClassDB::bind_method(D_METHOD("_set_size", "size"), &Control::_set_size);
  3371. ClassDB::bind_method(D_METHOD("set_custom_minimum_size", "size"), &Control::set_custom_minimum_size);
  3372. ClassDB::bind_method(D_METHOD("set_global_position", "position", "keep_offsets"), &Control::set_global_position, DEFVAL(false));
  3373. ClassDB::bind_method(D_METHOD("_set_global_position", "position"), &Control::_set_global_position);
  3374. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Control::set_rotation);
  3375. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Control::set_rotation_degrees);
  3376. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale);
  3377. ClassDB::bind_method(D_METHOD("set_pivot_offset", "pivot_offset"), &Control::set_pivot_offset);
  3378. ClassDB::bind_method(D_METHOD("set_pivot_offset_ratio", "ratio"), &Control::set_pivot_offset_ratio);
  3379. ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin);
  3380. ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end);
  3381. ClassDB::bind_method(D_METHOD("get_position"), &Control::get_position);
  3382. ClassDB::bind_method(D_METHOD("get_size"), &Control::get_size);
  3383. ClassDB::bind_method(D_METHOD("get_rotation"), &Control::get_rotation);
  3384. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Control::get_rotation_degrees);
  3385. ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale);
  3386. ClassDB::bind_method(D_METHOD("get_pivot_offset"), &Control::get_pivot_offset);
  3387. ClassDB::bind_method(D_METHOD("get_pivot_offset_ratio"), &Control::get_pivot_offset_ratio);
  3388. ClassDB::bind_method(D_METHOD("get_combined_pivot_offset"), &Control::get_combined_pivot_offset);
  3389. ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size);
  3390. ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_parent_area_size);
  3391. ClassDB::bind_method(D_METHOD("get_global_position"), &Control::get_global_position);
  3392. ClassDB::bind_method(D_METHOD("get_screen_position"), &Control::get_screen_position);
  3393. ClassDB::bind_method(D_METHOD("get_rect"), &Control::get_rect);
  3394. ClassDB::bind_method(D_METHOD("get_global_rect"), &Control::get_global_rect);
  3395. ClassDB::bind_method(D_METHOD("set_focus_mode", "mode"), &Control::set_focus_mode);
  3396. ClassDB::bind_method(D_METHOD("get_focus_mode"), &Control::get_focus_mode);
  3397. ClassDB::bind_method(D_METHOD("get_focus_mode_with_override"), &Control::get_focus_mode_with_override);
  3398. ClassDB::bind_method(D_METHOD("set_focus_behavior_recursive", "focus_behavior_recursive"), &Control::set_focus_behavior_recursive);
  3399. ClassDB::bind_method(D_METHOD("get_focus_behavior_recursive"), &Control::get_focus_behavior_recursive);
  3400. ClassDB::bind_method(D_METHOD("has_focus", "ignore_hidden_focus"), &Control::has_focus, DEFVAL(false));
  3401. ClassDB::bind_method(D_METHOD("grab_focus", "hide_focus"), &Control::grab_focus, DEFVAL(false));
  3402. ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus);
  3403. ClassDB::bind_method(D_METHOD("find_prev_valid_focus"), &Control::find_prev_valid_focus);
  3404. ClassDB::bind_method(D_METHOD("find_next_valid_focus"), &Control::find_next_valid_focus);
  3405. ClassDB::bind_method(D_METHOD("find_valid_focus_neighbor", "side"), &Control::find_valid_focus_neighbor);
  3406. ClassDB::bind_method(D_METHOD("set_h_size_flags", "flags"), &Control::set_h_size_flags);
  3407. ClassDB::bind_method(D_METHOD("get_h_size_flags"), &Control::get_h_size_flags);
  3408. ClassDB::bind_method(D_METHOD("set_stretch_ratio", "ratio"), &Control::set_stretch_ratio);
  3409. ClassDB::bind_method(D_METHOD("get_stretch_ratio"), &Control::get_stretch_ratio);
  3410. ClassDB::bind_method(D_METHOD("set_v_size_flags", "flags"), &Control::set_v_size_flags);
  3411. ClassDB::bind_method(D_METHOD("get_v_size_flags"), &Control::get_v_size_flags);
  3412. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Control::set_theme);
  3413. ClassDB::bind_method(D_METHOD("get_theme"), &Control::get_theme);
  3414. ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Control::set_theme_type_variation);
  3415. ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Control::get_theme_type_variation);
  3416. ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Control::begin_bulk_theme_override);
  3417. ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Control::end_bulk_theme_override);
  3418. ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Control::add_theme_icon_override);
  3419. ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Control::add_theme_style_override);
  3420. ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Control::add_theme_font_override);
  3421. ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Control::add_theme_font_size_override);
  3422. ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Control::add_theme_color_override);
  3423. ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Control::add_theme_constant_override);
  3424. ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Control::remove_theme_icon_override);
  3425. ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Control::remove_theme_style_override);
  3426. ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Control::remove_theme_font_override);
  3427. ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Control::remove_theme_font_size_override);
  3428. ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Control::remove_theme_color_override);
  3429. ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Control::remove_theme_constant_override);
  3430. ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Control::get_theme_icon, DEFVAL(StringName()));
  3431. ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Control::get_theme_stylebox, DEFVAL(StringName()));
  3432. ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Control::get_theme_font, DEFVAL(StringName()));
  3433. ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Control::get_theme_font_size, DEFVAL(StringName()));
  3434. ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Control::get_theme_color, DEFVAL(StringName()));
  3435. ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Control::get_theme_constant, DEFVAL(StringName()));
  3436. ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Control::has_theme_icon_override);
  3437. ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Control::has_theme_stylebox_override);
  3438. ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Control::has_theme_font_override);
  3439. ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Control::has_theme_font_size_override);
  3440. ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Control::has_theme_color_override);
  3441. ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Control::has_theme_constant_override);
  3442. ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Control::has_theme_icon, DEFVAL(StringName()));
  3443. ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Control::has_theme_stylebox, DEFVAL(StringName()));
  3444. ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Control::has_theme_font, DEFVAL(StringName()));
  3445. ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Control::has_theme_font_size, DEFVAL(StringName()));
  3446. ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Control::has_theme_color, DEFVAL(StringName()));
  3447. ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Control::has_theme_constant, DEFVAL(StringName()));
  3448. ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Control::get_theme_default_base_scale);
  3449. ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Control::get_theme_default_font);
  3450. ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Control::get_theme_default_font_size);
  3451. ClassDB::bind_method(D_METHOD("get_parent_control"), &Control::get_parent_control);
  3452. ClassDB::bind_method(D_METHOD("set_h_grow_direction", "direction"), &Control::set_h_grow_direction);
  3453. ClassDB::bind_method(D_METHOD("get_h_grow_direction"), &Control::get_h_grow_direction);
  3454. ClassDB::bind_method(D_METHOD("set_v_grow_direction", "direction"), &Control::set_v_grow_direction);
  3455. ClassDB::bind_method(D_METHOD("get_v_grow_direction"), &Control::get_v_grow_direction);
  3456. ClassDB::bind_method(D_METHOD("set_tooltip_auto_translate_mode", "mode"), &Control::set_tooltip_auto_translate_mode);
  3457. ClassDB::bind_method(D_METHOD("get_tooltip_auto_translate_mode"), &Control::get_tooltip_auto_translate_mode);
  3458. ClassDB::bind_method(D_METHOD("set_tooltip_text", "hint"), &Control::set_tooltip_text);
  3459. ClassDB::bind_method(D_METHOD("get_tooltip_text"), &Control::get_tooltip_text);
  3460. ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2()));
  3461. ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Control::set_default_cursor_shape);
  3462. ClassDB::bind_method(D_METHOD("get_default_cursor_shape"), &Control::get_default_cursor_shape);
  3463. ClassDB::bind_method(D_METHOD("get_cursor_shape", "position"), &Control::get_cursor_shape, DEFVAL(Point2()));
  3464. ClassDB::bind_method(D_METHOD("set_focus_neighbor", "side", "neighbor"), &Control::set_focus_neighbor);
  3465. ClassDB::bind_method(D_METHOD("get_focus_neighbor", "side"), &Control::get_focus_neighbor);
  3466. ClassDB::bind_method(D_METHOD("set_focus_next", "next"), &Control::set_focus_next);
  3467. ClassDB::bind_method(D_METHOD("get_focus_next"), &Control::get_focus_next);
  3468. ClassDB::bind_method(D_METHOD("set_focus_previous", "previous"), &Control::set_focus_previous);
  3469. ClassDB::bind_method(D_METHOD("get_focus_previous"), &Control::get_focus_previous);
  3470. ClassDB::bind_method(D_METHOD("force_drag", "data", "preview"), &Control::force_drag);
  3471. ClassDB::bind_method(D_METHOD("accessibility_drag"), &Control::accessibility_drag);
  3472. ClassDB::bind_method(D_METHOD("accessibility_drop"), &Control::accessibility_drop);
  3473. ClassDB::bind_method(D_METHOD("set_accessibility_name", "name"), &Control::set_accessibility_name);
  3474. ClassDB::bind_method(D_METHOD("get_accessibility_name"), &Control::get_accessibility_name);
  3475. ClassDB::bind_method(D_METHOD("set_accessibility_description", "description"), &Control::set_accessibility_description);
  3476. ClassDB::bind_method(D_METHOD("get_accessibility_description"), &Control::get_accessibility_description);
  3477. ClassDB::bind_method(D_METHOD("set_accessibility_live", "mode"), &Control::set_accessibility_live);
  3478. ClassDB::bind_method(D_METHOD("get_accessibility_live"), &Control::get_accessibility_live);
  3479. ClassDB::bind_method(D_METHOD("set_accessibility_controls_nodes", "node_path"), &Control::set_accessibility_controls_nodes);
  3480. ClassDB::bind_method(D_METHOD("get_accessibility_controls_nodes"), &Control::get_accessibility_controls_nodes);
  3481. ClassDB::bind_method(D_METHOD("set_accessibility_described_by_nodes", "node_path"), &Control::set_accessibility_described_by_nodes);
  3482. ClassDB::bind_method(D_METHOD("get_accessibility_described_by_nodes"), &Control::get_accessibility_described_by_nodes);
  3483. ClassDB::bind_method(D_METHOD("set_accessibility_labeled_by_nodes", "node_path"), &Control::set_accessibility_labeled_by_nodes);
  3484. ClassDB::bind_method(D_METHOD("get_accessibility_labeled_by_nodes"), &Control::get_accessibility_labeled_by_nodes);
  3485. ClassDB::bind_method(D_METHOD("set_accessibility_flow_to_nodes", "node_path"), &Control::set_accessibility_flow_to_nodes);
  3486. ClassDB::bind_method(D_METHOD("get_accessibility_flow_to_nodes"), &Control::get_accessibility_flow_to_nodes);
  3487. ClassDB::bind_method(D_METHOD("set_mouse_filter", "filter"), &Control::set_mouse_filter);
  3488. ClassDB::bind_method(D_METHOD("get_mouse_filter"), &Control::get_mouse_filter);
  3489. ClassDB::bind_method(D_METHOD("get_mouse_filter_with_override"), &Control::get_mouse_filter_with_override);
  3490. ClassDB::bind_method(D_METHOD("set_mouse_behavior_recursive", "mouse_behavior_recursive"), &Control::set_mouse_behavior_recursive);
  3491. ClassDB::bind_method(D_METHOD("get_mouse_behavior_recursive"), &Control::get_mouse_behavior_recursive);
  3492. ClassDB::bind_method(D_METHOD("set_force_pass_scroll_events", "force_pass_scroll_events"), &Control::set_force_pass_scroll_events);
  3493. ClassDB::bind_method(D_METHOD("is_force_pass_scroll_events"), &Control::is_force_pass_scroll_events);
  3494. ClassDB::bind_method(D_METHOD("set_clip_contents", "enable"), &Control::set_clip_contents);
  3495. ClassDB::bind_method(D_METHOD("is_clipping_contents"), &Control::is_clipping_contents);
  3496. ClassDB::bind_method(D_METHOD("grab_click_focus"), &Control::grab_click_focus);
  3497. ClassDB::bind_method(D_METHOD("set_drag_forwarding", "drag_func", "can_drop_func", "drop_func"), &Control::set_drag_forwarding);
  3498. ClassDB::bind_method(D_METHOD("set_drag_preview", "control"), &Control::set_drag_preview);
  3499. ClassDB::bind_method(D_METHOD("is_drag_successful"), &Control::is_drag_successful);
  3500. ClassDB::bind_method(D_METHOD("warp_mouse", "position"), &Control::warp_mouse);
  3501. ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &Control::set_shortcut_context);
  3502. ClassDB::bind_method(D_METHOD("get_shortcut_context"), &Control::get_shortcut_context);
  3503. ClassDB::bind_method(D_METHOD("update_minimum_size"), &Control::update_minimum_size);
  3504. ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Control::set_layout_direction);
  3505. ClassDB::bind_method(D_METHOD("get_layout_direction"), &Control::get_layout_direction);
  3506. ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Control::is_layout_rtl);
  3507. #ifndef DISABLE_DEPRECATED
  3508. ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Control::set_auto_translate);
  3509. ClassDB::bind_method(D_METHOD("is_auto_translating"), &Control::is_auto_translating);
  3510. #endif // DISABLE_DEPRECATED
  3511. ClassDB::bind_method(D_METHOD("set_localize_numeral_system", "enable"), &Control::set_localize_numeral_system);
  3512. ClassDB::bind_method(D_METHOD("is_localizing_numeral_system"), &Control::is_localizing_numeral_system);
  3513. ADD_GROUP("Layout", "");
  3514. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_contents"), "set_clip_contents", "is_clipping_contents");
  3515. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "custom_minimum_size", PROPERTY_HINT_NONE, "suffix:px"), "set_custom_minimum_size", "get_custom_minimum_size");
  3516. ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_direction", PROPERTY_HINT_ENUM, "Inherited,Based on Application Locale,Left-to-Right,Right-to-Left,Based on System Locale"), "set_layout_direction", "get_layout_direction");
  3517. ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_mode", PROPERTY_HINT_ENUM, "Position,Anchors,Container,Uncontrolled", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_layout_mode", "_get_layout_mode");
  3518. ADD_PROPERTY_DEFAULT("layout_mode", LayoutMode::LAYOUT_MODE_POSITION);
  3519. constexpr struct {
  3520. const char *name;
  3521. LayoutPreset value;
  3522. } anchors_presets[] = {
  3523. { TTRC("Full Rect"), PRESET_FULL_RECT },
  3524. { TTRC("Top Left"), PRESET_TOP_LEFT },
  3525. { TTRC("Top Right"), PRESET_TOP_RIGHT },
  3526. { TTRC("Bottom Right"), PRESET_BOTTOM_RIGHT },
  3527. { TTRC("Bottom Left"), PRESET_BOTTOM_LEFT },
  3528. { TTRC("Center Left"), PRESET_CENTER_LEFT },
  3529. { TTRC("Center Top"), PRESET_CENTER_TOP },
  3530. { TTRC("Center Right"), PRESET_CENTER_RIGHT },
  3531. { TTRC("Center Bottom"), PRESET_CENTER_BOTTOM },
  3532. { TTRC("Center"), PRESET_CENTER },
  3533. { TTRC("Left Wide"), PRESET_LEFT_WIDE },
  3534. { TTRC("Top Wide"), PRESET_TOP_WIDE },
  3535. { TTRC("Right Wide"), PRESET_RIGHT_WIDE },
  3536. { TTRC("Bottom Wide"), PRESET_BOTTOM_WIDE },
  3537. { TTRC("VCenter Wide"), PRESET_VCENTER_WIDE },
  3538. { TTRC("HCenter Wide"), PRESET_HCENTER_WIDE },
  3539. };
  3540. StringBuilder builder;
  3541. builder.append(TTRC("Custom"));
  3542. builder.append(":-1");
  3543. for (size_t i = 0; i < std_size(anchors_presets); i++) {
  3544. builder.append(",");
  3545. builder.append(anchors_presets[i].name);
  3546. builder.append(":");
  3547. builder.append(itos(anchors_presets[i].value));
  3548. }
  3549. const String anchors_presets_options = builder.as_string();
  3550. ADD_PROPERTY(PropertyInfo(Variant::INT, "anchors_preset", PROPERTY_HINT_ENUM, anchors_presets_options, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_anchors_layout_preset", "_get_anchors_layout_preset");
  3551. ADD_PROPERTY_DEFAULT("anchors_preset", -1);
  3552. ADD_SUBGROUP_INDENT("Anchor Points", "anchor_", 1);
  3553. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_left", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_LEFT);
  3554. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_top", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_TOP);
  3555. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_right", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_RIGHT);
  3556. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_bottom", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_BOTTOM);
  3557. ADD_SUBGROUP_INDENT("Anchor Offsets", "offset_", 1);
  3558. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_left", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_LEFT);
  3559. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_top", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_TOP);
  3560. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_right", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_RIGHT);
  3561. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_bottom", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_BOTTOM);
  3562. ADD_SUBGROUP_INDENT("Grow Direction", "grow_", 1);
  3563. ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_horizontal", PROPERTY_HINT_ENUM, "Left,Right,Both"), "set_h_grow_direction", "get_h_grow_direction");
  3564. ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_vertical", PROPERTY_HINT_ENUM, "Top,Bottom,Both"), "set_v_grow_direction", "get_v_grow_direction");
  3565. ADD_SUBGROUP("Transform", "");
  3566. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "_set_size", "get_size");
  3567. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "_set_position", "get_position");
  3568. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "_set_global_position", "get_global_position");
  3569. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
  3570. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
  3571. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
  3572. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_pivot_offset", "get_pivot_offset");
  3573. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset_ratio"), "set_pivot_offset_ratio", "get_pivot_offset_ratio");
  3574. ADD_SUBGROUP("Container Sizing", "size_flags_");
  3575. ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_horizontal", PROPERTY_HINT_FLAGS, "Fill:1,Expand:2,Shrink Center:4,Shrink End:8"), "set_h_size_flags", "get_h_size_flags");
  3576. ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_vertical", PROPERTY_HINT_FLAGS, "Fill:1,Expand:2,Shrink Center:4,Shrink End:8"), "set_v_size_flags", "get_v_size_flags");
  3577. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "0,20,0.01,or_greater"), "set_stretch_ratio", "get_stretch_ratio");
  3578. ADD_GROUP("Localization", "");
  3579. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "localize_numeral_system"), "set_localize_numeral_system", "is_localizing_numeral_system");
  3580. #ifndef DISABLE_DEPRECATED
  3581. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_auto_translate", "is_auto_translating");
  3582. #endif // DISABLE_DEPRECATED
  3583. ADD_GROUP("Tooltip", "tooltip_");
  3584. ADD_PROPERTY(PropertyInfo(Variant::STRING, "tooltip_text", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip_text", "get_tooltip_text");
  3585. ADD_PROPERTY(PropertyInfo(Variant::INT, "tooltip_auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_tooltip_auto_translate_mode", "get_tooltip_auto_translate_mode");
  3586. ADD_GROUP("Focus", "focus_");
  3587. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_left", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_LEFT);
  3588. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_top", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_TOP);
  3589. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_right", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_RIGHT);
  3590. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_bottom", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_BOTTOM);
  3591. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_next", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_next", "get_focus_next");
  3592. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_previous", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_previous", "get_focus_previous");
  3593. ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All,Accessibility"), "set_focus_mode", "get_focus_mode");
  3594. ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_behavior_recursive", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_focus_behavior_recursive", "get_focus_behavior_recursive");
  3595. ADD_GROUP("Mouse", "mouse_");
  3596. ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_filter", PROPERTY_HINT_ENUM, "Stop,Pass (Propagate Up),Ignore"), "set_mouse_filter", "get_mouse_filter");
  3597. ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_behavior_recursive", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_mouse_behavior_recursive", "get_mouse_behavior_recursive");
  3598. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mouse_force_pass_scroll_events"), "set_force_pass_scroll_events", "is_force_pass_scroll_events");
  3599. ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_default_cursor_shape", PROPERTY_HINT_ENUM, "Arrow,I-Beam,Pointing Hand,Cross,Wait,Busy,Drag,Can Drop,Forbidden,Vertical Resize,Horizontal Resize,Secondary Diagonal Resize,Main Diagonal Resize,Move,Vertical Split,Horizontal Split,Help"), "set_default_cursor_shape", "get_default_cursor_shape");
  3600. ADD_GROUP("Input", "");
  3601. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_NODE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
  3602. ADD_GROUP("Accessibility", "accessibility_");
  3603. ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_name"), "set_accessibility_name", "get_accessibility_name");
  3604. ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_description"), "set_accessibility_description", "get_accessibility_description");
  3605. ADD_PROPERTY(PropertyInfo(Variant::INT, "accessibility_live", PROPERTY_HINT_ENUM, "Off,Polite,Assertive"), "set_accessibility_live", "get_accessibility_live");
  3606. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_controls_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_controls_nodes", "get_accessibility_controls_nodes");
  3607. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_described_by_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_described_by_nodes", "get_accessibility_described_by_nodes");
  3608. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_labeled_by_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_labeled_by_nodes", "get_accessibility_labeled_by_nodes");
  3609. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessibility_flow_to_nodes", PROPERTY_HINT_ARRAY_TYPE, "NodePath"), "set_accessibility_flow_to_nodes", "get_accessibility_flow_to_nodes");
  3610. ADD_GROUP("Theme", "theme_");
  3611. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  3612. ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
  3613. BIND_ENUM_CONSTANT(FOCUS_NONE);
  3614. BIND_ENUM_CONSTANT(FOCUS_CLICK);
  3615. BIND_ENUM_CONSTANT(FOCUS_ALL);
  3616. BIND_ENUM_CONSTANT(FOCUS_ACCESSIBILITY);
  3617. BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_INHERITED);
  3618. BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_DISABLED);
  3619. BIND_ENUM_CONSTANT(FOCUS_BEHAVIOR_ENABLED);
  3620. BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_INHERITED);
  3621. BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_DISABLED);
  3622. BIND_ENUM_CONSTANT(MOUSE_BEHAVIOR_ENABLED);
  3623. BIND_CONSTANT(NOTIFICATION_RESIZED);
  3624. BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER);
  3625. BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT);
  3626. BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER_SELF);
  3627. BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT_SELF);
  3628. BIND_CONSTANT(NOTIFICATION_FOCUS_ENTER);
  3629. BIND_CONSTANT(NOTIFICATION_FOCUS_EXIT);
  3630. BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
  3631. BIND_CONSTANT(NOTIFICATION_SCROLL_BEGIN);
  3632. BIND_CONSTANT(NOTIFICATION_SCROLL_END);
  3633. BIND_CONSTANT(NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  3634. BIND_ENUM_CONSTANT(CURSOR_ARROW);
  3635. BIND_ENUM_CONSTANT(CURSOR_IBEAM);
  3636. BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
  3637. BIND_ENUM_CONSTANT(CURSOR_CROSS);
  3638. BIND_ENUM_CONSTANT(CURSOR_WAIT);
  3639. BIND_ENUM_CONSTANT(CURSOR_BUSY);
  3640. BIND_ENUM_CONSTANT(CURSOR_DRAG);
  3641. BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
  3642. BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
  3643. BIND_ENUM_CONSTANT(CURSOR_VSIZE);
  3644. BIND_ENUM_CONSTANT(CURSOR_HSIZE);
  3645. BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
  3646. BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
  3647. BIND_ENUM_CONSTANT(CURSOR_MOVE);
  3648. BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
  3649. BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
  3650. BIND_ENUM_CONSTANT(CURSOR_HELP);
  3651. BIND_ENUM_CONSTANT(PRESET_TOP_LEFT);
  3652. BIND_ENUM_CONSTANT(PRESET_TOP_RIGHT);
  3653. BIND_ENUM_CONSTANT(PRESET_BOTTOM_LEFT);
  3654. BIND_ENUM_CONSTANT(PRESET_BOTTOM_RIGHT);
  3655. BIND_ENUM_CONSTANT(PRESET_CENTER_LEFT);
  3656. BIND_ENUM_CONSTANT(PRESET_CENTER_TOP);
  3657. BIND_ENUM_CONSTANT(PRESET_CENTER_RIGHT);
  3658. BIND_ENUM_CONSTANT(PRESET_CENTER_BOTTOM);
  3659. BIND_ENUM_CONSTANT(PRESET_CENTER);
  3660. BIND_ENUM_CONSTANT(PRESET_LEFT_WIDE);
  3661. BIND_ENUM_CONSTANT(PRESET_TOP_WIDE);
  3662. BIND_ENUM_CONSTANT(PRESET_RIGHT_WIDE);
  3663. BIND_ENUM_CONSTANT(PRESET_BOTTOM_WIDE);
  3664. BIND_ENUM_CONSTANT(PRESET_VCENTER_WIDE);
  3665. BIND_ENUM_CONSTANT(PRESET_HCENTER_WIDE);
  3666. BIND_ENUM_CONSTANT(PRESET_FULL_RECT);
  3667. BIND_ENUM_CONSTANT(PRESET_MODE_MINSIZE);
  3668. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_WIDTH);
  3669. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_HEIGHT);
  3670. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_SIZE);
  3671. BIND_BITFIELD_FLAG(SIZE_SHRINK_BEGIN);
  3672. BIND_BITFIELD_FLAG(SIZE_FILL);
  3673. BIND_BITFIELD_FLAG(SIZE_EXPAND);
  3674. BIND_BITFIELD_FLAG(SIZE_EXPAND_FILL);
  3675. BIND_BITFIELD_FLAG(SIZE_SHRINK_CENTER);
  3676. BIND_BITFIELD_FLAG(SIZE_SHRINK_END);
  3677. BIND_ENUM_CONSTANT(MOUSE_FILTER_STOP);
  3678. BIND_ENUM_CONSTANT(MOUSE_FILTER_PASS);
  3679. BIND_ENUM_CONSTANT(MOUSE_FILTER_IGNORE);
  3680. BIND_ENUM_CONSTANT(GROW_DIRECTION_BEGIN);
  3681. BIND_ENUM_CONSTANT(GROW_DIRECTION_END);
  3682. BIND_ENUM_CONSTANT(GROW_DIRECTION_BOTH);
  3683. BIND_ENUM_CONSTANT(ANCHOR_BEGIN);
  3684. BIND_ENUM_CONSTANT(ANCHOR_END);
  3685. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
  3686. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_APPLICATION_LOCALE);
  3687. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
  3688. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
  3689. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_SYSTEM_LOCALE);
  3690. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_MAX);
  3691. #ifndef DISABLE_DEPRECATED
  3692. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
  3693. #endif // DISABLE_DEPRECATED
  3694. BIND_ENUM_CONSTANT(TEXT_DIRECTION_INHERITED);
  3695. BIND_ENUM_CONSTANT(TEXT_DIRECTION_AUTO);
  3696. BIND_ENUM_CONSTANT(TEXT_DIRECTION_LTR);
  3697. BIND_ENUM_CONSTANT(TEXT_DIRECTION_RTL);
  3698. ADD_SIGNAL(MethodInfo("resized"));
  3699. ADD_SIGNAL(MethodInfo("gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  3700. ADD_SIGNAL(MethodInfo("mouse_entered"));
  3701. ADD_SIGNAL(MethodInfo("mouse_exited"));
  3702. ADD_SIGNAL(MethodInfo("focus_entered"));
  3703. ADD_SIGNAL(MethodInfo("focus_exited"));
  3704. ADD_SIGNAL(MethodInfo("size_flags_changed"));
  3705. ADD_SIGNAL(MethodInfo("minimum_size_changed"));
  3706. ADD_SIGNAL(MethodInfo("theme_changed"));
  3707. GDVIRTUAL_BIND(_has_point, "point");
  3708. GDVIRTUAL_BIND(_structured_text_parser, "args", "text");
  3709. GDVIRTUAL_BIND(_get_minimum_size);
  3710. GDVIRTUAL_BIND(_get_tooltip, "at_position");
  3711. GDVIRTUAL_BIND(_get_drag_data, "at_position");
  3712. GDVIRTUAL_BIND(_can_drop_data, "at_position", "data");
  3713. GDVIRTUAL_BIND(_drop_data, "at_position", "data");
  3714. GDVIRTUAL_BIND(_make_custom_tooltip, "for_text");
  3715. GDVIRTUAL_BIND(_accessibility_get_contextual_info);
  3716. GDVIRTUAL_BIND(_get_accessibility_container_name, "node");
  3717. GDVIRTUAL_BIND(_gui_input, "event");
  3718. }
  3719. Control::Control() {
  3720. _define_ancestry(AncestralClass::CONTROL);
  3721. data.theme_owner = memnew(ThemeOwner(this));
  3722. set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
  3723. }
  3724. Control::~Control() {
  3725. memdelete(data.theme_owner);
  3726. // Resources need to be disconnected.
  3727. for (KeyValue<StringName, Ref<Texture2D>> &E : data.theme_icon_override) {
  3728. E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  3729. }
  3730. for (KeyValue<StringName, Ref<StyleBox>> &E : data.theme_style_override) {
  3731. E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  3732. }
  3733. for (KeyValue<StringName, Ref<Font>> &E : data.theme_font_override) {
  3734. E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  3735. }
  3736. // Then override maps can be simply cleared.
  3737. data.theme_icon_override.clear();
  3738. data.theme_style_override.clear();
  3739. data.theme_font_override.clear();
  3740. data.theme_font_size_override.clear();
  3741. data.theme_color_override.clear();
  3742. data.theme_constant_override.clear();
  3743. }