control.cpp 117 KB

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