control.cpp 115 KB

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