control.cpp 126 KB

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