control.cpp 91 KB

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