viewport.cpp 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053
  1. /*************************************************************************/
  2. /* viewport.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "viewport.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_string_names.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/object/message_queue.h"
  35. #include "core/string/translation.h"
  36. #include "core/templates/pair.h"
  37. #include "scene/2d/audio_listener_2d.h"
  38. #include "scene/2d/camera_2d.h"
  39. #include "scene/2d/collision_object_2d.h"
  40. #ifndef _3D_DISABLED
  41. #include "scene/3d/audio_listener_3d.h"
  42. #include "scene/3d/camera_3d.h"
  43. #include "scene/3d/collision_object_3d.h"
  44. #include "scene/3d/world_environment.h"
  45. #endif // _3D_DISABLED
  46. #include "scene/gui/control.h"
  47. #include "scene/gui/label.h"
  48. #include "scene/gui/popup.h"
  49. #include "scene/gui/popup_menu.h"
  50. #include "scene/gui/subviewport_container.h"
  51. #include "scene/main/canvas_layer.h"
  52. #include "scene/main/window.h"
  53. #include "scene/resources/mesh.h"
  54. #include "scene/resources/text_line.h"
  55. #include "scene/resources/world_2d.h"
  56. #include "scene/scene_string_names.h"
  57. #include "servers/audio_server.h"
  58. #include "servers/rendering/rendering_server_globals.h"
  59. void ViewportTexture::setup_local_to_scene() {
  60. Node *local_scene = get_local_scene();
  61. if (!local_scene) {
  62. return;
  63. }
  64. if (vp) {
  65. vp->viewport_textures.erase(this);
  66. }
  67. vp = nullptr;
  68. Node *vpn = local_scene->get_node(path);
  69. ERR_FAIL_COND_MSG(!vpn, "ViewportTexture: Path to node is invalid.");
  70. vp = Object::cast_to<Viewport>(vpn);
  71. ERR_FAIL_COND_MSG(!vp, "ViewportTexture: Path to node does not point to a viewport.");
  72. vp->viewport_textures.insert(this);
  73. if (proxy_ph.is_valid()) {
  74. RS::get_singleton()->texture_proxy_update(proxy, vp->texture_rid);
  75. RS::get_singleton()->free(proxy_ph);
  76. } else {
  77. ERR_FAIL_COND(proxy.is_valid()); // Should be invalid.
  78. proxy = RS::get_singleton()->texture_proxy_create(vp->texture_rid);
  79. }
  80. }
  81. void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) {
  82. if (path == p_path) {
  83. return;
  84. }
  85. path = p_path;
  86. if (get_local_scene()) {
  87. setup_local_to_scene();
  88. }
  89. }
  90. NodePath ViewportTexture::get_viewport_path_in_scene() const {
  91. return path;
  92. }
  93. int ViewportTexture::get_width() const {
  94. ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it.");
  95. return vp->size.width;
  96. }
  97. int ViewportTexture::get_height() const {
  98. ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it.");
  99. return vp->size.height;
  100. }
  101. Size2 ViewportTexture::get_size() const {
  102. ERR_FAIL_COND_V_MSG(!vp, Size2(), "Viewport Texture must be set to use it.");
  103. return vp->size;
  104. }
  105. RID ViewportTexture::get_rid() const {
  106. if (proxy.is_null()) {
  107. proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
  108. proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);
  109. }
  110. return proxy;
  111. }
  112. bool ViewportTexture::has_alpha() const {
  113. return false;
  114. }
  115. Ref<Image> ViewportTexture::get_image() const {
  116. ERR_FAIL_COND_V_MSG(!vp, Ref<Image>(), "Viewport Texture must be set to use it.");
  117. return RS::get_singleton()->texture_2d_get(vp->texture_rid);
  118. }
  119. void ViewportTexture::_bind_methods() {
  120. ClassDB::bind_method(D_METHOD("set_viewport_path_in_scene", "path"), &ViewportTexture::set_viewport_path_in_scene);
  121. ClassDB::bind_method(D_METHOD("get_viewport_path_in_scene"), &ViewportTexture::get_viewport_path_in_scene);
  122. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "viewport_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "SubViewport", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT), "set_viewport_path_in_scene", "get_viewport_path_in_scene");
  123. }
  124. ViewportTexture::ViewportTexture() {
  125. set_local_to_scene(true);
  126. }
  127. ViewportTexture::~ViewportTexture() {
  128. if (vp) {
  129. vp->viewport_textures.erase(this);
  130. }
  131. if (proxy_ph.is_valid()) {
  132. RS::get_singleton()->free(proxy_ph);
  133. }
  134. if (proxy.is_valid()) {
  135. RS::get_singleton()->free(proxy);
  136. }
  137. }
  138. /////////////////////////////////////
  139. // Aliases used to provide custom styles to tooltips in the default
  140. // theme and editor theme.
  141. // TooltipPanel is also used for custom tooltips, while TooltipLabel
  142. // is only relevant for default tooltips.
  143. class TooltipPanel : public PopupPanel {
  144. GDCLASS(TooltipPanel, PopupPanel);
  145. public:
  146. TooltipPanel() {}
  147. };
  148. class TooltipLabel : public Label {
  149. GDCLASS(TooltipLabel, Label);
  150. public:
  151. TooltipLabel() {}
  152. };
  153. /////////////////////////////////////
  154. void Viewport::_sub_window_update_order() {
  155. for (int i = 0; i < gui.sub_windows.size(); i++) {
  156. RS::get_singleton()->canvas_item_set_draw_index(gui.sub_windows[i].canvas_item, i);
  157. }
  158. }
  159. void Viewport::_sub_window_register(Window *p_window) {
  160. ERR_FAIL_COND(!is_inside_tree());
  161. for (int i = 0; i < gui.sub_windows.size(); i++) {
  162. ERR_FAIL_COND(gui.sub_windows[i].window == p_window);
  163. }
  164. if (gui.sub_windows.size() == 0) {
  165. subwindow_canvas = RS::get_singleton()->canvas_create();
  166. RS::get_singleton()->viewport_attach_canvas(viewport, subwindow_canvas);
  167. RS::get_singleton()->viewport_set_canvas_stacking(viewport, subwindow_canvas, SUBWINDOW_CANVAS_LAYER, 0);
  168. }
  169. SubWindow sw;
  170. sw.canvas_item = RS::get_singleton()->canvas_item_create();
  171. RS::get_singleton()->canvas_item_set_parent(sw.canvas_item, subwindow_canvas);
  172. sw.window = p_window;
  173. gui.sub_windows.push_back(sw);
  174. _sub_window_grab_focus(p_window);
  175. RenderingServer::get_singleton()->viewport_set_parent_viewport(p_window->viewport, viewport);
  176. }
  177. void Viewport::_sub_window_update(Window *p_window) {
  178. int index = -1;
  179. for (int i = 0; i < gui.sub_windows.size(); i++) {
  180. if (gui.sub_windows[i].window == p_window) {
  181. index = i;
  182. break;
  183. }
  184. }
  185. ERR_FAIL_COND(index == -1);
  186. const SubWindow &sw = gui.sub_windows[index];
  187. Transform2D pos;
  188. pos.set_origin(p_window->get_position());
  189. RS::get_singleton()->canvas_item_clear(sw.canvas_item);
  190. Rect2i r = Rect2i(p_window->get_position(), sw.window->get_size());
  191. if (!p_window->get_flag(Window::FLAG_BORDERLESS)) {
  192. Ref<StyleBox> panel = p_window->get_theme_stylebox(SNAME("embedded_border"));
  193. panel->draw(sw.canvas_item, r);
  194. // Draw the title bar text.
  195. Ref<Font> title_font = p_window->get_theme_font(SNAME("title_font"));
  196. int font_size = p_window->get_theme_font_size(SNAME("title_font_size"));
  197. Color title_color = p_window->get_theme_color(SNAME("title_color"));
  198. int title_height = p_window->get_theme_constant(SNAME("title_height"));
  199. int close_h_ofs = p_window->get_theme_constant(SNAME("close_h_offset"));
  200. int close_v_ofs = p_window->get_theme_constant(SNAME("close_v_offset"));
  201. TextLine title_text = TextLine(p_window->atr(p_window->get_title()), title_font, font_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale());
  202. title_text.set_width(r.size.width - panel->get_minimum_size().x - close_h_ofs);
  203. title_text.set_direction(p_window->is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  204. int x = (r.size.width - title_text.get_size().x) / 2;
  205. int y = (-title_height - title_text.get_size().y) / 2;
  206. Color font_outline_color = p_window->get_theme_color(SNAME("title_outline_modulate"));
  207. int outline_size = p_window->get_theme_constant(SNAME("title_outline_size"));
  208. if (outline_size > 0 && font_outline_color.a > 0) {
  209. title_text.draw_outline(sw.canvas_item, r.position + Point2(x, y), outline_size, font_outline_color);
  210. }
  211. title_text.draw(sw.canvas_item, r.position + Point2(x, y), title_color);
  212. bool pressed = gui.subwindow_focused == sw.window && gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE && gui.subwindow_drag_close_inside;
  213. Ref<Texture2D> close_icon = p_window->get_theme_icon(pressed ? "close_pressed" : "close");
  214. close_icon->draw(sw.canvas_item, r.position + Vector2(r.size.width - close_h_ofs, -close_v_ofs));
  215. }
  216. RS::get_singleton()->canvas_item_add_texture_rect(sw.canvas_item, r, sw.window->get_texture()->get_rid());
  217. }
  218. void Viewport::_sub_window_grab_focus(Window *p_window) {
  219. if (p_window == nullptr) {
  220. // Release current focus.
  221. if (gui.subwindow_focused) {
  222. gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  223. gui.subwindow_focused = nullptr;
  224. gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
  225. }
  226. Window *this_window = Object::cast_to<Window>(this);
  227. if (this_window) {
  228. this_window->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  229. }
  230. return;
  231. }
  232. int index = -1;
  233. for (int i = 0; i < gui.sub_windows.size(); i++) {
  234. if (gui.sub_windows[i].window == p_window) {
  235. index = i;
  236. break;
  237. }
  238. }
  239. ERR_FAIL_COND(index == -1);
  240. if (p_window->get_flag(Window::FLAG_NO_FOCUS)) {
  241. // Can only move to foreground, but no focus granted.
  242. SubWindow sw = gui.sub_windows[index];
  243. gui.sub_windows.remove_at(index);
  244. gui.sub_windows.push_back(sw);
  245. index = gui.sub_windows.size() - 1;
  246. _sub_window_update_order();
  247. return;
  248. }
  249. if (gui.subwindow_focused) {
  250. if (gui.subwindow_focused == p_window) {
  251. return; // Nothing to do.
  252. }
  253. gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  254. gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
  255. } else {
  256. Window *this_window = Object::cast_to<Window>(this);
  257. if (this_window) {
  258. this_window->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  259. }
  260. }
  261. Window *old_focus = gui.subwindow_focused;
  262. gui.subwindow_focused = p_window;
  263. gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  264. { // Move to foreground.
  265. SubWindow sw = gui.sub_windows[index];
  266. gui.sub_windows.remove_at(index);
  267. gui.sub_windows.push_back(sw);
  268. index = gui.sub_windows.size() - 1;
  269. _sub_window_update_order();
  270. }
  271. if (old_focus) {
  272. _sub_window_update(old_focus);
  273. }
  274. _sub_window_update(p_window);
  275. }
  276. void Viewport::_sub_window_remove(Window *p_window) {
  277. for (int i = 0; i < gui.sub_windows.size(); i++) {
  278. if (gui.sub_windows[i].window == p_window) {
  279. RS::get_singleton()->free(gui.sub_windows[i].canvas_item);
  280. gui.sub_windows.remove_at(i);
  281. break;
  282. }
  283. }
  284. if (gui.sub_windows.size() == 0) {
  285. RS::get_singleton()->free(subwindow_canvas);
  286. subwindow_canvas = RID();
  287. }
  288. if (gui.subwindow_focused == p_window) {
  289. Window *parent_visible = p_window->get_parent_visible_window();
  290. gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
  291. gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
  292. if (parent_visible && parent_visible != this) {
  293. gui.subwindow_focused = parent_visible;
  294. gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  295. } else {
  296. gui.subwindow_focused = nullptr;
  297. Window *this_window = Object::cast_to<Window>(this);
  298. if (this_window) {
  299. this_window->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);
  300. }
  301. }
  302. }
  303. RenderingServer::get_singleton()->viewport_set_parent_viewport(p_window->viewport, p_window->parent ? p_window->parent->viewport : RID());
  304. }
  305. void Viewport::_notification(int p_what) {
  306. switch (p_what) {
  307. case NOTIFICATION_ENTER_TREE: {
  308. if (get_parent()) {
  309. parent = get_parent()->get_viewport();
  310. RenderingServer::get_singleton()->viewport_set_parent_viewport(viewport, parent->get_viewport_rid());
  311. } else {
  312. parent = nullptr;
  313. }
  314. current_canvas = find_world_2d()->get_canvas();
  315. RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
  316. _update_audio_listener_2d();
  317. #ifndef _3D_DISABLED
  318. RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
  319. _update_audio_listener_3d();
  320. #endif // _3D_DISABLED
  321. add_to_group("_viewports");
  322. if (get_tree()->is_debugging_collisions_hint()) {
  323. PhysicsServer2D::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(), get_tree()->get_collision_debug_contact_count());
  324. contact_2d_debug = RenderingServer::get_singleton()->canvas_item_create();
  325. RenderingServer::get_singleton()->canvas_item_set_parent(contact_2d_debug, find_world_2d()->get_canvas());
  326. #ifndef _3D_DISABLED
  327. PhysicsServer3D::get_singleton()->space_set_debug_contacts(find_world_3d()->get_space(), get_tree()->get_collision_debug_contact_count());
  328. contact_3d_debug_multimesh = RenderingServer::get_singleton()->multimesh_create();
  329. RenderingServer::get_singleton()->multimesh_allocate_data(contact_3d_debug_multimesh, get_tree()->get_collision_debug_contact_count(), RS::MULTIMESH_TRANSFORM_3D, false);
  330. RenderingServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, 0);
  331. RenderingServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh, get_tree()->get_debug_contact_mesh()->get_rid());
  332. contact_3d_debug_instance = RenderingServer::get_singleton()->instance_create();
  333. RenderingServer::get_singleton()->instance_set_base(contact_3d_debug_instance, contact_3d_debug_multimesh);
  334. RenderingServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world_3d()->get_scenario());
  335. RenderingServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance, RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true);
  336. #endif // _3D_DISABLED
  337. set_physics_process_internal(true);
  338. }
  339. } break;
  340. case NOTIFICATION_READY: {
  341. #ifndef _3D_DISABLED
  342. if (audio_listener_3d_set.size() && !audio_listener_3d) {
  343. AudioListener3D *first = nullptr;
  344. for (Set<AudioListener3D *>::Element *E = audio_listener_3d_set.front(); E; E = E->next()) {
  345. if (first == nullptr || first->is_greater_than(E->get())) {
  346. first = E->get();
  347. }
  348. }
  349. if (first) {
  350. first->make_current();
  351. }
  352. }
  353. if (camera_3d_set.size() && !camera_3d) {
  354. // There are cameras but no current camera, pick first in tree and make it current.
  355. Camera3D *first = nullptr;
  356. for (Set<Camera3D *>::Element *E = camera_3d_set.front(); E; E = E->next()) {
  357. if (first == nullptr || first->is_greater_than(E->get())) {
  358. first = E->get();
  359. }
  360. }
  361. if (first) {
  362. first->make_current();
  363. }
  364. }
  365. #endif // _3D_DISABLED
  366. } break;
  367. case NOTIFICATION_EXIT_TREE: {
  368. _gui_cancel_tooltip();
  369. RenderingServer::get_singleton()->viewport_set_scenario(viewport, RID());
  370. RenderingServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
  371. if (contact_2d_debug.is_valid()) {
  372. RenderingServer::get_singleton()->free(contact_2d_debug);
  373. contact_2d_debug = RID();
  374. }
  375. if (contact_3d_debug_multimesh.is_valid()) {
  376. RenderingServer::get_singleton()->free(contact_3d_debug_multimesh);
  377. RenderingServer::get_singleton()->free(contact_3d_debug_instance);
  378. contact_3d_debug_instance = RID();
  379. contact_3d_debug_multimesh = RID();
  380. }
  381. remove_from_group("_viewports");
  382. set_physics_process_internal(false);
  383. RS::get_singleton()->viewport_set_active(viewport, false);
  384. RenderingServer::get_singleton()->viewport_set_parent_viewport(viewport, RID());
  385. } break;
  386. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  387. if (!get_tree()) {
  388. return;
  389. }
  390. if (get_tree()->is_debugging_collisions_hint() && contact_2d_debug.is_valid()) {
  391. RenderingServer::get_singleton()->canvas_item_clear(contact_2d_debug);
  392. RenderingServer::get_singleton()->canvas_item_set_draw_index(contact_2d_debug, 0xFFFFF); //very high index
  393. Vector<Vector2> points = PhysicsServer2D::get_singleton()->space_get_contacts(find_world_2d()->get_space());
  394. int point_count = PhysicsServer2D::get_singleton()->space_get_contact_count(find_world_2d()->get_space());
  395. Color ccol = get_tree()->get_debug_collision_contact_color();
  396. for (int i = 0; i < point_count; i++) {
  397. RenderingServer::get_singleton()->canvas_item_add_rect(contact_2d_debug, Rect2(points[i] - Vector2(2, 2), Vector2(5, 5)), ccol);
  398. }
  399. }
  400. #ifndef _3D_DISABLED
  401. if (get_tree()->is_debugging_collisions_hint() && contact_3d_debug_multimesh.is_valid()) {
  402. Vector<Vector3> points = PhysicsServer3D::get_singleton()->space_get_contacts(find_world_3d()->get_space());
  403. int point_count = PhysicsServer3D::get_singleton()->space_get_contact_count(find_world_3d()->get_space());
  404. RS::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, point_count);
  405. for (int i = 0; i < point_count; i++) {
  406. Transform3D point_transform;
  407. point_transform.origin = points[i];
  408. RS::get_singleton()->multimesh_instance_set_transform(contact_3d_debug_multimesh, i, point_transform);
  409. }
  410. }
  411. #endif // _3D_DISABLED
  412. } break;
  413. case NOTIFICATION_VP_MOUSE_ENTER: {
  414. gui.mouse_in_viewport = true;
  415. } break;
  416. case NOTIFICATION_VP_MOUSE_EXIT: {
  417. gui.mouse_in_viewport = false;
  418. _drop_physics_mouseover();
  419. _drop_mouse_over();
  420. // When the mouse exits the viewport, we want to end mouse_over, but
  421. // not mouse_focus, because, for example, we want to continue
  422. // dragging a scrollbar even if the mouse has left the viewport.
  423. } break;
  424. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  425. _drop_physics_mouseover();
  426. if (gui.mouse_focus && !gui.forced_mouse_focus) {
  427. _drop_mouse_focus();
  428. }
  429. // When the window focus changes, we want to end mouse_focus, but
  430. // not the mouse_over. Note: The OS will trigger a separate mouse
  431. // exit event if the change in focus results in the mouse exiting
  432. // the window.
  433. } break;
  434. }
  435. }
  436. void Viewport::_process_picking() {
  437. if (!is_inside_tree()) {
  438. return;
  439. }
  440. if (!physics_object_picking) {
  441. return;
  442. }
  443. if (to_screen_rect != Rect2i() && Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  444. return;
  445. }
  446. _drop_physics_mouseover(true);
  447. #ifndef _3D_DISABLED
  448. Vector2 last_pos(1e20, 1e20);
  449. CollisionObject3D *last_object = nullptr;
  450. ObjectID last_id;
  451. PhysicsDirectSpaceState3D::RayResult result;
  452. #endif // _3D_DISABLED
  453. PhysicsDirectSpaceState2D *ss2d = PhysicsServer2D::get_singleton()->space_get_direct_state(find_world_2d()->get_space());
  454. if (physics_has_last_mousepos) {
  455. // If no mouse event exists, create a motion one. This is necessary because objects or camera may have moved.
  456. // While this extra event is sent, it is checked if both camera and last object and last ID did not move.
  457. // If nothing changed, the event is discarded to avoid flooding with unnecessary motion events every frame.
  458. bool has_mouse_event = false;
  459. for (const Ref<InputEvent> &m : physics_picking_events) {
  460. if (m.is_valid()) {
  461. has_mouse_event = true;
  462. break;
  463. }
  464. }
  465. if (!has_mouse_event) {
  466. Ref<InputEventMouseMotion> mm;
  467. mm.instantiate();
  468. mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
  469. mm->set_global_position(physics_last_mousepos);
  470. mm->set_position(physics_last_mousepos);
  471. mm->set_alt_pressed(physics_last_mouse_state.alt);
  472. mm->set_shift_pressed(physics_last_mouse_state.shift);
  473. mm->set_ctrl_pressed(physics_last_mouse_state.control);
  474. mm->set_meta_pressed(physics_last_mouse_state.meta);
  475. mm->set_button_mask(physics_last_mouse_state.mouse_mask);
  476. physics_picking_events.push_back(mm);
  477. }
  478. }
  479. while (physics_picking_events.size()) {
  480. Ref<InputEvent> ev = physics_picking_events.front()->get();
  481. physics_picking_events.pop_front();
  482. Vector2 pos;
  483. bool is_mouse = false;
  484. Ref<InputEventMouseMotion> mm = ev;
  485. if (mm.is_valid()) {
  486. pos = mm->get_position();
  487. is_mouse = true;
  488. physics_has_last_mousepos = true;
  489. physics_last_mousepos = pos;
  490. physics_last_mouse_state.alt = mm->is_alt_pressed();
  491. physics_last_mouse_state.shift = mm->is_shift_pressed();
  492. physics_last_mouse_state.control = mm->is_ctrl_pressed();
  493. physics_last_mouse_state.meta = mm->is_meta_pressed();
  494. physics_last_mouse_state.mouse_mask = mm->get_button_mask();
  495. }
  496. Ref<InputEventMouseButton> mb = ev;
  497. if (mb.is_valid()) {
  498. pos = mb->get_position();
  499. is_mouse = true;
  500. physics_has_last_mousepos = true;
  501. physics_last_mousepos = pos;
  502. physics_last_mouse_state.alt = mb->is_alt_pressed();
  503. physics_last_mouse_state.shift = mb->is_shift_pressed();
  504. physics_last_mouse_state.control = mb->is_ctrl_pressed();
  505. physics_last_mouse_state.meta = mb->is_meta_pressed();
  506. if (mb->is_pressed()) {
  507. physics_last_mouse_state.mouse_mask |= mouse_button_to_mask(mb->get_button_index());
  508. } else {
  509. physics_last_mouse_state.mouse_mask &= ~mouse_button_to_mask(mb->get_button_index());
  510. // If touch mouse raised, assume we don't know last mouse pos until new events come
  511. if (mb->get_device() == InputEvent::DEVICE_ID_TOUCH_MOUSE) {
  512. physics_has_last_mousepos = false;
  513. }
  514. }
  515. }
  516. Ref<InputEventKey> k = ev;
  517. if (k.is_valid()) {
  518. // Only for mask.
  519. physics_last_mouse_state.alt = k->is_alt_pressed();
  520. physics_last_mouse_state.shift = k->is_shift_pressed();
  521. physics_last_mouse_state.control = k->is_ctrl_pressed();
  522. physics_last_mouse_state.meta = k->is_meta_pressed();
  523. continue;
  524. }
  525. Ref<InputEventScreenDrag> sd = ev;
  526. if (sd.is_valid()) {
  527. pos = sd->get_position();
  528. }
  529. Ref<InputEventScreenTouch> st = ev;
  530. if (st.is_valid()) {
  531. pos = st->get_position();
  532. }
  533. if (ss2d) {
  534. // Send to 2D.
  535. uint64_t frame = get_tree()->get_frame();
  536. PhysicsDirectSpaceState2D::ShapeResult res[64];
  537. for (Set<CanvasLayer *>::Element *E = canvas_layers.front(); E; E = E->next()) {
  538. Transform2D canvas_transform;
  539. ObjectID canvas_layer_id;
  540. if (E->get()) {
  541. // A descendant CanvasLayer.
  542. canvas_transform = E->get()->get_transform();
  543. canvas_layer_id = E->get()->get_instance_id();
  544. } else {
  545. // This Viewport's builtin canvas.
  546. canvas_transform = get_canvas_transform();
  547. canvas_layer_id = ObjectID();
  548. }
  549. Vector2 point = canvas_transform.affine_inverse().xform(pos);
  550. PhysicsDirectSpaceState2D::PointParameters point_params;
  551. point_params.position = point;
  552. point_params.canvas_instance_id = canvas_layer_id;
  553. point_params.collide_with_areas = true;
  554. point_params.pick_point = true;
  555. int rc = ss2d->intersect_point(point_params, res, 64);
  556. for (int i = 0; i < rc; i++) {
  557. if (res[i].collider_id.is_valid() && res[i].collider) {
  558. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(res[i].collider);
  559. if (co && co->can_process()) {
  560. bool send_event = true;
  561. if (is_mouse) {
  562. Map<ObjectID, uint64_t>::Element *F = physics_2d_mouseover.find(res[i].collider_id);
  563. if (!F) {
  564. physics_2d_mouseover.insert(res[i].collider_id, frame);
  565. co->_mouse_enter();
  566. } else {
  567. F->get() = frame;
  568. // It was already hovered, so don't send the event if it's faked.
  569. if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
  570. send_event = false;
  571. }
  572. }
  573. Map<Pair<ObjectID, int>, uint64_t, PairSort<ObjectID, int>>::Element *SF = physics_2d_shape_mouseover.find(Pair(res[i].collider_id, res[i].shape));
  574. if (!SF) {
  575. physics_2d_shape_mouseover.insert(Pair(res[i].collider_id, res[i].shape), frame);
  576. co->_mouse_shape_enter(res[i].shape);
  577. } else {
  578. SF->get() = frame;
  579. }
  580. }
  581. if (send_event) {
  582. co->_input_event_call(this, ev, res[i].shape);
  583. }
  584. }
  585. }
  586. }
  587. }
  588. if (is_mouse) {
  589. _cleanup_mouseover_colliders(false, false, frame);
  590. }
  591. }
  592. #ifndef _3D_DISABLED
  593. bool captured = false;
  594. if (physics_object_capture.is_valid()) {
  595. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(physics_object_capture));
  596. if (co && camera_3d) {
  597. _collision_object_3d_input_event(co, camera_3d, ev, Vector3(), Vector3(), 0);
  598. captured = true;
  599. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  600. physics_object_capture = ObjectID();
  601. }
  602. } else {
  603. physics_object_capture = ObjectID();
  604. }
  605. }
  606. if (captured) {
  607. // None.
  608. } else if (pos == last_pos) {
  609. if (last_id.is_valid()) {
  610. if (ObjectDB::get_instance(last_id) && last_object) {
  611. // Good, exists.
  612. _collision_object_3d_input_event(last_object, camera_3d, ev, result.position, result.normal, result.shape);
  613. if (last_object->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  614. physics_object_capture = last_id;
  615. }
  616. }
  617. }
  618. } else {
  619. if (camera_3d) {
  620. Vector3 from = camera_3d->project_ray_origin(pos);
  621. Vector3 dir = camera_3d->project_ray_normal(pos);
  622. real_t far = camera_3d->far;
  623. PhysicsDirectSpaceState3D *space = PhysicsServer3D::get_singleton()->space_get_direct_state(find_world_3d()->get_space());
  624. if (space) {
  625. PhysicsDirectSpaceState3D::RayParameters ray_params;
  626. ray_params.from = from;
  627. ray_params.to = from + dir * far;
  628. ray_params.collide_with_areas = true;
  629. ray_params.pick_ray = true;
  630. bool col = space->intersect_ray(ray_params, result);
  631. ObjectID new_collider;
  632. if (col) {
  633. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(result.collider);
  634. if (co && co->can_process()) {
  635. _collision_object_3d_input_event(co, camera_3d, ev, result.position, result.normal, result.shape);
  636. last_object = co;
  637. last_id = result.collider_id;
  638. new_collider = last_id;
  639. if (co->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  640. physics_object_capture = last_id;
  641. }
  642. }
  643. }
  644. if (is_mouse && new_collider != physics_object_over) {
  645. if (physics_object_over.is_valid()) {
  646. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(physics_object_over));
  647. if (co) {
  648. co->_mouse_exit();
  649. }
  650. }
  651. if (new_collider.is_valid()) {
  652. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(new_collider));
  653. if (co) {
  654. co->_mouse_enter();
  655. }
  656. }
  657. physics_object_over = new_collider;
  658. }
  659. }
  660. last_pos = pos;
  661. }
  662. }
  663. #endif // _3D_DISABLED
  664. }
  665. }
  666. RID Viewport::get_viewport_rid() const {
  667. return viewport;
  668. }
  669. void Viewport::update_canvas_items() {
  670. if (!is_inside_tree()) {
  671. return;
  672. }
  673. _update_canvas_items(this);
  674. }
  675. void Viewport::_set_size(const Size2i &p_size, const Size2i &p_size_2d_override, const Rect2i &p_to_screen_rect, const Transform2D &p_stretch_transform, bool p_allocated) {
  676. if (size == p_size && size_allocated == p_allocated && stretch_transform == p_stretch_transform && p_size_2d_override == size_2d_override && to_screen_rect == p_to_screen_rect) {
  677. return;
  678. }
  679. size = p_size;
  680. size_allocated = p_allocated;
  681. size_2d_override = p_size_2d_override;
  682. stretch_transform = p_stretch_transform;
  683. to_screen_rect = p_to_screen_rect;
  684. if (p_allocated) {
  685. RS::get_singleton()->viewport_set_size(viewport, size.width, size.height);
  686. } else {
  687. RS::get_singleton()->viewport_set_size(viewport, 0, 0);
  688. }
  689. _update_global_transform();
  690. update_canvas_items();
  691. emit_signal(SNAME("size_changed"));
  692. }
  693. Size2i Viewport::_get_size() const {
  694. return size;
  695. }
  696. Size2i Viewport::_get_size_2d_override() const {
  697. return size_2d_override;
  698. }
  699. bool Viewport::_is_size_allocated() const {
  700. return size_allocated;
  701. }
  702. Rect2 Viewport::get_visible_rect() const {
  703. Rect2 r;
  704. if (size == Size2()) {
  705. r = Rect2(Point2(), DisplayServer::get_singleton()->window_get_size());
  706. } else {
  707. r = Rect2(Point2(), size);
  708. }
  709. if (size_2d_override != Size2i()) {
  710. r.size = size_2d_override;
  711. }
  712. return r;
  713. }
  714. void Viewport::_update_audio_listener_2d() {
  715. if (AudioServer::get_singleton()) {
  716. AudioServer::get_singleton()->notify_listener_changed();
  717. }
  718. }
  719. void Viewport::set_as_audio_listener_2d(bool p_enable) {
  720. if (p_enable == is_audio_listener_2d_enabled) {
  721. return;
  722. }
  723. is_audio_listener_2d_enabled = p_enable;
  724. _update_audio_listener_2d();
  725. }
  726. bool Viewport::is_audio_listener_2d() const {
  727. return is_audio_listener_2d_enabled;
  728. }
  729. AudioListener2D *Viewport::get_audio_listener_2d() const {
  730. return audio_listener_2d;
  731. }
  732. void Viewport::enable_canvas_transform_override(bool p_enable) {
  733. if (override_canvas_transform == p_enable) {
  734. return;
  735. }
  736. override_canvas_transform = p_enable;
  737. if (p_enable) {
  738. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform_override);
  739. } else {
  740. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform);
  741. }
  742. }
  743. bool Viewport::is_canvas_transform_override_enbled() const {
  744. return override_canvas_transform;
  745. }
  746. void Viewport::set_canvas_transform_override(const Transform2D &p_transform) {
  747. if (canvas_transform_override == p_transform) {
  748. return;
  749. }
  750. canvas_transform_override = p_transform;
  751. if (override_canvas_transform) {
  752. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform_override);
  753. }
  754. }
  755. Transform2D Viewport::get_canvas_transform_override() const {
  756. return canvas_transform_override;
  757. }
  758. void Viewport::set_canvas_transform(const Transform2D &p_transform) {
  759. canvas_transform = p_transform;
  760. if (!override_canvas_transform) {
  761. RenderingServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform);
  762. }
  763. }
  764. Transform2D Viewport::get_canvas_transform() const {
  765. return canvas_transform;
  766. }
  767. void Viewport::_update_global_transform() {
  768. Transform2D sxform = stretch_transform * global_canvas_transform;
  769. RenderingServer::get_singleton()->viewport_set_global_canvas_transform(viewport, sxform);
  770. }
  771. void Viewport::set_global_canvas_transform(const Transform2D &p_transform) {
  772. global_canvas_transform = p_transform;
  773. _update_global_transform();
  774. }
  775. Transform2D Viewport::get_global_canvas_transform() const {
  776. return global_canvas_transform;
  777. }
  778. void Viewport::_camera_2d_set(Camera2D *p_camera_2d) {
  779. camera_2d = p_camera_2d;
  780. }
  781. void Viewport::_audio_listener_2d_set(AudioListener2D *p_listener) {
  782. if (audio_listener_2d == p_listener) {
  783. return;
  784. } else if (audio_listener_2d) {
  785. audio_listener_2d->clear_current();
  786. }
  787. audio_listener_2d = p_listener;
  788. }
  789. void Viewport::_audio_listener_2d_remove(AudioListener2D *p_listener) {
  790. if (audio_listener_2d == p_listener) {
  791. audio_listener_2d = nullptr;
  792. }
  793. }
  794. void Viewport::_canvas_layer_add(CanvasLayer *p_canvas_layer) {
  795. canvas_layers.insert(p_canvas_layer);
  796. }
  797. void Viewport::_canvas_layer_remove(CanvasLayer *p_canvas_layer) {
  798. canvas_layers.erase(p_canvas_layer);
  799. }
  800. void Viewport::set_transparent_background(bool p_enable) {
  801. transparent_bg = p_enable;
  802. RS::get_singleton()->viewport_set_transparent_background(viewport, p_enable);
  803. }
  804. bool Viewport::has_transparent_background() const {
  805. return transparent_bg;
  806. }
  807. void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) {
  808. if (world_2d == p_world_2d) {
  809. return;
  810. }
  811. if (parent && parent->find_world_2d() == p_world_2d) {
  812. WARN_PRINT("Unable to use parent world_2d as world_2d");
  813. return;
  814. }
  815. if (is_inside_tree()) {
  816. RenderingServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
  817. }
  818. if (p_world_2d.is_valid()) {
  819. world_2d = p_world_2d;
  820. } else {
  821. WARN_PRINT("Invalid world_2d");
  822. world_2d = Ref<World2D>(memnew(World2D));
  823. }
  824. _update_audio_listener_2d();
  825. if (is_inside_tree()) {
  826. current_canvas = find_world_2d()->get_canvas();
  827. RenderingServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
  828. }
  829. }
  830. Ref<World2D> Viewport::find_world_2d() const {
  831. if (world_2d.is_valid()) {
  832. return world_2d;
  833. } else if (parent) {
  834. return parent->find_world_2d();
  835. } else {
  836. return Ref<World2D>();
  837. }
  838. }
  839. void Viewport::_propagate_viewport_notification(Node *p_node, int p_what) {
  840. p_node->notification(p_what);
  841. for (int i = 0; i < p_node->get_child_count(); i++) {
  842. Node *c = p_node->get_child(i);
  843. if (Object::cast_to<Viewport>(c)) {
  844. continue;
  845. }
  846. _propagate_viewport_notification(c, p_what);
  847. }
  848. }
  849. Ref<World2D> Viewport::get_world_2d() const {
  850. return world_2d;
  851. }
  852. Camera2D *Viewport::get_camera_2d() const {
  853. return camera_2d;
  854. }
  855. Transform2D Viewport::get_final_transform() const {
  856. return stretch_transform * global_canvas_transform;
  857. }
  858. void Viewport::_update_canvas_items(Node *p_node) {
  859. if (p_node != this) {
  860. Window *w = Object::cast_to<Window>(p_node);
  861. if (w && (!w->is_inside_tree() || !w->is_embedded())) {
  862. return;
  863. }
  864. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
  865. if (ci) {
  866. ci->update();
  867. }
  868. }
  869. int cc = p_node->get_child_count();
  870. for (int i = 0; i < cc; i++) {
  871. _update_canvas_items(p_node->get_child(i));
  872. }
  873. }
  874. Ref<ViewportTexture> Viewport::get_texture() const {
  875. return default_texture;
  876. }
  877. void Viewport::set_shadow_atlas_size(int p_size) {
  878. shadow_atlas_size = p_size;
  879. RS::get_singleton()->viewport_set_shadow_atlas_size(viewport, p_size, shadow_atlas_16_bits);
  880. }
  881. int Viewport::get_shadow_atlas_size() const {
  882. return shadow_atlas_size;
  883. }
  884. void Viewport::set_shadow_atlas_16_bits(bool p_16_bits) {
  885. if (shadow_atlas_16_bits == p_16_bits) {
  886. return;
  887. }
  888. shadow_atlas_16_bits = p_16_bits;
  889. RS::get_singleton()->viewport_set_shadow_atlas_size(viewport, shadow_atlas_size, shadow_atlas_16_bits);
  890. }
  891. bool Viewport::get_shadow_atlas_16_bits() const {
  892. return shadow_atlas_16_bits;
  893. }
  894. void Viewport::set_shadow_atlas_quadrant_subdiv(int p_quadrant, ShadowAtlasQuadrantSubdiv p_subdiv) {
  895. ERR_FAIL_INDEX(p_quadrant, 4);
  896. ERR_FAIL_INDEX(p_subdiv, SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
  897. if (shadow_atlas_quadrant_subdiv[p_quadrant] == p_subdiv) {
  898. return;
  899. }
  900. shadow_atlas_quadrant_subdiv[p_quadrant] = p_subdiv;
  901. static const int subdiv[SHADOW_ATLAS_QUADRANT_SUBDIV_MAX] = { 0, 1, 4, 16, 64, 256, 1024 };
  902. RS::get_singleton()->viewport_set_shadow_atlas_quadrant_subdivision(viewport, p_quadrant, subdiv[p_subdiv]);
  903. }
  904. Viewport::ShadowAtlasQuadrantSubdiv Viewport::get_shadow_atlas_quadrant_subdiv(int p_quadrant) const {
  905. ERR_FAIL_INDEX_V(p_quadrant, 4, SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
  906. return shadow_atlas_quadrant_subdiv[p_quadrant];
  907. }
  908. Transform2D Viewport::_get_input_pre_xform() const {
  909. Transform2D pre_xf;
  910. if (to_screen_rect.size.x != 0 && to_screen_rect.size.y != 0) {
  911. pre_xf.columns[2] = -to_screen_rect.position;
  912. pre_xf.scale(Vector2(size) / to_screen_rect.size);
  913. }
  914. return pre_xf;
  915. }
  916. Ref<InputEvent> Viewport::_make_input_local(const Ref<InputEvent> &ev) {
  917. if (ev.is_null()) {
  918. return ev; // No transformation defined for null event
  919. }
  920. Transform2D ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
  921. return ev->xformed_by(ai);
  922. }
  923. Vector2 Viewport::get_mouse_position() const {
  924. return gui.last_mouse_pos;
  925. }
  926. void Viewport::warp_mouse(const Vector2 &p_position) {
  927. Transform2D xform = get_screen_transform();
  928. Vector2 gpos = xform.xform(p_position).round();
  929. Input::get_singleton()->warp_mouse(gpos);
  930. }
  931. void Viewport::_gui_sort_roots() {
  932. if (!gui.roots_order_dirty) {
  933. return;
  934. }
  935. gui.roots.sort_custom<Control::CComparator>();
  936. gui.roots_order_dirty = false;
  937. }
  938. void Viewport::_gui_cancel_tooltip() {
  939. gui.tooltip_control = nullptr;
  940. if (gui.tooltip_timer.is_valid()) {
  941. gui.tooltip_timer->release_connections();
  942. gui.tooltip_timer = Ref<SceneTreeTimer>();
  943. }
  944. if (gui.tooltip_popup) {
  945. gui.tooltip_popup->queue_delete();
  946. gui.tooltip_popup = nullptr;
  947. gui.tooltip_label = nullptr;
  948. }
  949. }
  950. String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Control **r_tooltip_owner) {
  951. Vector2 pos = p_pos;
  952. String tooltip;
  953. while (p_control) {
  954. tooltip = p_control->get_tooltip(pos);
  955. // Temporary solution for PopupMenus.
  956. PopupMenu *menu = Object::cast_to<PopupMenu>(this);
  957. if (menu) {
  958. tooltip = menu->get_tooltip(pos);
  959. }
  960. if (r_tooltip_owner) {
  961. *r_tooltip_owner = p_control;
  962. }
  963. // If we found a tooltip, we stop here.
  964. if (!tooltip.is_empty()) {
  965. break;
  966. }
  967. // Otherwise, we check parent controls unless some conditions prevent it.
  968. if (p_control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  969. break;
  970. }
  971. if (p_control->is_set_as_top_level()) {
  972. break;
  973. }
  974. // Transform cursor pos for parent control.
  975. pos = p_control->get_transform().xform(pos);
  976. p_control = p_control->get_parent_control();
  977. }
  978. return tooltip;
  979. }
  980. void Viewport::_gui_show_tooltip() {
  981. if (!gui.tooltip_control) {
  982. return;
  983. }
  984. // Get the Control under cursor and the relevant tooltip text, if any.
  985. Control *tooltip_owner = nullptr;
  986. String tooltip_text = _gui_get_tooltip(
  987. gui.tooltip_control,
  988. gui.tooltip_control->get_global_transform().xform_inv(gui.last_mouse_pos),
  989. &tooltip_owner);
  990. tooltip_text = tooltip_text.strip_edges();
  991. if (tooltip_text.is_empty()) {
  992. return; // Nothing to show.
  993. }
  994. // Remove previous popup if we change something.
  995. if (gui.tooltip_popup) {
  996. memdelete(gui.tooltip_popup);
  997. gui.tooltip_popup = nullptr;
  998. gui.tooltip_label = nullptr;
  999. }
  1000. if (!tooltip_owner) {
  1001. return;
  1002. }
  1003. // Popup window which houses the tooltip content.
  1004. TooltipPanel *panel = memnew(TooltipPanel);
  1005. // Controls can implement `make_custom_tooltip` to provide their own tooltip.
  1006. // This should be a Control node which will be added as child to a TooltipPanel.
  1007. Control *base_tooltip = tooltip_owner->make_custom_tooltip(tooltip_text);
  1008. // If no custom tooltip is given, use a default implementation.
  1009. if (!base_tooltip) {
  1010. gui.tooltip_label = memnew(TooltipLabel);
  1011. gui.tooltip_label->set_auto_translate(gui.tooltip_control->is_auto_translating());
  1012. gui.tooltip_label->set_text(tooltip_text);
  1013. base_tooltip = gui.tooltip_label;
  1014. panel->connect("mouse_entered", callable_mp(this, &Viewport::_gui_cancel_tooltip));
  1015. }
  1016. base_tooltip->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1017. panel->set_transient(true);
  1018. panel->set_flag(Window::FLAG_NO_FOCUS, true);
  1019. panel->set_flag(Window::FLAG_POPUP, false);
  1020. panel->set_wrap_controls(true);
  1021. panel->add_child(base_tooltip);
  1022. gui.tooltip_popup = panel;
  1023. tooltip_owner->add_child(gui.tooltip_popup);
  1024. Point2 tooltip_offset = ProjectSettings::get_singleton()->get("display/mouse_cursor/tooltip_position_offset");
  1025. Rect2 r(gui.tooltip_pos + tooltip_offset, gui.tooltip_popup->get_contents_minimum_size());
  1026. Window *window = gui.tooltip_popup->get_parent_visible_window();
  1027. Rect2i vr = window->get_usable_parent_rect();
  1028. if (r.size.x + r.position.x > vr.size.x + vr.position.x) {
  1029. r.position.x = vr.position.x + vr.size.x - r.size.x;
  1030. } else if (r.position.x < vr.position.x) {
  1031. r.position.x = vr.position.x;
  1032. }
  1033. if (r.size.y + r.position.y > vr.size.y + vr.position.y) {
  1034. r.position.y = vr.position.y + vr.size.y - r.size.y;
  1035. } else if (r.position.y < vr.position.y) {
  1036. r.position.y = vr.position.y;
  1037. }
  1038. gui.tooltip_popup->set_current_screen(window->get_current_screen());
  1039. gui.tooltip_popup->set_position(r.position);
  1040. gui.tooltip_popup->set_size(r.size);
  1041. DisplayServer::WindowID active_popup = DisplayServer::get_singleton()->window_get_active_popup();
  1042. if (active_popup == DisplayServer::INVALID_WINDOW_ID || active_popup == window->get_window_id()) {
  1043. gui.tooltip_popup->show();
  1044. }
  1045. gui.tooltip_popup->child_controls_changed();
  1046. }
  1047. void Viewport::_gui_call_input(Control *p_control, const Ref<InputEvent> &p_input) {
  1048. Ref<InputEvent> ev = p_input;
  1049. // Mouse wheel events can't be stopped.
  1050. Ref<InputEventMouseButton> mb = p_input;
  1051. bool cant_stop_me_now = (mb.is_valid() &&
  1052. (mb->get_button_index() == MouseButton::WHEEL_DOWN ||
  1053. mb->get_button_index() == MouseButton::WHEEL_UP ||
  1054. mb->get_button_index() == MouseButton::WHEEL_LEFT ||
  1055. mb->get_button_index() == MouseButton::WHEEL_RIGHT));
  1056. Ref<InputEventPanGesture> pn = p_input;
  1057. cant_stop_me_now = pn.is_valid() || cant_stop_me_now;
  1058. bool ismouse = ev.is_valid() || Object::cast_to<InputEventMouseMotion>(*p_input) != nullptr;
  1059. CanvasItem *ci = p_control;
  1060. while (ci) {
  1061. Control *control = Object::cast_to<Control>(ci);
  1062. if (control) {
  1063. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1064. control->_call_gui_input(ev);
  1065. }
  1066. if (!control->is_inside_tree() || control->is_set_as_top_level()) {
  1067. break;
  1068. }
  1069. if (gui.key_event_accepted) {
  1070. break;
  1071. }
  1072. if (!cant_stop_me_now && control->data.mouse_filter == Control::MOUSE_FILTER_STOP && ismouse) {
  1073. break;
  1074. }
  1075. }
  1076. if (ci->is_set_as_top_level()) {
  1077. break;
  1078. }
  1079. ev = ev->xformed_by(ci->get_transform()); // Transform event upwards.
  1080. ci = ci->get_parent_item();
  1081. }
  1082. }
  1083. void Viewport::_gui_call_notification(Control *p_control, int p_what) {
  1084. CanvasItem *ci = p_control;
  1085. while (ci) {
  1086. Control *control = Object::cast_to<Control>(ci);
  1087. if (control) {
  1088. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1089. control->notification(p_what);
  1090. }
  1091. if (!control->is_inside_tree()) {
  1092. break;
  1093. }
  1094. if (!control->is_inside_tree() || control->is_set_as_top_level()) {
  1095. break;
  1096. }
  1097. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1098. break;
  1099. }
  1100. }
  1101. if (ci->is_set_as_top_level()) {
  1102. break;
  1103. }
  1104. ci = ci->get_parent_item();
  1105. }
  1106. }
  1107. Control *Viewport::gui_find_control(const Point2 &p_global) {
  1108. // Handle subwindows.
  1109. _gui_sort_roots();
  1110. for (List<Control *>::Element *E = gui.roots.back(); E; E = E->prev()) {
  1111. Control *sw = E->get();
  1112. if (!sw->is_visible_in_tree()) {
  1113. continue;
  1114. }
  1115. Transform2D xform;
  1116. CanvasItem *pci = sw->get_parent_item();
  1117. if (pci) {
  1118. xform = pci->get_global_transform_with_canvas();
  1119. } else {
  1120. xform = sw->get_canvas_transform();
  1121. }
  1122. Control *ret = _gui_find_control_at_pos(sw, p_global, xform, gui.focus_inv_xform);
  1123. if (ret) {
  1124. return ret;
  1125. }
  1126. }
  1127. return nullptr;
  1128. }
  1129. Control *Viewport::_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Transform2D &p_xform, Transform2D &r_inv_xform) {
  1130. if (Object::cast_to<Viewport>(p_node)) {
  1131. return nullptr;
  1132. }
  1133. if (!p_node->is_visible()) {
  1134. return nullptr; // Canvas item hidden, discard.
  1135. }
  1136. Transform2D matrix = p_xform * p_node->get_transform();
  1137. // matrix.basis_determinant() == 0.0f implies that node does not exist on scene
  1138. if (matrix.basis_determinant() == 0.0f) {
  1139. return nullptr;
  1140. }
  1141. Control *c = Object::cast_to<Control>(p_node);
  1142. if (!c || !c->is_clipping_contents() || c->has_point(matrix.affine_inverse().xform(p_global))) {
  1143. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  1144. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node->get_child(i));
  1145. if (!ci || ci->is_set_as_top_level()) {
  1146. continue;
  1147. }
  1148. Control *ret = _gui_find_control_at_pos(ci, p_global, matrix, r_inv_xform);
  1149. if (ret) {
  1150. return ret;
  1151. }
  1152. }
  1153. }
  1154. if (!c || c->data.mouse_filter == Control::MOUSE_FILTER_IGNORE) {
  1155. return nullptr;
  1156. }
  1157. matrix.affine_invert();
  1158. if (!c->has_point(matrix.xform(p_global))) {
  1159. return nullptr;
  1160. }
  1161. Control *drag_preview = _gui_get_drag_preview();
  1162. if (!drag_preview || (c != drag_preview && !drag_preview->is_ancestor_of(c))) {
  1163. r_inv_xform = matrix;
  1164. return c;
  1165. }
  1166. return nullptr;
  1167. }
  1168. bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check) {
  1169. // Attempt grab, try parent controls too.
  1170. CanvasItem *ci = p_at_control;
  1171. while (ci) {
  1172. Control *control = Object::cast_to<Control>(ci);
  1173. if (control) {
  1174. if (control->can_drop_data(p_at_pos, gui.drag_data)) {
  1175. if (!p_just_check) {
  1176. control->drop_data(p_at_pos, gui.drag_data);
  1177. }
  1178. return true;
  1179. }
  1180. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1181. break;
  1182. }
  1183. }
  1184. p_at_pos = ci->get_transform().xform(p_at_pos);
  1185. if (ci->is_set_as_top_level()) {
  1186. break;
  1187. }
  1188. ci = ci->get_parent_item();
  1189. }
  1190. return false;
  1191. }
  1192. void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
  1193. ERR_FAIL_COND(p_event.is_null());
  1194. Ref<InputEventMouseButton> mb = p_event;
  1195. if (mb.is_valid()) {
  1196. gui.key_event_accepted = false;
  1197. Point2 mpos = mb->get_position();
  1198. gui.last_mouse_pos = mpos;
  1199. if (mb->is_pressed()) {
  1200. Size2 pos = mpos;
  1201. if (gui.mouse_focus_mask != MouseButton::NONE) {
  1202. // Do not steal mouse focus and stuff while a focus mask exists.
  1203. gui.mouse_focus_mask |= mouse_button_to_mask(mb->get_button_index());
  1204. } else {
  1205. gui.mouse_focus = gui_find_control(pos);
  1206. gui.last_mouse_focus = gui.mouse_focus;
  1207. if (!gui.mouse_focus) {
  1208. gui.mouse_focus_mask = MouseButton::NONE;
  1209. return;
  1210. }
  1211. gui.mouse_focus_mask = mouse_button_to_mask(mb->get_button_index());
  1212. if (mb->get_button_index() == MouseButton::LEFT) {
  1213. gui.drag_accum = Vector2();
  1214. gui.drag_attempted = false;
  1215. }
  1216. }
  1217. mb = mb->xformed_by(Transform2D()); // Make a copy of the event.
  1218. mb->set_global_position(pos);
  1219. pos = gui.focus_inv_xform.xform(pos);
  1220. mb->set_position(pos);
  1221. #ifdef DEBUG_ENABLED
  1222. if (EngineDebugger::get_singleton() && gui.mouse_focus) {
  1223. Array arr;
  1224. arr.push_back(gui.mouse_focus->get_path());
  1225. arr.push_back(gui.mouse_focus->get_class());
  1226. EngineDebugger::get_singleton()->send_message("scene:click_ctrl", arr);
  1227. }
  1228. #endif
  1229. if (mb->get_button_index() == MouseButton::LEFT) { // Assign focus.
  1230. CanvasItem *ci = gui.mouse_focus;
  1231. while (ci) {
  1232. Control *control = Object::cast_to<Control>(ci);
  1233. if (control) {
  1234. if (control->get_focus_mode() != Control::FOCUS_NONE) {
  1235. if (control != gui.key_focus) {
  1236. control->grab_focus();
  1237. }
  1238. break;
  1239. }
  1240. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1241. break;
  1242. }
  1243. }
  1244. if (ci->is_set_as_top_level()) {
  1245. break;
  1246. }
  1247. ci = ci->get_parent_item();
  1248. }
  1249. }
  1250. if (gui.mouse_focus && gui.mouse_focus->can_process()) {
  1251. _gui_call_input(gui.mouse_focus, mb);
  1252. }
  1253. set_input_as_handled();
  1254. if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == MouseButton::LEFT) {
  1255. // Alternate drop use (when using force_drag(), as proposed by #5342).
  1256. gui.drag_successful = false;
  1257. if (gui.mouse_focus) {
  1258. gui.drag_successful = _gui_drop(gui.mouse_focus, pos, false);
  1259. }
  1260. gui.drag_data = Variant();
  1261. gui.dragging = false;
  1262. Control *drag_preview = _gui_get_drag_preview();
  1263. if (drag_preview) {
  1264. memdelete(drag_preview);
  1265. gui.drag_preview_id = ObjectID();
  1266. }
  1267. _propagate_viewport_notification(this, NOTIFICATION_DRAG_END);
  1268. // Change mouse accordingly.
  1269. }
  1270. _gui_cancel_tooltip();
  1271. } else {
  1272. if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == MouseButton::LEFT) {
  1273. gui.drag_successful = false;
  1274. if (gui.drag_mouse_over) {
  1275. gui.drag_successful = _gui_drop(gui.drag_mouse_over, gui.drag_mouse_over_pos, false);
  1276. }
  1277. Control *drag_preview = _gui_get_drag_preview();
  1278. if (drag_preview) {
  1279. memdelete(drag_preview);
  1280. gui.drag_preview_id = ObjectID();
  1281. }
  1282. gui.drag_data = Variant();
  1283. gui.dragging = false;
  1284. gui.drag_mouse_over = nullptr;
  1285. _propagate_viewport_notification(this, NOTIFICATION_DRAG_END);
  1286. // Change mouse accordingly.
  1287. }
  1288. gui.mouse_focus_mask &= ~mouse_button_to_mask(mb->get_button_index()); // Remove from mask.
  1289. if (!gui.mouse_focus) {
  1290. // Release event is only sent if a mouse focus (previously pressed button) exists.
  1291. return;
  1292. }
  1293. Size2 pos = mpos;
  1294. mb = mb->xformed_by(Transform2D()); // Make a copy.
  1295. mb->set_global_position(pos);
  1296. pos = gui.focus_inv_xform.xform(pos);
  1297. mb->set_position(pos);
  1298. Control *mouse_focus = gui.mouse_focus;
  1299. // Disable mouse focus if needed before calling input,
  1300. // this makes popups on mouse press event work better,
  1301. // as the release will never be received otherwise.
  1302. if (gui.mouse_focus_mask == MouseButton::NONE) {
  1303. gui.mouse_focus = nullptr;
  1304. gui.forced_mouse_focus = false;
  1305. }
  1306. if (mouse_focus && mouse_focus->can_process()) {
  1307. _gui_call_input(mouse_focus, mb);
  1308. }
  1309. set_input_as_handled();
  1310. }
  1311. }
  1312. Ref<InputEventMouseMotion> mm = p_event;
  1313. if (mm.is_valid()) {
  1314. gui.key_event_accepted = false;
  1315. Point2 mpos = mm->get_position();
  1316. gui.last_mouse_pos = mpos;
  1317. // Drag & drop.
  1318. if (!gui.drag_attempted && gui.mouse_focus && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) {
  1319. gui.drag_accum += mm->get_relative();
  1320. float len = gui.drag_accum.length();
  1321. if (len > 10) {
  1322. { // Attempt grab, try parent controls too.
  1323. CanvasItem *ci = gui.mouse_focus;
  1324. while (ci) {
  1325. Control *control = Object::cast_to<Control>(ci);
  1326. if (control) {
  1327. gui.dragging = true;
  1328. gui.drag_data = control->get_drag_data(control->get_global_transform_with_canvas().affine_inverse().xform(mpos) - gui.drag_accum);
  1329. if (gui.drag_data.get_type() != Variant::NIL) {
  1330. gui.mouse_focus = nullptr;
  1331. gui.forced_mouse_focus = false;
  1332. gui.mouse_focus_mask = MouseButton::NONE;
  1333. break;
  1334. } else {
  1335. Control *drag_preview = _gui_get_drag_preview();
  1336. if (drag_preview) {
  1337. ERR_PRINT("Don't set a drag preview and return null data. Preview was deleted and drag request ignored.");
  1338. memdelete(drag_preview);
  1339. gui.drag_preview_id = ObjectID();
  1340. }
  1341. gui.dragging = false;
  1342. }
  1343. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1344. break;
  1345. }
  1346. }
  1347. if (ci->is_set_as_top_level()) {
  1348. break;
  1349. }
  1350. ci = ci->get_parent_item();
  1351. }
  1352. }
  1353. gui.drag_attempted = true;
  1354. if (gui.drag_data.get_type() != Variant::NIL) {
  1355. _propagate_viewport_notification(this, NOTIFICATION_DRAG_BEGIN);
  1356. }
  1357. }
  1358. }
  1359. Control *over = nullptr;
  1360. if (gui.mouse_in_viewport) {
  1361. over = gui_find_control(mpos);
  1362. }
  1363. if (over != gui.mouse_over) {
  1364. _drop_mouse_over();
  1365. _gui_cancel_tooltip();
  1366. if (over) {
  1367. _gui_call_notification(over, Control::NOTIFICATION_MOUSE_ENTER);
  1368. gui.mouse_over = over;
  1369. }
  1370. }
  1371. if (gui.mouse_focus) {
  1372. over = gui.mouse_focus;
  1373. }
  1374. DisplayServer::CursorShape ds_cursor_shape = (DisplayServer::CursorShape)Input::get_singleton()->get_default_cursor_shape();
  1375. if (over) {
  1376. Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
  1377. Size2 pos = localizer.xform(mpos);
  1378. Vector2 velocity = localizer.basis_xform(mm->get_velocity());
  1379. Vector2 rel = localizer.basis_xform(mm->get_relative());
  1380. mm = mm->xformed_by(Transform2D()); // Make a copy.
  1381. mm->set_global_position(mpos);
  1382. mm->set_velocity(velocity);
  1383. mm->set_relative(rel);
  1384. if (mm->get_button_mask() == MouseButton::NONE) {
  1385. // Nothing pressed.
  1386. bool is_tooltip_shown = false;
  1387. if (gui.tooltip_popup) {
  1388. if (gui.tooltip_control) {
  1389. String tooltip = _gui_get_tooltip(over, gui.tooltip_control->get_global_transform().xform_inv(mpos));
  1390. tooltip = tooltip.strip_edges();
  1391. if (tooltip.length() == 0) {
  1392. _gui_cancel_tooltip();
  1393. } else if (gui.tooltip_label) {
  1394. if (tooltip == gui.tooltip_label->get_text()) {
  1395. is_tooltip_shown = true;
  1396. }
  1397. } else {
  1398. Variant t = gui.tooltip_popup->call("get_tooltip_text");
  1399. if (t.get_type() == Variant::STRING) {
  1400. if (tooltip == String(t)) {
  1401. is_tooltip_shown = true;
  1402. }
  1403. } else {
  1404. is_tooltip_shown = true; // Nothing to compare against, likely using custom control, so if it changes there is nothing we can do.
  1405. }
  1406. }
  1407. } else {
  1408. _gui_cancel_tooltip();
  1409. }
  1410. }
  1411. if (!is_tooltip_shown && over->can_process()) {
  1412. if (gui.tooltip_timer.is_valid()) {
  1413. gui.tooltip_timer->release_connections();
  1414. gui.tooltip_timer = Ref<SceneTreeTimer>();
  1415. }
  1416. gui.tooltip_control = over;
  1417. gui.tooltip_pos = over->get_screen_transform().xform(pos);
  1418. gui.tooltip_timer = get_tree()->create_timer(gui.tooltip_delay);
  1419. gui.tooltip_timer->set_ignore_time_scale(true);
  1420. gui.tooltip_timer->connect("timeout", callable_mp(this, &Viewport::_gui_show_tooltip));
  1421. }
  1422. }
  1423. mm->set_position(pos);
  1424. Control::CursorShape cursor_shape = Control::CURSOR_ARROW;
  1425. {
  1426. Control *c = over;
  1427. Vector2 cpos = pos;
  1428. while (c) {
  1429. if (gui.mouse_focus_mask != MouseButton::NONE || c->has_point(cpos)) {
  1430. cursor_shape = c->get_cursor_shape(cpos);
  1431. } else {
  1432. cursor_shape = Control::CURSOR_ARROW;
  1433. }
  1434. cpos = c->get_transform().xform(cpos);
  1435. if (cursor_shape != Control::CURSOR_ARROW) {
  1436. break;
  1437. }
  1438. if (c->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1439. break;
  1440. }
  1441. if (c->is_set_as_top_level()) {
  1442. break;
  1443. }
  1444. c = c->get_parent_control();
  1445. }
  1446. }
  1447. ds_cursor_shape = (DisplayServer::CursorShape)cursor_shape;
  1448. if (over && over->can_process()) {
  1449. _gui_call_input(over, mm);
  1450. }
  1451. set_input_as_handled();
  1452. }
  1453. if (gui.drag_data.get_type() != Variant::NIL) {
  1454. // Handle drag & drop.
  1455. Control *drag_preview = _gui_get_drag_preview();
  1456. if (drag_preview) {
  1457. drag_preview->set_position(mpos);
  1458. }
  1459. gui.drag_mouse_over = over;
  1460. gui.drag_mouse_over_pos = Vector2();
  1461. // Find the window this is above of.
  1462. // See if there is an embedder.
  1463. Viewport *embedder = nullptr;
  1464. Vector2 viewport_pos;
  1465. if (is_embedding_subwindows()) {
  1466. embedder = this;
  1467. viewport_pos = mpos;
  1468. } else {
  1469. // Not an embedder, but may be a subwindow of an embedder.
  1470. Window *w = Object::cast_to<Window>(this);
  1471. if (w) {
  1472. if (w->is_embedded()) {
  1473. embedder = w->_get_embedder();
  1474. Transform2D ai = (get_final_transform().affine_inverse() * _get_input_pre_xform()).affine_inverse();
  1475. viewport_pos = ai.xform(mpos) + w->get_position(); // To parent coords.
  1476. }
  1477. }
  1478. }
  1479. Viewport *viewport_under = nullptr;
  1480. if (embedder) {
  1481. // Use embedder logic.
  1482. for (int i = embedder->gui.sub_windows.size() - 1; i >= 0; i--) {
  1483. Window *sw = embedder->gui.sub_windows[i].window;
  1484. Rect2 swrect = Rect2i(sw->get_position(), sw->get_size());
  1485. if (!sw->get_flag(Window::FLAG_BORDERLESS)) {
  1486. int title_height = sw->get_theme_constant(SNAME("title_height"));
  1487. swrect.position.y -= title_height;
  1488. swrect.size.y += title_height;
  1489. }
  1490. if (swrect.has_point(viewport_pos)) {
  1491. viewport_under = sw;
  1492. viewport_pos -= sw->get_position();
  1493. }
  1494. }
  1495. if (!viewport_under) {
  1496. // Not in a subwindow, likely in embedder.
  1497. viewport_under = embedder;
  1498. }
  1499. } else {
  1500. // Use DisplayServer logic.
  1501. Vector2i screen_mouse_pos = DisplayServer::get_singleton()->mouse_get_position();
  1502. DisplayServer::WindowID window_id = DisplayServer::get_singleton()->get_window_at_screen_position(screen_mouse_pos);
  1503. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1504. ObjectID object_under = DisplayServer::get_singleton()->window_get_attached_instance_id(window_id);
  1505. if (object_under != ObjectID()) { // Fetch window.
  1506. Window *w = Object::cast_to<Window>(ObjectDB::get_instance(object_under));
  1507. if (w) {
  1508. viewport_under = w;
  1509. viewport_pos = screen_mouse_pos - w->get_position();
  1510. }
  1511. }
  1512. }
  1513. }
  1514. if (viewport_under) {
  1515. if (viewport_under != this) {
  1516. Transform2D ai = (viewport_under->get_final_transform().affine_inverse() * viewport_under->_get_input_pre_xform());
  1517. viewport_pos = ai.xform(viewport_pos);
  1518. }
  1519. // Find control under at position.
  1520. gui.drag_mouse_over = viewport_under->gui_find_control(viewport_pos);
  1521. if (gui.drag_mouse_over) {
  1522. Transform2D localizer = gui.drag_mouse_over->get_global_transform_with_canvas().affine_inverse();
  1523. gui.drag_mouse_over_pos = localizer.xform(viewport_pos);
  1524. bool can_drop = _gui_drop(gui.drag_mouse_over, gui.drag_mouse_over_pos, true);
  1525. if (!can_drop) {
  1526. ds_cursor_shape = DisplayServer::CURSOR_FORBIDDEN;
  1527. } else {
  1528. ds_cursor_shape = DisplayServer::CURSOR_CAN_DROP;
  1529. }
  1530. }
  1531. } else {
  1532. gui.drag_mouse_over = nullptr;
  1533. }
  1534. }
  1535. DisplayServer::get_singleton()->cursor_set_shape(ds_cursor_shape);
  1536. }
  1537. Ref<InputEventScreenTouch> touch_event = p_event;
  1538. if (touch_event.is_valid()) {
  1539. Size2 pos = touch_event->get_position();
  1540. if (touch_event->is_pressed()) {
  1541. Control *over = gui_find_control(pos);
  1542. if (over) {
  1543. if (over->can_process()) {
  1544. touch_event = touch_event->xformed_by(Transform2D()); // Make a copy.
  1545. if (over == gui.mouse_focus) {
  1546. pos = gui.focus_inv_xform.xform(pos);
  1547. } else {
  1548. pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
  1549. }
  1550. touch_event->set_position(pos);
  1551. _gui_call_input(over, touch_event);
  1552. }
  1553. set_input_as_handled();
  1554. return;
  1555. }
  1556. } else if (touch_event->get_index() == 0 && gui.last_mouse_focus) {
  1557. if (gui.last_mouse_focus->can_process()) {
  1558. touch_event = touch_event->xformed_by(Transform2D()); // Make a copy.
  1559. touch_event->set_position(gui.focus_inv_xform.xform(pos));
  1560. _gui_call_input(gui.last_mouse_focus, touch_event);
  1561. }
  1562. set_input_as_handled();
  1563. return;
  1564. }
  1565. }
  1566. Ref<InputEventGesture> gesture_event = p_event;
  1567. if (gesture_event.is_valid()) {
  1568. gui.key_event_accepted = false;
  1569. _gui_cancel_tooltip();
  1570. Size2 pos = gesture_event->get_position();
  1571. Control *over = gui_find_control(pos);
  1572. if (over) {
  1573. if (over->can_process()) {
  1574. gesture_event = gesture_event->xformed_by(Transform2D()); // Make a copy.
  1575. if (over == gui.mouse_focus) {
  1576. pos = gui.focus_inv_xform.xform(pos);
  1577. } else {
  1578. pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
  1579. }
  1580. gesture_event->set_position(pos);
  1581. _gui_call_input(over, gesture_event);
  1582. }
  1583. set_input_as_handled();
  1584. return;
  1585. }
  1586. }
  1587. Ref<InputEventScreenDrag> drag_event = p_event;
  1588. if (drag_event.is_valid()) {
  1589. Control *over = gui.mouse_focus;
  1590. if (!over) {
  1591. over = gui_find_control(drag_event->get_position());
  1592. }
  1593. if (over) {
  1594. if (over->can_process()) {
  1595. Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
  1596. Size2 pos = localizer.xform(drag_event->get_position());
  1597. Vector2 velocity = localizer.basis_xform(drag_event->get_velocity());
  1598. Vector2 rel = localizer.basis_xform(drag_event->get_relative());
  1599. drag_event = drag_event->xformed_by(Transform2D()); // Make a copy.
  1600. drag_event->set_velocity(velocity);
  1601. drag_event->set_relative(rel);
  1602. drag_event->set_position(pos);
  1603. _gui_call_input(over, drag_event);
  1604. }
  1605. set_input_as_handled();
  1606. return;
  1607. }
  1608. }
  1609. if (mm.is_null() && mb.is_null() && p_event->is_action_type()) {
  1610. if (gui.key_focus && !gui.key_focus->is_visible_in_tree()) {
  1611. gui.key_focus->release_focus();
  1612. }
  1613. if (gui.key_focus) {
  1614. gui.key_event_accepted = false;
  1615. if (gui.key_focus->can_process()) {
  1616. gui.key_focus->_call_gui_input(p_event);
  1617. }
  1618. if (gui.key_event_accepted) {
  1619. set_input_as_handled();
  1620. return;
  1621. }
  1622. }
  1623. Control *from = gui.key_focus ? gui.key_focus : nullptr;
  1624. if (from && p_event->is_pressed()) {
  1625. Control *next = nullptr;
  1626. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  1627. if (joypadmotion_event.is_valid()) {
  1628. Input *input = Input::get_singleton();
  1629. if (p_event->is_action_pressed("ui_focus_next") && input->is_action_just_pressed("ui_focus_next")) {
  1630. next = from->find_next_valid_focus();
  1631. }
  1632. if (p_event->is_action_pressed("ui_focus_prev") && input->is_action_just_pressed("ui_focus_prev")) {
  1633. next = from->find_prev_valid_focus();
  1634. }
  1635. if (p_event->is_action_pressed("ui_up") && input->is_action_just_pressed("ui_up")) {
  1636. next = from->_get_focus_neighbor(SIDE_TOP);
  1637. }
  1638. if (p_event->is_action_pressed("ui_left") && input->is_action_just_pressed("ui_left")) {
  1639. next = from->_get_focus_neighbor(SIDE_LEFT);
  1640. }
  1641. if (p_event->is_action_pressed("ui_right") && input->is_action_just_pressed("ui_right")) {
  1642. next = from->_get_focus_neighbor(SIDE_RIGHT);
  1643. }
  1644. if (p_event->is_action_pressed("ui_down") && input->is_action_just_pressed("ui_down")) {
  1645. next = from->_get_focus_neighbor(SIDE_BOTTOM);
  1646. }
  1647. } else {
  1648. if (p_event->is_action_pressed("ui_focus_next", true, true)) {
  1649. next = from->find_next_valid_focus();
  1650. }
  1651. if (p_event->is_action_pressed("ui_focus_prev", true, true)) {
  1652. next = from->find_prev_valid_focus();
  1653. }
  1654. if (p_event->is_action_pressed("ui_up", true, true)) {
  1655. next = from->_get_focus_neighbor(SIDE_TOP);
  1656. }
  1657. if (p_event->is_action_pressed("ui_left", true, true)) {
  1658. next = from->_get_focus_neighbor(SIDE_LEFT);
  1659. }
  1660. if (p_event->is_action_pressed("ui_right", true, true)) {
  1661. next = from->_get_focus_neighbor(SIDE_RIGHT);
  1662. }
  1663. if (p_event->is_action_pressed("ui_down", true, true)) {
  1664. next = from->_get_focus_neighbor(SIDE_BOTTOM);
  1665. }
  1666. }
  1667. if (next) {
  1668. next->grab_focus();
  1669. set_input_as_handled();
  1670. }
  1671. }
  1672. }
  1673. }
  1674. void Viewport::_gui_cleanup_internal_state(Ref<InputEvent> p_event) {
  1675. ERR_FAIL_COND(p_event.is_null());
  1676. Ref<InputEventMouseButton> mb = p_event;
  1677. if (mb.is_valid()) {
  1678. if (!mb->is_pressed()) {
  1679. gui.mouse_focus_mask &= ~mouse_button_to_mask(mb->get_button_index()); // Remove from mask.
  1680. }
  1681. }
  1682. }
  1683. List<Control *>::Element *Viewport::_gui_add_root_control(Control *p_control) {
  1684. gui.roots_order_dirty = true;
  1685. return gui.roots.push_back(p_control);
  1686. }
  1687. void Viewport::_gui_set_root_order_dirty() {
  1688. gui.roots_order_dirty = true;
  1689. }
  1690. void Viewport::_gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control) {
  1691. ERR_FAIL_COND_MSG(p_data.get_type() == Variant::NIL, "Drag data must be a value.");
  1692. gui.dragging = true;
  1693. gui.drag_data = p_data;
  1694. gui.mouse_focus = nullptr;
  1695. if (p_control) {
  1696. _gui_set_drag_preview(p_base, p_control);
  1697. }
  1698. _propagate_viewport_notification(this, NOTIFICATION_DRAG_BEGIN);
  1699. }
  1700. void Viewport::_gui_set_drag_preview(Control *p_base, Control *p_control) {
  1701. ERR_FAIL_NULL(p_control);
  1702. ERR_FAIL_COND(!Object::cast_to<Control>((Object *)p_control));
  1703. ERR_FAIL_COND(p_control->is_inside_tree());
  1704. ERR_FAIL_COND(p_control->get_parent() != nullptr);
  1705. Control *drag_preview = _gui_get_drag_preview();
  1706. if (drag_preview) {
  1707. memdelete(drag_preview);
  1708. }
  1709. p_control->set_as_top_level(true);
  1710. p_control->set_position(gui.last_mouse_pos);
  1711. p_base->get_root_parent_control()->add_child(p_control); // Add as child of viewport.
  1712. p_control->raise();
  1713. gui.drag_preview_id = p_control->get_instance_id();
  1714. }
  1715. Control *Viewport::_gui_get_drag_preview() {
  1716. if (gui.drag_preview_id.is_null()) {
  1717. return nullptr;
  1718. } else {
  1719. Control *drag_preview = Object::cast_to<Control>(ObjectDB::get_instance(gui.drag_preview_id));
  1720. if (!drag_preview) {
  1721. ERR_PRINT("Don't free the control set as drag preview.");
  1722. gui.drag_preview_id = ObjectID();
  1723. }
  1724. return drag_preview;
  1725. }
  1726. }
  1727. void Viewport::_gui_remove_root_control(List<Control *>::Element *RI) {
  1728. gui.roots.erase(RI);
  1729. }
  1730. void Viewport::_gui_unfocus_control(Control *p_control) {
  1731. if (gui.key_focus == p_control) {
  1732. gui.key_focus->release_focus();
  1733. }
  1734. }
  1735. void Viewport::_gui_hide_control(Control *p_control) {
  1736. if (gui.mouse_focus == p_control) {
  1737. _drop_mouse_focus();
  1738. }
  1739. if (gui.key_focus == p_control) {
  1740. gui_release_focus();
  1741. }
  1742. if (gui.mouse_over == p_control) {
  1743. gui.mouse_over = nullptr;
  1744. }
  1745. if (gui.drag_mouse_over == p_control) {
  1746. gui.drag_mouse_over = nullptr;
  1747. }
  1748. if (gui.tooltip_control == p_control) {
  1749. _gui_cancel_tooltip();
  1750. }
  1751. }
  1752. void Viewport::_gui_remove_control(Control *p_control) {
  1753. if (gui.mouse_focus == p_control) {
  1754. gui.mouse_focus = nullptr;
  1755. gui.forced_mouse_focus = false;
  1756. gui.mouse_focus_mask = MouseButton::NONE;
  1757. }
  1758. if (gui.last_mouse_focus == p_control) {
  1759. gui.last_mouse_focus = nullptr;
  1760. }
  1761. if (gui.key_focus == p_control) {
  1762. gui.key_focus = nullptr;
  1763. }
  1764. if (gui.mouse_over == p_control) {
  1765. gui.mouse_over = nullptr;
  1766. }
  1767. if (gui.drag_mouse_over == p_control) {
  1768. gui.drag_mouse_over = nullptr;
  1769. }
  1770. if (gui.tooltip_control == p_control) {
  1771. gui.tooltip_control = nullptr;
  1772. }
  1773. }
  1774. Window *Viewport::get_base_window() const {
  1775. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  1776. Viewport *v = const_cast<Viewport *>(this);
  1777. Window *w = Object::cast_to<Window>(v);
  1778. while (!w) {
  1779. v = v->get_parent_viewport();
  1780. w = Object::cast_to<Window>(v);
  1781. }
  1782. return w;
  1783. }
  1784. void Viewport::_gui_remove_focus_for_window(Node *p_window) {
  1785. if (get_base_window() == p_window) {
  1786. gui_release_focus();
  1787. }
  1788. }
  1789. bool Viewport::_gui_control_has_focus(const Control *p_control) {
  1790. return gui.key_focus == p_control;
  1791. }
  1792. void Viewport::_gui_control_grab_focus(Control *p_control) {
  1793. if (gui.key_focus && gui.key_focus == p_control) {
  1794. // No need for change.
  1795. return;
  1796. }
  1797. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, "_viewports", "_gui_remove_focus_for_window", (Node *)get_base_window());
  1798. gui.key_focus = p_control;
  1799. emit_signal(SNAME("gui_focus_changed"), p_control);
  1800. p_control->notification(Control::NOTIFICATION_FOCUS_ENTER);
  1801. p_control->update();
  1802. }
  1803. void Viewport::_gui_accept_event() {
  1804. gui.key_event_accepted = true;
  1805. if (is_inside_tree()) {
  1806. set_input_as_handled();
  1807. }
  1808. }
  1809. void Viewport::_drop_mouse_over() {
  1810. if (gui.mouse_over) {
  1811. _gui_call_notification(gui.mouse_over, Control::NOTIFICATION_MOUSE_EXIT);
  1812. gui.mouse_over = nullptr;
  1813. }
  1814. }
  1815. void Viewport::_drop_mouse_focus() {
  1816. Control *c = gui.mouse_focus;
  1817. MouseButton mask = gui.mouse_focus_mask;
  1818. gui.mouse_focus = nullptr;
  1819. gui.forced_mouse_focus = false;
  1820. gui.mouse_focus_mask = MouseButton::NONE;
  1821. for (int i = 0; i < 3; i++) {
  1822. if ((int)mask & (1 << i)) {
  1823. Ref<InputEventMouseButton> mb;
  1824. mb.instantiate();
  1825. mb->set_position(c->get_local_mouse_position());
  1826. mb->set_global_position(c->get_local_mouse_position());
  1827. mb->set_button_index(MouseButton(i + 1));
  1828. mb->set_pressed(false);
  1829. c->_call_gui_input(mb);
  1830. }
  1831. }
  1832. }
  1833. void Viewport::_drop_physics_mouseover(bool p_paused_only) {
  1834. physics_has_last_mousepos = false;
  1835. _cleanup_mouseover_colliders(true, p_paused_only);
  1836. #ifndef _3D_DISABLED
  1837. if (physics_object_over.is_valid()) {
  1838. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(ObjectDB::get_instance(physics_object_over));
  1839. if (co) {
  1840. if (!co->is_inside_tree()) {
  1841. physics_object_over = ObjectID();
  1842. physics_object_capture = ObjectID();
  1843. } else if (!(p_paused_only && co->can_process())) {
  1844. co->_mouse_exit();
  1845. physics_object_over = ObjectID();
  1846. physics_object_capture = ObjectID();
  1847. }
  1848. }
  1849. }
  1850. #endif // _3D_DISABLED
  1851. }
  1852. void Viewport::_cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference) {
  1853. List<Map<ObjectID, uint64_t>::Element *> to_erase;
  1854. for (Map<ObjectID, uint64_t>::Element *E = physics_2d_mouseover.front(); E; E = E->next()) {
  1855. if (!p_clean_all_frames && E->get() == p_frame_reference) {
  1856. continue;
  1857. }
  1858. Object *o = ObjectDB::get_instance(E->key());
  1859. if (o) {
  1860. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
  1861. if (co && co->is_inside_tree()) {
  1862. if (p_clean_all_frames && p_paused_only && co->can_process()) {
  1863. continue;
  1864. }
  1865. co->_mouse_exit();
  1866. }
  1867. }
  1868. to_erase.push_back(E);
  1869. }
  1870. while (to_erase.size()) {
  1871. physics_2d_mouseover.erase(to_erase.front()->get());
  1872. to_erase.pop_front();
  1873. }
  1874. // Per-shape.
  1875. List<Map<Pair<ObjectID, int>, uint64_t, PairSort<ObjectID, int>>::Element *> shapes_to_erase;
  1876. for (Map<Pair<ObjectID, int>, uint64_t, PairSort<ObjectID, int>>::Element *E = physics_2d_shape_mouseover.front(); E; E = E->next()) {
  1877. if (!p_clean_all_frames && E->get() == p_frame_reference) {
  1878. continue;
  1879. }
  1880. Object *o = ObjectDB::get_instance(E->key().first);
  1881. if (o) {
  1882. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
  1883. if (co && co->is_inside_tree()) {
  1884. if (p_clean_all_frames && p_paused_only && co->can_process()) {
  1885. continue;
  1886. }
  1887. co->_mouse_shape_exit(E->key().second);
  1888. }
  1889. }
  1890. shapes_to_erase.push_back(E);
  1891. }
  1892. while (shapes_to_erase.size()) {
  1893. physics_2d_shape_mouseover.erase(shapes_to_erase.front()->get());
  1894. shapes_to_erase.pop_front();
  1895. }
  1896. }
  1897. void Viewport::_gui_grab_click_focus(Control *p_control) {
  1898. gui.mouse_click_grabber = p_control;
  1899. call_deferred(SNAME("_post_gui_grab_click_focus"));
  1900. }
  1901. void Viewport::_post_gui_grab_click_focus() {
  1902. Control *focus_grabber = gui.mouse_click_grabber;
  1903. if (!focus_grabber) {
  1904. // Redundant grab requests were made.
  1905. return;
  1906. }
  1907. gui.mouse_click_grabber = nullptr;
  1908. if (gui.mouse_focus) {
  1909. if (gui.mouse_focus == focus_grabber) {
  1910. return;
  1911. }
  1912. MouseButton mask = gui.mouse_focus_mask;
  1913. Point2 click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  1914. for (int i = 0; i < 3; i++) {
  1915. if ((int)mask & (1 << i)) {
  1916. Ref<InputEventMouseButton> mb;
  1917. mb.instantiate();
  1918. // Send unclick.
  1919. mb->set_position(click);
  1920. mb->set_button_index(MouseButton(i + 1));
  1921. mb->set_pressed(false);
  1922. gui.mouse_focus->_call_gui_input(mb);
  1923. }
  1924. }
  1925. gui.mouse_focus = focus_grabber;
  1926. gui.focus_inv_xform = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse();
  1927. click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  1928. for (int i = 0; i < 3; i++) {
  1929. if ((int)mask & (1 << i)) {
  1930. Ref<InputEventMouseButton> mb;
  1931. mb.instantiate();
  1932. // Send click.
  1933. mb->set_position(click);
  1934. mb->set_button_index(MouseButton(i + 1));
  1935. mb->set_pressed(true);
  1936. MessageQueue::get_singleton()->push_callable(callable_mp(gui.mouse_focus, &Control::_call_gui_input), mb);
  1937. }
  1938. }
  1939. }
  1940. }
  1941. ///////////////////////////////
  1942. void Viewport::push_text_input(const String &p_text) {
  1943. if (gui.subwindow_focused) {
  1944. gui.subwindow_focused->push_text_input(p_text);
  1945. return;
  1946. }
  1947. if (gui.key_focus) {
  1948. gui.key_focus->call("set_text", p_text);
  1949. }
  1950. }
  1951. Viewport::SubWindowResize Viewport::_sub_window_get_resize_margin(Window *p_subwindow, const Point2 &p_point) {
  1952. if (p_subwindow->get_flag(Window::FLAG_BORDERLESS) || p_subwindow->get_flag(Window::FLAG_RESIZE_DISABLED)) {
  1953. return SUB_WINDOW_RESIZE_DISABLED;
  1954. }
  1955. Rect2i r = Rect2i(p_subwindow->get_position(), p_subwindow->get_size());
  1956. int title_height = p_subwindow->get_theme_constant(SNAME("title_height"));
  1957. r.position.y -= title_height;
  1958. r.size.y += title_height;
  1959. if (r.has_point(p_point)) {
  1960. return SUB_WINDOW_RESIZE_DISABLED; // It's inside, so no resize.
  1961. }
  1962. int dist_x = p_point.x < r.position.x ? (p_point.x - r.position.x) : (p_point.x > (r.position.x + r.size.x) ? (p_point.x - (r.position.x + r.size.x)) : 0);
  1963. int dist_y = p_point.y < r.position.y ? (p_point.y - r.position.y) : (p_point.y > (r.position.y + r.size.y) ? (p_point.y - (r.position.y + r.size.y)) : 0);
  1964. int limit = p_subwindow->get_theme_constant(SNAME("resize_margin"));
  1965. if (ABS(dist_x) > limit) {
  1966. return SUB_WINDOW_RESIZE_DISABLED;
  1967. }
  1968. if (ABS(dist_y) > limit) {
  1969. return SUB_WINDOW_RESIZE_DISABLED;
  1970. }
  1971. if (dist_x < 0 && dist_y < 0) {
  1972. return SUB_WINDOW_RESIZE_TOP_LEFT;
  1973. }
  1974. if (dist_x == 0 && dist_y < 0) {
  1975. return SUB_WINDOW_RESIZE_TOP;
  1976. }
  1977. if (dist_x > 0 && dist_y < 0) {
  1978. return SUB_WINDOW_RESIZE_TOP_RIGHT;
  1979. }
  1980. if (dist_x < 0 && dist_y == 0) {
  1981. return SUB_WINDOW_RESIZE_LEFT;
  1982. }
  1983. if (dist_x > 0 && dist_y == 0) {
  1984. return SUB_WINDOW_RESIZE_RIGHT;
  1985. }
  1986. if (dist_x < 0 && dist_y > 0) {
  1987. return SUB_WINDOW_RESIZE_BOTTOM_LEFT;
  1988. }
  1989. if (dist_x == 0 && dist_y > 0) {
  1990. return SUB_WINDOW_RESIZE_BOTTOM;
  1991. }
  1992. if (dist_x > 0 && dist_y > 0) {
  1993. return SUB_WINDOW_RESIZE_BOTTOM_RIGHT;
  1994. }
  1995. return SUB_WINDOW_RESIZE_DISABLED;
  1996. }
  1997. bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) {
  1998. if (gui.subwindow_drag != SUB_WINDOW_DRAG_DISABLED) {
  1999. ERR_FAIL_COND_V(gui.subwindow_focused == nullptr, false);
  2000. Ref<InputEventMouseButton> mb = p_event;
  2001. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  2002. if (gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE) {
  2003. if (gui.subwindow_drag_close_rect.has_point(mb->get_position())) {
  2004. // Close window.
  2005. gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST);
  2006. }
  2007. }
  2008. gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
  2009. if (gui.subwindow_focused != nullptr) { // May have been erased.
  2010. _sub_window_update(gui.subwindow_focused);
  2011. }
  2012. }
  2013. Ref<InputEventMouseMotion> mm = p_event;
  2014. if (mm.is_valid()) {
  2015. if (gui.subwindow_drag == SUB_WINDOW_DRAG_MOVE) {
  2016. Vector2 diff = mm->get_position() - gui.subwindow_drag_from;
  2017. Rect2i new_rect(gui.subwindow_drag_pos + diff, gui.subwindow_focused->get_size());
  2018. if (gui.subwindow_focused->is_clamped_to_embedder()) {
  2019. Size2i limit = get_visible_rect().size;
  2020. if (new_rect.position.x + new_rect.size.x > limit.x) {
  2021. new_rect.position.x = limit.x - new_rect.size.x;
  2022. }
  2023. if (new_rect.position.y + new_rect.size.y > limit.y) {
  2024. new_rect.position.y = limit.y - new_rect.size.y;
  2025. }
  2026. if (new_rect.position.x < 0) {
  2027. new_rect.position.x = 0;
  2028. }
  2029. int title_height = gui.subwindow_focused->get_flag(Window::FLAG_BORDERLESS) ? 0 : gui.subwindow_focused->get_theme_constant(SNAME("title_height"));
  2030. if (new_rect.position.y < title_height) {
  2031. new_rect.position.y = title_height;
  2032. }
  2033. }
  2034. gui.subwindow_focused->_rect_changed_callback(new_rect);
  2035. }
  2036. if (gui.subwindow_drag == SUB_WINDOW_DRAG_CLOSE) {
  2037. gui.subwindow_drag_close_inside = gui.subwindow_drag_close_rect.has_point(mm->get_position());
  2038. }
  2039. if (gui.subwindow_drag == SUB_WINDOW_DRAG_RESIZE) {
  2040. Vector2i diff = mm->get_position() - gui.subwindow_drag_from;
  2041. Size2i min_size = gui.subwindow_focused->get_min_size();
  2042. if (gui.subwindow_focused->is_wrapping_controls()) {
  2043. Size2i cms = gui.subwindow_focused->get_contents_minimum_size();
  2044. min_size.x = MAX(cms.x, min_size.x);
  2045. min_size.y = MAX(cms.y, min_size.y);
  2046. }
  2047. min_size.x = MAX(min_size.x, 1);
  2048. min_size.y = MAX(min_size.y, 1);
  2049. Rect2i r = gui.subwindow_resize_from_rect;
  2050. Size2i limit = r.size - min_size;
  2051. switch (gui.subwindow_resize_mode) {
  2052. case SUB_WINDOW_RESIZE_TOP_LEFT: {
  2053. diff.x = MIN(diff.x, limit.x);
  2054. diff.y = MIN(diff.y, limit.y);
  2055. r.position += diff;
  2056. r.size -= diff;
  2057. } break;
  2058. case SUB_WINDOW_RESIZE_TOP: {
  2059. diff.x = 0;
  2060. diff.y = MIN(diff.y, limit.y);
  2061. r.position += diff;
  2062. r.size -= diff;
  2063. } break;
  2064. case SUB_WINDOW_RESIZE_TOP_RIGHT: {
  2065. diff.x = MAX(diff.x, -limit.x);
  2066. diff.y = MIN(diff.y, limit.y);
  2067. r.position.y += diff.y;
  2068. r.size.y -= diff.y;
  2069. r.size.x += diff.x;
  2070. } break;
  2071. case SUB_WINDOW_RESIZE_LEFT: {
  2072. diff.x = MIN(diff.x, limit.x);
  2073. diff.y = 0;
  2074. r.position += diff;
  2075. r.size -= diff;
  2076. } break;
  2077. case SUB_WINDOW_RESIZE_RIGHT: {
  2078. diff.x = MAX(diff.x, -limit.x);
  2079. r.size.x += diff.x;
  2080. } break;
  2081. case SUB_WINDOW_RESIZE_BOTTOM_LEFT: {
  2082. diff.x = MIN(diff.x, limit.x);
  2083. diff.y = MAX(diff.y, -limit.y);
  2084. r.position.x += diff.x;
  2085. r.size.x -= diff.x;
  2086. r.size.y += diff.y;
  2087. } break;
  2088. case SUB_WINDOW_RESIZE_BOTTOM: {
  2089. diff.y = MAX(diff.y, -limit.y);
  2090. r.size.y += diff.y;
  2091. } break;
  2092. case SUB_WINDOW_RESIZE_BOTTOM_RIGHT: {
  2093. diff.x = MAX(diff.x, -limit.x);
  2094. diff.y = MAX(diff.y, -limit.y);
  2095. r.size += diff;
  2096. } break;
  2097. default: {
  2098. }
  2099. }
  2100. gui.subwindow_focused->_rect_changed_callback(r);
  2101. }
  2102. if (gui.subwindow_focused) { // May have been erased.
  2103. _sub_window_update(gui.subwindow_focused);
  2104. }
  2105. }
  2106. return true; // Handled.
  2107. }
  2108. Ref<InputEventMouseButton> mb = p_event;
  2109. // If the event is a mouse button, we need to check whether another window was clicked.
  2110. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  2111. bool click_on_window = false;
  2112. for (int i = gui.sub_windows.size() - 1; i >= 0; i--) {
  2113. SubWindow sw = gui.sub_windows.write[i];
  2114. // Clicked inside window?
  2115. Rect2i r = Rect2i(sw.window->get_position(), sw.window->get_size());
  2116. if (!sw.window->get_flag(Window::FLAG_BORDERLESS)) {
  2117. // Check top bar.
  2118. int title_height = sw.window->get_theme_constant(SNAME("title_height"));
  2119. Rect2i title_bar = r;
  2120. title_bar.position.y -= title_height;
  2121. title_bar.size.y = title_height;
  2122. if (title_bar.has_point(mb->get_position())) {
  2123. click_on_window = true;
  2124. int close_h_ofs = sw.window->get_theme_constant(SNAME("close_h_offset"));
  2125. int close_v_ofs = sw.window->get_theme_constant(SNAME("close_v_offset"));
  2126. Ref<Texture2D> close_icon = sw.window->get_theme_icon(SNAME("close"));
  2127. Rect2 close_rect;
  2128. close_rect.position = Vector2(r.position.x + r.size.x - close_v_ofs, r.position.y - close_h_ofs);
  2129. close_rect.size = close_icon->get_size();
  2130. if (gui.subwindow_focused != sw.window) {
  2131. // Refocus.
  2132. _sub_window_grab_focus(sw.window);
  2133. }
  2134. if (close_rect.has_point(mb->get_position())) {
  2135. gui.subwindow_drag = SUB_WINDOW_DRAG_CLOSE;
  2136. gui.subwindow_drag_close_inside = true; // Starts inside.
  2137. gui.subwindow_drag_close_rect = close_rect;
  2138. } else {
  2139. gui.subwindow_drag = SUB_WINDOW_DRAG_MOVE;
  2140. }
  2141. gui.subwindow_drag_from = mb->get_position();
  2142. gui.subwindow_drag_pos = sw.window->get_position();
  2143. _sub_window_update(sw.window);
  2144. } else {
  2145. gui.subwindow_resize_mode = _sub_window_get_resize_margin(sw.window, mb->get_position());
  2146. if (gui.subwindow_resize_mode != SUB_WINDOW_RESIZE_DISABLED) {
  2147. gui.subwindow_resize_from_rect = r;
  2148. gui.subwindow_drag_from = mb->get_position();
  2149. gui.subwindow_drag = SUB_WINDOW_DRAG_RESIZE;
  2150. click_on_window = true;
  2151. }
  2152. }
  2153. }
  2154. if (!click_on_window && r.has_point(mb->get_position())) {
  2155. // Clicked, see if it needs to fetch focus.
  2156. if (gui.subwindow_focused != sw.window) {
  2157. // Refocus.
  2158. _sub_window_grab_focus(sw.window);
  2159. }
  2160. click_on_window = true;
  2161. }
  2162. if (click_on_window) {
  2163. break;
  2164. }
  2165. }
  2166. if (!click_on_window && gui.subwindow_focused) {
  2167. // No window found and clicked, remove focus.
  2168. _sub_window_grab_focus(nullptr);
  2169. }
  2170. }
  2171. if (gui.subwindow_focused) {
  2172. Ref<InputEventMouseMotion> mm = p_event;
  2173. if (mm.is_valid()) {
  2174. SubWindowResize resize = _sub_window_get_resize_margin(gui.subwindow_focused, mm->get_position());
  2175. if (resize != SUB_WINDOW_RESIZE_DISABLED) {
  2176. DisplayServer::CursorShape shapes[SUB_WINDOW_RESIZE_MAX] = {
  2177. DisplayServer::CURSOR_ARROW,
  2178. DisplayServer::CURSOR_FDIAGSIZE,
  2179. DisplayServer::CURSOR_VSIZE,
  2180. DisplayServer::CURSOR_BDIAGSIZE,
  2181. DisplayServer::CURSOR_HSIZE,
  2182. DisplayServer::CURSOR_HSIZE,
  2183. DisplayServer::CURSOR_BDIAGSIZE,
  2184. DisplayServer::CURSOR_VSIZE,
  2185. DisplayServer::CURSOR_FDIAGSIZE
  2186. };
  2187. DisplayServer::get_singleton()->cursor_set_shape(shapes[resize]);
  2188. return true; // Reserved for showing the resize cursor.
  2189. }
  2190. }
  2191. }
  2192. if (gui.subwindow_drag != SUB_WINDOW_DRAG_DISABLED) {
  2193. return true; // Dragging, don't pass the event.
  2194. }
  2195. if (!gui.subwindow_focused) {
  2196. return false;
  2197. }
  2198. Transform2D window_ofs;
  2199. window_ofs.set_origin(-gui.subwindow_focused->get_position());
  2200. Ref<InputEvent> ev = p_event->xformed_by(window_ofs);
  2201. gui.subwindow_focused->_window_input(ev);
  2202. return true;
  2203. }
  2204. void Viewport::push_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
  2205. ERR_FAIL_COND(!is_inside_tree());
  2206. if (disable_input) {
  2207. return;
  2208. }
  2209. if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_ancestor_of(this)) {
  2210. return;
  2211. }
  2212. local_input_handled = false;
  2213. Ref<InputEvent> ev;
  2214. if (!p_local_coords) {
  2215. ev = _make_input_local(p_event);
  2216. } else {
  2217. ev = p_event;
  2218. }
  2219. if (is_embedding_subwindows() && _sub_windows_forward_input(ev)) {
  2220. set_input_as_handled();
  2221. return;
  2222. }
  2223. if (!_can_consume_input_events()) {
  2224. return;
  2225. }
  2226. if (!is_input_handled()) {
  2227. get_tree()->_call_input_pause(input_group, SceneTree::CALL_INPUT_TYPE_INPUT, ev, this); //not a bug, must happen before GUI, order is _input -> gui input -> _unhandled input
  2228. }
  2229. if (!is_input_handled()) {
  2230. _gui_input_event(ev);
  2231. } else {
  2232. // Cleanup internal GUI state after accepting event during _input().
  2233. _gui_cleanup_internal_state(ev);
  2234. }
  2235. event_count++;
  2236. }
  2237. void Viewport::push_unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
  2238. ERR_FAIL_COND(p_event.is_null());
  2239. ERR_FAIL_COND(!is_inside_tree());
  2240. local_input_handled = false;
  2241. if (disable_input || !_can_consume_input_events()) {
  2242. return;
  2243. }
  2244. if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_ancestor_of(this)) {
  2245. return;
  2246. }
  2247. Ref<InputEvent> ev;
  2248. if (!p_local_coords) {
  2249. ev = _make_input_local(p_event);
  2250. } else {
  2251. ev = p_event;
  2252. }
  2253. // Shortcut Input.
  2254. if (Object::cast_to<InputEventKey>(*ev) != nullptr || Object::cast_to<InputEventShortcut>(*ev) != nullptr) {
  2255. get_tree()->_call_input_pause(shortcut_input_group, SceneTree::CALL_INPUT_TYPE_SHORTCUT_INPUT, ev, this);
  2256. }
  2257. // Unhandled Input.
  2258. if (!is_input_handled()) {
  2259. get_tree()->_call_input_pause(unhandled_input_group, SceneTree::CALL_INPUT_TYPE_UNHANDLED_INPUT, ev, this);
  2260. }
  2261. // Unhandled key Input - Used for performance reasons - This is called a lot less than _unhandled_input since it ignores MouseMotion, and to handle Unicode input with Alt / Ctrl modifiers after handling shortcuts.
  2262. if (!is_input_handled() && (Object::cast_to<InputEventKey>(*ev) != nullptr)) {
  2263. get_tree()->_call_input_pause(unhandled_key_input_group, SceneTree::CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT, ev, this);
  2264. }
  2265. if (physics_object_picking && !is_input_handled()) {
  2266. if (Input::get_singleton()->get_mouse_mode() != Input::MOUSE_MODE_CAPTURED &&
  2267. (Object::cast_to<InputEventMouseButton>(*ev) ||
  2268. Object::cast_to<InputEventMouseMotion>(*ev) ||
  2269. Object::cast_to<InputEventScreenDrag>(*ev) ||
  2270. Object::cast_to<InputEventScreenTouch>(*ev) ||
  2271. Object::cast_to<InputEventKey>(*ev) // To remember state.
  2272. )) {
  2273. physics_picking_events.push_back(ev);
  2274. }
  2275. }
  2276. }
  2277. void Viewport::set_physics_object_picking(bool p_enable) {
  2278. physics_object_picking = p_enable;
  2279. if (physics_object_picking) {
  2280. add_to_group("_picking_viewports");
  2281. } else {
  2282. physics_picking_events.clear();
  2283. if (is_in_group("_picking_viewports")) {
  2284. remove_from_group("_picking_viewports");
  2285. }
  2286. }
  2287. }
  2288. bool Viewport::get_physics_object_picking() {
  2289. return physics_object_picking;
  2290. }
  2291. Vector2 Viewport::get_camera_coords(const Vector2 &p_viewport_coords) const {
  2292. Transform2D xf = get_final_transform();
  2293. return xf.xform(p_viewport_coords);
  2294. }
  2295. Vector2 Viewport::get_camera_rect_size() const {
  2296. return size;
  2297. }
  2298. void Viewport::set_disable_input(bool p_disable) {
  2299. if (p_disable == disable_input) {
  2300. return;
  2301. }
  2302. if (p_disable) {
  2303. _drop_mouse_focus();
  2304. _drop_mouse_over();
  2305. _gui_cancel_tooltip();
  2306. }
  2307. disable_input = p_disable;
  2308. }
  2309. bool Viewport::is_input_disabled() const {
  2310. return disable_input;
  2311. }
  2312. Variant Viewport::gui_get_drag_data() const {
  2313. return gui.drag_data;
  2314. }
  2315. TypedArray<String> Viewport::get_configuration_warnings() const {
  2316. TypedArray<String> warnings = Node::get_configuration_warnings();
  2317. if (size.x == 0 || size.y == 0) {
  2318. warnings.push_back(RTR("Viewport size must be greater than 0 to render anything."));
  2319. }
  2320. return warnings;
  2321. }
  2322. void Viewport::gui_reset_canvas_sort_index() {
  2323. gui.canvas_sort_index = 0;
  2324. }
  2325. int Viewport::gui_get_canvas_sort_index() {
  2326. return gui.canvas_sort_index++;
  2327. }
  2328. void Viewport::gui_release_focus() {
  2329. if (gui.key_focus) {
  2330. Control *f = gui.key_focus;
  2331. gui.key_focus = nullptr;
  2332. f->notification(Control::NOTIFICATION_FOCUS_EXIT, true);
  2333. f->update();
  2334. }
  2335. }
  2336. Control *Viewport::gui_get_focus_owner() {
  2337. return gui.key_focus;
  2338. }
  2339. void Viewport::set_msaa(MSAA p_msaa) {
  2340. ERR_FAIL_INDEX(p_msaa, MSAA_MAX);
  2341. if (msaa == p_msaa) {
  2342. return;
  2343. }
  2344. msaa = p_msaa;
  2345. RS::get_singleton()->viewport_set_msaa(viewport, RS::ViewportMSAA(p_msaa));
  2346. }
  2347. Viewport::MSAA Viewport::get_msaa() const {
  2348. return msaa;
  2349. }
  2350. void Viewport::set_screen_space_aa(ScreenSpaceAA p_screen_space_aa) {
  2351. ERR_FAIL_INDEX(p_screen_space_aa, SCREEN_SPACE_AA_MAX);
  2352. if (screen_space_aa == p_screen_space_aa) {
  2353. return;
  2354. }
  2355. screen_space_aa = p_screen_space_aa;
  2356. RS::get_singleton()->viewport_set_screen_space_aa(viewport, RS::ViewportScreenSpaceAA(p_screen_space_aa));
  2357. }
  2358. Viewport::ScreenSpaceAA Viewport::get_screen_space_aa() const {
  2359. return screen_space_aa;
  2360. }
  2361. void Viewport::set_use_debanding(bool p_use_debanding) {
  2362. if (use_debanding == p_use_debanding) {
  2363. return;
  2364. }
  2365. use_debanding = p_use_debanding;
  2366. RS::get_singleton()->viewport_set_use_debanding(viewport, p_use_debanding);
  2367. }
  2368. bool Viewport::is_using_debanding() const {
  2369. return use_debanding;
  2370. }
  2371. void Viewport::set_mesh_lod_threshold(float p_pixels) {
  2372. mesh_lod_threshold = p_pixels;
  2373. RS::get_singleton()->viewport_set_mesh_lod_threshold(viewport, mesh_lod_threshold);
  2374. }
  2375. float Viewport::get_mesh_lod_threshold() const {
  2376. return mesh_lod_threshold;
  2377. }
  2378. void Viewport::set_use_occlusion_culling(bool p_use_occlusion_culling) {
  2379. if (use_occlusion_culling == p_use_occlusion_culling) {
  2380. return;
  2381. }
  2382. use_occlusion_culling = p_use_occlusion_culling;
  2383. RS::get_singleton()->viewport_set_use_occlusion_culling(viewport, p_use_occlusion_culling);
  2384. notify_property_list_changed();
  2385. }
  2386. bool Viewport::is_using_occlusion_culling() const {
  2387. return use_occlusion_culling;
  2388. }
  2389. void Viewport::set_debug_draw(DebugDraw p_debug_draw) {
  2390. debug_draw = p_debug_draw;
  2391. RS::get_singleton()->viewport_set_debug_draw(viewport, RS::ViewportDebugDraw(p_debug_draw));
  2392. }
  2393. Viewport::DebugDraw Viewport::get_debug_draw() const {
  2394. return debug_draw;
  2395. }
  2396. int Viewport::get_render_info(RenderInfoType p_type, RenderInfo p_info) {
  2397. return RS::get_singleton()->viewport_get_render_info(viewport, RS::ViewportRenderInfoType(p_type), RS::ViewportRenderInfo(p_info));
  2398. }
  2399. void Viewport::set_snap_controls_to_pixels(bool p_enable) {
  2400. snap_controls_to_pixels = p_enable;
  2401. }
  2402. bool Viewport::is_snap_controls_to_pixels_enabled() const {
  2403. return snap_controls_to_pixels;
  2404. }
  2405. void Viewport::set_snap_2d_transforms_to_pixel(bool p_enable) {
  2406. snap_2d_transforms_to_pixel = p_enable;
  2407. RS::get_singleton()->viewport_set_snap_2d_transforms_to_pixel(viewport, snap_2d_transforms_to_pixel);
  2408. }
  2409. bool Viewport::is_snap_2d_transforms_to_pixel_enabled() const {
  2410. return snap_2d_transforms_to_pixel;
  2411. }
  2412. void Viewport::set_snap_2d_vertices_to_pixel(bool p_enable) {
  2413. snap_2d_vertices_to_pixel = p_enable;
  2414. RS::get_singleton()->viewport_set_snap_2d_vertices_to_pixel(viewport, snap_2d_vertices_to_pixel);
  2415. }
  2416. bool Viewport::is_snap_2d_vertices_to_pixel_enabled() const {
  2417. return snap_2d_vertices_to_pixel;
  2418. }
  2419. bool Viewport::gui_is_dragging() const {
  2420. return gui.dragging;
  2421. }
  2422. bool Viewport::gui_is_drag_successful() const {
  2423. return gui.drag_successful;
  2424. }
  2425. void Viewport::set_input_as_handled() {
  2426. _drop_physics_mouseover();
  2427. if (!handle_input_locally) {
  2428. ERR_FAIL_COND(!is_inside_tree());
  2429. Viewport *vp = this;
  2430. while (true) {
  2431. if (Object::cast_to<Window>(vp)) {
  2432. break;
  2433. }
  2434. if (!vp->get_parent()) {
  2435. break;
  2436. }
  2437. vp = vp->get_parent()->get_viewport();
  2438. }
  2439. if (vp != this) {
  2440. vp->set_input_as_handled();
  2441. return;
  2442. }
  2443. }
  2444. local_input_handled = true;
  2445. }
  2446. bool Viewport::is_input_handled() const {
  2447. if (!handle_input_locally) {
  2448. ERR_FAIL_COND_V(!is_inside_tree(), false);
  2449. const Viewport *vp = this;
  2450. while (true) {
  2451. if (Object::cast_to<Window>(vp)) {
  2452. break;
  2453. }
  2454. if (!vp->get_parent()) {
  2455. break;
  2456. }
  2457. vp = vp->get_parent()->get_viewport();
  2458. }
  2459. if (vp != this) {
  2460. return vp->is_input_handled();
  2461. }
  2462. }
  2463. return local_input_handled;
  2464. }
  2465. void Viewport::set_handle_input_locally(bool p_enable) {
  2466. handle_input_locally = p_enable;
  2467. }
  2468. bool Viewport::is_handling_input_locally() const {
  2469. return handle_input_locally;
  2470. }
  2471. void Viewport::set_default_canvas_item_texture_filter(DefaultCanvasItemTextureFilter p_filter) {
  2472. ERR_FAIL_INDEX(p_filter, DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_MAX);
  2473. if (default_canvas_item_texture_filter == p_filter) {
  2474. return;
  2475. }
  2476. default_canvas_item_texture_filter = p_filter;
  2477. switch (default_canvas_item_texture_filter) {
  2478. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
  2479. RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  2480. break;
  2481. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR:
  2482. RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR);
  2483. break;
  2484. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
  2485. RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS);
  2486. break;
  2487. case DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
  2488. RS::get_singleton()->viewport_set_default_canvas_item_texture_filter(viewport, RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  2489. break;
  2490. default: {
  2491. }
  2492. }
  2493. }
  2494. Viewport::DefaultCanvasItemTextureFilter Viewport::get_default_canvas_item_texture_filter() const {
  2495. return default_canvas_item_texture_filter;
  2496. }
  2497. void Viewport::set_default_canvas_item_texture_repeat(DefaultCanvasItemTextureRepeat p_repeat) {
  2498. ERR_FAIL_INDEX(p_repeat, DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MAX);
  2499. if (default_canvas_item_texture_repeat == p_repeat) {
  2500. return;
  2501. }
  2502. default_canvas_item_texture_repeat = p_repeat;
  2503. switch (default_canvas_item_texture_repeat) {
  2504. case DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_DISABLED:
  2505. RS::get_singleton()->viewport_set_default_canvas_item_texture_repeat(viewport, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  2506. break;
  2507. case DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_ENABLED:
  2508. RS::get_singleton()->viewport_set_default_canvas_item_texture_repeat(viewport, RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  2509. break;
  2510. case DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MIRROR:
  2511. RS::get_singleton()->viewport_set_default_canvas_item_texture_repeat(viewport, RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR);
  2512. break;
  2513. default: {
  2514. }
  2515. }
  2516. }
  2517. Viewport::DefaultCanvasItemTextureRepeat Viewport::get_default_canvas_item_texture_repeat() const {
  2518. return default_canvas_item_texture_repeat;
  2519. }
  2520. DisplayServer::WindowID Viewport::get_window_id() const {
  2521. return DisplayServer::MAIN_WINDOW_ID;
  2522. }
  2523. Viewport *Viewport::get_parent_viewport() const {
  2524. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  2525. if (!get_parent()) {
  2526. return nullptr; //root viewport
  2527. }
  2528. return get_parent()->get_viewport();
  2529. }
  2530. void Viewport::set_embedding_subwindows(bool p_embed) {
  2531. gui.embed_subwindows_hint = p_embed;
  2532. }
  2533. bool Viewport::is_embedding_subwindows() const {
  2534. return gui.embed_subwindows_hint;
  2535. }
  2536. void Viewport::pass_mouse_focus_to(Viewport *p_viewport, Control *p_control) {
  2537. ERR_FAIL_NULL(p_viewport);
  2538. ERR_FAIL_NULL(p_control);
  2539. if (gui.mouse_focus) {
  2540. p_viewport->gui.mouse_focus = p_control;
  2541. p_viewport->gui.mouse_focus_mask = gui.mouse_focus_mask;
  2542. p_viewport->gui.key_focus = p_control;
  2543. p_viewport->gui.forced_mouse_focus = true;
  2544. gui.mouse_focus = nullptr;
  2545. gui.forced_mouse_focus = false;
  2546. gui.mouse_focus_mask = MouseButton::NONE;
  2547. }
  2548. }
  2549. void Viewport::set_sdf_oversize(SDFOversize p_sdf_oversize) {
  2550. ERR_FAIL_INDEX(p_sdf_oversize, SDF_OVERSIZE_MAX);
  2551. sdf_oversize = p_sdf_oversize;
  2552. RS::get_singleton()->viewport_set_sdf_oversize_and_scale(viewport, RS::ViewportSDFOversize(sdf_oversize), RS::ViewportSDFScale(sdf_scale));
  2553. }
  2554. Viewport::SDFOversize Viewport::get_sdf_oversize() const {
  2555. return sdf_oversize;
  2556. }
  2557. void Viewport::set_sdf_scale(SDFScale p_sdf_scale) {
  2558. ERR_FAIL_INDEX(p_sdf_scale, SDF_SCALE_MAX);
  2559. sdf_scale = p_sdf_scale;
  2560. RS::get_singleton()->viewport_set_sdf_oversize_and_scale(viewport, RS::ViewportSDFOversize(sdf_oversize), RS::ViewportSDFScale(sdf_scale));
  2561. }
  2562. Viewport::SDFScale Viewport::get_sdf_scale() const {
  2563. return sdf_scale;
  2564. }
  2565. Transform2D Viewport::get_screen_transform() const {
  2566. return _get_input_pre_xform().affine_inverse() * get_final_transform();
  2567. }
  2568. #ifndef _3D_DISABLED
  2569. AudioListener3D *Viewport::get_audio_listener_3d() const {
  2570. return audio_listener_3d;
  2571. }
  2572. void Viewport::set_as_audio_listener_3d(bool p_enable) {
  2573. if (p_enable == is_audio_listener_3d_enabled) {
  2574. return;
  2575. }
  2576. is_audio_listener_3d_enabled = p_enable;
  2577. _update_audio_listener_3d();
  2578. }
  2579. bool Viewport::is_audio_listener_3d() const {
  2580. return is_audio_listener_3d_enabled;
  2581. }
  2582. void Viewport::_update_audio_listener_3d() {
  2583. if (AudioServer::get_singleton()) {
  2584. AudioServer::get_singleton()->notify_listener_changed();
  2585. }
  2586. }
  2587. void Viewport::_listener_transform_3d_changed_notify() {
  2588. }
  2589. void Viewport::_audio_listener_3d_set(AudioListener3D *p_listener) {
  2590. if (audio_listener_3d == p_listener) {
  2591. return;
  2592. }
  2593. audio_listener_3d = p_listener;
  2594. _update_audio_listener_3d();
  2595. _listener_transform_3d_changed_notify();
  2596. }
  2597. bool Viewport::_audio_listener_3d_add(AudioListener3D *p_listener) {
  2598. audio_listener_3d_set.insert(p_listener);
  2599. return audio_listener_3d_set.size() == 1;
  2600. }
  2601. void Viewport::_audio_listener_3d_remove(AudioListener3D *p_listener) {
  2602. audio_listener_3d_set.erase(p_listener);
  2603. if (audio_listener_3d == p_listener) {
  2604. audio_listener_3d = nullptr;
  2605. }
  2606. }
  2607. void Viewport::_audio_listener_3d_make_next_current(AudioListener3D *p_exclude) {
  2608. if (audio_listener_3d_set.size() > 0) {
  2609. for (Set<AudioListener3D *>::Element *E = audio_listener_3d_set.front(); E; E = E->next()) {
  2610. if (p_exclude == E->get()) {
  2611. continue;
  2612. }
  2613. if (!E->get()->is_inside_tree()) {
  2614. continue;
  2615. }
  2616. if (audio_listener_3d != nullptr) {
  2617. return;
  2618. }
  2619. E->get()->make_current();
  2620. }
  2621. } else {
  2622. // Attempt to reset listener to the camera position.
  2623. if (camera_3d != nullptr) {
  2624. _update_audio_listener_3d();
  2625. _camera_3d_transform_changed_notify();
  2626. }
  2627. }
  2628. }
  2629. void Viewport::_collision_object_3d_input_event(CollisionObject3D *p_object, Camera3D *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) {
  2630. Transform3D object_transform = p_object->get_global_transform();
  2631. Transform3D camera_transform = p_camera->get_global_transform();
  2632. ObjectID id = p_object->get_instance_id();
  2633. // Avoid sending the fake event unnecessarily if nothing really changed in the context.
  2634. if (object_transform == physics_last_object_transform && camera_transform == physics_last_camera_transform && physics_last_id == id) {
  2635. Ref<InputEventMouseMotion> mm = p_input_event;
  2636. if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
  2637. return; // Discarded.
  2638. }
  2639. }
  2640. p_object->_input_event_call(camera_3d, p_input_event, p_pos, p_normal, p_shape);
  2641. physics_last_object_transform = object_transform;
  2642. physics_last_camera_transform = camera_transform;
  2643. physics_last_id = id;
  2644. }
  2645. Camera3D *Viewport::get_camera_3d() const {
  2646. return camera_3d;
  2647. }
  2648. void Viewport::_camera_3d_transform_changed_notify() {
  2649. }
  2650. void Viewport::_camera_3d_set(Camera3D *p_camera) {
  2651. if (camera_3d == p_camera) {
  2652. return;
  2653. }
  2654. if (camera_3d) {
  2655. camera_3d->notification(Camera3D::NOTIFICATION_LOST_CURRENT);
  2656. }
  2657. camera_3d = p_camera;
  2658. if (!camera_3d_override) {
  2659. if (camera_3d) {
  2660. RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera_3d->get_camera());
  2661. } else {
  2662. RenderingServer::get_singleton()->viewport_attach_camera(viewport, RID());
  2663. }
  2664. }
  2665. if (camera_3d) {
  2666. camera_3d->notification(Camera3D::NOTIFICATION_BECAME_CURRENT);
  2667. }
  2668. _update_audio_listener_3d();
  2669. _camera_3d_transform_changed_notify();
  2670. }
  2671. bool Viewport::_camera_3d_add(Camera3D *p_camera) {
  2672. camera_3d_set.insert(p_camera);
  2673. return camera_3d_set.size() == 1;
  2674. }
  2675. void Viewport::_camera_3d_remove(Camera3D *p_camera) {
  2676. camera_3d_set.erase(p_camera);
  2677. if (camera_3d == p_camera) {
  2678. camera_3d->notification(Camera3D::NOTIFICATION_LOST_CURRENT);
  2679. camera_3d = nullptr;
  2680. }
  2681. }
  2682. void Viewport::_camera_3d_make_next_current(Camera3D *p_exclude) {
  2683. for (Set<Camera3D *>::Element *E = camera_3d_set.front(); E; E = E->next()) {
  2684. if (p_exclude == E->get()) {
  2685. continue;
  2686. }
  2687. if (!E->get()->is_inside_tree()) {
  2688. continue;
  2689. }
  2690. if (camera_3d != nullptr) {
  2691. return;
  2692. }
  2693. E->get()->make_current();
  2694. }
  2695. }
  2696. void Viewport::enable_camera_3d_override(bool p_enable) {
  2697. if (p_enable == camera_3d_override) {
  2698. return;
  2699. }
  2700. if (p_enable) {
  2701. camera_3d_override.rid = RenderingServer::get_singleton()->camera_create();
  2702. } else {
  2703. RenderingServer::get_singleton()->free(camera_3d_override.rid);
  2704. camera_3d_override.rid = RID();
  2705. }
  2706. if (p_enable) {
  2707. RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera_3d_override.rid);
  2708. } else if (camera_3d) {
  2709. RenderingServer::get_singleton()->viewport_attach_camera(viewport, camera_3d->get_camera());
  2710. } else {
  2711. RenderingServer::get_singleton()->viewport_attach_camera(viewport, RID());
  2712. }
  2713. }
  2714. void Viewport::set_camera_3d_override_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far) {
  2715. if (camera_3d_override) {
  2716. if (camera_3d_override.fov == p_fovy_degrees && camera_3d_override.z_near == p_z_near &&
  2717. camera_3d_override.z_far == p_z_far && camera_3d_override.projection == Camera3DOverrideData::PROJECTION_PERSPECTIVE) {
  2718. return;
  2719. }
  2720. camera_3d_override.fov = p_fovy_degrees;
  2721. camera_3d_override.z_near = p_z_near;
  2722. camera_3d_override.z_far = p_z_far;
  2723. camera_3d_override.projection = Camera3DOverrideData::PROJECTION_PERSPECTIVE;
  2724. RenderingServer::get_singleton()->camera_set_perspective(camera_3d_override.rid, camera_3d_override.fov, camera_3d_override.z_near, camera_3d_override.z_far);
  2725. }
  2726. }
  2727. void Viewport::set_camera_3d_override_orthogonal(real_t p_size, real_t p_z_near, real_t p_z_far) {
  2728. if (camera_3d_override) {
  2729. if (camera_3d_override.size == p_size && camera_3d_override.z_near == p_z_near &&
  2730. camera_3d_override.z_far == p_z_far && camera_3d_override.projection == Camera3DOverrideData::PROJECTION_ORTHOGONAL) {
  2731. return;
  2732. }
  2733. camera_3d_override.size = p_size;
  2734. camera_3d_override.z_near = p_z_near;
  2735. camera_3d_override.z_far = p_z_far;
  2736. camera_3d_override.projection = Camera3DOverrideData::PROJECTION_ORTHOGONAL;
  2737. RenderingServer::get_singleton()->camera_set_orthogonal(camera_3d_override.rid, camera_3d_override.size, camera_3d_override.z_near, camera_3d_override.z_far);
  2738. }
  2739. }
  2740. void Viewport::set_disable_3d(bool p_disable) {
  2741. disable_3d = p_disable;
  2742. RenderingServer::get_singleton()->viewport_set_disable_3d(viewport, disable_3d);
  2743. }
  2744. bool Viewport::is_3d_disabled() const {
  2745. return disable_3d;
  2746. }
  2747. bool Viewport::is_camera_3d_override_enabled() const {
  2748. return camera_3d_override;
  2749. }
  2750. void Viewport::set_camera_3d_override_transform(const Transform3D &p_transform) {
  2751. if (camera_3d_override) {
  2752. camera_3d_override.transform = p_transform;
  2753. RenderingServer::get_singleton()->camera_set_transform(camera_3d_override.rid, p_transform);
  2754. }
  2755. }
  2756. Transform3D Viewport::get_camera_3d_override_transform() const {
  2757. if (camera_3d_override) {
  2758. return camera_3d_override.transform;
  2759. }
  2760. return Transform3D();
  2761. }
  2762. Ref<World3D> Viewport::get_world_3d() const {
  2763. return world_3d;
  2764. }
  2765. Ref<World3D> Viewport::find_world_3d() const {
  2766. if (own_world_3d.is_valid()) {
  2767. return own_world_3d;
  2768. } else if (world_3d.is_valid()) {
  2769. return world_3d;
  2770. } else if (parent) {
  2771. return parent->find_world_3d();
  2772. } else {
  2773. return Ref<World3D>();
  2774. }
  2775. }
  2776. void Viewport::set_world_3d(const Ref<World3D> &p_world_3d) {
  2777. if (world_3d == p_world_3d) {
  2778. return;
  2779. }
  2780. if (is_inside_tree()) {
  2781. _propagate_exit_world_3d(this);
  2782. }
  2783. if (own_world_3d.is_valid() && world_3d.is_valid()) {
  2784. world_3d->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed));
  2785. }
  2786. world_3d = p_world_3d;
  2787. if (own_world_3d.is_valid()) {
  2788. if (world_3d.is_valid()) {
  2789. own_world_3d = world_3d->duplicate();
  2790. world_3d->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed));
  2791. } else {
  2792. own_world_3d = Ref<World3D>(memnew(World3D));
  2793. }
  2794. }
  2795. if (is_inside_tree()) {
  2796. _propagate_enter_world_3d(this);
  2797. }
  2798. if (is_inside_tree()) {
  2799. RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
  2800. }
  2801. _update_audio_listener_3d();
  2802. }
  2803. void Viewport::_own_world_3d_changed() {
  2804. ERR_FAIL_COND(world_3d.is_null());
  2805. ERR_FAIL_COND(own_world_3d.is_null());
  2806. if (is_inside_tree()) {
  2807. _propagate_exit_world_3d(this);
  2808. }
  2809. own_world_3d = world_3d->duplicate();
  2810. if (is_inside_tree()) {
  2811. _propagate_enter_world_3d(this);
  2812. }
  2813. if (is_inside_tree()) {
  2814. RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
  2815. }
  2816. _update_audio_listener_3d();
  2817. }
  2818. void Viewport::set_use_own_world_3d(bool p_world_3d) {
  2819. if (p_world_3d == own_world_3d.is_valid()) {
  2820. return;
  2821. }
  2822. if (is_inside_tree()) {
  2823. _propagate_exit_world_3d(this);
  2824. }
  2825. if (!p_world_3d) {
  2826. own_world_3d = Ref<World3D>();
  2827. if (world_3d.is_valid()) {
  2828. world_3d->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed));
  2829. }
  2830. } else {
  2831. if (world_3d.is_valid()) {
  2832. own_world_3d = world_3d->duplicate();
  2833. world_3d->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Viewport::_own_world_3d_changed));
  2834. } else {
  2835. own_world_3d = Ref<World3D>(memnew(World3D));
  2836. }
  2837. }
  2838. if (is_inside_tree()) {
  2839. _propagate_enter_world_3d(this);
  2840. }
  2841. if (is_inside_tree()) {
  2842. RenderingServer::get_singleton()->viewport_set_scenario(viewport, find_world_3d()->get_scenario());
  2843. }
  2844. _update_audio_listener_3d();
  2845. }
  2846. bool Viewport::is_using_own_world_3d() const {
  2847. return own_world_3d.is_valid();
  2848. }
  2849. void Viewport::_propagate_enter_world_3d(Node *p_node) {
  2850. if (p_node != this) {
  2851. if (!p_node->is_inside_tree()) { //may not have entered scene yet
  2852. return;
  2853. }
  2854. if (Object::cast_to<Node3D>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
  2855. p_node->notification(Node3D::NOTIFICATION_ENTER_WORLD);
  2856. } else {
  2857. Viewport *v = Object::cast_to<Viewport>(p_node);
  2858. if (v) {
  2859. if (v->world_3d.is_valid() || v->own_world_3d.is_valid()) {
  2860. return;
  2861. }
  2862. }
  2863. }
  2864. }
  2865. for (int i = 0; i < p_node->get_child_count(); i++) {
  2866. _propagate_enter_world_3d(p_node->get_child(i));
  2867. }
  2868. }
  2869. void Viewport::_propagate_exit_world_3d(Node *p_node) {
  2870. if (p_node != this) {
  2871. if (!p_node->is_inside_tree()) { //may have exited scene already
  2872. return;
  2873. }
  2874. if (Object::cast_to<Node3D>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
  2875. p_node->notification(Node3D::NOTIFICATION_EXIT_WORLD);
  2876. } else {
  2877. Viewport *v = Object::cast_to<Viewport>(p_node);
  2878. if (v) {
  2879. if (v->world_3d.is_valid() || v->own_world_3d.is_valid()) {
  2880. return;
  2881. }
  2882. }
  2883. }
  2884. }
  2885. for (int i = 0; i < p_node->get_child_count(); i++) {
  2886. _propagate_exit_world_3d(p_node->get_child(i));
  2887. }
  2888. }
  2889. void Viewport::set_use_xr(bool p_use_xr) {
  2890. use_xr = p_use_xr;
  2891. RS::get_singleton()->viewport_set_use_xr(viewport, use_xr);
  2892. }
  2893. bool Viewport::is_using_xr() {
  2894. return use_xr;
  2895. }
  2896. void Viewport::set_scaling_3d_mode(Scaling3DMode p_scaling_3d_mode) {
  2897. if (scaling_3d_mode == p_scaling_3d_mode) {
  2898. return;
  2899. }
  2900. scaling_3d_mode = p_scaling_3d_mode;
  2901. RS::get_singleton()->viewport_set_scaling_3d_mode(viewport, (RS::ViewportScaling3DMode)(int)p_scaling_3d_mode);
  2902. }
  2903. Viewport::Scaling3DMode Viewport::get_scaling_3d_mode() const {
  2904. return scaling_3d_mode;
  2905. }
  2906. void Viewport::set_scaling_3d_scale(float p_scaling_3d_scale) {
  2907. // Clamp to reasonable values that are actually useful.
  2908. // Values above 2.0 don't serve a practical purpose since the viewport
  2909. // isn't displayed with mipmaps.
  2910. scaling_3d_scale = CLAMP(p_scaling_3d_scale, 0.1, 2.0);
  2911. RS::get_singleton()->viewport_set_scaling_3d_scale(viewport, scaling_3d_scale);
  2912. }
  2913. float Viewport::get_scaling_3d_scale() const {
  2914. return scaling_3d_scale;
  2915. }
  2916. void Viewport::set_fsr_sharpness(float p_fsr_sharpness) {
  2917. if (fsr_sharpness == p_fsr_sharpness) {
  2918. return;
  2919. }
  2920. if (p_fsr_sharpness < 0.0f) {
  2921. p_fsr_sharpness = 0.0f;
  2922. }
  2923. fsr_sharpness = p_fsr_sharpness;
  2924. RS::get_singleton()->viewport_set_fsr_sharpness(viewport, p_fsr_sharpness);
  2925. }
  2926. float Viewport::get_fsr_sharpness() const {
  2927. return fsr_sharpness;
  2928. }
  2929. void Viewport::set_fsr_mipmap_bias(float p_fsr_mipmap_bias) {
  2930. if (fsr_mipmap_bias == p_fsr_mipmap_bias) {
  2931. return;
  2932. }
  2933. fsr_mipmap_bias = p_fsr_mipmap_bias;
  2934. RS::get_singleton()->viewport_set_fsr_mipmap_bias(viewport, p_fsr_mipmap_bias);
  2935. }
  2936. float Viewport::get_fsr_mipmap_bias() const {
  2937. return fsr_mipmap_bias;
  2938. }
  2939. #endif // _3D_DISABLED
  2940. void Viewport::_bind_methods() {
  2941. ClassDB::bind_method(D_METHOD("set_world_2d", "world_2d"), &Viewport::set_world_2d);
  2942. ClassDB::bind_method(D_METHOD("get_world_2d"), &Viewport::get_world_2d);
  2943. ClassDB::bind_method(D_METHOD("find_world_2d"), &Viewport::find_world_2d);
  2944. ClassDB::bind_method(D_METHOD("set_canvas_transform", "xform"), &Viewport::set_canvas_transform);
  2945. ClassDB::bind_method(D_METHOD("get_canvas_transform"), &Viewport::get_canvas_transform);
  2946. ClassDB::bind_method(D_METHOD("set_global_canvas_transform", "xform"), &Viewport::set_global_canvas_transform);
  2947. ClassDB::bind_method(D_METHOD("get_global_canvas_transform"), &Viewport::get_global_canvas_transform);
  2948. ClassDB::bind_method(D_METHOD("get_final_transform"), &Viewport::get_final_transform);
  2949. ClassDB::bind_method(D_METHOD("get_visible_rect"), &Viewport::get_visible_rect);
  2950. ClassDB::bind_method(D_METHOD("set_transparent_background", "enable"), &Viewport::set_transparent_background);
  2951. ClassDB::bind_method(D_METHOD("has_transparent_background"), &Viewport::has_transparent_background);
  2952. ClassDB::bind_method(D_METHOD("set_msaa", "msaa"), &Viewport::set_msaa);
  2953. ClassDB::bind_method(D_METHOD("get_msaa"), &Viewport::get_msaa);
  2954. ClassDB::bind_method(D_METHOD("set_screen_space_aa", "screen_space_aa"), &Viewport::set_screen_space_aa);
  2955. ClassDB::bind_method(D_METHOD("get_screen_space_aa"), &Viewport::get_screen_space_aa);
  2956. ClassDB::bind_method(D_METHOD("set_use_debanding", "enable"), &Viewport::set_use_debanding);
  2957. ClassDB::bind_method(D_METHOD("is_using_debanding"), &Viewport::is_using_debanding);
  2958. ClassDB::bind_method(D_METHOD("set_use_occlusion_culling", "enable"), &Viewport::set_use_occlusion_culling);
  2959. ClassDB::bind_method(D_METHOD("is_using_occlusion_culling"), &Viewport::is_using_occlusion_culling);
  2960. ClassDB::bind_method(D_METHOD("set_debug_draw", "debug_draw"), &Viewport::set_debug_draw);
  2961. ClassDB::bind_method(D_METHOD("get_debug_draw"), &Viewport::get_debug_draw);
  2962. ClassDB::bind_method(D_METHOD("get_render_info", "type", "info"), &Viewport::get_render_info);
  2963. ClassDB::bind_method(D_METHOD("get_texture"), &Viewport::get_texture);
  2964. ClassDB::bind_method(D_METHOD("set_physics_object_picking", "enable"), &Viewport::set_physics_object_picking);
  2965. ClassDB::bind_method(D_METHOD("get_physics_object_picking"), &Viewport::get_physics_object_picking);
  2966. ClassDB::bind_method(D_METHOD("get_viewport_rid"), &Viewport::get_viewport_rid);
  2967. ClassDB::bind_method(D_METHOD("push_text_input", "text"), &Viewport::push_text_input);
  2968. ClassDB::bind_method(D_METHOD("push_input", "event", "in_local_coords"), &Viewport::push_input, DEFVAL(false));
  2969. ClassDB::bind_method(D_METHOD("push_unhandled_input", "event", "in_local_coords"), &Viewport::push_unhandled_input, DEFVAL(false));
  2970. ClassDB::bind_method(D_METHOD("get_camera_2d"), &Viewport::get_camera_2d);
  2971. ClassDB::bind_method(D_METHOD("set_as_audio_listener_2d", "enable"), &Viewport::set_as_audio_listener_2d);
  2972. ClassDB::bind_method(D_METHOD("is_audio_listener_2d"), &Viewport::is_audio_listener_2d);
  2973. ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position);
  2974. ClassDB::bind_method(D_METHOD("warp_mouse", "position"), &Viewport::warp_mouse);
  2975. ClassDB::bind_method(D_METHOD("gui_get_drag_data"), &Viewport::gui_get_drag_data);
  2976. ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging);
  2977. ClassDB::bind_method(D_METHOD("gui_is_drag_successful"), &Viewport::gui_is_drag_successful);
  2978. ClassDB::bind_method(D_METHOD("gui_release_focus"), &Viewport::gui_release_focus);
  2979. ClassDB::bind_method(D_METHOD("gui_get_focus_owner"), &Viewport::gui_get_focus_owner);
  2980. ClassDB::bind_method(D_METHOD("set_disable_input", "disable"), &Viewport::set_disable_input);
  2981. ClassDB::bind_method(D_METHOD("is_input_disabled"), &Viewport::is_input_disabled);
  2982. ClassDB::bind_method(D_METHOD("_gui_remove_focus_for_window"), &Viewport::_gui_remove_focus_for_window);
  2983. ClassDB::bind_method(D_METHOD("_post_gui_grab_click_focus"), &Viewport::_post_gui_grab_click_focus);
  2984. ClassDB::bind_method(D_METHOD("set_shadow_atlas_size", "size"), &Viewport::set_shadow_atlas_size);
  2985. ClassDB::bind_method(D_METHOD("get_shadow_atlas_size"), &Viewport::get_shadow_atlas_size);
  2986. ClassDB::bind_method(D_METHOD("set_shadow_atlas_16_bits", "enable"), &Viewport::set_shadow_atlas_16_bits);
  2987. ClassDB::bind_method(D_METHOD("get_shadow_atlas_16_bits"), &Viewport::get_shadow_atlas_16_bits);
  2988. ClassDB::bind_method(D_METHOD("set_snap_controls_to_pixels", "enabled"), &Viewport::set_snap_controls_to_pixels);
  2989. ClassDB::bind_method(D_METHOD("is_snap_controls_to_pixels_enabled"), &Viewport::is_snap_controls_to_pixels_enabled);
  2990. ClassDB::bind_method(D_METHOD("set_snap_2d_transforms_to_pixel", "enabled"), &Viewport::set_snap_2d_transforms_to_pixel);
  2991. ClassDB::bind_method(D_METHOD("is_snap_2d_transforms_to_pixel_enabled"), &Viewport::is_snap_2d_transforms_to_pixel_enabled);
  2992. ClassDB::bind_method(D_METHOD("set_snap_2d_vertices_to_pixel", "enabled"), &Viewport::set_snap_2d_vertices_to_pixel);
  2993. ClassDB::bind_method(D_METHOD("is_snap_2d_vertices_to_pixel_enabled"), &Viewport::is_snap_2d_vertices_to_pixel_enabled);
  2994. ClassDB::bind_method(D_METHOD("set_shadow_atlas_quadrant_subdiv", "quadrant", "subdiv"), &Viewport::set_shadow_atlas_quadrant_subdiv);
  2995. ClassDB::bind_method(D_METHOD("get_shadow_atlas_quadrant_subdiv", "quadrant"), &Viewport::get_shadow_atlas_quadrant_subdiv);
  2996. ClassDB::bind_method(D_METHOD("set_input_as_handled"), &Viewport::set_input_as_handled);
  2997. ClassDB::bind_method(D_METHOD("is_input_handled"), &Viewport::is_input_handled);
  2998. ClassDB::bind_method(D_METHOD("set_handle_input_locally", "enable"), &Viewport::set_handle_input_locally);
  2999. ClassDB::bind_method(D_METHOD("is_handling_input_locally"), &Viewport::is_handling_input_locally);
  3000. ClassDB::bind_method(D_METHOD("set_default_canvas_item_texture_filter", "mode"), &Viewport::set_default_canvas_item_texture_filter);
  3001. ClassDB::bind_method(D_METHOD("get_default_canvas_item_texture_filter"), &Viewport::get_default_canvas_item_texture_filter);
  3002. ClassDB::bind_method(D_METHOD("set_embedding_subwindows", "enable"), &Viewport::set_embedding_subwindows);
  3003. ClassDB::bind_method(D_METHOD("is_embedding_subwindows"), &Viewport::is_embedding_subwindows);
  3004. ClassDB::bind_method(D_METHOD("set_default_canvas_item_texture_repeat", "mode"), &Viewport::set_default_canvas_item_texture_repeat);
  3005. ClassDB::bind_method(D_METHOD("get_default_canvas_item_texture_repeat"), &Viewport::get_default_canvas_item_texture_repeat);
  3006. ClassDB::bind_method(D_METHOD("set_sdf_oversize", "oversize"), &Viewport::set_sdf_oversize);
  3007. ClassDB::bind_method(D_METHOD("get_sdf_oversize"), &Viewport::get_sdf_oversize);
  3008. ClassDB::bind_method(D_METHOD("set_sdf_scale", "scale"), &Viewport::set_sdf_scale);
  3009. ClassDB::bind_method(D_METHOD("get_sdf_scale"), &Viewport::get_sdf_scale);
  3010. ClassDB::bind_method(D_METHOD("set_mesh_lod_threshold", "pixels"), &Viewport::set_mesh_lod_threshold);
  3011. ClassDB::bind_method(D_METHOD("get_mesh_lod_threshold"), &Viewport::get_mesh_lod_threshold);
  3012. ClassDB::bind_method(D_METHOD("_process_picking"), &Viewport::_process_picking);
  3013. #ifndef _3D_DISABLED
  3014. ClassDB::bind_method(D_METHOD("set_world_3d", "world_3d"), &Viewport::set_world_3d);
  3015. ClassDB::bind_method(D_METHOD("get_world_3d"), &Viewport::get_world_3d);
  3016. ClassDB::bind_method(D_METHOD("find_world_3d"), &Viewport::find_world_3d);
  3017. ClassDB::bind_method(D_METHOD("set_use_own_world_3d", "enable"), &Viewport::set_use_own_world_3d);
  3018. ClassDB::bind_method(D_METHOD("is_using_own_world_3d"), &Viewport::is_using_own_world_3d);
  3019. ClassDB::bind_method(D_METHOD("get_camera_3d"), &Viewport::get_camera_3d);
  3020. ClassDB::bind_method(D_METHOD("set_as_audio_listener_3d", "enable"), &Viewport::set_as_audio_listener_3d);
  3021. ClassDB::bind_method(D_METHOD("is_audio_listener_3d"), &Viewport::is_audio_listener_3d);
  3022. ClassDB::bind_method(D_METHOD("set_disable_3d", "disable"), &Viewport::set_disable_3d);
  3023. ClassDB::bind_method(D_METHOD("is_3d_disabled"), &Viewport::is_3d_disabled);
  3024. ClassDB::bind_method(D_METHOD("set_use_xr", "use"), &Viewport::set_use_xr);
  3025. ClassDB::bind_method(D_METHOD("is_using_xr"), &Viewport::is_using_xr);
  3026. ClassDB::bind_method(D_METHOD("set_scaling_3d_mode", "scaling_3d_mode"), &Viewport::set_scaling_3d_mode);
  3027. ClassDB::bind_method(D_METHOD("get_scaling_3d_mode"), &Viewport::get_scaling_3d_mode);
  3028. ClassDB::bind_method(D_METHOD("set_scaling_3d_scale", "scale"), &Viewport::set_scaling_3d_scale);
  3029. ClassDB::bind_method(D_METHOD("get_scaling_3d_scale"), &Viewport::get_scaling_3d_scale);
  3030. ClassDB::bind_method(D_METHOD("set_fsr_sharpness", "fsr_sharpness"), &Viewport::set_fsr_sharpness);
  3031. ClassDB::bind_method(D_METHOD("get_fsr_sharpness"), &Viewport::get_fsr_sharpness);
  3032. ClassDB::bind_method(D_METHOD("set_fsr_mipmap_bias", "fsr_mipmap_bias"), &Viewport::set_fsr_mipmap_bias);
  3033. ClassDB::bind_method(D_METHOD("get_fsr_mipmap_bias"), &Viewport::get_fsr_mipmap_bias);
  3034. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_3d"), "set_disable_3d", "is_3d_disabled");
  3035. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_xr"), "set_use_xr", "is_using_xr");
  3036. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world_3d"), "set_use_own_world_3d", "is_using_own_world_3d");
  3037. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_3d", PROPERTY_HINT_RESOURCE_TYPE, "World3D"), "set_world_3d", "get_world_3d");
  3038. #endif // _3D_DISABLED
  3039. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", PROPERTY_USAGE_NONE), "set_world_2d", "get_world_2d");
  3040. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background");
  3041. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally");
  3042. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_2d_transforms_to_pixel"), "set_snap_2d_transforms_to_pixel", "is_snap_2d_transforms_to_pixel_enabled");
  3043. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snap_2d_vertices_to_pixel"), "set_snap_2d_vertices_to_pixel", "is_snap_2d_vertices_to_pixel_enabled");
  3044. ADD_GROUP("Rendering", "");
  3045. ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), "set_msaa", "get_msaa");
  3046. ADD_PROPERTY(PropertyInfo(Variant::INT, "screen_space_aa", PROPERTY_HINT_ENUM, "Disabled (Fastest),FXAA (Fast)"), "set_screen_space_aa", "get_screen_space_aa");
  3047. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_debanding"), "set_use_debanding", "is_using_debanding");
  3048. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_occlusion_culling"), "set_use_occlusion_culling", "is_using_occlusion_culling");
  3049. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mesh_lod_threshold", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_mesh_lod_threshold", "get_mesh_lod_threshold");
  3050. ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Overdraw,Wireframe"), "set_debug_draw", "get_debug_draw");
  3051. #ifndef _3D_DISABLED
  3052. ADD_GROUP("Scaling 3D", "");
  3053. ADD_PROPERTY(PropertyInfo(Variant::INT, "scaling_3d_mode", PROPERTY_HINT_ENUM, "Bilinear (Fastest),FSR 1.0 (Fast)"), "set_scaling_3d_mode", "get_scaling_3d_mode");
  3054. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scaling_3d_scale", PROPERTY_HINT_RANGE, "0.25,2.0,0.01"), "set_scaling_3d_scale", "get_scaling_3d_scale");
  3055. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fsr_mipmap_bias", PROPERTY_HINT_RANGE, "-2,2,0.1"), "set_fsr_mipmap_bias", "get_fsr_mipmap_bias");
  3056. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fsr_sharpness", PROPERTY_HINT_RANGE, "0,2,0.1"), "set_fsr_sharpness", "get_fsr_sharpness");
  3057. #endif
  3058. ADD_GROUP("Canvas Items", "canvas_item_");
  3059. ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_item_default_texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Linear Mipmap,Nearest Mipmap"), "set_default_canvas_item_texture_filter", "get_default_canvas_item_texture_filter");
  3060. ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_item_default_texture_repeat", PROPERTY_HINT_ENUM, "Disabled,Enabled,Mirror"), "set_default_canvas_item_texture_repeat", "get_default_canvas_item_texture_repeat");
  3061. ADD_GROUP("Audio Listener", "audio_listener_");
  3062. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_2d"), "set_as_audio_listener_2d", "is_audio_listener_2d");
  3063. #ifndef _3D_DISABLED
  3064. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_3d"), "set_as_audio_listener_3d", "is_audio_listener_3d");
  3065. #endif
  3066. ADD_GROUP("Physics", "physics_");
  3067. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking"), "set_physics_object_picking", "get_physics_object_picking");
  3068. ADD_GROUP("GUI", "gui_");
  3069. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_disable_input"), "set_disable_input", "is_input_disabled");
  3070. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_snap_controls_to_pixels"), "set_snap_controls_to_pixels", "is_snap_controls_to_pixels_enabled");
  3071. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_embed_subwindows"), "set_embedding_subwindows", "is_embedding_subwindows");
  3072. ADD_GROUP("SDF", "sdf_");
  3073. ADD_PROPERTY(PropertyInfo(Variant::INT, "sdf_oversize", PROPERTY_HINT_ENUM, "100%,120%,150%,200%"), "set_sdf_oversize", "get_sdf_oversize");
  3074. ADD_PROPERTY(PropertyInfo(Variant::INT, "sdf_scale", PROPERTY_HINT_ENUM, "100%,50%,25%"), "set_sdf_scale", "get_sdf_scale");
  3075. ADD_GROUP("Shadow Atlas", "shadow_atlas_");
  3076. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_atlas_size"), "set_shadow_atlas_size", "get_shadow_atlas_size");
  3077. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_atlas_16_bits"), "set_shadow_atlas_16_bits", "get_shadow_atlas_16_bits");
  3078. ADD_PROPERTYI(PropertyInfo(Variant::INT, "shadow_atlas_quad_0", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_shadow_atlas_quadrant_subdiv", "get_shadow_atlas_quadrant_subdiv", 0);
  3079. ADD_PROPERTYI(PropertyInfo(Variant::INT, "shadow_atlas_quad_1", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_shadow_atlas_quadrant_subdiv", "get_shadow_atlas_quadrant_subdiv", 1);
  3080. ADD_PROPERTYI(PropertyInfo(Variant::INT, "shadow_atlas_quad_2", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_shadow_atlas_quadrant_subdiv", "get_shadow_atlas_quadrant_subdiv", 2);
  3081. ADD_PROPERTYI(PropertyInfo(Variant::INT, "shadow_atlas_quad_3", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), "set_shadow_atlas_quadrant_subdiv", "get_shadow_atlas_quadrant_subdiv", 3);
  3082. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "canvas_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_canvas_transform", "get_canvas_transform");
  3083. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_canvas_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_global_canvas_transform", "get_global_canvas_transform");
  3084. ADD_SIGNAL(MethodInfo("size_changed"));
  3085. ADD_SIGNAL(MethodInfo("gui_focus_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
  3086. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
  3087. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1);
  3088. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  3089. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_16);
  3090. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_64);
  3091. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_256);
  3092. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1024);
  3093. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
  3094. BIND_ENUM_CONSTANT(SCALING_3D_MODE_BILINEAR);
  3095. BIND_ENUM_CONSTANT(SCALING_3D_MODE_FSR);
  3096. BIND_ENUM_CONSTANT(SCALING_3D_MODE_MAX);
  3097. BIND_ENUM_CONSTANT(MSAA_DISABLED);
  3098. BIND_ENUM_CONSTANT(MSAA_2X);
  3099. BIND_ENUM_CONSTANT(MSAA_4X);
  3100. BIND_ENUM_CONSTANT(MSAA_8X);
  3101. BIND_ENUM_CONSTANT(MSAA_MAX);
  3102. BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_DISABLED);
  3103. BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_FXAA);
  3104. BIND_ENUM_CONSTANT(SCREEN_SPACE_AA_MAX);
  3105. BIND_ENUM_CONSTANT(RENDER_INFO_OBJECTS_IN_FRAME);
  3106. BIND_ENUM_CONSTANT(RENDER_INFO_PRIMITIVES_IN_FRAME);
  3107. BIND_ENUM_CONSTANT(RENDER_INFO_DRAW_CALLS_IN_FRAME);
  3108. BIND_ENUM_CONSTANT(RENDER_INFO_MAX);
  3109. BIND_ENUM_CONSTANT(RENDER_INFO_TYPE_VISIBLE);
  3110. BIND_ENUM_CONSTANT(RENDER_INFO_TYPE_SHADOW);
  3111. BIND_ENUM_CONSTANT(RENDER_INFO_TYPE_MAX);
  3112. BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLED);
  3113. BIND_ENUM_CONSTANT(DEBUG_DRAW_UNSHADED);
  3114. BIND_ENUM_CONSTANT(DEBUG_DRAW_LIGHTING);
  3115. BIND_ENUM_CONSTANT(DEBUG_DRAW_OVERDRAW);
  3116. BIND_ENUM_CONSTANT(DEBUG_DRAW_WIREFRAME);
  3117. BIND_ENUM_CONSTANT(DEBUG_DRAW_NORMAL_BUFFER);
  3118. BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_ALBEDO);
  3119. BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_LIGHTING);
  3120. BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_EMISSION);
  3121. BIND_ENUM_CONSTANT(DEBUG_DRAW_SHADOW_ATLAS);
  3122. BIND_ENUM_CONSTANT(DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS);
  3123. BIND_ENUM_CONSTANT(DEBUG_DRAW_SCENE_LUMINANCE);
  3124. BIND_ENUM_CONSTANT(DEBUG_DRAW_SSAO);
  3125. BIND_ENUM_CONSTANT(DEBUG_DRAW_SSIL);
  3126. BIND_ENUM_CONSTANT(DEBUG_DRAW_PSSM_SPLITS);
  3127. BIND_ENUM_CONSTANT(DEBUG_DRAW_DECAL_ATLAS);
  3128. BIND_ENUM_CONSTANT(DEBUG_DRAW_SDFGI);
  3129. BIND_ENUM_CONSTANT(DEBUG_DRAW_SDFGI_PROBES);
  3130. BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_BUFFER);
  3131. BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLE_LOD);
  3132. BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_OMNI_LIGHTS);
  3133. BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_SPOT_LIGHTS);
  3134. BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_DECALS);
  3135. BIND_ENUM_CONSTANT(DEBUG_DRAW_CLUSTER_REFLECTION_PROBES);
  3136. BIND_ENUM_CONSTANT(DEBUG_DRAW_OCCLUDERS)
  3137. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST);
  3138. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR);
  3139. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS);
  3140. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  3141. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_MAX);
  3142. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  3143. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_ENABLED);
  3144. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MIRROR);
  3145. BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_REPEAT_MAX);
  3146. BIND_ENUM_CONSTANT(SDF_OVERSIZE_100_PERCENT);
  3147. BIND_ENUM_CONSTANT(SDF_OVERSIZE_120_PERCENT);
  3148. BIND_ENUM_CONSTANT(SDF_OVERSIZE_150_PERCENT);
  3149. BIND_ENUM_CONSTANT(SDF_OVERSIZE_200_PERCENT);
  3150. BIND_ENUM_CONSTANT(SDF_OVERSIZE_MAX);
  3151. BIND_ENUM_CONSTANT(SDF_SCALE_100_PERCENT);
  3152. BIND_ENUM_CONSTANT(SDF_SCALE_50_PERCENT);
  3153. BIND_ENUM_CONSTANT(SDF_SCALE_25_PERCENT);
  3154. BIND_ENUM_CONSTANT(SDF_SCALE_MAX);
  3155. }
  3156. Viewport::Viewport() {
  3157. world_2d = Ref<World2D>(memnew(World2D));
  3158. viewport = RenderingServer::get_singleton()->viewport_create();
  3159. texture_rid = RenderingServer::get_singleton()->viewport_get_texture(viewport);
  3160. default_texture.instantiate();
  3161. default_texture->vp = const_cast<Viewport *>(this);
  3162. viewport_textures.insert(default_texture.ptr());
  3163. default_texture->proxy = RS::get_singleton()->texture_proxy_create(texture_rid);
  3164. canvas_layers.insert(nullptr); // This eases picking code (interpreted as the canvas of the Viewport).
  3165. set_shadow_atlas_size(shadow_atlas_size);
  3166. for (int i = 0; i < 4; i++) {
  3167. shadow_atlas_quadrant_subdiv[i] = SHADOW_ATLAS_QUADRANT_SUBDIV_MAX;
  3168. }
  3169. set_shadow_atlas_quadrant_subdiv(0, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  3170. set_shadow_atlas_quadrant_subdiv(1, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  3171. set_shadow_atlas_quadrant_subdiv(2, SHADOW_ATLAS_QUADRANT_SUBDIV_16);
  3172. set_shadow_atlas_quadrant_subdiv(3, SHADOW_ATLAS_QUADRANT_SUBDIV_64);
  3173. set_mesh_lod_threshold(mesh_lod_threshold);
  3174. String id = itos(get_instance_id());
  3175. input_group = "_vp_input" + id;
  3176. gui_input_group = "_vp_gui_input" + id;
  3177. unhandled_input_group = "_vp_unhandled_input" + id;
  3178. shortcut_input_group = "_vp_shortcut_input" + id;
  3179. unhandled_key_input_group = "_vp_unhandled_key_input" + id;
  3180. // Window tooltip.
  3181. gui.tooltip_delay = GLOBAL_DEF("gui/timers/tooltip_delay_sec", 0.5);
  3182. ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/tooltip_delay_sec", PropertyInfo(Variant::FLOAT, "gui/timers/tooltip_delay_sec", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater")); // No negative numbers
  3183. #ifndef _3D_DISABLED
  3184. Viewport::Scaling3DMode scaling_3d_mode = (Viewport::Scaling3DMode)(int)GLOBAL_GET("rendering/scaling_3d/mode");
  3185. set_scaling_3d_mode(scaling_3d_mode);
  3186. set_scaling_3d_scale(GLOBAL_GET("rendering/scaling_3d/scale"));
  3187. float fsr_sharpness = GLOBAL_GET("rendering/scaling_3d/fsr_sharpness");
  3188. set_fsr_sharpness(fsr_sharpness);
  3189. float fsr_mipmap_bias = GLOBAL_GET("rendering/scaling_3d/fsr_mipmap_bias");
  3190. set_fsr_mipmap_bias(fsr_mipmap_bias);
  3191. #endif // _3D_DISABLED
  3192. set_sdf_oversize(sdf_oversize); // Set to server.
  3193. }
  3194. Viewport::~Viewport() {
  3195. // Erase itself from viewport textures.
  3196. for (Set<ViewportTexture *>::Element *E = viewport_textures.front(); E; E = E->next()) {
  3197. E->get()->vp = nullptr;
  3198. }
  3199. RenderingServer::get_singleton()->free(viewport);
  3200. }
  3201. /////////////////////////////////
  3202. void SubViewport::set_size(const Size2i &p_size) {
  3203. _set_size(p_size, _get_size_2d_override(), Rect2i(), _stretch_transform(), true);
  3204. SubViewportContainer *c = Object::cast_to<SubViewportContainer>(get_parent());
  3205. if (c) {
  3206. c->update_minimum_size();
  3207. }
  3208. }
  3209. Size2i SubViewport::get_size() const {
  3210. return _get_size();
  3211. }
  3212. void SubViewport::set_size_2d_override(const Size2i &p_size) {
  3213. _set_size(_get_size(), p_size, Rect2i(), _stretch_transform(), true);
  3214. }
  3215. Size2i SubViewport::get_size_2d_override() const {
  3216. return _get_size_2d_override();
  3217. }
  3218. void SubViewport::set_size_2d_override_stretch(bool p_enable) {
  3219. if (p_enable == size_2d_override_stretch) {
  3220. return;
  3221. }
  3222. size_2d_override_stretch = p_enable;
  3223. _set_size(_get_size(), _get_size_2d_override(), Rect2i(), _stretch_transform(), true);
  3224. }
  3225. bool SubViewport::is_size_2d_override_stretch_enabled() const {
  3226. return size_2d_override_stretch;
  3227. }
  3228. void SubViewport::set_update_mode(UpdateMode p_mode) {
  3229. update_mode = p_mode;
  3230. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::ViewportUpdateMode(p_mode));
  3231. }
  3232. SubViewport::UpdateMode SubViewport::get_update_mode() const {
  3233. return update_mode;
  3234. }
  3235. void SubViewport::set_clear_mode(ClearMode p_mode) {
  3236. clear_mode = p_mode;
  3237. RS::get_singleton()->viewport_set_clear_mode(get_viewport_rid(), RS::ViewportClearMode(p_mode));
  3238. }
  3239. SubViewport::ClearMode SubViewport::get_clear_mode() const {
  3240. return clear_mode;
  3241. }
  3242. DisplayServer::WindowID SubViewport::get_window_id() const {
  3243. return DisplayServer::INVALID_WINDOW_ID;
  3244. }
  3245. Transform2D SubViewport::_stretch_transform() {
  3246. Transform2D transform = Transform2D();
  3247. Size2i view_size_2d_override = _get_size_2d_override();
  3248. if (size_2d_override_stretch && view_size_2d_override.width > 0 && view_size_2d_override.height > 0) {
  3249. Size2 scale = _get_size() / view_size_2d_override;
  3250. transform.scale(scale);
  3251. }
  3252. return transform;
  3253. }
  3254. Transform2D SubViewport::get_screen_transform() const {
  3255. Transform2D container_transform = Transform2D();
  3256. SubViewportContainer *c = Object::cast_to<SubViewportContainer>(get_parent());
  3257. if (c) {
  3258. if (c->is_stretch_enabled()) {
  3259. container_transform.scale(Vector2(c->get_stretch_shrink(), c->get_stretch_shrink()));
  3260. }
  3261. container_transform = c->get_viewport()->get_screen_transform() * c->get_global_transform_with_canvas() * container_transform;
  3262. } else {
  3263. WARN_PRINT_ONCE("SubViewport is not a child of a SubViewportContainer. get_screen_transform doesn't return the actual screen position.");
  3264. }
  3265. return container_transform * Viewport::get_screen_transform();
  3266. }
  3267. void SubViewport::_notification(int p_what) {
  3268. switch (p_what) {
  3269. case NOTIFICATION_ENTER_TREE: {
  3270. RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
  3271. } break;
  3272. case NOTIFICATION_EXIT_TREE: {
  3273. RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
  3274. } break;
  3275. }
  3276. }
  3277. void SubViewport::_bind_methods() {
  3278. ClassDB::bind_method(D_METHOD("set_size", "size"), &SubViewport::set_size);
  3279. ClassDB::bind_method(D_METHOD("get_size"), &SubViewport::get_size);
  3280. ClassDB::bind_method(D_METHOD("set_size_2d_override", "size"), &SubViewport::set_size_2d_override);
  3281. ClassDB::bind_method(D_METHOD("get_size_2d_override"), &SubViewport::get_size_2d_override);
  3282. ClassDB::bind_method(D_METHOD("set_size_2d_override_stretch", "enable"), &SubViewport::set_size_2d_override_stretch);
  3283. ClassDB::bind_method(D_METHOD("is_size_2d_override_stretch_enabled"), &SubViewport::is_size_2d_override_stretch_enabled);
  3284. ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &SubViewport::set_update_mode);
  3285. ClassDB::bind_method(D_METHOD("get_update_mode"), &SubViewport::get_update_mode);
  3286. ClassDB::bind_method(D_METHOD("set_clear_mode", "mode"), &SubViewport::set_clear_mode);
  3287. ClassDB::bind_method(D_METHOD("get_clear_mode"), &SubViewport::get_clear_mode);
  3288. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size");
  3289. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size_2d_override"), "set_size_2d_override", "get_size_2d_override");
  3290. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "size_2d_override_stretch"), "set_size_2d_override_stretch", "is_size_2d_override_stretch_enabled");
  3291. ADD_GROUP("Render Target", "render_target_");
  3292. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_clear_mode", PROPERTY_HINT_ENUM, "Always,Never,Next Frame"), "set_clear_mode", "get_clear_mode");
  3293. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_update_mode", PROPERTY_HINT_ENUM, "Disabled,Once,When Visible,When Parent Visible,Always"), "set_update_mode", "get_update_mode");
  3294. BIND_ENUM_CONSTANT(CLEAR_MODE_ALWAYS);
  3295. BIND_ENUM_CONSTANT(CLEAR_MODE_NEVER);
  3296. BIND_ENUM_CONSTANT(CLEAR_MODE_ONCE);
  3297. BIND_ENUM_CONSTANT(UPDATE_DISABLED);
  3298. BIND_ENUM_CONSTANT(UPDATE_ONCE);
  3299. BIND_ENUM_CONSTANT(UPDATE_WHEN_VISIBLE);
  3300. BIND_ENUM_CONSTANT(UPDATE_WHEN_PARENT_VISIBLE);
  3301. BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
  3302. }
  3303. SubViewport::SubViewport() {}
  3304. SubViewport::~SubViewport() {}