viewport.cpp 108 KB

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