control.cpp 92 KB

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