control.cpp 148 KB

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