2
0

control.cpp 157 KB

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