control.cpp 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993
  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/os/os.h"
  34. #include "core/string/translation_server.h"
  35. #include "scene/gui/scroll_container.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() const {
  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. _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. Control::MouseFilter Control::get_mouse_filter_with_recursive() const {
  1579. ERR_READ_THREAD_GUARD_V(MOUSE_FILTER_IGNORE);
  1580. if (_is_parent_mouse_disabled()) {
  1581. return MOUSE_FILTER_IGNORE;
  1582. }
  1583. return data.mouse_filter;
  1584. }
  1585. void Control::set_mouse_recursive_behavior(RecursiveBehavior p_recursive_mouse_behavior) {
  1586. ERR_MAIN_THREAD_GUARD;
  1587. ERR_FAIL_INDEX((int)p_recursive_mouse_behavior, 4);
  1588. if (data.mouse_recursive_behavior == p_recursive_mouse_behavior) {
  1589. return;
  1590. }
  1591. data.mouse_recursive_behavior = p_recursive_mouse_behavior;
  1592. if (p_recursive_mouse_behavior == RECURSIVE_BEHAVIOR_INHERITED) {
  1593. Control *parent = get_parent_control();
  1594. if (parent) {
  1595. _apply_mouse_behavior_recursively(parent->data.parent_mouse_recursive_behavior, false);
  1596. } else {
  1597. _apply_mouse_behavior_recursively(RECURSIVE_BEHAVIOR_ENABLED, false);
  1598. }
  1599. } else {
  1600. _apply_mouse_behavior_recursively(p_recursive_mouse_behavior, false);
  1601. }
  1602. if (get_viewport()) {
  1603. get_viewport()->_gui_update_mouse_over();
  1604. }
  1605. }
  1606. Control::RecursiveBehavior Control::get_mouse_recursive_behavior() const {
  1607. ERR_READ_THREAD_GUARD_V(RECURSIVE_BEHAVIOR_INHERITED);
  1608. return data.mouse_recursive_behavior;
  1609. }
  1610. void Control::set_force_pass_scroll_events(bool p_force_pass_scroll_events) {
  1611. ERR_MAIN_THREAD_GUARD;
  1612. data.force_pass_scroll_events = p_force_pass_scroll_events;
  1613. }
  1614. bool Control::is_force_pass_scroll_events() const {
  1615. ERR_READ_THREAD_GUARD_V(false);
  1616. return data.force_pass_scroll_events;
  1617. }
  1618. void Control::warp_mouse(const Point2 &p_position) {
  1619. ERR_MAIN_THREAD_GUARD;
  1620. ERR_FAIL_COND(!is_inside_tree());
  1621. get_viewport()->warp_mouse(get_global_transform_with_canvas().xform(p_position));
  1622. }
  1623. void Control::set_shortcut_context(const Node *p_node) {
  1624. ERR_MAIN_THREAD_GUARD;
  1625. if (p_node != nullptr) {
  1626. data.shortcut_context = p_node->get_instance_id();
  1627. } else {
  1628. data.shortcut_context = ObjectID();
  1629. }
  1630. }
  1631. Node *Control::get_shortcut_context() const {
  1632. ERR_READ_THREAD_GUARD_V(nullptr);
  1633. Object *ctx_obj = ObjectDB::get_instance(data.shortcut_context);
  1634. Node *ctx_node = Object::cast_to<Node>(ctx_obj);
  1635. return ctx_node;
  1636. }
  1637. bool Control::is_focus_owner_in_shortcut_context() const {
  1638. ERR_READ_THREAD_GUARD_V(false);
  1639. if (data.shortcut_context == ObjectID()) {
  1640. // No context, therefore global - always "in" context.
  1641. return true;
  1642. }
  1643. const Node *ctx_node = get_shortcut_context();
  1644. const Control *vp_focus = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  1645. // If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
  1646. return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
  1647. }
  1648. // Drag and drop handling.
  1649. void Control::set_drag_forwarding(const Callable &p_drag, const Callable &p_can_drop, const Callable &p_drop) {
  1650. ERR_MAIN_THREAD_GUARD;
  1651. data.forward_drag = p_drag;
  1652. data.forward_can_drop = p_can_drop;
  1653. data.forward_drop = p_drop;
  1654. }
  1655. Variant Control::get_drag_data(const Point2 &p_point) {
  1656. ERR_READ_THREAD_GUARD_V(Variant());
  1657. Variant ret;
  1658. if (data.forward_drag.is_valid()) {
  1659. Variant p = p_point;
  1660. const Variant *vp[1] = { &p };
  1661. Callable::CallError ce;
  1662. data.forward_drag.callp((const Variant **)vp, 1, ret, ce);
  1663. if (ce.error != Callable::CallError::CALL_OK) {
  1664. 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) + ".");
  1665. }
  1666. return ret;
  1667. }
  1668. GDVIRTUAL_CALL(_get_drag_data, p_point, ret);
  1669. return ret;
  1670. }
  1671. bool Control::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  1672. ERR_READ_THREAD_GUARD_V(false);
  1673. if (data.forward_can_drop.is_valid()) {
  1674. Variant ret;
  1675. Variant p = p_point;
  1676. const Variant *vp[2] = { &p, &p_data };
  1677. Callable::CallError ce;
  1678. data.forward_can_drop.callp((const Variant **)vp, 2, ret, ce);
  1679. if (ce.error != Callable::CallError::CALL_OK) {
  1680. 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) + ".");
  1681. }
  1682. return ret;
  1683. }
  1684. bool ret = false;
  1685. GDVIRTUAL_CALL(_can_drop_data, p_point, p_data, ret);
  1686. return ret;
  1687. }
  1688. void Control::drop_data(const Point2 &p_point, const Variant &p_data) {
  1689. ERR_READ_THREAD_GUARD;
  1690. if (data.forward_drop.is_valid()) {
  1691. Variant ret;
  1692. Variant p = p_point;
  1693. const Variant *vp[2] = { &p, &p_data };
  1694. Callable::CallError ce;
  1695. data.forward_drop.callp((const Variant **)vp, 2, ret, ce);
  1696. if (ce.error != Callable::CallError::CALL_OK) {
  1697. ERR_FAIL_MSG("Error calling forwarded method from 'drop_data': " + Variant::get_callable_error_text(data.forward_drop, (const Variant **)vp, 2, ce) + ".");
  1698. }
  1699. return;
  1700. }
  1701. GDVIRTUAL_CALL(_drop_data, p_point, p_data);
  1702. }
  1703. void Control::force_drag(const Variant &p_data, Control *p_control) {
  1704. ERR_MAIN_THREAD_GUARD;
  1705. ERR_FAIL_COND(!is_inside_tree());
  1706. ERR_FAIL_COND(p_data.get_type() == Variant::NIL);
  1707. get_viewport()->_gui_force_drag(this, p_data, p_control);
  1708. }
  1709. void Control::set_drag_preview(Control *p_control) {
  1710. ERR_MAIN_THREAD_GUARD;
  1711. ERR_FAIL_COND(!is_inside_tree());
  1712. ERR_FAIL_COND(!get_viewport()->gui_is_dragging());
  1713. get_viewport()->_gui_set_drag_preview(this, p_control);
  1714. }
  1715. bool Control::is_drag_successful() const {
  1716. ERR_READ_THREAD_GUARD_V(false);
  1717. return is_inside_tree() && get_viewport()->gui_is_drag_successful();
  1718. }
  1719. // Focus.
  1720. void Control::set_focus_mode(FocusMode p_focus_mode) {
  1721. ERR_MAIN_THREAD_GUARD;
  1722. ERR_FAIL_INDEX((int)p_focus_mode, 3);
  1723. if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode != FOCUS_NONE && has_focus()) {
  1724. release_focus();
  1725. }
  1726. data.focus_mode = p_focus_mode;
  1727. }
  1728. Control::FocusMode Control::get_focus_mode() const {
  1729. ERR_READ_THREAD_GUARD_V(FOCUS_NONE);
  1730. return data.focus_mode;
  1731. }
  1732. Control::FocusMode Control::get_focus_mode_with_recursive() const {
  1733. ERR_READ_THREAD_GUARD_V(FOCUS_NONE);
  1734. if (_is_focus_disabled_recursively()) {
  1735. return FOCUS_NONE;
  1736. }
  1737. return data.focus_mode;
  1738. }
  1739. void Control::set_focus_recursive_behavior(RecursiveBehavior p_recursive_focus_behavior) {
  1740. ERR_MAIN_THREAD_GUARD;
  1741. ERR_FAIL_INDEX((int)p_recursive_focus_behavior, 4);
  1742. if (data.focus_recursive_behavior == p_recursive_focus_behavior) {
  1743. return;
  1744. }
  1745. data.focus_recursive_behavior = p_recursive_focus_behavior;
  1746. if (p_recursive_focus_behavior == RECURSIVE_BEHAVIOR_INHERITED) {
  1747. Control *parent = get_parent_control();
  1748. if (parent) {
  1749. _apply_focus_behavior_recursively(parent->data.parent_focus_recursive_behavior, false);
  1750. } else {
  1751. _apply_focus_behavior_recursively(RECURSIVE_BEHAVIOR_ENABLED, false);
  1752. }
  1753. } else {
  1754. _apply_focus_behavior_recursively(p_recursive_focus_behavior, false);
  1755. }
  1756. }
  1757. Control::RecursiveBehavior Control::get_focus_recursive_behavior() const {
  1758. ERR_READ_THREAD_GUARD_V(RECURSIVE_BEHAVIOR_INHERITED);
  1759. return data.focus_recursive_behavior;
  1760. }
  1761. bool Control::has_focus() const {
  1762. ERR_READ_THREAD_GUARD_V(false);
  1763. return is_inside_tree() && get_viewport()->_gui_control_has_focus(this);
  1764. }
  1765. void Control::grab_focus() {
  1766. ERR_MAIN_THREAD_GUARD;
  1767. ERR_FAIL_COND(!is_inside_tree());
  1768. if (data.focus_mode == FOCUS_NONE) {
  1769. WARN_PRINT("This control can't grab focus. Use set_focus_mode() to allow a control to get focus.");
  1770. return;
  1771. }
  1772. get_viewport()->_gui_control_grab_focus(this);
  1773. }
  1774. void Control::grab_click_focus() {
  1775. ERR_MAIN_THREAD_GUARD;
  1776. ERR_FAIL_COND(!is_inside_tree());
  1777. get_viewport()->_gui_grab_click_focus(this);
  1778. }
  1779. void Control::release_focus() {
  1780. ERR_MAIN_THREAD_GUARD;
  1781. ERR_FAIL_COND(!is_inside_tree());
  1782. if (!has_focus()) {
  1783. return;
  1784. }
  1785. get_viewport()->gui_release_focus();
  1786. }
  1787. static Control *_next_control(Control *p_from) {
  1788. if (p_from->is_set_as_top_level()) {
  1789. return nullptr; // Can't go above.
  1790. }
  1791. Control *parent = p_from->get_parent_control();
  1792. if (!parent) {
  1793. return nullptr;
  1794. }
  1795. int next = p_from->get_index();
  1796. ERR_FAIL_INDEX_V(next, parent->get_child_count(), nullptr);
  1797. for (int i = (next + 1); i < parent->get_child_count(); i++) {
  1798. Control *c = Object::cast_to<Control>(parent->get_child(i));
  1799. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  1800. continue;
  1801. }
  1802. return c;
  1803. }
  1804. // No next in parent, try the same in parent.
  1805. return _next_control(parent);
  1806. }
  1807. Control *Control::find_next_valid_focus() const {
  1808. ERR_READ_THREAD_GUARD_V(nullptr);
  1809. // If the focus property is manually overwritten, attempt to use it.
  1810. if (!data.focus_next.is_empty()) {
  1811. Node *n = get_node_or_null(data.focus_next);
  1812. ERR_FAIL_NULL_V_MSG(n, nullptr, "Next focus node path is invalid: '" + data.focus_next + "'.");
  1813. Control *c = Object::cast_to<Control>(n);
  1814. ERR_FAIL_NULL_V_MSG(c, nullptr, "Next focus node is not a control: '" + n->get_name() + "'.");
  1815. if (c->is_visible_in_tree() && c->get_focus_mode_with_recursive() != FOCUS_NONE) {
  1816. return c;
  1817. }
  1818. }
  1819. Control *from = const_cast<Control *>(this);
  1820. while (true) {
  1821. // Find next child.
  1822. Control *next_child = nullptr;
  1823. for (int i = 0; i < from->get_child_count(); i++) {
  1824. Control *c = Object::cast_to<Control>(from->get_child(i));
  1825. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  1826. continue;
  1827. }
  1828. next_child = c;
  1829. break;
  1830. }
  1831. if (!next_child) {
  1832. next_child = _next_control(from);
  1833. if (!next_child) { // Nothing else. Go up and find either window or subwindow.
  1834. next_child = const_cast<Control *>(this);
  1835. while (next_child) {
  1836. if (next_child->is_set_as_top_level()) {
  1837. break;
  1838. }
  1839. if (next_child->data.RI) {
  1840. break;
  1841. }
  1842. next_child = next_child->data.parent_control;
  1843. }
  1844. }
  1845. }
  1846. if (!next_child) {
  1847. break;
  1848. }
  1849. if (next_child->get_focus_mode_with_recursive() == FOCUS_ALL) {
  1850. return next_child;
  1851. }
  1852. if (next_child == from || next_child == this) {
  1853. return nullptr; // Stuck in a loop with no next control.
  1854. }
  1855. from = next_child; // Try to find the next control with focus mode FOCUS_ALL.
  1856. }
  1857. return nullptr;
  1858. }
  1859. static Control *_prev_control(Control *p_from) {
  1860. for (int i = p_from->get_child_count() - 1; i >= 0; i--) {
  1861. Control *c = Object::cast_to<Control>(p_from->get_child(i));
  1862. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  1863. continue;
  1864. }
  1865. // Find the last child as prev, try the same in the last child.
  1866. return _prev_control(c);
  1867. }
  1868. return p_from; // Not found in the children, return itself.
  1869. }
  1870. Control *Control::find_prev_valid_focus() const {
  1871. ERR_READ_THREAD_GUARD_V(nullptr);
  1872. // If the focus property is manually overwritten, attempt to use it.
  1873. if (!data.focus_prev.is_empty()) {
  1874. Node *n = get_node_or_null(data.focus_prev);
  1875. ERR_FAIL_NULL_V_MSG(n, nullptr, "Previous focus node path is invalid: '" + data.focus_prev + "'.");
  1876. Control *c = Object::cast_to<Control>(n);
  1877. ERR_FAIL_NULL_V_MSG(c, nullptr, "Previous focus node is not a control: '" + n->get_name() + "'.");
  1878. if (c->is_visible_in_tree() && c->get_focus_mode_with_recursive() != FOCUS_NONE) {
  1879. return c;
  1880. }
  1881. }
  1882. Control *from = const_cast<Control *>(this);
  1883. while (true) {
  1884. // Find prev child.
  1885. Control *prev_child = nullptr;
  1886. if (from->is_set_as_top_level() || !from->data.parent_control) {
  1887. // Find last of the children.
  1888. prev_child = _prev_control(from); // Wrap start here.
  1889. } else {
  1890. for (int i = (from->get_index() - 1); i >= 0; i--) {
  1891. Control *c = Object::cast_to<Control>(from->get_parent()->get_child(i));
  1892. if (!c || !c->is_visible_in_tree() || c->is_set_as_top_level()) {
  1893. continue;
  1894. }
  1895. prev_child = c;
  1896. break;
  1897. }
  1898. if (!prev_child) {
  1899. prev_child = from->data.parent_control;
  1900. } else {
  1901. prev_child = _prev_control(prev_child);
  1902. }
  1903. }
  1904. if (prev_child->get_focus_mode_with_recursive() == FOCUS_ALL) {
  1905. return prev_child;
  1906. }
  1907. if (prev_child == from || prev_child == this) {
  1908. return nullptr; // Stuck in a loop with no prev control.
  1909. }
  1910. from = prev_child; // Try to find the prev control with focus mode FOCUS_ALL.
  1911. }
  1912. return nullptr;
  1913. }
  1914. void Control::set_focus_neighbor(Side p_side, const NodePath &p_neighbor) {
  1915. ERR_MAIN_THREAD_GUARD;
  1916. ERR_FAIL_INDEX((int)p_side, 4);
  1917. data.focus_neighbor[p_side] = p_neighbor;
  1918. }
  1919. NodePath Control::get_focus_neighbor(Side p_side) const {
  1920. ERR_READ_THREAD_GUARD_V(NodePath());
  1921. ERR_FAIL_INDEX_V((int)p_side, 4, NodePath());
  1922. return data.focus_neighbor[p_side];
  1923. }
  1924. void Control::set_focus_next(const NodePath &p_next) {
  1925. ERR_MAIN_THREAD_GUARD;
  1926. data.focus_next = p_next;
  1927. }
  1928. NodePath Control::get_focus_next() const {
  1929. ERR_READ_THREAD_GUARD_V(NodePath());
  1930. return data.focus_next;
  1931. }
  1932. void Control::set_focus_previous(const NodePath &p_prev) {
  1933. ERR_MAIN_THREAD_GUARD;
  1934. data.focus_prev = p_prev;
  1935. }
  1936. NodePath Control::get_focus_previous() const {
  1937. ERR_READ_THREAD_GUARD_V(NodePath());
  1938. return data.focus_prev;
  1939. }
  1940. #define MAX_NEIGHBOR_SEARCH_COUNT 512
  1941. Control *Control::_get_focus_neighbor(Side p_side, int p_count) {
  1942. ERR_FAIL_INDEX_V((int)p_side, 4, nullptr);
  1943. if (p_count >= MAX_NEIGHBOR_SEARCH_COUNT) {
  1944. return nullptr;
  1945. }
  1946. if (!data.focus_neighbor[p_side].is_empty()) {
  1947. Node *n = get_node_or_null(data.focus_neighbor[p_side]);
  1948. ERR_FAIL_NULL_V_MSG(n, nullptr, "Neighbor focus node path is invalid: '" + data.focus_neighbor[p_side] + "'.");
  1949. Control *c = Object::cast_to<Control>(n);
  1950. ERR_FAIL_NULL_V_MSG(c, nullptr, "Neighbor focus node is not a control: '" + n->get_name() + "'.");
  1951. if (c->is_visible_in_tree() && c->get_focus_mode_with_recursive() != FOCUS_NONE) {
  1952. return c;
  1953. }
  1954. c = c->_get_focus_neighbor(p_side, p_count + 1);
  1955. return c;
  1956. }
  1957. real_t square_of_dist = 1e14;
  1958. Control *result = nullptr;
  1959. const Vector2 dir[4] = {
  1960. Vector2(-1, 0),
  1961. Vector2(0, -1),
  1962. Vector2(1, 0),
  1963. Vector2(0, 1)
  1964. };
  1965. Vector2 vdir = dir[p_side];
  1966. Rect2 r = get_global_rect();
  1967. real_t begin_d = vdir.dot(r.get_position());
  1968. real_t end_d = vdir.dot(r.get_end());
  1969. real_t maxd = MAX(begin_d, end_d);
  1970. Rect2 clamp = Rect2(-1e7, -1e7, 2e7, 2e7);
  1971. Rect2 result_rect;
  1972. Node *base = this;
  1973. while (base) {
  1974. ScrollContainer *sc = Object::cast_to<ScrollContainer>(base);
  1975. if (sc) {
  1976. Rect2 sc_r = sc->get_global_rect();
  1977. bool follow_focus = sc->is_following_focus();
  1978. if (result && !follow_focus && !sc_r.intersects(result_rect)) {
  1979. result = nullptr; // Skip invisible control.
  1980. }
  1981. if (result == nullptr) {
  1982. real_t sc_begin_d = vdir.dot(sc_r.get_position());
  1983. real_t sc_end_d = vdir.dot(sc_r.get_end());
  1984. real_t sc_maxd = sc_begin_d;
  1985. real_t sc_mind = sc_end_d;
  1986. if (sc_begin_d < sc_end_d) {
  1987. sc_maxd = sc_end_d;
  1988. sc_mind = sc_begin_d;
  1989. }
  1990. if (!follow_focus && maxd < sc_mind) {
  1991. // Reposition to find visible control.
  1992. maxd = sc_mind;
  1993. r.set_position(r.get_position() + (sc_mind - maxd) * vdir);
  1994. }
  1995. if (follow_focus || sc_maxd > maxd) {
  1996. _window_find_focus_neighbor(vdir, base, r, clamp, maxd, square_of_dist, &result);
  1997. }
  1998. if (result == nullptr) {
  1999. // Reposition to search upwards.
  2000. maxd = sc_maxd;
  2001. r.set_position(r.get_position() + (sc_maxd - maxd) * vdir);
  2002. } else {
  2003. result_rect = result->get_global_rect();
  2004. if (follow_focus) {
  2005. real_t r_begin_d = vdir.dot(result_rect.get_position());
  2006. real_t r_end_d = vdir.dot(result_rect.get_end());
  2007. real_t r_maxd = r_begin_d;
  2008. real_t r_mind = r_end_d;
  2009. if (r_begin_d < r_end_d) {
  2010. r_maxd = r_end_d;
  2011. r_mind = r_begin_d;
  2012. }
  2013. if (r_maxd > sc_maxd) {
  2014. result_rect.set_position(result_rect.get_position() + (sc_maxd - r_maxd) * vdir);
  2015. } else if (r_mind < sc_mind) {
  2016. result_rect.set_position(result_rect.get_position() + (sc_mind - r_mind) * vdir);
  2017. }
  2018. }
  2019. }
  2020. }
  2021. }
  2022. Control *c = Object::cast_to<Control>(base);
  2023. if (c) {
  2024. if (c->data.RI) {
  2025. break;
  2026. }
  2027. }
  2028. base = base->get_parent();
  2029. }
  2030. if (result) {
  2031. return result;
  2032. }
  2033. if (!base) {
  2034. return nullptr;
  2035. }
  2036. _window_find_focus_neighbor(vdir, base, r, clamp, maxd, square_of_dist, &result);
  2037. return result;
  2038. }
  2039. bool Control::_is_focus_disabled_recursively() const {
  2040. switch (data.focus_recursive_behavior) {
  2041. case RECURSIVE_BEHAVIOR_INHERITED:
  2042. return data.parent_focus_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED;
  2043. case RECURSIVE_BEHAVIOR_DISABLED:
  2044. return true;
  2045. case RECURSIVE_BEHAVIOR_ENABLED:
  2046. return false;
  2047. }
  2048. return false;
  2049. }
  2050. void Control::_apply_focus_behavior_recursively(RecursiveBehavior p_focus_recursive_behavior, bool p_skip_non_inherited) {
  2051. if (is_inside_tree() && (data.focus_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED || (data.focus_recursive_behavior == RECURSIVE_BEHAVIOR_INHERITED && p_focus_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED)) && has_focus()) {
  2052. release_focus();
  2053. }
  2054. if (p_skip_non_inherited && data.focus_recursive_behavior != RECURSIVE_BEHAVIOR_INHERITED) {
  2055. return;
  2056. }
  2057. data.parent_focus_recursive_behavior = p_focus_recursive_behavior;
  2058. for (int i = 0; i < get_child_count(); i++) {
  2059. Control *control = Object::cast_to<Control>(get_child(i));
  2060. if (control) {
  2061. control->_apply_focus_behavior_recursively(p_focus_recursive_behavior, true);
  2062. }
  2063. }
  2064. }
  2065. bool Control::_is_parent_mouse_disabled() const {
  2066. switch (data.mouse_recursive_behavior) {
  2067. case RECURSIVE_BEHAVIOR_INHERITED:
  2068. return data.parent_mouse_recursive_behavior == RECURSIVE_BEHAVIOR_DISABLED;
  2069. case RECURSIVE_BEHAVIOR_DISABLED:
  2070. return true;
  2071. case RECURSIVE_BEHAVIOR_ENABLED:
  2072. return false;
  2073. }
  2074. return false;
  2075. }
  2076. void Control::_apply_mouse_behavior_recursively(RecursiveBehavior p_mouse_recursive_behavior, bool p_skip_non_inherited) {
  2077. if (p_skip_non_inherited && data.mouse_recursive_behavior != RECURSIVE_BEHAVIOR_INHERITED) {
  2078. return;
  2079. }
  2080. data.parent_mouse_recursive_behavior = p_mouse_recursive_behavior;
  2081. for (int i = 0; i < get_child_count(); i++) {
  2082. Control *control = Object::cast_to<Control>(get_child(i));
  2083. if (control) {
  2084. control->_apply_mouse_behavior_recursively(p_mouse_recursive_behavior, true);
  2085. }
  2086. }
  2087. }
  2088. Control *Control::find_valid_focus_neighbor(Side p_side) const {
  2089. return const_cast<Control *>(this)->_get_focus_neighbor(p_side);
  2090. }
  2091. void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, const Rect2 &p_rect, const Rect2 &p_clamp, real_t p_min, real_t &r_closest_dist_squared, Control **r_closest) {
  2092. if (Object::cast_to<Viewport>(p_at)) {
  2093. return; // Bye.
  2094. }
  2095. Control *c = Object::cast_to<Control>(p_at);
  2096. Container *container = Object::cast_to<Container>(p_at);
  2097. bool in_container = container ? container->is_ancestor_of(this) : false;
  2098. if (c && c != this && c->get_focus_mode_with_recursive() == FOCUS_ALL && !in_container && p_clamp.intersects(c->get_global_rect())) {
  2099. Rect2 r_c = c->get_global_rect();
  2100. r_c = r_c.intersection(p_clamp);
  2101. real_t begin_d = p_dir.dot(r_c.get_position());
  2102. real_t end_d = p_dir.dot(r_c.get_end());
  2103. real_t max = MAX(begin_d, end_d);
  2104. // Use max to allow navigation to overlapping controls (for ScrollContainer case).
  2105. if (max > (p_min + CMP_EPSILON)) {
  2106. // Calculate the shortest distance. (No shear transform)
  2107. // Flip along axis(es) so that C falls in the first quadrant of c (as origin) for easy calculation.
  2108. // The same transformation would put the direction vector in the positive direction (+x or +y).
  2109. // | -------------
  2110. // | | | |
  2111. // | |-----C-----|
  2112. // ----|---a | | |
  2113. // | | | b------------
  2114. // -|---c---|----------------------->
  2115. // | | |
  2116. // ----|----
  2117. // cC = ca + ab + bC
  2118. // The shortest distance is the vector ab's length or its positive projection length.
  2119. Vector2 cC_origin = r_c.get_center() - p_rect.get_center();
  2120. Vector2 cC = cC_origin.abs(); // Converted to fall in the first quadrant of c.
  2121. Vector2 ab = cC - 0.5 * r_c.get_size() - 0.5 * p_rect.get_size();
  2122. real_t min_d_squared = 0.0;
  2123. if (ab.x > 0.0) {
  2124. min_d_squared += ab.x * ab.x;
  2125. }
  2126. if (ab.y > 0.0) {
  2127. min_d_squared += ab.y * ab.y;
  2128. }
  2129. if (min_d_squared < r_closest_dist_squared || *r_closest == nullptr) {
  2130. r_closest_dist_squared = min_d_squared;
  2131. *r_closest = c;
  2132. } else if (min_d_squared == r_closest_dist_squared) {
  2133. // Tie-breaking aims to address situations where a potential focus neighbor's bounding rect
  2134. // is right next to the currently focused control (e.g. in BoxContainer with
  2135. // separation overridden to 0). This needs specific handling so that the correct
  2136. // focus neighbor is selected.
  2137. Point2 p_center = p_rect.get_center();
  2138. Control *closest = *r_closest;
  2139. Point2 closest_center = closest->get_global_rect().get_center();
  2140. // Tie-break in favor of the control most aligned with p_dir.
  2141. if (Math::abs(p_dir.cross(cC_origin)) < Math::abs(p_dir.cross(closest_center - p_center))) {
  2142. *r_closest = c;
  2143. }
  2144. }
  2145. }
  2146. }
  2147. ScrollContainer *sc = Object::cast_to<ScrollContainer>(c);
  2148. Rect2 intersection = p_clamp;
  2149. if (sc) {
  2150. if (!sc->is_following_focus() || !in_container) {
  2151. intersection = p_clamp.intersection(sc->get_global_rect());
  2152. if (!intersection.has_area()) {
  2153. return;
  2154. }
  2155. }
  2156. }
  2157. for (int i = 0; i < p_at->get_child_count(); i++) {
  2158. Node *child = p_at->get_child(i);
  2159. Control *childc = Object::cast_to<Control>(child);
  2160. if (childc) {
  2161. if (childc->data.RI) {
  2162. continue; // Subwindow, ignore.
  2163. }
  2164. if (!childc->is_visible_in_tree()) {
  2165. continue; // Skip invisible node trees.
  2166. }
  2167. if (Object::cast_to<ScrollContainer>(childc) && childc->is_ancestor_of(this)) {
  2168. continue; // Already searched in it, skip it.
  2169. }
  2170. }
  2171. _window_find_focus_neighbor(p_dir, child, p_rect, intersection, p_min, r_closest_dist_squared, r_closest);
  2172. }
  2173. }
  2174. // Rendering.
  2175. void Control::set_default_cursor_shape(CursorShape p_shape) {
  2176. ERR_MAIN_THREAD_GUARD;
  2177. ERR_FAIL_INDEX(int(p_shape), CURSOR_MAX);
  2178. if (data.default_cursor == p_shape) {
  2179. return;
  2180. }
  2181. data.default_cursor = p_shape;
  2182. if (!is_inside_tree()) {
  2183. return;
  2184. }
  2185. if (!get_global_rect().has_point(get_global_mouse_position())) {
  2186. return;
  2187. }
  2188. // Display the new cursor shape instantly.
  2189. get_viewport()->update_mouse_cursor_state();
  2190. }
  2191. Control::CursorShape Control::get_default_cursor_shape() const {
  2192. ERR_READ_THREAD_GUARD_V(CURSOR_ARROW);
  2193. return data.default_cursor;
  2194. }
  2195. Control::CursorShape Control::get_cursor_shape(const Point2 &p_pos) const {
  2196. ERR_READ_THREAD_GUARD_V(CURSOR_ARROW);
  2197. return data.default_cursor;
  2198. }
  2199. void Control::set_disable_visibility_clip(bool p_ignore) {
  2200. ERR_MAIN_THREAD_GUARD;
  2201. if (data.disable_visibility_clip == p_ignore) {
  2202. return;
  2203. }
  2204. data.disable_visibility_clip = p_ignore;
  2205. queue_redraw();
  2206. }
  2207. bool Control::is_visibility_clip_disabled() const {
  2208. ERR_READ_THREAD_GUARD_V(false);
  2209. return data.disable_visibility_clip;
  2210. }
  2211. void Control::set_clip_contents(bool p_clip) {
  2212. ERR_MAIN_THREAD_GUARD;
  2213. if (data.clip_contents == p_clip) {
  2214. return;
  2215. }
  2216. data.clip_contents = p_clip;
  2217. queue_redraw();
  2218. }
  2219. bool Control::is_clipping_contents() {
  2220. ERR_READ_THREAD_GUARD_V(false);
  2221. return data.clip_contents;
  2222. }
  2223. // Theming.
  2224. void Control::_theme_changed() {
  2225. if (is_inside_tree()) {
  2226. data.theme_owner->propagate_theme_changed(this, this, true, false);
  2227. }
  2228. }
  2229. void Control::_notify_theme_override_changed() {
  2230. if (!data.bulk_theme_override && is_inside_tree()) {
  2231. notification(NOTIFICATION_THEME_CHANGED);
  2232. }
  2233. }
  2234. void Control::_invalidate_theme_cache() {
  2235. data.theme_icon_cache.clear();
  2236. data.theme_style_cache.clear();
  2237. data.theme_font_cache.clear();
  2238. data.theme_font_size_cache.clear();
  2239. data.theme_color_cache.clear();
  2240. data.theme_constant_cache.clear();
  2241. }
  2242. void Control::_update_theme_item_cache() {
  2243. ThemeDB::get_singleton()->update_class_instance_items(this);
  2244. }
  2245. void Control::set_theme_owner_node(Node *p_node) {
  2246. ERR_MAIN_THREAD_GUARD;
  2247. data.theme_owner->set_owner_node(p_node);
  2248. }
  2249. Node *Control::get_theme_owner_node() const {
  2250. ERR_READ_THREAD_GUARD_V(nullptr);
  2251. return data.theme_owner->get_owner_node();
  2252. }
  2253. bool Control::has_theme_owner_node() const {
  2254. ERR_READ_THREAD_GUARD_V(false);
  2255. return data.theme_owner->has_owner_node();
  2256. }
  2257. void Control::set_theme_context(ThemeContext *p_context, bool p_propagate) {
  2258. ERR_MAIN_THREAD_GUARD;
  2259. data.theme_owner->set_owner_context(p_context, p_propagate);
  2260. }
  2261. void Control::set_theme(const Ref<Theme> &p_theme) {
  2262. ERR_MAIN_THREAD_GUARD;
  2263. if (data.theme == p_theme) {
  2264. return;
  2265. }
  2266. if (data.theme.is_valid()) {
  2267. data.theme->disconnect_changed(callable_mp(this, &Control::_theme_changed));
  2268. }
  2269. data.theme = p_theme;
  2270. if (data.theme.is_valid()) {
  2271. data.theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
  2272. data.theme->connect_changed(callable_mp(this, &Control::_theme_changed), CONNECT_DEFERRED);
  2273. return;
  2274. }
  2275. Control *parent_c = Object::cast_to<Control>(get_parent());
  2276. if (parent_c && parent_c->has_theme_owner_node()) {
  2277. data.theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
  2278. return;
  2279. }
  2280. Window *parent_w = cast_to<Window>(get_parent());
  2281. if (parent_w && parent_w->has_theme_owner_node()) {
  2282. data.theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
  2283. return;
  2284. }
  2285. data.theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
  2286. }
  2287. Ref<Theme> Control::get_theme() const {
  2288. ERR_READ_THREAD_GUARD_V(Ref<Theme>());
  2289. return data.theme;
  2290. }
  2291. void Control::set_theme_type_variation(const StringName &p_theme_type) {
  2292. ERR_MAIN_THREAD_GUARD;
  2293. if (data.theme_type_variation == p_theme_type) {
  2294. return;
  2295. }
  2296. data.theme_type_variation = p_theme_type;
  2297. if (is_inside_tree()) {
  2298. notification(NOTIFICATION_THEME_CHANGED);
  2299. }
  2300. }
  2301. StringName Control::get_theme_type_variation() const {
  2302. ERR_READ_THREAD_GUARD_V(StringName());
  2303. return data.theme_type_variation;
  2304. }
  2305. /// Theme property lookup.
  2306. Ref<Texture2D> Control::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  2307. ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
  2308. if (!data.initialized) {
  2309. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2310. }
  2311. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2312. const Ref<Texture2D> *tex = data.theme_icon_override.getptr(p_name);
  2313. if (tex) {
  2314. return *tex;
  2315. }
  2316. }
  2317. if (data.theme_icon_cache.has(p_theme_type) && data.theme_icon_cache[p_theme_type].has(p_name)) {
  2318. return data.theme_icon_cache[p_theme_type][p_name];
  2319. }
  2320. Vector<StringName> theme_types;
  2321. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2322. Ref<Texture2D> icon = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  2323. data.theme_icon_cache[p_theme_type][p_name] = icon;
  2324. return icon;
  2325. }
  2326. Ref<StyleBox> Control::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  2327. ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
  2328. if (!data.initialized) {
  2329. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2330. }
  2331. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2332. const Ref<StyleBox> *style = data.theme_style_override.getptr(p_name);
  2333. if (style) {
  2334. return *style;
  2335. }
  2336. }
  2337. if (data.theme_style_cache.has(p_theme_type) && data.theme_style_cache[p_theme_type].has(p_name)) {
  2338. return data.theme_style_cache[p_theme_type][p_name];
  2339. }
  2340. Vector<StringName> theme_types;
  2341. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2342. Ref<StyleBox> style = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  2343. data.theme_style_cache[p_theme_type][p_name] = style;
  2344. return style;
  2345. }
  2346. Ref<Font> Control::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  2347. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  2348. if (!data.initialized) {
  2349. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2350. }
  2351. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2352. const Ref<Font> *font = data.theme_font_override.getptr(p_name);
  2353. if (font) {
  2354. return *font;
  2355. }
  2356. }
  2357. if (data.theme_font_cache.has(p_theme_type) && data.theme_font_cache[p_theme_type].has(p_name)) {
  2358. return data.theme_font_cache[p_theme_type][p_name];
  2359. }
  2360. Vector<StringName> theme_types;
  2361. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2362. Ref<Font> font = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  2363. data.theme_font_cache[p_theme_type][p_name] = font;
  2364. return font;
  2365. }
  2366. int Control::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  2367. ERR_READ_THREAD_GUARD_V(0);
  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. const int *font_size = data.theme_font_size_override.getptr(p_name);
  2373. if (font_size && (*font_size) > 0) {
  2374. return *font_size;
  2375. }
  2376. }
  2377. if (data.theme_font_size_cache.has(p_theme_type) && data.theme_font_size_cache[p_theme_type].has(p_name)) {
  2378. return data.theme_font_size_cache[p_theme_type][p_name];
  2379. }
  2380. Vector<StringName> theme_types;
  2381. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2382. int font_size = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  2383. data.theme_font_size_cache[p_theme_type][p_name] = font_size;
  2384. return font_size;
  2385. }
  2386. Color Control::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  2387. ERR_READ_THREAD_GUARD_V(Color());
  2388. if (!data.initialized) {
  2389. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2390. }
  2391. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2392. const Color *color = data.theme_color_override.getptr(p_name);
  2393. if (color) {
  2394. return *color;
  2395. }
  2396. }
  2397. if (data.theme_color_cache.has(p_theme_type) && data.theme_color_cache[p_theme_type].has(p_name)) {
  2398. return data.theme_color_cache[p_theme_type][p_name];
  2399. }
  2400. Vector<StringName> theme_types;
  2401. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2402. Color color = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  2403. data.theme_color_cache[p_theme_type][p_name] = color;
  2404. return color;
  2405. }
  2406. int Control::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  2407. ERR_READ_THREAD_GUARD_V(0);
  2408. if (!data.initialized) {
  2409. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2410. }
  2411. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2412. const int *constant = data.theme_constant_override.getptr(p_name);
  2413. if (constant) {
  2414. return *constant;
  2415. }
  2416. }
  2417. if (data.theme_constant_cache.has(p_theme_type) && data.theme_constant_cache[p_theme_type].has(p_name)) {
  2418. return data.theme_constant_cache[p_theme_type][p_name];
  2419. }
  2420. Vector<StringName> theme_types;
  2421. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2422. int constant = data.theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  2423. data.theme_constant_cache[p_theme_type][p_name] = constant;
  2424. return constant;
  2425. }
  2426. Variant Control::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  2427. switch (p_data_type) {
  2428. case Theme::DATA_TYPE_COLOR:
  2429. return get_theme_color(p_name, p_theme_type);
  2430. case Theme::DATA_TYPE_CONSTANT:
  2431. return get_theme_constant(p_name, p_theme_type);
  2432. case Theme::DATA_TYPE_FONT:
  2433. return get_theme_font(p_name, p_theme_type);
  2434. case Theme::DATA_TYPE_FONT_SIZE:
  2435. return get_theme_font_size(p_name, p_theme_type);
  2436. case Theme::DATA_TYPE_ICON:
  2437. return get_theme_icon(p_name, p_theme_type);
  2438. case Theme::DATA_TYPE_STYLEBOX:
  2439. return get_theme_stylebox(p_name, p_theme_type);
  2440. case Theme::DATA_TYPE_MAX:
  2441. break; // Can't happen, but silences warning.
  2442. }
  2443. return Variant();
  2444. }
  2445. #ifdef TOOLS_ENABLED
  2446. Ref<Texture2D> Control::get_editor_theme_icon(const StringName &p_name) const {
  2447. return get_theme_icon(p_name, SNAME("EditorIcons"));
  2448. }
  2449. #endif // TOOLS_ENABLED
  2450. bool Control::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  2451. ERR_READ_THREAD_GUARD_V(false);
  2452. if (!data.initialized) {
  2453. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2454. }
  2455. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2456. if (has_theme_icon_override(p_name)) {
  2457. return true;
  2458. }
  2459. }
  2460. Vector<StringName> theme_types;
  2461. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2462. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  2463. }
  2464. bool Control::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  2465. ERR_READ_THREAD_GUARD_V(false);
  2466. if (!data.initialized) {
  2467. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2468. }
  2469. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2470. if (has_theme_stylebox_override(p_name)) {
  2471. return true;
  2472. }
  2473. }
  2474. Vector<StringName> theme_types;
  2475. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2476. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  2477. }
  2478. bool Control::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  2479. ERR_READ_THREAD_GUARD_V(false);
  2480. if (!data.initialized) {
  2481. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2482. }
  2483. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2484. if (has_theme_font_override(p_name)) {
  2485. return true;
  2486. }
  2487. }
  2488. Vector<StringName> theme_types;
  2489. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2490. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  2491. }
  2492. bool Control::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  2493. ERR_READ_THREAD_GUARD_V(false);
  2494. if (!data.initialized) {
  2495. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2496. }
  2497. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2498. if (has_theme_font_size_override(p_name)) {
  2499. return true;
  2500. }
  2501. }
  2502. Vector<StringName> theme_types;
  2503. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2504. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  2505. }
  2506. bool Control::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  2507. ERR_READ_THREAD_GUARD_V(false);
  2508. if (!data.initialized) {
  2509. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2510. }
  2511. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2512. if (has_theme_color_override(p_name)) {
  2513. return true;
  2514. }
  2515. }
  2516. Vector<StringName> theme_types;
  2517. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2518. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  2519. }
  2520. bool Control::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  2521. ERR_READ_THREAD_GUARD_V(false);
  2522. if (!data.initialized) {
  2523. WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
  2524. }
  2525. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) {
  2526. if (has_theme_constant_override(p_name)) {
  2527. return true;
  2528. }
  2529. }
  2530. Vector<StringName> theme_types;
  2531. data.theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
  2532. return data.theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  2533. }
  2534. /// Local property overrides.
  2535. void Control::add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon) {
  2536. ERR_MAIN_THREAD_GUARD;
  2537. ERR_FAIL_COND(p_icon.is_null());
  2538. if (data.theme_icon_override.has(p_name)) {
  2539. data.theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2540. }
  2541. data.theme_icon_override[p_name] = p_icon;
  2542. data.theme_icon_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2543. _notify_theme_override_changed();
  2544. }
  2545. void Control::add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
  2546. ERR_MAIN_THREAD_GUARD;
  2547. ERR_FAIL_COND(p_style.is_null());
  2548. if (data.theme_style_override.has(p_name)) {
  2549. data.theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2550. }
  2551. data.theme_style_override[p_name] = p_style;
  2552. data.theme_style_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2553. _notify_theme_override_changed();
  2554. }
  2555. void Control::add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font) {
  2556. ERR_MAIN_THREAD_GUARD;
  2557. ERR_FAIL_COND(p_font.is_null());
  2558. if (data.theme_font_override.has(p_name)) {
  2559. data.theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2560. }
  2561. data.theme_font_override[p_name] = p_font;
  2562. data.theme_font_override[p_name]->connect_changed(callable_mp(this, &Control::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  2563. _notify_theme_override_changed();
  2564. }
  2565. void Control::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
  2566. ERR_MAIN_THREAD_GUARD;
  2567. data.theme_font_size_override[p_name] = p_font_size;
  2568. _notify_theme_override_changed();
  2569. }
  2570. void Control::add_theme_color_override(const StringName &p_name, const Color &p_color) {
  2571. ERR_MAIN_THREAD_GUARD;
  2572. data.theme_color_override[p_name] = p_color;
  2573. _notify_theme_override_changed();
  2574. }
  2575. void Control::add_theme_constant_override(const StringName &p_name, int p_constant) {
  2576. ERR_MAIN_THREAD_GUARD;
  2577. data.theme_constant_override[p_name] = p_constant;
  2578. _notify_theme_override_changed();
  2579. }
  2580. void Control::remove_theme_icon_override(const StringName &p_name) {
  2581. ERR_MAIN_THREAD_GUARD;
  2582. if (data.theme_icon_override.has(p_name)) {
  2583. data.theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2584. }
  2585. data.theme_icon_override.erase(p_name);
  2586. _notify_theme_override_changed();
  2587. }
  2588. void Control::remove_theme_style_override(const StringName &p_name) {
  2589. ERR_MAIN_THREAD_GUARD;
  2590. if (data.theme_style_override.has(p_name)) {
  2591. data.theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2592. }
  2593. data.theme_style_override.erase(p_name);
  2594. _notify_theme_override_changed();
  2595. }
  2596. void Control::remove_theme_font_override(const StringName &p_name) {
  2597. ERR_MAIN_THREAD_GUARD;
  2598. if (data.theme_font_override.has(p_name)) {
  2599. data.theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  2600. }
  2601. data.theme_font_override.erase(p_name);
  2602. _notify_theme_override_changed();
  2603. }
  2604. void Control::remove_theme_font_size_override(const StringName &p_name) {
  2605. ERR_MAIN_THREAD_GUARD;
  2606. data.theme_font_size_override.erase(p_name);
  2607. _notify_theme_override_changed();
  2608. }
  2609. void Control::remove_theme_color_override(const StringName &p_name) {
  2610. ERR_MAIN_THREAD_GUARD;
  2611. data.theme_color_override.erase(p_name);
  2612. _notify_theme_override_changed();
  2613. }
  2614. void Control::remove_theme_constant_override(const StringName &p_name) {
  2615. ERR_MAIN_THREAD_GUARD;
  2616. data.theme_constant_override.erase(p_name);
  2617. _notify_theme_override_changed();
  2618. }
  2619. bool Control::has_theme_icon_override(const StringName &p_name) const {
  2620. ERR_READ_THREAD_GUARD_V(false);
  2621. const Ref<Texture2D> *tex = data.theme_icon_override.getptr(p_name);
  2622. return tex != nullptr;
  2623. }
  2624. bool Control::has_theme_stylebox_override(const StringName &p_name) const {
  2625. ERR_READ_THREAD_GUARD_V(false);
  2626. const Ref<StyleBox> *style = data.theme_style_override.getptr(p_name);
  2627. return style != nullptr;
  2628. }
  2629. bool Control::has_theme_font_override(const StringName &p_name) const {
  2630. ERR_READ_THREAD_GUARD_V(false);
  2631. const Ref<Font> *font = data.theme_font_override.getptr(p_name);
  2632. return font != nullptr;
  2633. }
  2634. bool Control::has_theme_font_size_override(const StringName &p_name) const {
  2635. ERR_READ_THREAD_GUARD_V(false);
  2636. const int *font_size = data.theme_font_size_override.getptr(p_name);
  2637. return font_size != nullptr;
  2638. }
  2639. bool Control::has_theme_color_override(const StringName &p_name) const {
  2640. ERR_READ_THREAD_GUARD_V(false);
  2641. const Color *color = data.theme_color_override.getptr(p_name);
  2642. return color != nullptr;
  2643. }
  2644. bool Control::has_theme_constant_override(const StringName &p_name) const {
  2645. ERR_READ_THREAD_GUARD_V(false);
  2646. const int *constant = data.theme_constant_override.getptr(p_name);
  2647. return constant != nullptr;
  2648. }
  2649. /// Default theme properties.
  2650. float Control::get_theme_default_base_scale() const {
  2651. ERR_READ_THREAD_GUARD_V(0);
  2652. return data.theme_owner->get_theme_default_base_scale();
  2653. }
  2654. Ref<Font> Control::get_theme_default_font() const {
  2655. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  2656. return data.theme_owner->get_theme_default_font();
  2657. }
  2658. int Control::get_theme_default_font_size() const {
  2659. ERR_READ_THREAD_GUARD_V(0);
  2660. return data.theme_owner->get_theme_default_font_size();
  2661. }
  2662. /// Bulk actions.
  2663. void Control::begin_bulk_theme_override() {
  2664. ERR_MAIN_THREAD_GUARD;
  2665. data.bulk_theme_override = true;
  2666. }
  2667. void Control::end_bulk_theme_override() {
  2668. ERR_MAIN_THREAD_GUARD;
  2669. ERR_FAIL_COND(!data.bulk_theme_override);
  2670. data.bulk_theme_override = false;
  2671. _notify_theme_override_changed();
  2672. }
  2673. // Internationalization.
  2674. TypedArray<Vector3i> Control::structured_text_parser(TextServer::StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const {
  2675. ERR_READ_THREAD_GUARD_V(TypedArray<Vector3i>());
  2676. if (p_parser_type == TextServer::STRUCTURED_TEXT_CUSTOM) {
  2677. TypedArray<Vector3i> ret;
  2678. GDVIRTUAL_CALL(_structured_text_parser, p_args, p_text, ret);
  2679. return ret;
  2680. } else {
  2681. return TS->parse_structured_text(p_parser_type, p_args, p_text);
  2682. }
  2683. }
  2684. void Control::set_layout_direction(Control::LayoutDirection p_direction) {
  2685. ERR_MAIN_THREAD_GUARD;
  2686. if (data.layout_dir == p_direction) {
  2687. return;
  2688. }
  2689. ERR_FAIL_INDEX(p_direction, LAYOUT_DIRECTION_MAX);
  2690. data.layout_dir = p_direction;
  2691. propagate_notification(NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  2692. }
  2693. Control::LayoutDirection Control::get_layout_direction() const {
  2694. ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
  2695. return data.layout_dir;
  2696. }
  2697. bool Control::is_layout_rtl() const {
  2698. ERR_READ_THREAD_GUARD_V(false);
  2699. if (data.is_rtl_dirty) {
  2700. data.is_rtl_dirty = false;
  2701. if (data.layout_dir == LAYOUT_DIRECTION_INHERITED) {
  2702. #ifdef TOOLS_ENABLED
  2703. if (is_part_of_edited_scene() && GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2704. data.is_rtl = true;
  2705. return data.is_rtl;
  2706. }
  2707. if (is_inside_tree()) {
  2708. Node *edited_scene_root = get_tree()->get_edited_scene_root();
  2709. if (edited_scene_root == this) {
  2710. int proj_root_layout_direction = GLOBAL_GET(SNAME("internationalization/rendering/root_node_layout_direction"));
  2711. if (proj_root_layout_direction == 1) {
  2712. data.is_rtl = false;
  2713. } else if (proj_root_layout_direction == 2) {
  2714. data.is_rtl = true;
  2715. } else if (proj_root_layout_direction == 3) {
  2716. String locale = OS::get_singleton()->get_locale();
  2717. data.is_rtl = TS->is_locale_right_to_left(locale);
  2718. } else {
  2719. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2720. data.is_rtl = TS->is_locale_right_to_left(locale);
  2721. }
  2722. return data.is_rtl;
  2723. }
  2724. }
  2725. #else
  2726. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2727. data.is_rtl = true;
  2728. return data.is_rtl;
  2729. }
  2730. #endif // TOOLS_ENABLED
  2731. Node *parent_node = get_parent();
  2732. while (parent_node) {
  2733. Control *parent_control = Object::cast_to<Control>(parent_node);
  2734. if (parent_control) {
  2735. data.is_rtl = parent_control->is_layout_rtl();
  2736. return data.is_rtl;
  2737. }
  2738. Window *parent_window = Object::cast_to<Window>(parent_node);
  2739. if (parent_window) {
  2740. data.is_rtl = parent_window->is_layout_rtl();
  2741. return data.is_rtl;
  2742. }
  2743. parent_node = parent_node->get_parent();
  2744. }
  2745. if (root_layout_direction == 1) {
  2746. data.is_rtl = false;
  2747. } else if (root_layout_direction == 2) {
  2748. data.is_rtl = true;
  2749. } else if (root_layout_direction == 3) {
  2750. String locale = OS::get_singleton()->get_locale();
  2751. data.is_rtl = TS->is_locale_right_to_left(locale);
  2752. } else {
  2753. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2754. data.is_rtl = TS->is_locale_right_to_left(locale);
  2755. }
  2756. } else if (data.layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
  2757. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2758. data.is_rtl = true;
  2759. } else {
  2760. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2761. data.is_rtl = TS->is_locale_right_to_left(locale);
  2762. }
  2763. } else if (data.layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
  2764. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2765. const_cast<Control *>(this)->data.is_rtl = true;
  2766. } else {
  2767. String locale = OS::get_singleton()->get_locale();
  2768. const_cast<Control *>(this)->data.is_rtl = TS->is_locale_right_to_left(locale);
  2769. }
  2770. } else {
  2771. data.is_rtl = (data.layout_dir == LAYOUT_DIRECTION_RTL);
  2772. }
  2773. }
  2774. return data.is_rtl;
  2775. }
  2776. void Control::set_localize_numeral_system(bool p_enable) {
  2777. ERR_MAIN_THREAD_GUARD;
  2778. if (p_enable == data.localize_numeral_system) {
  2779. return;
  2780. }
  2781. data.localize_numeral_system = p_enable;
  2782. notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  2783. }
  2784. bool Control::is_localizing_numeral_system() const {
  2785. ERR_READ_THREAD_GUARD_V(false);
  2786. return data.localize_numeral_system;
  2787. }
  2788. #ifndef DISABLE_DEPRECATED
  2789. void Control::set_auto_translate(bool p_enable) {
  2790. ERR_MAIN_THREAD_GUARD;
  2791. set_auto_translate_mode(p_enable ? AUTO_TRANSLATE_MODE_ALWAYS : AUTO_TRANSLATE_MODE_DISABLED);
  2792. }
  2793. bool Control::is_auto_translating() const {
  2794. ERR_READ_THREAD_GUARD_V(false);
  2795. return can_auto_translate();
  2796. }
  2797. #endif // DISABLE_DEPRECATED
  2798. void Control::set_tooltip_auto_translate_mode(AutoTranslateMode p_mode) {
  2799. ERR_MAIN_THREAD_GUARD;
  2800. data.tooltip_auto_translate_mode = p_mode;
  2801. }
  2802. Node::AutoTranslateMode Control::get_tooltip_auto_translate_mode() const {
  2803. ERR_READ_THREAD_GUARD_V(AUTO_TRANSLATE_MODE_INHERIT);
  2804. return data.tooltip_auto_translate_mode;
  2805. }
  2806. // Extra properties.
  2807. void Control::set_tooltip_text(const String &p_hint) {
  2808. ERR_MAIN_THREAD_GUARD;
  2809. data.tooltip = p_hint;
  2810. update_configuration_warnings();
  2811. }
  2812. String Control::get_tooltip_text() const {
  2813. ERR_READ_THREAD_GUARD_V(String());
  2814. return data.tooltip;
  2815. }
  2816. String Control::get_tooltip(const Point2 &p_pos) const {
  2817. ERR_READ_THREAD_GUARD_V(String());
  2818. String ret;
  2819. if (GDVIRTUAL_CALL(_get_tooltip, p_pos, ret)) {
  2820. return ret;
  2821. }
  2822. return data.tooltip;
  2823. }
  2824. Control *Control::make_custom_tooltip(const String &p_text) const {
  2825. ERR_READ_THREAD_GUARD_V(nullptr);
  2826. Object *ret = nullptr;
  2827. GDVIRTUAL_CALL(_make_custom_tooltip, p_text, ret);
  2828. return Object::cast_to<Control>(ret);
  2829. }
  2830. // Base object overrides.
  2831. void Control::_notification(int p_notification) {
  2832. ERR_MAIN_THREAD_GUARD;
  2833. switch (p_notification) {
  2834. #ifdef TOOLS_ENABLED
  2835. case NOTIFICATION_EDITOR_PRE_SAVE: {
  2836. saving = true;
  2837. } break;
  2838. case NOTIFICATION_EDITOR_POST_SAVE: {
  2839. saving = false;
  2840. } break;
  2841. #endif // TOOLS_ENABLED
  2842. case NOTIFICATION_POSTINITIALIZE: {
  2843. data.initialized = true;
  2844. _invalidate_theme_cache();
  2845. _update_theme_item_cache();
  2846. } break;
  2847. case NOTIFICATION_PARENTED: {
  2848. Node *parent_node = get_parent();
  2849. data.parent_control = Object::cast_to<Control>(parent_node);
  2850. data.parent_window = Object::cast_to<Window>(parent_node);
  2851. data.theme_owner->assign_theme_on_parented(this);
  2852. _update_layout_mode();
  2853. set_focus_recursive_behavior(data.focus_recursive_behavior);
  2854. set_mouse_recursive_behavior(data.mouse_recursive_behavior);
  2855. } break;
  2856. case NOTIFICATION_UNPARENTED: {
  2857. data.parent_control = nullptr;
  2858. data.parent_window = nullptr;
  2859. data.theme_owner->clear_theme_on_unparented(this);
  2860. } break;
  2861. case NOTIFICATION_ENTER_TREE: {
  2862. // Emits NOTIFICATION_THEME_CHANGED internally.
  2863. set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
  2864. } break;
  2865. case NOTIFICATION_POST_ENTER_TREE: {
  2866. data.is_rtl_dirty = true;
  2867. update_minimum_size();
  2868. _size_changed();
  2869. } break;
  2870. case NOTIFICATION_EXIT_TREE: {
  2871. set_theme_context(nullptr, false);
  2872. release_focus();
  2873. get_viewport()->_gui_remove_control(this);
  2874. } break;
  2875. case NOTIFICATION_READY: {
  2876. #ifdef DEBUG_ENABLED
  2877. connect(SceneStringName(ready), callable_mp(this, &Control::_clear_size_warning), CONNECT_DEFERRED | CONNECT_ONE_SHOT);
  2878. #endif // DEBUG_ENABLED
  2879. } break;
  2880. case NOTIFICATION_ENTER_CANVAS: {
  2881. data.is_rtl_dirty = true;
  2882. CanvasItem *node = this;
  2883. bool has_parent_control = false;
  2884. while (!node->is_set_as_top_level()) {
  2885. CanvasItem *parent = Object::cast_to<CanvasItem>(node->get_parent());
  2886. if (!parent) {
  2887. break;
  2888. }
  2889. Control *parent_control = Object::cast_to<Control>(parent);
  2890. if (parent_control) {
  2891. has_parent_control = true;
  2892. break;
  2893. }
  2894. node = parent;
  2895. }
  2896. if (has_parent_control) {
  2897. // Do nothing, has a parent control.
  2898. } else {
  2899. // Is a regular root control or top_level.
  2900. Viewport *viewport = get_viewport();
  2901. ERR_FAIL_NULL(viewport);
  2902. data.RI = viewport->_gui_add_root_control(this);
  2903. get_parent()->connect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty), CONNECT_REFERENCE_COUNTED);
  2904. }
  2905. data.parent_canvas_item = get_parent_item();
  2906. if (data.parent_canvas_item) {
  2907. data.parent_canvas_item->connect(SceneStringName(item_rect_changed), callable_mp(this, &Control::_size_changed));
  2908. } else {
  2909. // Connect viewport.
  2910. Viewport *viewport = get_viewport();
  2911. ERR_FAIL_NULL(viewport);
  2912. viewport->connect("size_changed", callable_mp(this, &Control::_size_changed));
  2913. }
  2914. } break;
  2915. case NOTIFICATION_EXIT_CANVAS: {
  2916. if (data.parent_canvas_item) {
  2917. data.parent_canvas_item->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &Control::_size_changed));
  2918. data.parent_canvas_item = nullptr;
  2919. } else {
  2920. // Disconnect viewport.
  2921. Viewport *viewport = get_viewport();
  2922. ERR_FAIL_NULL(viewport);
  2923. viewport->disconnect("size_changed", callable_mp(this, &Control::_size_changed));
  2924. }
  2925. if (data.RI) {
  2926. get_viewport()->_gui_remove_root_control(data.RI);
  2927. data.RI = nullptr;
  2928. get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(get_viewport(), &Viewport::gui_set_root_order_dirty));
  2929. }
  2930. data.parent_canvas_item = nullptr;
  2931. data.is_rtl_dirty = true;
  2932. } break;
  2933. case NOTIFICATION_CHILD_ORDER_CHANGED: {
  2934. // Some parents need to know the order of the children to draw (like TabContainer),
  2935. // so we update them just in case.
  2936. queue_redraw();
  2937. } break;
  2938. case NOTIFICATION_RESIZED: {
  2939. emit_signal(SceneStringName(resized));
  2940. } break;
  2941. case NOTIFICATION_DRAW: {
  2942. _update_canvas_item_transform();
  2943. RenderingServer::get_singleton()->canvas_item_set_custom_rect(get_canvas_item(), !data.disable_visibility_clip, Rect2(Point2(), get_size()));
  2944. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), data.clip_contents);
  2945. } break;
  2946. case NOTIFICATION_MOUSE_ENTER: {
  2947. emit_signal(SceneStringName(mouse_entered));
  2948. } break;
  2949. case NOTIFICATION_MOUSE_EXIT: {
  2950. emit_signal(SceneStringName(mouse_exited));
  2951. } break;
  2952. case NOTIFICATION_FOCUS_ENTER: {
  2953. emit_signal(SceneStringName(focus_entered));
  2954. queue_redraw();
  2955. } break;
  2956. case NOTIFICATION_FOCUS_EXIT: {
  2957. emit_signal(SceneStringName(focus_exited));
  2958. queue_redraw();
  2959. } break;
  2960. case NOTIFICATION_THEME_CHANGED: {
  2961. emit_signal(SceneStringName(theme_changed));
  2962. _invalidate_theme_cache();
  2963. _update_theme_item_cache();
  2964. queue_redraw();
  2965. update_minimum_size();
  2966. _size_changed();
  2967. } break;
  2968. case NOTIFICATION_VISIBILITY_CHANGED: {
  2969. if (!is_visible_in_tree()) {
  2970. if (get_viewport() != nullptr) {
  2971. get_viewport()->_gui_hide_control(this);
  2972. }
  2973. } else {
  2974. update_minimum_size();
  2975. _size_changed();
  2976. }
  2977. } break;
  2978. case NOTIFICATION_TRANSLATION_CHANGED:
  2979. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  2980. if (is_inside_tree()) {
  2981. data.is_rtl_dirty = true;
  2982. _invalidate_theme_cache();
  2983. _update_theme_item_cache();
  2984. queue_redraw();
  2985. update_minimum_size();
  2986. _size_changed();
  2987. }
  2988. } break;
  2989. }
  2990. }
  2991. void Control::_bind_methods() {
  2992. ClassDB::bind_method(D_METHOD("accept_event"), &Control::accept_event);
  2993. ClassDB::bind_method(D_METHOD("get_minimum_size"), &Control::get_minimum_size);
  2994. ClassDB::bind_method(D_METHOD("get_combined_minimum_size"), &Control::get_combined_minimum_size);
  2995. ClassDB::bind_method(D_METHOD("_set_layout_mode", "mode"), &Control::_set_layout_mode);
  2996. ClassDB::bind_method(D_METHOD("_get_layout_mode"), &Control::_get_layout_mode);
  2997. ClassDB::bind_method(D_METHOD("_set_anchors_layout_preset", "preset"), &Control::_set_anchors_layout_preset);
  2998. ClassDB::bind_method(D_METHOD("_get_anchors_layout_preset"), &Control::_get_anchors_layout_preset);
  2999. ClassDB::bind_method(D_METHOD("set_anchors_preset", "preset", "keep_offsets"), &Control::set_anchors_preset, DEFVAL(false));
  3000. ClassDB::bind_method(D_METHOD("set_offsets_preset", "preset", "resize_mode", "margin"), &Control::set_offsets_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
  3001. 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));
  3002. ClassDB::bind_method(D_METHOD("_set_anchor", "side", "anchor"), &Control::_set_anchor);
  3003. ClassDB::bind_method(D_METHOD("set_anchor", "side", "anchor", "keep_offset", "push_opposite_anchor"), &Control::set_anchor, DEFVAL(false), DEFVAL(true));
  3004. ClassDB::bind_method(D_METHOD("get_anchor", "side"), &Control::get_anchor);
  3005. ClassDB::bind_method(D_METHOD("set_offset", "side", "offset"), &Control::set_offset);
  3006. ClassDB::bind_method(D_METHOD("get_offset", "offset"), &Control::get_offset);
  3007. ClassDB::bind_method(D_METHOD("set_anchor_and_offset", "side", "anchor", "offset", "push_opposite_anchor"), &Control::set_anchor_and_offset, DEFVAL(false));
  3008. ClassDB::bind_method(D_METHOD("set_begin", "position"), &Control::set_begin);
  3009. ClassDB::bind_method(D_METHOD("set_end", "position"), &Control::set_end);
  3010. ClassDB::bind_method(D_METHOD("set_position", "position", "keep_offsets"), &Control::set_position, DEFVAL(false));
  3011. ClassDB::bind_method(D_METHOD("_set_position", "position"), &Control::_set_position);
  3012. ClassDB::bind_method(D_METHOD("set_size", "size", "keep_offsets"), &Control::set_size, DEFVAL(false));
  3013. ClassDB::bind_method(D_METHOD("reset_size"), &Control::reset_size);
  3014. ClassDB::bind_method(D_METHOD("_set_size", "size"), &Control::_set_size);
  3015. ClassDB::bind_method(D_METHOD("set_custom_minimum_size", "size"), &Control::set_custom_minimum_size);
  3016. ClassDB::bind_method(D_METHOD("set_global_position", "position", "keep_offsets"), &Control::set_global_position, DEFVAL(false));
  3017. ClassDB::bind_method(D_METHOD("_set_global_position", "position"), &Control::_set_global_position);
  3018. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Control::set_rotation);
  3019. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Control::set_rotation_degrees);
  3020. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale);
  3021. ClassDB::bind_method(D_METHOD("set_pivot_offset", "pivot_offset"), &Control::set_pivot_offset);
  3022. ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin);
  3023. ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end);
  3024. ClassDB::bind_method(D_METHOD("get_position"), &Control::get_position);
  3025. ClassDB::bind_method(D_METHOD("get_size"), &Control::get_size);
  3026. ClassDB::bind_method(D_METHOD("get_rotation"), &Control::get_rotation);
  3027. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Control::get_rotation_degrees);
  3028. ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale);
  3029. ClassDB::bind_method(D_METHOD("get_pivot_offset"), &Control::get_pivot_offset);
  3030. ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size);
  3031. ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_parent_area_size);
  3032. ClassDB::bind_method(D_METHOD("get_global_position"), &Control::get_global_position);
  3033. ClassDB::bind_method(D_METHOD("get_screen_position"), &Control::get_screen_position);
  3034. ClassDB::bind_method(D_METHOD("get_rect"), &Control::get_rect);
  3035. ClassDB::bind_method(D_METHOD("get_global_rect"), &Control::get_global_rect);
  3036. ClassDB::bind_method(D_METHOD("set_focus_mode", "mode"), &Control::set_focus_mode);
  3037. ClassDB::bind_method(D_METHOD("get_focus_mode"), &Control::get_focus_mode);
  3038. ClassDB::bind_method(D_METHOD("get_focus_mode_with_recursive"), &Control::get_focus_mode_with_recursive);
  3039. ClassDB::bind_method(D_METHOD("set_focus_recursive_behavior", "focus_recursive_behavior"), &Control::set_focus_recursive_behavior);
  3040. ClassDB::bind_method(D_METHOD("get_focus_recursive_behavior"), &Control::get_focus_recursive_behavior);
  3041. ClassDB::bind_method(D_METHOD("has_focus"), &Control::has_focus);
  3042. ClassDB::bind_method(D_METHOD("grab_focus"), &Control::grab_focus);
  3043. ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus);
  3044. ClassDB::bind_method(D_METHOD("find_prev_valid_focus"), &Control::find_prev_valid_focus);
  3045. ClassDB::bind_method(D_METHOD("find_next_valid_focus"), &Control::find_next_valid_focus);
  3046. ClassDB::bind_method(D_METHOD("find_valid_focus_neighbor", "side"), &Control::find_valid_focus_neighbor);
  3047. ClassDB::bind_method(D_METHOD("set_h_size_flags", "flags"), &Control::set_h_size_flags);
  3048. ClassDB::bind_method(D_METHOD("get_h_size_flags"), &Control::get_h_size_flags);
  3049. ClassDB::bind_method(D_METHOD("set_stretch_ratio", "ratio"), &Control::set_stretch_ratio);
  3050. ClassDB::bind_method(D_METHOD("get_stretch_ratio"), &Control::get_stretch_ratio);
  3051. ClassDB::bind_method(D_METHOD("set_v_size_flags", "flags"), &Control::set_v_size_flags);
  3052. ClassDB::bind_method(D_METHOD("get_v_size_flags"), &Control::get_v_size_flags);
  3053. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Control::set_theme);
  3054. ClassDB::bind_method(D_METHOD("get_theme"), &Control::get_theme);
  3055. ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Control::set_theme_type_variation);
  3056. ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Control::get_theme_type_variation);
  3057. ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Control::begin_bulk_theme_override);
  3058. ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Control::end_bulk_theme_override);
  3059. ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Control::add_theme_icon_override);
  3060. ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Control::add_theme_style_override);
  3061. ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Control::add_theme_font_override);
  3062. ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Control::add_theme_font_size_override);
  3063. ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Control::add_theme_color_override);
  3064. ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Control::add_theme_constant_override);
  3065. ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Control::remove_theme_icon_override);
  3066. ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Control::remove_theme_style_override);
  3067. ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Control::remove_theme_font_override);
  3068. ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Control::remove_theme_font_size_override);
  3069. ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Control::remove_theme_color_override);
  3070. ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Control::remove_theme_constant_override);
  3071. ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Control::get_theme_icon, DEFVAL(StringName()));
  3072. ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Control::get_theme_stylebox, DEFVAL(StringName()));
  3073. ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Control::get_theme_font, DEFVAL(StringName()));
  3074. ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Control::get_theme_font_size, DEFVAL(StringName()));
  3075. ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Control::get_theme_color, DEFVAL(StringName()));
  3076. ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Control::get_theme_constant, DEFVAL(StringName()));
  3077. ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Control::has_theme_icon_override);
  3078. ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Control::has_theme_stylebox_override);
  3079. ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Control::has_theme_font_override);
  3080. ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Control::has_theme_font_size_override);
  3081. ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Control::has_theme_color_override);
  3082. ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Control::has_theme_constant_override);
  3083. ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Control::has_theme_icon, DEFVAL(StringName()));
  3084. ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Control::has_theme_stylebox, DEFVAL(StringName()));
  3085. ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Control::has_theme_font, DEFVAL(StringName()));
  3086. ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Control::has_theme_font_size, DEFVAL(StringName()));
  3087. ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Control::has_theme_color, DEFVAL(StringName()));
  3088. ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Control::has_theme_constant, DEFVAL(StringName()));
  3089. ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Control::get_theme_default_base_scale);
  3090. ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Control::get_theme_default_font);
  3091. ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Control::get_theme_default_font_size);
  3092. ClassDB::bind_method(D_METHOD("get_parent_control"), &Control::get_parent_control);
  3093. ClassDB::bind_method(D_METHOD("set_h_grow_direction", "direction"), &Control::set_h_grow_direction);
  3094. ClassDB::bind_method(D_METHOD("get_h_grow_direction"), &Control::get_h_grow_direction);
  3095. ClassDB::bind_method(D_METHOD("set_v_grow_direction", "direction"), &Control::set_v_grow_direction);
  3096. ClassDB::bind_method(D_METHOD("get_v_grow_direction"), &Control::get_v_grow_direction);
  3097. ClassDB::bind_method(D_METHOD("set_tooltip_auto_translate_mode", "mode"), &Control::set_tooltip_auto_translate_mode);
  3098. ClassDB::bind_method(D_METHOD("get_tooltip_auto_translate_mode"), &Control::get_tooltip_auto_translate_mode);
  3099. ClassDB::bind_method(D_METHOD("set_tooltip_text", "hint"), &Control::set_tooltip_text);
  3100. ClassDB::bind_method(D_METHOD("get_tooltip_text"), &Control::get_tooltip_text);
  3101. ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2()));
  3102. ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Control::set_default_cursor_shape);
  3103. ClassDB::bind_method(D_METHOD("get_default_cursor_shape"), &Control::get_default_cursor_shape);
  3104. ClassDB::bind_method(D_METHOD("get_cursor_shape", "position"), &Control::get_cursor_shape, DEFVAL(Point2()));
  3105. ClassDB::bind_method(D_METHOD("set_focus_neighbor", "side", "neighbor"), &Control::set_focus_neighbor);
  3106. ClassDB::bind_method(D_METHOD("get_focus_neighbor", "side"), &Control::get_focus_neighbor);
  3107. ClassDB::bind_method(D_METHOD("set_focus_next", "next"), &Control::set_focus_next);
  3108. ClassDB::bind_method(D_METHOD("get_focus_next"), &Control::get_focus_next);
  3109. ClassDB::bind_method(D_METHOD("set_focus_previous", "previous"), &Control::set_focus_previous);
  3110. ClassDB::bind_method(D_METHOD("get_focus_previous"), &Control::get_focus_previous);
  3111. ClassDB::bind_method(D_METHOD("force_drag", "data", "preview"), &Control::force_drag);
  3112. ClassDB::bind_method(D_METHOD("set_mouse_filter", "filter"), &Control::set_mouse_filter);
  3113. ClassDB::bind_method(D_METHOD("get_mouse_filter"), &Control::get_mouse_filter);
  3114. ClassDB::bind_method(D_METHOD("get_mouse_filter_with_recursive"), &Control::get_mouse_filter_with_recursive);
  3115. ClassDB::bind_method(D_METHOD("set_mouse_recursive_behavior", "mouse_recursive_behavior"), &Control::set_mouse_recursive_behavior);
  3116. ClassDB::bind_method(D_METHOD("get_mouse_recursive_behavior"), &Control::get_mouse_recursive_behavior);
  3117. ClassDB::bind_method(D_METHOD("set_force_pass_scroll_events", "force_pass_scroll_events"), &Control::set_force_pass_scroll_events);
  3118. ClassDB::bind_method(D_METHOD("is_force_pass_scroll_events"), &Control::is_force_pass_scroll_events);
  3119. ClassDB::bind_method(D_METHOD("set_clip_contents", "enable"), &Control::set_clip_contents);
  3120. ClassDB::bind_method(D_METHOD("is_clipping_contents"), &Control::is_clipping_contents);
  3121. ClassDB::bind_method(D_METHOD("grab_click_focus"), &Control::grab_click_focus);
  3122. ClassDB::bind_method(D_METHOD("set_drag_forwarding", "drag_func", "can_drop_func", "drop_func"), &Control::set_drag_forwarding);
  3123. ClassDB::bind_method(D_METHOD("set_drag_preview", "control"), &Control::set_drag_preview);
  3124. ClassDB::bind_method(D_METHOD("is_drag_successful"), &Control::is_drag_successful);
  3125. ClassDB::bind_method(D_METHOD("warp_mouse", "position"), &Control::warp_mouse);
  3126. ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &Control::set_shortcut_context);
  3127. ClassDB::bind_method(D_METHOD("get_shortcut_context"), &Control::get_shortcut_context);
  3128. ClassDB::bind_method(D_METHOD("update_minimum_size"), &Control::update_minimum_size);
  3129. ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Control::set_layout_direction);
  3130. ClassDB::bind_method(D_METHOD("get_layout_direction"), &Control::get_layout_direction);
  3131. ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Control::is_layout_rtl);
  3132. #ifndef DISABLE_DEPRECATED
  3133. ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Control::set_auto_translate);
  3134. ClassDB::bind_method(D_METHOD("is_auto_translating"), &Control::is_auto_translating);
  3135. #endif // DISABLE_DEPRECATED
  3136. ClassDB::bind_method(D_METHOD("set_localize_numeral_system", "enable"), &Control::set_localize_numeral_system);
  3137. ClassDB::bind_method(D_METHOD("is_localizing_numeral_system"), &Control::is_localizing_numeral_system);
  3138. ADD_GROUP("Layout", "");
  3139. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_contents"), "set_clip_contents", "is_clipping_contents");
  3140. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "custom_minimum_size", PROPERTY_HINT_NONE, "suffix:px"), "set_custom_minimum_size", "get_custom_minimum_size");
  3141. 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");
  3142. 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");
  3143. ADD_PROPERTY_DEFAULT("layout_mode", LayoutMode::LAYOUT_MODE_POSITION);
  3144. const String anchors_presets_options = "Custom:-1,PresetFullRect:15,"
  3145. "PresetTopLeft:0,PresetTopRight:1,PresetBottomRight:3,PresetBottomLeft:2,"
  3146. "PresetCenterLeft:4,PresetCenterTop:5,PresetCenterRight:6,PresetCenterBottom:7,PresetCenter:8,"
  3147. "PresetLeftWide:9,PresetTopWide:10,PresetRightWide:11,PresetBottomWide:12,PresetVCenterWide:13,PresetHCenterWide:14";
  3148. 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");
  3149. ADD_PROPERTY_DEFAULT("anchors_preset", -1);
  3150. ADD_SUBGROUP_INDENT("Anchor Points", "anchor_", 1);
  3151. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_left", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_LEFT);
  3152. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_top", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_TOP);
  3153. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_right", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_RIGHT);
  3154. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anchor_bottom", PROPERTY_HINT_RANGE, "0,1,0.001,or_less,or_greater"), "_set_anchor", "get_anchor", SIDE_BOTTOM);
  3155. ADD_SUBGROUP_INDENT("Anchor Offsets", "offset_", 1);
  3156. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_left", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_LEFT);
  3157. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_top", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_TOP);
  3158. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_right", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_RIGHT);
  3159. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "offset_bottom", PROPERTY_HINT_RANGE, "-4096,4096,1,or_less,or_greater,suffix:px"), "set_offset", "get_offset", SIDE_BOTTOM);
  3160. ADD_SUBGROUP_INDENT("Grow Direction", "grow_", 1);
  3161. ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_horizontal", PROPERTY_HINT_ENUM, "Left,Right,Both"), "set_h_grow_direction", "get_h_grow_direction");
  3162. ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_vertical", PROPERTY_HINT_ENUM, "Top,Bottom,Both"), "set_v_grow_direction", "get_v_grow_direction");
  3163. ADD_SUBGROUP("Transform", "");
  3164. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "_set_size", "get_size");
  3165. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_EDITOR), "_set_position", "get_position");
  3166. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "global_position", PROPERTY_HINT_NONE, "suffix:px", PROPERTY_USAGE_NONE), "_set_global_position", "get_global_position");
  3167. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_less,or_greater,radians_as_degrees"), "set_rotation", "get_rotation");
  3168. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rotation_degrees", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_rotation_degrees", "get_rotation_degrees");
  3169. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scale"), "set_scale", "get_scale");
  3170. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "pivot_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_pivot_offset", "get_pivot_offset");
  3171. ADD_SUBGROUP("Container Sizing", "size_flags_");
  3172. 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");
  3173. 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");
  3174. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "0,20,0.01,or_greater"), "set_stretch_ratio", "get_stretch_ratio");
  3175. ADD_GROUP("Localization", "");
  3176. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "localize_numeral_system"), "set_localize_numeral_system", "is_localizing_numeral_system");
  3177. #ifndef DISABLE_DEPRECATED
  3178. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_auto_translate", "is_auto_translating");
  3179. #endif // DISABLE_DEPRECATED
  3180. ADD_GROUP("Tooltip", "tooltip_");
  3181. ADD_PROPERTY(PropertyInfo(Variant::STRING, "tooltip_text", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip_text", "get_tooltip_text");
  3182. 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");
  3183. ADD_GROUP("Focus", "focus_");
  3184. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_left", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_LEFT);
  3185. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_top", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_TOP);
  3186. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_right", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_RIGHT);
  3187. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbor_bottom", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbor", "get_focus_neighbor", SIDE_BOTTOM);
  3188. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_next", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_next", "get_focus_next");
  3189. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_previous", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_previous", "get_focus_previous");
  3190. ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode");
  3191. ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_recursive_behavior", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_focus_recursive_behavior", "get_focus_recursive_behavior");
  3192. ADD_GROUP("Mouse", "mouse_");
  3193. ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_filter", PROPERTY_HINT_ENUM, "Stop,Pass (Propagate Up),Ignore"), "set_mouse_filter", "get_mouse_filter");
  3194. ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_recursive_behavior", PROPERTY_HINT_ENUM, "Inherited,Disabled,Enabled"), "set_mouse_recursive_behavior", "get_mouse_recursive_behavior");
  3195. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mouse_force_pass_scroll_events"), "set_force_pass_scroll_events", "is_force_pass_scroll_events");
  3196. 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");
  3197. ADD_GROUP("Input", "");
  3198. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_NODE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
  3199. ADD_GROUP("Theme", "theme_");
  3200. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  3201. ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
  3202. BIND_ENUM_CONSTANT(FOCUS_NONE);
  3203. BIND_ENUM_CONSTANT(FOCUS_CLICK);
  3204. BIND_ENUM_CONSTANT(FOCUS_ALL);
  3205. BIND_ENUM_CONSTANT(RECURSIVE_BEHAVIOR_INHERITED);
  3206. BIND_ENUM_CONSTANT(RECURSIVE_BEHAVIOR_DISABLED);
  3207. BIND_ENUM_CONSTANT(RECURSIVE_BEHAVIOR_ENABLED);
  3208. BIND_CONSTANT(NOTIFICATION_RESIZED);
  3209. BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER);
  3210. BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT);
  3211. BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER_SELF);
  3212. BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT_SELF);
  3213. BIND_CONSTANT(NOTIFICATION_FOCUS_ENTER);
  3214. BIND_CONSTANT(NOTIFICATION_FOCUS_EXIT);
  3215. BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
  3216. BIND_CONSTANT(NOTIFICATION_SCROLL_BEGIN);
  3217. BIND_CONSTANT(NOTIFICATION_SCROLL_END);
  3218. BIND_CONSTANT(NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  3219. BIND_ENUM_CONSTANT(CURSOR_ARROW);
  3220. BIND_ENUM_CONSTANT(CURSOR_IBEAM);
  3221. BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
  3222. BIND_ENUM_CONSTANT(CURSOR_CROSS);
  3223. BIND_ENUM_CONSTANT(CURSOR_WAIT);
  3224. BIND_ENUM_CONSTANT(CURSOR_BUSY);
  3225. BIND_ENUM_CONSTANT(CURSOR_DRAG);
  3226. BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
  3227. BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
  3228. BIND_ENUM_CONSTANT(CURSOR_VSIZE);
  3229. BIND_ENUM_CONSTANT(CURSOR_HSIZE);
  3230. BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
  3231. BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
  3232. BIND_ENUM_CONSTANT(CURSOR_MOVE);
  3233. BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
  3234. BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
  3235. BIND_ENUM_CONSTANT(CURSOR_HELP);
  3236. BIND_ENUM_CONSTANT(PRESET_TOP_LEFT);
  3237. BIND_ENUM_CONSTANT(PRESET_TOP_RIGHT);
  3238. BIND_ENUM_CONSTANT(PRESET_BOTTOM_LEFT);
  3239. BIND_ENUM_CONSTANT(PRESET_BOTTOM_RIGHT);
  3240. BIND_ENUM_CONSTANT(PRESET_CENTER_LEFT);
  3241. BIND_ENUM_CONSTANT(PRESET_CENTER_TOP);
  3242. BIND_ENUM_CONSTANT(PRESET_CENTER_RIGHT);
  3243. BIND_ENUM_CONSTANT(PRESET_CENTER_BOTTOM);
  3244. BIND_ENUM_CONSTANT(PRESET_CENTER);
  3245. BIND_ENUM_CONSTANT(PRESET_LEFT_WIDE);
  3246. BIND_ENUM_CONSTANT(PRESET_TOP_WIDE);
  3247. BIND_ENUM_CONSTANT(PRESET_RIGHT_WIDE);
  3248. BIND_ENUM_CONSTANT(PRESET_BOTTOM_WIDE);
  3249. BIND_ENUM_CONSTANT(PRESET_VCENTER_WIDE);
  3250. BIND_ENUM_CONSTANT(PRESET_HCENTER_WIDE);
  3251. BIND_ENUM_CONSTANT(PRESET_FULL_RECT);
  3252. BIND_ENUM_CONSTANT(PRESET_MODE_MINSIZE);
  3253. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_WIDTH);
  3254. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_HEIGHT);
  3255. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_SIZE);
  3256. BIND_BITFIELD_FLAG(SIZE_SHRINK_BEGIN);
  3257. BIND_BITFIELD_FLAG(SIZE_FILL);
  3258. BIND_BITFIELD_FLAG(SIZE_EXPAND);
  3259. BIND_BITFIELD_FLAG(SIZE_EXPAND_FILL);
  3260. BIND_BITFIELD_FLAG(SIZE_SHRINK_CENTER);
  3261. BIND_BITFIELD_FLAG(SIZE_SHRINK_END);
  3262. BIND_ENUM_CONSTANT(MOUSE_FILTER_STOP);
  3263. BIND_ENUM_CONSTANT(MOUSE_FILTER_PASS);
  3264. BIND_ENUM_CONSTANT(MOUSE_FILTER_IGNORE);
  3265. BIND_ENUM_CONSTANT(GROW_DIRECTION_BEGIN);
  3266. BIND_ENUM_CONSTANT(GROW_DIRECTION_END);
  3267. BIND_ENUM_CONSTANT(GROW_DIRECTION_BOTH);
  3268. BIND_ENUM_CONSTANT(ANCHOR_BEGIN);
  3269. BIND_ENUM_CONSTANT(ANCHOR_END);
  3270. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
  3271. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_APPLICATION_LOCALE);
  3272. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
  3273. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
  3274. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_SYSTEM_LOCALE);
  3275. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_MAX);
  3276. #ifndef DISABLE_DEPRECATED
  3277. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
  3278. #endif // DISABLE_DEPRECATED
  3279. BIND_ENUM_CONSTANT(TEXT_DIRECTION_INHERITED);
  3280. BIND_ENUM_CONSTANT(TEXT_DIRECTION_AUTO);
  3281. BIND_ENUM_CONSTANT(TEXT_DIRECTION_LTR);
  3282. BIND_ENUM_CONSTANT(TEXT_DIRECTION_RTL);
  3283. ADD_SIGNAL(MethodInfo("resized"));
  3284. ADD_SIGNAL(MethodInfo("gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  3285. ADD_SIGNAL(MethodInfo("mouse_entered"));
  3286. ADD_SIGNAL(MethodInfo("mouse_exited"));
  3287. ADD_SIGNAL(MethodInfo("focus_entered"));
  3288. ADD_SIGNAL(MethodInfo("focus_exited"));
  3289. ADD_SIGNAL(MethodInfo("size_flags_changed"));
  3290. ADD_SIGNAL(MethodInfo("minimum_size_changed"));
  3291. ADD_SIGNAL(MethodInfo("theme_changed"));
  3292. GDVIRTUAL_BIND(_has_point, "point");
  3293. GDVIRTUAL_BIND(_structured_text_parser, "args", "text");
  3294. GDVIRTUAL_BIND(_get_minimum_size);
  3295. GDVIRTUAL_BIND(_get_tooltip, "at_position");
  3296. GDVIRTUAL_BIND(_get_drag_data, "at_position");
  3297. GDVIRTUAL_BIND(_can_drop_data, "at_position", "data");
  3298. GDVIRTUAL_BIND(_drop_data, "at_position", "data");
  3299. GDVIRTUAL_BIND(_make_custom_tooltip, "for_text");
  3300. GDVIRTUAL_BIND(_gui_input, "event");
  3301. }
  3302. Control::Control() {
  3303. data.theme_owner = memnew(ThemeOwner(this));
  3304. set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
  3305. }
  3306. Control::~Control() {
  3307. memdelete(data.theme_owner);
  3308. // Resources need to be disconnected.
  3309. for (KeyValue<StringName, Ref<Texture2D>> &E : data.theme_icon_override) {
  3310. E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  3311. }
  3312. for (KeyValue<StringName, Ref<StyleBox>> &E : data.theme_style_override) {
  3313. E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  3314. }
  3315. for (KeyValue<StringName, Ref<Font>> &E : data.theme_font_override) {
  3316. E.value->disconnect_changed(callable_mp(this, &Control::_notify_theme_override_changed));
  3317. }
  3318. // Then override maps can be simply cleared.
  3319. data.theme_icon_override.clear();
  3320. data.theme_style_override.clear();
  3321. data.theme_font_override.clear();
  3322. data.theme_font_size_override.clear();
  3323. data.theme_color_override.clear();
  3324. data.theme_constant_override.clear();
  3325. }