viewport.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234
  1. /*************************************************************************/
  2. /* viewport.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "viewport.h"
  31. #include "core/os/input.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #include "scene/2d/collision_object_2d.h"
  35. #include "scene/3d/camera.h"
  36. #include "scene/3d/collision_object.h"
  37. #include "scene/3d/listener.h"
  38. #include "scene/3d/spatial.h"
  39. #include "scene/3d/world_environment.h"
  40. #include "scene/gui/control.h"
  41. #include "scene/gui/label.h"
  42. #include "scene/gui/menu_button.h"
  43. #include "scene/gui/panel.h"
  44. #include "scene/gui/panel_container.h"
  45. #include "scene/gui/popup_menu.h"
  46. #include "scene/main/canvas_layer.h"
  47. #include "scene/main/timer.h"
  48. #include "scene/resources/mesh.h"
  49. #include "scene/scene_string_names.h"
  50. #include "servers/physics_2d_server.h"
  51. void ViewportTexture::setup_local_to_scene() {
  52. if (vp) {
  53. vp->viewport_textures.erase(this);
  54. }
  55. vp = NULL;
  56. Node *local_scene = get_local_scene();
  57. if (!local_scene) {
  58. return;
  59. }
  60. Node *vpn = local_scene->get_node(path);
  61. ERR_EXPLAIN("ViewportTexture: Path to node is invalid");
  62. ERR_FAIL_COND(!vpn);
  63. vp = Object::cast_to<Viewport>(vpn);
  64. ERR_EXPLAIN("ViewportTexture: Path to node does not point to a viewport");
  65. ERR_FAIL_COND(!vp);
  66. vp->viewport_textures.insert(this);
  67. VS::get_singleton()->texture_set_proxy(proxy, vp->texture_rid);
  68. vp->texture_flags = flags;
  69. VS::get_singleton()->texture_set_flags(vp->texture_rid, flags);
  70. }
  71. void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) {
  72. if (path == p_path)
  73. return;
  74. path = p_path;
  75. if (get_local_scene()) {
  76. setup_local_to_scene();
  77. }
  78. }
  79. NodePath ViewportTexture::get_viewport_path_in_scene() const {
  80. return path;
  81. }
  82. int ViewportTexture::get_width() const {
  83. ERR_FAIL_COND_V(!vp, 0);
  84. return vp->size.width;
  85. }
  86. int ViewportTexture::get_height() const {
  87. ERR_FAIL_COND_V(!vp, 0);
  88. return vp->size.height;
  89. }
  90. Size2 ViewportTexture::get_size() const {
  91. ERR_FAIL_COND_V(!vp, Size2());
  92. return vp->size;
  93. }
  94. RID ViewportTexture::get_rid() const {
  95. //ERR_FAIL_COND_V(!vp, RID());
  96. return proxy;
  97. }
  98. bool ViewportTexture::has_alpha() const {
  99. return false;
  100. }
  101. Ref<Image> ViewportTexture::get_data() const {
  102. ERR_FAIL_COND_V(!vp, Ref<Image>());
  103. return VS::get_singleton()->texture_get_data(vp->texture_rid);
  104. }
  105. void ViewportTexture::set_flags(uint32_t p_flags) {
  106. flags = p_flags;
  107. if (!vp)
  108. return;
  109. vp->texture_flags = flags;
  110. VS::get_singleton()->texture_set_flags(vp->texture_rid, flags);
  111. }
  112. uint32_t ViewportTexture::get_flags() const {
  113. return flags;
  114. }
  115. void ViewportTexture::_bind_methods() {
  116. ClassDB::bind_method(D_METHOD("set_viewport_path_in_scene", "path"), &ViewportTexture::set_viewport_path_in_scene);
  117. ClassDB::bind_method(D_METHOD("get_viewport_path_in_scene"), &ViewportTexture::get_viewport_path_in_scene);
  118. 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");
  119. }
  120. ViewportTexture::ViewportTexture() {
  121. vp = NULL;
  122. flags = 0;
  123. set_local_to_scene(true);
  124. proxy = VS::get_singleton()->texture_create();
  125. }
  126. ViewportTexture::~ViewportTexture() {
  127. if (vp) {
  128. vp->viewport_textures.erase(this);
  129. }
  130. VS::get_singleton()->free(proxy);
  131. }
  132. /////////////////////////////////////
  133. class TooltipPanel : public PanelContainer {
  134. GDCLASS(TooltipPanel, PanelContainer)
  135. public:
  136. TooltipPanel(){};
  137. };
  138. class TooltipLabel : public Label {
  139. GDCLASS(TooltipLabel, Label)
  140. public:
  141. TooltipLabel(){};
  142. };
  143. Viewport::GUI::GUI() {
  144. dragging = false;
  145. mouse_focus = NULL;
  146. mouse_click_grabber = NULL;
  147. mouse_focus_mask = 0;
  148. key_focus = NULL;
  149. mouse_over = NULL;
  150. tooltip = NULL;
  151. tooltip_popup = NULL;
  152. tooltip_label = NULL;
  153. subwindow_visibility_dirty = false;
  154. subwindow_order_dirty = false;
  155. }
  156. /////////////////////////////////////
  157. void Viewport::_update_stretch_transform() {
  158. if (size_override_stretch && size_override) {
  159. stretch_transform = Transform2D();
  160. Size2 scale = size / (size_override_size + size_override_margin * 2);
  161. stretch_transform.scale(scale);
  162. stretch_transform.elements[2] = size_override_margin * scale;
  163. } else {
  164. stretch_transform = Transform2D();
  165. }
  166. _update_global_transform();
  167. }
  168. void Viewport::update_worlds() {
  169. if (!is_inside_tree())
  170. return;
  171. Rect2 abstracted_rect = Rect2(Vector2(), get_visible_rect().size);
  172. Rect2 xformed_rect = (global_canvas_transform * canvas_transform).affine_inverse().xform(abstracted_rect);
  173. find_world_2d()->_update_viewport(this, xformed_rect);
  174. find_world_2d()->_update();
  175. find_world()->_update(get_tree()->get_frame());
  176. }
  177. 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) {
  178. Transform object_transform = p_object->get_global_transform();
  179. Transform camera_transform = p_camera->get_global_transform();
  180. ObjectID id = p_object->get_instance_id();
  181. //avoid sending the fake event unnecessarily if nothing really changed in the context
  182. if (object_transform == physics_last_object_transform && camera_transform == physics_last_camera_transform && physics_last_id == id) {
  183. Ref<InputEventMouseMotion> mm = p_input_event;
  184. if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
  185. return; //discarded
  186. }
  187. }
  188. p_object->_input_event(camera, p_input_event, p_pos, p_normal, p_shape);
  189. physics_last_object_transform = object_transform;
  190. physics_last_camera_transform = camera_transform;
  191. physics_last_id = id;
  192. }
  193. void Viewport::_notification(int p_what) {
  194. switch (p_what) {
  195. case NOTIFICATION_ENTER_TREE: {
  196. if (get_parent()) {
  197. parent = get_parent()->get_viewport();
  198. VisualServer::get_singleton()->viewport_set_parent_viewport(viewport, parent->get_viewport_rid());
  199. } else {
  200. parent = NULL;
  201. }
  202. current_canvas = find_world_2d()->get_canvas();
  203. VisualServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario());
  204. VisualServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
  205. _update_listener();
  206. _update_listener_2d();
  207. find_world_2d()->_register_viewport(this, Rect2());
  208. add_to_group("_viewports");
  209. if (get_tree()->is_debugging_collisions_hint()) {
  210. //2D
  211. Physics2DServer::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(), get_tree()->get_collision_debug_contact_count());
  212. contact_2d_debug = VisualServer::get_singleton()->canvas_item_create();
  213. VisualServer::get_singleton()->canvas_item_set_parent(contact_2d_debug, find_world_2d()->get_canvas());
  214. //3D
  215. PhysicsServer::get_singleton()->space_set_debug_contacts(find_world()->get_space(), get_tree()->get_collision_debug_contact_count());
  216. contact_3d_debug_multimesh = VisualServer::get_singleton()->multimesh_create();
  217. VisualServer::get_singleton()->multimesh_allocate(contact_3d_debug_multimesh, get_tree()->get_collision_debug_contact_count(), VS::MULTIMESH_TRANSFORM_3D, VS::MULTIMESH_COLOR_8BIT);
  218. VisualServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, 0);
  219. VisualServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh, get_tree()->get_debug_contact_mesh()->get_rid());
  220. contact_3d_debug_instance = VisualServer::get_singleton()->instance_create();
  221. VisualServer::get_singleton()->instance_set_base(contact_3d_debug_instance, contact_3d_debug_multimesh);
  222. VisualServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance, find_world()->get_scenario());
  223. //VisualServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance, VS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS, true);
  224. }
  225. VS::get_singleton()->viewport_set_active(viewport, true);
  226. } break;
  227. case NOTIFICATION_READY: {
  228. #ifndef _3D_DISABLED
  229. if (listeners.size() && !listener) {
  230. Listener *first = NULL;
  231. for (Set<Listener *>::Element *E = listeners.front(); E; E = E->next()) {
  232. if (first == NULL || first->is_greater_than(E->get())) {
  233. first = E->get();
  234. }
  235. }
  236. if (first)
  237. first->make_current();
  238. }
  239. if (cameras.size() && !camera) {
  240. //there are cameras but no current camera, pick first in tree and make it current
  241. Camera *first = NULL;
  242. for (Set<Camera *>::Element *E = cameras.front(); E; E = E->next()) {
  243. if (first == NULL || first->is_greater_than(E->get())) {
  244. first = E->get();
  245. }
  246. }
  247. if (first)
  248. first->make_current();
  249. }
  250. #endif
  251. // Enable processing for tooltips, collision debugging, physics object picking, etc.
  252. set_process_internal(true);
  253. set_physics_process_internal(true);
  254. } break;
  255. case NOTIFICATION_EXIT_TREE: {
  256. _gui_cancel_tooltip();
  257. if (world_2d.is_valid())
  258. world_2d->_remove_viewport(this);
  259. VisualServer::get_singleton()->viewport_set_scenario(viewport, RID());
  260. // SpatialSoundServer::get_singleton()->listener_set_space(internal_listener, RID());
  261. VisualServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
  262. if (contact_2d_debug.is_valid()) {
  263. VisualServer::get_singleton()->free(contact_2d_debug);
  264. contact_2d_debug = RID();
  265. }
  266. if (contact_3d_debug_multimesh.is_valid()) {
  267. VisualServer::get_singleton()->free(contact_3d_debug_multimesh);
  268. VisualServer::get_singleton()->free(contact_3d_debug_instance);
  269. contact_3d_debug_instance = RID();
  270. contact_3d_debug_multimesh = RID();
  271. }
  272. remove_from_group("_viewports");
  273. VS::get_singleton()->viewport_set_active(viewport, false);
  274. } break;
  275. case NOTIFICATION_INTERNAL_PROCESS: {
  276. if (gui.tooltip_timer >= 0) {
  277. gui.tooltip_timer -= get_process_delta_time();
  278. if (gui.tooltip_timer < 0) {
  279. _gui_show_tooltip();
  280. }
  281. }
  282. } break;
  283. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  284. if (get_tree()->is_debugging_collisions_hint() && contact_2d_debug.is_valid()) {
  285. VisualServer::get_singleton()->canvas_item_clear(contact_2d_debug);
  286. VisualServer::get_singleton()->canvas_item_set_draw_index(contact_2d_debug, 0xFFFFF); //very high index
  287. Vector<Vector2> points = Physics2DServer::get_singleton()->space_get_contacts(find_world_2d()->get_space());
  288. int point_count = Physics2DServer::get_singleton()->space_get_contact_count(find_world_2d()->get_space());
  289. Color ccol = get_tree()->get_debug_collision_contact_color();
  290. for (int i = 0; i < point_count; i++) {
  291. VisualServer::get_singleton()->canvas_item_add_rect(contact_2d_debug, Rect2(points[i] - Vector2(2, 2), Vector2(5, 5)), ccol);
  292. }
  293. }
  294. if (get_tree()->is_debugging_collisions_hint() && contact_3d_debug_multimesh.is_valid()) {
  295. Vector<Vector3> points = PhysicsServer::get_singleton()->space_get_contacts(find_world()->get_space());
  296. int point_count = PhysicsServer::get_singleton()->space_get_contact_count(find_world()->get_space());
  297. VS::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh, point_count);
  298. }
  299. if (physics_object_picking && (to_screen_rect == Rect2() || Input::get_singleton()->get_mouse_mode() != Input::MOUSE_MODE_CAPTURED)) {
  300. #ifndef _3D_DISABLED
  301. Vector2 last_pos(1e20, 1e20);
  302. CollisionObject *last_object = NULL;
  303. ObjectID last_id = 0;
  304. #endif
  305. PhysicsDirectSpaceState::RayResult result;
  306. Physics2DDirectSpaceState *ss2d = Physics2DServer::get_singleton()->space_get_direct_state(find_world_2d()->get_space());
  307. if (physics_has_last_mousepos) {
  308. // if no mouse event exists, create a motion one. This is necessary because objects or camera may have moved.
  309. // 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
  310. bool has_mouse_event = false;
  311. for (List<Ref<InputEvent> >::Element *E = physics_picking_events.front(); E; E = E->next()) {
  312. Ref<InputEventMouse> m = E->get();
  313. if (m.is_valid()) {
  314. has_mouse_event = true;
  315. break;
  316. }
  317. }
  318. if (!has_mouse_event) {
  319. Ref<InputEventMouseMotion> mm;
  320. mm.instance();
  321. mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
  322. mm->set_global_position(physics_last_mousepos);
  323. mm->set_position(physics_last_mousepos);
  324. mm->set_alt(physics_last_mouse_state.alt);
  325. mm->set_shift(physics_last_mouse_state.shift);
  326. mm->set_control(physics_last_mouse_state.control);
  327. mm->set_metakey(physics_last_mouse_state.meta);
  328. mm->set_button_mask(physics_last_mouse_state.mouse_mask);
  329. physics_picking_events.push_back(mm);
  330. }
  331. }
  332. while (physics_picking_events.size()) {
  333. Ref<InputEvent> ev = physics_picking_events.front()->get();
  334. physics_picking_events.pop_front();
  335. Vector2 pos;
  336. bool is_mouse = false;
  337. Ref<InputEventMouseMotion> mm = ev;
  338. if (mm.is_valid()) {
  339. pos = mm->get_position();
  340. is_mouse = true;
  341. physics_has_last_mousepos = true;
  342. physics_last_mousepos = pos;
  343. physics_last_mouse_state.alt = mm->get_alt();
  344. physics_last_mouse_state.shift = mm->get_shift();
  345. physics_last_mouse_state.control = mm->get_control();
  346. physics_last_mouse_state.meta = mm->get_metakey();
  347. physics_last_mouse_state.mouse_mask = mm->get_button_mask();
  348. }
  349. Ref<InputEventMouseButton> mb = ev;
  350. if (mb.is_valid()) {
  351. pos = mb->get_position();
  352. is_mouse = true;
  353. physics_has_last_mousepos = true;
  354. physics_last_mousepos = pos;
  355. physics_last_mouse_state.alt = mb->get_alt();
  356. physics_last_mouse_state.shift = mb->get_shift();
  357. physics_last_mouse_state.control = mb->get_control();
  358. physics_last_mouse_state.meta = mb->get_metakey();
  359. if (mb->is_pressed()) {
  360. physics_last_mouse_state.mouse_mask |= (1 << (mb->get_button_index() - 1));
  361. } else {
  362. physics_last_mouse_state.mouse_mask &= ~(1 << (mb->get_button_index() - 1));
  363. // If touch mouse raised, assume we don't know last mouse pos until new events come
  364. if (mb->get_device() == InputEvent::DEVICE_ID_TOUCH_MOUSE) {
  365. physics_has_last_mousepos = false;
  366. }
  367. }
  368. }
  369. Ref<InputEventKey> k = ev;
  370. if (k.is_valid()) {
  371. //only for mask
  372. physics_last_mouse_state.alt = k->get_alt();
  373. physics_last_mouse_state.shift = k->get_shift();
  374. physics_last_mouse_state.control = k->get_control();
  375. physics_last_mouse_state.meta = k->get_metakey();
  376. continue;
  377. }
  378. Ref<InputEventScreenDrag> sd = ev;
  379. if (sd.is_valid()) {
  380. pos = sd->get_position();
  381. }
  382. Ref<InputEventScreenTouch> st = ev;
  383. if (st.is_valid()) {
  384. pos = st->get_position();
  385. }
  386. if (ss2d) {
  387. //send to 2D
  388. uint64_t frame = get_tree()->get_frame();
  389. Physics2DDirectSpaceState::ShapeResult res[64];
  390. for (Set<CanvasLayer *>::Element *E = canvas_layers.front(); E; E = E->next()) {
  391. Transform2D canvas_transform;
  392. ObjectID canvas_layer_id;
  393. if (E->get()) {
  394. // A descendant CanvasLayer
  395. canvas_transform = E->get()->get_transform();
  396. canvas_layer_id = E->get()->get_instance_id();
  397. } else {
  398. // This Viewport's builtin canvas
  399. canvas_transform = get_canvas_transform();
  400. canvas_layer_id = 0;
  401. }
  402. Vector2 point = canvas_transform.affine_inverse().xform(pos);
  403. int rc = ss2d->intersect_point_on_canvas(point, canvas_layer_id, res, 64, Set<RID>(), 0xFFFFFFFF, true, true, true);
  404. for (int i = 0; i < rc; i++) {
  405. if (res[i].collider_id && res[i].collider) {
  406. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(res[i].collider);
  407. if (co) {
  408. bool send_event = true;
  409. if (is_mouse) {
  410. Map<ObjectID, uint64_t>::Element *F = physics_2d_mouseover.find(res[i].collider_id);
  411. if (!F) {
  412. F = physics_2d_mouseover.insert(res[i].collider_id, frame);
  413. co->_mouse_enter();
  414. } else {
  415. F->get() = frame;
  416. // It was already hovered, so don't send the event if it's faked
  417. if (mm.is_valid() && mm->get_device() == InputEvent::DEVICE_ID_INTERNAL) {
  418. send_event = false;
  419. }
  420. }
  421. }
  422. if (send_event) {
  423. co->_input_event(this, ev, res[i].shape);
  424. }
  425. }
  426. }
  427. }
  428. }
  429. if (is_mouse) {
  430. List<Map<ObjectID, uint64_t>::Element *> to_erase;
  431. for (Map<ObjectID, uint64_t>::Element *E = physics_2d_mouseover.front(); E; E = E->next()) {
  432. if (E->get() != frame) {
  433. Object *o = ObjectDB::get_instance(E->key());
  434. if (o) {
  435. CollisionObject2D *co = Object::cast_to<CollisionObject2D>(o);
  436. if (co) {
  437. co->_mouse_exit();
  438. }
  439. }
  440. to_erase.push_back(E);
  441. }
  442. }
  443. while (to_erase.size()) {
  444. physics_2d_mouseover.erase(to_erase.front()->get());
  445. to_erase.pop_front();
  446. }
  447. }
  448. }
  449. #ifndef _3D_DISABLED
  450. bool captured = false;
  451. if (physics_object_capture != 0) {
  452. CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(physics_object_capture));
  453. if (co) {
  454. _collision_object_input_event(co, camera, ev, Vector3(), Vector3(), 0);
  455. captured = true;
  456. if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) {
  457. physics_object_capture = 0;
  458. }
  459. } else {
  460. physics_object_capture = 0;
  461. }
  462. }
  463. if (captured) {
  464. //none
  465. } else if (pos == last_pos) {
  466. if (last_id) {
  467. if (ObjectDB::get_instance(last_id) && last_object) {
  468. //good, exists
  469. _collision_object_input_event(last_object, camera, ev, result.position, result.normal, result.shape);
  470. if (last_object->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) {
  471. physics_object_capture = last_id;
  472. }
  473. }
  474. }
  475. } else {
  476. if (camera) {
  477. Vector3 from = camera->project_ray_origin(pos);
  478. Vector3 dir = camera->project_ray_normal(pos);
  479. PhysicsDirectSpaceState *space = PhysicsServer::get_singleton()->space_get_direct_state(find_world()->get_space());
  480. if (space) {
  481. bool col = space->intersect_ray(from, from + dir * 10000, result, Set<RID>(), 0xFFFFFFFF, true, true, true);
  482. ObjectID new_collider = 0;
  483. if (col) {
  484. CollisionObject *co = Object::cast_to<CollisionObject>(result.collider);
  485. if (co) {
  486. _collision_object_input_event(co, camera, ev, result.position, result.normal, result.shape);
  487. last_object = co;
  488. last_id = result.collider_id;
  489. new_collider = last_id;
  490. if (co->get_capture_input_on_drag() && mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) {
  491. physics_object_capture = last_id;
  492. }
  493. }
  494. }
  495. if (is_mouse && new_collider != physics_object_over) {
  496. if (physics_object_over) {
  497. CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(physics_object_over));
  498. if (co) {
  499. co->_mouse_exit();
  500. }
  501. }
  502. if (new_collider) {
  503. CollisionObject *co = Object::cast_to<CollisionObject>(ObjectDB::get_instance(new_collider));
  504. if (co) {
  505. co->_mouse_enter();
  506. }
  507. }
  508. physics_object_over = new_collider;
  509. }
  510. }
  511. last_pos = pos;
  512. }
  513. }
  514. #endif
  515. }
  516. }
  517. } break;
  518. case SceneTree::NOTIFICATION_WM_FOCUS_OUT: {
  519. if (gui.mouse_focus) {
  520. //if mouse is being pressed, send a release event
  521. _drop_mouse_focus();
  522. }
  523. } break;
  524. }
  525. }
  526. RID Viewport::get_viewport_rid() const {
  527. return viewport;
  528. }
  529. void Viewport::set_use_arvr(bool p_use_arvr) {
  530. arvr = p_use_arvr;
  531. VS::get_singleton()->viewport_set_use_arvr(viewport, arvr);
  532. }
  533. bool Viewport::use_arvr() {
  534. return arvr;
  535. }
  536. void Viewport::update_canvas_items() {
  537. if (!is_inside_tree())
  538. return;
  539. _update_canvas_items(this);
  540. }
  541. void Viewport::set_size(const Size2 &p_size) {
  542. if (size == p_size.floor())
  543. return;
  544. size = p_size.floor();
  545. VS::get_singleton()->viewport_set_size(viewport, size.width, size.height);
  546. _update_stretch_transform();
  547. emit_signal("size_changed");
  548. }
  549. Rect2 Viewport::get_visible_rect() const {
  550. Rect2 r;
  551. if (size == Size2()) {
  552. r = Rect2(Point2(), OS::get_singleton()->get_window_size());
  553. } else {
  554. r = Rect2(Point2(), size);
  555. }
  556. if (size_override) {
  557. r.size = size_override_size;
  558. }
  559. return r;
  560. }
  561. Size2 Viewport::get_size() const {
  562. return size;
  563. }
  564. void Viewport::_update_listener() {
  565. /*
  566. 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()))) {
  567. SpatialSoundServer::get_singleton()->listener_set_space(internal_listener, find_world()->get_sound_space());
  568. } else {
  569. SpatialSoundServer::get_singleton()->listener_set_space(internal_listener, RID());
  570. }
  571. */
  572. }
  573. void Viewport::_update_listener_2d() {
  574. /*
  575. if (is_inside_tree() && audio_listener && (!get_parent() || (Object::cast_to<Control>(get_parent()) && Object::cast_to<Control>(get_parent())->is_visible_in_tree())))
  576. SpatialSound2DServer::get_singleton()->listener_set_space(internal_listener_2d, find_world_2d()->get_sound_space());
  577. else
  578. SpatialSound2DServer::get_singleton()->listener_set_space(internal_listener_2d, RID());
  579. */
  580. }
  581. void Viewport::set_as_audio_listener(bool p_enable) {
  582. if (p_enable == audio_listener)
  583. return;
  584. audio_listener = p_enable;
  585. _update_listener();
  586. }
  587. bool Viewport::is_audio_listener() const {
  588. return audio_listener;
  589. }
  590. void Viewport::set_as_audio_listener_2d(bool p_enable) {
  591. if (p_enable == audio_listener_2d)
  592. return;
  593. audio_listener_2d = p_enable;
  594. _update_listener_2d();
  595. }
  596. bool Viewport::is_audio_listener_2d() const {
  597. return audio_listener_2d;
  598. }
  599. void Viewport::set_canvas_transform(const Transform2D &p_transform) {
  600. canvas_transform = p_transform;
  601. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport, find_world_2d()->get_canvas(), canvas_transform);
  602. }
  603. Transform2D Viewport::get_canvas_transform() const {
  604. return canvas_transform;
  605. }
  606. void Viewport::_update_global_transform() {
  607. Transform2D sxform = stretch_transform * global_canvas_transform;
  608. VisualServer::get_singleton()->viewport_set_global_canvas_transform(viewport, sxform);
  609. }
  610. void Viewport::set_global_canvas_transform(const Transform2D &p_transform) {
  611. global_canvas_transform = p_transform;
  612. _update_global_transform();
  613. }
  614. Transform2D Viewport::get_global_canvas_transform() const {
  615. return global_canvas_transform;
  616. }
  617. void Viewport::_listener_transform_changed_notify() {
  618. #ifndef _3D_DISABLED
  619. //if (listener)
  620. // SpatialSoundServer::get_singleton()->listener_set_transform(internal_listener, listener->get_listener_transform());
  621. #endif
  622. }
  623. void Viewport::_listener_set(Listener *p_listener) {
  624. #ifndef _3D_DISABLED
  625. if (listener == p_listener)
  626. return;
  627. listener = p_listener;
  628. _update_listener();
  629. _listener_transform_changed_notify();
  630. #endif
  631. }
  632. bool Viewport::_listener_add(Listener *p_listener) {
  633. listeners.insert(p_listener);
  634. return listeners.size() == 1;
  635. }
  636. void Viewport::_listener_remove(Listener *p_listener) {
  637. listeners.erase(p_listener);
  638. if (listener == p_listener) {
  639. listener = NULL;
  640. }
  641. }
  642. #ifndef _3D_DISABLED
  643. void Viewport::_listener_make_next_current(Listener *p_exclude) {
  644. if (listeners.size() > 0) {
  645. for (Set<Listener *>::Element *E = listeners.front(); E; E = E->next()) {
  646. if (p_exclude == E->get())
  647. continue;
  648. if (!E->get()->is_inside_tree())
  649. continue;
  650. if (listener != NULL)
  651. return;
  652. E->get()->make_current();
  653. }
  654. } else {
  655. // Attempt to reset listener to the camera position
  656. if (camera != NULL) {
  657. _update_listener();
  658. _camera_transform_changed_notify();
  659. }
  660. }
  661. }
  662. #endif
  663. void Viewport::_camera_transform_changed_notify() {
  664. #ifndef _3D_DISABLED
  665. // If there is an active listener in the scene, it takes priority over the camera
  666. // if (camera && !listener)
  667. // SpatialSoundServer::get_singleton()->listener_set_transform(internal_listener, camera->get_camera_transform());
  668. #endif
  669. }
  670. void Viewport::_camera_set(Camera *p_camera) {
  671. #ifndef _3D_DISABLED
  672. if (camera == p_camera)
  673. return;
  674. if (camera && find_world().is_valid()) {
  675. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  676. }
  677. camera = p_camera;
  678. if (camera)
  679. VisualServer::get_singleton()->viewport_attach_camera(viewport, camera->get_camera());
  680. else
  681. VisualServer::get_singleton()->viewport_attach_camera(viewport, RID());
  682. if (camera && find_world().is_valid()) {
  683. camera->notification(Camera::NOTIFICATION_BECAME_CURRENT);
  684. }
  685. _update_listener();
  686. _camera_transform_changed_notify();
  687. #endif
  688. }
  689. bool Viewport::_camera_add(Camera *p_camera) {
  690. cameras.insert(p_camera);
  691. return cameras.size() == 1;
  692. }
  693. void Viewport::_camera_remove(Camera *p_camera) {
  694. cameras.erase(p_camera);
  695. if (camera == p_camera) {
  696. if (camera && find_world().is_valid()) {
  697. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  698. }
  699. camera = NULL;
  700. }
  701. }
  702. #ifndef _3D_DISABLED
  703. void Viewport::_camera_make_next_current(Camera *p_exclude) {
  704. for (Set<Camera *>::Element *E = cameras.front(); E; E = E->next()) {
  705. if (p_exclude == E->get())
  706. continue;
  707. if (!E->get()->is_inside_tree())
  708. continue;
  709. if (camera != NULL)
  710. return;
  711. E->get()->make_current();
  712. }
  713. }
  714. #endif
  715. void Viewport::_canvas_layer_add(CanvasLayer *p_canvas_layer) {
  716. canvas_layers.insert(p_canvas_layer);
  717. }
  718. void Viewport::_canvas_layer_remove(CanvasLayer *p_canvas_layer) {
  719. canvas_layers.erase(p_canvas_layer);
  720. }
  721. void Viewport::set_transparent_background(bool p_enable) {
  722. transparent_bg = p_enable;
  723. VS::get_singleton()->viewport_set_transparent_background(viewport, p_enable);
  724. }
  725. bool Viewport::has_transparent_background() const {
  726. return transparent_bg;
  727. }
  728. void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) {
  729. if (world_2d == p_world_2d)
  730. return;
  731. if (parent && parent->find_world_2d() == p_world_2d) {
  732. WARN_PRINT("Unable to use parent world as world_2d");
  733. return;
  734. }
  735. if (is_inside_tree()) {
  736. find_world_2d()->_remove_viewport(this);
  737. VisualServer::get_singleton()->viewport_remove_canvas(viewport, current_canvas);
  738. }
  739. if (p_world_2d.is_valid())
  740. world_2d = p_world_2d;
  741. else {
  742. WARN_PRINT("Invalid world");
  743. world_2d = Ref<World2D>(memnew(World2D));
  744. }
  745. _update_listener_2d();
  746. if (is_inside_tree()) {
  747. current_canvas = find_world_2d()->get_canvas();
  748. VisualServer::get_singleton()->viewport_attach_canvas(viewport, current_canvas);
  749. find_world_2d()->_register_viewport(this, Rect2());
  750. }
  751. }
  752. Ref<World2D> Viewport::find_world_2d() const {
  753. if (world_2d.is_valid())
  754. return world_2d;
  755. else if (parent)
  756. return parent->find_world_2d();
  757. else
  758. return Ref<World2D>();
  759. }
  760. void Viewport::_propagate_enter_world(Node *p_node) {
  761. if (p_node != this) {
  762. if (!p_node->is_inside_tree()) //may not have entered scene yet
  763. return;
  764. if (Object::cast_to<Spatial>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
  765. p_node->notification(Spatial::NOTIFICATION_ENTER_WORLD);
  766. } else {
  767. Viewport *v = Object::cast_to<Viewport>(p_node);
  768. if (v) {
  769. if (v->world.is_valid())
  770. return;
  771. }
  772. }
  773. }
  774. for (int i = 0; i < p_node->get_child_count(); i++) {
  775. _propagate_enter_world(p_node->get_child(i));
  776. }
  777. }
  778. void Viewport::_propagate_viewport_notification(Node *p_node, int p_what) {
  779. p_node->notification(p_what);
  780. for (int i = 0; i < p_node->get_child_count(); i++) {
  781. Node *c = p_node->get_child(i);
  782. if (Object::cast_to<Viewport>(c))
  783. continue;
  784. _propagate_viewport_notification(c, p_what);
  785. }
  786. }
  787. void Viewport::_propagate_exit_world(Node *p_node) {
  788. if (p_node != this) {
  789. if (!p_node->is_inside_tree()) //may have exited scene already
  790. return;
  791. if (Object::cast_to<Spatial>(p_node) || Object::cast_to<WorldEnvironment>(p_node)) {
  792. p_node->notification(Spatial::NOTIFICATION_EXIT_WORLD);
  793. } else {
  794. Viewport *v = Object::cast_to<Viewport>(p_node);
  795. if (v) {
  796. if (v->world.is_valid())
  797. return;
  798. }
  799. }
  800. }
  801. for (int i = 0; i < p_node->get_child_count(); i++) {
  802. _propagate_exit_world(p_node->get_child(i));
  803. }
  804. }
  805. void Viewport::set_world(const Ref<World> &p_world) {
  806. if (world == p_world)
  807. return;
  808. if (is_inside_tree())
  809. _propagate_exit_world(this);
  810. #ifndef _3D_DISABLED
  811. if (find_world().is_valid() && camera)
  812. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  813. #endif
  814. world = p_world;
  815. if (is_inside_tree())
  816. _propagate_enter_world(this);
  817. #ifndef _3D_DISABLED
  818. if (find_world().is_valid() && camera)
  819. camera->notification(Camera::NOTIFICATION_BECAME_CURRENT);
  820. #endif
  821. //propagate exit
  822. if (is_inside_tree()) {
  823. VisualServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario());
  824. }
  825. _update_listener();
  826. }
  827. Ref<World> Viewport::get_world() const {
  828. return world;
  829. }
  830. Ref<World2D> Viewport::get_world_2d() const {
  831. return world_2d;
  832. }
  833. Ref<World> Viewport::find_world() const {
  834. if (own_world.is_valid())
  835. return own_world;
  836. else if (world.is_valid())
  837. return world;
  838. else if (parent)
  839. return parent->find_world();
  840. else
  841. return Ref<World>();
  842. }
  843. Listener *Viewport::get_listener() const {
  844. return listener;
  845. }
  846. Camera *Viewport::get_camera() const {
  847. return camera;
  848. }
  849. Transform2D Viewport::get_final_transform() const {
  850. return stretch_transform * global_canvas_transform;
  851. }
  852. void Viewport::_update_canvas_items(Node *p_node) {
  853. if (p_node != this) {
  854. Viewport *vp = Object::cast_to<Viewport>(p_node);
  855. if (vp)
  856. return;
  857. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
  858. if (ci) {
  859. ci->update();
  860. }
  861. }
  862. int cc = p_node->get_child_count();
  863. for (int i = 0; i < cc; i++) {
  864. _update_canvas_items(p_node->get_child(i));
  865. }
  866. }
  867. void Viewport::set_size_override(bool p_enable, const Size2 &p_size, const Vector2 &p_margin) {
  868. if (size_override == p_enable && p_size == size_override_size)
  869. return;
  870. size_override = p_enable;
  871. if (p_size.x >= 0 || p_size.y >= 0) {
  872. size_override_size = p_size;
  873. }
  874. size_override_margin = p_margin;
  875. _update_stretch_transform();
  876. emit_signal("size_changed");
  877. }
  878. Size2 Viewport::get_size_override() const {
  879. return size_override_size;
  880. }
  881. bool Viewport::is_size_override_enabled() const {
  882. return size_override;
  883. }
  884. void Viewport::set_size_override_stretch(bool p_enable) {
  885. if (p_enable == size_override_stretch)
  886. return;
  887. size_override_stretch = p_enable;
  888. _update_stretch_transform();
  889. }
  890. bool Viewport::is_size_override_stretch_enabled() const {
  891. return size_override_stretch;
  892. }
  893. void Viewport::set_update_mode(UpdateMode p_mode) {
  894. update_mode = p_mode;
  895. VS::get_singleton()->viewport_set_update_mode(viewport, VS::ViewportUpdateMode(p_mode));
  896. }
  897. Viewport::UpdateMode Viewport::get_update_mode() const {
  898. return update_mode;
  899. }
  900. Ref<ViewportTexture> Viewport::get_texture() const {
  901. return default_texture;
  902. }
  903. void Viewport::set_vflip(bool p_enable) {
  904. vflip = p_enable;
  905. VisualServer::get_singleton()->viewport_set_vflip(viewport, p_enable);
  906. }
  907. bool Viewport::get_vflip() const {
  908. return vflip;
  909. }
  910. void Viewport::set_clear_mode(ClearMode p_mode) {
  911. clear_mode = p_mode;
  912. VS::get_singleton()->viewport_set_clear_mode(viewport, VS::ViewportClearMode(p_mode));
  913. }
  914. Viewport::ClearMode Viewport::get_clear_mode() const {
  915. return clear_mode;
  916. }
  917. void Viewport::set_shadow_atlas_size(int p_size) {
  918. if (shadow_atlas_size == p_size)
  919. return;
  920. shadow_atlas_size = p_size;
  921. VS::get_singleton()->viewport_set_shadow_atlas_size(viewport, p_size);
  922. }
  923. int Viewport::get_shadow_atlas_size() const {
  924. return shadow_atlas_size;
  925. }
  926. void Viewport::set_shadow_atlas_quadrant_subdiv(int p_quadrant, ShadowAtlasQuadrantSubdiv p_subdiv) {
  927. ERR_FAIL_INDEX(p_quadrant, 4);
  928. ERR_FAIL_INDEX(p_subdiv, SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
  929. if (shadow_atlas_quadrant_subdiv[p_quadrant] == p_subdiv)
  930. return;
  931. shadow_atlas_quadrant_subdiv[p_quadrant] = p_subdiv;
  932. static const int subdiv[SHADOW_ATLAS_QUADRANT_SUBDIV_MAX] = { 0, 1, 4, 16, 64, 256, 1024 };
  933. VS::get_singleton()->viewport_set_shadow_atlas_quadrant_subdivision(viewport, p_quadrant, subdiv[p_subdiv]);
  934. }
  935. Viewport::ShadowAtlasQuadrantSubdiv Viewport::get_shadow_atlas_quadrant_subdiv(int p_quadrant) const {
  936. ERR_FAIL_INDEX_V(p_quadrant, 4, SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
  937. return shadow_atlas_quadrant_subdiv[p_quadrant];
  938. }
  939. Transform2D Viewport::_get_input_pre_xform() const {
  940. Transform2D pre_xf;
  941. if (to_screen_rect != Rect2()) {
  942. pre_xf.elements[2] = -to_screen_rect.position;
  943. pre_xf.scale(size / to_screen_rect.size);
  944. }
  945. return pre_xf;
  946. }
  947. Vector2 Viewport::_get_window_offset() const {
  948. if (get_parent() && get_parent()->has_method("get_global_position")) {
  949. return get_parent()->call("get_global_position");
  950. }
  951. return Vector2();
  952. }
  953. Ref<InputEvent> Viewport::_make_input_local(const Ref<InputEvent> &ev) {
  954. Vector2 vp_ofs = _get_window_offset();
  955. Transform2D ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
  956. return ev->xformed_by(ai, -vp_ofs);
  957. }
  958. void Viewport::_vp_input_text(const String &p_text) {
  959. if (gui.key_focus) {
  960. gui.key_focus->call("set_text", p_text);
  961. }
  962. }
  963. void Viewport::_vp_input(const Ref<InputEvent> &p_ev) {
  964. if (disable_input)
  965. return;
  966. #ifdef TOOLS_ENABLED
  967. if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
  968. return;
  969. }
  970. #endif
  971. if (to_screen_rect == Rect2())
  972. return; //if render target, can't get input events
  973. //this one handles system input, p_ev are in system coordinates
  974. //they are converted to viewport coordinates
  975. Ref<InputEvent> ev = _make_input_local(p_ev);
  976. input(ev);
  977. }
  978. void Viewport::_vp_unhandled_input(const Ref<InputEvent> &p_ev) {
  979. if (disable_input)
  980. return;
  981. #ifdef TOOLS_ENABLED
  982. if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
  983. return;
  984. }
  985. #endif
  986. /*
  987. if (parent_control && !parent_control->is_visible_in_tree())
  988. return;
  989. */
  990. if (to_screen_rect == Rect2())
  991. return; //if render target, can't get input events
  992. //this one handles system input, p_ev are in system coordinates
  993. //they are converted to viewport coordinates
  994. Ref<InputEvent> ev = _make_input_local(p_ev);
  995. unhandled_input(ev);
  996. }
  997. Vector2 Viewport::get_mouse_position() const {
  998. return (get_final_transform().affine_inverse() * _get_input_pre_xform()).xform(Input::get_singleton()->get_mouse_position() - _get_window_offset());
  999. }
  1000. void Viewport::warp_mouse(const Vector2 &p_pos) {
  1001. Vector2 gpos = (get_final_transform().affine_inverse() * _get_input_pre_xform()).affine_inverse().xform(p_pos);
  1002. Input::get_singleton()->warp_mouse_position(gpos);
  1003. }
  1004. void Viewport::_gui_prepare_subwindows() {
  1005. if (gui.subwindow_visibility_dirty) {
  1006. gui.subwindows.clear();
  1007. for (List<Control *>::Element *E = gui.all_known_subwindows.front(); E; E = E->next()) {
  1008. if (E->get()->is_visible_in_tree()) {
  1009. gui.subwindows.push_back(E->get());
  1010. }
  1011. }
  1012. gui.subwindow_visibility_dirty = false;
  1013. gui.subwindow_order_dirty = true;
  1014. }
  1015. _gui_sort_subwindows();
  1016. }
  1017. void Viewport::_gui_sort_subwindows() {
  1018. if (!gui.subwindow_order_dirty)
  1019. return;
  1020. gui.modal_stack.sort_custom<Control::CComparator>();
  1021. gui.subwindows.sort_custom<Control::CComparator>();
  1022. gui.subwindow_order_dirty = false;
  1023. }
  1024. void Viewport::_gui_sort_modal_stack() {
  1025. gui.modal_stack.sort_custom<Control::CComparator>();
  1026. }
  1027. void Viewport::_gui_sort_roots() {
  1028. if (!gui.roots_order_dirty)
  1029. return;
  1030. gui.roots.sort_custom<Control::CComparator>();
  1031. gui.roots_order_dirty = false;
  1032. }
  1033. void Viewport::_gui_cancel_tooltip() {
  1034. gui.tooltip = NULL;
  1035. gui.tooltip_timer = -1;
  1036. if (gui.tooltip_popup) {
  1037. gui.tooltip_popup->queue_delete();
  1038. gui.tooltip_popup = NULL;
  1039. gui.tooltip_label = NULL;
  1040. }
  1041. }
  1042. String Viewport::_gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Control **r_which) {
  1043. Vector2 pos = p_pos;
  1044. String tooltip;
  1045. while (p_control) {
  1046. tooltip = p_control->get_tooltip(pos);
  1047. if (r_which) {
  1048. *r_which = p_control;
  1049. }
  1050. if (tooltip != String())
  1051. break;
  1052. pos = p_control->get_transform().xform(pos);
  1053. if (p_control->data.mouse_filter == Control::MOUSE_FILTER_STOP)
  1054. break;
  1055. if (p_control->is_set_as_toplevel())
  1056. break;
  1057. p_control = p_control->get_parent_control();
  1058. }
  1059. return tooltip;
  1060. }
  1061. void Viewport::_gui_show_tooltip() {
  1062. if (!gui.tooltip) {
  1063. return;
  1064. }
  1065. Control *which = NULL;
  1066. String tooltip = _gui_get_tooltip(gui.tooltip, gui.tooltip->get_global_transform().xform_inv(gui.tooltip_pos), &which);
  1067. if (tooltip.length() == 0)
  1068. return; // bye
  1069. if (gui.tooltip_popup) {
  1070. memdelete(gui.tooltip_popup);
  1071. gui.tooltip_popup = NULL;
  1072. gui.tooltip_label = NULL;
  1073. }
  1074. if (!which) {
  1075. return;
  1076. }
  1077. Control *rp = which; //->get_root_parent_control();
  1078. if (!rp)
  1079. return;
  1080. gui.tooltip_popup = which->make_custom_tooltip(tooltip);
  1081. if (!gui.tooltip_popup) {
  1082. gui.tooltip_popup = memnew(TooltipPanel);
  1083. gui.tooltip_label = memnew(TooltipLabel);
  1084. gui.tooltip_popup->add_child(gui.tooltip_label);
  1085. Ref<StyleBox> ttp = gui.tooltip_label->get_stylebox("panel", "TooltipPanel");
  1086. gui.tooltip_label->set_anchor_and_margin(MARGIN_LEFT, Control::ANCHOR_BEGIN, ttp->get_margin(MARGIN_LEFT));
  1087. gui.tooltip_label->set_anchor_and_margin(MARGIN_TOP, Control::ANCHOR_BEGIN, ttp->get_margin(MARGIN_TOP));
  1088. gui.tooltip_label->set_anchor_and_margin(MARGIN_RIGHT, Control::ANCHOR_END, -ttp->get_margin(MARGIN_RIGHT));
  1089. gui.tooltip_label->set_anchor_and_margin(MARGIN_BOTTOM, Control::ANCHOR_END, -ttp->get_margin(MARGIN_BOTTOM));
  1090. gui.tooltip_label->set_text(tooltip.strip_edges());
  1091. }
  1092. rp->add_child(gui.tooltip_popup);
  1093. gui.tooltip_popup->force_parent_owned();
  1094. gui.tooltip_popup->set_as_toplevel(true);
  1095. //gui.tooltip_popup->hide();
  1096. Point2 tooltip_offset = ProjectSettings::get_singleton()->get("display/mouse_cursor/tooltip_position_offset");
  1097. Rect2 r(gui.tooltip_pos + tooltip_offset, gui.tooltip_popup->get_minimum_size());
  1098. Rect2 vr = gui.tooltip_popup->get_viewport_rect();
  1099. if (r.size.x + r.position.x > vr.size.x)
  1100. r.position.x = vr.size.x - r.size.x;
  1101. else if (r.position.x < 0)
  1102. r.position.x = 0;
  1103. if (r.size.y + r.position.y > vr.size.y)
  1104. r.position.y = vr.size.y - r.size.y;
  1105. else if (r.position.y < 0)
  1106. r.position.y = 0;
  1107. gui.tooltip_popup->set_global_position(r.position);
  1108. gui.tooltip_popup->set_size(r.size);
  1109. gui.tooltip_popup->raise();
  1110. gui.tooltip_popup->show();
  1111. }
  1112. void Viewport::_gui_call_input(Control *p_control, const Ref<InputEvent> &p_input) {
  1113. //_block();
  1114. Ref<InputEvent> ev = p_input;
  1115. //mouse wheel events can't be stopped
  1116. Ref<InputEventMouseButton> mb = p_input;
  1117. bool cant_stop_me_now = (mb.is_valid() &&
  1118. (mb->get_button_index() == BUTTON_WHEEL_DOWN ||
  1119. mb->get_button_index() == BUTTON_WHEEL_UP ||
  1120. mb->get_button_index() == BUTTON_WHEEL_LEFT ||
  1121. mb->get_button_index() == BUTTON_WHEEL_RIGHT));
  1122. Ref<InputEventPanGesture> pn = p_input;
  1123. cant_stop_me_now = pn.is_valid() || cant_stop_me_now;
  1124. bool ismouse = ev.is_valid() || Object::cast_to<InputEventMouseMotion>(*p_input) != NULL;
  1125. CanvasItem *ci = p_control;
  1126. while (ci) {
  1127. Control *control = Object::cast_to<Control>(ci);
  1128. if (control) {
  1129. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1130. 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)
  1131. }
  1132. if (gui.key_event_accepted)
  1133. break;
  1134. if (!control->is_inside_tree())
  1135. break;
  1136. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1137. control->call_multilevel(SceneStringNames::get_singleton()->_gui_input, ev);
  1138. }
  1139. if (!control->is_inside_tree() || control->is_set_as_toplevel())
  1140. break;
  1141. if (gui.key_event_accepted)
  1142. break;
  1143. if (!cant_stop_me_now && control->data.mouse_filter == Control::MOUSE_FILTER_STOP && ismouse)
  1144. break;
  1145. }
  1146. if (ci->is_set_as_toplevel())
  1147. break;
  1148. ev = ev->xformed_by(ci->get_transform()); //transform event upwards
  1149. ci = ci->get_parent_item();
  1150. }
  1151. //_unblock();
  1152. }
  1153. void Viewport::_gui_call_notification(Control *p_control, int p_what) {
  1154. CanvasItem *ci = p_control;
  1155. while (ci) {
  1156. Control *control = Object::cast_to<Control>(ci);
  1157. if (control) {
  1158. if (control->data.mouse_filter != Control::MOUSE_FILTER_IGNORE) {
  1159. control->notification(p_what);
  1160. }
  1161. if (!control->is_inside_tree())
  1162. break;
  1163. if (!control->is_inside_tree() || control->is_set_as_toplevel())
  1164. break;
  1165. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP)
  1166. break;
  1167. }
  1168. if (ci->is_set_as_toplevel())
  1169. break;
  1170. ci = ci->get_parent_item();
  1171. }
  1172. //_unblock();
  1173. }
  1174. Control *Viewport::_gui_find_control(const Point2 &p_global) {
  1175. _gui_prepare_subwindows();
  1176. for (List<Control *>::Element *E = gui.subwindows.back(); E; E = E->prev()) {
  1177. Control *sw = E->get();
  1178. if (!sw->is_visible_in_tree())
  1179. continue;
  1180. Transform2D xform;
  1181. CanvasItem *pci = sw->get_parent_item();
  1182. if (pci)
  1183. xform = pci->get_global_transform_with_canvas();
  1184. else
  1185. xform = sw->get_canvas_transform();
  1186. Control *ret = _gui_find_control_at_pos(sw, p_global, xform, gui.focus_inv_xform);
  1187. if (ret)
  1188. return ret;
  1189. }
  1190. _gui_sort_roots();
  1191. for (List<Control *>::Element *E = gui.roots.back(); E; E = E->prev()) {
  1192. Control *sw = E->get();
  1193. if (!sw->is_visible_in_tree())
  1194. continue;
  1195. Transform2D xform;
  1196. CanvasItem *pci = sw->get_parent_item();
  1197. if (pci)
  1198. xform = pci->get_global_transform_with_canvas();
  1199. else
  1200. xform = sw->get_canvas_transform();
  1201. Control *ret = _gui_find_control_at_pos(sw, p_global, xform, gui.focus_inv_xform);
  1202. if (ret)
  1203. return ret;
  1204. }
  1205. return NULL;
  1206. }
  1207. Control *Viewport::_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Transform2D &p_xform, Transform2D &r_inv_xform) {
  1208. if (Object::cast_to<Viewport>(p_node))
  1209. return NULL;
  1210. //subwindows first!!
  1211. if (!p_node->is_visible()) {
  1212. //return _find_next_visible_control_at_pos(p_node,p_global,r_inv_xform);
  1213. return NULL; //canvas item hidden, discard
  1214. }
  1215. Transform2D matrix = p_xform * p_node->get_transform();
  1216. // matrix.basis_determinant() == 0.0f implies that node does not exist on scene
  1217. if (matrix.basis_determinant() == 0.0f)
  1218. return NULL;
  1219. Control *c = Object::cast_to<Control>(p_node);
  1220. if (!c || !c->clips_input() || c->has_point(matrix.affine_inverse().xform(p_global))) {
  1221. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  1222. if (p_node == gui.tooltip_popup)
  1223. continue;
  1224. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node->get_child(i));
  1225. if (!ci || ci->is_set_as_toplevel())
  1226. continue;
  1227. Control *ret = _gui_find_control_at_pos(ci, p_global, matrix, r_inv_xform);
  1228. if (ret)
  1229. return ret;
  1230. }
  1231. }
  1232. if (!c)
  1233. return NULL;
  1234. matrix.affine_invert();
  1235. //conditions for considering this as a valid control for return
  1236. if (c->data.mouse_filter != Control::MOUSE_FILTER_IGNORE && c->has_point(matrix.xform(p_global)) && (!gui.drag_preview || (c != gui.drag_preview && !gui.drag_preview->is_a_parent_of(c)))) {
  1237. r_inv_xform = matrix;
  1238. return c;
  1239. } else
  1240. return NULL;
  1241. }
  1242. bool Viewport::_gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check) {
  1243. { //attempt grab, try parent controls too
  1244. CanvasItem *ci = p_at_control;
  1245. while (ci) {
  1246. Control *control = Object::cast_to<Control>(ci);
  1247. if (control) {
  1248. if (control->can_drop_data(p_at_pos, gui.drag_data)) {
  1249. if (!p_just_check) {
  1250. control->drop_data(p_at_pos, gui.drag_data);
  1251. }
  1252. return true;
  1253. }
  1254. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP)
  1255. break;
  1256. }
  1257. p_at_pos = ci->get_transform().xform(p_at_pos);
  1258. if (ci->is_set_as_toplevel())
  1259. break;
  1260. ci = ci->get_parent_item();
  1261. }
  1262. }
  1263. return false;
  1264. }
  1265. void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
  1266. //?
  1267. /*
  1268. if (!is_visible()) {
  1269. return; //simple and plain
  1270. }
  1271. */
  1272. Ref<InputEventMouseButton> mb = p_event;
  1273. if (mb.is_valid()) {
  1274. gui.key_event_accepted = false;
  1275. Point2 mpos = mb->get_position();
  1276. if (mb->is_pressed()) {
  1277. Size2 pos = mpos;
  1278. if (gui.mouse_focus_mask) {
  1279. //do not steal mouse focus and stuff while a focus mask exists
  1280. gui.mouse_focus_mask |= 1 << (mb->get_button_index() - 1); //add the button to the mask
  1281. } else {
  1282. bool is_handled = false;
  1283. _gui_sort_modal_stack();
  1284. while (!gui.modal_stack.empty()) {
  1285. Control *top = gui.modal_stack.back()->get();
  1286. Vector2 pos2 = top->get_global_transform_with_canvas().affine_inverse().xform(mpos);
  1287. if (!top->has_point(pos2)) {
  1288. if (top->data.modal_exclusive || top->data.modal_frame == Engine::get_singleton()->get_frames_drawn()) {
  1289. //cancel event, sorry, modal exclusive EATS UP ALL
  1290. //alternative, you can't pop out a window the same frame it was made modal (fixes many issues)
  1291. set_input_as_handled();
  1292. return; // no one gets the event if exclusive NO ONE
  1293. }
  1294. top->notification(Control::NOTIFICATION_MODAL_CLOSE);
  1295. top->_modal_stack_remove();
  1296. top->hide();
  1297. if (!top->pass_on_modal_close_click()) {
  1298. is_handled = true;
  1299. }
  1300. } else {
  1301. break;
  1302. }
  1303. }
  1304. if (is_handled) {
  1305. set_input_as_handled();
  1306. return;
  1307. }
  1308. //Matrix32 parent_xform;
  1309. /*
  1310. if (data.parent_canvas_item)
  1311. parent_xform=data.parent_canvas_item->get_global_transform();
  1312. */
  1313. gui.mouse_focus = _gui_find_control(pos);
  1314. gui.last_mouse_focus = gui.mouse_focus;
  1315. if (!gui.mouse_focus) {
  1316. gui.mouse_focus_mask = 0;
  1317. return;
  1318. }
  1319. gui.mouse_focus_mask = 1 << (mb->get_button_index() - 1);
  1320. if (mb->get_button_index() == BUTTON_LEFT) {
  1321. gui.drag_accum = Vector2();
  1322. gui.drag_attempted = false;
  1323. }
  1324. }
  1325. mb = mb->xformed_by(Transform2D()); // make a copy of the event
  1326. mb->set_global_position(pos);
  1327. pos = gui.focus_inv_xform.xform(pos);
  1328. mb->set_position(pos);
  1329. #ifdef DEBUG_ENABLED
  1330. if (ScriptDebugger::get_singleton() && gui.mouse_focus) {
  1331. Array arr;
  1332. arr.push_back(gui.mouse_focus->get_path());
  1333. arr.push_back(gui.mouse_focus->get_class());
  1334. ScriptDebugger::get_singleton()->send_message("click_ctrl", arr);
  1335. }
  1336. #endif
  1337. if (mb->get_button_index() == BUTTON_LEFT) { //assign focus
  1338. CanvasItem *ci = gui.mouse_focus;
  1339. while (ci) {
  1340. Control *control = Object::cast_to<Control>(ci);
  1341. if (control) {
  1342. if (control->get_focus_mode() != Control::FOCUS_NONE) {
  1343. if (control != gui.key_focus) {
  1344. control->grab_focus();
  1345. }
  1346. break;
  1347. }
  1348. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP)
  1349. break;
  1350. }
  1351. if (ci->is_set_as_toplevel())
  1352. break;
  1353. ci = ci->get_parent_item();
  1354. }
  1355. }
  1356. if (gui.mouse_focus && gui.mouse_focus->can_process()) {
  1357. _gui_call_input(gui.mouse_focus, mb);
  1358. }
  1359. set_input_as_handled();
  1360. if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == BUTTON_LEFT) {
  1361. //alternate drop use (when using force_drag(), as proposed by #5342
  1362. if (gui.mouse_focus) {
  1363. _gui_drop(gui.mouse_focus, pos, false);
  1364. }
  1365. gui.drag_data = Variant();
  1366. gui.dragging = false;
  1367. if (gui.drag_preview) {
  1368. memdelete(gui.drag_preview);
  1369. gui.drag_preview = NULL;
  1370. }
  1371. _propagate_viewport_notification(this, NOTIFICATION_DRAG_END);
  1372. //change mouse accordingly
  1373. }
  1374. _gui_cancel_tooltip();
  1375. //gui.tooltip_popup->hide();
  1376. } else {
  1377. if (gui.drag_data.get_type() != Variant::NIL && mb->get_button_index() == BUTTON_LEFT) {
  1378. if (gui.mouse_over) {
  1379. Size2 pos = mpos;
  1380. pos = gui.focus_inv_xform.xform(pos);
  1381. _gui_drop(gui.mouse_over, pos, false);
  1382. }
  1383. if (gui.drag_preview && mb->get_button_index() == BUTTON_LEFT) {
  1384. memdelete(gui.drag_preview);
  1385. gui.drag_preview = NULL;
  1386. }
  1387. gui.drag_data = Variant();
  1388. gui.dragging = false;
  1389. _propagate_viewport_notification(this, NOTIFICATION_DRAG_END);
  1390. //change mouse accordingly
  1391. }
  1392. gui.mouse_focus_mask &= ~(1 << (mb->get_button_index() - 1)); //remove from mask
  1393. if (!gui.mouse_focus) {
  1394. //release event is only sent if a mouse focus (previously pressed button) exists
  1395. return;
  1396. }
  1397. Size2 pos = mpos;
  1398. mb = mb->xformed_by(Transform2D()); //make a copy
  1399. mb->set_global_position(pos);
  1400. pos = gui.focus_inv_xform.xform(pos);
  1401. mb->set_position(pos);
  1402. Control *mouse_focus = gui.mouse_focus;
  1403. //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
  1404. if (gui.mouse_focus_mask == 0) {
  1405. gui.mouse_focus = NULL;
  1406. }
  1407. if (mouse_focus && mouse_focus->can_process()) {
  1408. _gui_call_input(mouse_focus, mb);
  1409. }
  1410. /*if (gui.drag_data.get_type()!=Variant::NIL && mb->get_button_index()==BUTTON_LEFT) {
  1411. _propagate_viewport_notification(this,NOTIFICATION_DRAG_END);
  1412. gui.drag_data=Variant(); //always clear
  1413. }*/
  1414. set_input_as_handled();
  1415. }
  1416. }
  1417. Ref<InputEventMouseMotion> mm = p_event;
  1418. if (mm.is_valid()) {
  1419. gui.key_event_accepted = false;
  1420. Point2 mpos = mm->get_position();
  1421. gui.last_mouse_pos = mpos;
  1422. Control *over = NULL;
  1423. // D&D
  1424. if (!gui.drag_attempted && gui.mouse_focus && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  1425. gui.drag_accum += mm->get_relative();
  1426. float len = gui.drag_accum.length();
  1427. if (len > 10) {
  1428. { //attempt grab, try parent controls too
  1429. CanvasItem *ci = gui.mouse_focus;
  1430. while (ci) {
  1431. Control *control = Object::cast_to<Control>(ci);
  1432. if (control) {
  1433. gui.dragging = true;
  1434. gui.drag_data = control->get_drag_data(control->get_global_transform_with_canvas().affine_inverse().xform(mpos) - gui.drag_accum);
  1435. if (gui.drag_data.get_type() != Variant::NIL) {
  1436. gui.mouse_focus = NULL;
  1437. gui.mouse_focus_mask = 0;
  1438. break;
  1439. } else {
  1440. if (gui.drag_preview != NULL) {
  1441. ERR_PRINT("Don't set a drag preview and return null data. Preview was deleted and drag request ignored.");
  1442. memdelete(gui.drag_preview);
  1443. gui.drag_preview = NULL;
  1444. }
  1445. gui.dragging = false;
  1446. }
  1447. if (control->data.mouse_filter == Control::MOUSE_FILTER_STOP)
  1448. break;
  1449. }
  1450. if (ci->is_set_as_toplevel())
  1451. break;
  1452. ci = ci->get_parent_item();
  1453. }
  1454. }
  1455. gui.drag_attempted = true;
  1456. if (gui.drag_data.get_type() != Variant::NIL) {
  1457. _propagate_viewport_notification(this, NOTIFICATION_DRAG_BEGIN);
  1458. }
  1459. }
  1460. }
  1461. if (gui.mouse_focus) {
  1462. over = gui.mouse_focus;
  1463. //recompute focus_inv_xform again here
  1464. } else {
  1465. over = _gui_find_control(mpos);
  1466. }
  1467. if (gui.drag_data.get_type() == Variant::NIL && over && !gui.modal_stack.empty()) {
  1468. Control *top = gui.modal_stack.back()->get();
  1469. if (over != top && !top->is_a_parent_of(over)) {
  1470. PopupMenu *popup_menu = Object::cast_to<PopupMenu>(top);
  1471. MenuButton *popup_menu_parent = NULL;
  1472. MenuButton *menu_button = Object::cast_to<MenuButton>(over);
  1473. if (popup_menu) {
  1474. popup_menu_parent = Object::cast_to<MenuButton>(popup_menu->get_parent());
  1475. if (!popup_menu_parent) {
  1476. // Go through the parents to see if there's a MenuButton at the end.
  1477. while (Object::cast_to<PopupMenu>(popup_menu->get_parent())) {
  1478. popup_menu = Object::cast_to<PopupMenu>(popup_menu->get_parent());
  1479. }
  1480. popup_menu_parent = Object::cast_to<MenuButton>(popup_menu->get_parent());
  1481. }
  1482. }
  1483. // If the mouse is over a menu button, this menu will open automatically
  1484. // if there is already a pop-up menu open at the same hierarchical level.
  1485. if (popup_menu_parent && menu_button && popup_menu_parent->is_switch_on_hover() &&
  1486. !menu_button->is_disabled() && menu_button->is_switch_on_hover() &&
  1487. (popup_menu_parent->get_parent()->is_a_parent_of(menu_button) ||
  1488. menu_button->get_parent()->is_a_parent_of(popup_menu))) {
  1489. popup_menu->notification(Control::NOTIFICATION_MODAL_CLOSE);
  1490. popup_menu->_modal_stack_remove();
  1491. popup_menu->hide();
  1492. menu_button->pressed();
  1493. } else {
  1494. over = NULL; //nothing can be found outside the modal stack
  1495. }
  1496. }
  1497. }
  1498. if (over != gui.mouse_over) {
  1499. if (gui.mouse_over) {
  1500. _gui_call_notification(gui.mouse_over, Control::NOTIFICATION_MOUSE_EXIT);
  1501. }
  1502. _gui_cancel_tooltip();
  1503. if (over) {
  1504. _gui_call_notification(over, Control::NOTIFICATION_MOUSE_ENTER);
  1505. }
  1506. }
  1507. gui.mouse_over = over;
  1508. if (gui.drag_preview) {
  1509. gui.drag_preview->set_position(mpos);
  1510. }
  1511. if (!over) {
  1512. OS::get_singleton()->set_cursor_shape((OS::CursorShape)Input::get_singleton()->get_default_cursor_shape());
  1513. return;
  1514. }
  1515. Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
  1516. Size2 pos = localizer.xform(mpos);
  1517. Vector2 speed = localizer.basis_xform(mm->get_speed());
  1518. Vector2 rel = localizer.basis_xform(mm->get_relative());
  1519. mm = mm->xformed_by(Transform2D()); //make a copy
  1520. mm->set_global_position(mpos);
  1521. mm->set_speed(speed);
  1522. mm->set_relative(rel);
  1523. if (mm->get_button_mask() == 0) {
  1524. //nothing pressed
  1525. bool can_tooltip = true;
  1526. if (!gui.modal_stack.empty()) {
  1527. if (gui.modal_stack.back()->get() != over && !gui.modal_stack.back()->get()->is_a_parent_of(over))
  1528. can_tooltip = false;
  1529. }
  1530. bool is_tooltip_shown = false;
  1531. if (gui.tooltip_popup) {
  1532. if (can_tooltip && gui.tooltip) {
  1533. String tooltip = _gui_get_tooltip(over, gui.tooltip->get_global_transform().xform_inv(mpos));
  1534. if (tooltip.length() == 0)
  1535. _gui_cancel_tooltip();
  1536. else if (gui.tooltip_label) {
  1537. if (tooltip == gui.tooltip_label->get_text()) {
  1538. is_tooltip_shown = true;
  1539. }
  1540. } else if (tooltip == String(gui.tooltip_popup->call("get_tooltip_text"))) {
  1541. is_tooltip_shown = true;
  1542. }
  1543. } else
  1544. _gui_cancel_tooltip();
  1545. }
  1546. if (can_tooltip && !is_tooltip_shown) {
  1547. gui.tooltip = over;
  1548. gui.tooltip_pos = mpos; //(parent_xform * get_transform()).affine_inverse().xform(pos);
  1549. gui.tooltip_timer = gui.tooltip_delay;
  1550. }
  1551. }
  1552. //pos = gui.focus_inv_xform.xform(pos);
  1553. mm->set_position(pos);
  1554. Control::CursorShape cursor_shape = Control::CURSOR_ARROW;
  1555. {
  1556. Control *c = over;
  1557. Vector2 cpos = pos;
  1558. while (c) {
  1559. cursor_shape = c->get_cursor_shape(cpos);
  1560. cpos = c->get_transform().xform(cpos);
  1561. if (cursor_shape != Control::CURSOR_ARROW)
  1562. break;
  1563. if (c->data.mouse_filter == Control::MOUSE_FILTER_STOP)
  1564. break;
  1565. if (c->is_set_as_toplevel())
  1566. break;
  1567. c = c->get_parent_control();
  1568. }
  1569. }
  1570. OS::get_singleton()->set_cursor_shape((OS::CursorShape)cursor_shape);
  1571. if (over && over->can_process()) {
  1572. _gui_call_input(over, mm);
  1573. }
  1574. set_input_as_handled();
  1575. if (gui.drag_data.get_type() != Variant::NIL && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  1576. bool can_drop = _gui_drop(over, pos, true);
  1577. if (!can_drop) {
  1578. OS::get_singleton()->set_cursor_shape(OS::CURSOR_FORBIDDEN);
  1579. } else {
  1580. OS::get_singleton()->set_cursor_shape(OS::CURSOR_CAN_DROP);
  1581. }
  1582. //change mouse accordingly i guess
  1583. }
  1584. }
  1585. Ref<InputEventScreenTouch> touch_event = p_event;
  1586. if (touch_event.is_valid()) {
  1587. Size2 pos = touch_event->get_position();
  1588. if (touch_event->is_pressed()) {
  1589. Control *over = _gui_find_control(pos);
  1590. if (over) {
  1591. if (!gui.modal_stack.empty()) {
  1592. Control *top = gui.modal_stack.back()->get();
  1593. if (over != top && !top->is_a_parent_of(over)) {
  1594. return;
  1595. }
  1596. }
  1597. if (over->can_process()) {
  1598. touch_event = touch_event->xformed_by(Transform2D()); //make a copy
  1599. if (over == gui.mouse_focus) {
  1600. pos = gui.focus_inv_xform.xform(pos);
  1601. } else {
  1602. pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
  1603. }
  1604. touch_event->set_position(pos);
  1605. _gui_call_input(over, touch_event);
  1606. }
  1607. set_input_as_handled();
  1608. return;
  1609. }
  1610. } else if (touch_event->get_index() == 0 && gui.last_mouse_focus) {
  1611. if (gui.last_mouse_focus->can_process()) {
  1612. touch_event = touch_event->xformed_by(Transform2D()); //make a copy
  1613. touch_event->set_position(gui.focus_inv_xform.xform(pos));
  1614. _gui_call_input(gui.last_mouse_focus, touch_event);
  1615. }
  1616. set_input_as_handled();
  1617. return;
  1618. }
  1619. }
  1620. Ref<InputEventGesture> gesture_event = p_event;
  1621. if (gesture_event.is_valid()) {
  1622. gui.key_event_accepted = false;
  1623. _gui_cancel_tooltip();
  1624. Size2 pos = gesture_event->get_position();
  1625. Control *over = _gui_find_control(pos);
  1626. if (over) {
  1627. if (over->can_process()) {
  1628. gesture_event = gesture_event->xformed_by(Transform2D()); //make a copy
  1629. if (over == gui.mouse_focus) {
  1630. pos = gui.focus_inv_xform.xform(pos);
  1631. } else {
  1632. pos = over->get_global_transform_with_canvas().affine_inverse().xform(pos);
  1633. }
  1634. gesture_event->set_position(pos);
  1635. _gui_call_input(over, gesture_event);
  1636. }
  1637. set_input_as_handled();
  1638. return;
  1639. }
  1640. }
  1641. Ref<InputEventScreenDrag> drag_event = p_event;
  1642. if (drag_event.is_valid()) {
  1643. Control *over = gui.mouse_focus;
  1644. if (!over) {
  1645. over = _gui_find_control(drag_event->get_position());
  1646. }
  1647. if (over) {
  1648. if (!gui.modal_stack.empty()) {
  1649. Control *top = gui.modal_stack.back()->get();
  1650. if (over != top && !top->is_a_parent_of(over)) {
  1651. return;
  1652. }
  1653. }
  1654. if (over->can_process()) {
  1655. Transform2D localizer = over->get_global_transform_with_canvas().affine_inverse();
  1656. Size2 pos = localizer.xform(drag_event->get_position());
  1657. Vector2 speed = localizer.basis_xform(drag_event->get_speed());
  1658. Vector2 rel = localizer.basis_xform(drag_event->get_relative());
  1659. drag_event = drag_event->xformed_by(Transform2D()); //make a copy
  1660. drag_event->set_speed(speed);
  1661. drag_event->set_relative(rel);
  1662. drag_event->set_position(pos);
  1663. _gui_call_input(over, drag_event);
  1664. }
  1665. set_input_as_handled();
  1666. return;
  1667. }
  1668. }
  1669. if (mm.is_null() && mb.is_null() && p_event->is_action_type()) {
  1670. if (gui.key_focus && !gui.key_focus->is_visible_in_tree()) {
  1671. gui.key_focus->release_focus();
  1672. }
  1673. if (gui.key_focus) {
  1674. gui.key_event_accepted = false;
  1675. if (gui.key_focus->can_process()) {
  1676. gui.key_focus->call_multilevel(SceneStringNames::get_singleton()->_gui_input, p_event);
  1677. if (gui.key_focus) //maybe lost it
  1678. gui.key_focus->emit_signal(SceneStringNames::get_singleton()->gui_input, p_event);
  1679. }
  1680. if (gui.key_event_accepted) {
  1681. set_input_as_handled();
  1682. return;
  1683. }
  1684. }
  1685. if (p_event->is_pressed() && p_event->is_action("ui_cancel") && !gui.modal_stack.empty()) {
  1686. _gui_sort_modal_stack();
  1687. Control *top = gui.modal_stack.back()->get();
  1688. if (!top->data.modal_exclusive) {
  1689. top->notification(Control::NOTIFICATION_MODAL_CLOSE);
  1690. top->_modal_stack_remove();
  1691. top->hide();
  1692. // Close modal, set input as handled
  1693. set_input_as_handled();
  1694. return;
  1695. }
  1696. }
  1697. Control *from = gui.key_focus ? gui.key_focus : NULL; //hmm
  1698. //keyboard focus
  1699. //if (from && p_event->is_pressed() && !p_event->get_alt() && !p_event->get_metakey() && !p_event->key->get_command()) {
  1700. Ref<InputEventKey> k = p_event;
  1701. //need to check for mods, otherwise any combination of alt/ctrl/shift+<up/down/left/righ/etc> is handled here when it shouldn't be.
  1702. bool mods = k.is_valid() && (k->get_control() || k->get_alt() || k->get_shift() || k->get_metakey());
  1703. if (from && p_event->is_pressed()) {
  1704. Control *next = NULL;
  1705. if (p_event->is_action_pressed("ui_focus_next")) {
  1706. next = from->find_next_valid_focus();
  1707. }
  1708. if (p_event->is_action_pressed("ui_focus_prev")) {
  1709. next = from->find_prev_valid_focus();
  1710. }
  1711. if (!mods && p_event->is_action_pressed("ui_up")) {
  1712. next = from->_get_focus_neighbour(MARGIN_TOP);
  1713. }
  1714. if (!mods && p_event->is_action_pressed("ui_left")) {
  1715. next = from->_get_focus_neighbour(MARGIN_LEFT);
  1716. }
  1717. if (!mods && p_event->is_action_pressed("ui_right")) {
  1718. next = from->_get_focus_neighbour(MARGIN_RIGHT);
  1719. }
  1720. if (!mods && p_event->is_action_pressed("ui_down")) {
  1721. next = from->_get_focus_neighbour(MARGIN_BOTTOM);
  1722. }
  1723. if (next) {
  1724. next->grab_focus();
  1725. set_input_as_handled();
  1726. }
  1727. }
  1728. }
  1729. }
  1730. List<Control *>::Element *Viewport::_gui_add_root_control(Control *p_control) {
  1731. gui.roots_order_dirty = true;
  1732. return gui.roots.push_back(p_control);
  1733. }
  1734. List<Control *>::Element *Viewport::_gui_add_subwindow_control(Control *p_control) {
  1735. p_control->connect("visibility_changed", this, "_subwindow_visibility_changed");
  1736. if (p_control->is_visible_in_tree()) {
  1737. gui.subwindow_order_dirty = true;
  1738. gui.subwindows.push_back(p_control);
  1739. }
  1740. return gui.all_known_subwindows.push_back(p_control);
  1741. }
  1742. void Viewport::_gui_set_subwindow_order_dirty() {
  1743. gui.subwindow_order_dirty = true;
  1744. }
  1745. void Viewport::_gui_set_root_order_dirty() {
  1746. gui.roots_order_dirty = true;
  1747. }
  1748. void Viewport::_gui_remove_modal_control(List<Control *>::Element *MI) {
  1749. gui.modal_stack.erase(MI);
  1750. }
  1751. void Viewport::_gui_remove_from_modal_stack(List<Control *>::Element *MI, ObjectID p_prev_focus_owner) {
  1752. //transfer the focus stack to the next
  1753. List<Control *>::Element *next = MI->next();
  1754. gui.modal_stack.erase(MI);
  1755. MI = NULL;
  1756. if (p_prev_focus_owner) {
  1757. // for previous window in stack, pass the focus so it feels more
  1758. // natural
  1759. if (!next) { //top of stack
  1760. Object *pfo = ObjectDB::get_instance(p_prev_focus_owner);
  1761. Control *pfoc = Object::cast_to<Control>(pfo);
  1762. if (!pfoc)
  1763. return;
  1764. if (!pfoc->is_inside_tree() || !pfoc->is_visible_in_tree())
  1765. return;
  1766. pfoc->grab_focus();
  1767. } else {
  1768. next->get()->_modal_set_prev_focus_owner(p_prev_focus_owner);
  1769. }
  1770. }
  1771. }
  1772. void Viewport::_gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control) {
  1773. ERR_EXPLAIN("Drag data must be a value");
  1774. ERR_FAIL_COND(p_data.get_type() == Variant::NIL);
  1775. gui.dragging = true;
  1776. gui.drag_data = p_data;
  1777. gui.mouse_focus = NULL;
  1778. if (p_control) {
  1779. _gui_set_drag_preview(p_base, p_control);
  1780. }
  1781. }
  1782. void Viewport::_gui_set_drag_preview(Control *p_base, Control *p_control) {
  1783. ERR_FAIL_NULL(p_control);
  1784. ERR_FAIL_COND(!Object::cast_to<Control>((Object *)p_control));
  1785. ERR_FAIL_COND(p_control->is_inside_tree());
  1786. ERR_FAIL_COND(p_control->get_parent() != NULL);
  1787. if (gui.drag_preview) {
  1788. memdelete(gui.drag_preview);
  1789. }
  1790. p_control->set_as_toplevel(true);
  1791. p_control->set_position(gui.last_mouse_pos);
  1792. p_base->get_root_parent_control()->add_child(p_control); //add as child of viewport
  1793. p_control->raise();
  1794. gui.drag_preview = p_control;
  1795. }
  1796. void Viewport::_gui_remove_root_control(List<Control *>::Element *RI) {
  1797. gui.roots.erase(RI);
  1798. }
  1799. void Viewport::_gui_remove_subwindow_control(List<Control *>::Element *SI) {
  1800. ERR_FAIL_COND(!SI);
  1801. Control *control = SI->get();
  1802. control->disconnect("visibility_changed", this, "_subwindow_visibility_changed");
  1803. List<Control *>::Element *E = gui.subwindows.find(control);
  1804. if (E)
  1805. gui.subwindows.erase(E);
  1806. gui.all_known_subwindows.erase(SI);
  1807. }
  1808. void Viewport::_gui_unfocus_control(Control *p_control) {
  1809. if (gui.key_focus == p_control) {
  1810. gui.key_focus->release_focus();
  1811. }
  1812. }
  1813. void Viewport::_gui_hid_control(Control *p_control) {
  1814. if (gui.mouse_focus == p_control) {
  1815. _drop_mouse_focus();
  1816. }
  1817. /* ???
  1818. if (data.window==p_control) {
  1819. window->drag_data=Variant();
  1820. if (window->drag_preview) {
  1821. memdelete( window->drag_preview);
  1822. window->drag_preview=NULL;
  1823. }
  1824. }
  1825. */
  1826. if (gui.key_focus == p_control)
  1827. _gui_remove_focus();
  1828. if (gui.mouse_over == p_control)
  1829. gui.mouse_over = NULL;
  1830. if (gui.tooltip == p_control)
  1831. _gui_cancel_tooltip();
  1832. }
  1833. void Viewport::_gui_remove_control(Control *p_control) {
  1834. if (gui.mouse_focus == p_control) {
  1835. gui.mouse_focus = NULL;
  1836. gui.mouse_focus_mask = 0;
  1837. }
  1838. if (gui.last_mouse_focus == p_control) {
  1839. gui.last_mouse_focus = NULL;
  1840. }
  1841. if (gui.key_focus == p_control)
  1842. gui.key_focus = NULL;
  1843. if (gui.mouse_over == p_control)
  1844. gui.mouse_over = NULL;
  1845. if (gui.tooltip == p_control)
  1846. gui.tooltip = NULL;
  1847. if (gui.tooltip_popup == p_control) {
  1848. _gui_cancel_tooltip();
  1849. }
  1850. }
  1851. void Viewport::_gui_remove_focus() {
  1852. if (gui.key_focus) {
  1853. Node *f = gui.key_focus;
  1854. gui.key_focus = NULL;
  1855. f->notification(Control::NOTIFICATION_FOCUS_EXIT, true);
  1856. }
  1857. }
  1858. bool Viewport::_gui_is_modal_on_top(const Control *p_control) {
  1859. return (gui.modal_stack.size() && gui.modal_stack.back()->get() == p_control);
  1860. }
  1861. bool Viewport::_gui_control_has_focus(const Control *p_control) {
  1862. return gui.key_focus == p_control;
  1863. }
  1864. void Viewport::_gui_control_grab_focus(Control *p_control) {
  1865. //no need for change
  1866. if (gui.key_focus && gui.key_focus == p_control)
  1867. return;
  1868. get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, "_viewports", "_gui_remove_focus");
  1869. gui.key_focus = p_control;
  1870. p_control->notification(Control::NOTIFICATION_FOCUS_ENTER);
  1871. p_control->update();
  1872. }
  1873. void Viewport::_gui_accept_event() {
  1874. gui.key_event_accepted = true;
  1875. if (is_inside_tree())
  1876. set_input_as_handled();
  1877. }
  1878. void Viewport::_drop_mouse_focus() {
  1879. Control *c = gui.mouse_focus;
  1880. int mask = gui.mouse_focus_mask;
  1881. gui.mouse_focus = NULL;
  1882. gui.mouse_focus_mask = 0;
  1883. for (int i = 0; i < 3; i++) {
  1884. if (mask & (1 << i)) {
  1885. Ref<InputEventMouseButton> mb;
  1886. mb.instance();
  1887. mb->set_position(c->get_local_mouse_position());
  1888. mb->set_global_position(c->get_local_mouse_position());
  1889. mb->set_button_index(i + 1);
  1890. mb->set_pressed(false);
  1891. c->call_multilevel(SceneStringNames::get_singleton()->_gui_input, mb);
  1892. }
  1893. }
  1894. }
  1895. List<Control *>::Element *Viewport::_gui_show_modal(Control *p_control) {
  1896. List<Control *>::Element *node = gui.modal_stack.push_back(p_control);
  1897. if (gui.key_focus)
  1898. p_control->_modal_set_prev_focus_owner(gui.key_focus->get_instance_id());
  1899. else
  1900. p_control->_modal_set_prev_focus_owner(0);
  1901. if (gui.mouse_focus && !p_control->is_a_parent_of(gui.mouse_focus) && !gui.mouse_click_grabber) {
  1902. _drop_mouse_focus();
  1903. }
  1904. return node;
  1905. }
  1906. Control *Viewport::_gui_get_focus_owner() {
  1907. return gui.key_focus;
  1908. }
  1909. void Viewport::_gui_grab_click_focus(Control *p_control) {
  1910. gui.mouse_click_grabber = p_control;
  1911. call_deferred("_post_gui_grab_click_focus");
  1912. }
  1913. void Viewport::_post_gui_grab_click_focus() {
  1914. Control *focus_grabber = gui.mouse_click_grabber;
  1915. if (!focus_grabber) {
  1916. // Redundant grab requests were made
  1917. return;
  1918. }
  1919. gui.mouse_click_grabber = NULL;
  1920. if (gui.mouse_focus) {
  1921. if (gui.mouse_focus == focus_grabber)
  1922. return;
  1923. int mask = gui.mouse_focus_mask;
  1924. Point2 click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  1925. for (int i = 0; i < 3; i++) {
  1926. if (mask & (1 << i)) {
  1927. Ref<InputEventMouseButton> mb;
  1928. mb.instance();
  1929. //send unclic
  1930. mb->set_position(click);
  1931. mb->set_button_index(i + 1);
  1932. mb->set_pressed(false);
  1933. gui.mouse_focus->call_multilevel(SceneStringNames::get_singleton()->_gui_input, mb);
  1934. }
  1935. }
  1936. gui.mouse_focus = focus_grabber;
  1937. gui.focus_inv_xform = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse();
  1938. click = gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  1939. for (int i = 0; i < 3; i++) {
  1940. if (mask & (1 << i)) {
  1941. Ref<InputEventMouseButton> mb;
  1942. mb.instance();
  1943. //send clic
  1944. mb->set_position(click);
  1945. mb->set_button_index(i + 1);
  1946. mb->set_pressed(true);
  1947. gui.mouse_focus->call_deferred(SceneStringNames::get_singleton()->_gui_input, mb);
  1948. }
  1949. }
  1950. }
  1951. }
  1952. ///////////////////////////////
  1953. void Viewport::input(const Ref<InputEvent> &p_event) {
  1954. ERR_FAIL_COND(!is_inside_tree());
  1955. local_input_handled = false;
  1956. if (!is_input_handled()) {
  1957. get_tree()->_call_input_pause(input_group, "_input", p_event); //not a bug, must happen before GUI, order is _input -> gui input -> _unhandled input
  1958. }
  1959. if (!is_input_handled()) {
  1960. _gui_input_event(p_event);
  1961. }
  1962. //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
  1963. }
  1964. void Viewport::unhandled_input(const Ref<InputEvent> &p_event) {
  1965. ERR_FAIL_COND(!is_inside_tree());
  1966. get_tree()->_call_input_pause(unhandled_input_group, "_unhandled_input", p_event);
  1967. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev);
  1968. if (!get_tree()->input_handled && Object::cast_to<InputEventKey>(*p_event) != NULL) {
  1969. get_tree()->_call_input_pause(unhandled_key_input_group, "_unhandled_key_input", p_event);
  1970. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev);
  1971. }
  1972. if (physics_object_picking && !get_tree()->input_handled) {
  1973. if (Input::get_singleton()->get_mouse_mode() != Input::MOUSE_MODE_CAPTURED &&
  1974. (Object::cast_to<InputEventMouseButton>(*p_event) ||
  1975. Object::cast_to<InputEventMouseMotion>(*p_event) ||
  1976. Object::cast_to<InputEventScreenDrag>(*p_event) ||
  1977. Object::cast_to<InputEventScreenTouch>(*p_event) ||
  1978. Object::cast_to<InputEventKey>(*p_event) //to remember state
  1979. )) {
  1980. physics_picking_events.push_back(p_event);
  1981. }
  1982. }
  1983. }
  1984. void Viewport::set_use_own_world(bool p_world) {
  1985. if (p_world == own_world.is_valid())
  1986. return;
  1987. if (is_inside_tree())
  1988. _propagate_exit_world(this);
  1989. #ifndef _3D_DISABLED
  1990. if (find_world().is_valid() && camera)
  1991. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  1992. #endif
  1993. if (!p_world)
  1994. own_world = Ref<World>();
  1995. else
  1996. own_world = Ref<World>(memnew(World));
  1997. if (is_inside_tree())
  1998. _propagate_enter_world(this);
  1999. #ifndef _3D_DISABLED
  2000. if (find_world().is_valid() && camera)
  2001. camera->notification(Camera::NOTIFICATION_BECAME_CURRENT);
  2002. #endif
  2003. //propagate exit
  2004. if (is_inside_tree()) {
  2005. VisualServer::get_singleton()->viewport_set_scenario(viewport, find_world()->get_scenario());
  2006. }
  2007. _update_listener();
  2008. }
  2009. bool Viewport::is_using_own_world() const {
  2010. return own_world.is_valid();
  2011. }
  2012. void Viewport::set_attach_to_screen_rect(const Rect2 &p_rect) {
  2013. VS::get_singleton()->viewport_attach_to_screen(viewport, p_rect);
  2014. to_screen_rect = p_rect;
  2015. }
  2016. Rect2 Viewport::get_attach_to_screen_rect() const {
  2017. return to_screen_rect;
  2018. }
  2019. void Viewport::set_physics_object_picking(bool p_enable) {
  2020. physics_object_picking = p_enable;
  2021. if (!physics_object_picking) {
  2022. physics_picking_events.clear();
  2023. }
  2024. }
  2025. bool Viewport::get_physics_object_picking() {
  2026. return physics_object_picking;
  2027. }
  2028. Vector2 Viewport::get_camera_coords(const Vector2 &p_viewport_coords) const {
  2029. Transform2D xf = get_final_transform();
  2030. return xf.xform(p_viewport_coords);
  2031. }
  2032. Vector2 Viewport::get_camera_rect_size() const {
  2033. return size;
  2034. }
  2035. bool Viewport::gui_has_modal_stack() const {
  2036. return gui.modal_stack.size();
  2037. }
  2038. void Viewport::set_disable_input(bool p_disable) {
  2039. disable_input = p_disable;
  2040. }
  2041. bool Viewport::is_input_disabled() const {
  2042. return disable_input;
  2043. }
  2044. void Viewport::set_disable_3d(bool p_disable) {
  2045. disable_3d = p_disable;
  2046. VS::get_singleton()->viewport_set_disable_3d(viewport, p_disable);
  2047. }
  2048. bool Viewport::is_3d_disabled() const {
  2049. return disable_3d;
  2050. }
  2051. void Viewport::set_keep_3d_linear(bool p_keep_3d_linear) {
  2052. keep_3d_linear = p_keep_3d_linear;
  2053. VS::get_singleton()->viewport_set_keep_3d_linear(viewport, keep_3d_linear);
  2054. }
  2055. bool Viewport::get_keep_3d_linear() const {
  2056. return keep_3d_linear;
  2057. }
  2058. Variant Viewport::gui_get_drag_data() const {
  2059. return gui.drag_data;
  2060. }
  2061. Control *Viewport::get_modal_stack_top() const {
  2062. return gui.modal_stack.size() ? gui.modal_stack.back()->get() : NULL;
  2063. }
  2064. String Viewport::get_configuration_warning() const {
  2065. /*if (get_parent() && !Object::cast_to<Control>(get_parent()) && !render_target) {
  2066. return TTR("This viewport is not set as render target. If you intend for it to display its contents directly to the screen, make it a child of a Control so it can obtain a size. Otherwise, make it a RenderTarget and assign its internal texture to some node for display.");
  2067. }*/
  2068. return String();
  2069. }
  2070. void Viewport::gui_reset_canvas_sort_index() {
  2071. gui.canvas_sort_index = 0;
  2072. }
  2073. int Viewport::gui_get_canvas_sort_index() {
  2074. return gui.canvas_sort_index++;
  2075. }
  2076. void Viewport::set_msaa(MSAA p_msaa) {
  2077. ERR_FAIL_INDEX(p_msaa, 5);
  2078. if (msaa == p_msaa)
  2079. return;
  2080. msaa = p_msaa;
  2081. VS::get_singleton()->viewport_set_msaa(viewport, VS::ViewportMSAA(p_msaa));
  2082. }
  2083. Viewport::MSAA Viewport::get_msaa() const {
  2084. return msaa;
  2085. }
  2086. void Viewport::set_hdr(bool p_hdr) {
  2087. if (hdr == p_hdr)
  2088. return;
  2089. hdr = p_hdr;
  2090. VS::get_singleton()->viewport_set_hdr(viewport, p_hdr);
  2091. }
  2092. bool Viewport::get_hdr() const {
  2093. return hdr;
  2094. }
  2095. void Viewport::set_usage(Usage p_usage) {
  2096. usage = p_usage;
  2097. VS::get_singleton()->viewport_set_usage(viewport, VS::ViewportUsage(p_usage));
  2098. }
  2099. Viewport::Usage Viewport::get_usage() const {
  2100. return usage;
  2101. }
  2102. void Viewport::set_debug_draw(DebugDraw p_debug_draw) {
  2103. debug_draw = p_debug_draw;
  2104. VS::get_singleton()->viewport_set_debug_draw(viewport, VS::ViewportDebugDraw(p_debug_draw));
  2105. }
  2106. Viewport::DebugDraw Viewport::get_debug_draw() const {
  2107. return debug_draw;
  2108. }
  2109. int Viewport::get_render_info(RenderInfo p_info) {
  2110. return VS::get_singleton()->viewport_get_render_info(viewport, VS::ViewportRenderInfo(p_info));
  2111. }
  2112. void Viewport::set_snap_controls_to_pixels(bool p_enable) {
  2113. snap_controls_to_pixels = p_enable;
  2114. }
  2115. bool Viewport::is_snap_controls_to_pixels_enabled() const {
  2116. return snap_controls_to_pixels;
  2117. }
  2118. bool Viewport::gui_is_dragging() const {
  2119. return gui.dragging;
  2120. }
  2121. void Viewport::set_input_as_handled() {
  2122. if (handle_input_locally) {
  2123. local_input_handled = true;
  2124. } else {
  2125. ERR_FAIL_COND(!is_inside_tree());
  2126. get_tree()->set_input_as_handled();
  2127. }
  2128. }
  2129. bool Viewport::is_input_handled() const {
  2130. if (handle_input_locally) {
  2131. return local_input_handled;
  2132. } else {
  2133. ERR_FAIL_COND_V(!is_inside_tree(), false);
  2134. return get_tree()->is_input_handled();
  2135. }
  2136. }
  2137. void Viewport::set_handle_input_locally(bool p_enable) {
  2138. handle_input_locally = p_enable;
  2139. }
  2140. bool Viewport::is_handling_input_locally() const {
  2141. return handle_input_locally;
  2142. }
  2143. void Viewport::_validate_property(PropertyInfo &property) const {
  2144. if (VisualServer::get_singleton()->is_low_end() && property.name == "hdr") {
  2145. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  2146. }
  2147. }
  2148. void Viewport::_bind_methods() {
  2149. ClassDB::bind_method(D_METHOD("set_use_arvr", "use"), &Viewport::set_use_arvr);
  2150. ClassDB::bind_method(D_METHOD("use_arvr"), &Viewport::use_arvr);
  2151. ClassDB::bind_method(D_METHOD("set_size", "size"), &Viewport::set_size);
  2152. ClassDB::bind_method(D_METHOD("get_size"), &Viewport::get_size);
  2153. ClassDB::bind_method(D_METHOD("set_world_2d", "world_2d"), &Viewport::set_world_2d);
  2154. ClassDB::bind_method(D_METHOD("get_world_2d"), &Viewport::get_world_2d);
  2155. ClassDB::bind_method(D_METHOD("find_world_2d"), &Viewport::find_world_2d);
  2156. ClassDB::bind_method(D_METHOD("set_world", "world"), &Viewport::set_world);
  2157. ClassDB::bind_method(D_METHOD("get_world"), &Viewport::get_world);
  2158. ClassDB::bind_method(D_METHOD("find_world"), &Viewport::find_world);
  2159. ClassDB::bind_method(D_METHOD("set_canvas_transform", "xform"), &Viewport::set_canvas_transform);
  2160. ClassDB::bind_method(D_METHOD("get_canvas_transform"), &Viewport::get_canvas_transform);
  2161. ClassDB::bind_method(D_METHOD("set_global_canvas_transform", "xform"), &Viewport::set_global_canvas_transform);
  2162. ClassDB::bind_method(D_METHOD("get_global_canvas_transform"), &Viewport::get_global_canvas_transform);
  2163. ClassDB::bind_method(D_METHOD("get_final_transform"), &Viewport::get_final_transform);
  2164. ClassDB::bind_method(D_METHOD("get_visible_rect"), &Viewport::get_visible_rect);
  2165. ClassDB::bind_method(D_METHOD("set_transparent_background", "enable"), &Viewport::set_transparent_background);
  2166. ClassDB::bind_method(D_METHOD("has_transparent_background"), &Viewport::has_transparent_background);
  2167. ClassDB::bind_method(D_METHOD("_vp_input"), &Viewport::_vp_input);
  2168. ClassDB::bind_method(D_METHOD("_vp_input_text", "text"), &Viewport::_vp_input_text);
  2169. ClassDB::bind_method(D_METHOD("_vp_unhandled_input"), &Viewport::_vp_unhandled_input);
  2170. ClassDB::bind_method(D_METHOD("set_size_override", "enable", "size", "margin"), &Viewport::set_size_override, DEFVAL(Size2(-1, -1)), DEFVAL(Size2(0, 0)));
  2171. ClassDB::bind_method(D_METHOD("get_size_override"), &Viewport::get_size_override);
  2172. ClassDB::bind_method(D_METHOD("is_size_override_enabled"), &Viewport::is_size_override_enabled);
  2173. ClassDB::bind_method(D_METHOD("set_size_override_stretch", "enabled"), &Viewport::set_size_override_stretch);
  2174. ClassDB::bind_method(D_METHOD("is_size_override_stretch_enabled"), &Viewport::is_size_override_stretch_enabled);
  2175. ClassDB::bind_method(D_METHOD("set_vflip", "enable"), &Viewport::set_vflip);
  2176. ClassDB::bind_method(D_METHOD("get_vflip"), &Viewport::get_vflip);
  2177. ClassDB::bind_method(D_METHOD("set_clear_mode", "mode"), &Viewport::set_clear_mode);
  2178. ClassDB::bind_method(D_METHOD("get_clear_mode"), &Viewport::get_clear_mode);
  2179. ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &Viewport::set_update_mode);
  2180. ClassDB::bind_method(D_METHOD("get_update_mode"), &Viewport::get_update_mode);
  2181. ClassDB::bind_method(D_METHOD("set_msaa", "msaa"), &Viewport::set_msaa);
  2182. ClassDB::bind_method(D_METHOD("get_msaa"), &Viewport::get_msaa);
  2183. ClassDB::bind_method(D_METHOD("set_hdr", "enable"), &Viewport::set_hdr);
  2184. ClassDB::bind_method(D_METHOD("get_hdr"), &Viewport::get_hdr);
  2185. ClassDB::bind_method(D_METHOD("set_usage", "usage"), &Viewport::set_usage);
  2186. ClassDB::bind_method(D_METHOD("get_usage"), &Viewport::get_usage);
  2187. ClassDB::bind_method(D_METHOD("set_debug_draw", "debug_draw"), &Viewport::set_debug_draw);
  2188. ClassDB::bind_method(D_METHOD("get_debug_draw"), &Viewport::get_debug_draw);
  2189. ClassDB::bind_method(D_METHOD("get_render_info", "info"), &Viewport::get_render_info);
  2190. ClassDB::bind_method(D_METHOD("get_texture"), &Viewport::get_texture);
  2191. ClassDB::bind_method(D_METHOD("set_physics_object_picking", "enable"), &Viewport::set_physics_object_picking);
  2192. ClassDB::bind_method(D_METHOD("get_physics_object_picking"), &Viewport::get_physics_object_picking);
  2193. ClassDB::bind_method(D_METHOD("get_viewport_rid"), &Viewport::get_viewport_rid);
  2194. ClassDB::bind_method(D_METHOD("input", "local_event"), &Viewport::input);
  2195. ClassDB::bind_method(D_METHOD("unhandled_input", "local_event"), &Viewport::unhandled_input);
  2196. ClassDB::bind_method(D_METHOD("update_worlds"), &Viewport::update_worlds);
  2197. ClassDB::bind_method(D_METHOD("set_use_own_world", "enable"), &Viewport::set_use_own_world);
  2198. ClassDB::bind_method(D_METHOD("is_using_own_world"), &Viewport::is_using_own_world);
  2199. ClassDB::bind_method(D_METHOD("get_camera"), &Viewport::get_camera);
  2200. ClassDB::bind_method(D_METHOD("set_as_audio_listener", "enable"), &Viewport::set_as_audio_listener);
  2201. ClassDB::bind_method(D_METHOD("is_audio_listener"), &Viewport::is_audio_listener);
  2202. ClassDB::bind_method(D_METHOD("set_as_audio_listener_2d", "enable"), &Viewport::set_as_audio_listener_2d);
  2203. ClassDB::bind_method(D_METHOD("is_audio_listener_2d"), &Viewport::is_audio_listener_2d);
  2204. ClassDB::bind_method(D_METHOD("set_attach_to_screen_rect", "rect"), &Viewport::set_attach_to_screen_rect);
  2205. ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position);
  2206. ClassDB::bind_method(D_METHOD("warp_mouse", "to_position"), &Viewport::warp_mouse);
  2207. ClassDB::bind_method(D_METHOD("gui_has_modal_stack"), &Viewport::gui_has_modal_stack);
  2208. ClassDB::bind_method(D_METHOD("gui_get_drag_data"), &Viewport::gui_get_drag_data);
  2209. ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging);
  2210. ClassDB::bind_method(D_METHOD("get_modal_stack_top"), &Viewport::get_modal_stack_top);
  2211. ClassDB::bind_method(D_METHOD("set_disable_input", "disable"), &Viewport::set_disable_input);
  2212. ClassDB::bind_method(D_METHOD("is_input_disabled"), &Viewport::is_input_disabled);
  2213. ClassDB::bind_method(D_METHOD("set_disable_3d", "disable"), &Viewport::set_disable_3d);
  2214. ClassDB::bind_method(D_METHOD("is_3d_disabled"), &Viewport::is_3d_disabled);
  2215. ClassDB::bind_method(D_METHOD("set_keep_3d_linear", "keep_3d_linear"), &Viewport::set_keep_3d_linear);
  2216. ClassDB::bind_method(D_METHOD("get_keep_3d_linear"), &Viewport::get_keep_3d_linear);
  2217. ClassDB::bind_method(D_METHOD("_gui_show_tooltip"), &Viewport::_gui_show_tooltip);
  2218. ClassDB::bind_method(D_METHOD("_gui_remove_focus"), &Viewport::_gui_remove_focus);
  2219. ClassDB::bind_method(D_METHOD("_post_gui_grab_click_focus"), &Viewport::_post_gui_grab_click_focus);
  2220. ClassDB::bind_method(D_METHOD("set_shadow_atlas_size", "size"), &Viewport::set_shadow_atlas_size);
  2221. ClassDB::bind_method(D_METHOD("get_shadow_atlas_size"), &Viewport::get_shadow_atlas_size);
  2222. ClassDB::bind_method(D_METHOD("set_snap_controls_to_pixels", "enabled"), &Viewport::set_snap_controls_to_pixels);
  2223. ClassDB::bind_method(D_METHOD("is_snap_controls_to_pixels_enabled"), &Viewport::is_snap_controls_to_pixels_enabled);
  2224. ClassDB::bind_method(D_METHOD("set_shadow_atlas_quadrant_subdiv", "quadrant", "subdiv"), &Viewport::set_shadow_atlas_quadrant_subdiv);
  2225. ClassDB::bind_method(D_METHOD("get_shadow_atlas_quadrant_subdiv", "quadrant"), &Viewport::get_shadow_atlas_quadrant_subdiv);
  2226. ClassDB::bind_method(D_METHOD("set_input_as_handled"), &Viewport::set_input_as_handled);
  2227. ClassDB::bind_method(D_METHOD("is_input_handled"), &Viewport::is_input_handled);
  2228. ClassDB::bind_method(D_METHOD("set_handle_input_locally", "enable"), &Viewport::set_handle_input_locally);
  2229. ClassDB::bind_method(D_METHOD("is_handling_input_locally"), &Viewport::is_handling_input_locally);
  2230. ClassDB::bind_method(D_METHOD("_subwindow_visibility_changed"), &Viewport::_subwindow_visibility_changed);
  2231. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arvr"), "set_use_arvr", "use_arvr");
  2232. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  2233. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world"), "set_use_own_world", "is_using_own_world");
  2234. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world", PROPERTY_HINT_RESOURCE_TYPE, "World"), "set_world", "get_world");
  2235. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", 0), "set_world_2d", "get_world_2d");
  2236. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent_bg"), "set_transparent_background", "has_transparent_background");
  2237. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally");
  2238. ADD_GROUP("Rendering", "");
  2239. ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x"), "set_msaa", "get_msaa");
  2240. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hdr"), "set_hdr", "get_hdr");
  2241. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_3d"), "set_disable_3d", "is_3d_disabled");
  2242. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_3d_linear"), "set_keep_3d_linear", "get_keep_3d_linear");
  2243. ADD_PROPERTY(PropertyInfo(Variant::INT, "usage", PROPERTY_HINT_ENUM, "2D,2D No-Sampling,3D,3D No-Effects"), "set_usage", "get_usage");
  2244. ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Overdraw,Wireframe"), "set_debug_draw", "get_debug_draw");
  2245. ADD_GROUP("Render Target", "render_target_");
  2246. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "render_target_v_flip"), "set_vflip", "get_vflip");
  2247. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_clear_mode", PROPERTY_HINT_ENUM, "Always,Never,Next Frame"), "set_clear_mode", "get_clear_mode");
  2248. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_target_update_mode", PROPERTY_HINT_ENUM, "Disabled,Once,When Visible,Always"), "set_update_mode", "get_update_mode");
  2249. ADD_GROUP("Audio Listener", "audio_listener_");
  2250. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_2d"), "set_as_audio_listener_2d", "is_audio_listener_2d");
  2251. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_listener_enable_3d"), "set_as_audio_listener", "is_audio_listener");
  2252. ADD_GROUP("Physics", "physics_");
  2253. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_object_picking"), "set_physics_object_picking", "get_physics_object_picking");
  2254. ADD_GROUP("GUI", "gui_");
  2255. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_disable_input"), "set_disable_input", "is_input_disabled");
  2256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gui_snap_controls_to_pixels"), "set_snap_controls_to_pixels", "is_snap_controls_to_pixels_enabled");
  2257. ADD_GROUP("Shadow Atlas", "shadow_atlas_");
  2258. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_atlas_size"), "set_shadow_atlas_size", "get_shadow_atlas_size");
  2259. 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);
  2260. 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);
  2261. 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);
  2262. 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);
  2263. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "canvas_transform", PROPERTY_HINT_NONE, "", 0), "set_canvas_transform", "get_canvas_transform");
  2264. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "global_canvas_transform", PROPERTY_HINT_NONE, "", 0), "set_global_canvas_transform", "get_global_canvas_transform");
  2265. ADD_SIGNAL(MethodInfo("size_changed"));
  2266. BIND_ENUM_CONSTANT(UPDATE_DISABLED);
  2267. BIND_ENUM_CONSTANT(UPDATE_ONCE);
  2268. BIND_ENUM_CONSTANT(UPDATE_WHEN_VISIBLE);
  2269. BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
  2270. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED);
  2271. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1);
  2272. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  2273. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_16);
  2274. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_64);
  2275. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_256);
  2276. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_1024);
  2277. BIND_ENUM_CONSTANT(SHADOW_ATLAS_QUADRANT_SUBDIV_MAX);
  2278. BIND_ENUM_CONSTANT(RENDER_INFO_OBJECTS_IN_FRAME);
  2279. BIND_ENUM_CONSTANT(RENDER_INFO_VERTICES_IN_FRAME);
  2280. BIND_ENUM_CONSTANT(RENDER_INFO_MATERIAL_CHANGES_IN_FRAME);
  2281. BIND_ENUM_CONSTANT(RENDER_INFO_SHADER_CHANGES_IN_FRAME);
  2282. BIND_ENUM_CONSTANT(RENDER_INFO_SURFACE_CHANGES_IN_FRAME);
  2283. BIND_ENUM_CONSTANT(RENDER_INFO_DRAW_CALLS_IN_FRAME);
  2284. BIND_ENUM_CONSTANT(RENDER_INFO_MAX);
  2285. BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLED);
  2286. BIND_ENUM_CONSTANT(DEBUG_DRAW_UNSHADED);
  2287. BIND_ENUM_CONSTANT(DEBUG_DRAW_OVERDRAW);
  2288. BIND_ENUM_CONSTANT(DEBUG_DRAW_WIREFRAME);
  2289. BIND_ENUM_CONSTANT(MSAA_DISABLED);
  2290. BIND_ENUM_CONSTANT(MSAA_2X);
  2291. BIND_ENUM_CONSTANT(MSAA_4X);
  2292. BIND_ENUM_CONSTANT(MSAA_8X);
  2293. BIND_ENUM_CONSTANT(MSAA_16X);
  2294. BIND_ENUM_CONSTANT(USAGE_2D);
  2295. BIND_ENUM_CONSTANT(USAGE_2D_NO_SAMPLING);
  2296. BIND_ENUM_CONSTANT(USAGE_3D);
  2297. BIND_ENUM_CONSTANT(USAGE_3D_NO_EFFECTS);
  2298. BIND_ENUM_CONSTANT(CLEAR_MODE_ALWAYS);
  2299. BIND_ENUM_CONSTANT(CLEAR_MODE_NEVER);
  2300. BIND_ENUM_CONSTANT(CLEAR_MODE_ONLY_NEXT_FRAME);
  2301. }
  2302. void Viewport::_subwindow_visibility_changed() {
  2303. // unfortunately, we don't know the sender, i.e. which subwindow changed;
  2304. // so we have to check them all.
  2305. gui.subwindow_visibility_dirty = true;
  2306. }
  2307. Viewport::Viewport() {
  2308. world_2d = Ref<World2D>(memnew(World2D));
  2309. viewport = VisualServer::get_singleton()->viewport_create();
  2310. texture_rid = VisualServer::get_singleton()->viewport_get_texture(viewport);
  2311. texture_flags = 0;
  2312. default_texture.instance();
  2313. default_texture->vp = const_cast<Viewport *>(this);
  2314. viewport_textures.insert(default_texture.ptr());
  2315. VS::get_singleton()->texture_set_proxy(default_texture->proxy, texture_rid);
  2316. //internal_listener = SpatialSoundServer::get_singleton()->listener_create();
  2317. audio_listener = false;
  2318. //internal_listener_2d = SpatialSound2DServer::get_singleton()->listener_create();
  2319. audio_listener_2d = false;
  2320. transparent_bg = false;
  2321. parent = NULL;
  2322. listener = NULL;
  2323. camera = NULL;
  2324. canvas_layers.insert(NULL); // This eases picking code (interpreted as the canvas of the Viewport)
  2325. arvr = false;
  2326. size_override = false;
  2327. size_override_stretch = false;
  2328. size_override_size = Size2(1, 1);
  2329. gen_mipmaps = false;
  2330. vflip = false;
  2331. //clear=true;
  2332. update_mode = UPDATE_WHEN_VISIBLE;
  2333. physics_object_picking = false;
  2334. physics_object_capture = 0;
  2335. physics_object_over = 0;
  2336. physics_has_last_mousepos = false;
  2337. physics_last_mousepos = Vector2(Math_INF, Math_INF);
  2338. shadow_atlas_size = 0;
  2339. for (int i = 0; i < 4; i++) {
  2340. shadow_atlas_quadrant_subdiv[i] = SHADOW_ATLAS_QUADRANT_SUBDIV_MAX;
  2341. }
  2342. set_shadow_atlas_quadrant_subdiv(0, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  2343. set_shadow_atlas_quadrant_subdiv(1, SHADOW_ATLAS_QUADRANT_SUBDIV_4);
  2344. set_shadow_atlas_quadrant_subdiv(2, SHADOW_ATLAS_QUADRANT_SUBDIV_16);
  2345. set_shadow_atlas_quadrant_subdiv(3, SHADOW_ATLAS_QUADRANT_SUBDIV_64);
  2346. String id = itos(get_instance_id());
  2347. input_group = "_vp_input" + id;
  2348. gui_input_group = "_vp_gui_input" + id;
  2349. unhandled_input_group = "_vp_unhandled_input" + id;
  2350. unhandled_key_input_group = "_vp_unhandled_key_input" + id;
  2351. disable_input = false;
  2352. disable_3d = false;
  2353. keep_3d_linear = false;
  2354. //window tooltip
  2355. gui.tooltip_timer = -1;
  2356. //gui.tooltip_timer->force_parent_owned();
  2357. gui.tooltip_delay = GLOBAL_DEF("gui/timers/tooltip_delay_sec", 0.7);
  2358. 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
  2359. gui.tooltip = NULL;
  2360. gui.tooltip_label = NULL;
  2361. gui.drag_preview = NULL;
  2362. gui.drag_attempted = false;
  2363. gui.canvas_sort_index = 0;
  2364. gui.roots_order_dirty = false;
  2365. gui.mouse_focus = NULL;
  2366. gui.last_mouse_focus = NULL;
  2367. msaa = MSAA_DISABLED;
  2368. hdr = true;
  2369. usage = USAGE_3D;
  2370. debug_draw = DEBUG_DRAW_DISABLED;
  2371. clear_mode = CLEAR_MODE_ALWAYS;
  2372. snap_controls_to_pixels = true;
  2373. physics_last_mouse_state.alt = false;
  2374. physics_last_mouse_state.control = false;
  2375. physics_last_mouse_state.shift = false;
  2376. physics_last_mouse_state.meta = false;
  2377. physics_last_mouse_state.mouse_mask = 0;
  2378. local_input_handled = false;
  2379. handle_input_locally = true;
  2380. physics_last_id = 0; //ensures first time there will be a check
  2381. }
  2382. Viewport::~Viewport() {
  2383. //erase itself from viewport textures
  2384. for (Set<ViewportTexture *>::Element *E = viewport_textures.front(); E; E = E->next()) {
  2385. E->get()->vp = NULL;
  2386. }
  2387. VisualServer::get_singleton()->free(viewport);
  2388. //SpatialSoundServer::get_singleton()->free(internal_listener);
  2389. //SpatialSound2DServer::get_singleton()->free(internal_listener_2d);
  2390. }