control.cpp 91 KB

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