control.cpp 127 KB

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