control.cpp 81 KB

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