control.cpp 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052
  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 "core/message_queue.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include "core/project_settings.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/panel.h"
  38. #include "scene/main/canvas_layer.h"
  39. #include "scene/main/viewport.h"
  40. #include "scene/scene_string_names.h"
  41. #include "servers/visual_server.h"
  42. #ifdef TOOLS_ENABLED
  43. #include "editor/editor_settings.h"
  44. #include "editor/plugins/canvas_item_editor_plugin.h"
  45. #endif
  46. #ifdef TOOLS_ENABLED
  47. Dictionary Control::_edit_get_state() const {
  48. Dictionary s;
  49. s["rotation"] = get_rotation();
  50. s["scale"] = get_scale();
  51. s["pivot"] = get_pivot_offset();
  52. Array anchors;
  53. anchors.push_back(get_anchor(MARGIN_LEFT));
  54. anchors.push_back(get_anchor(MARGIN_TOP));
  55. anchors.push_back(get_anchor(MARGIN_RIGHT));
  56. anchors.push_back(get_anchor(MARGIN_BOTTOM));
  57. s["anchors"] = anchors;
  58. Array margins;
  59. margins.push_back(get_margin(MARGIN_LEFT));
  60. margins.push_back(get_margin(MARGIN_TOP));
  61. margins.push_back(get_margin(MARGIN_RIGHT));
  62. margins.push_back(get_margin(MARGIN_BOTTOM));
  63. s["margins"] = margins;
  64. return s;
  65. }
  66. void Control::_edit_set_state(const Dictionary &p_state) {
  67. ERR_FAIL_COND((p_state.size() <= 0) ||
  68. !p_state.has("rotation") || !p_state.has("scale") ||
  69. !p_state.has("pivot") || !p_state.has("anchors") || !p_state.has("margins"));
  70. Dictionary state = p_state;
  71. set_rotation(state["rotation"]);
  72. set_scale(state["scale"]);
  73. set_pivot_offset(state["pivot"]);
  74. Array anchors = state["anchors"];
  75. data.anchor[MARGIN_LEFT] = anchors[0];
  76. data.anchor[MARGIN_TOP] = anchors[1];
  77. data.anchor[MARGIN_RIGHT] = anchors[2];
  78. data.anchor[MARGIN_BOTTOM] = anchors[3];
  79. Array margins = state["margins"];
  80. data.margin[MARGIN_LEFT] = margins[0];
  81. data.margin[MARGIN_TOP] = margins[1];
  82. data.margin[MARGIN_RIGHT] = margins[2];
  83. data.margin[MARGIN_BOTTOM] = margins[3];
  84. _size_changed();
  85. _change_notify("anchor_left");
  86. _change_notify("anchor_right");
  87. _change_notify("anchor_top");
  88. _change_notify("anchor_bottom");
  89. }
  90. void Control::_edit_set_position(const Point2 &p_position) {
  91. #ifdef TOOLS_ENABLED
  92. ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
  93. set_position(p_position, CanvasItemEditor::get_singleton()->is_anchors_mode_enabled() && Object::cast_to<Control>(data.parent));
  94. #else
  95. // Unlikely to happen. TODO: enclose all _edit_ functions into TOOLS_ENABLED
  96. set_position(p_position);
  97. #endif
  98. }
  99. Point2 Control::_edit_get_position() const {
  100. return get_position();
  101. }
  102. void Control::_edit_set_scale(const Size2 &p_scale) {
  103. set_scale(p_scale);
  104. }
  105. Size2 Control::_edit_get_scale() const {
  106. return data.scale;
  107. }
  108. void Control::_edit_set_rect(const Rect2 &p_edit_rect) {
  109. #ifdef TOOLS_ENABLED
  110. ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
  111. set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)).snapped(Vector2(1, 1)), CanvasItemEditor::get_singleton()->is_anchors_mode_enabled());
  112. set_size(p_edit_rect.size.snapped(Vector2(1, 1)), CanvasItemEditor::get_singleton()->is_anchors_mode_enabled());
  113. #else
  114. // Unlikely to happen. TODO: enclose all _edit_ functions into TOOLS_ENABLED
  115. set_position((get_position() + get_transform().basis_xform(p_edit_rect.position)).snapped(Vector2(1, 1)));
  116. set_size(p_edit_rect.size.snapped(Vector2(1, 1)));
  117. #endif
  118. }
  119. Rect2 Control::_edit_get_rect() const {
  120. return Rect2(Point2(), get_size());
  121. }
  122. bool Control::_edit_use_rect() const {
  123. return true;
  124. }
  125. void Control::_edit_set_rotation(float p_rotation) {
  126. set_rotation(p_rotation);
  127. }
  128. float Control::_edit_get_rotation() const {
  129. return get_rotation();
  130. }
  131. bool Control::_edit_use_rotation() const {
  132. return true;
  133. }
  134. void Control::_edit_set_pivot(const Point2 &p_pivot) {
  135. Vector2 delta_pivot = p_pivot - get_pivot_offset();
  136. 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);
  137. set_position(get_position() + move);
  138. set_pivot_offset(p_pivot);
  139. }
  140. Point2 Control::_edit_get_pivot() const {
  141. return get_pivot_offset();
  142. }
  143. bool Control::_edit_use_pivot() const {
  144. return true;
  145. }
  146. Size2 Control::_edit_get_minimum_size() const {
  147. return get_combined_minimum_size();
  148. }
  149. #endif
  150. void Control::set_custom_minimum_size(const Size2 &p_custom) {
  151. if (p_custom == data.custom_minimum_size) {
  152. return;
  153. }
  154. data.custom_minimum_size = p_custom;
  155. minimum_size_changed();
  156. }
  157. Size2 Control::get_custom_minimum_size() const {
  158. return data.custom_minimum_size;
  159. }
  160. void Control::_update_minimum_size_cache() {
  161. Size2 minsize = get_minimum_size();
  162. minsize.x = MAX(minsize.x, data.custom_minimum_size.x);
  163. minsize.y = MAX(minsize.y, data.custom_minimum_size.y);
  164. bool size_changed = false;
  165. if (data.minimum_size_cache != minsize) {
  166. size_changed = true;
  167. }
  168. data.minimum_size_cache = minsize;
  169. data.minimum_size_valid = true;
  170. if (size_changed) {
  171. minimum_size_changed();
  172. }
  173. }
  174. Size2 Control::get_combined_minimum_size() const {
  175. if (!data.minimum_size_valid) {
  176. const_cast<Control *>(this)->_update_minimum_size_cache();
  177. }
  178. return data.minimum_size_cache;
  179. }
  180. Transform2D Control::_get_internal_transform() const {
  181. Transform2D rot_scale;
  182. rot_scale.set_rotation_and_scale(data.rotation, data.scale);
  183. Transform2D offset;
  184. offset.set_origin(-data.pivot_offset);
  185. return offset.affine_inverse() * (rot_scale * offset);
  186. }
  187. bool Control::_set(const StringName &p_name, const Variant &p_value) {
  188. String name = p_name;
  189. if (!name.begins_with("custom")) {
  190. return false;
  191. }
  192. if (p_value.get_type() == Variant::NIL) {
  193. if (name.begins_with("custom_icons/")) {
  194. String dname = name.get_slicec('/', 1);
  195. if (data.icon_override.has(dname)) {
  196. data.icon_override[dname]->disconnect("changed", this, "_override_changed");
  197. }
  198. data.icon_override.erase(dname);
  199. notification(NOTIFICATION_THEME_CHANGED);
  200. } else if (name.begins_with("custom_shaders/")) {
  201. String dname = name.get_slicec('/', 1);
  202. if (data.shader_override.has(dname)) {
  203. data.shader_override[dname]->disconnect("changed", this, "_override_changed");
  204. }
  205. data.shader_override.erase(dname);
  206. notification(NOTIFICATION_THEME_CHANGED);
  207. } else if (name.begins_with("custom_styles/")) {
  208. String dname = name.get_slicec('/', 1);
  209. if (data.style_override.has(dname)) {
  210. data.style_override[dname]->disconnect("changed", this, "_override_changed");
  211. }
  212. data.style_override.erase(dname);
  213. notification(NOTIFICATION_THEME_CHANGED);
  214. } else if (name.begins_with("custom_fonts/")) {
  215. String dname = name.get_slicec('/', 1);
  216. if (data.font_override.has(dname)) {
  217. data.font_override[dname]->disconnect("changed", this, "_override_changed");
  218. }
  219. data.font_override.erase(dname);
  220. notification(NOTIFICATION_THEME_CHANGED);
  221. } else if (name.begins_with("custom_colors/")) {
  222. String dname = name.get_slicec('/', 1);
  223. data.color_override.erase(dname);
  224. notification(NOTIFICATION_THEME_CHANGED);
  225. } else if (name.begins_with("custom_constants/")) {
  226. String dname = name.get_slicec('/', 1);
  227. data.constant_override.erase(dname);
  228. notification(NOTIFICATION_THEME_CHANGED);
  229. } else {
  230. return false;
  231. }
  232. } else {
  233. if (name.begins_with("custom_icons/")) {
  234. String dname = name.get_slicec('/', 1);
  235. add_icon_override(dname, p_value);
  236. } else if (name.begins_with("custom_shaders/")) {
  237. String dname = name.get_slicec('/', 1);
  238. add_shader_override(dname, p_value);
  239. } else if (name.begins_with("custom_styles/")) {
  240. String dname = name.get_slicec('/', 1);
  241. add_style_override(dname, p_value);
  242. } else if (name.begins_with("custom_fonts/")) {
  243. String dname = name.get_slicec('/', 1);
  244. add_font_override(dname, p_value);
  245. } else if (name.begins_with("custom_colors/")) {
  246. String dname = name.get_slicec('/', 1);
  247. add_color_override(dname, p_value);
  248. } else if (name.begins_with("custom_constants/")) {
  249. String dname = name.get_slicec('/', 1);
  250. add_constant_override(dname, p_value);
  251. } else {
  252. return false;
  253. }
  254. }
  255. return true;
  256. }
  257. void Control::_update_minimum_size() {
  258. if (!is_inside_tree()) {
  259. return;
  260. }
  261. Size2 minsize = get_combined_minimum_size();
  262. data.updating_last_minimum_size = false;
  263. if (minsize != data.last_minimum_size) {
  264. data.last_minimum_size = minsize;
  265. _size_changed();
  266. emit_signal(SceneStringNames::get_singleton()->minimum_size_changed);
  267. }
  268. }
  269. bool Control::_get(const StringName &p_name, Variant &r_ret) const {
  270. String sname = p_name;
  271. if (!sname.begins_with("custom")) {
  272. return false;
  273. }
  274. if (sname.begins_with("custom_icons/")) {
  275. String name = sname.get_slicec('/', 1);
  276. r_ret = data.icon_override.has(name) ? Variant(data.icon_override[name]) : Variant();
  277. } else if (sname.begins_with("custom_shaders/")) {
  278. String name = sname.get_slicec('/', 1);
  279. r_ret = data.shader_override.has(name) ? Variant(data.shader_override[name]) : Variant();
  280. } else if (sname.begins_with("custom_styles/")) {
  281. String name = sname.get_slicec('/', 1);
  282. r_ret = data.style_override.has(name) ? Variant(data.style_override[name]) : Variant();
  283. } else if (sname.begins_with("custom_fonts/")) {
  284. String name = sname.get_slicec('/', 1);
  285. r_ret = data.font_override.has(name) ? Variant(data.font_override[name]) : Variant();
  286. } else if (sname.begins_with("custom_colors/")) {
  287. String name = sname.get_slicec('/', 1);
  288. r_ret = data.color_override.has(name) ? Variant(data.color_override[name]) : Variant();
  289. } else if (sname.begins_with("custom_constants/")) {
  290. String name = sname.get_slicec('/', 1);
  291. r_ret = data.constant_override.has(name) ? Variant(data.constant_override[name]) : Variant();
  292. } else {
  293. return false;
  294. }
  295. return true;
  296. }
  297. void Control::_get_property_list(List<PropertyInfo> *p_list) const {
  298. Ref<Theme> theme = Theme::get_default();
  299. p_list->push_back(PropertyInfo(Variant::NIL, "Theme Overrides", PROPERTY_HINT_NONE, "custom_", PROPERTY_USAGE_GROUP));
  300. {
  301. List<StringName> names;
  302. theme->get_color_list(get_class_name(), &names);
  303. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  304. uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  305. if (data.color_override.has(E->get())) {
  306. hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  307. }
  308. p_list->push_back(PropertyInfo(Variant::COLOR, "custom_colors/" + E->get(), PROPERTY_HINT_NONE, "", hint));
  309. }
  310. }
  311. {
  312. List<StringName> names;
  313. theme->get_constant_list(get_class_name(), &names);
  314. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  315. uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  316. if (data.constant_override.has(E->get())) {
  317. hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  318. }
  319. p_list->push_back(PropertyInfo(Variant::INT, "custom_constants/" + E->get(), PROPERTY_HINT_RANGE, "-16384,16384", hint));
  320. }
  321. }
  322. {
  323. List<StringName> names;
  324. theme->get_font_list(get_class_name(), &names);
  325. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  326. uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  327. if (data.font_override.has(E->get())) {
  328. hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  329. }
  330. p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_fonts/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Font", hint));
  331. }
  332. }
  333. {
  334. List<StringName> names;
  335. theme->get_icon_list(get_class_name(), &names);
  336. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  337. uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  338. if (data.icon_override.has(E->get())) {
  339. hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  340. }
  341. p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_icons/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Texture", hint));
  342. }
  343. }
  344. {
  345. List<StringName> names;
  346. theme->get_shader_list(get_class_name(), &names);
  347. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  348. uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  349. if (data.shader_override.has(E->get())) {
  350. hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  351. }
  352. p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_shaders/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "Shader,VisualShader", hint));
  353. }
  354. }
  355. {
  356. List<StringName> names;
  357. theme->get_stylebox_list(get_class_name(), &names);
  358. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  359. uint32_t hint = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  360. if (data.style_override.has(E->get())) {
  361. hint |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  362. }
  363. p_list->push_back(PropertyInfo(Variant::OBJECT, "custom_styles/" + E->get(), PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", hint));
  364. }
  365. }
  366. }
  367. Control *Control::get_parent_control() const {
  368. return data.parent;
  369. }
  370. void Control::_resize(const Size2 &p_size) {
  371. _size_changed();
  372. }
  373. //moved theme configuration here, so controls can set up even if still not inside active scene
  374. void Control::add_child_notify(Node *p_child) {
  375. Control *child_c = Object::cast_to<Control>(p_child);
  376. if (!child_c) {
  377. return;
  378. }
  379. if (child_c->data.theme.is_null() && data.theme_owner) {
  380. _propagate_theme_changed(child_c, data.theme_owner); //need to propagate here, since many controls may require setting up stuff
  381. }
  382. }
  383. void Control::remove_child_notify(Node *p_child) {
  384. Control *child_c = Object::cast_to<Control>(p_child);
  385. if (!child_c) {
  386. return;
  387. }
  388. if (child_c->data.theme_owner && child_c->data.theme.is_null()) {
  389. _propagate_theme_changed(child_c, nullptr);
  390. }
  391. }
  392. void Control::_update_canvas_item_transform() {
  393. Transform2D xform = _get_internal_transform();
  394. xform[2] += get_position();
  395. // We use a little workaround to avoid flickering when moving the pivot with _edit_set_pivot()
  396. if (is_inside_tree() && Math::abs(Math::sin(data.rotation * 4.0f)) < 0.00001f && get_viewport()->is_snap_controls_to_pixels_enabled()) {
  397. xform[2] = xform[2].round();
  398. }
  399. VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), xform);
  400. }
  401. void Control::_notification(int p_notification) {
  402. switch (p_notification) {
  403. case NOTIFICATION_ENTER_TREE: {
  404. } break;
  405. case NOTIFICATION_POST_ENTER_TREE: {
  406. data.minimum_size_valid = false;
  407. _size_changed();
  408. } break;
  409. case NOTIFICATION_EXIT_TREE: {
  410. ERR_FAIL_COND(!get_viewport());
  411. release_focus();
  412. get_viewport()->_gui_remove_control(this);
  413. } break;
  414. case NOTIFICATION_ENTER_CANVAS: {
  415. data.parent = Object::cast_to<Control>(get_parent());
  416. if (is_set_as_toplevel()) {
  417. data.SI = get_viewport()->_gui_add_subwindow_control(this);
  418. if (data.theme.is_null() && data.parent && data.parent->data.theme_owner) {
  419. data.theme_owner = data.parent->data.theme_owner;
  420. notification(NOTIFICATION_THEME_CHANGED);
  421. }
  422. } else {
  423. Node *parent = this; //meh
  424. Control *parent_control = nullptr;
  425. bool subwindow = false;
  426. while (parent) {
  427. parent = parent->get_parent();
  428. if (!parent) {
  429. break;
  430. }
  431. CanvasItem *ci = Object::cast_to<CanvasItem>(parent);
  432. if (ci && ci->is_set_as_toplevel()) {
  433. subwindow = true;
  434. break;
  435. }
  436. parent_control = Object::cast_to<Control>(parent);
  437. if (parent_control) {
  438. break;
  439. } else if (ci) {
  440. } else {
  441. break;
  442. }
  443. }
  444. if (parent_control) {
  445. //do nothing, has a parent control
  446. if (data.theme.is_null() && parent_control->data.theme_owner) {
  447. data.theme_owner = parent_control->data.theme_owner;
  448. notification(NOTIFICATION_THEME_CHANGED);
  449. }
  450. } else if (subwindow) {
  451. //is a subwindow (process input before other controls for that canvas)
  452. data.SI = get_viewport()->_gui_add_subwindow_control(this);
  453. } else {
  454. //is a regular root control
  455. Viewport *viewport = get_viewport();
  456. ERR_FAIL_COND(!viewport);
  457. data.RI = viewport->_gui_add_root_control(this);
  458. }
  459. data.parent_canvas_item = get_parent_item();
  460. if (data.parent_canvas_item) {
  461. data.parent_canvas_item->connect("item_rect_changed", this, "_size_changed");
  462. } else {
  463. //connect viewport
  464. Viewport *viewport = get_viewport();
  465. ERR_FAIL_COND(!viewport);
  466. viewport->connect("size_changed", this, "_size_changed");
  467. }
  468. }
  469. } break;
  470. case NOTIFICATION_EXIT_CANVAS: {
  471. if (data.parent_canvas_item) {
  472. data.parent_canvas_item->disconnect("item_rect_changed", this, "_size_changed");
  473. data.parent_canvas_item = nullptr;
  474. } else if (!is_set_as_toplevel()) {
  475. //disconnect viewport
  476. Viewport *viewport = get_viewport();
  477. ERR_FAIL_COND(!viewport);
  478. viewport->disconnect("size_changed", this, "_size_changed");
  479. }
  480. if (data.MI) {
  481. get_viewport()->_gui_remove_modal_control(data.MI);
  482. data.MI = nullptr;
  483. }
  484. if (data.SI) {
  485. get_viewport()->_gui_remove_subwindow_control(data.SI);
  486. data.SI = nullptr;
  487. }
  488. if (data.RI) {
  489. get_viewport()->_gui_remove_root_control(data.RI);
  490. data.RI = nullptr;
  491. }
  492. data.parent = nullptr;
  493. data.parent_canvas_item = nullptr;
  494. /*
  495. if (data.theme_owner && data.theme.is_null()) {
  496. data.theme_owner=NULL;
  497. notification(NOTIFICATION_THEME_CHANGED);
  498. }
  499. */
  500. } break;
  501. case NOTIFICATION_MOVED_IN_PARENT: {
  502. // some parents need to know the order of the children to draw (like TabContainer)
  503. // update if necessary
  504. if (data.parent) {
  505. data.parent->update();
  506. }
  507. update();
  508. if (data.SI) {
  509. get_viewport()->_gui_set_subwindow_order_dirty();
  510. }
  511. if (data.RI) {
  512. get_viewport()->_gui_set_root_order_dirty();
  513. }
  514. } break;
  515. case NOTIFICATION_RESIZED: {
  516. emit_signal(SceneStringNames::get_singleton()->resized);
  517. } break;
  518. case NOTIFICATION_DRAW: {
  519. _update_canvas_item_transform();
  520. VisualServer::get_singleton()->canvas_item_set_custom_rect(get_canvas_item(), !data.disable_visibility_clip, Rect2(Point2(), get_size()));
  521. VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), data.clip_contents);
  522. //emit_signal(SceneStringNames::get_singleton()->draw);
  523. } break;
  524. case NOTIFICATION_MOUSE_ENTER: {
  525. emit_signal(SceneStringNames::get_singleton()->mouse_entered);
  526. } break;
  527. case NOTIFICATION_MOUSE_EXIT: {
  528. emit_signal(SceneStringNames::get_singleton()->mouse_exited);
  529. } break;
  530. case NOTIFICATION_FOCUS_ENTER: {
  531. emit_signal(SceneStringNames::get_singleton()->focus_entered);
  532. update();
  533. } break;
  534. case NOTIFICATION_FOCUS_EXIT: {
  535. emit_signal(SceneStringNames::get_singleton()->focus_exited);
  536. update();
  537. } break;
  538. case NOTIFICATION_THEME_CHANGED: {
  539. minimum_size_changed();
  540. update();
  541. } break;
  542. case NOTIFICATION_MODAL_CLOSE: {
  543. emit_signal("modal_closed");
  544. } break;
  545. case NOTIFICATION_VISIBILITY_CHANGED: {
  546. if (!is_visible_in_tree()) {
  547. if (get_viewport() != nullptr) {
  548. get_viewport()->_gui_hid_control(this);
  549. }
  550. if (is_inside_tree()) {
  551. _modal_stack_remove();
  552. }
  553. //remove key focus
  554. //remove modalness
  555. } else {
  556. data.minimum_size_valid = false;
  557. _size_changed();
  558. }
  559. } break;
  560. case SceneTree::NOTIFICATION_WM_UNFOCUS_REQUEST: {
  561. get_viewport()->_gui_unfocus_control(this);
  562. } break;
  563. }
  564. }
  565. bool Control::clips_input() const {
  566. if (get_script_instance()) {
  567. return get_script_instance()->call(SceneStringNames::get_singleton()->_clips_input);
  568. }
  569. return false;
  570. }
  571. bool Control::has_point(const Point2 &p_point) const {
  572. if (get_script_instance()) {
  573. Variant v = p_point;
  574. const Variant *p = &v;
  575. Variant::CallError ce;
  576. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->has_point, &p, 1, ce);
  577. if (ce.error == Variant::CallError::CALL_OK) {
  578. return ret;
  579. }
  580. }
  581. return Rect2(Point2(), get_size()).has_point(p_point);
  582. }
  583. void Control::set_drag_forwarding(Control *p_target) {
  584. if (p_target) {
  585. data.drag_owner = p_target->get_instance_id();
  586. } else {
  587. data.drag_owner = 0;
  588. }
  589. }
  590. Variant Control::get_drag_data(const Point2 &p_point) {
  591. if (data.drag_owner) {
  592. Object *obj = ObjectDB::get_instance(data.drag_owner);
  593. if (obj) {
  594. Control *c = Object::cast_to<Control>(obj);
  595. return c->call("get_drag_data_fw", p_point, this);
  596. }
  597. }
  598. if (get_script_instance()) {
  599. Variant v = p_point;
  600. const Variant *p = &v;
  601. Variant::CallError ce;
  602. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->get_drag_data, &p, 1, ce);
  603. if (ce.error == Variant::CallError::CALL_OK) {
  604. return ret;
  605. }
  606. }
  607. return Variant();
  608. }
  609. bool Control::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  610. if (data.drag_owner) {
  611. Object *obj = ObjectDB::get_instance(data.drag_owner);
  612. if (obj) {
  613. Control *c = Object::cast_to<Control>(obj);
  614. return c->call("can_drop_data_fw", p_point, p_data, this);
  615. }
  616. }
  617. if (get_script_instance()) {
  618. Variant v = p_point;
  619. const Variant *p[2] = { &v, &p_data };
  620. Variant::CallError ce;
  621. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->can_drop_data, p, 2, ce);
  622. if (ce.error == Variant::CallError::CALL_OK) {
  623. return ret;
  624. }
  625. }
  626. return false;
  627. }
  628. void Control::drop_data(const Point2 &p_point, const Variant &p_data) {
  629. if (data.drag_owner) {
  630. Object *obj = ObjectDB::get_instance(data.drag_owner);
  631. if (obj) {
  632. Control *c = Object::cast_to<Control>(obj);
  633. c->call("drop_data_fw", p_point, p_data, this);
  634. return;
  635. }
  636. }
  637. if (get_script_instance()) {
  638. Variant v = p_point;
  639. const Variant *p[2] = { &v, &p_data };
  640. Variant::CallError ce;
  641. Variant ret = get_script_instance()->call(SceneStringNames::get_singleton()->drop_data, p, 2, ce);
  642. if (ce.error == Variant::CallError::CALL_OK) {
  643. return;
  644. }
  645. }
  646. }
  647. void Control::force_drag(const Variant &p_data, Control *p_control) {
  648. ERR_FAIL_COND(!is_inside_tree());
  649. ERR_FAIL_COND(p_data.get_type() == Variant::NIL);
  650. get_viewport()->_gui_force_drag(this, p_data, p_control);
  651. }
  652. void Control::set_drag_preview(Control *p_control) {
  653. ERR_FAIL_COND(!is_inside_tree());
  654. ERR_FAIL_COND(!get_viewport()->gui_is_dragging());
  655. get_viewport()->_gui_set_drag_preview(this, p_control);
  656. }
  657. bool Control::is_window_modal_on_top() const {
  658. if (!is_inside_tree()) {
  659. return false;
  660. }
  661. return get_viewport()->_gui_is_modal_on_top(this);
  662. }
  663. uint64_t Control::get_modal_frame() const {
  664. return data.modal_frame;
  665. }
  666. Size2 Control::get_minimum_size() const {
  667. ScriptInstance *si = const_cast<Control *>(this)->get_script_instance();
  668. if (si) {
  669. Variant::CallError ce;
  670. Variant s = si->call(SceneStringNames::get_singleton()->_get_minimum_size, nullptr, 0, ce);
  671. if (ce.error == Variant::CallError::CALL_OK) {
  672. return s;
  673. }
  674. }
  675. return Size2();
  676. }
  677. Ref<Texture> Control::get_icon(const StringName &p_name, const StringName &p_theme_type) const {
  678. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  679. const Ref<Texture> *tex = data.icon_override.getptr(p_name);
  680. if (tex) {
  681. return *tex;
  682. }
  683. }
  684. StringName type = p_theme_type ? p_theme_type : get_class_name();
  685. // try with custom themes
  686. Control *theme_owner = data.theme_owner;
  687. while (theme_owner) {
  688. StringName class_name = type;
  689. while (class_name != StringName()) {
  690. if (theme_owner->data.theme->has_icon(p_name, class_name)) {
  691. return theme_owner->data.theme->get_icon(p_name, class_name);
  692. }
  693. class_name = ClassDB::get_parent_class_nocheck(class_name);
  694. }
  695. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  696. if (parent) {
  697. theme_owner = parent->data.theme_owner;
  698. } else {
  699. theme_owner = nullptr;
  700. }
  701. }
  702. if (Theme::get_project_default().is_valid()) {
  703. if (Theme::get_project_default()->has_icon(p_name, type)) {
  704. return Theme::get_project_default()->get_icon(p_name, type);
  705. }
  706. }
  707. return Theme::get_default()->get_icon(p_name, type);
  708. }
  709. Ref<Shader> Control::get_shader(const StringName &p_name, const StringName &p_theme_type) const {
  710. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  711. const Ref<Shader> *sdr = data.shader_override.getptr(p_name);
  712. if (sdr) {
  713. return *sdr;
  714. }
  715. }
  716. StringName type = p_theme_type ? p_theme_type : get_class_name();
  717. // try with custom themes
  718. Control *theme_owner = data.theme_owner;
  719. while (theme_owner) {
  720. StringName class_name = type;
  721. while (class_name != StringName()) {
  722. if (theme_owner->data.theme->has_shader(p_name, class_name)) {
  723. return theme_owner->data.theme->get_shader(p_name, class_name);
  724. }
  725. class_name = ClassDB::get_parent_class_nocheck(class_name);
  726. }
  727. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  728. if (parent) {
  729. theme_owner = parent->data.theme_owner;
  730. } else {
  731. theme_owner = nullptr;
  732. }
  733. }
  734. if (Theme::get_project_default().is_valid()) {
  735. if (Theme::get_project_default()->has_shader(p_name, type)) {
  736. return Theme::get_project_default()->get_shader(p_name, type);
  737. }
  738. }
  739. return Theme::get_default()->get_shader(p_name, type);
  740. }
  741. Ref<StyleBox> Control::get_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  742. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  743. const Ref<StyleBox> *style = data.style_override.getptr(p_name);
  744. if (style) {
  745. return *style;
  746. }
  747. }
  748. StringName type = p_theme_type ? p_theme_type : get_class_name();
  749. // try with custom themes
  750. Control *theme_owner = data.theme_owner;
  751. StringName class_name = type;
  752. while (theme_owner) {
  753. while (class_name != StringName()) {
  754. if (theme_owner->data.theme->has_stylebox(p_name, class_name)) {
  755. return theme_owner->data.theme->get_stylebox(p_name, class_name);
  756. }
  757. class_name = ClassDB::get_parent_class_nocheck(class_name);
  758. }
  759. class_name = type;
  760. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  761. if (parent) {
  762. theme_owner = parent->data.theme_owner;
  763. } else {
  764. theme_owner = nullptr;
  765. }
  766. }
  767. while (class_name != StringName()) {
  768. if (Theme::get_project_default().is_valid() && Theme::get_project_default()->has_stylebox(p_name, type)) {
  769. return Theme::get_project_default()->get_stylebox(p_name, type);
  770. }
  771. if (Theme::get_default()->has_stylebox(p_name, class_name)) {
  772. return Theme::get_default()->get_stylebox(p_name, class_name);
  773. }
  774. class_name = ClassDB::get_parent_class_nocheck(class_name);
  775. }
  776. return Theme::get_default()->get_stylebox(p_name, type);
  777. }
  778. Ref<Font> Control::get_font(const StringName &p_name, const StringName &p_theme_type) const {
  779. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  780. const Ref<Font> *font = data.font_override.getptr(p_name);
  781. if (font) {
  782. return *font;
  783. }
  784. }
  785. StringName type = p_theme_type ? p_theme_type : get_class_name();
  786. // try with custom themes
  787. Control *theme_owner = data.theme_owner;
  788. while (theme_owner) {
  789. StringName class_name = type;
  790. while (class_name != StringName()) {
  791. if (theme_owner->data.theme->has_font(p_name, class_name)) {
  792. return theme_owner->data.theme->get_font(p_name, class_name);
  793. }
  794. class_name = ClassDB::get_parent_class_nocheck(class_name);
  795. }
  796. if (theme_owner->data.theme->get_default_theme_font().is_valid()) {
  797. return theme_owner->data.theme->get_default_theme_font();
  798. }
  799. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  800. if (parent) {
  801. theme_owner = parent->data.theme_owner;
  802. } else {
  803. theme_owner = nullptr;
  804. }
  805. }
  806. return Theme::get_default()->get_font(p_name, type);
  807. }
  808. Color Control::get_color(const StringName &p_name, const StringName &p_theme_type) const {
  809. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  810. const Color *color = data.color_override.getptr(p_name);
  811. if (color) {
  812. return *color;
  813. }
  814. }
  815. StringName type = p_theme_type ? p_theme_type : get_class_name();
  816. // try with custom themes
  817. Control *theme_owner = data.theme_owner;
  818. while (theme_owner) {
  819. StringName class_name = type;
  820. while (class_name != StringName()) {
  821. if (theme_owner->data.theme->has_color(p_name, class_name)) {
  822. return theme_owner->data.theme->get_color(p_name, class_name);
  823. }
  824. class_name = ClassDB::get_parent_class_nocheck(class_name);
  825. }
  826. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  827. if (parent) {
  828. theme_owner = parent->data.theme_owner;
  829. } else {
  830. theme_owner = nullptr;
  831. }
  832. }
  833. if (Theme::get_project_default().is_valid()) {
  834. if (Theme::get_project_default()->has_color(p_name, type)) {
  835. return Theme::get_project_default()->get_color(p_name, type);
  836. }
  837. }
  838. return Theme::get_default()->get_color(p_name, type);
  839. }
  840. int Control::get_constant(const StringName &p_name, const StringName &p_theme_type) const {
  841. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  842. const int *constant = data.constant_override.getptr(p_name);
  843. if (constant) {
  844. return *constant;
  845. }
  846. }
  847. StringName type = p_theme_type ? p_theme_type : get_class_name();
  848. // try with custom themes
  849. Control *theme_owner = data.theme_owner;
  850. while (theme_owner) {
  851. StringName class_name = type;
  852. while (class_name != StringName()) {
  853. if (theme_owner->data.theme->has_constant(p_name, class_name)) {
  854. return theme_owner->data.theme->get_constant(p_name, class_name);
  855. }
  856. class_name = ClassDB::get_parent_class_nocheck(class_name);
  857. }
  858. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  859. if (parent) {
  860. theme_owner = parent->data.theme_owner;
  861. } else {
  862. theme_owner = nullptr;
  863. }
  864. }
  865. if (Theme::get_project_default().is_valid()) {
  866. if (Theme::get_project_default()->has_constant(p_name, type)) {
  867. return Theme::get_project_default()->get_constant(p_name, type);
  868. }
  869. }
  870. return Theme::get_default()->get_constant(p_name, type);
  871. }
  872. bool Control::has_icon_override(const StringName &p_name) const {
  873. const Ref<Texture> *tex = data.icon_override.getptr(p_name);
  874. return tex != nullptr;
  875. }
  876. bool Control::has_shader_override(const StringName &p_name) const {
  877. const Ref<Shader> *sdr = data.shader_override.getptr(p_name);
  878. return sdr != nullptr;
  879. }
  880. bool Control::has_stylebox_override(const StringName &p_name) const {
  881. const Ref<StyleBox> *style = data.style_override.getptr(p_name);
  882. return style != nullptr;
  883. }
  884. bool Control::has_font_override(const StringName &p_name) const {
  885. const Ref<Font> *font = data.font_override.getptr(p_name);
  886. return font != nullptr;
  887. }
  888. bool Control::has_color_override(const StringName &p_name) const {
  889. const Color *color = data.color_override.getptr(p_name);
  890. return color != nullptr;
  891. }
  892. bool Control::has_constant_override(const StringName &p_name) const {
  893. const int *constant = data.constant_override.getptr(p_name);
  894. return constant != nullptr;
  895. }
  896. bool Control::has_icon(const StringName &p_name, const StringName &p_theme_type) const {
  897. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  898. if (has_icon_override(p_name)) {
  899. return true;
  900. }
  901. }
  902. StringName type = p_theme_type ? p_theme_type : get_class_name();
  903. // try with custom themes
  904. Control *theme_owner = data.theme_owner;
  905. while (theme_owner) {
  906. StringName class_name = type;
  907. while (class_name != StringName()) {
  908. if (theme_owner->data.theme->has_icon(p_name, class_name)) {
  909. return true;
  910. }
  911. class_name = ClassDB::get_parent_class_nocheck(class_name);
  912. }
  913. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  914. if (parent) {
  915. theme_owner = parent->data.theme_owner;
  916. } else {
  917. theme_owner = nullptr;
  918. }
  919. }
  920. if (Theme::get_project_default().is_valid()) {
  921. if (Theme::get_project_default()->has_icon(p_name, type)) {
  922. return true;
  923. }
  924. }
  925. return Theme::get_default()->has_icon(p_name, type);
  926. }
  927. bool Control::has_shader(const StringName &p_name, const StringName &p_theme_type) const {
  928. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  929. if (has_shader_override(p_name)) {
  930. return true;
  931. }
  932. }
  933. StringName type = p_theme_type ? p_theme_type : get_class_name();
  934. // try with custom themes
  935. Control *theme_owner = data.theme_owner;
  936. while (theme_owner) {
  937. StringName class_name = type;
  938. while (class_name != StringName()) {
  939. if (theme_owner->data.theme->has_shader(p_name, class_name)) {
  940. return true;
  941. }
  942. class_name = ClassDB::get_parent_class_nocheck(class_name);
  943. }
  944. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  945. if (parent) {
  946. theme_owner = parent->data.theme_owner;
  947. } else {
  948. theme_owner = nullptr;
  949. }
  950. }
  951. if (Theme::get_project_default().is_valid()) {
  952. if (Theme::get_project_default()->has_shader(p_name, type)) {
  953. return true;
  954. }
  955. }
  956. return Theme::get_default()->has_shader(p_name, type);
  957. }
  958. bool Control::has_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  959. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  960. if (has_stylebox_override(p_name)) {
  961. return true;
  962. }
  963. }
  964. StringName type = p_theme_type ? p_theme_type : get_class_name();
  965. // try with custom themes
  966. Control *theme_owner = data.theme_owner;
  967. while (theme_owner) {
  968. StringName class_name = type;
  969. while (class_name != StringName()) {
  970. if (theme_owner->data.theme->has_stylebox(p_name, class_name)) {
  971. return true;
  972. }
  973. class_name = ClassDB::get_parent_class_nocheck(class_name);
  974. }
  975. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  976. if (parent) {
  977. theme_owner = parent->data.theme_owner;
  978. } else {
  979. theme_owner = nullptr;
  980. }
  981. }
  982. if (Theme::get_project_default().is_valid()) {
  983. if (Theme::get_project_default()->has_stylebox(p_name, type)) {
  984. return true;
  985. }
  986. }
  987. return Theme::get_default()->has_stylebox(p_name, type);
  988. }
  989. bool Control::has_font(const StringName &p_name, const StringName &p_theme_type) const {
  990. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  991. if (has_font_override(p_name)) {
  992. return true;
  993. }
  994. }
  995. StringName type = p_theme_type ? p_theme_type : get_class_name();
  996. // try with custom themes
  997. Control *theme_owner = data.theme_owner;
  998. while (theme_owner) {
  999. StringName class_name = type;
  1000. while (class_name != StringName()) {
  1001. if (theme_owner->data.theme->has_font(p_name, class_name)) {
  1002. return true;
  1003. }
  1004. class_name = ClassDB::get_parent_class_nocheck(class_name);
  1005. }
  1006. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  1007. if (parent) {
  1008. theme_owner = parent->data.theme_owner;
  1009. } else {
  1010. theme_owner = nullptr;
  1011. }
  1012. }
  1013. if (Theme::get_project_default().is_valid()) {
  1014. if (Theme::get_project_default()->has_font(p_name, type)) {
  1015. return true;
  1016. }
  1017. }
  1018. return Theme::get_default()->has_font(p_name, type);
  1019. }
  1020. bool Control::has_color(const StringName &p_name, const StringName &p_theme_type) const {
  1021. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  1022. if (has_color_override(p_name)) {
  1023. return true;
  1024. }
  1025. }
  1026. StringName type = p_theme_type ? p_theme_type : get_class_name();
  1027. // try with custom themes
  1028. Control *theme_owner = data.theme_owner;
  1029. while (theme_owner) {
  1030. StringName class_name = type;
  1031. while (class_name != StringName()) {
  1032. if (theme_owner->data.theme->has_color(p_name, class_name)) {
  1033. return true;
  1034. }
  1035. class_name = ClassDB::get_parent_class_nocheck(class_name);
  1036. }
  1037. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  1038. if (parent) {
  1039. theme_owner = parent->data.theme_owner;
  1040. } else {
  1041. theme_owner = nullptr;
  1042. }
  1043. }
  1044. if (Theme::get_project_default().is_valid()) {
  1045. if (Theme::get_project_default()->has_color(p_name, type)) {
  1046. return true;
  1047. }
  1048. }
  1049. return Theme::get_default()->has_color(p_name, type);
  1050. }
  1051. bool Control::has_constant(const StringName &p_name, const StringName &p_theme_type) const {
  1052. if (p_theme_type == StringName() || p_theme_type == get_class_name()) {
  1053. if (has_constant_override(p_name)) {
  1054. return true;
  1055. }
  1056. }
  1057. StringName type = p_theme_type ? p_theme_type : get_class_name();
  1058. // try with custom themes
  1059. Control *theme_owner = data.theme_owner;
  1060. while (theme_owner) {
  1061. StringName class_name = type;
  1062. while (class_name != StringName()) {
  1063. if (theme_owner->data.theme->has_constant(p_name, class_name)) {
  1064. return true;
  1065. }
  1066. class_name = ClassDB::get_parent_class_nocheck(class_name);
  1067. }
  1068. Control *parent = Object::cast_to<Control>(theme_owner->get_parent());
  1069. if (parent) {
  1070. theme_owner = parent->data.theme_owner;
  1071. } else {
  1072. theme_owner = nullptr;
  1073. }
  1074. }
  1075. if (Theme::get_project_default().is_valid()) {
  1076. if (Theme::get_project_default()->has_constant(p_name, type)) {
  1077. return true;
  1078. }
  1079. }
  1080. return Theme::get_default()->has_constant(p_name, type);
  1081. }
  1082. Ref<Font> Control::get_theme_default_font() const {
  1083. // First, look through each control or window node in the branch, until no valid parent can be found.
  1084. // Only nodes with a theme resource attached are considered.
  1085. // For each theme resource see if their assigned theme has the default value defined and valid.
  1086. Control *theme_owner = data.theme_owner;
  1087. while (theme_owner) {
  1088. if (theme_owner && theme_owner->data.theme->has_default_theme_font()) {
  1089. return theme_owner->data.theme->get_default_theme_font();
  1090. }
  1091. Node *parent = theme_owner->get_parent();
  1092. Control *parent_c = Object::cast_to<Control>(parent);
  1093. if (parent_c) {
  1094. theme_owner = parent_c->data.theme_owner;
  1095. } else {
  1096. theme_owner = nullptr;
  1097. }
  1098. }
  1099. // Secondly, check the project-defined Theme resource.
  1100. if (Theme::get_project_default().is_valid()) {
  1101. if (Theme::get_project_default()->has_default_theme_font()) {
  1102. return Theme::get_project_default()->get_default_theme_font();
  1103. }
  1104. }
  1105. // Lastly, fall back on the default Theme.
  1106. return Theme::get_default()->get_default_theme_font();
  1107. }
  1108. Rect2 Control::get_parent_anchorable_rect() const {
  1109. if (!is_inside_tree()) {
  1110. return Rect2();
  1111. }
  1112. Rect2 parent_rect;
  1113. if (data.parent_canvas_item) {
  1114. parent_rect = data.parent_canvas_item->get_anchorable_rect();
  1115. } else {
  1116. parent_rect = get_viewport()->get_visible_rect();
  1117. }
  1118. return parent_rect;
  1119. }
  1120. Size2 Control::get_parent_area_size() const {
  1121. return get_parent_anchorable_rect().size;
  1122. }
  1123. void Control::_size_changed() {
  1124. Rect2 parent_rect = get_parent_anchorable_rect();
  1125. float margin_pos[4];
  1126. for (int i = 0; i < 4; i++) {
  1127. float area = parent_rect.size[i & 1];
  1128. margin_pos[i] = data.margin[i] + (data.anchor[i] * area);
  1129. }
  1130. Point2 new_pos_cache = Point2(margin_pos[0], margin_pos[1]);
  1131. Size2 new_size_cache = Point2(margin_pos[2], margin_pos[3]) - new_pos_cache;
  1132. Size2 minimum_size = get_combined_minimum_size();
  1133. if (minimum_size.width > new_size_cache.width) {
  1134. if (data.h_grow == GROW_DIRECTION_BEGIN) {
  1135. new_pos_cache.x += new_size_cache.width - minimum_size.width;
  1136. } else if (data.h_grow == GROW_DIRECTION_BOTH) {
  1137. new_pos_cache.x += 0.5 * (new_size_cache.width - minimum_size.width);
  1138. }
  1139. new_size_cache.width = minimum_size.width;
  1140. }
  1141. if (minimum_size.height > new_size_cache.height) {
  1142. if (data.v_grow == GROW_DIRECTION_BEGIN) {
  1143. new_pos_cache.y += new_size_cache.height - minimum_size.height;
  1144. } else if (data.v_grow == GROW_DIRECTION_BOTH) {
  1145. new_pos_cache.y += 0.5 * (new_size_cache.height - minimum_size.height);
  1146. }
  1147. new_size_cache.height = minimum_size.height;
  1148. }
  1149. bool pos_changed = new_pos_cache != data.pos_cache;
  1150. bool size_changed = new_size_cache != data.size_cache;
  1151. data.pos_cache = new_pos_cache;
  1152. data.size_cache = new_size_cache;
  1153. if (is_inside_tree()) {
  1154. if (size_changed) {
  1155. notification(NOTIFICATION_RESIZED);
  1156. }
  1157. if (pos_changed || size_changed) {
  1158. item_rect_changed(size_changed);
  1159. _change_notify_margins();
  1160. _notify_transform();
  1161. }
  1162. if (pos_changed && !size_changed) {
  1163. _update_canvas_item_transform(); //move because it won't be updated
  1164. }
  1165. }
  1166. }
  1167. void Control::set_anchor(Margin p_margin, float p_anchor, bool p_keep_margin, bool p_push_opposite_anchor) {
  1168. ERR_FAIL_INDEX((int)p_margin, 4);
  1169. Rect2 parent_rect = get_parent_anchorable_rect();
  1170. float parent_range = (p_margin == MARGIN_LEFT || p_margin == MARGIN_RIGHT) ? parent_rect.size.x : parent_rect.size.y;
  1171. float previous_margin_pos = data.margin[p_margin] + data.anchor[p_margin] * parent_range;
  1172. float previous_opposite_margin_pos = data.margin[(p_margin + 2) % 4] + data.anchor[(p_margin + 2) % 4] * parent_range;
  1173. data.anchor[p_margin] = p_anchor;
  1174. if (((p_margin == MARGIN_LEFT || p_margin == MARGIN_TOP) && data.anchor[p_margin] > data.anchor[(p_margin + 2) % 4]) ||
  1175. ((p_margin == MARGIN_RIGHT || p_margin == MARGIN_BOTTOM) && data.anchor[p_margin] < data.anchor[(p_margin + 2) % 4])) {
  1176. if (p_push_opposite_anchor) {
  1177. data.anchor[(p_margin + 2) % 4] = data.anchor[p_margin];
  1178. } else {
  1179. data.anchor[p_margin] = data.anchor[(p_margin + 2) % 4];
  1180. }
  1181. }
  1182. if (!p_keep_margin) {
  1183. data.margin[p_margin] = previous_margin_pos - data.anchor[p_margin] * parent_range;
  1184. if (p_push_opposite_anchor) {
  1185. data.margin[(p_margin + 2) % 4] = previous_opposite_margin_pos - data.anchor[(p_margin + 2) % 4] * parent_range;
  1186. }
  1187. }
  1188. if (is_inside_tree()) {
  1189. _size_changed();
  1190. }
  1191. update();
  1192. _change_notify("anchor_left");
  1193. _change_notify("anchor_right");
  1194. _change_notify("anchor_top");
  1195. _change_notify("anchor_bottom");
  1196. }
  1197. void Control::_set_anchor(Margin p_margin, float p_anchor) {
  1198. set_anchor(p_margin, p_anchor);
  1199. }
  1200. void Control::set_anchor_and_margin(Margin p_margin, float p_anchor, float p_pos, bool p_push_opposite_anchor) {
  1201. set_anchor(p_margin, p_anchor, false, p_push_opposite_anchor);
  1202. set_margin(p_margin, p_pos);
  1203. }
  1204. void Control::set_anchors_preset(LayoutPreset p_preset, bool p_keep_margins) {
  1205. ERR_FAIL_INDEX((int)p_preset, 16);
  1206. //Left
  1207. switch (p_preset) {
  1208. case PRESET_TOP_LEFT:
  1209. case PRESET_BOTTOM_LEFT:
  1210. case PRESET_CENTER_LEFT:
  1211. case PRESET_TOP_WIDE:
  1212. case PRESET_BOTTOM_WIDE:
  1213. case PRESET_LEFT_WIDE:
  1214. case PRESET_HCENTER_WIDE:
  1215. case PRESET_WIDE:
  1216. set_anchor(MARGIN_LEFT, ANCHOR_BEGIN, p_keep_margins);
  1217. break;
  1218. case PRESET_CENTER_TOP:
  1219. case PRESET_CENTER_BOTTOM:
  1220. case PRESET_CENTER:
  1221. case PRESET_VCENTER_WIDE:
  1222. set_anchor(MARGIN_LEFT, 0.5, p_keep_margins);
  1223. break;
  1224. case PRESET_TOP_RIGHT:
  1225. case PRESET_BOTTOM_RIGHT:
  1226. case PRESET_CENTER_RIGHT:
  1227. case PRESET_RIGHT_WIDE:
  1228. set_anchor(MARGIN_LEFT, ANCHOR_END, p_keep_margins);
  1229. break;
  1230. }
  1231. // Top
  1232. switch (p_preset) {
  1233. case PRESET_TOP_LEFT:
  1234. case PRESET_TOP_RIGHT:
  1235. case PRESET_CENTER_TOP:
  1236. case PRESET_LEFT_WIDE:
  1237. case PRESET_RIGHT_WIDE:
  1238. case PRESET_TOP_WIDE:
  1239. case PRESET_VCENTER_WIDE:
  1240. case PRESET_WIDE:
  1241. set_anchor(MARGIN_TOP, ANCHOR_BEGIN, p_keep_margins);
  1242. break;
  1243. case PRESET_CENTER_LEFT:
  1244. case PRESET_CENTER_RIGHT:
  1245. case PRESET_CENTER:
  1246. case PRESET_HCENTER_WIDE:
  1247. set_anchor(MARGIN_TOP, 0.5, p_keep_margins);
  1248. break;
  1249. case PRESET_BOTTOM_LEFT:
  1250. case PRESET_BOTTOM_RIGHT:
  1251. case PRESET_CENTER_BOTTOM:
  1252. case PRESET_BOTTOM_WIDE:
  1253. set_anchor(MARGIN_TOP, ANCHOR_END, p_keep_margins);
  1254. break;
  1255. }
  1256. // Right
  1257. switch (p_preset) {
  1258. case PRESET_TOP_LEFT:
  1259. case PRESET_BOTTOM_LEFT:
  1260. case PRESET_CENTER_LEFT:
  1261. case PRESET_LEFT_WIDE:
  1262. set_anchor(MARGIN_RIGHT, ANCHOR_BEGIN, p_keep_margins);
  1263. break;
  1264. case PRESET_CENTER_TOP:
  1265. case PRESET_CENTER_BOTTOM:
  1266. case PRESET_CENTER:
  1267. case PRESET_VCENTER_WIDE:
  1268. set_anchor(MARGIN_RIGHT, 0.5, p_keep_margins);
  1269. break;
  1270. case PRESET_TOP_RIGHT:
  1271. case PRESET_BOTTOM_RIGHT:
  1272. case PRESET_CENTER_RIGHT:
  1273. case PRESET_TOP_WIDE:
  1274. case PRESET_RIGHT_WIDE:
  1275. case PRESET_BOTTOM_WIDE:
  1276. case PRESET_HCENTER_WIDE:
  1277. case PRESET_WIDE:
  1278. set_anchor(MARGIN_RIGHT, ANCHOR_END, p_keep_margins);
  1279. break;
  1280. }
  1281. // Bottom
  1282. switch (p_preset) {
  1283. case PRESET_TOP_LEFT:
  1284. case PRESET_TOP_RIGHT:
  1285. case PRESET_CENTER_TOP:
  1286. case PRESET_TOP_WIDE:
  1287. set_anchor(MARGIN_BOTTOM, ANCHOR_BEGIN, p_keep_margins);
  1288. break;
  1289. case PRESET_CENTER_LEFT:
  1290. case PRESET_CENTER_RIGHT:
  1291. case PRESET_CENTER:
  1292. case PRESET_HCENTER_WIDE:
  1293. set_anchor(MARGIN_BOTTOM, 0.5, p_keep_margins);
  1294. break;
  1295. case PRESET_BOTTOM_LEFT:
  1296. case PRESET_BOTTOM_RIGHT:
  1297. case PRESET_CENTER_BOTTOM:
  1298. case PRESET_LEFT_WIDE:
  1299. case PRESET_RIGHT_WIDE:
  1300. case PRESET_BOTTOM_WIDE:
  1301. case PRESET_VCENTER_WIDE:
  1302. case PRESET_WIDE:
  1303. set_anchor(MARGIN_BOTTOM, ANCHOR_END, p_keep_margins);
  1304. break;
  1305. }
  1306. }
  1307. void Control::set_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
  1308. ERR_FAIL_INDEX((int)p_preset, 16);
  1309. ERR_FAIL_INDEX((int)p_resize_mode, 4);
  1310. // Calculate the size if the node is not resized
  1311. Size2 min_size = get_minimum_size();
  1312. Size2 new_size = get_size();
  1313. if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_HEIGHT) {
  1314. new_size.x = min_size.x;
  1315. }
  1316. if (p_resize_mode == PRESET_MODE_MINSIZE || p_resize_mode == PRESET_MODE_KEEP_WIDTH) {
  1317. new_size.y = min_size.y;
  1318. }
  1319. Rect2 parent_rect = get_parent_anchorable_rect();
  1320. //Left
  1321. switch (p_preset) {
  1322. case PRESET_TOP_LEFT:
  1323. case PRESET_BOTTOM_LEFT:
  1324. case PRESET_CENTER_LEFT:
  1325. case PRESET_TOP_WIDE:
  1326. case PRESET_BOTTOM_WIDE:
  1327. case PRESET_LEFT_WIDE:
  1328. case PRESET_HCENTER_WIDE:
  1329. case PRESET_WIDE:
  1330. data.margin[0] = parent_rect.size.x * (0.0 - data.anchor[0]) + p_margin + parent_rect.position.x;
  1331. break;
  1332. case PRESET_CENTER_TOP:
  1333. case PRESET_CENTER_BOTTOM:
  1334. case PRESET_CENTER:
  1335. case PRESET_VCENTER_WIDE:
  1336. data.margin[0] = parent_rect.size.x * (0.5 - data.anchor[0]) - new_size.x / 2 + parent_rect.position.x;
  1337. break;
  1338. case PRESET_TOP_RIGHT:
  1339. case PRESET_BOTTOM_RIGHT:
  1340. case PRESET_CENTER_RIGHT:
  1341. case PRESET_RIGHT_WIDE:
  1342. data.margin[0] = parent_rect.size.x * (1.0 - data.anchor[0]) - new_size.x - p_margin + parent_rect.position.x;
  1343. break;
  1344. }
  1345. // Top
  1346. switch (p_preset) {
  1347. case PRESET_TOP_LEFT:
  1348. case PRESET_TOP_RIGHT:
  1349. case PRESET_CENTER_TOP:
  1350. case PRESET_LEFT_WIDE:
  1351. case PRESET_RIGHT_WIDE:
  1352. case PRESET_TOP_WIDE:
  1353. case PRESET_VCENTER_WIDE:
  1354. case PRESET_WIDE:
  1355. data.margin[1] = parent_rect.size.y * (0.0 - data.anchor[1]) + p_margin + parent_rect.position.y;
  1356. break;
  1357. case PRESET_CENTER_LEFT:
  1358. case PRESET_CENTER_RIGHT:
  1359. case PRESET_CENTER:
  1360. case PRESET_HCENTER_WIDE:
  1361. data.margin[1] = parent_rect.size.y * (0.5 - data.anchor[1]) - new_size.y / 2 + parent_rect.position.y;
  1362. break;
  1363. case PRESET_BOTTOM_LEFT:
  1364. case PRESET_BOTTOM_RIGHT:
  1365. case PRESET_CENTER_BOTTOM:
  1366. case PRESET_BOTTOM_WIDE:
  1367. data.margin[1] = parent_rect.size.y * (1.0 - data.anchor[1]) - new_size.y - p_margin + parent_rect.position.y;
  1368. break;
  1369. }
  1370. // Right
  1371. switch (p_preset) {
  1372. case PRESET_TOP_LEFT:
  1373. case PRESET_BOTTOM_LEFT:
  1374. case PRESET_CENTER_LEFT:
  1375. case PRESET_LEFT_WIDE:
  1376. data.margin[2] = parent_rect.size.x * (0.0 - data.anchor[2]) + new_size.x + p_margin + parent_rect.position.x;
  1377. break;
  1378. case PRESET_CENTER_TOP:
  1379. case PRESET_CENTER_BOTTOM:
  1380. case PRESET_CENTER:
  1381. case PRESET_VCENTER_WIDE:
  1382. data.margin[2] = parent_rect.size.x * (0.5 - data.anchor[2]) + new_size.x / 2 + parent_rect.position.x;
  1383. break;
  1384. case PRESET_TOP_RIGHT:
  1385. case PRESET_BOTTOM_RIGHT:
  1386. case PRESET_CENTER_RIGHT:
  1387. case PRESET_TOP_WIDE:
  1388. case PRESET_RIGHT_WIDE:
  1389. case PRESET_BOTTOM_WIDE:
  1390. case PRESET_HCENTER_WIDE:
  1391. case PRESET_WIDE:
  1392. data.margin[2] = parent_rect.size.x * (1.0 - data.anchor[2]) - p_margin + parent_rect.position.x;
  1393. break;
  1394. }
  1395. // Bottom
  1396. switch (p_preset) {
  1397. case PRESET_TOP_LEFT:
  1398. case PRESET_TOP_RIGHT:
  1399. case PRESET_CENTER_TOP:
  1400. case PRESET_TOP_WIDE:
  1401. data.margin[3] = parent_rect.size.y * (0.0 - data.anchor[3]) + new_size.y + p_margin + parent_rect.position.y;
  1402. break;
  1403. case PRESET_CENTER_LEFT:
  1404. case PRESET_CENTER_RIGHT:
  1405. case PRESET_CENTER:
  1406. case PRESET_HCENTER_WIDE:
  1407. data.margin[3] = parent_rect.size.y * (0.5 - data.anchor[3]) + new_size.y / 2 + parent_rect.position.y;
  1408. break;
  1409. case PRESET_BOTTOM_LEFT:
  1410. case PRESET_BOTTOM_RIGHT:
  1411. case PRESET_CENTER_BOTTOM:
  1412. case PRESET_LEFT_WIDE:
  1413. case PRESET_RIGHT_WIDE:
  1414. case PRESET_BOTTOM_WIDE:
  1415. case PRESET_VCENTER_WIDE:
  1416. case PRESET_WIDE:
  1417. data.margin[3] = parent_rect.size.y * (1.0 - data.anchor[3]) - p_margin + parent_rect.position.y;
  1418. break;
  1419. }
  1420. _size_changed();
  1421. }
  1422. void Control::set_anchors_and_margins_preset(LayoutPreset p_preset, LayoutPresetMode p_resize_mode, int p_margin) {
  1423. set_anchors_preset(p_preset);
  1424. set_margins_preset(p_preset, p_resize_mode, p_margin);
  1425. }
  1426. float Control::get_anchor(Margin p_margin) const {
  1427. ERR_FAIL_INDEX_V(int(p_margin), 4, 0.0);
  1428. return data.anchor[p_margin];
  1429. }
  1430. void Control::_change_notify_margins() {
  1431. // this avoids sending the whole object data again on a change
  1432. _change_notify("margin_left");
  1433. _change_notify("margin_top");
  1434. _change_notify("margin_right");
  1435. _change_notify("margin_bottom");
  1436. _change_notify("rect_position");
  1437. _change_notify("rect_size");
  1438. }
  1439. void Control::set_margin(Margin p_margin, float p_value) {
  1440. ERR_FAIL_INDEX((int)p_margin, 4);
  1441. data.margin[p_margin] = p_value;
  1442. _size_changed();
  1443. }
  1444. void Control::set_begin(const Size2 &p_point) {
  1445. data.margin[0] = p_point.x;
  1446. data.margin[1] = p_point.y;
  1447. _size_changed();
  1448. }
  1449. void Control::set_end(const Size2 &p_point) {
  1450. data.margin[2] = p_point.x;
  1451. data.margin[3] = p_point.y;
  1452. _size_changed();
  1453. }
  1454. float Control::get_margin(Margin p_margin) const {
  1455. ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
  1456. return data.margin[p_margin];
  1457. }
  1458. Size2 Control::get_begin() const {
  1459. return Size2(data.margin[0], data.margin[1]);
  1460. }
  1461. Size2 Control::get_end() const {
  1462. return Size2(data.margin[2], data.margin[3]);
  1463. }
  1464. Point2 Control::get_global_position() const {
  1465. return get_global_transform().get_origin();
  1466. }
  1467. void Control::_set_global_position(const Point2 &p_point) {
  1468. set_global_position(p_point);
  1469. }
  1470. void Control::set_global_position(const Point2 &p_point, bool p_keep_margins) {
  1471. Transform2D inv;
  1472. if (data.parent_canvas_item) {
  1473. inv = data.parent_canvas_item->get_global_transform().affine_inverse();
  1474. }
  1475. set_position(inv.xform(p_point), p_keep_margins);
  1476. }
  1477. void Control::_compute_anchors(Rect2 p_rect, const float p_margins[4], float (&r_anchors)[4]) {
  1478. Size2 parent_rect_size = get_parent_anchorable_rect().size;
  1479. ERR_FAIL_COND(parent_rect_size.x == 0.0);
  1480. ERR_FAIL_COND(parent_rect_size.y == 0.0);
  1481. r_anchors[0] = (p_rect.position.x - p_margins[0]) / parent_rect_size.x;
  1482. r_anchors[1] = (p_rect.position.y - p_margins[1]) / parent_rect_size.y;
  1483. r_anchors[2] = (p_rect.position.x + p_rect.size.x - p_margins[2]) / parent_rect_size.x;
  1484. r_anchors[3] = (p_rect.position.y + p_rect.size.y - p_margins[3]) / parent_rect_size.y;
  1485. }
  1486. void Control::_compute_margins(Rect2 p_rect, const float p_anchors[4], float (&r_margins)[4]) {
  1487. Size2 parent_rect_size = get_parent_anchorable_rect().size;
  1488. r_margins[0] = p_rect.position.x - (p_anchors[0] * parent_rect_size.x);
  1489. r_margins[1] = p_rect.position.y - (p_anchors[1] * parent_rect_size.y);
  1490. r_margins[2] = p_rect.position.x + p_rect.size.x - (p_anchors[2] * parent_rect_size.x);
  1491. r_margins[3] = p_rect.position.y + p_rect.size.y - (p_anchors[3] * parent_rect_size.y);
  1492. }
  1493. void Control::_set_position(const Size2 &p_point) {
  1494. set_position(p_point);
  1495. }
  1496. void Control::set_position(const Size2 &p_point, bool p_keep_margins) {
  1497. if (p_keep_margins) {
  1498. _compute_anchors(Rect2(p_point, data.size_cache), data.margin, data.anchor);
  1499. _change_notify("anchor_left");
  1500. _change_notify("anchor_right");
  1501. _change_notify("anchor_top");
  1502. _change_notify("anchor_bottom");
  1503. } else {
  1504. _compute_margins(Rect2(p_point, data.size_cache), data.anchor, data.margin);
  1505. }
  1506. _size_changed();
  1507. }
  1508. void Control::_set_size(const Size2 &p_size) {
  1509. set_size(p_size);
  1510. }
  1511. void Control::set_size(const Size2 &p_size, bool p_keep_margins) {
  1512. Size2 new_size = p_size;
  1513. Size2 min = get_combined_minimum_size();
  1514. if (new_size.x < min.x) {
  1515. new_size.x = min.x;
  1516. }
  1517. if (new_size.y < min.y) {
  1518. new_size.y = min.y;
  1519. }
  1520. if (p_keep_margins) {
  1521. _compute_anchors(Rect2(data.pos_cache, new_size), data.margin, data.anchor);
  1522. _change_notify("anchor_left");
  1523. _change_notify("anchor_right");
  1524. _change_notify("anchor_top");
  1525. _change_notify("anchor_bottom");
  1526. } else {
  1527. _compute_margins(Rect2(data.pos_cache, new_size), data.anchor, data.margin);
  1528. }
  1529. _size_changed();
  1530. }
  1531. Size2 Control::get_position() const {
  1532. return data.pos_cache;
  1533. }
  1534. Size2 Control::get_size() const {
  1535. return data.size_cache;
  1536. }
  1537. Rect2 Control::get_global_rect() const {
  1538. return Rect2(get_global_position(), get_size());
  1539. }
  1540. Rect2 Control::get_window_rect() const {
  1541. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  1542. Rect2 gr = get_global_rect();
  1543. gr.position += get_viewport()->get_visible_rect().position;
  1544. return gr;
  1545. }
  1546. Rect2 Control::get_rect() const {
  1547. return Rect2(get_position(), get_size());
  1548. }
  1549. Rect2 Control::get_anchorable_rect() const {
  1550. return Rect2(Point2(), get_size());
  1551. }
  1552. void Control::add_icon_override(const StringName &p_name, const Ref<Texture> &p_icon) {
  1553. if (data.icon_override.has(p_name)) {
  1554. data.icon_override[p_name]->disconnect("changed", this, "_override_changed");
  1555. }
  1556. // clear if "null" is passed instead of a icon
  1557. if (p_icon.is_null()) {
  1558. data.icon_override.erase(p_name);
  1559. } else {
  1560. data.icon_override[p_name] = p_icon;
  1561. if (data.icon_override[p_name].is_valid()) {
  1562. data.icon_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
  1563. }
  1564. }
  1565. notification(NOTIFICATION_THEME_CHANGED);
  1566. }
  1567. void Control::add_shader_override(const StringName &p_name, const Ref<Shader> &p_shader) {
  1568. if (data.shader_override.has(p_name)) {
  1569. data.shader_override[p_name]->disconnect("changed", this, "_override_changed");
  1570. }
  1571. // clear if "null" is passed instead of a shader
  1572. if (p_shader.is_null()) {
  1573. data.shader_override.erase(p_name);
  1574. } else {
  1575. data.shader_override[p_name] = p_shader;
  1576. if (data.shader_override[p_name].is_valid()) {
  1577. data.shader_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
  1578. }
  1579. }
  1580. notification(NOTIFICATION_THEME_CHANGED);
  1581. }
  1582. void Control::add_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
  1583. if (data.style_override.has(p_name)) {
  1584. data.style_override[p_name]->disconnect("changed", this, "_override_changed");
  1585. }
  1586. // clear if "null" is passed instead of a style
  1587. if (p_style.is_null()) {
  1588. data.style_override.erase(p_name);
  1589. } else {
  1590. data.style_override[p_name] = p_style;
  1591. if (data.style_override[p_name].is_valid()) {
  1592. data.style_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
  1593. }
  1594. }
  1595. notification(NOTIFICATION_THEME_CHANGED);
  1596. }
  1597. void Control::add_font_override(const StringName &p_name, const Ref<Font> &p_font) {
  1598. if (data.font_override.has(p_name)) {
  1599. data.font_override[p_name]->disconnect("changed", this, "_override_changed");
  1600. }
  1601. // clear if "null" is passed instead of a font
  1602. if (p_font.is_null()) {
  1603. data.font_override.erase(p_name);
  1604. } else {
  1605. data.font_override[p_name] = p_font;
  1606. if (data.font_override[p_name].is_valid()) {
  1607. data.font_override[p_name]->connect("changed", this, "_override_changed", Vector<Variant>(), CONNECT_REFERENCE_COUNTED);
  1608. }
  1609. }
  1610. notification(NOTIFICATION_THEME_CHANGED);
  1611. }
  1612. void Control::add_color_override(const StringName &p_name, const Color &p_color) {
  1613. data.color_override[p_name] = p_color;
  1614. notification(NOTIFICATION_THEME_CHANGED);
  1615. }
  1616. void Control::add_constant_override(const StringName &p_name, int p_constant) {
  1617. data.constant_override[p_name] = p_constant;
  1618. notification(NOTIFICATION_THEME_CHANGED);
  1619. }
  1620. void Control::set_focus_mode(FocusMode p_focus_mode) {
  1621. ERR_FAIL_INDEX((int)p_focus_mode, 3);
  1622. if (is_inside_tree() && p_focus_mode == FOCUS_NONE && data.focus_mode != FOCUS_NONE && has_focus()) {
  1623. release_focus();
  1624. }
  1625. data.focus_mode = p_focus_mode;
  1626. }
  1627. static Control *_next_control(Control *p_from) {
  1628. if (p_from->is_set_as_toplevel()) {
  1629. return nullptr; // can't go above
  1630. }
  1631. Control *parent = Object::cast_to<Control>(p_from->get_parent());
  1632. if (!parent) {
  1633. return nullptr;
  1634. }
  1635. int next = p_from->get_position_in_parent();
  1636. ERR_FAIL_INDEX_V(next, parent->get_child_count(), nullptr);
  1637. for (int i = (next + 1); i < parent->get_child_count(); i++) {
  1638. Control *c = Object::cast_to<Control>(parent->get_child(i));
  1639. if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) {
  1640. continue;
  1641. }
  1642. return c;
  1643. }
  1644. //no next in parent, try the same in parent
  1645. return _next_control(parent);
  1646. }
  1647. Control *Control::find_next_valid_focus() const {
  1648. Control *from = const_cast<Control *>(this);
  1649. while (true) {
  1650. // If the focus property is manually overwritten, attempt to use it.
  1651. if (!data.focus_next.is_empty()) {
  1652. Node *n = get_node(data.focus_next);
  1653. Control *c;
  1654. if (n) {
  1655. c = Object::cast_to<Control>(n);
  1656. ERR_FAIL_COND_V_MSG(!c, nullptr, "Next focus node is not a control: " + n->get_name() + ".");
  1657. } else {
  1658. return nullptr;
  1659. }
  1660. if (c->is_visible() && c->get_focus_mode() != FOCUS_NONE) {
  1661. return c;
  1662. }
  1663. }
  1664. // find next child
  1665. Control *next_child = nullptr;
  1666. for (int i = 0; i < from->get_child_count(); i++) {
  1667. Control *c = Object::cast_to<Control>(from->get_child(i));
  1668. if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) {
  1669. continue;
  1670. }
  1671. next_child = c;
  1672. break;
  1673. }
  1674. if (!next_child) {
  1675. next_child = _next_control(from);
  1676. if (!next_child) { //nothing else.. go up and find either window or subwindow
  1677. next_child = const_cast<Control *>(this);
  1678. while (next_child && !next_child->is_set_as_toplevel()) {
  1679. next_child = cast_to<Control>(next_child->get_parent());
  1680. }
  1681. if (!next_child) {
  1682. next_child = const_cast<Control *>(this);
  1683. while (next_child) {
  1684. if (next_child->data.SI || next_child->data.RI) {
  1685. break;
  1686. }
  1687. next_child = next_child->get_parent_control();
  1688. }
  1689. }
  1690. }
  1691. }
  1692. if (next_child == this) { // no next control->
  1693. return (get_focus_mode() == FOCUS_ALL) ? next_child : nullptr;
  1694. }
  1695. if (next_child) {
  1696. if (next_child->get_focus_mode() == FOCUS_ALL) {
  1697. return next_child;
  1698. }
  1699. from = next_child;
  1700. } else {
  1701. break;
  1702. }
  1703. }
  1704. return nullptr;
  1705. }
  1706. static Control *_prev_control(Control *p_from) {
  1707. Control *child = nullptr;
  1708. for (int i = p_from->get_child_count() - 1; i >= 0; i--) {
  1709. Control *c = Object::cast_to<Control>(p_from->get_child(i));
  1710. if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) {
  1711. continue;
  1712. }
  1713. child = c;
  1714. break;
  1715. }
  1716. if (!child) {
  1717. return p_from;
  1718. }
  1719. //no prev in parent, try the same in parent
  1720. return _prev_control(child);
  1721. }
  1722. Control *Control::find_prev_valid_focus() const {
  1723. Control *from = const_cast<Control *>(this);
  1724. while (true) {
  1725. // If the focus property is manually overwritten, attempt to use it.
  1726. if (!data.focus_prev.is_empty()) {
  1727. Node *n = get_node(data.focus_prev);
  1728. Control *c;
  1729. if (n) {
  1730. c = Object::cast_to<Control>(n);
  1731. ERR_FAIL_COND_V_MSG(!c, nullptr, "Previous focus node is not a control: " + n->get_name() + ".");
  1732. } else {
  1733. return nullptr;
  1734. }
  1735. if (c->is_visible() && c->get_focus_mode() != FOCUS_NONE) {
  1736. return c;
  1737. }
  1738. }
  1739. // find prev child
  1740. Control *prev_child = nullptr;
  1741. if (from->is_set_as_toplevel() || !Object::cast_to<Control>(from->get_parent())) {
  1742. //find last of the children
  1743. prev_child = _prev_control(from);
  1744. } else {
  1745. for (int i = (from->get_position_in_parent() - 1); i >= 0; i--) {
  1746. Control *c = Object::cast_to<Control>(from->get_parent()->get_child(i));
  1747. if (!c || !c->is_visible_in_tree() || c->is_set_as_toplevel()) {
  1748. continue;
  1749. }
  1750. prev_child = c;
  1751. break;
  1752. }
  1753. if (!prev_child) {
  1754. prev_child = Object::cast_to<Control>(from->get_parent());
  1755. } else {
  1756. prev_child = _prev_control(prev_child);
  1757. }
  1758. }
  1759. if (prev_child == this) { // no prev control->
  1760. return (get_focus_mode() == FOCUS_ALL) ? prev_child : nullptr;
  1761. }
  1762. if (prev_child->get_focus_mode() == FOCUS_ALL) {
  1763. return prev_child;
  1764. }
  1765. from = prev_child;
  1766. }
  1767. return nullptr;
  1768. }
  1769. Control::FocusMode Control::get_focus_mode() const {
  1770. return data.focus_mode;
  1771. }
  1772. bool Control::has_focus() const {
  1773. return is_inside_tree() && get_viewport()->_gui_control_has_focus(this);
  1774. }
  1775. void Control::grab_focus() {
  1776. ERR_FAIL_COND(!is_inside_tree());
  1777. if (data.focus_mode == FOCUS_NONE) {
  1778. WARN_PRINT("This control can't grab focus. Use set_focus_mode() to allow a control to get focus.");
  1779. return;
  1780. }
  1781. get_viewport()->_gui_control_grab_focus(this);
  1782. }
  1783. void Control::release_focus() {
  1784. ERR_FAIL_COND(!is_inside_tree());
  1785. if (!has_focus()) {
  1786. return;
  1787. }
  1788. get_viewport()->_gui_remove_focus();
  1789. update();
  1790. }
  1791. bool Control::is_toplevel_control() const {
  1792. return is_inside_tree() && (!data.parent_canvas_item && !data.RI && is_set_as_toplevel());
  1793. }
  1794. void Control::show_modal(bool p_exclusive) {
  1795. ERR_FAIL_COND(!is_inside_tree());
  1796. ERR_FAIL_COND(!data.SI);
  1797. if (is_visible_in_tree()) {
  1798. hide();
  1799. }
  1800. ERR_FAIL_COND(data.MI != nullptr);
  1801. show();
  1802. raise();
  1803. data.modal_exclusive = p_exclusive;
  1804. data.MI = get_viewport()->_gui_show_modal(this);
  1805. data.modal_frame = Engine::get_singleton()->get_frames_drawn();
  1806. }
  1807. void Control::_modal_set_prev_focus_owner(ObjectID p_prev) {
  1808. data.modal_prev_focus_owner = p_prev;
  1809. }
  1810. void Control::_modal_stack_remove() {
  1811. ERR_FAIL_COND(!is_inside_tree());
  1812. if (!data.MI) {
  1813. return;
  1814. }
  1815. List<Control *>::Element *element = data.MI;
  1816. data.MI = nullptr;
  1817. get_viewport()->_gui_remove_from_modal_stack(element, data.modal_prev_focus_owner);
  1818. data.modal_prev_focus_owner = 0;
  1819. }
  1820. void Control::_propagate_theme_changed(CanvasItem *p_at, Control *p_owner, bool p_assign) {
  1821. Control *c = Object::cast_to<Control>(p_at);
  1822. if (c && c != p_owner && c->data.theme.is_valid()) { // has a theme, this can't be propagated
  1823. return;
  1824. }
  1825. for (int i = 0; i < p_at->get_child_count(); i++) {
  1826. CanvasItem *child = Object::cast_to<CanvasItem>(p_at->get_child(i));
  1827. if (child) {
  1828. _propagate_theme_changed(child, p_owner, p_assign);
  1829. }
  1830. }
  1831. if (c) {
  1832. if (p_assign) {
  1833. c->data.theme_owner = p_owner;
  1834. }
  1835. c->notification(NOTIFICATION_THEME_CHANGED);
  1836. }
  1837. }
  1838. void Control::_theme_changed() {
  1839. _propagate_theme_changed(this, this, false);
  1840. }
  1841. void Control::set_theme(const Ref<Theme> &p_theme) {
  1842. if (data.theme == p_theme) {
  1843. return;
  1844. }
  1845. if (data.theme.is_valid()) {
  1846. data.theme->disconnect("changed", this, "_theme_changed");
  1847. }
  1848. data.theme = p_theme;
  1849. if (!p_theme.is_null()) {
  1850. data.theme_owner = this;
  1851. _propagate_theme_changed(this, this);
  1852. } else {
  1853. Control *parent = cast_to<Control>(get_parent());
  1854. if (parent && parent->data.theme_owner) {
  1855. _propagate_theme_changed(this, parent->data.theme_owner);
  1856. } else {
  1857. _propagate_theme_changed(this, nullptr);
  1858. }
  1859. }
  1860. if (data.theme.is_valid()) {
  1861. data.theme->connect("changed", this, "_theme_changed", varray(), CONNECT_DEFERRED);
  1862. }
  1863. }
  1864. void Control::accept_event() {
  1865. if (is_inside_tree()) {
  1866. get_viewport()->_gui_accept_event();
  1867. }
  1868. }
  1869. Ref<Theme> Control::get_theme() const {
  1870. return data.theme;
  1871. }
  1872. void Control::set_tooltip(const String &p_tooltip) {
  1873. data.tooltip = p_tooltip;
  1874. update_configuration_warning();
  1875. }
  1876. String Control::get_tooltip(const Point2 &p_pos) const {
  1877. return data.tooltip;
  1878. }
  1879. Control *Control::make_custom_tooltip(const String &p_text) const {
  1880. if (get_script_instance()) {
  1881. return const_cast<Control *>(this)->call("_make_custom_tooltip", p_text);
  1882. }
  1883. return nullptr;
  1884. }
  1885. void Control::set_default_cursor_shape(CursorShape p_shape) {
  1886. ERR_FAIL_INDEX(int(p_shape), CURSOR_MAX);
  1887. data.default_cursor = p_shape;
  1888. }
  1889. Control::CursorShape Control::get_default_cursor_shape() const {
  1890. return data.default_cursor;
  1891. }
  1892. Control::CursorShape Control::get_cursor_shape(const Point2 &p_pos) const {
  1893. return data.default_cursor;
  1894. }
  1895. Transform2D Control::get_transform() const {
  1896. Transform2D xform = _get_internal_transform();
  1897. xform[2] += get_position();
  1898. return xform;
  1899. }
  1900. String Control::_get_tooltip() const {
  1901. return data.tooltip;
  1902. }
  1903. void Control::set_focus_neighbour(Margin p_margin, const NodePath &p_neighbour) {
  1904. ERR_FAIL_INDEX((int)p_margin, 4);
  1905. data.focus_neighbour[p_margin] = p_neighbour;
  1906. }
  1907. NodePath Control::get_focus_neighbour(Margin p_margin) const {
  1908. ERR_FAIL_INDEX_V((int)p_margin, 4, NodePath());
  1909. return data.focus_neighbour[p_margin];
  1910. }
  1911. void Control::set_focus_next(const NodePath &p_next) {
  1912. data.focus_next = p_next;
  1913. }
  1914. NodePath Control::get_focus_next() const {
  1915. return data.focus_next;
  1916. }
  1917. void Control::set_focus_previous(const NodePath &p_prev) {
  1918. data.focus_prev = p_prev;
  1919. }
  1920. NodePath Control::get_focus_previous() const {
  1921. return data.focus_prev;
  1922. }
  1923. #define MAX_NEIGHBOUR_SEARCH_COUNT 512
  1924. Control *Control::_get_focus_neighbour(Margin p_margin, int p_count) {
  1925. ERR_FAIL_INDEX_V((int)p_margin, 4, nullptr);
  1926. if (p_count >= MAX_NEIGHBOUR_SEARCH_COUNT) {
  1927. return nullptr;
  1928. }
  1929. if (!data.focus_neighbour[p_margin].is_empty()) {
  1930. Control *c = nullptr;
  1931. Node *n = get_node(data.focus_neighbour[p_margin]);
  1932. if (n) {
  1933. c = Object::cast_to<Control>(n);
  1934. ERR_FAIL_COND_V_MSG(!c, nullptr, "Neighbor focus node is not a control: " + n->get_name() + ".");
  1935. } else {
  1936. return nullptr;
  1937. }
  1938. bool valid = true;
  1939. if (!c->is_visible()) {
  1940. valid = false;
  1941. }
  1942. if (c->get_focus_mode() == FOCUS_NONE) {
  1943. valid = false;
  1944. }
  1945. if (valid) {
  1946. return c;
  1947. }
  1948. c = c->_get_focus_neighbour(p_margin, p_count + 1);
  1949. return c;
  1950. }
  1951. float dist = 1e7;
  1952. Control *result = nullptr;
  1953. Point2 points[4];
  1954. Transform2D xform = get_global_transform();
  1955. points[0] = xform.xform(Point2());
  1956. points[1] = xform.xform(Point2(get_size().x, 0));
  1957. points[2] = xform.xform(get_size());
  1958. points[3] = xform.xform(Point2(0, get_size().y));
  1959. const Vector2 dir[4] = {
  1960. Vector2(-1, 0),
  1961. Vector2(0, -1),
  1962. Vector2(1, 0),
  1963. Vector2(0, 1)
  1964. };
  1965. Vector2 vdir = dir[p_margin];
  1966. float maxd = -1e7;
  1967. for (int i = 0; i < 4; i++) {
  1968. float d = vdir.dot(points[i]);
  1969. if (d > maxd) {
  1970. maxd = d;
  1971. }
  1972. }
  1973. Node *base = this;
  1974. while (base) {
  1975. Control *c = Object::cast_to<Control>(base);
  1976. if (c) {
  1977. if (c->data.SI) {
  1978. break;
  1979. }
  1980. if (c->data.RI) {
  1981. break;
  1982. }
  1983. }
  1984. base = base->get_parent();
  1985. }
  1986. if (!base) {
  1987. return nullptr;
  1988. }
  1989. _window_find_focus_neighbour(vdir, base, points, maxd, dist, &result);
  1990. return result;
  1991. }
  1992. void Control::_window_find_focus_neighbour(const Vector2 &p_dir, Node *p_at, const Point2 *p_points, float p_min, float &r_closest_dist, Control **r_closest) {
  1993. if (Object::cast_to<Viewport>(p_at)) {
  1994. return; //bye
  1995. }
  1996. Control *c = Object::cast_to<Control>(p_at);
  1997. if (c && c != this && c->get_focus_mode() == FOCUS_ALL && c->is_visible_in_tree()) {
  1998. Point2 points[4];
  1999. Transform2D xform = c->get_global_transform();
  2000. points[0] = xform.xform(Point2());
  2001. points[1] = xform.xform(Point2(c->get_size().x, 0));
  2002. points[2] = xform.xform(c->get_size());
  2003. points[3] = xform.xform(Point2(0, c->get_size().y));
  2004. float min = 1e7;
  2005. for (int i = 0; i < 4; i++) {
  2006. float d = p_dir.dot(points[i]);
  2007. if (d < min) {
  2008. min = d;
  2009. }
  2010. }
  2011. if (min > (p_min - CMP_EPSILON)) {
  2012. for (int i = 0; i < 4; i++) {
  2013. Vector2 la = p_points[i];
  2014. Vector2 lb = p_points[(i + 1) % 4];
  2015. for (int j = 0; j < 4; j++) {
  2016. Vector2 fa = points[j];
  2017. Vector2 fb = points[(j + 1) % 4];
  2018. Vector2 pa, pb;
  2019. float d = Geometry::get_closest_points_between_segments(la, lb, fa, fb, pa, pb);
  2020. //float d = Geometry::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));
  2021. if (d < r_closest_dist) {
  2022. r_closest_dist = d;
  2023. *r_closest = c;
  2024. }
  2025. }
  2026. }
  2027. }
  2028. }
  2029. for (int i = 0; i < p_at->get_child_count(); i++) {
  2030. Node *child = p_at->get_child(i);
  2031. Control *childc = Object::cast_to<Control>(child);
  2032. if (childc && childc->data.SI) {
  2033. continue; //subwindow, ignore
  2034. }
  2035. _window_find_focus_neighbour(p_dir, p_at->get_child(i), p_points, p_min, r_closest_dist, r_closest);
  2036. }
  2037. }
  2038. void Control::set_h_size_flags(int p_flags) {
  2039. if (data.h_size_flags == p_flags) {
  2040. return;
  2041. }
  2042. data.h_size_flags = p_flags;
  2043. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  2044. }
  2045. int Control::get_h_size_flags() const {
  2046. return data.h_size_flags;
  2047. }
  2048. void Control::set_v_size_flags(int p_flags) {
  2049. if (data.v_size_flags == p_flags) {
  2050. return;
  2051. }
  2052. data.v_size_flags = p_flags;
  2053. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  2054. }
  2055. void Control::set_stretch_ratio(float p_ratio) {
  2056. if (data.expand == p_ratio) {
  2057. return;
  2058. }
  2059. data.expand = p_ratio;
  2060. emit_signal(SceneStringNames::get_singleton()->size_flags_changed);
  2061. }
  2062. float Control::get_stretch_ratio() const {
  2063. return data.expand;
  2064. }
  2065. void Control::grab_click_focus() {
  2066. ERR_FAIL_COND(!is_inside_tree());
  2067. get_viewport()->_gui_grab_click_focus(this);
  2068. }
  2069. void Control::minimum_size_changed() {
  2070. if (!is_inside_tree() || data.block_minimum_size_adjust) {
  2071. return;
  2072. }
  2073. Control *invalidate = this;
  2074. //invalidate cache upwards
  2075. while (invalidate && invalidate->data.minimum_size_valid) {
  2076. invalidate->data.minimum_size_valid = false;
  2077. if (invalidate->is_set_as_toplevel()) {
  2078. break; // do not go further up
  2079. }
  2080. invalidate = invalidate->data.parent;
  2081. }
  2082. if (!is_visible_in_tree()) {
  2083. return;
  2084. }
  2085. if (data.updating_last_minimum_size) {
  2086. return;
  2087. }
  2088. data.updating_last_minimum_size = true;
  2089. MessageQueue::get_singleton()->push_call(this, "_update_minimum_size");
  2090. }
  2091. int Control::get_v_size_flags() const {
  2092. return data.v_size_flags;
  2093. }
  2094. void Control::set_mouse_filter(MouseFilter p_filter) {
  2095. ERR_FAIL_INDEX(p_filter, 3);
  2096. data.mouse_filter = p_filter;
  2097. update_configuration_warning();
  2098. }
  2099. Control::MouseFilter Control::get_mouse_filter() const {
  2100. return data.mouse_filter;
  2101. }
  2102. void Control::set_pass_on_modal_close_click(bool p_pass_on) {
  2103. data.pass_on_modal_close_click = p_pass_on;
  2104. }
  2105. bool Control::get_pass_on_modal_close_click() const {
  2106. return data.pass_on_modal_close_click;
  2107. }
  2108. Control *Control::get_focus_owner() const {
  2109. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  2110. return get_viewport()->_gui_get_focus_owner();
  2111. }
  2112. void Control::warp_mouse(const Point2 &p_to_pos) {
  2113. ERR_FAIL_COND(!is_inside_tree());
  2114. get_viewport()->warp_mouse(get_global_transform().xform(p_to_pos));
  2115. }
  2116. bool Control::is_text_field() const {
  2117. return false;
  2118. }
  2119. void Control::set_rotation(float p_radians) {
  2120. data.rotation = p_radians;
  2121. update();
  2122. _notify_transform();
  2123. _change_notify("rect_rotation");
  2124. }
  2125. float Control::get_rotation() const {
  2126. return data.rotation;
  2127. }
  2128. void Control::set_rotation_degrees(float p_degrees) {
  2129. set_rotation(Math::deg2rad(p_degrees));
  2130. }
  2131. float Control::get_rotation_degrees() const {
  2132. return Math::rad2deg(get_rotation());
  2133. }
  2134. void Control::_override_changed() {
  2135. notification(NOTIFICATION_THEME_CHANGED);
  2136. minimum_size_changed(); // overrides are likely to affect minimum size
  2137. }
  2138. void Control::set_pivot_offset(const Vector2 &p_pivot) {
  2139. data.pivot_offset = p_pivot;
  2140. update();
  2141. _notify_transform();
  2142. _change_notify("rect_pivot_offset");
  2143. }
  2144. Vector2 Control::get_pivot_offset() const {
  2145. return data.pivot_offset;
  2146. }
  2147. void Control::set_scale(const Vector2 &p_scale) {
  2148. data.scale = p_scale;
  2149. // Avoid having 0 scale values, can lead to errors in physics and rendering.
  2150. if (data.scale.x == 0) {
  2151. data.scale.x = CMP_EPSILON;
  2152. }
  2153. if (data.scale.y == 0) {
  2154. data.scale.y = CMP_EPSILON;
  2155. }
  2156. update();
  2157. _notify_transform();
  2158. _change_notify("rect_scale");
  2159. }
  2160. Vector2 Control::get_scale() const {
  2161. return data.scale;
  2162. }
  2163. Control *Control::get_root_parent_control() const {
  2164. const CanvasItem *ci = this;
  2165. const Control *root = this;
  2166. while (ci) {
  2167. const Control *c = Object::cast_to<Control>(ci);
  2168. if (c) {
  2169. root = c;
  2170. if (c->data.RI || c->data.MI || c->is_toplevel_control()) {
  2171. break;
  2172. }
  2173. }
  2174. ci = ci->get_parent_item();
  2175. }
  2176. return const_cast<Control *>(root);
  2177. }
  2178. void Control::set_block_minimum_size_adjust(bool p_block) {
  2179. data.block_minimum_size_adjust = p_block;
  2180. }
  2181. bool Control::is_minimum_size_adjust_blocked() const {
  2182. return data.block_minimum_size_adjust;
  2183. }
  2184. void Control::set_disable_visibility_clip(bool p_ignore) {
  2185. data.disable_visibility_clip = p_ignore;
  2186. update();
  2187. }
  2188. bool Control::is_visibility_clip_disabled() const {
  2189. return data.disable_visibility_clip;
  2190. }
  2191. void Control::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  2192. #ifdef TOOLS_ENABLED
  2193. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\"";
  2194. #else
  2195. const String quote_style = "\"";
  2196. #endif
  2197. Node::get_argument_options(p_function, p_idx, r_options);
  2198. if (p_idx == 0) {
  2199. List<StringName> sn;
  2200. String pf = p_function;
  2201. if (pf == "add_color_override" || pf == "has_color" || pf == "has_color_override" || pf == "get_color") {
  2202. Theme::get_default()->get_color_list(get_class(), &sn);
  2203. } else if (pf == "add_style_override" || pf == "has_style" || pf == "has_style_override" || pf == "get_style") {
  2204. Theme::get_default()->get_stylebox_list(get_class(), &sn);
  2205. } else if (pf == "add_font_override" || pf == "has_font" || pf == "has_font_override" || pf == "get_font") {
  2206. Theme::get_default()->get_font_list(get_class(), &sn);
  2207. } else if (pf == "add_constant_override" || pf == "has_constant" || pf == "has_constant_override" || pf == "get_constant") {
  2208. Theme::get_default()->get_constant_list(get_class(), &sn);
  2209. }
  2210. sn.sort_custom<StringName::AlphCompare>();
  2211. for (List<StringName>::Element *E = sn.front(); E; E = E->next()) {
  2212. r_options->push_back(quote_style + E->get() + quote_style);
  2213. }
  2214. }
  2215. }
  2216. String Control::get_configuration_warning() const {
  2217. String warning = CanvasItem::get_configuration_warning();
  2218. if (data.mouse_filter == MOUSE_FILTER_IGNORE && data.tooltip != "") {
  2219. if (warning != String()) {
  2220. warning += "\n\n";
  2221. }
  2222. warning += TTR("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\".");
  2223. }
  2224. return warning;
  2225. }
  2226. void Control::set_clip_contents(bool p_clip) {
  2227. data.clip_contents = p_clip;
  2228. update();
  2229. }
  2230. bool Control::is_clipping_contents() {
  2231. return data.clip_contents;
  2232. }
  2233. void Control::set_h_grow_direction(GrowDirection p_direction) {
  2234. ERR_FAIL_INDEX((int)p_direction, 3);
  2235. data.h_grow = p_direction;
  2236. _size_changed();
  2237. }
  2238. Control::GrowDirection Control::get_h_grow_direction() const {
  2239. return data.h_grow;
  2240. }
  2241. void Control::set_v_grow_direction(GrowDirection p_direction) {
  2242. ERR_FAIL_INDEX((int)p_direction, 3);
  2243. data.v_grow = p_direction;
  2244. _size_changed();
  2245. }
  2246. Control::GrowDirection Control::get_v_grow_direction() const {
  2247. return data.v_grow;
  2248. }
  2249. void Control::_bind_methods() {
  2250. //ClassDB::bind_method(D_METHOD("_window_resize_event"),&Control::_window_resize_event);
  2251. ClassDB::bind_method(D_METHOD("_size_changed"), &Control::_size_changed);
  2252. ClassDB::bind_method(D_METHOD("_update_minimum_size"), &Control::_update_minimum_size);
  2253. ClassDB::bind_method(D_METHOD("accept_event"), &Control::accept_event);
  2254. ClassDB::bind_method(D_METHOD("get_minimum_size"), &Control::get_minimum_size);
  2255. ClassDB::bind_method(D_METHOD("get_combined_minimum_size"), &Control::get_combined_minimum_size);
  2256. ClassDB::bind_method(D_METHOD("set_anchors_preset", "preset", "keep_margins"), &Control::set_anchors_preset, DEFVAL(false));
  2257. ClassDB::bind_method(D_METHOD("set_margins_preset", "preset", "resize_mode", "margin"), &Control::set_margins_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
  2258. ClassDB::bind_method(D_METHOD("set_anchors_and_margins_preset", "preset", "resize_mode", "margin"), &Control::set_anchors_and_margins_preset, DEFVAL(PRESET_MODE_MINSIZE), DEFVAL(0));
  2259. ClassDB::bind_method(D_METHOD("_set_anchor", "margin", "anchor"), &Control::_set_anchor);
  2260. ClassDB::bind_method(D_METHOD("set_anchor", "margin", "anchor", "keep_margin", "push_opposite_anchor"), &Control::set_anchor, DEFVAL(false), DEFVAL(true));
  2261. ClassDB::bind_method(D_METHOD("get_anchor", "margin"), &Control::get_anchor);
  2262. ClassDB::bind_method(D_METHOD("set_margin", "margin", "offset"), &Control::set_margin);
  2263. ClassDB::bind_method(D_METHOD("set_anchor_and_margin", "margin", "anchor", "offset", "push_opposite_anchor"), &Control::set_anchor_and_margin, DEFVAL(false));
  2264. ClassDB::bind_method(D_METHOD("set_begin", "position"), &Control::set_begin);
  2265. ClassDB::bind_method(D_METHOD("set_end", "position"), &Control::set_end);
  2266. ClassDB::bind_method(D_METHOD("set_position", "position", "keep_margins"), &Control::set_position, DEFVAL(false));
  2267. ClassDB::bind_method(D_METHOD("_set_position", "margin"), &Control::_set_position);
  2268. ClassDB::bind_method(D_METHOD("set_size", "size", "keep_margins"), &Control::set_size, DEFVAL(false));
  2269. ClassDB::bind_method(D_METHOD("_set_size", "size"), &Control::_set_size);
  2270. ClassDB::bind_method(D_METHOD("set_custom_minimum_size", "size"), &Control::set_custom_minimum_size);
  2271. ClassDB::bind_method(D_METHOD("set_global_position", "position", "keep_margins"), &Control::set_global_position, DEFVAL(false));
  2272. ClassDB::bind_method(D_METHOD("_set_global_position", "position"), &Control::_set_global_position);
  2273. ClassDB::bind_method(D_METHOD("set_rotation", "radians"), &Control::set_rotation);
  2274. ClassDB::bind_method(D_METHOD("set_rotation_degrees", "degrees"), &Control::set_rotation_degrees);
  2275. ClassDB::bind_method(D_METHOD("set_scale", "scale"), &Control::set_scale);
  2276. ClassDB::bind_method(D_METHOD("set_pivot_offset", "pivot_offset"), &Control::set_pivot_offset);
  2277. ClassDB::bind_method(D_METHOD("get_margin", "margin"), &Control::get_margin);
  2278. ClassDB::bind_method(D_METHOD("get_begin"), &Control::get_begin);
  2279. ClassDB::bind_method(D_METHOD("get_end"), &Control::get_end);
  2280. ClassDB::bind_method(D_METHOD("get_position"), &Control::get_position);
  2281. ClassDB::bind_method(D_METHOD("get_size"), &Control::get_size);
  2282. ClassDB::bind_method(D_METHOD("get_rotation"), &Control::get_rotation);
  2283. ClassDB::bind_method(D_METHOD("get_rotation_degrees"), &Control::get_rotation_degrees);
  2284. ClassDB::bind_method(D_METHOD("get_scale"), &Control::get_scale);
  2285. ClassDB::bind_method(D_METHOD("get_pivot_offset"), &Control::get_pivot_offset);
  2286. ClassDB::bind_method(D_METHOD("get_custom_minimum_size"), &Control::get_custom_minimum_size);
  2287. ClassDB::bind_method(D_METHOD("get_parent_area_size"), &Control::get_parent_area_size);
  2288. ClassDB::bind_method(D_METHOD("get_global_position"), &Control::get_global_position);
  2289. ClassDB::bind_method(D_METHOD("get_rect"), &Control::get_rect);
  2290. ClassDB::bind_method(D_METHOD("get_global_rect"), &Control::get_global_rect);
  2291. ClassDB::bind_method(D_METHOD("show_modal", "exclusive"), &Control::show_modal, DEFVAL(false));
  2292. ClassDB::bind_method(D_METHOD("set_focus_mode", "mode"), &Control::set_focus_mode);
  2293. ClassDB::bind_method(D_METHOD("get_focus_mode"), &Control::get_focus_mode);
  2294. ClassDB::bind_method(D_METHOD("has_focus"), &Control::has_focus);
  2295. ClassDB::bind_method(D_METHOD("grab_focus"), &Control::grab_focus);
  2296. ClassDB::bind_method(D_METHOD("release_focus"), &Control::release_focus);
  2297. ClassDB::bind_method(D_METHOD("find_prev_valid_focus"), &Control::find_prev_valid_focus);
  2298. ClassDB::bind_method(D_METHOD("find_next_valid_focus"), &Control::find_next_valid_focus);
  2299. ClassDB::bind_method(D_METHOD("get_focus_owner"), &Control::get_focus_owner);
  2300. ClassDB::bind_method(D_METHOD("set_h_size_flags", "flags"), &Control::set_h_size_flags);
  2301. ClassDB::bind_method(D_METHOD("get_h_size_flags"), &Control::get_h_size_flags);
  2302. ClassDB::bind_method(D_METHOD("set_stretch_ratio", "ratio"), &Control::set_stretch_ratio);
  2303. ClassDB::bind_method(D_METHOD("get_stretch_ratio"), &Control::get_stretch_ratio);
  2304. ClassDB::bind_method(D_METHOD("set_v_size_flags", "flags"), &Control::set_v_size_flags);
  2305. ClassDB::bind_method(D_METHOD("get_v_size_flags"), &Control::get_v_size_flags);
  2306. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Control::set_theme);
  2307. ClassDB::bind_method(D_METHOD("get_theme"), &Control::get_theme);
  2308. ClassDB::bind_method(D_METHOD("add_icon_override", "name", "texture"), &Control::add_icon_override);
  2309. ClassDB::bind_method(D_METHOD("add_shader_override", "name", "shader"), &Control::add_shader_override);
  2310. ClassDB::bind_method(D_METHOD("add_stylebox_override", "name", "stylebox"), &Control::add_style_override);
  2311. ClassDB::bind_method(D_METHOD("add_font_override", "name", "font"), &Control::add_font_override);
  2312. ClassDB::bind_method(D_METHOD("add_color_override", "name", "color"), &Control::add_color_override);
  2313. ClassDB::bind_method(D_METHOD("add_constant_override", "name", "constant"), &Control::add_constant_override);
  2314. ClassDB::bind_method(D_METHOD("get_icon", "name", "theme_type"), &Control::get_icon, DEFVAL(""));
  2315. ClassDB::bind_method(D_METHOD("get_stylebox", "name", "theme_type"), &Control::get_stylebox, DEFVAL(""));
  2316. ClassDB::bind_method(D_METHOD("get_font", "name", "theme_type"), &Control::get_font, DEFVAL(""));
  2317. ClassDB::bind_method(D_METHOD("get_color", "name", "theme_type"), &Control::get_color, DEFVAL(""));
  2318. ClassDB::bind_method(D_METHOD("get_constant", "name", "theme_type"), &Control::get_constant, DEFVAL(""));
  2319. ClassDB::bind_method(D_METHOD("has_icon_override", "name"), &Control::has_icon_override);
  2320. ClassDB::bind_method(D_METHOD("has_shader_override", "name"), &Control::has_shader_override);
  2321. ClassDB::bind_method(D_METHOD("has_stylebox_override", "name"), &Control::has_stylebox_override);
  2322. ClassDB::bind_method(D_METHOD("has_font_override", "name"), &Control::has_font_override);
  2323. ClassDB::bind_method(D_METHOD("has_color_override", "name"), &Control::has_color_override);
  2324. ClassDB::bind_method(D_METHOD("has_constant_override", "name"), &Control::has_constant_override);
  2325. ClassDB::bind_method(D_METHOD("has_icon", "name", "theme_type"), &Control::has_icon, DEFVAL(""));
  2326. ClassDB::bind_method(D_METHOD("has_stylebox", "name", "theme_type"), &Control::has_stylebox, DEFVAL(""));
  2327. ClassDB::bind_method(D_METHOD("has_font", "name", "theme_type"), &Control::has_font, DEFVAL(""));
  2328. ClassDB::bind_method(D_METHOD("has_color", "name", "theme_type"), &Control::has_color, DEFVAL(""));
  2329. ClassDB::bind_method(D_METHOD("has_constant", "name", "theme_type"), &Control::has_constant, DEFVAL(""));
  2330. ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Control::get_theme_default_font);
  2331. ClassDB::bind_method(D_METHOD("get_parent_control"), &Control::get_parent_control);
  2332. ClassDB::bind_method(D_METHOD("set_h_grow_direction", "direction"), &Control::set_h_grow_direction);
  2333. ClassDB::bind_method(D_METHOD("get_h_grow_direction"), &Control::get_h_grow_direction);
  2334. ClassDB::bind_method(D_METHOD("set_v_grow_direction", "direction"), &Control::set_v_grow_direction);
  2335. ClassDB::bind_method(D_METHOD("get_v_grow_direction"), &Control::get_v_grow_direction);
  2336. ClassDB::bind_method(D_METHOD("set_tooltip", "tooltip"), &Control::set_tooltip);
  2337. ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2()));
  2338. ClassDB::bind_method(D_METHOD("_get_tooltip"), &Control::_get_tooltip);
  2339. ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Control::set_default_cursor_shape);
  2340. ClassDB::bind_method(D_METHOD("get_default_cursor_shape"), &Control::get_default_cursor_shape);
  2341. ClassDB::bind_method(D_METHOD("get_cursor_shape", "position"), &Control::get_cursor_shape, DEFVAL(Point2()));
  2342. ClassDB::bind_method(D_METHOD("set_focus_neighbour", "margin", "neighbour"), &Control::set_focus_neighbour);
  2343. ClassDB::bind_method(D_METHOD("get_focus_neighbour", "margin"), &Control::get_focus_neighbour);
  2344. ClassDB::bind_method(D_METHOD("set_focus_next", "next"), &Control::set_focus_next);
  2345. ClassDB::bind_method(D_METHOD("get_focus_next"), &Control::get_focus_next);
  2346. ClassDB::bind_method(D_METHOD("set_focus_previous", "previous"), &Control::set_focus_previous);
  2347. ClassDB::bind_method(D_METHOD("get_focus_previous"), &Control::get_focus_previous);
  2348. ClassDB::bind_method(D_METHOD("force_drag", "data", "preview"), &Control::force_drag);
  2349. ClassDB::bind_method(D_METHOD("set_mouse_filter", "filter"), &Control::set_mouse_filter);
  2350. ClassDB::bind_method(D_METHOD("get_mouse_filter"), &Control::get_mouse_filter);
  2351. ClassDB::bind_method(D_METHOD("set_pass_on_modal_close_click", "enabled"), &Control::set_pass_on_modal_close_click);
  2352. ClassDB::bind_method(D_METHOD("get_pass_on_modal_close_click"), &Control::get_pass_on_modal_close_click);
  2353. ClassDB::bind_method(D_METHOD("set_clip_contents", "enable"), &Control::set_clip_contents);
  2354. ClassDB::bind_method(D_METHOD("is_clipping_contents"), &Control::is_clipping_contents);
  2355. ClassDB::bind_method(D_METHOD("grab_click_focus"), &Control::grab_click_focus);
  2356. ClassDB::bind_method(D_METHOD("set_drag_forwarding", "target"), &Control::set_drag_forwarding);
  2357. ClassDB::bind_method(D_METHOD("set_drag_preview", "control"), &Control::set_drag_preview);
  2358. ClassDB::bind_method(D_METHOD("warp_mouse", "to_position"), &Control::warp_mouse);
  2359. ClassDB::bind_method(D_METHOD("minimum_size_changed"), &Control::minimum_size_changed);
  2360. ClassDB::bind_method(D_METHOD("_theme_changed"), &Control::_theme_changed);
  2361. ClassDB::bind_method(D_METHOD("_override_changed"), &Control::_override_changed);
  2362. BIND_VMETHOD(MethodInfo("_gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2363. BIND_VMETHOD(MethodInfo(Variant::VECTOR2, "_get_minimum_size"));
  2364. MethodInfo get_drag_data = MethodInfo("get_drag_data", PropertyInfo(Variant::VECTOR2, "position"));
  2365. get_drag_data.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  2366. BIND_VMETHOD(get_drag_data);
  2367. BIND_VMETHOD(MethodInfo(Variant::BOOL, "can_drop_data", PropertyInfo(Variant::VECTOR2, "position"), PropertyInfo(Variant::NIL, "data")));
  2368. BIND_VMETHOD(MethodInfo("drop_data", PropertyInfo(Variant::VECTOR2, "position"), PropertyInfo(Variant::NIL, "data")));
  2369. BIND_VMETHOD(MethodInfo(
  2370. PropertyInfo(Variant::OBJECT, "control", PROPERTY_HINT_RESOURCE_TYPE, "Control"),
  2371. "_make_custom_tooltip", PropertyInfo(Variant::STRING, "for_text")));
  2372. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_clips_input"));
  2373. ADD_GROUP("Anchor", "anchor_");
  2374. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_left", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_LEFT);
  2375. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_top", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_TOP);
  2376. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_right", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_RIGHT);
  2377. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anchor_bottom", PROPERTY_HINT_RANGE, "0,1,0.001,or_lesser,or_greater"), "_set_anchor", "get_anchor", MARGIN_BOTTOM);
  2378. ADD_GROUP("Margin", "margin_");
  2379. ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_left", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_LEFT);
  2380. ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_top", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_TOP);
  2381. ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_right", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_RIGHT);
  2382. ADD_PROPERTYI(PropertyInfo(Variant::INT, "margin_bottom", PROPERTY_HINT_RANGE, "-4096,4096"), "set_margin", "get_margin", MARGIN_BOTTOM);
  2383. ADD_GROUP("Grow Direction", "grow_");
  2384. ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_horizontal", PROPERTY_HINT_ENUM, "Begin,End,Both"), "set_h_grow_direction", "get_h_grow_direction");
  2385. ADD_PROPERTY(PropertyInfo(Variant::INT, "grow_vertical", PROPERTY_HINT_ENUM, "Begin,End,Both"), "set_v_grow_direction", "get_v_grow_direction");
  2386. ADD_GROUP("Rect", "rect_");
  2387. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_position", "get_position");
  2388. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_global_position", PROPERTY_HINT_NONE, "", 0), "_set_global_position", "get_global_position");
  2389. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "_set_size", "get_size");
  2390. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_min_size"), "set_custom_minimum_size", "get_custom_minimum_size");
  2391. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rect_rotation", PROPERTY_HINT_RANGE, "-360,360,0.1,or_lesser,or_greater"), "set_rotation_degrees", "get_rotation_degrees");
  2392. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_scale"), "set_scale", "get_scale");
  2393. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "rect_pivot_offset"), "set_pivot_offset", "get_pivot_offset");
  2394. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rect_clip_content"), "set_clip_contents", "is_clipping_contents");
  2395. ADD_GROUP("Hint", "hint_");
  2396. ADD_PROPERTY(PropertyInfo(Variant::STRING, "hint_tooltip", PROPERTY_HINT_MULTILINE_TEXT), "set_tooltip", "_get_tooltip");
  2397. ADD_GROUP("Focus", "focus_");
  2398. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_left", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_LEFT);
  2399. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_top", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_TOP);
  2400. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_right", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_RIGHT);
  2401. ADD_PROPERTYI(PropertyInfo(Variant::NODE_PATH, "focus_neighbour_bottom", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_neighbour", "get_focus_neighbour", MARGIN_BOTTOM);
  2402. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_next", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_next", "get_focus_next");
  2403. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "focus_previous", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Control"), "set_focus_previous", "get_focus_previous");
  2404. ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode");
  2405. ADD_GROUP("Mouse", "mouse_");
  2406. ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_filter", PROPERTY_HINT_ENUM, "Stop,Pass,Ignore"), "set_mouse_filter", "get_mouse_filter");
  2407. ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_default_cursor_shape", PROPERTY_HINT_ENUM, "Arrow,Ibeam,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");
  2408. ADD_GROUP("Input", "input_");
  2409. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_pass_on_modal_close_click"), "set_pass_on_modal_close_click", "get_pass_on_modal_close_click");
  2410. ADD_GROUP("Size Flags", "size_flags_");
  2411. ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_horizontal", PROPERTY_HINT_FLAGS, "Fill,Expand,Shrink Center,Shrink End"), "set_h_size_flags", "get_h_size_flags");
  2412. ADD_PROPERTY(PropertyInfo(Variant::INT, "size_flags_vertical", PROPERTY_HINT_FLAGS, "Fill,Expand,Shrink Center,Shrink End"), "set_v_size_flags", "get_v_size_flags");
  2413. ADD_PROPERTY(PropertyInfo(Variant::REAL, "size_flags_stretch_ratio", PROPERTY_HINT_RANGE, "0,20,0.01,or_greater"), "set_stretch_ratio", "get_stretch_ratio");
  2414. ADD_GROUP("Theme", "");
  2415. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  2416. BIND_ENUM_CONSTANT(FOCUS_NONE);
  2417. BIND_ENUM_CONSTANT(FOCUS_CLICK);
  2418. BIND_ENUM_CONSTANT(FOCUS_ALL);
  2419. BIND_CONSTANT(NOTIFICATION_RESIZED);
  2420. BIND_CONSTANT(NOTIFICATION_MOUSE_ENTER);
  2421. BIND_CONSTANT(NOTIFICATION_MOUSE_EXIT);
  2422. BIND_CONSTANT(NOTIFICATION_FOCUS_ENTER);
  2423. BIND_CONSTANT(NOTIFICATION_FOCUS_EXIT);
  2424. BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
  2425. BIND_CONSTANT(NOTIFICATION_MODAL_CLOSE);
  2426. BIND_CONSTANT(NOTIFICATION_SCROLL_BEGIN);
  2427. BIND_CONSTANT(NOTIFICATION_SCROLL_END);
  2428. BIND_ENUM_CONSTANT(CURSOR_ARROW);
  2429. BIND_ENUM_CONSTANT(CURSOR_IBEAM);
  2430. BIND_ENUM_CONSTANT(CURSOR_POINTING_HAND);
  2431. BIND_ENUM_CONSTANT(CURSOR_CROSS);
  2432. BIND_ENUM_CONSTANT(CURSOR_WAIT);
  2433. BIND_ENUM_CONSTANT(CURSOR_BUSY);
  2434. BIND_ENUM_CONSTANT(CURSOR_DRAG);
  2435. BIND_ENUM_CONSTANT(CURSOR_CAN_DROP);
  2436. BIND_ENUM_CONSTANT(CURSOR_FORBIDDEN);
  2437. BIND_ENUM_CONSTANT(CURSOR_VSIZE);
  2438. BIND_ENUM_CONSTANT(CURSOR_HSIZE);
  2439. BIND_ENUM_CONSTANT(CURSOR_BDIAGSIZE);
  2440. BIND_ENUM_CONSTANT(CURSOR_FDIAGSIZE);
  2441. BIND_ENUM_CONSTANT(CURSOR_MOVE);
  2442. BIND_ENUM_CONSTANT(CURSOR_VSPLIT);
  2443. BIND_ENUM_CONSTANT(CURSOR_HSPLIT);
  2444. BIND_ENUM_CONSTANT(CURSOR_HELP);
  2445. BIND_ENUM_CONSTANT(PRESET_TOP_LEFT);
  2446. BIND_ENUM_CONSTANT(PRESET_TOP_RIGHT);
  2447. BIND_ENUM_CONSTANT(PRESET_BOTTOM_LEFT);
  2448. BIND_ENUM_CONSTANT(PRESET_BOTTOM_RIGHT);
  2449. BIND_ENUM_CONSTANT(PRESET_CENTER_LEFT);
  2450. BIND_ENUM_CONSTANT(PRESET_CENTER_TOP);
  2451. BIND_ENUM_CONSTANT(PRESET_CENTER_RIGHT);
  2452. BIND_ENUM_CONSTANT(PRESET_CENTER_BOTTOM);
  2453. BIND_ENUM_CONSTANT(PRESET_CENTER);
  2454. BIND_ENUM_CONSTANT(PRESET_LEFT_WIDE);
  2455. BIND_ENUM_CONSTANT(PRESET_TOP_WIDE);
  2456. BIND_ENUM_CONSTANT(PRESET_RIGHT_WIDE);
  2457. BIND_ENUM_CONSTANT(PRESET_BOTTOM_WIDE);
  2458. BIND_ENUM_CONSTANT(PRESET_VCENTER_WIDE);
  2459. BIND_ENUM_CONSTANT(PRESET_HCENTER_WIDE);
  2460. BIND_ENUM_CONSTANT(PRESET_WIDE);
  2461. BIND_ENUM_CONSTANT(PRESET_MODE_MINSIZE);
  2462. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_WIDTH);
  2463. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_HEIGHT);
  2464. BIND_ENUM_CONSTANT(PRESET_MODE_KEEP_SIZE);
  2465. BIND_ENUM_CONSTANT(SIZE_FILL);
  2466. BIND_ENUM_CONSTANT(SIZE_EXPAND);
  2467. BIND_ENUM_CONSTANT(SIZE_EXPAND_FILL);
  2468. BIND_ENUM_CONSTANT(SIZE_SHRINK_CENTER);
  2469. BIND_ENUM_CONSTANT(SIZE_SHRINK_END);
  2470. BIND_ENUM_CONSTANT(MOUSE_FILTER_STOP);
  2471. BIND_ENUM_CONSTANT(MOUSE_FILTER_PASS);
  2472. BIND_ENUM_CONSTANT(MOUSE_FILTER_IGNORE);
  2473. BIND_ENUM_CONSTANT(GROW_DIRECTION_BEGIN);
  2474. BIND_ENUM_CONSTANT(GROW_DIRECTION_END);
  2475. BIND_ENUM_CONSTANT(GROW_DIRECTION_BOTH);
  2476. BIND_ENUM_CONSTANT(ANCHOR_BEGIN);
  2477. BIND_ENUM_CONSTANT(ANCHOR_END);
  2478. ADD_SIGNAL(MethodInfo("resized"));
  2479. ADD_SIGNAL(MethodInfo("gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2480. ADD_SIGNAL(MethodInfo("mouse_entered"));
  2481. ADD_SIGNAL(MethodInfo("mouse_exited"));
  2482. ADD_SIGNAL(MethodInfo("focus_entered"));
  2483. ADD_SIGNAL(MethodInfo("focus_exited"));
  2484. ADD_SIGNAL(MethodInfo("size_flags_changed"));
  2485. ADD_SIGNAL(MethodInfo("minimum_size_changed"));
  2486. ADD_SIGNAL(MethodInfo("modal_closed"));
  2487. BIND_VMETHOD(MethodInfo(Variant::BOOL, "has_point", PropertyInfo(Variant::VECTOR2, "point")));
  2488. }
  2489. Control::Control() {
  2490. data.parent = nullptr;
  2491. data.mouse_filter = MOUSE_FILTER_STOP;
  2492. data.pass_on_modal_close_click = true;
  2493. data.SI = nullptr;
  2494. data.MI = nullptr;
  2495. data.RI = nullptr;
  2496. data.theme_owner = nullptr;
  2497. data.modal_exclusive = false;
  2498. data.default_cursor = CURSOR_ARROW;
  2499. data.h_size_flags = SIZE_FILL;
  2500. data.v_size_flags = SIZE_FILL;
  2501. data.expand = 1;
  2502. data.rotation = 0;
  2503. data.parent_canvas_item = nullptr;
  2504. data.scale = Vector2(1, 1);
  2505. data.drag_owner = 0;
  2506. data.modal_frame = 0;
  2507. data.block_minimum_size_adjust = false;
  2508. data.disable_visibility_clip = false;
  2509. data.h_grow = GROW_DIRECTION_END;
  2510. data.v_grow = GROW_DIRECTION_END;
  2511. data.minimum_size_valid = false;
  2512. data.updating_last_minimum_size = false;
  2513. data.clip_contents = false;
  2514. for (int i = 0; i < 4; i++) {
  2515. data.anchor[i] = ANCHOR_BEGIN;
  2516. data.margin[i] = 0;
  2517. }
  2518. data.focus_mode = FOCUS_NONE;
  2519. data.modal_prev_focus_owner = 0;
  2520. }
  2521. Control::~Control() {
  2522. }