control.cpp 130 KB

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