viewport.cpp 90 KB

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