viewport.cpp 79 KB

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