control.cpp 99 KB

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