viewport.cpp 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702
  1. /**************************************************************************/
  2. /* viewport.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/core_string_names.h"
  32. #include "core/os/input.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #include "scene/2d/collision_object_2d.h"
  36. #include "scene/2d/listener_2d.h"
  37. #include "scene/3d/camera.h"
  38. #include "scene/3d/collision_object.h"
  39. #include "scene/3d/listener.h"
  40. #include "scene/3d/spatial.h"
  41. #include "scene/3d/world_environment.h"
  42. #include "scene/gui/control.h"
  43. #include "scene/gui/label.h"
  44. #include "scene/gui/menu_button.h"
  45. #include "scene/gui/panel.h"
  46. #include "scene/gui/panel_container.h"
  47. #include "scene/gui/popup_menu.h"
  48. #include "scene/gui/viewport_container.h"
  49. #include "scene/main/canvas_layer.h"
  50. #include "scene/main/timer.h"
  51. #include "scene/resources/mesh.h"
  52. #include "scene/scene_string_names.h"
  53. #include "servers/physics_2d_server.h"
  54. LocalVector<Viewport::GUI::CanvasParent> Viewport::GUI::canvas_parents_dirty_order;
  55. void ViewportTexture::setup_local_to_scene() {
  56. Node *local_scene = get_local_scene();
  57. if (!local_scene) {
  58. return;
  59. }
  60. if (vp) {
  61. vp->viewport_textures.erase(this);
  62. }
  63. vp = nullptr;
  64. Node *vpn = local_scene->get_node(path);
  65. ERR_FAIL_COND_MSG(!vpn, "ViewportTexture: Path to node is invalid.");
  66. vp = Object::cast_to<Viewport>(vpn);
  67. ERR_FAIL_COND_MSG(!vp, "ViewportTexture: Path to node does not point to a viewport.");
  68. vp->viewport_textures.insert(this);
  69. VS::get_singleton()->texture_set_proxy(proxy, vp->texture_rid);
  70. vp->texture_flags = flags;
  71. VS::get_singleton()->texture_set_flags(vp->texture_rid, flags);
  72. }
  73. void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) {
  74. if (path == p_path) {
  75. return;
  76. }
  77. path = p_path;
  78. if (get_local_scene()) {
  79. setup_local_to_scene();
  80. }
  81. }
  82. NodePath ViewportTexture::get_viewport_path_in_scene() const {
  83. return path;
  84. }
  85. int ViewportTexture::get_width() const {
  86. ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it.");
  87. return vp->size.width;
  88. }
  89. int ViewportTexture::get_height() const {
  90. ERR_FAIL_COND_V_MSG(!vp, 0, "Viewport Texture must be set to use it.");
  91. return vp->size.height;
  92. }
  93. Size2 ViewportTexture::get_size() const {
  94. ERR_FAIL_COND_V_MSG(!vp, Size2(), "Viewport Texture must be set to use it.");
  95. return vp->size;
  96. }
  97. RID ViewportTexture::get_rid() const {
  98. //ERR_FAIL_COND_V_MSG(!vp, RID(), "Viewport Texture must be set to use it.");
  99. return proxy;
  100. }
  101. bool ViewportTexture::has_alpha() const {
  102. return false;
  103. }
  104. Ref<Image> ViewportTexture::get_data() const {
  105. ERR_FAIL_COND_V_MSG(!vp, Ref<Image>(), "Viewport Texture must be set to use it.");
  106. return VS::get_singleton()->texture_get_data(vp->texture_rid);
  107. }
  108. void ViewportTexture::set_flags(uint32_t p_flags) {
  109. flags = p_flags;
  110. if (!vp) {
  111. return;
  112. }
  113. vp->texture_flags = flags;
  114. VS::get_singleton()->texture_set_flags(vp->texture_rid, flags);
  115. }
  116. uint32_t ViewportTexture::get_flags() const {
  117. return flags;
  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, "Viewport", 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. vp = nullptr;
  126. flags = 0;
  127. set_local_to_scene(true);
  128. proxy = RID_PRIME(VS::get_singleton()->texture_create());
  129. }
  130. ViewportTexture::~ViewportTexture() {
  131. if (vp) {
  132. vp->viewport_textures.erase(this);
  133. }
  134. VS::get_singleton()->free(proxy);
  135. }
  136. /////////////////////////////////////
  137. // Aliases used to provide custom styles to tooltips in the default
  138. // theme and editor theme.
  139. // TooltipPanel is also used for custom tooltips, while TooltipLabel
  140. // is only relevant for default tooltips.
  141. class TooltipPanel : public PanelContainer {
  142. GDCLASS(TooltipPanel, PanelContainer);
  143. public:
  144. TooltipPanel(){};
  145. };
  146. class TooltipLabel : public Label {
  147. GDCLASS(TooltipLabel, Label);
  148. public:
  149. TooltipLabel(){};
  150. };
  151. /////////////////////////////////////
  152. Viewport::GUI::GUI() {
  153. dragging = false;
  154. drag_successful = false;
  155. mouse_in_window = true;
  156. mouse_focus = nullptr;
  157. mouse_click_grabber = nullptr;
  158. mouse_focus_mask = 0;
  159. key_focus = nullptr;
  160. mouse_over = nullptr;
  161. tooltip_control = nullptr;
  162. tooltip_popup = nullptr;
  163. tooltip_label = nullptr;
  164. subwindow_visibility_dirty = false;
  165. subwindow_order_dirty = false;
  166. }
  167. /////////////////////////////////////
  168. void Viewport::_update_stretch_transform() {
  169. if (size_override_stretch && size_override) {
  170. stretch_transform = Transform2D();
  171. Size2 scale = size / (size_override_size + size_override_margin * 2);
  172. stretch_transform.scale(scale);
  173. stretch_transform.elements[2] = size_override_margin * scale;
  174. } else {
  175. stretch_transform = Transform2D();
  176. }
  177. _update_global_transform();
  178. }
  179. void Viewport::update_worlds() {
  180. if (!is_inside_tree()) {
  181. return;
  182. }
  183. Rect2 abstracted_rect = Rect2(Vector2(), get_visible_rect().size);
  184. Rect2 xformed_rect = (global_canvas_transform * canvas_transform).affine_inverse().xform(abstracted_rect);
  185. find_world_2d()->_update_viewport(this, xformed_rect);
  186. find_world_2d()->_update();
  187. find_world()->_update(get_tree()->get_frame());
  188. }
  189. void Viewport::_collision_object_input_event(CollisionObject *p_object, Camera *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) {
  190. Transform object_transform = p_object->get_global_transform();
  191. Transform camera_transform = p_camera->get_global_transform();
  192. ObjectID id = p_object->get_instance_id();
  193. //avoid sending the fake event unnecessarily if nothing really changed in the context
  194. if (object_transform == physics_last_object_transform && camera_transform == physics_last_camera_transform && physics_last_id == id) {
  195. Ref<InputEventMouseMotion> mm = p_input_event;
  196. if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
  197. return; //discarded
  198. }
  199. }
  200. p_object->_input_event(camera, p_input_event, p_pos, p_normal, p_shape);
  201. physics_last_object_transform = object_transform;
  202. physics_last_camera_transform = camera_transform;
  203. physics_last_id = id;
  204. }
  205. void Viewport::_own_world_changed() {
  206. ERR_FAIL_COND(world.is_null());
  207. ERR_FAIL_COND(own_world.is_null());
  208. if (is_inside_tree()) {
  209. _propagate_exit_world(this);
  210. }
  211. own_world = world->duplicate();
  212. if (is_inside_tree()) {
  213. _propagate_enter_world(this);
  214. }
  215. if (is_inside_tree()) {
  216. VisualServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario());
  217. }
  218. _update_listener();
  219. }
  220. void Viewport::_notification(int p_what) {
  221. switch (p_what) {
  222. case NOTIFICATION_ENTER_TREE: {
  223. if (get_parent()) {
  224. parent = get_parent()->get_viewport();
  225. VisualServer::get_singleton()->viewport_set_parent_viewport(viewport, parent->get_viewport_rid());
  226. } else {
  227. parent = nullptr;
  228. }
  229. current_canvas = find_world_2d()->get_canvas();
  230. VisualServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario());
  231. VisualServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
  232. _update_listener();
  233. _update_listener_2d();
  234. find_world_2d()->_register_viewport(this, Rect2());
  235. add_to_group("_viewports");
  236. if (get_tree()->is_debugging_collisions_hint()) {
  237. //2D
  238. Physics2DServer::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(), get_tree()->get_collision_debug_contact_count());
  239. contact_2d_debug = RID_PRIME(VisualServer::get_singleton()->canvas_item_create());
  240. VisualServer::get_singleton()->canvas_item_set_parent(contact_2d_debug, find_world_2d()->get_canvas());
  241. //3D
  242. PhysicsServer::get_singleton()->space_set_debug_contacts(find_world()->get_space(), get_tree()->get_collision_debug_contact_count());
  243. contact_3d_debug_multimesh = RID_PRIME(VisualServer::get_singleton()->multimesh_create());
  244. VisualServer::get_singleton()->multimesh_allocate(contact_3d_debug_multimesh, get_tree()->get_collision_debug_contact_count(), VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_8BIT);
  245. VisualServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, 0);
  246. VisualServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh, get_tree()->get_debug_contact_mesh()->get_rid());
  247. contact_3d_debug_instance = RID_PRIME(VisualServer::get_singleton()->instance_create());
  248. VisualServer::get_singleton()->instance_set_base(contact_3d_debug_instance, contact_3d_debug_multimesh);
  249. VisualServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world()->get_scenario());
  250. //VisualServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance, VS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS, true);
  251. }
  252. VS::get_singleton()->viewport_set_active(viewport, true);
  253. } break;
  254. case NOTIFICATION_READY: {
  255. #ifndef _3D_DISABLED
  256. if (listeners.size() && !listener) {
  257. Listener *first = nullptr;
  258. for (Set<Listener *>::Element *E = listeners.front(); E; E = E->next()) {
  259. if (first == nullptr || first->is_greater_than(E->get())) {
  260. first = E->get();
  261. }
  262. }
  263. if (first) {
  264. first->make_current();
  265. }
  266. }
  267. if (cameras.size() && !camera) {
  268. //there are cameras but no current camera, pick first in tree and make it current
  269. Camera *first = nullptr;
  270. for (Set<Camera *>::Element *E = cameras.front(); E; E = E->next()) {
  271. if (first == nullptr || first->is_greater_than(E->get())) {
  272. first = E->get();
  273. }
  274. }
  275. if (first) {
  276. first->make_current();
  277. }
  278. }
  279. #endif
  280. // Enable processing for tooltips, collision debugging, physics object picking, etc.
  281. set_physics_process_internal(true);
  282. } break;
  283. case NOTIFICATION_EXIT_TREE: {
  284. _gui_cancel_tooltip();
  285. if (world_2d.is_valid()) {
  286. world_2d->_remove_viewport(this);
  287. }
  288. VisualServer::get_singleton()->viewport_set_scenario(viewport, RID());
  289. // SpatialSoundServer::get_singleton()->listener_set_space(internal_listener, RID());
  290. VisualServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
  291. if (contact_2d_debug.is_valid()) {
  292. VisualServer::get_singleton()->free(contact_2d_debug);
  293. contact_2d_debug = RID();
  294. }
  295. if (contact_3d_debug_multimesh.is_valid()) {
  296. VisualServer::get_singleton()->free(contact_3d_debug_multimesh);
  297. VisualServer::get_singleton()->free(contact_3d_debug_instance);
  298. contact_3d_debug_instance = RID();
  299. contact_3d_debug_multimesh = RID();
  300. }
  301. remove_from_group("_viewports");
  302. VS::get_singleton()->viewport_set_active(viewport, false);
  303. } break;
  304. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  305. if (!get_tree()) {
  306. return;
  307. }
  308. if (get_tree()->is_debugging_collisions_hint() && contact_2d_debug.is_valid()) {
  309. VisualServer::get_singleton()->canvas_item_clear(contact_2d_debug);
  310. VisualServer::get_singleton()->canvas_item_set_draw_index(contact_2d_debug, 0xFFFFF); //very high index
  311. Vector<Vector2> points = Physics2DServer::get_singleton()->space_get_contacts(find_world_2d()->get_space());
  312. int point_count = Physics2DServer::get_singleton()->space_get_contact_count(find_world_2d()->get_space());
  313. Color ccol = get_tree()->get_debug_collision_contact_color();
  314. for (int i = 0; i < point_count; i++) {
  315. VisualServer::get_singleton()->canvas_item_add_rect(contact_2d_debug, Rect2(points[i] - Vector2(2, 2), Vector2(5, 5)), ccol);
  316. }
  317. }
  318. if (get_tree()->is_debugging_collisions_hint() && contact_3d_debug_multimesh.is_valid()) {
  319. Vector<Vector3> points = PhysicsServer::get_singleton()->space_get_contacts(find_world()->get_space());
  320. int point_count = PhysicsServer::get_singleton()->space_get_contact_count(find_world()->get_space());
  321. VS::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, point_count);
  322. for (int i = 0; i < point_count; i++) {
  323. Transform point_transform;
  324. point_transform.origin = points[i];
  325. VS::get_singleton()->multimesh_instance_set_transform(contact_3d_debug_multimesh, i, point_transform);
  326. }
  327. }
  328. if (!GLOBAL_GET("physics/common/enable_pause_aware_picking")) {
  329. _process_picking(false);
  330. }
  331. } break;
  332. case NOTIFICATION_WM_MOUSE_ENTER: {
  333. gui.mouse_in_window = true;
  334. } break;
  335. case NOTIFICATION_WM_MOUSE_EXIT: {
  336. gui.mouse_in_window = false;
  337. _drop_physics_mouseover();
  338. _drop_mouse_over();
  339. // When the mouse exits the window, we want to end mouse_over, but
  340. // not mouse_focus, because, for example, we want to continue
  341. // dragging a scrollbar even if the mouse has left the window.
  342. } break;
  343. case NOTIFICATION_WM_FOCUS_OUT: {
  344. _drop_physics_mouseover();
  345. if (gui.mouse_focus) {
  346. _drop_mouse_focus();
  347. }
  348. // When the window focus changes, we want to end mouse_focus, but
  349. // not the mouse_over. Note: The OS will trigger a separate mouse
  350. // exit event if the change in focus results in the mouse exiting
  351. // the window.
  352. } break;
  353. }
  354. }
  355. void Viewport::_process_picking(bool p_ignore_paused) {
  356. if (!is_inside_tree()) {
  357. return;
  358. }
  359. if (!physics_object_picking) {
  360. return;
  361. }
  362. if (to_screen_rect != Rect2() && Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  363. return;
  364. }
  365. if (p_ignore_paused) {
  366. _drop_physics_mouseover(true);
  367. }
  368. #ifndef _3D_DISABLED
  369. Vector2 last_pos(1e20, 1e20);
  370. CollisionObject *last_object = nullptr;
  371. ObjectID last_id = 0;
  372. #endif
  373. PhysicsDirectSpaceState::RayResult result;
  374. Physics2DDirectSpaceState *ss2d = Physics2DServer::get_singleton()->space_get_direct_state(find_world_2d()->get_space());
  375. if (physics_has_last_mousepos) {
  376. // if no mouse event exists, create a motion one. This is necessary because objects or camera may have moved.
  377. // while this extra event is sent, it is checked if both camera and last object and last ID did not move. If nothing changed, the event is discarded to avoid flooding with unnecessary motion events every frame
  378. bool has_mouse_event = false;
  379. for (List<Ref<InputEvent>>::Element *E = physics_picking_events.front(); E; E = E->next()) {
  380. Ref<InputEventMouse> m = E->get();
  381. if (m.is_valid()) {
  382. has_mouse_event = true;
  383. break;
  384. }
  385. }
  386. if (!has_mouse_event) {
  387. Ref<InputEventMouseMotion> mm;
  388. mm.instance();
  389. mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
  390. mm->set_global_position(physics_last_mousepos);
  391. mm->set_position(physics_last_mousepos);
  392. mm->set_alt(physics_last_mouse_state.alt);
  393. mm->set_shift(physics_last_mouse_state.shift);
  394. mm->set_control(physics_last_mouse_state.control);
  395. mm->set_metakey(physics_last_mouse_state.meta);
  396. mm->set_button_mask(physics_last_mouse_state.mouse_mask);
  397. physics_picking_events.push_back(mm);
  398. }
  399. }
  400. while (physics_picking_events.size()) {
  401. Ref<InputEvent> ev = physics_picking_events.front()->get();
  402. physics_picking_events.pop_front();
  403. Vector2 pos;
  404. bool is_mouse = false;
  405. Ref<InputEventMouseMotion> mm = ev;
  406. if (mm.is_valid()) {
  407. pos = mm->get_position();
  408. is_mouse = true;
  409. physics_has_last_mousepos = true;
  410. physics_last_mousepos = pos;
  411. physics_last_mouse_state.alt = mm->get_alt();
  412. physics_last_mouse_state.shift = mm->get_shift();
  413. physics_last_mouse_state.control = mm->get_control();
  414. physics_last_mouse_state.meta = mm->get_metakey();
  415. physics_last_mouse_state.mouse_mask = mm->get_button_mask();
  416. }
  417. Ref<InputEventMouseButton> mb = ev;
  418. if (mb.is_valid()) {
  419. pos = mb->get_position();
  420. is_mouse = true;
  421. physics_has_last_mousepos = true;
  422. physics_last_mousepos = pos;
  423. physics_last_mouse_state.alt = mb->get_alt();
  424. physics_last_mouse_state.shift = mb->get_shift();
  425. physics_last_mouse_state.control = mb->get_control();
  426. physics_last_mouse_state.meta = mb->get_metakey();
  427. if (mb->is_pressed()) {
  428. physics_last_mouse_state.mouse_mask |= (1 << (mb->get_button_index() - 1));
  429. } else {
  430. physics_last_mouse_state.mouse_mask &= ~(1 << (mb->get_button_index() - 1));
  431. // If touch mouse raised, assume we don't know last mouse pos until new events come
  432. if (mb->get_device() == InputEvent::DEVICE_ID_TOUCH_MOUSE) {
  433. physics_has_last_mousepos = false;
  434. }
  435. }
  436. }
  437. Ref<InputEventKey> k = ev;
  438. if (k.is_valid()) {
  439. //only for mask
  440. physics_last_mouse_state.alt = k->get_alt();
  441. physics_last_mouse_state.shift = k->get_shift();
  442. physics_last_mouse_state.control = k->get_control();
  443. physics_last_mouse_state.meta = k->get_metakey();
  444. continue;
  445. }
  446. Ref<InputEventScreenDrag> sd = ev;
  447. if (sd.is_valid()) {
  448. pos = sd->get_position();
  449. }
  450. Ref<InputEventScreenTouch> st = ev;
  451. if (st.is_valid()) {
  452. pos = st->get_position();
  453. }
  454. if (ss2d) {
  455. //send to 2D
  456. uint64_t frame = get_tree()->get_frame();
  457. Physics2DDirectSpaceState::ShapeResult res[64];
  458. for (Set<CanvasLayer *>::Element *E = canvas_layers.front(); E; E = E->next()) {
  459. Transform2D canvas_transform;
  460. ObjectID canvas_layer_id;
  461. if (E->get()) {
  462. // A descendant CanvasLayer
  463. canvas_transform = E->get()->get_final_transform();
  464. canvas_layer_id = E->get()->get_instance_id();
  465. } else {
  466. // This Viewport's builtin canvas
  467. canvas_transform = get_canvas_transform();
  468. canvas_layer_id = 0;
  469. }
  470. Vector2 point = canvas_transform.affine_inverse().xform(pos);
  471. int rc = ss2d->intersect_point_on_canvas(point, canvas_layer_id, res, 64, Set<RID>(), 0xFFFFFFFF, true, true, true);
  472. for (int i = 0; i < rc; i++) {
  473. if (res[i].collider_id && res[i].collider) {
  474. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(res[i].collider);
  475. if (co && (!p_ignore_paused || co->can_process())) {
  476. bool send_event = true;
  477. if (is_mouse) {
  478. Map<ObjectID, uint64_t>::Element *F = physics_2d_mouseover.find(res[i].collider_id);
  479. if (!F) {
  480. physics_2d_mouseover.insert(res[i].collider_id, frame);
  481. co->_mouse_enter();
  482. } else {
  483. F->get() = frame;
  484. // It was already hovered, so don't send the event if it's faked
  485. if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
  486. send_event = false;
  487. }
  488. }
  489. }
  490. if (send_event) {
  491. co->_input_event(this, ev, res[i].shape);
  492. }
  493. }
  494. }
  495. }
  496. }
  497. if (is_mouse) {
  498. List<Map<ObjectID, uint64_t>::Element *> to_erase;
  499. for (Map<ObjectID, uint64_t>::Element *E = physics_2d_mouseover.front(); E; E = E->next()) {
  500. if (E->get() != frame) {
  501. Object *o = ObjectDB::get_instance(E->key());
  502. if (o) {
  503. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
  504. if (co) {
  505. co->_mouse_exit();
  506. }
  507. }
  508. to_erase.push_back(E);
  509. }
  510. }
  511. while (to_erase.size()) {
  512. physics_2d_mouseover.erase(to_erase.front()->get());
  513. to_erase.pop_front();
  514. }
  515. }
  516. }
  517. #ifndef _3D_DISABLED
  518. bool captured = false;
  519. if (physics_object_capture != 0) {
  520. CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(physics_object_capture));
  521. if (co && camera) {
  522. _collision_object_input_event(co, camera, ev, Vector3(), Vector3(), 0);
  523. captured = true;
  524. if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) {
  525. physics_object_capture = 0;
  526. }
  527. } else {
  528. physics_object_capture = 0;
  529. }
  530. }
  531. if (captured) {
  532. //none
  533. } else if (pos == last_pos) {
  534. if (last_id) {
  535. if (ObjectDB::get_instance(last_id) && last_object) {
  536. //good, exists
  537. _collision_object_input_event(last_object, camera, ev, result.position, result.normal, result.shape);
  538. if (last_object->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) {
  539. physics_object_capture = last_id;
  540. }
  541. }
  542. }
  543. } else {
  544. if (camera) {
  545. Vector3 from = camera->project_ray_origin(pos);
  546. Vector3 dir = camera->project_ray_normal(pos);
  547. float far = camera->far;
  548. PhysicsDirectSpaceState *space = PhysicsServer::get_singleton()->space_get_direct_state(find_world()->get_space());
  549. if (space) {
  550. bool col = space->intersect_ray(from, from + dir * far, result, Set<RID>(), 0xFFFFFFFF, true, true, true);
  551. ObjectID new_collider = 0;
  552. if (col) {
  553. CollisionObject *co = Object::cast_to<CollisionObject>(result.collider);
  554. if (co && (!p_ignore_paused || co->can_process())) {
  555. _collision_object_input_event(co, camera, ev, result.position, result.normal, result.shape);
  556. last_object = co;
  557. last_id = result.collider_id;
  558. new_collider = last_id;
  559. if (co->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) {
  560. physics_object_capture = last_id;
  561. }
  562. }
  563. }
  564. if (is_mouse && new_collider != physics_object_over) {
  565. if (physics_object_over) {
  566. CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(physics_object_over));
  567. if (co) {
  568. co->_mouse_exit();
  569. }
  570. }
  571. if (new_collider) {
  572. CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(new_collider));
  573. if (co) {
  574. co->_mouse_enter();
  575. }
  576. }
  577. physics_object_over = new_collider;
  578. }
  579. }
  580. last_pos = pos;
  581. }
  582. }
  583. #endif
  584. }
  585. }
  586. RID Viewport::get_viewport_rid() const {
  587. return viewport;
  588. }
  589. void Viewport::set_use_arvr(bool p_use_arvr) {
  590. arvr = p_use_arvr;
  591. VS::get_singleton()->viewport_set_use_arvr(viewport, arvr);
  592. }
  593. bool Viewport::use_arvr() {
  594. return arvr;
  595. }
  596. void Viewport::update_canvas_items() {
  597. if (!is_inside_tree()) {
  598. return;
  599. }
  600. _update_canvas_items(this);
  601. }
  602. void Viewport::set_size(const Size2 &p_size) {
  603. if (size == p_size.floor()) {
  604. return;
  605. }
  606. size = p_size.floor();
  607. VS::get_singleton()->viewport_set_size(viewport, size.width, size.height);
  608. ViewportContainer *c = Object::cast_to<ViewportContainer>(get_parent());
  609. if (c) {
  610. c->minimum_size_changed();
  611. }
  612. _update_stretch_transform();
  613. update_configuration_warning();
  614. for (Set<ViewportTexture *>::Element *E = viewport_textures.front(); E; E = E->next()) {
  615. E->get()->emit_changed();
  616. }
  617. emit_signal("size_changed");
  618. }
  619. Rect2 Viewport::get_visible_rect() const {
  620. Rect2 r;
  621. if (size == Size2()) {
  622. r = Rect2(Point2(), OS::get_singleton()->get_window_size());
  623. } else {
  624. r = Rect2(Point2(), size);
  625. }
  626. if (size_override) {
  627. r.size = size_override_size;
  628. }
  629. return r;
  630. }
  631. Size2 Viewport::get_size() const {
  632. return size;
  633. }
  634. void Viewport::_update_listener() {
  635. /*
  636. if (is_inside_tree() && audio_listener && (camera || listener) && (!get_parent() || (Object::cast_to<Control>(get_parent()) && Object::cast_to<Control>(get_parent())->is_visible_in_tree()))) {
  637. SpatialSoundServer::get_singleton()->listener_set_space(internal_listener, find_world()->get_sound_space());
  638. } else {
  639. SpatialSoundServer::get_singleton()->listener_set_space(internal_listener, RID());
  640. }
  641. */
  642. }
  643. void Viewport::_update_listener_2d() {
  644. /*
  645. if (is_inside_tree() && audio_listener && (!get_parent() || (Object::cast_to<Control>(get_parent()) && Object::cast_to<Control>(get_parent())->is_visible_in_tree())))
  646. SpatialSound2DServer::get_singleton()->listener_set_space(internal_listener_2d, find_world_2d()->get_sound_space());
  647. else
  648. SpatialSound2DServer::get_singleton()->listener_set_space(internal_listener_2d, RID());
  649. */
  650. }
  651. void Viewport::set_as_audio_listener(bool p_enable) {
  652. if (p_enable == audio_listener) {
  653. return;
  654. }
  655. audio_listener = p_enable;
  656. _update_listener();
  657. }
  658. bool Viewport::is_audio_listener() const {
  659. return audio_listener;
  660. }
  661. void Viewport::set_as_audio_listener_2d(bool p_enable) {
  662. if (p_enable == audio_listener_2d) {
  663. return;
  664. }
  665. audio_listener_2d = p_enable;
  666. _update_listener_2d();
  667. }
  668. bool Viewport::is_audio_listener_2d() const {
  669. return audio_listener_2d;
  670. }
  671. Listener2D *Viewport::get_listener_2d() const {
  672. return listener_2d;
  673. }
  674. void Viewport::enable_canvas_transform_override(bool p_enable) {
  675. if (override_canvas_transform == p_enable) {
  676. return;
  677. }
  678. override_canvas_transform = p_enable;
  679. if (p_enable) {
  680. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform_override);
  681. } else {
  682. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform);
  683. }
  684. }
  685. bool Viewport::is_canvas_transform_override_enbled() const {
  686. return override_canvas_transform;
  687. }
  688. void Viewport::set_canvas_transform_override(const Transform2D &p_transform) {
  689. if (canvas_transform_override == p_transform) {
  690. return;
  691. }
  692. canvas_transform_override = p_transform;
  693. if (override_canvas_transform) {
  694. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform_override);
  695. }
  696. }
  697. Transform2D Viewport::get_canvas_transform_override() const {
  698. return canvas_transform_override;
  699. }
  700. void Viewport::set_canvas_transform(const Transform2D &p_transform) {
  701. canvas_transform = p_transform;
  702. if (!override_canvas_transform) {
  703. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform);
  704. }
  705. }
  706. Transform2D Viewport::get_canvas_transform() const {
  707. return canvas_transform;
  708. }
  709. void Viewport::_update_global_transform() {
  710. Transform2D sxform = stretch_transform * global_canvas_transform;
  711. VisualServer::get_singleton()->viewport_set_global_canvas_transform(viewport, sxform);
  712. }
  713. void Viewport::set_global_canvas_transform(const Transform2D &p_transform) {
  714. global_canvas_transform = p_transform;
  715. _update_global_transform();
  716. }
  717. Transform2D Viewport::get_global_canvas_transform() const {
  718. return global_canvas_transform;
  719. }
  720. void Viewport::_listener_transform_changed_notify() {
  721. #ifndef _3D_DISABLED
  722. //if (listener)
  723. // SpatialSoundServer::get_singleton()->listener_set_transform(internal_listener, listener->get_listener_transform());
  724. #endif
  725. }
  726. void Viewport::_listener_set(Listener *p_listener) {
  727. #ifndef _3D_DISABLED
  728. if (listener == p_listener) {
  729. return;
  730. }
  731. listener = p_listener;
  732. _update_listener();
  733. _listener_transform_changed_notify();
  734. #endif
  735. }
  736. bool Viewport::_listener_add(Listener *p_listener) {
  737. listeners.insert(p_listener);
  738. return listeners.size() == 1;
  739. }
  740. void Viewport::_listener_remove(Listener *p_listener) {
  741. listeners.erase(p_listener);
  742. if (listener == p_listener) {
  743. listener = nullptr;
  744. }
  745. }
  746. #ifndef _3D_DISABLED
  747. void Viewport::_listener_make_next_current(Listener *p_exclude) {
  748. if (listeners.size() > 0) {
  749. for (Set<Listener *>::Element *E = listeners.front(); E; E = E->next()) {
  750. if (p_exclude == E->get()) {
  751. continue;
  752. }
  753. if (!E->get()->is_inside_tree()) {
  754. continue;
  755. }
  756. if (listener != nullptr) {
  757. return;
  758. }
  759. E->get()->make_current();
  760. }
  761. } else {
  762. // Attempt to reset listener to the camera position
  763. if (camera != nullptr) {
  764. _update_listener();
  765. _camera_transform_changed_notify();
  766. }
  767. }
  768. }
  769. #endif
  770. void Viewport::_camera_transform_changed_notify() {
  771. #ifndef _3D_DISABLED
  772. // If there is an active listener in the scene, it takes priority over the camera
  773. // if (camera && !listener)
  774. // SpatialSoundServer::get_singleton()->listener_set_transform(internal_listener, camera->get_camera_transform());
  775. #endif
  776. }
  777. void Viewport::_camera_set(Camera *p_camera) {
  778. #ifndef _3D_DISABLED
  779. if (camera == p_camera) {
  780. return;
  781. }
  782. if (camera) {
  783. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  784. }
  785. camera = p_camera;
  786. if (!camera_override) {
  787. if (camera) {
  788. VisualServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera());
  789. } else {
  790. VisualServer::get_singleton()->viewport_attach_camera(viewport, RID());
  791. }
  792. }
  793. if (camera) {
  794. camera->notification(Camera::NOTIFICATION_BECAME_CURRENT);
  795. }
  796. _update_listener();
  797. _camera_transform_changed_notify();
  798. #endif
  799. }
  800. bool Viewport::_camera_add(Camera *p_camera) {
  801. cameras.insert(p_camera);
  802. return cameras.size() == 1;
  803. }
  804. void Viewport::_camera_remove(Camera *p_camera) {
  805. cameras.erase(p_camera);
  806. if (camera == p_camera) {
  807. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  808. camera = nullptr;
  809. }
  810. }
  811. #ifndef _3D_DISABLED
  812. void Viewport::_camera_make_next_current(Camera *p_exclude) {
  813. for (Set<Camera *>::Element *E = cameras.front(); E; E = E->next()) {
  814. if (p_exclude == E->get()) {
  815. continue;
  816. }
  817. if (!E->get()->is_inside_tree()) {
  818. continue;
  819. }
  820. if (camera != nullptr) {
  821. return;
  822. }
  823. E->get()->make_current();
  824. }
  825. }
  826. #endif
  827. void Viewport::_listener_2d_set(Listener2D *p_listener) {
  828. if (listener_2d == p_listener) {
  829. return;
  830. } else if (listener_2d) {
  831. listener_2d->clear_current();
  832. }
  833. listener_2d = p_listener;
  834. }
  835. void Viewport::_listener_2d_remove(Listener2D *p_listener) {
  836. if (listener_2d == p_listener) {
  837. listener_2d = nullptr;
  838. }
  839. }
  840. void Viewport::_canvas_layer_add(CanvasLayer *p_canvas_layer) {
  841. canvas_layers.insert(p_canvas_layer);
  842. }
  843. void Viewport::_canvas_layer_remove(CanvasLayer *p_canvas_layer) {
  844. canvas_layers.erase(p_canvas_layer);
  845. }
  846. void Viewport::set_transparent_background(bool p_enable) {
  847. transparent_bg = p_enable;
  848. VS::get_singleton()->viewport_set_transparent_background(viewport, p_enable);
  849. }
  850. bool Viewport::has_transparent_background() const {
  851. return transparent_bg;
  852. }
  853. void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) {
  854. if (world_2d == p_world_2d) {
  855. return;
  856. }
  857. if (parent && parent->find_world_2d() == p_world_2d) {
  858. WARN_PRINT("Unable to use parent world as world_2d");
  859. return;
  860. }
  861. if (is_inside_tree()) {
  862. find_world_2d()->_remove_viewport(this);
  863. VisualServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
  864. }
  865. if (p_world_2d.is_valid()) {
  866. world_2d = p_world_2d;
  867. } else {
  868. WARN_PRINT("Invalid world");
  869. world_2d = Ref<World2D>(memnew(World2D));
  870. }
  871. _update_listener_2d();
  872. if (is_inside_tree()) {
  873. current_canvas = find_world_2d()->get_canvas();
  874. VisualServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
  875. find_world_2d()->_register_viewport(this, Rect2());
  876. }
  877. }
  878. Ref<World2D> Viewport::find_world_2d() const {
  879. if (world_2d.is_valid()) {
  880. return world_2d;
  881. } else if (parent) {
  882. return parent->find_world_2d();
  883. } else {
  884. return Ref<World2D>();
  885. }
  886. }
  887. void Viewport::_propagate_enter_world(Node *p_node) {
  888. if (p_node != this) {
  889. if (!p_node->is_inside_tree()) { //may not have entered scene yet
  890. return;
  891. }
  892. if (Object::cast_to<Spatial>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
  893. p_node->notification(Spatial::NOTIFICATION_ENTER_WORLD);
  894. } else {
  895. Viewport *v = Object::cast_to<Viewport>(p_node);
  896. if (v) {
  897. if (v->world.is_valid() || v->own_world.is_valid()) {
  898. return;
  899. }
  900. }
  901. }
  902. }
  903. for (int i = 0; i < p_node->get_child_count(); i++) {
  904. _propagate_enter_world(p_node->get_child(i));
  905. }
  906. }
  907. void Viewport::_propagate_viewport_notification(Node *p_node, int p_what) {
  908. p_node->notification(p_what);
  909. for (int i = 0; i < p_node->get_child_count(); i++) {
  910. Node *c = p_node->get_child(i);
  911. if (Object::cast_to<Viewport>(c)) {
  912. continue;
  913. }
  914. _propagate_viewport_notification(c, p_what);
  915. }
  916. }
  917. void Viewport::_propagate_exit_world(Node *p_node) {
  918. if (p_node != this) {
  919. if (!p_node->is_inside_tree()) { //may have exited scene already
  920. return;
  921. }
  922. if (Object::cast_to<Spatial>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
  923. p_node->notification(Spatial::NOTIFICATION_EXIT_WORLD);
  924. } else {
  925. Viewport *v = Object::cast_to<Viewport>(p_node);
  926. if (v) {
  927. if (v->world.is_valid() || v->own_world.is_valid()) {
  928. return;
  929. }
  930. }
  931. }
  932. }
  933. for (int i = 0; i < p_node->get_child_count(); i++) {
  934. _propagate_exit_world(p_node->get_child(i));
  935. }
  936. }
  937. void Viewport::set_world(const Ref<World> &p_world) {
  938. if (world == p_world) {
  939. return;
  940. }
  941. if (is_inside_tree()) {
  942. _propagate_exit_world(this);
  943. }
  944. if (own_world.is_valid() && world.is_valid()) {
  945. world->disconnect(CoreStringNames::get_singleton()->changed, this, "_own_world_changed");
  946. }
  947. world = p_world;
  948. if (own_world.is_valid()) {
  949. if (world.is_valid()) {
  950. own_world = world->duplicate();
  951. world->connect(CoreStringNames::get_singleton()->changed, this, "_own_world_changed");
  952. } else {
  953. own_world = Ref<World>(memnew(World));
  954. }
  955. }
  956. if (is_inside_tree()) {
  957. _propagate_enter_world(this);
  958. }
  959. if (is_inside_tree()) {
  960. VisualServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario());
  961. }
  962. _update_listener();
  963. }
  964. Ref<World> Viewport::get_world() const {
  965. return world;
  966. }
  967. Ref<World2D> Viewport::get_world_2d() const {
  968. return world_2d;
  969. }
  970. Ref<World> Viewport::find_world() const {
  971. if (own_world.is_valid()) {
  972. return own_world;
  973. } else if (world.is_valid()) {
  974. return world;
  975. } else if (parent) {
  976. return parent->find_world();
  977. } else {
  978. return Ref<World>();
  979. }
  980. }
  981. Listener *Viewport::get_listener() const {
  982. return listener;
  983. }
  984. Camera *Viewport::get_camera() const {
  985. return camera;
  986. }
  987. void Viewport::enable_camera_override(bool p_enable) {
  988. #ifndef _3D_DISABLED
  989. if (p_enable == camera_override) {
  990. return;
  991. }
  992. if (p_enable) {
  993. camera_override.rid = RID_PRIME(VisualServer::get_singleton()->camera_create());
  994. } else {
  995. VisualServer::get_singleton()->free(camera_override.rid);
  996. camera_override.rid = RID();
  997. }
  998. if (p_enable) {
  999. VisualServer::get_singleton()->viewport_attach_camera(viewport, camera_override.rid);
  1000. } else if (camera) {
  1001. VisualServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera());
  1002. } else {
  1003. VisualServer::get_singleton()->viewport_attach_camera(viewport, RID());
  1004. }
  1005. #endif
  1006. }
  1007. bool Viewport::is_camera_override_enabled() const {
  1008. return camera_override;
  1009. }
  1010. void Viewport::set_camera_override_transform(const Transform &p_transform) {
  1011. if (camera_override) {
  1012. camera_override.transform = p_transform;
  1013. VisualServer::get_singleton()->camera_set_transform(camera_override.rid, p_transform);
  1014. }
  1015. }
  1016. Transform Viewport::get_camera_override_transform() const {
  1017. if (camera_override) {
  1018. return camera_override.transform;
  1019. }
  1020. return Transform();
  1021. }
  1022. void Viewport::set_camera_override_perspective(float p_fovy_degrees, float p_z_near, float p_z_far) {
  1023. if (camera_override) {
  1024. if (camera_override.fov == p_fovy_degrees && camera_override.z_near == p_z_near &&
  1025. camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_PERSPECTIVE) {
  1026. return;
  1027. }
  1028. camera_override.fov = p_fovy_degrees;
  1029. camera_override.z_near = p_z_near;
  1030. camera_override.z_far = p_z_far;
  1031. camera_override.projection = CameraOverrideData::PROJECTION_PERSPECTIVE;
  1032. VisualServer::get_singleton()->camera_set_perspective(camera_override.rid, camera_override.fov, camera_override.z_near, camera_override.z_far);
  1033. }
  1034. }
  1035. void Viewport::set_camera_override_orthogonal(float p_size, float p_z_near, float p_z_far) {
  1036. if (camera_override) {
  1037. if (camera_override.size == p_size && camera_override.z_near == p_z_near &&
  1038. camera_override.z_far == p_z_far && camera_override.projection == CameraOverrideData::PROJECTION_ORTHOGONAL) {
  1039. return;
  1040. }
  1041. camera_override.size = p_size;
  1042. camera_override.z_near = p_z_near;
  1043. camera_override.z_far = p_z_far;
  1044. camera_override.projection = CameraOverrideData::PROJECTION_ORTHOGONAL;
  1045. VisualServer::get_singleton()->camera_set_orthogonal(camera_override.rid, camera_override.size, camera_override.z_near, camera_override.z_far);
  1046. }
  1047. }
  1048. Transform2D Viewport::get_final_transform() const {
  1049. return stretch_transform * global_canvas_transform;
  1050. }
  1051. void Viewport::_update_canvas_items(Node *p_node) {
  1052. if (p_node != this) {
  1053. Viewport *vp = Object::cast_to<Viewport>(p_node);
  1054. if (vp) {
  1055. return;
  1056. }
  1057. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
  1058. if (ci) {
  1059. ci->update();
  1060. }
  1061. }
  1062. int cc = p_node->get_child_count();
  1063. for (int i = 0; i < cc; i++) {
  1064. _update_canvas_items(p_node->get_child(i));
  1065. }
  1066. }
  1067. void Viewport::set_size_override(bool p_enable, const Size2 &p_size, const Vector2 &p_margin) {
  1068. if (size_override == p_enable && p_size == size_override_size) {
  1069. return;
  1070. }
  1071. size_override = p_enable;
  1072. if (p_size.x >= 0 || p_size.y >= 0) {
  1073. size_override_size = p_size;
  1074. }
  1075. size_override_margin = p_margin;
  1076. _update_stretch_transform();
  1077. emit_signal("size_changed");
  1078. }
  1079. Size2 Viewport::get_size_override() const {
  1080. return size_override_size;
  1081. }
  1082. bool Viewport::is_size_override_enabled() const {
  1083. return size_override;
  1084. }
  1085. void Viewport::set_size_override_stretch(bool p_enable) {
  1086. if (p_enable == size_override_stretch) {
  1087. return;
  1088. }
  1089. size_override_stretch = p_enable;
  1090. _update_stretch_transform();
  1091. }
  1092. bool Viewport::is_size_override_stretch_enabled() const {
  1093. return size_override_stretch;
  1094. }
  1095. void Viewport::set_update_mode(UpdateMode p_mode) {
  1096. update_mode = p_mode;
  1097. VS::get_singleton()->viewport_set_update_mode(viewport, VS::ViewportUpdateMode(p_mode));
  1098. }
  1099. Viewport::UpdateMode Viewport::get_update_mode() const {
  1100. return update_mode;
  1101. }
  1102. Ref<ViewportTexture> Viewport::get_texture() const {
  1103. return default_texture;
  1104. }
  1105. void Viewport::set_vflip(bool p_enable) {
  1106. vflip = p_enable;
  1107. VisualServer::get_singleton()->viewport_set_vflip(viewport, p_enable);
  1108. }
  1109. bool Viewport::get_vflip() const {
  1110. return vflip;
  1111. }
  1112. void Viewport::set_clear_mode(ClearMode p_mode) {
  1113. clear_mode = p_mode;
  1114. VS::get_singleton()->viewport_set_clear_mode(viewport, VS::ViewportClearMode(p_mode));
  1115. }
  1116. Viewport::ClearMode Viewport::get_clear_mode() const {
  1117. return clear_mode;
  1118. }
  1119. void Viewport::set_shadow_atlas_size(int p_size) {
  1120. if (shadow_atlas_size == p_size) {
  1121. return;
  1122. }
  1123. shadow_atlas_size = p_size;
  1124. VS::get_singleton()->viewport_set_shadow_atlas_size(viewport, p_size);
  1125. }
  1126. int Viewport::get_shadow_atlas_size() const {
  1127. return shadow_atlas_size;
  1128. }
  1129. void Viewport::set_shadow_atlas_quadrant_subdiv(int p_quadrant, ShadowAtlasQuadrantSubdiv p_subdiv) {
  1130. ERR_FAIL_INDEX(p_quadrant, 4);
  1131. ERR_FAIL_INDEX(p_subdiv, SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
  1132. if (shadow_atlas_quadrant_subdiv[p_quadrant] == p_subdiv) {
  1133. return;
  1134. }
  1135. shadow_atlas_quadrant_subdiv[p_quadrant] = p_subdiv;
  1136. static const int subdiv[SHADOW_ATLAS_QUADRANT_SUBDIV_MAX] = { 0, 1, 4, 16, 64, 256, 1024 };
  1137. VS::get_singleton()->viewport_set_shadow_atlas_quadrant_subdivision(viewport, p_quadrant, subdiv[p_subdiv]);
  1138. }
  1139. Viewport::ShadowAtlasQuadrantSubdiv Viewport::get_shadow_atlas_quadrant_subdiv(int p_quadrant) const {
  1140. ERR_FAIL_INDEX_V(p_quadrant, 4, SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
  1141. return shadow_atlas_quadrant_subdiv[p_quadrant];
  1142. }
  1143. Transform2D Viewport::_get_input_pre_xform() const {
  1144. Transform2D pre_xf;
  1145. if (to_screen_rect != Rect2()) {
  1146. pre_xf.elements[2] = -to_screen_rect.position;
  1147. pre_xf.scale(size / to_screen_rect.size);
  1148. }
  1149. return pre_xf;
  1150. }
  1151. Vector2 Viewport::_get_window_offset() const {
  1152. if (get_parent() && get_parent()->has_method("get_global_position")) {
  1153. return get_parent()->call("get_global_position");
  1154. }
  1155. return Vector2();
  1156. }
  1157. Ref<InputEvent> Viewport::_make_input_local(const Ref<InputEvent> &ev) {
  1158. if (ev.is_null()) {
  1159. return ev;
  1160. }
  1161. Vector2 vp_ofs = _get_window_offset();
  1162. Transform2D ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
  1163. return ev->xformed_by(ai, -vp_ofs);
  1164. }
  1165. void Viewport::_vp_input_text(const String &p_text) {
  1166. if (gui.key_focus) {
  1167. gui.key_focus->call("set_text", p_text);
  1168. }
  1169. }
  1170. void Viewport::_vp_input(const Ref<InputEvent> &p_ev) {
  1171. ERR_FAIL_COND(p_ev.is_null());
  1172. if (disable_input) {
  1173. return;
  1174. }
  1175. #ifdef TOOLS_ENABLED
  1176. if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
  1177. return;
  1178. }
  1179. #endif
  1180. if (to_screen_rect == Rect2()) {
  1181. return; //if render target, can't get input events
  1182. }
  1183. //this one handles system input, p_ev are in system coordinates
  1184. //they are converted to viewport coordinates
  1185. Ref<InputEvent> ev = _make_input_local(p_ev);
  1186. input(ev);
  1187. }
  1188. void Viewport::_vp_unhandled_input(const Ref<InputEvent> &p_ev) {
  1189. ERR_FAIL_COND(p_ev.is_null());
  1190. if (disable_input) {
  1191. return;
  1192. }
  1193. #ifdef TOOLS_ENABLED
  1194. if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
  1195. return;
  1196. }
  1197. #endif
  1198. /*
  1199. if (parent_control && !parent_control->is_visible_in_tree())
  1200. return;
  1201. */
  1202. if (to_screen_rect == Rect2()) {
  1203. return; //if render target, can't get input events
  1204. }
  1205. //this one handles system input, p_ev are in system coordinates
  1206. //they are converted to viewport coordinates
  1207. Ref<InputEvent> ev = _make_input_local(p_ev);
  1208. unhandled_input(ev);
  1209. }
  1210. Vector2 Viewport::get_mouse_position() const {
  1211. return (get_final_transform().affine_inverse() * _get_input_pre_xform()).xform(Input::get_singleton()->get_mouse_position() - _get_window_offset());
  1212. }
  1213. void Viewport::warp_mouse(const Vector2 &p_pos) {
  1214. Vector2 gpos = (get_final_transform().affine_inverse() * _get_input_pre_xform()).affine_inverse().xform(p_pos);
  1215. Input::get_singleton()->warp_mouse_position(gpos);
  1216. }
  1217. void Viewport::_gui_prepare_subwindows() {
  1218. if (gui.subwindow_visibility_dirty) {
  1219. gui.subwindows.clear();
  1220. for (List<Control *>::Element *E = gui.all_known_subwindows.front(); E; E = E->next()) {
  1221. if (E->get()->is_visible_in_tree()) {
  1222. gui.subwindows.push_back(E->get());
  1223. }
  1224. }
  1225. gui.subwindow_visibility_dirty = false;
  1226. gui.subwindow_order_dirty = true;
  1227. }
  1228. _gui_sort_subwindows();
  1229. }
  1230. void Viewport::_gui_sort_subwindows() {
  1231. if (!gui.subwindow_order_dirty) {
  1232. return;
  1233. }
  1234. gui.modal_stack.sort_custom<Control::CComparator>();
  1235. gui.subwindows.sort_custom<Control::CComparator>();
  1236. gui.subwindow_order_dirty = false;
  1237. }
  1238. void Viewport::_gui_sort_modal_stack() {
  1239. gui.modal_stack.sort_custom<Control::CComparator>();
  1240. }
  1241. void Viewport::_gui_sort_roots() {
  1242. if (!gui.roots_order_dirty) {
  1243. return;
  1244. }
  1245. gui.roots.sort_custom<Control::CComparator>();
  1246. gui.roots_order_dirty = false;
  1247. }
  1248. void Viewport::_gui_cancel_tooltip() {
  1249. gui.tooltip_control = nullptr;
  1250. if (gui.tooltip_timer.is_valid()) {
  1251. gui.tooltip_timer->release_connections();
  1252. gui.tooltip_timer = Ref<SceneTreeTimer>();
  1253. }
  1254. if (gui.tooltip_popup) {
  1255. gui.tooltip_popup->queue_delete();
  1256. gui.tooltip_popup = nullptr;
  1257. gui.tooltip_label = nullptr;
  1258. }
  1259. }
  1260. String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Control **r_tooltip_owner) {
  1261. Vector2 pos = p_pos;
  1262. String tooltip;
  1263. while (p_control) {
  1264. tooltip = p_control->get_tooltip(pos);
  1265. if (r_tooltip_owner) {
  1266. *r_tooltip_owner = p_control;
  1267. }
  1268. // If we found a tooltip, we stop here.
  1269. if (!tooltip.empty()) {
  1270. break;
  1271. }
  1272. // Otherwise, we check parent controls unless some conditions prevent it.
  1273. if (p_control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1274. break;
  1275. }
  1276. if (p_control->is_set_as_toplevel()) {
  1277. break;
  1278. }
  1279. // Transform cursor pos for parent control.
  1280. pos = p_control->get_transform().xform(pos);
  1281. p_control = p_control->get_parent_control();
  1282. }
  1283. return tooltip;
  1284. }
  1285. void Viewport::_gui_show_tooltip() {
  1286. if (!gui.tooltip_control) {
  1287. return;
  1288. }
  1289. // Get the Control under cursor and the relevant tooltip text, if any.
  1290. Control *tooltip_owner = nullptr;
  1291. String tooltip_text = _gui_get_tooltip(
  1292. gui.tooltip_control,
  1293. gui.tooltip_control->get_global_transform().xform_inv(gui.tooltip_pos),
  1294. &tooltip_owner);
  1295. tooltip_text = tooltip_text.strip_edges();
  1296. if (tooltip_text.empty()) {
  1297. return; // Nothing to show.
  1298. }
  1299. // Remove previous popup if we change something.
  1300. if (gui.tooltip_popup) {
  1301. memdelete(gui.tooltip_popup);
  1302. gui.tooltip_popup = nullptr;
  1303. gui.tooltip_label = nullptr;
  1304. }
  1305. if (!tooltip_owner) {
  1306. return;
  1307. }
  1308. // Controls can implement `make_custom_tooltip` to provide their own tooltip.
  1309. // This should be a Control node which will be added as child to the tooltip owner.
  1310. gui.tooltip_popup = tooltip_owner->make_custom_tooltip(tooltip_text);
  1311. // If no custom tooltip is given, use a default implementation.
  1312. if (!gui.tooltip_popup) {
  1313. gui.tooltip_popup = memnew(TooltipPanel);
  1314. gui.tooltip_label = memnew(TooltipLabel);
  1315. gui.tooltip_popup->add_child(gui.tooltip_label);
  1316. Ref<StyleBox> ttp = gui.tooltip_label->get_stylebox("panel", "TooltipPanel");
  1317. gui.tooltip_label->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_BEGIN, ttp->get_margin(MARGIN_LEFT));
  1318. gui.tooltip_label->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, ttp->get_margin(MARGIN_TOP));
  1319. gui.tooltip_label->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -ttp->get_margin(MARGIN_RIGHT));
  1320. gui.tooltip_label->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, -ttp->get_margin(MARGIN_BOTTOM));
  1321. gui.tooltip_label->set_text(tooltip_text);
  1322. }
  1323. tooltip_owner->add_child(gui.tooltip_popup);
  1324. gui.tooltip_popup->force_parent_owned();
  1325. gui.tooltip_popup->set_as_toplevel(true);
  1326. if (gui.tooltip_control) { // Avoids crash when rapidly switching controls.
  1327. gui.tooltip_popup->set_scale(gui.tooltip_control->get_global_transform().get_scale());
  1328. }
  1329. Point2 tooltip_offset = ProjectSettings::get_singleton()->get("display/mouse_cursor/tooltip_position_offset");
  1330. Rect2 r(gui.tooltip_pos + tooltip_offset, gui.tooltip_popup->get_minimum_size());
  1331. Rect2 vr = gui.tooltip_popup->get_viewport_rect();
  1332. if (r.size.x * gui.tooltip_popup->get_scale().x + r.position.x > vr.size.x) {
  1333. r.position.x = vr.size.x - r.size.x * gui.tooltip_popup->get_scale().x;
  1334. } else if (r.position.x < 0) {
  1335. r.position.x = 0;
  1336. }
  1337. if (r.size.y * gui.tooltip_popup->get_scale().y + r.position.y > vr.size.y) {
  1338. r.position.y = vr.size.y - r.size.y * gui.tooltip_popup->get_scale().y;
  1339. } else if (r.position.y < 0) {
  1340. r.position.y = 0;
  1341. }
  1342. gui.tooltip_popup->set_global_position(r.position);
  1343. gui.tooltip_popup->set_size(r.size);
  1344. gui.tooltip_popup->raise();
  1345. gui.tooltip_popup->show();
  1346. }
  1347. void Viewport::_gui_call_input(Control *p_control, const Ref<InputEvent> &p_input) {
  1348. //_block();
  1349. Ref<InputEvent> ev = p_input;
  1350. //mouse wheel events can't be stopped
  1351. Ref<InputEventMouseButton> mb = p_input;
  1352. bool cant_stop_me_now = (mb.is_valid() &&
  1353. (mb->get_button_index() == BUTTON_WHEEL_DOWN ||
  1354. mb->get_button_index() == BUTTON_WHEEL_UP ||
  1355. mb->get_button_index() == BUTTON_WHEEL_LEFT ||
  1356. mb->get_button_index() == BUTTON_WHEEL_RIGHT));
  1357. Ref<InputEventPanGesture> pn = p_input;
  1358. cant_stop_me_now = pn.is_valid() || cant_stop_me_now;
  1359. bool ismouse = ev.is_valid() || Object::cast_to<InputEventMouseMotion>(*p_input) != nullptr;
  1360. CanvasItem *ci = p_control;
  1361. while (ci) {
  1362. Control *control = Object::cast_to<Control>(ci);
  1363. if (control) {
  1364. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1365. control->emit_signal(SceneStringNames::get_singleton()->gui_input, ev); //signal should be first, so it's possible to override an event (and then accept it)
  1366. }
  1367. if (gui.key_event_accepted) {
  1368. break;
  1369. }
  1370. if (!control->is_inside_tree()) {
  1371. break;
  1372. }
  1373. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1374. control->call_multilevel(SceneStringNames::get_singleton()->_gui_input, ev);
  1375. }
  1376. if (!control->is_inside_tree() || control->is_set_as_toplevel()) {
  1377. break;
  1378. }
  1379. if (gui.key_event_accepted) {
  1380. break;
  1381. }
  1382. if (!cant_stop_me_now && control->data.mouse_filter == Control::MOUSE_FILTER_STOP && ismouse) {
  1383. break;
  1384. }
  1385. }
  1386. if (ci->is_set_as_toplevel()) {
  1387. break;
  1388. }
  1389. ev = ev->xformed_by(ci->get_transform()); //transform event upwards
  1390. ci = ci->get_parent_item();
  1391. }
  1392. //_unblock();
  1393. }
  1394. void Viewport::_gui_call_notification(Control *p_control, int p_what) {
  1395. CanvasItem *ci = p_control;
  1396. while (ci) {
  1397. Control *control = Object::cast_to<Control>(ci);
  1398. if (control) {
  1399. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1400. control->notification(p_what);
  1401. }
  1402. if (!control->is_inside_tree()) {
  1403. break;
  1404. }
  1405. if (!control->is_inside_tree() || control->is_set_as_toplevel()) {
  1406. break;
  1407. }
  1408. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1409. break;
  1410. }
  1411. }
  1412. if (ci->is_set_as_toplevel()) {
  1413. break;
  1414. }
  1415. ci = ci->get_parent_item();
  1416. }
  1417. //_unblock();
  1418. }
  1419. Control *Viewport::_gui_find_control(const Point2 &p_global) {
  1420. _gui_prepare_subwindows();
  1421. for (List<Control *>::Element *E = gui.subwindows.back(); E; E = E->prev()) {
  1422. Control *sw = E->get();
  1423. if (!sw->is_visible_in_tree()) {
  1424. continue;
  1425. }
  1426. Transform2D xform;
  1427. CanvasItem *pci = sw->get_parent_item();
  1428. if (pci) {
  1429. xform = pci->get_global_transform_with_canvas();
  1430. } else {
  1431. xform = sw->get_canvas_transform();
  1432. }
  1433. Control *ret = _gui_find_control_at_pos(sw, p_global, xform, gui.focus_inv_xform);
  1434. if (ret) {
  1435. return ret;
  1436. }
  1437. }
  1438. _gui_sort_roots();
  1439. for (List<Control *>::Element *E = gui.roots.back(); E; E = E->prev()) {
  1440. Control *sw = E->get();
  1441. if (!sw->is_visible_in_tree()) {
  1442. continue;
  1443. }
  1444. Transform2D xform;
  1445. CanvasItem *pci = sw->get_parent_item();
  1446. if (pci) {
  1447. xform = pci->get_global_transform_with_canvas();
  1448. } else {
  1449. xform = sw->get_canvas_transform();
  1450. }
  1451. Control *ret = _gui_find_control_at_pos(sw, p_global, xform, gui.focus_inv_xform);
  1452. if (ret) {
  1453. return ret;
  1454. }
  1455. }
  1456. return nullptr;
  1457. }
  1458. Control *Viewport::_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Transform2D &p_xform, Transform2D &r_inv_xform) {
  1459. if (Object::cast_to<Viewport>(p_node)) {
  1460. return nullptr;
  1461. }
  1462. //subwindows first!!
  1463. if (!p_node->is_visible()) {
  1464. //return _find_next_visible_control_at_pos(p_node,p_global,r_inv_xform);
  1465. return nullptr; //canvas item hidden, discard
  1466. }
  1467. Transform2D matrix = p_xform * p_node->get_transform();
  1468. // matrix.determinant() == 0.0f implies that node does not exist on scene
  1469. if (matrix.determinant() == 0.0f) {
  1470. return nullptr;
  1471. }
  1472. Control *c = Object::cast_to<Control>(p_node);
  1473. if (!c || !c->clips_input() || c->has_point(matrix.affine_inverse().xform(p_global))) {
  1474. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  1475. if (p_node == gui.tooltip_popup) {
  1476. continue;
  1477. }
  1478. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node->get_child(i));
  1479. if (!ci || ci->is_set_as_toplevel()) {
  1480. continue;
  1481. }
  1482. Control *ret = _gui_find_control_at_pos(ci, p_global, matrix, r_inv_xform);
  1483. if (ret) {
  1484. return ret;
  1485. }
  1486. }
  1487. }
  1488. if (!c || c->data.mouse_filter == Control::MOUSE_FILTER_IGNORE) {
  1489. return nullptr;
  1490. }
  1491. matrix.affine_invert();
  1492. if (!c->has_point(matrix.xform(p_global))) {
  1493. return nullptr;
  1494. }
  1495. Control *drag_preview = _gui_get_drag_preview();
  1496. if (!drag_preview || (c != drag_preview && !drag_preview->is_a_parent_of(c))) {
  1497. r_inv_xform = matrix;
  1498. return c;
  1499. }
  1500. return nullptr;
  1501. }
  1502. bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check) {
  1503. { //attempt grab, try parent controls too
  1504. CanvasItem *ci = p_at_control;
  1505. while (ci) {
  1506. Control *control = Object::cast_to<Control>(ci);
  1507. if (control) {
  1508. if (control->can_drop_data(p_at_pos, gui.drag_data)) {
  1509. if (!p_just_check) {
  1510. control->drop_data(p_at_pos, gui.drag_data);
  1511. }
  1512. return true;
  1513. }
  1514. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1515. break;
  1516. }
  1517. }
  1518. p_at_pos = ci->get_transform().xform(p_at_pos);
  1519. if (ci->is_set_as_toplevel()) {
  1520. break;
  1521. }
  1522. ci = ci->get_parent_item();
  1523. }
  1524. }
  1525. return false;
  1526. }
  1527. void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
  1528. ERR_FAIL_COND(p_event.is_null());
  1529. //?
  1530. /*
  1531. if (!is_visible()) {
  1532. return; //simple and plain
  1533. }
  1534. */
  1535. Ref<InputEventMouseButton> mb = p_event;
  1536. if (mb.is_valid()) {
  1537. gui.key_event_accepted = false;
  1538. Point2 mpos = mb->get_position();
  1539. if (mb->is_pressed()) {
  1540. Size2 pos = mpos;
  1541. if (gui.mouse_focus_mask) {
  1542. //do not steal mouse focus and stuff while a focus mask exists
  1543. gui.mouse_focus_mask |= 1 << (mb->get_button_index() - 1); //add the button to the mask
  1544. } else {
  1545. bool is_handled = false;
  1546. _gui_sort_modal_stack();
  1547. while (!gui.modal_stack.empty()) {
  1548. Control *top = gui.modal_stack.back()->get();
  1549. Vector2 pos2 = top->get_global_transform_with_canvas().affine_inverse().xform(mpos);
  1550. if (!top->has_point(pos2)) {
  1551. if (top->data.modal_exclusive || top->data.modal_frame == Engine::get_singleton()->get_frames_drawn()) {
  1552. //cancel event, sorry, modal exclusive EATS UP ALL
  1553. //alternative, you can't pop out a window the same frame it was made modal (fixes many issues)
  1554. set_input_as_handled();
  1555. return; // no one gets the event if exclusive NO ONE
  1556. }
  1557. if (mb->get_button_index() == BUTTON_WHEEL_UP || mb->get_button_index() == BUTTON_WHEEL_DOWN || mb->get_button_index() == BUTTON_WHEEL_LEFT || mb->get_button_index() == BUTTON_WHEEL_RIGHT) {
  1558. //cancel scroll wheel events, only clicks should trigger focus changes.
  1559. set_input_as_handled();
  1560. return;
  1561. }
  1562. top->notification(Control::NOTIFICATION_MODAL_CLOSE);
  1563. top->_modal_stack_remove();
  1564. top->hide();
  1565. if (!top->get_pass_on_modal_close_click()) {
  1566. is_handled = true;
  1567. }
  1568. } else {
  1569. break;
  1570. }
  1571. }
  1572. if (is_handled) {
  1573. set_input_as_handled();
  1574. return;
  1575. }
  1576. //Matrix32 parent_xform;
  1577. /*
  1578. if (data.parent_canvas_item)
  1579. parent_xform=data.parent_canvas_item->get_global_transform();
  1580. */
  1581. gui.mouse_focus = _gui_find_control(pos);
  1582. gui.last_mouse_focus = gui.mouse_focus;
  1583. if (!gui.mouse_focus) {
  1584. gui.mouse_focus_mask = 0;
  1585. return;
  1586. }
  1587. gui.mouse_focus_mask = 1 << (mb->get_button_index() - 1);
  1588. if (mb->get_button_index() == BUTTON_LEFT) {
  1589. gui.drag_accum = Vector2();
  1590. gui.drag_attempted = false;
  1591. }
  1592. }
  1593. mb = mb->xformed_by(Transform2D()); // make a copy of the event
  1594. mb->set_global_position(pos);
  1595. pos = gui.focus_inv_xform.xform(pos);
  1596. mb->set_position(pos);
  1597. #ifdef DEBUG_ENABLED
  1598. if (ScriptDebugger::get_singleton() && gui.mouse_focus) {
  1599. Array arr;
  1600. arr.push_back(gui.mouse_focus->get_path());
  1601. arr.push_back(gui.mouse_focus->get_class());
  1602. ScriptDebugger::get_singleton()->send_message("click_ctrl", arr);
  1603. }
  1604. #endif
  1605. if (mb->get_button_index() == BUTTON_LEFT) { //assign focus
  1606. CanvasItem *ci = gui.mouse_focus;
  1607. while (ci) {
  1608. Control *control = Object::cast_to<Control>(ci);
  1609. if (control) {
  1610. if (control->get_focus_mode() != Control::FOCUS_NONE) {
  1611. if (control != gui.key_focus) {
  1612. control->grab_focus();
  1613. }
  1614. break;
  1615. }
  1616. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1617. break;
  1618. }
  1619. }
  1620. if (ci->is_set_as_toplevel()) {
  1621. break;
  1622. }
  1623. ci = ci->get_parent_item();
  1624. }
  1625. }
  1626. if (gui.mouse_focus && gui.mouse_focus->can_process()) {
  1627. _gui_call_input(gui.mouse_focus, mb);
  1628. }
  1629. set_input_as_handled();
  1630. if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == BUTTON_LEFT) {
  1631. //alternate drop use (when using force_drag(), as proposed by #5342
  1632. gui.drag_successful = false;
  1633. if (gui.mouse_focus) {
  1634. gui.drag_successful = _gui_drop(gui.mouse_focus, pos, false);
  1635. }
  1636. gui.drag_data = Variant();
  1637. gui.dragging = false;
  1638. Control *drag_preview = _gui_get_drag_preview();
  1639. if (drag_preview) {
  1640. memdelete(drag_preview);
  1641. gui.drag_preview_id = 0;
  1642. }
  1643. _propagate_viewport_notification(this, NOTIFICATION_DRAG_END);
  1644. //change mouse accordingly
  1645. }
  1646. _gui_cancel_tooltip();
  1647. } else {
  1648. if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == BUTTON_LEFT) {
  1649. gui.drag_successful = false;
  1650. if (gui.mouse_over) {
  1651. Size2 pos = mpos;
  1652. pos = gui.focus_inv_xform.xform(pos);
  1653. gui.drag_successful = _gui_drop(gui.mouse_over, pos, false);
  1654. }
  1655. Control *drag_preview = _gui_get_drag_preview();
  1656. if (drag_preview) {
  1657. memdelete(drag_preview);
  1658. gui.drag_preview_id = 0;
  1659. }
  1660. gui.drag_data = Variant();
  1661. gui.dragging = false;
  1662. _propagate_viewport_notification(this, NOTIFICATION_DRAG_END);
  1663. //change mouse accordingly
  1664. }
  1665. gui.mouse_focus_mask &= ~(1 << (mb->get_button_index() - 1)); //remove from mask
  1666. if (!gui.mouse_focus) {
  1667. //release event is only sent if a mouse focus (previously pressed button) exists
  1668. return;
  1669. }
  1670. Size2 pos = mpos;
  1671. mb = mb->xformed_by(Transform2D()); //make a copy
  1672. mb->set_global_position(pos);
  1673. pos = gui.focus_inv_xform.xform(pos);
  1674. mb->set_position(pos);
  1675. Control *mouse_focus = gui.mouse_focus;
  1676. //disable mouse focus if needed before calling input, this makes popups on mouse press event work better, as the release will never be received otherwise
  1677. if (gui.mouse_focus_mask == 0) {
  1678. gui.mouse_focus = nullptr;
  1679. }
  1680. if (mouse_focus && mouse_focus->can_process()) {
  1681. _gui_call_input(mouse_focus, mb);
  1682. }
  1683. /*if (gui.drag_data.get_type()!=Variant::NIL && mb->get_button_index()==BUTTON_LEFT) {
  1684. _propagate_viewport_notification(this,NOTIFICATION_DRAG_END);
  1685. gui.drag_data=Variant(); //always clear
  1686. }*/
  1687. // In case the mouse was released after for example dragging a scrollbar,
  1688. // check whether the current control is different from the stored one. If
  1689. // it is different, rather than wait for it to be updated the next time the
  1690. // mouse is moved, notify the control so that it can e.g. drop the highlight.
  1691. // This code is duplicated from the mm.is_valid()-case further below.
  1692. Control *over = nullptr;
  1693. if (gui.mouse_focus) {
  1694. over = gui.mouse_focus;
  1695. } else {
  1696. over = _gui_find_control(mpos);
  1697. }
  1698. if (gui.mouse_focus_mask == 0 && over != gui.mouse_over) {
  1699. _drop_mouse_over();
  1700. _gui_cancel_tooltip();
  1701. if (over) {
  1702. _gui_call_notification(over, Control::NOTIFICATION_MOUSE_ENTER);
  1703. }
  1704. }
  1705. gui.mouse_over = over;
  1706. set_input_as_handled();
  1707. }
  1708. }
  1709. Ref<InputEventMouseMotion> mm = p_event;
  1710. if (mm.is_valid()) {
  1711. gui.key_event_accepted = false;
  1712. Point2 mpos = mm->get_position();
  1713. gui.last_mouse_pos = mpos;
  1714. // D&D
  1715. if (!gui.drag_attempted && gui.mouse_focus && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  1716. gui.drag_accum += mm->get_relative();
  1717. float len = gui.drag_accum.length();
  1718. if (len > 10) {
  1719. { //attempt grab, try parent controls too
  1720. CanvasItem *ci = gui.mouse_focus;
  1721. while (ci) {
  1722. Control *control = Object::cast_to<Control>(ci);
  1723. if (control) {
  1724. gui.dragging = true;
  1725. gui.drag_data = control->get_drag_data(control->get_global_transform_with_canvas().affine_inverse().xform(mpos) - gui.drag_accum);
  1726. if (gui.drag_data.get_type() != Variant::NIL) {
  1727. gui.mouse_focus = nullptr;
  1728. gui.mouse_focus_mask = 0;
  1729. break;
  1730. } else {
  1731. Control *drag_preview = _gui_get_drag_preview();
  1732. if (drag_preview) {
  1733. ERR_PRINT("Don't set a drag preview and return null data. Preview was deleted and drag request ignored.");
  1734. memdelete(drag_preview);
  1735. gui.drag_preview_id = 0;
  1736. }
  1737. gui.dragging = false;
  1738. }
  1739. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1740. break;
  1741. }
  1742. }
  1743. if (ci->is_set_as_toplevel()) {
  1744. break;
  1745. }
  1746. ci = ci->get_parent_item();
  1747. }
  1748. }
  1749. gui.drag_attempted = true;
  1750. if (gui.drag_data.get_type() != Variant::NIL) {
  1751. _propagate_viewport_notification(this, NOTIFICATION_DRAG_BEGIN);
  1752. }
  1753. }
  1754. }
  1755. Control *over = nullptr;
  1756. if (gui.mouse_focus) {
  1757. over = gui.mouse_focus;
  1758. } else if (gui.mouse_in_window) {
  1759. over = _gui_find_control(mpos);
  1760. }
  1761. if (gui.drag_data.get_type() == Variant::NIL && over && !gui.modal_stack.empty()) {
  1762. Control *top = gui.modal_stack.back()->get();
  1763. if (over != top && !top->is_a_parent_of(over)) {
  1764. PopupMenu *popup_menu = Object::cast_to<PopupMenu>(top);
  1765. MenuButton *popup_menu_parent = nullptr;
  1766. MenuButton *menu_button = Object::cast_to<MenuButton>(over);
  1767. if (popup_menu) {
  1768. popup_menu_parent = Object::cast_to<MenuButton>(popup_menu->get_parent());
  1769. if (!popup_menu_parent) {
  1770. // Go through the parents to see if there's a MenuButton at the end.
  1771. while (Object::cast_to<PopupMenu>(popup_menu->get_parent())) {
  1772. popup_menu = Object::cast_to<PopupMenu>(popup_menu->get_parent());
  1773. }
  1774. popup_menu_parent = Object::cast_to<MenuButton>(popup_menu->get_parent());
  1775. }
  1776. }
  1777. // If the mouse is over a menu button, this menu will open automatically
  1778. // if there is already a pop-up menu open at the same hierarchical level.
  1779. if (popup_menu_parent && menu_button && popup_menu_parent->is_switch_on_hover() &&
  1780. !menu_button->is_disabled() && menu_button->is_switch_on_hover() &&
  1781. (popup_menu_parent->get_parent()->is_a_parent_of(menu_button) ||
  1782. menu_button->get_parent()->is_a_parent_of(popup_menu))) {
  1783. popup_menu->notification(Control::NOTIFICATION_MODAL_CLOSE);
  1784. popup_menu->_modal_stack_remove();
  1785. popup_menu->hide();
  1786. menu_button->pressed();
  1787. // As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually.
  1788. menu_button->get_popup()->set_current_index(-1);
  1789. } else {
  1790. over = nullptr; //nothing can be found outside the modal stack
  1791. }
  1792. }
  1793. }
  1794. if (over != gui.mouse_over) {
  1795. _drop_mouse_over();
  1796. _gui_cancel_tooltip();
  1797. if (over) {
  1798. _gui_call_notification(over, Control::NOTIFICATION_MOUSE_ENTER);
  1799. gui.mouse_over = over;
  1800. }
  1801. }
  1802. Control *drag_preview = _gui_get_drag_preview();
  1803. if (drag_preview) {
  1804. drag_preview->set_position(mpos);
  1805. }
  1806. if (!over) {
  1807. OS::get_singleton()->set_cursor_shape((OS::CursorShape)Input::get_singleton()->get_default_cursor_shape());
  1808. return;
  1809. }
  1810. Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
  1811. Size2 pos = localizer.xform(mpos);
  1812. Vector2 speed = localizer.basis_xform(mm->get_speed());
  1813. Vector2 rel = localizer.basis_xform(mm->get_relative());
  1814. mm = mm->xformed_by(Transform2D()); //make a copy
  1815. mm->set_global_position(mpos);
  1816. mm->set_speed(speed);
  1817. mm->set_relative(rel);
  1818. if (mm->get_button_mask() == 0) {
  1819. //nothing pressed
  1820. bool can_tooltip = true;
  1821. if (!gui.modal_stack.empty()) {
  1822. if (gui.modal_stack.back()->get() != over && !gui.modal_stack.back()->get()->is_a_parent_of(over)) {
  1823. can_tooltip = false;
  1824. }
  1825. }
  1826. bool is_tooltip_shown = false;
  1827. if (gui.tooltip_popup) {
  1828. if (can_tooltip && gui.tooltip_control) {
  1829. String tooltip = _gui_get_tooltip(over, gui.tooltip_control->get_global_transform().xform_inv(mpos));
  1830. if (tooltip.length() == 0) {
  1831. _gui_cancel_tooltip();
  1832. } else if (gui.tooltip_label) {
  1833. if (tooltip == gui.tooltip_label->get_text()) {
  1834. is_tooltip_shown = true;
  1835. }
  1836. }
  1837. } else {
  1838. _gui_cancel_tooltip();
  1839. }
  1840. }
  1841. if (can_tooltip && !is_tooltip_shown && over->can_process()) {
  1842. if (gui.tooltip_timer.is_valid()) {
  1843. gui.tooltip_timer->release_connections();
  1844. gui.tooltip_timer = Ref<SceneTreeTimer>();
  1845. }
  1846. gui.tooltip_control = over;
  1847. gui.tooltip_pos = mpos;
  1848. gui.tooltip_timer = get_tree()->create_timer(gui.tooltip_delay);
  1849. gui.tooltip_timer->set_ignore_time_scale(true);
  1850. gui.tooltip_timer->connect("timeout", this, "_gui_show_tooltip");
  1851. }
  1852. }
  1853. mm->set_position(pos);
  1854. Control::CursorShape cursor_shape = Control::CURSOR_ARROW;
  1855. {
  1856. Control *c = over;
  1857. Vector2 cpos = pos;
  1858. while (c) {
  1859. cursor_shape = c->get_cursor_shape(cpos);
  1860. cpos = c->get_transform().xform(cpos);
  1861. if (cursor_shape != Control::CURSOR_ARROW) {
  1862. break;
  1863. }
  1864. if (c->data.mouse_filter == Control::MOUSE_FILTER_STOP) {
  1865. break;
  1866. }
  1867. if (c->is_set_as_toplevel()) {
  1868. break;
  1869. }
  1870. c = c->get_parent_control();
  1871. }
  1872. }
  1873. OS::get_singleton()->set_cursor_shape((OS::CursorShape)cursor_shape);
  1874. if (over && over->can_process()) {
  1875. _gui_call_input(over, mm);
  1876. }
  1877. set_input_as_handled();
  1878. if (gui.drag_data.get_type() != Variant::NIL) {
  1879. bool can_drop = _gui_drop(over, pos, true);
  1880. if (!can_drop) {
  1881. OS::get_singleton()->set_cursor_shape(OS::CURSOR_FORBIDDEN);
  1882. } else {
  1883. OS::get_singleton()->set_cursor_shape(OS::CURSOR_CAN_DROP);
  1884. }
  1885. }
  1886. }
  1887. Ref<InputEventScreenTouch> touch_event = p_event;
  1888. if (touch_event.is_valid()) {
  1889. Size2 pos = touch_event->get_position();
  1890. const int touch_index = touch_event->get_index();
  1891. if (touch_event->is_pressed()) {
  1892. Control *over = _gui_find_control(pos);
  1893. if (over) {
  1894. gui.touch_focus[touch_index] = over->get_instance_id();
  1895. if (!gui.modal_stack.empty()) {
  1896. Control *top = gui.modal_stack.back()->get();
  1897. if (over != top && !top->is_a_parent_of(over)) {
  1898. return;
  1899. }
  1900. }
  1901. if (over->can_process()) {
  1902. touch_event = touch_event->xformed_by(Transform2D()); //make a copy
  1903. if (over == gui.mouse_focus) {
  1904. pos = gui.focus_inv_xform.xform(pos);
  1905. } else {
  1906. pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
  1907. }
  1908. touch_event->set_position(pos);
  1909. _gui_call_input(over, touch_event);
  1910. set_input_as_handled();
  1911. }
  1912. return;
  1913. }
  1914. } else {
  1915. ObjectID control_id = gui.touch_focus[touch_index];
  1916. Control *over = Object::cast_to<Control>(ObjectDB::get_instance(control_id));
  1917. if (over && over->can_process()) {
  1918. touch_event = touch_event->xformed_by(Transform2D()); //make a copy
  1919. if (over == gui.last_mouse_focus) {
  1920. pos = gui.focus_inv_xform.xform(pos);
  1921. } else {
  1922. pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
  1923. }
  1924. touch_event->set_position(pos);
  1925. _gui_call_input(over, touch_event);
  1926. set_input_as_handled();
  1927. }
  1928. gui.touch_focus.erase(touch_index);
  1929. return;
  1930. }
  1931. }
  1932. Ref<InputEventGesture> gesture_event = p_event;
  1933. if (gesture_event.is_valid()) {
  1934. gui.key_event_accepted = false;
  1935. _gui_cancel_tooltip();
  1936. Size2 pos = gesture_event->get_position();
  1937. Control *over = _gui_find_control(pos);
  1938. if (over) {
  1939. if (over->can_process()) {
  1940. gesture_event = gesture_event->xformed_by(Transform2D()); //make a copy
  1941. if (over == gui.mouse_focus) {
  1942. pos = gui.focus_inv_xform.xform(pos);
  1943. } else {
  1944. pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
  1945. }
  1946. gesture_event->set_position(pos);
  1947. _gui_call_input(over, gesture_event);
  1948. }
  1949. set_input_as_handled();
  1950. return;
  1951. }
  1952. }
  1953. Ref<InputEventScreenDrag> drag_event = p_event;
  1954. if (drag_event.is_valid()) {
  1955. const int drag_event_index = drag_event->get_index();
  1956. ObjectID control_id = gui.touch_focus[drag_event_index];
  1957. Control *over = Object::cast_to<Control>(ObjectDB::get_instance(control_id));
  1958. if (!over) {
  1959. over = _gui_find_control(drag_event->get_position());
  1960. }
  1961. if (over) {
  1962. if (!gui.modal_stack.empty()) {
  1963. Control *top = gui.modal_stack.back()->get();
  1964. if (over != top && !top->is_a_parent_of(over)) {
  1965. return;
  1966. }
  1967. }
  1968. if (over->can_process()) {
  1969. Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
  1970. Size2 pos = localizer.xform(drag_event->get_position());
  1971. Vector2 speed = localizer.basis_xform(drag_event->get_speed());
  1972. Vector2 rel = localizer.basis_xform(drag_event->get_relative());
  1973. drag_event = drag_event->xformed_by(Transform2D()); //make a copy
  1974. drag_event->set_speed(speed);
  1975. drag_event->set_relative(rel);
  1976. drag_event->set_position(pos);
  1977. _gui_call_input(over, drag_event);
  1978. }
  1979. set_input_as_handled();
  1980. return;
  1981. }
  1982. }
  1983. if (mm.is_null() && mb.is_null() && p_event->is_action_type()) {
  1984. if (gui.key_focus && !gui.key_focus->is_visible_in_tree()) {
  1985. gui.key_focus->release_focus();
  1986. }
  1987. if (gui.key_focus) {
  1988. gui.key_event_accepted = false;
  1989. if (gui.key_focus->can_process()) {
  1990. gui.key_focus->call_multilevel(SceneStringNames::get_singleton()->_gui_input, p_event);
  1991. if (gui.key_focus) { //maybe lost it
  1992. gui.key_focus->emit_signal(SceneStringNames::get_singleton()->gui_input, p_event);
  1993. }
  1994. }
  1995. if (gui.key_event_accepted) {
  1996. set_input_as_handled();
  1997. return;
  1998. }
  1999. }
  2000. if (p_event->is_pressed() && p_event->is_action("ui_cancel") && !gui.modal_stack.empty()) {
  2001. _gui_sort_modal_stack();
  2002. Control *top = gui.modal_stack.back()->get();
  2003. if (!top->data.modal_exclusive) {
  2004. top->notification(Control::NOTIFICATION_MODAL_CLOSE);
  2005. top->_modal_stack_remove();
  2006. top->hide();
  2007. // Close modal, set input as handled
  2008. set_input_as_handled();
  2009. return;
  2010. }
  2011. }
  2012. Control *from = gui.key_focus ? gui.key_focus : nullptr; //hmm
  2013. //keyboard focus
  2014. //if (from && p_event->is_pressed() && !p_event->get_alt() && !p_event->get_metakey() && !p_event->key->get_command()) {
  2015. Ref<InputEventKey> k = p_event;
  2016. //need to check for mods, otherwise any combination of alt/ctrl/shift+<up/down/left/right/etc> is handled here when it shouldn't be.
  2017. bool mods = k.is_valid() && (k->get_control() || k->get_alt() || k->get_shift() || k->get_metakey());
  2018. if (from && p_event->is_pressed()) {
  2019. Control *next = nullptr;
  2020. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  2021. if (joypadmotion_event.is_valid()) {
  2022. Input *input = Input::get_singleton();
  2023. if (p_event->is_action_pressed("ui_focus_next") && input->is_action_just_pressed("ui_focus_next")) {
  2024. next = from->find_next_valid_focus();
  2025. }
  2026. if (p_event->is_action_pressed("ui_focus_prev") && input->is_action_just_pressed("ui_focus_prev")) {
  2027. next = from->find_prev_valid_focus();
  2028. }
  2029. if (!mods && p_event->is_action_pressed("ui_up") && input->is_action_just_pressed("ui_up")) {
  2030. next = from->_get_focus_neighbour(MARGIN_TOP);
  2031. }
  2032. if (!mods && p_event->is_action_pressed("ui_left") && input->is_action_just_pressed("ui_left")) {
  2033. next = from->_get_focus_neighbour(MARGIN_LEFT);
  2034. }
  2035. if (!mods && p_event->is_action_pressed("ui_right") && input->is_action_just_pressed("ui_right")) {
  2036. next = from->_get_focus_neighbour(MARGIN_RIGHT);
  2037. }
  2038. if (!mods && p_event->is_action_pressed("ui_down") && input->is_action_just_pressed("ui_down")) {
  2039. next = from->_get_focus_neighbour(MARGIN_BOTTOM);
  2040. }
  2041. } else {
  2042. if (p_event->is_action_pressed("ui_focus_next", true, true)) {
  2043. next = from->find_next_valid_focus();
  2044. }
  2045. if (p_event->is_action_pressed("ui_focus_prev", true, true)) {
  2046. next = from->find_prev_valid_focus();
  2047. }
  2048. if (!mods && p_event->is_action_pressed("ui_up", true)) {
  2049. next = from->_get_focus_neighbour(MARGIN_TOP);
  2050. }
  2051. if (!mods && p_event->is_action_pressed("ui_left", true)) {
  2052. next = from->_get_focus_neighbour(MARGIN_LEFT);
  2053. }
  2054. if (!mods && p_event->is_action_pressed("ui_right", true)) {
  2055. next = from->_get_focus_neighbour(MARGIN_RIGHT);
  2056. }
  2057. if (!mods && p_event->is_action_pressed("ui_down", true)) {
  2058. next = from->_get_focus_neighbour(MARGIN_BOTTOM);
  2059. }
  2060. }
  2061. if (next) {
  2062. next->grab_focus();
  2063. set_input_as_handled();
  2064. }
  2065. }
  2066. }
  2067. }
  2068. void Viewport::_gui_cleanup_internal_state(Ref<InputEvent> p_event) {
  2069. ERR_FAIL_COND(p_event.is_null());
  2070. Ref<InputEventMouseButton> mb = p_event;
  2071. if (mb.is_valid()) {
  2072. if (!mb->is_pressed()) {
  2073. gui.mouse_focus_mask &= ~(1 << (mb->get_button_index() - 1)); // Remove from mask.
  2074. }
  2075. }
  2076. }
  2077. List<Control *>::Element *Viewport::_gui_add_root_control(Control *p_control) {
  2078. gui.roots_order_dirty = true;
  2079. return gui.roots.push_back(p_control);
  2080. }
  2081. List<Control *>::Element *Viewport::_gui_add_subwindow_control(Control *p_control) {
  2082. p_control->connect("visibility_changed", this, "_subwindow_visibility_changed");
  2083. if (p_control->is_visible_in_tree()) {
  2084. gui.subwindow_order_dirty = true;
  2085. gui.subwindows.push_back(p_control);
  2086. }
  2087. return gui.all_known_subwindows.push_back(p_control);
  2088. }
  2089. void Viewport::_gui_set_subwindow_order_dirty() {
  2090. gui.subwindow_order_dirty = true;
  2091. }
  2092. void Viewport::_gui_set_root_order_dirty() {
  2093. gui.roots_order_dirty = true;
  2094. }
  2095. void Viewport::_gui_remove_modal_control(List<Control *>::Element *MI) {
  2096. gui.modal_stack.erase(MI);
  2097. }
  2098. void Viewport::_gui_remove_from_modal_stack(List<Control *>::Element *MI, ObjectID p_prev_focus_owner) {
  2099. //transfer the focus stack to the next
  2100. List<Control *>::Element *next = MI->next();
  2101. gui.modal_stack.erase(MI);
  2102. if (p_prev_focus_owner) {
  2103. // for previous window in stack, pass the focus so it feels more
  2104. // natural
  2105. if (!next) { //top of stack
  2106. Object *pfo = ObjectDB::get_instance(p_prev_focus_owner);
  2107. Control *pfoc = Object::cast_to<Control>(pfo);
  2108. if (!pfoc) {
  2109. return;
  2110. }
  2111. if (!pfoc->is_inside_tree() || !pfoc->is_visible_in_tree()) {
  2112. return;
  2113. }
  2114. pfoc->grab_focus();
  2115. } else {
  2116. next->get()->_modal_set_prev_focus_owner(p_prev_focus_owner);
  2117. }
  2118. }
  2119. }
  2120. void Viewport::_gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control) {
  2121. ERR_FAIL_COND_MSG(p_data.get_type() == Variant::NIL, "Drag data must be a value.");
  2122. gui.dragging = true;
  2123. gui.drag_data = p_data;
  2124. gui.mouse_focus = nullptr;
  2125. if (p_control) {
  2126. _gui_set_drag_preview(p_base, p_control);
  2127. }
  2128. _propagate_viewport_notification(this, NOTIFICATION_DRAG_BEGIN);
  2129. }
  2130. void Viewport::_gui_set_drag_preview(Control *p_base, Control *p_control) {
  2131. ERR_FAIL_NULL(p_control);
  2132. ERR_FAIL_COND(!Object::cast_to<Control>((Object *)p_control));
  2133. ERR_FAIL_COND(p_control->is_inside_tree());
  2134. ERR_FAIL_COND(p_control->get_parent() != nullptr);
  2135. Control *drag_preview = _gui_get_drag_preview();
  2136. if (drag_preview) {
  2137. memdelete(drag_preview);
  2138. }
  2139. p_control->set_as_toplevel(true);
  2140. p_control->set_position(gui.last_mouse_pos);
  2141. p_base->get_root_parent_control()->add_child(p_control); //add as child of viewport
  2142. p_control->raise();
  2143. gui.drag_preview_id = p_control->get_instance_id();
  2144. }
  2145. Control *Viewport::_gui_get_drag_preview() {
  2146. if (!gui.drag_preview_id) {
  2147. return nullptr;
  2148. } else {
  2149. Control *drag_preview = Object::cast_to<Control>(ObjectDB::get_instance(gui.drag_preview_id));
  2150. if (!drag_preview) {
  2151. ERR_PRINT("Don't free the control set as drag preview.");
  2152. gui.drag_preview_id = 0;
  2153. }
  2154. return drag_preview;
  2155. }
  2156. }
  2157. void Viewport::_gui_remove_root_control(List<Control *>::Element *RI) {
  2158. gui.roots.erase(RI);
  2159. }
  2160. void Viewport::_gui_remove_subwindow_control(List<Control *>::Element *SI) {
  2161. ERR_FAIL_COND(!SI);
  2162. Control *control = SI->get();
  2163. control->disconnect("visibility_changed", this, "_subwindow_visibility_changed");
  2164. List<Control *>::Element *E = gui.subwindows.find(control);
  2165. if (E) {
  2166. gui.subwindows.erase(E);
  2167. }
  2168. gui.all_known_subwindows.erase(SI);
  2169. }
  2170. void Viewport::_gui_unfocus_control(Control *p_control) {
  2171. if (gui.key_focus == p_control) {
  2172. gui.key_focus->release_focus();
  2173. }
  2174. }
  2175. void Viewport::_gui_hid_control(Control *p_control) {
  2176. if (gui.mouse_focus == p_control) {
  2177. _drop_mouse_focus();
  2178. }
  2179. if (gui.key_focus == p_control) {
  2180. _gui_remove_focus();
  2181. }
  2182. if (gui.mouse_over == p_control) {
  2183. gui.mouse_over = nullptr;
  2184. }
  2185. if (gui.tooltip_control == p_control) {
  2186. _gui_cancel_tooltip();
  2187. }
  2188. }
  2189. void Viewport::_gui_remove_control(Control *p_control) {
  2190. if (gui.mouse_focus == p_control) {
  2191. gui.mouse_focus = nullptr;
  2192. gui.mouse_focus_mask = 0;
  2193. }
  2194. if (gui.last_mouse_focus == p_control) {
  2195. gui.last_mouse_focus = nullptr;
  2196. }
  2197. if (gui.key_focus == p_control) {
  2198. gui.key_focus = nullptr;
  2199. }
  2200. if (gui.mouse_over == p_control) {
  2201. gui.mouse_over = nullptr;
  2202. }
  2203. if (gui.tooltip_control == p_control) {
  2204. gui.tooltip_control = nullptr;
  2205. }
  2206. if (gui.tooltip_popup == p_control) {
  2207. _gui_cancel_tooltip();
  2208. }
  2209. }
  2210. void Viewport::_gui_remove_focus() {
  2211. if (gui.key_focus) {
  2212. Node *f = gui.key_focus;
  2213. gui.key_focus = nullptr;
  2214. f->notification(Control::NOTIFICATION_FOCUS_EXIT, true);
  2215. }
  2216. }
  2217. bool Viewport::_gui_is_modal_on_top(const Control *p_control) {
  2218. return (gui.modal_stack.size() && gui.modal_stack.back()->get() == p_control);
  2219. }
  2220. bool Viewport::_gui_control_has_focus(const Control *p_control) {
  2221. return gui.key_focus == p_control;
  2222. }
  2223. void Viewport::_gui_control_grab_focus(Control *p_control) {
  2224. //no need for change
  2225. if (gui.key_focus && gui.key_focus == p_control) {
  2226. return;
  2227. }
  2228. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, "_viewports", "_gui_remove_focus");
  2229. gui.key_focus = p_control;
  2230. emit_signal("gui_focus_changed", p_control);
  2231. p_control->notification(Control::NOTIFICATION_FOCUS_ENTER);
  2232. p_control->update();
  2233. }
  2234. void Viewport::_gui_accept_event() {
  2235. gui.key_event_accepted = true;
  2236. if (is_inside_tree()) {
  2237. set_input_as_handled();
  2238. }
  2239. }
  2240. void Viewport::_drop_mouse_focus() {
  2241. Control *c = gui.mouse_focus;
  2242. int mask = gui.mouse_focus_mask;
  2243. gui.mouse_focus = nullptr;
  2244. gui.mouse_focus_mask = 0;
  2245. for (int i = 0; i < 3; i++) {
  2246. if (mask & (1 << i)) {
  2247. Ref<InputEventMouseButton> mb;
  2248. mb.instance();
  2249. mb->set_position(c->get_local_mouse_position());
  2250. mb->set_global_position(c->get_local_mouse_position());
  2251. mb->set_button_index(i + 1);
  2252. mb->set_pressed(false);
  2253. c->call_multilevel(SceneStringNames::get_singleton()->_gui_input, mb);
  2254. }
  2255. }
  2256. }
  2257. void Viewport::_drop_mouse_over() {
  2258. if (gui.mouse_over) {
  2259. _gui_call_notification(gui.mouse_over, Control::NOTIFICATION_MOUSE_EXIT);
  2260. gui.mouse_over = nullptr;
  2261. }
  2262. }
  2263. void Viewport::_drop_physics_mouseover(bool p_paused_only) {
  2264. physics_has_last_mousepos = false;
  2265. List<Map<ObjectID, uint64_t>::Element *> to_erase;
  2266. for (Map<ObjectID, uint64_t>::Element *E = physics_2d_mouseover.front(); E; E = E->next()) {
  2267. Object *o = ObjectDB::get_instance(E->key());
  2268. if (o) {
  2269. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
  2270. if (co) {
  2271. if (!co->is_inside_tree()) {
  2272. to_erase.push_back(E);
  2273. continue;
  2274. }
  2275. if (p_paused_only && co->can_process()) {
  2276. continue;
  2277. }
  2278. co->_mouse_exit();
  2279. to_erase.push_back(E);
  2280. }
  2281. }
  2282. }
  2283. while (to_erase.size()) {
  2284. physics_2d_mouseover.erase(to_erase.front()->get());
  2285. to_erase.pop_front();
  2286. }
  2287. #ifndef _3D_DISABLED
  2288. if (physics_object_over) {
  2289. CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(physics_object_over));
  2290. if (co) {
  2291. if (!co->is_inside_tree()) {
  2292. physics_object_over = physics_object_capture = 0;
  2293. } else if (!(p_paused_only && co->can_process())) {
  2294. co->_mouse_exit();
  2295. physics_object_over = physics_object_capture = 0;
  2296. }
  2297. }
  2298. }
  2299. #endif
  2300. }
  2301. List<Control *>::Element *Viewport::_gui_show_modal(Control *p_control) {
  2302. List<Control *>::Element *node = gui.modal_stack.push_back(p_control);
  2303. if (gui.key_focus) {
  2304. p_control->_modal_set_prev_focus_owner(gui.key_focus->get_instance_id());
  2305. } else {
  2306. p_control->_modal_set_prev_focus_owner(0);
  2307. }
  2308. if (gui.mouse_focus && !p_control->is_a_parent_of(gui.mouse_focus) && !gui.mouse_click_grabber) {
  2309. _drop_mouse_focus();
  2310. }
  2311. return node;
  2312. }
  2313. Control *Viewport::_gui_get_focus_owner() {
  2314. return gui.key_focus;
  2315. }
  2316. void Viewport::_gui_grab_click_focus(Control *p_control) {
  2317. gui.mouse_click_grabber = p_control;
  2318. call_deferred("_post_gui_grab_click_focus");
  2319. }
  2320. void Viewport::_post_gui_grab_click_focus() {
  2321. Control *focus_grabber = gui.mouse_click_grabber;
  2322. if (!focus_grabber) {
  2323. // Redundant grab requests were made
  2324. return;
  2325. }
  2326. gui.mouse_click_grabber = nullptr;
  2327. if (gui.mouse_focus) {
  2328. if (gui.mouse_focus == focus_grabber) {
  2329. return;
  2330. }
  2331. int mask = gui.mouse_focus_mask;
  2332. Point2 click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  2333. for (int i = 0; i < 3; i++) {
  2334. if (mask & (1 << i)) {
  2335. Ref<InputEventMouseButton> mb;
  2336. mb.instance();
  2337. //send unclic
  2338. mb->set_position(click);
  2339. mb->set_button_index(i + 1);
  2340. mb->set_pressed(false);
  2341. gui.mouse_focus->call_multilevel(SceneStringNames::get_singleton()->_gui_input, mb);
  2342. }
  2343. }
  2344. gui.mouse_focus = focus_grabber;
  2345. gui.focus_inv_xform = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse();
  2346. click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  2347. for (int i = 0; i < 3; i++) {
  2348. if (mask & (1 << i)) {
  2349. Ref<InputEventMouseButton> mb;
  2350. mb.instance();
  2351. //send clic
  2352. mb->set_position(click);
  2353. mb->set_button_index(i + 1);
  2354. mb->set_pressed(true);
  2355. gui.mouse_focus->call_deferred(SceneStringNames::get_singleton()->_gui_input, mb);
  2356. }
  2357. }
  2358. }
  2359. }
  2360. ///////////////////////////////
  2361. void Viewport::input(const Ref<InputEvent> &p_event) {
  2362. ERR_FAIL_COND(!is_inside_tree());
  2363. local_input_handled = false;
  2364. if (!is_input_handled()) {
  2365. get_tree()->_call_input_pause(input_group, "_input", p_event); //not a bug, must happen before GUI, order is _input -> gui input -> _unhandled input
  2366. }
  2367. if (!is_input_handled()) {
  2368. _gui_input_event(p_event);
  2369. } else {
  2370. _gui_cleanup_internal_state(p_event);
  2371. }
  2372. //get_tree()->call_group(SceneTree::GROUP_CALL_REVERSE|SceneTree::GROUP_CALL_REALTIME|SceneTree::GROUP_CALL_MULIILEVEL,gui_input_group,"_gui_input",p_event); //special one for GUI, as controls use their own process check
  2373. }
  2374. void Viewport::unhandled_input(const Ref<InputEvent> &p_event) {
  2375. ERR_FAIL_COND(!is_inside_tree());
  2376. get_tree()->_call_input_pause(unhandled_input_group, "_unhandled_input", p_event);
  2377. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev);
  2378. if (!get_tree()->input_handled && Object::cast_to<InputEventKey>(*p_event) != nullptr) {
  2379. get_tree()->_call_input_pause(unhandled_key_input_group, "_unhandled_key_input", p_event);
  2380. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev);
  2381. }
  2382. if (physics_object_picking && !get_tree()->input_handled) {
  2383. if (Input::get_singleton()->get_mouse_mode() != Input::MOUSE_MODE_CAPTURED &&
  2384. (Object::cast_to<InputEventMouseButton>(*p_event) ||
  2385. Object::cast_to<InputEventMouseMotion>(*p_event) ||
  2386. Object::cast_to<InputEventScreenDrag>(*p_event) ||
  2387. Object::cast_to<InputEventScreenTouch>(*p_event) ||
  2388. Object::cast_to<InputEventKey>(*p_event) //to remember state
  2389. )) {
  2390. physics_picking_events.push_back(p_event);
  2391. }
  2392. }
  2393. }
  2394. void Viewport::set_use_own_world(bool p_use_own_world) {
  2395. if (p_use_own_world == own_world.is_valid()) {
  2396. return;
  2397. }
  2398. if (is_inside_tree()) {
  2399. _propagate_exit_world(this);
  2400. }
  2401. if (p_use_own_world) {
  2402. if (world.is_valid()) {
  2403. own_world = world->duplicate();
  2404. world->connect(CoreStringNames::get_singleton()->changed, this, "_own_world_changed");
  2405. } else {
  2406. own_world = Ref<World>(memnew(World));
  2407. }
  2408. } else {
  2409. own_world = Ref<World>();
  2410. if (world.is_valid()) {
  2411. world->disconnect(CoreStringNames::get_singleton()->changed, this, "_own_world_changed");
  2412. }
  2413. }
  2414. if (is_inside_tree()) {
  2415. _propagate_enter_world(this);
  2416. }
  2417. if (is_inside_tree()) {
  2418. VisualServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario());
  2419. }
  2420. _update_listener();
  2421. }
  2422. bool Viewport::is_using_own_world() const {
  2423. return own_world.is_valid();
  2424. }
  2425. void Viewport::set_attach_to_screen_rect(const Rect2 &p_rect) {
  2426. VS::get_singleton()->viewport_attach_to_screen(viewport, p_rect);
  2427. to_screen_rect = p_rect;
  2428. }
  2429. Rect2 Viewport::get_attach_to_screen_rect() const {
  2430. return to_screen_rect;
  2431. }
  2432. void Viewport::set_use_render_direct_to_screen(bool p_render_direct_to_screen) {
  2433. if (p_render_direct_to_screen == render_direct_to_screen) {
  2434. return;
  2435. }
  2436. render_direct_to_screen = p_render_direct_to_screen;
  2437. VS::get_singleton()->viewport_set_render_direct_to_screen(viewport, p_render_direct_to_screen);
  2438. }
  2439. bool Viewport::is_using_render_direct_to_screen() const {
  2440. return render_direct_to_screen;
  2441. }
  2442. void Viewport::set_physics_object_picking(bool p_enable) {
  2443. physics_object_picking = p_enable;
  2444. if (!physics_object_picking) {
  2445. physics_picking_events.clear();
  2446. }
  2447. }
  2448. bool Viewport::get_physics_object_picking() {
  2449. return physics_object_picking;
  2450. }
  2451. Vector2 Viewport::get_camera_coords(const Vector2 &p_viewport_coords) const {
  2452. Transform2D xf = get_final_transform();
  2453. return xf.xform(p_viewport_coords);
  2454. }
  2455. Vector2 Viewport::get_camera_rect_size() const {
  2456. return size;
  2457. }
  2458. bool Viewport::gui_has_modal_stack() const {
  2459. return gui.modal_stack.size();
  2460. }
  2461. void Viewport::set_disable_input(bool p_disable) {
  2462. if (p_disable == disable_input) {
  2463. return;
  2464. }
  2465. if (p_disable && GLOBAL_GET("gui/common/drop_mouse_on_gui_input_disabled")) {
  2466. _drop_mouse_focus();
  2467. _drop_mouse_over();
  2468. _gui_cancel_tooltip();
  2469. }
  2470. disable_input = p_disable;
  2471. }
  2472. bool Viewport::is_input_disabled() const {
  2473. return disable_input;
  2474. }
  2475. void Viewport::set_disable_3d(bool p_disable) {
  2476. disable_3d = p_disable;
  2477. VS::get_singleton()->viewport_set_disable_3d(viewport, p_disable);
  2478. }
  2479. bool Viewport::is_3d_disabled() const {
  2480. return disable_3d;
  2481. }
  2482. void Viewport::set_keep_3d_linear(bool p_keep_3d_linear) {
  2483. keep_3d_linear = p_keep_3d_linear;
  2484. VS::get_singleton()->viewport_set_keep_3d_linear(viewport, keep_3d_linear);
  2485. }
  2486. bool Viewport::get_keep_3d_linear() const {
  2487. return keep_3d_linear;
  2488. }
  2489. Variant Viewport::gui_get_drag_data() const {
  2490. return gui.drag_data;
  2491. }
  2492. Control *Viewport::get_modal_stack_top() const {
  2493. return gui.modal_stack.size() ? gui.modal_stack.back()->get() : NULL;
  2494. }
  2495. String Viewport::get_configuration_warning() const {
  2496. String warning = Node::get_configuration_warning();
  2497. if (size.x <= 1 || size.y <= 1) {
  2498. if (warning != String()) {
  2499. warning += "\n\n";
  2500. }
  2501. warning += TTR("The Viewport size must be greater than or equal to 2 pixels on both dimensions to render anything.");
  2502. }
  2503. if (!VisualServer::get_singleton()->is_low_end() && hdr && (usage == USAGE_2D || usage == USAGE_2D_NO_SAMPLING)) {
  2504. if (warning != String()) {
  2505. warning += "\n\n";
  2506. }
  2507. warning += TTR("This Viewport has HDR enabled, but its Usage is set to 2D or 2D No-Sampling.\nHDR is only supported in Viewports that have their Usage set to 3D or 3D No-Effects.\nHDR will be disabled for this Viewport.");
  2508. }
  2509. return warning;
  2510. }
  2511. void Viewport::gui_reset_canvas_sort_index() {
  2512. gui.canvas_sort_index = 0;
  2513. }
  2514. int Viewport::gui_get_canvas_sort_index() {
  2515. return gui.canvas_sort_index++;
  2516. }
  2517. void Viewport::set_msaa(MSAA p_msaa) {
  2518. ERR_FAIL_INDEX(p_msaa, 7);
  2519. if (msaa == p_msaa) {
  2520. return;
  2521. }
  2522. msaa = p_msaa;
  2523. VS::get_singleton()->viewport_set_msaa(viewport, VS::ViewportMSAA(p_msaa));
  2524. }
  2525. Viewport::MSAA Viewport::get_msaa() const {
  2526. return msaa;
  2527. }
  2528. void Viewport::set_use_fxaa(bool p_fxaa) {
  2529. if (p_fxaa == use_fxaa) {
  2530. return;
  2531. }
  2532. use_fxaa = p_fxaa;
  2533. VS::get_singleton()->viewport_set_use_fxaa(viewport, use_fxaa);
  2534. }
  2535. bool Viewport::get_use_fxaa() const {
  2536. return use_fxaa;
  2537. }
  2538. void Viewport::set_use_debanding(bool p_debanding) {
  2539. if (p_debanding == use_debanding) {
  2540. return;
  2541. }
  2542. use_debanding = p_debanding;
  2543. VS::get_singleton()->viewport_set_use_debanding(viewport, use_debanding);
  2544. }
  2545. bool Viewport::get_use_debanding() const {
  2546. return use_debanding;
  2547. }
  2548. void Viewport::set_sharpen_intensity(float p_intensity) {
  2549. if (p_intensity == sharpen_intensity) {
  2550. return;
  2551. }
  2552. sharpen_intensity = p_intensity;
  2553. VS::get_singleton()->viewport_set_sharpen_intensity(viewport, sharpen_intensity);
  2554. }
  2555. float Viewport::get_sharpen_intensity() const {
  2556. return sharpen_intensity;
  2557. }
  2558. void Viewport::set_hdr(bool p_hdr) {
  2559. if (hdr == p_hdr) {
  2560. return;
  2561. }
  2562. hdr = p_hdr;
  2563. VS::get_singleton()->viewport_set_hdr(viewport, p_hdr);
  2564. update_configuration_warning();
  2565. }
  2566. bool Viewport::get_hdr() const {
  2567. return hdr;
  2568. }
  2569. void Viewport::set_use_32_bpc_depth(bool p_enable) {
  2570. if (use_32_bpc_depth == p_enable) {
  2571. return;
  2572. }
  2573. use_32_bpc_depth = p_enable;
  2574. VS::get_singleton()->viewport_set_use_32_bpc_depth(viewport, p_enable);
  2575. }
  2576. bool Viewport::is_using_32_bpc_depth() const {
  2577. return use_32_bpc_depth;
  2578. }
  2579. void Viewport::set_usage(Usage p_usage) {
  2580. if (usage == p_usage) {
  2581. return;
  2582. }
  2583. usage = p_usage;
  2584. VS::get_singleton()->viewport_set_usage(viewport, VS::ViewportUsage(p_usage));
  2585. update_configuration_warning();
  2586. }
  2587. Viewport::Usage Viewport::get_usage() const {
  2588. return usage;
  2589. }
  2590. void Viewport::set_debug_draw(DebugDraw p_debug_draw) {
  2591. debug_draw = p_debug_draw;
  2592. VS::get_singleton()->viewport_set_debug_draw(viewport, VS::ViewportDebugDraw(p_debug_draw));
  2593. }
  2594. Viewport::DebugDraw Viewport::get_debug_draw() const {
  2595. return debug_draw;
  2596. }
  2597. int Viewport::get_render_info(RenderInfo p_info) {
  2598. return VS::get_singleton()->viewport_get_render_info(viewport, VS::ViewportRenderInfo(p_info));
  2599. }
  2600. void Viewport::set_snap_controls_to_pixels(bool p_enable) {
  2601. snap_controls_to_pixels = p_enable;
  2602. }
  2603. bool Viewport::is_snap_controls_to_pixels_enabled() const {
  2604. return snap_controls_to_pixels;
  2605. }
  2606. bool Viewport::gui_is_dragging() const {
  2607. return gui.dragging;
  2608. }
  2609. bool Viewport::gui_is_drag_successful() const {
  2610. return gui.drag_successful;
  2611. }
  2612. void Viewport::set_input_as_handled() {
  2613. _drop_physics_mouseover();
  2614. if (handle_input_locally) {
  2615. local_input_handled = true;
  2616. } else {
  2617. ERR_FAIL_COND(!is_inside_tree());
  2618. get_tree()->set_input_as_handled();
  2619. }
  2620. }
  2621. bool Viewport::is_input_handled() const {
  2622. if (handle_input_locally) {
  2623. return local_input_handled;
  2624. } else {
  2625. ERR_FAIL_COND_V(!is_inside_tree(), false);
  2626. return get_tree()->is_input_handled();
  2627. }
  2628. }
  2629. void Viewport::set_handle_input_locally(bool p_enable) {
  2630. handle_input_locally = p_enable;
  2631. }
  2632. bool Viewport::is_handling_input_locally() const {
  2633. return handle_input_locally;
  2634. }
  2635. void Viewport::notify_canvas_parent_child_count_reduced(const Node &p_parent) {
  2636. uint32_t id = p_parent.get_canvas_parent_id();
  2637. ERR_FAIL_COND(id == UINT32_MAX);
  2638. ERR_FAIL_UNSIGNED_INDEX(id, GUI::canvas_parents_dirty_order.size());
  2639. GUI::CanvasParent &d = GUI::canvas_parents_dirty_order[id];
  2640. // No pending children moved.
  2641. if (!d.last_child_moved_plus_one) {
  2642. return;
  2643. }
  2644. uint32_t num_children = p_parent.get_child_count();
  2645. d.last_child_moved_plus_one = MIN(d.last_child_moved_plus_one, num_children);
  2646. }
  2647. void Viewport::notify_canvas_parent_children_moved(Node &r_parent, uint32_t p_first_child, uint32_t p_last_child_plus_one) {
  2648. // First ensure the parent has a CanvasParent allocated.
  2649. uint32_t id = r_parent.get_canvas_parent_id();
  2650. if (id == UINT32_MAX) {
  2651. id = GUI::canvas_parents_dirty_order.size();
  2652. r_parent.set_canvas_parent_id(id);
  2653. GUI::canvas_parents_dirty_order.resize(id + 1);
  2654. GUI::CanvasParent &d = GUI::canvas_parents_dirty_order[id];
  2655. d.id = r_parent.get_instance_id();
  2656. d.first_child_moved = p_first_child;
  2657. d.last_child_moved_plus_one = p_last_child_plus_one;
  2658. return;
  2659. }
  2660. GUI::CanvasParent &d = GUI::canvas_parents_dirty_order[id];
  2661. DEV_CHECK_ONCE(d.id == r_parent.get_instance_id());
  2662. d.first_child_moved = MIN(d.first_child_moved, p_first_child);
  2663. d.last_child_moved_plus_one = MAX(d.last_child_moved_plus_one, p_last_child_plus_one);
  2664. }
  2665. void Viewport::flush_canvas_parents_dirty_order() {
  2666. bool canvas_layers_moved = false;
  2667. for (uint32_t n = 0; n < GUI::canvas_parents_dirty_order.size(); n++) {
  2668. GUI::CanvasParent &d = GUI::canvas_parents_dirty_order[n];
  2669. Object *obj = ObjectDB::get_instance(d.id);
  2670. if (!obj) {
  2671. // May have been deleted.
  2672. continue;
  2673. }
  2674. Node *node = static_cast<Node *>(obj);
  2675. Control *parent_control = Object::cast_to<Control>(node);
  2676. // Allow anything subscribing to this signal (skeletons etc)
  2677. // to make any changes necessary.
  2678. node->emit_signal("child_order_changed");
  2679. // Reset the id stored in the node, as this data is cleared after every flush.
  2680. node->set_canvas_parent_id(UINT32_MAX);
  2681. // This should be very rare, as the last_child_moved_plus_one is usually kept up
  2682. // to date when within the scene tree. But it may become out of date outside the scene tree.
  2683. // This will cause no problems, just a very small possibility of more notifications being sent than
  2684. // necessary in that very rare situation.
  2685. if (d.last_child_moved_plus_one > (uint32_t)node->get_child_count()) {
  2686. d.last_child_moved_plus_one = node->get_child_count();
  2687. }
  2688. bool subwindow_order_dirty = false;
  2689. bool root_order_dirty = false;
  2690. bool control_found = false;
  2691. for (uint32_t c = d.first_child_moved; c < d.last_child_moved_plus_one; c++) {
  2692. Node *child = node->get_child(c);
  2693. CanvasItem *ci = Object::cast_to<CanvasItem>(child);
  2694. if (ci) {
  2695. ci->update_draw_order();
  2696. Control *control = Object::cast_to<Control>(ci);
  2697. if (control) {
  2698. control->_query_order_update(subwindow_order_dirty, root_order_dirty);
  2699. if (parent_control && !control_found) {
  2700. control_found = true;
  2701. // In case of regressions, this could be moved to AFTER
  2702. // the children have been updated.
  2703. parent_control->update();
  2704. }
  2705. control->update();
  2706. }
  2707. continue;
  2708. }
  2709. CanvasLayer *cl = Object::cast_to<CanvasLayer>(child);
  2710. if (cl) {
  2711. // If any canvas layers moved, we need to do an expensive update,
  2712. // so we ensure doing this only once.
  2713. if (!canvas_layers_moved) {
  2714. canvas_layers_moved = true;
  2715. cl->update_draw_order();
  2716. }
  2717. }
  2718. }
  2719. Viewport *viewport = node->get_viewport();
  2720. if (viewport) {
  2721. if (subwindow_order_dirty) {
  2722. viewport->_gui_set_subwindow_order_dirty();
  2723. }
  2724. if (root_order_dirty) {
  2725. viewport->_gui_set_root_order_dirty();
  2726. }
  2727. }
  2728. node->notification(Node::NOTIFICATION_CHILD_ORDER_CHANGED);
  2729. }
  2730. GUI::canvas_parents_dirty_order.clear();
  2731. }
  2732. void Viewport::_validate_property(PropertyInfo &property) const {
  2733. if (VisualServer::get_singleton()->is_low_end() && (property.name == "hdr" || property.name == "use_32_bpc_depth" || property.name == "debanding" || property.name == "sharpen_intensity" || property.name == "debug_draw")) {
  2734. // Only available in GLES3.
  2735. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  2736. }
  2737. if (!VisualServer::get_singleton()->is_low_end() && (property.name == "render_direct_to_screen")) {
  2738. // Only available in GLES2.
  2739. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  2740. }
  2741. }
  2742. void Viewport::_bind_methods() {
  2743. ClassDB::bind_method(D_METHOD("set_use_arvr", "use"), &Viewport::set_use_arvr);
  2744. ClassDB::bind_method(D_METHOD("use_arvr"), &Viewport::use_arvr);
  2745. ClassDB::bind_method(D_METHOD("set_size", "size"), &Viewport::set_size);
  2746. ClassDB::bind_method(D_METHOD("get_size"), &Viewport::get_size);
  2747. ClassDB::bind_method(D_METHOD("set_world_2d", "world_2d"), &Viewport::set_world_2d);
  2748. ClassDB::bind_method(D_METHOD("get_world_2d"), &Viewport::get_world_2d);
  2749. ClassDB::bind_method(D_METHOD("find_world_2d"), &Viewport::find_world_2d);
  2750. ClassDB::bind_method(D_METHOD("set_world", "world"), &Viewport::set_world);
  2751. ClassDB::bind_method(D_METHOD("get_world"), &Viewport::get_world);
  2752. ClassDB::bind_method(D_METHOD("find_world"), &Viewport::find_world);
  2753. ClassDB::bind_method(D_METHOD("set_canvas_transform", "xform"), &Viewport::set_canvas_transform);
  2754. ClassDB::bind_method(D_METHOD("get_canvas_transform"), &Viewport::get_canvas_transform);
  2755. ClassDB::bind_method(D_METHOD("set_global_canvas_transform", "xform"), &Viewport::set_global_canvas_transform);
  2756. ClassDB::bind_method(D_METHOD("get_global_canvas_transform"), &Viewport::get_global_canvas_transform);
  2757. ClassDB::bind_method(D_METHOD("get_final_transform"), &Viewport::get_final_transform);
  2758. ClassDB::bind_method(D_METHOD("get_visible_rect"), &Viewport::get_visible_rect);
  2759. ClassDB::bind_method(D_METHOD("set_transparent_background", "enable"), &Viewport::set_transparent_background);
  2760. ClassDB::bind_method(D_METHOD("has_transparent_background"), &Viewport::has_transparent_background);
  2761. ClassDB::bind_method(D_METHOD("_vp_input"), &Viewport::_vp_input);
  2762. ClassDB::bind_method(D_METHOD("_vp_input_text", "text"), &Viewport::_vp_input_text);
  2763. ClassDB::bind_method(D_METHOD("_vp_unhandled_input"), &Viewport::_vp_unhandled_input);
  2764. ClassDB::bind_method(D_METHOD("set_size_override", "enable", "size", "margin"), &Viewport::set_size_override, DEFVAL(Size2(-1, -1)), DEFVAL(Size2(0, 0)));
  2765. ClassDB::bind_method(D_METHOD("get_size_override"), &Viewport::get_size_override);
  2766. ClassDB::bind_method(D_METHOD("is_size_override_enabled"), &Viewport::is_size_override_enabled);
  2767. ClassDB::bind_method(D_METHOD("set_size_override_stretch", "enabled"), &Viewport::set_size_override_stretch);
  2768. ClassDB::bind_method(D_METHOD("is_size_override_stretch_enabled"), &Viewport::is_size_override_stretch_enabled);
  2769. ClassDB::bind_method(D_METHOD("set_vflip", "enable"), &Viewport::set_vflip);
  2770. ClassDB::bind_method(D_METHOD("get_vflip"), &Viewport::get_vflip);
  2771. ClassDB::bind_method(D_METHOD("set_clear_mode", "mode"), &Viewport::set_clear_mode);
  2772. ClassDB::bind_method(D_METHOD("get_clear_mode"), &Viewport::get_clear_mode);
  2773. ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &Viewport::set_update_mode);
  2774. ClassDB::bind_method(D_METHOD("get_update_mode"), &Viewport::get_update_mode);
  2775. ClassDB::bind_method(D_METHOD("set_msaa", "msaa"), &Viewport::set_msaa);
  2776. ClassDB::bind_method(D_METHOD("get_msaa"), &Viewport::get_msaa);
  2777. ClassDB::bind_method(D_METHOD("set_use_fxaa", "enable"), &Viewport::set_use_fxaa);
  2778. ClassDB::bind_method(D_METHOD("get_use_fxaa"), &Viewport::get_use_fxaa);
  2779. ClassDB::bind_method(D_METHOD("set_use_debanding", "enable"), &Viewport::set_use_debanding);
  2780. ClassDB::bind_method(D_METHOD("get_use_debanding"), &Viewport::get_use_debanding);
  2781. ClassDB::bind_method(D_METHOD("set_sharpen_intensity", "intensity"), &Viewport::set_sharpen_intensity);
  2782. ClassDB::bind_method(D_METHOD("get_sharpen_intensity"), &Viewport::get_sharpen_intensity);
  2783. ClassDB::bind_method(D_METHOD("set_hdr", "enable"), &Viewport::set_hdr);
  2784. ClassDB::bind_method(D_METHOD("get_hdr"), &Viewport::get_hdr);
  2785. ClassDB::bind_method(D_METHOD("set_use_32_bpc_depth", "enable"), &Viewport::set_use_32_bpc_depth);
  2786. ClassDB::bind_method(D_METHOD("get_use_32_bpc_depth"), &Viewport::is_using_32_bpc_depth);
  2787. ClassDB::bind_method(D_METHOD("set_usage", "usage"), &Viewport::set_usage);
  2788. ClassDB::bind_method(D_METHOD("get_usage"), &Viewport::get_usage);
  2789. ClassDB::bind_method(D_METHOD("set_debug_draw", "debug_draw"), &Viewport::set_debug_draw);
  2790. ClassDB::bind_method(D_METHOD("get_debug_draw"), &Viewport::get_debug_draw);
  2791. ClassDB::bind_method(D_METHOD("get_render_info", "info"), &Viewport::get_render_info);
  2792. ClassDB::bind_method(D_METHOD("get_texture"), &Viewport::get_texture);
  2793. ClassDB::bind_method(D_METHOD("set_physics_object_picking", "enable"), &Viewport::set_physics_object_picking);
  2794. ClassDB::bind_method(D_METHOD("get_physics_object_picking"), &Viewport::get_physics_object_picking);
  2795. ClassDB::bind_method(D_METHOD("get_viewport_rid"), &Viewport::get_viewport_rid);
  2796. ClassDB::bind_method(D_METHOD("input", "local_event"), &Viewport::input);
  2797. ClassDB::bind_method(D_METHOD("unhandled_input", "local_event"), &Viewport::unhandled_input);
  2798. ClassDB::bind_method(D_METHOD("update_worlds"), &Viewport::update_worlds);
  2799. ClassDB::bind_method(D_METHOD("set_use_own_world", "enable"), &Viewport::set_use_own_world);
  2800. ClassDB::bind_method(D_METHOD("is_using_own_world"), &Viewport::is_using_own_world);
  2801. ClassDB::bind_method(D_METHOD("get_camera"), &Viewport::get_camera);
  2802. ClassDB::bind_method(D_METHOD("set_as_audio_listener", "enable"), &Viewport::set_as_audio_listener);
  2803. ClassDB::bind_method(D_METHOD("is_audio_listener"), &Viewport::is_audio_listener);
  2804. ClassDB::bind_method(D_METHOD("set_as_audio_listener_2d", "enable"), &Viewport::set_as_audio_listener_2d);
  2805. ClassDB::bind_method(D_METHOD("is_audio_listener_2d"), &Viewport::is_audio_listener_2d);
  2806. ClassDB::bind_method(D_METHOD("set_attach_to_screen_rect", "rect"), &Viewport::set_attach_to_screen_rect);
  2807. ClassDB::bind_method(D_METHOD("set_use_render_direct_to_screen", "enable"), &Viewport::set_use_render_direct_to_screen);
  2808. ClassDB::bind_method(D_METHOD("is_using_render_direct_to_screen"), &Viewport::is_using_render_direct_to_screen);
  2809. ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position);
  2810. ClassDB::bind_method(D_METHOD("warp_mouse", "to_position"), &Viewport::warp_mouse);
  2811. ClassDB::bind_method(D_METHOD("gui_has_modal_stack"), &Viewport::gui_has_modal_stack);
  2812. ClassDB::bind_method(D_METHOD("gui_get_drag_data"), &Viewport::gui_get_drag_data);
  2813. ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging);
  2814. ClassDB::bind_method(D_METHOD("gui_is_drag_successful"), &Viewport::gui_is_drag_successful);
  2815. ClassDB::bind_method(D_METHOD("get_modal_stack_top"), &Viewport::get_modal_stack_top);
  2816. ClassDB::bind_method(D_METHOD("set_disable_input", "disable"), &Viewport::set_disable_input);
  2817. ClassDB::bind_method(D_METHOD("is_input_disabled"), &Viewport::is_input_disabled);
  2818. ClassDB::bind_method(D_METHOD("set_disable_3d", "disable"), &Viewport::set_disable_3d);
  2819. ClassDB::bind_method(D_METHOD("is_3d_disabled"), &Viewport::is_3d_disabled);
  2820. ClassDB::bind_method(D_METHOD("set_keep_3d_linear", "keep_3d_linear"), &Viewport::set_keep_3d_linear);
  2821. ClassDB::bind_method(D_METHOD("get_keep_3d_linear"), &Viewport::get_keep_3d_linear);
  2822. ClassDB::bind_method(D_METHOD("_gui_show_tooltip"), &Viewport::_gui_show_tooltip);
  2823. ClassDB::bind_method(D_METHOD("_gui_remove_focus"), &Viewport::_gui_remove_focus);
  2824. ClassDB::bind_method(D_METHOD("_post_gui_grab_click_focus"), &Viewport::_post_gui_grab_click_focus);
  2825. ClassDB::bind_method(D_METHOD("set_shadow_atlas_size", "size"), &Viewport::set_shadow_atlas_size);
  2826. ClassDB::bind_method(D_METHOD("get_shadow_atlas_size"), &Viewport::get_shadow_atlas_size);
  2827. ClassDB::bind_method(D_METHOD("set_snap_controls_to_pixels", "enabled"), &Viewport::set_snap_controls_to_pixels);
  2828. ClassDB::bind_method(D_METHOD("is_snap_controls_to_pixels_enabled"), &Viewport::is_snap_controls_to_pixels_enabled);
  2829. ClassDB::bind_method(D_METHOD("set_shadow_atlas_quadrant_subdiv", "quadrant", "subdiv"), &Viewport::set_shadow_atlas_quadrant_subdiv);
  2830. ClassDB::bind_method(D_METHOD("get_shadow_atlas_quadrant_subdiv", "quadrant"), &Viewport::get_shadow_atlas_quadrant_subdiv);
  2831. ClassDB::bind_method(D_METHOD("set_input_as_handled"), &Viewport::set_input_as_handled);
  2832. ClassDB::bind_method(D_METHOD("is_input_handled"), &Viewport::is_input_handled);
  2833. ClassDB::bind_method(D_METHOD("set_handle_input_locally", "enable"), &Viewport::set_handle_input_locally);
  2834. ClassDB::bind_method(D_METHOD("is_handling_input_locally"), &Viewport::is_handling_input_locally);
  2835. ClassDB::bind_method(D_METHOD("_subwindow_visibility_changed"), &Viewport::_subwindow_visibility_changed);
  2836. ClassDB::bind_method(D_METHOD("_own_world_changed"), &Viewport::_own_world_changed);
  2837. ClassDB::bind_method(D_METHOD("_process_picking", "ignore_paused"), &Viewport::_process_picking);
  2838. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arvr"), "set_use_arvr", "use_arvr");
  2839. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  2840. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "size_override_stretch"), "set_size_override_stretch", "is_size_override_stretch_enabled");
  2841. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world"), "set_use_own_world", "is_using_own_world");
  2842. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world", PROPERTY_HINT_RESOURCE_TYPE, "World"), "set_world", "get_world");
  2843. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", 0), "set_world_2d", "get_world_2d");
  2844. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background");
  2845. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally");
  2846. ADD_GROUP("Rendering", "");
  2847. ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"), "set_msaa", "get_msaa");
  2848. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fxaa"), "set_use_fxaa", "get_use_fxaa");
  2849. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debanding"), "set_use_debanding", "get_use_debanding");
  2850. ADD_PROPERTY(PropertyInfo(Variant::REAL, "sharpen_intensity", PROPERTY_HINT_RANGE, "0,1"), "set_sharpen_intensity", "get_sharpen_intensity");
  2851. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hdr"), "set_hdr", "get_hdr");
  2852. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_32_bpc_depth"), "set_use_32_bpc_depth", "get_use_32_bpc_depth");
  2853. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_3d"), "set_disable_3d", "is_3d_disabled");
  2854. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_3d_linear"), "set_keep_3d_linear", "get_keep_3d_linear");
  2855. ADD_PROPERTY(PropertyInfo(Variant::INT, "usage", PROPERTY_HINT_ENUM, "2D,2D Without Sampling,3D,3D Without Effects"), "set_usage", "get_usage");
  2856. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "render_direct_to_screen"), "set_use_render_direct_to_screen", "is_using_render_direct_to_screen");
  2857. ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Overdraw,Wireframe"), "set_debug_draw", "get_debug_draw");
  2858. ADD_GROUP("Render Target", "render_target_");
  2859. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "render_target_v_flip"), "set_vflip", "get_vflip");
  2860. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_clear_mode", PROPERTY_HINT_ENUM, "Always,Never,Next Frame"), "set_clear_mode", "get_clear_mode");
  2861. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_update_mode", PROPERTY_HINT_ENUM, "Disabled,Once,When Visible,Always"), "set_update_mode", "get_update_mode");
  2862. ADD_GROUP("Audio Listener", "audio_listener_");
  2863. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_2d"), "set_as_audio_listener_2d", "is_audio_listener_2d");
  2864. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_3d"), "set_as_audio_listener", "is_audio_listener");
  2865. ADD_GROUP("Physics", "physics_");
  2866. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking"), "set_physics_object_picking", "get_physics_object_picking");
  2867. ADD_GROUP("GUI", "gui_");
  2868. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_disable_input"), "set_disable_input", "is_input_disabled");
  2869. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_snap_controls_to_pixels"), "set_snap_controls_to_pixels", "is_snap_controls_to_pixels_enabled");
  2870. ADD_GROUP("Shadow Atlas", "shadow_atlas_");
  2871. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_atlas_size", PROPERTY_HINT_RANGE, "0,16384,256"), "set_shadow_atlas_size", "get_shadow_atlas_size");
  2872. 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);
  2873. 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);
  2874. 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);
  2875. 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);
  2876. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "canvas_transform", PROPERTY_HINT_NONE, "", 0), "set_canvas_transform", "get_canvas_transform");
  2877. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_canvas_transform", PROPERTY_HINT_NONE, "", 0), "set_global_canvas_transform", "get_global_canvas_transform");
  2878. ADD_SIGNAL(MethodInfo("size_changed"));
  2879. ADD_SIGNAL(MethodInfo("gui_focus_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
  2880. BIND_ENUM_CONSTANT(UPDATE_DISABLED);
  2881. BIND_ENUM_CONSTANT(UPDATE_ONCE);
  2882. BIND_ENUM_CONSTANT(UPDATE_WHEN_VISIBLE);
  2883. BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
  2884. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
  2885. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1);
  2886. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  2887. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_16);
  2888. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_64);
  2889. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_256);
  2890. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1024);
  2891. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
  2892. BIND_ENUM_CONSTANT(RENDER_INFO_OBJECTS_IN_FRAME);
  2893. BIND_ENUM_CONSTANT(RENDER_INFO_VERTICES_IN_FRAME);
  2894. BIND_ENUM_CONSTANT(RENDER_INFO_MATERIAL_CHANGES_IN_FRAME);
  2895. BIND_ENUM_CONSTANT(RENDER_INFO_SHADER_CHANGES_IN_FRAME);
  2896. BIND_ENUM_CONSTANT(RENDER_INFO_SURFACE_CHANGES_IN_FRAME);
  2897. BIND_ENUM_CONSTANT(RENDER_INFO_DRAW_CALLS_IN_FRAME);
  2898. BIND_ENUM_CONSTANT(RENDER_INFO_2D_ITEMS_IN_FRAME);
  2899. BIND_ENUM_CONSTANT(RENDER_INFO_2D_DRAW_CALLS_IN_FRAME);
  2900. BIND_ENUM_CONSTANT(RENDER_INFO_MAX);
  2901. BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLED);
  2902. BIND_ENUM_CONSTANT(DEBUG_DRAW_UNSHADED);
  2903. BIND_ENUM_CONSTANT(DEBUG_DRAW_OVERDRAW);
  2904. BIND_ENUM_CONSTANT(DEBUG_DRAW_WIREFRAME);
  2905. BIND_ENUM_CONSTANT(MSAA_DISABLED);
  2906. BIND_ENUM_CONSTANT(MSAA_2X);
  2907. BIND_ENUM_CONSTANT(MSAA_4X);
  2908. BIND_ENUM_CONSTANT(MSAA_8X);
  2909. BIND_ENUM_CONSTANT(MSAA_16X);
  2910. BIND_ENUM_CONSTANT(USAGE_2D);
  2911. BIND_ENUM_CONSTANT(USAGE_2D_NO_SAMPLING);
  2912. BIND_ENUM_CONSTANT(USAGE_3D);
  2913. BIND_ENUM_CONSTANT(USAGE_3D_NO_EFFECTS);
  2914. BIND_ENUM_CONSTANT(CLEAR_MODE_ALWAYS);
  2915. BIND_ENUM_CONSTANT(CLEAR_MODE_NEVER);
  2916. BIND_ENUM_CONSTANT(CLEAR_MODE_ONLY_NEXT_FRAME);
  2917. }
  2918. void Viewport::_subwindow_visibility_changed() {
  2919. // unfortunately, we don't know the sender, i.e. which subwindow changed;
  2920. // so we have to check them all.
  2921. gui.subwindow_visibility_dirty = true;
  2922. }
  2923. Viewport::Viewport() {
  2924. world_2d = Ref<World2D>(memnew(World2D));
  2925. viewport = RID_PRIME(VisualServer::get_singleton()->viewport_create());
  2926. texture_rid = VisualServer::get_singleton()->viewport_get_texture(viewport);
  2927. texture_flags = 0;
  2928. render_direct_to_screen = false;
  2929. default_texture.instance();
  2930. default_texture->vp = const_cast<Viewport *>(this);
  2931. viewport_textures.insert(default_texture.ptr());
  2932. VS::get_singleton()->texture_set_proxy(default_texture->proxy, texture_rid);
  2933. //internal_listener = SpatialSoundServer::get_singleton()->listener_create();
  2934. audio_listener = false;
  2935. //internal_listener_2d = SpatialSound2DServer::get_singleton()->listener_create();
  2936. audio_listener_2d = false;
  2937. transparent_bg = false;
  2938. parent = nullptr;
  2939. listener = nullptr;
  2940. camera = nullptr;
  2941. override_canvas_transform = false;
  2942. canvas_layers.insert(NULL); // This eases picking code (interpreted as the canvas of the Viewport)
  2943. arvr = false;
  2944. size_override = false;
  2945. size_override_stretch = false;
  2946. size_override_size = Size2(1, 1);
  2947. gen_mipmaps = false;
  2948. vflip = false;
  2949. //clear=true;
  2950. update_mode = UPDATE_WHEN_VISIBLE;
  2951. physics_object_picking = false;
  2952. physics_object_capture = 0;
  2953. physics_object_over = 0;
  2954. physics_has_last_mousepos = false;
  2955. physics_last_mousepos = Vector2(Math_INF, Math_INF);
  2956. shadow_atlas_size = 0;
  2957. for (int i = 0; i < 4; i++) {
  2958. shadow_atlas_quadrant_subdiv[i] = SHADOW_ATLAS_QUADRANT_SUBDIV_MAX;
  2959. }
  2960. set_shadow_atlas_quadrant_subdiv(0, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  2961. set_shadow_atlas_quadrant_subdiv(1, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  2962. set_shadow_atlas_quadrant_subdiv(2, SHADOW_ATLAS_QUADRANT_SUBDIV_16);
  2963. set_shadow_atlas_quadrant_subdiv(3, SHADOW_ATLAS_QUADRANT_SUBDIV_64);
  2964. String id = itos(get_instance_id());
  2965. input_group = "_vp_input" + id;
  2966. gui_input_group = "_vp_gui_input" + id;
  2967. unhandled_input_group = "_vp_unhandled_input" + id;
  2968. unhandled_key_input_group = "_vp_unhandled_key_input" + id;
  2969. disable_input = false;
  2970. disable_3d = false;
  2971. keep_3d_linear = false;
  2972. // Window tooltip.
  2973. gui.tooltip_timer = -1;
  2974. gui.tooltip_delay = GLOBAL_DEF("gui/timers/tooltip_delay_sec", 0.5);
  2975. ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/tooltip_delay_sec", PropertyInfo(Variant::REAL, "gui/timers/tooltip_delay_sec", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater")); // No negative numbers
  2976. gui.tooltip_control = nullptr;
  2977. gui.tooltip_label = nullptr;
  2978. gui.drag_preview_id = 0;
  2979. gui.drag_attempted = false;
  2980. gui.canvas_sort_index = 0;
  2981. gui.roots_order_dirty = false;
  2982. gui.mouse_focus = nullptr;
  2983. gui.last_mouse_focus = nullptr;
  2984. msaa = MSAA_DISABLED;
  2985. use_fxaa = false;
  2986. use_debanding = false;
  2987. sharpen_intensity = 0.0;
  2988. hdr = true;
  2989. use_32_bpc_depth = false;
  2990. usage = USAGE_3D;
  2991. debug_draw = DEBUG_DRAW_DISABLED;
  2992. clear_mode = CLEAR_MODE_ALWAYS;
  2993. snap_controls_to_pixels = true;
  2994. physics_last_mouse_state.alt = false;
  2995. physics_last_mouse_state.control = false;
  2996. physics_last_mouse_state.shift = false;
  2997. physics_last_mouse_state.meta = false;
  2998. physics_last_mouse_state.mouse_mask = 0;
  2999. local_input_handled = false;
  3000. handle_input_locally = true;
  3001. physics_last_id = 0; //ensures first time there will be a check
  3002. // Physics interpolation mode for viewports is a special case.
  3003. // Typically viewports will be housed within Controls,
  3004. // and Controls default to PHYSICS_INTERPOLATION_MODE_OFF.
  3005. // Viewports can thus inherit physics interpolation OFF, which is unexpected.
  3006. // Setting to ON allows each viewport to have a fresh interpolation state.
  3007. set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_ON);
  3008. }
  3009. Viewport::~Viewport() {
  3010. //erase itself from viewport textures
  3011. for (Set<ViewportTexture *>::Element *E = viewport_textures.front(); E; E = E->next()) {
  3012. E->get()->vp = nullptr;
  3013. }
  3014. VisualServer::get_singleton()->free(viewport);
  3015. //SpatialSoundServer::get_singleton()->free(internal_listener);
  3016. //SpatialSound2DServer::get_singleton()->free(internal_listener_2d);
  3017. }