2
0

control.cpp 93 KB

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