control.cpp 125 KB

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