control.cpp 156 KB

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