control.cpp 115 KB

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