viewport.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575
  1. /*************************************************************************/
  2. /* viewport.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "viewport.h"
  30. #include "os/os.h"
  31. #include "scene/3d/spatial.h"
  32. #include "os/input.h"
  33. #include "servers/physics_2d_server.h"
  34. //#include "scene/3d/camera.h"
  35. #include "servers/spatial_sound_server.h"
  36. #include "servers/spatial_sound_2d_server.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/3d/camera.h"
  39. #include "scene/resources/mesh.h"
  40. #include "scene/3d/spatial_indexer.h"
  41. #include "scene/3d/collision_object.h"
  42. #include "scene/2d/collision_object_2d.h"
  43. #include "scene/gui/panel.h"
  44. #include "scene/gui/label.h"
  45. #include "scene/main/timer.h"
  46. #include "scene/scene_string_names.h"
  47. #include "globals.h"
  48. int RenderTargetTexture::get_width() const {
  49. ERR_FAIL_COND_V(!vp,0);
  50. return vp->rect.size.width;
  51. }
  52. int RenderTargetTexture::get_height() const{
  53. ERR_FAIL_COND_V(!vp,0);
  54. return vp->rect.size.height;
  55. }
  56. Size2 RenderTargetTexture::get_size() const{
  57. ERR_FAIL_COND_V(!vp,Size2());
  58. return vp->rect.size;
  59. }
  60. RID RenderTargetTexture::get_rid() const{
  61. ERR_FAIL_COND_V(!vp,RID());
  62. return vp->render_target_texture_rid;
  63. }
  64. bool RenderTargetTexture::has_alpha() const{
  65. return false;
  66. }
  67. void RenderTargetTexture::set_flags(uint32_t p_flags){
  68. ERR_FAIL_COND(!vp);
  69. if (p_flags&FLAG_FILTER)
  70. flags=FLAG_FILTER;
  71. else
  72. flags=0;
  73. VS::get_singleton()->texture_set_flags(vp->render_target_texture_rid,flags);
  74. }
  75. uint32_t RenderTargetTexture::get_flags() const{
  76. return flags;
  77. }
  78. RenderTargetTexture::RenderTargetTexture(Viewport *p_vp){
  79. vp=p_vp;
  80. flags=0;
  81. }
  82. /////////////////////////////////////
  83. class TooltipPanel : public Panel {
  84. OBJ_TYPE(TooltipPanel,Panel)
  85. public:
  86. TooltipPanel() {};
  87. };
  88. class TooltipLabel : public Label {
  89. OBJ_TYPE(TooltipLabel,Label)
  90. public:
  91. TooltipLabel() {};
  92. };
  93. Viewport::GUI::GUI() {
  94. mouse_focus=NULL;
  95. mouse_focus_button=-1;
  96. key_focus=NULL;
  97. mouse_over=NULL;
  98. cancelled_input_ID=0;
  99. tooltip=NULL;
  100. tooltip_popup=NULL;
  101. tooltip_label=NULL;
  102. subwindow_order_dirty=false;
  103. }
  104. /////////////////////////////////////
  105. void Viewport::_update_stretch_transform() {
  106. if (size_override_stretch && size_override) {
  107. //print_line("sive override size "+size_override_size);
  108. //print_line("rect size "+rect.size);
  109. stretch_transform=Matrix32();
  110. Size2 scale = rect.size/(size_override_size+size_override_margin*2);
  111. stretch_transform.scale(scale);
  112. stretch_transform.elements[2]=size_override_margin*scale;
  113. } else {
  114. stretch_transform=Matrix32();
  115. }
  116. _update_global_transform();
  117. }
  118. void Viewport::_update_rect() {
  119. if (!is_inside_tree())
  120. return;
  121. if (!render_target && parent_control) {
  122. Control *c = parent_control;
  123. rect.pos=Point2();
  124. rect.size=c->get_size();
  125. }
  126. VisualServer::ViewportRect vr;
  127. vr.x=rect.pos.x;
  128. vr.y=rect.pos.y;
  129. if (render_target) {
  130. vr.x=0;
  131. vr.y=0;
  132. }
  133. vr.width=rect.size.width;
  134. vr.height=rect.size.height;
  135. VisualServer::get_singleton()->viewport_set_rect(viewport,vr);
  136. last_vp_rect=rect;
  137. if (canvas_item.is_valid()) {
  138. VisualServer::get_singleton()->canvas_item_set_custom_rect(canvas_item,true,rect);
  139. }
  140. emit_signal("size_changed");
  141. render_target_texture->emit_changed();
  142. }
  143. void Viewport::_parent_resized() {
  144. _update_rect();
  145. }
  146. void Viewport::_parent_draw() {
  147. }
  148. void Viewport::_parent_visibility_changed() {
  149. if (parent_control) {
  150. Control *c = parent_control;
  151. VisualServer::get_singleton()->canvas_item_set_visible(canvas_item,c->is_visible());
  152. _update_listener();
  153. _update_listener_2d();
  154. }
  155. }
  156. void Viewport::_vp_enter_tree() {
  157. if (parent_control) {
  158. Control *cparent=parent_control;
  159. RID parent_ci = cparent->get_canvas_item();
  160. ERR_FAIL_COND(!parent_ci.is_valid());
  161. canvas_item = VisualServer::get_singleton()->canvas_item_create();
  162. VisualServer::get_singleton()->canvas_item_set_parent(canvas_item,parent_ci);
  163. VisualServer::get_singleton()->canvas_item_set_visible(canvas_item,false);
  164. VisualServer::get_singleton()->canvas_item_attach_viewport(canvas_item,viewport);
  165. parent_control->connect("resized",this,"_parent_resized");
  166. parent_control->connect("visibility_changed",this,"_parent_visibility_changed");
  167. } else if (!parent){
  168. VisualServer::get_singleton()->viewport_attach_to_screen(viewport,0);
  169. }
  170. }
  171. void Viewport::_vp_exit_tree() {
  172. if (parent_control) {
  173. parent_control->disconnect("resized",this,"_parent_resized");
  174. }
  175. if (parent_control) {
  176. parent_control->disconnect("visibility_changed",this,"_parent_visibility_changed");
  177. }
  178. if (canvas_item.is_valid()) {
  179. VisualServer::get_singleton()->free(canvas_item);
  180. canvas_item=RID();
  181. }
  182. if (!parent) {
  183. VisualServer::get_singleton()->viewport_detach(viewport);
  184. }
  185. }
  186. void Viewport::update_worlds() {
  187. if (!is_inside_tree())
  188. return;
  189. Rect2 xformed_rect = (global_canvas_transform * canvas_transform).affine_inverse().xform(get_visible_rect());
  190. find_world_2d()->_update_viewport(this,xformed_rect);
  191. find_world_2d()->_update();
  192. find_world()->_update(get_tree()->get_frame());
  193. }
  194. void Viewport::_test_new_mouseover(ObjectID new_collider) {
  195. #ifndef _3D_DISABLED
  196. if (new_collider!=physics_object_over) {
  197. if (physics_object_over) {
  198. Object *obj = ObjectDB::get_instance(physics_object_over);
  199. if (obj) {
  200. CollisionObject *co = obj->cast_to<CollisionObject>();
  201. if (co) {
  202. co->_mouse_exit();
  203. }
  204. }
  205. }
  206. if (new_collider) {
  207. Object *obj = ObjectDB::get_instance(new_collider);
  208. if (obj) {
  209. CollisionObject *co = obj->cast_to<CollisionObject>();
  210. if (co) {
  211. co->_mouse_enter();
  212. }
  213. }
  214. }
  215. physics_object_over=new_collider;
  216. }
  217. #endif
  218. }
  219. void Viewport::_notification(int p_what) {
  220. switch( p_what ) {
  221. case NOTIFICATION_ENTER_TREE: {
  222. if (get_parent()) {
  223. Node *parent=get_parent();
  224. if (parent) {
  225. parent_control=parent->cast_to<Control>();
  226. }
  227. }
  228. parent=NULL;
  229. Node *parent_node=get_parent();
  230. while(parent_node) {
  231. parent = parent_node->cast_to<Viewport>();
  232. if (parent)
  233. break;
  234. parent_node=parent_node->get_parent();
  235. }
  236. if (!render_target)
  237. _vp_enter_tree();
  238. current_canvas=find_world_2d()->get_canvas();
  239. VisualServer::get_singleton()->viewport_set_scenario(viewport,find_world()->get_scenario());
  240. VisualServer::get_singleton()->viewport_attach_canvas(viewport,current_canvas);
  241. _update_listener();
  242. _update_listener_2d();
  243. _update_rect();
  244. if (world_2d.is_valid()) {
  245. find_world_2d()->_register_viewport(this,Rect2());
  246. //best to defer this and not do it here, as it can annoy a lot of setup logic if user
  247. //adds a node and then moves it, will get enter/exit screen/viewport notifications
  248. //unnecesarily
  249. // update_worlds();
  250. }
  251. add_to_group("_viewports");
  252. if (get_tree()->is_debugging_collisions_hint()) {
  253. //2D
  254. Physics2DServer::get_singleton()->space_set_debug_contacts(find_world_2d()->get_space(),get_tree()->get_collision_debug_contact_count());
  255. contact_2d_debug=VisualServer::get_singleton()->canvas_item_create();
  256. VisualServer::get_singleton()->canvas_item_set_parent(contact_2d_debug,find_world_2d()->get_canvas());
  257. //3D
  258. PhysicsServer::get_singleton()->space_set_debug_contacts(find_world()->get_space(),get_tree()->get_collision_debug_contact_count());
  259. contact_3d_debug_multimesh=VisualServer::get_singleton()->multimesh_create();
  260. VisualServer::get_singleton()->multimesh_set_instance_count(contact_3d_debug_multimesh,get_tree()->get_collision_debug_contact_count());
  261. VisualServer::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh,0);
  262. VisualServer::get_singleton()->multimesh_set_mesh(contact_3d_debug_multimesh,get_tree()->get_debug_contact_mesh()->get_rid());
  263. contact_3d_debug_instance=VisualServer::get_singleton()->instance_create();
  264. VisualServer::get_singleton()->instance_set_base(contact_3d_debug_instance,contact_3d_debug_multimesh);
  265. VisualServer::get_singleton()->instance_set_scenario(contact_3d_debug_instance,find_world()->get_scenario());
  266. VisualServer::get_singleton()->instance_geometry_set_flag(contact_3d_debug_instance,VS::INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS,true);
  267. }
  268. } break;
  269. case NOTIFICATION_READY: {
  270. #ifndef _3D_DISABLED
  271. if (cameras.size() && !camera) {
  272. //there are cameras but no current camera, pick first in tree and make it current
  273. Camera *first=NULL;
  274. for(Set<Camera*>::Element *E=cameras.front();E;E=E->next()) {
  275. if (first==NULL || first->is_greater_than(E->get())) {
  276. first=E->get();
  277. }
  278. }
  279. if (first)
  280. first->make_current();
  281. }
  282. #endif
  283. } break;
  284. case NOTIFICATION_EXIT_TREE: {
  285. _gui_cancel_tooltip();
  286. if (world_2d.is_valid())
  287. world_2d->_remove_viewport(this);
  288. if (!render_target)
  289. _vp_exit_tree();
  290. VisualServer::get_singleton()->viewport_set_scenario(viewport,RID());
  291. SpatialSoundServer::get_singleton()->listener_set_space(listener,RID());
  292. VisualServer::get_singleton()->viewport_remove_canvas(viewport,current_canvas);
  293. if (contact_2d_debug.is_valid()) {
  294. VisualServer::get_singleton()->free(contact_2d_debug);
  295. contact_2d_debug=RID();
  296. }
  297. if (contact_3d_debug_multimesh.is_valid()) {
  298. VisualServer::get_singleton()->free(contact_3d_debug_multimesh);
  299. VisualServer::get_singleton()->free(contact_3d_debug_instance);
  300. contact_3d_debug_instance=RID();
  301. contact_3d_debug_multimesh=RID();
  302. }
  303. remove_from_group("_viewports");
  304. parent_control=NULL;
  305. } break;
  306. case NOTIFICATION_FIXED_PROCESS: {
  307. if (gui.tooltip_timer>=0) {
  308. gui.tooltip_timer-=get_fixed_process_delta_time();
  309. if (gui.tooltip_timer<0) {
  310. _gui_show_tooltip();
  311. }
  312. }
  313. if (get_tree()->is_debugging_collisions_hint() && contact_2d_debug.is_valid()) {
  314. VisualServer::get_singleton()->canvas_item_clear(contact_2d_debug);
  315. VisualServer::get_singleton()->canvas_item_raise(contact_2d_debug);
  316. Vector<Vector2> points = Physics2DServer::get_singleton()->space_get_contacts(find_world_2d()->get_space());
  317. int point_count = Physics2DServer::get_singleton()->space_get_contact_count(find_world_2d()->get_space());
  318. Color ccol = get_tree()->get_debug_collision_contact_color();
  319. for(int i=0;i<point_count;i++) {
  320. VisualServer::get_singleton()->canvas_item_add_rect(contact_2d_debug,Rect2(points[i]-Vector2(2,2),Vector2(5,5)),ccol);
  321. }
  322. }
  323. if (get_tree()->is_debugging_collisions_hint() && contact_3d_debug_multimesh.is_valid()) {
  324. Vector<Vector3> points = PhysicsServer::get_singleton()->space_get_contacts(find_world()->get_space());
  325. int point_count = PhysicsServer::get_singleton()->space_get_contact_count(find_world()->get_space());
  326. VS::get_singleton()->multimesh_set_visible_instances(contact_3d_debug_multimesh,point_count);
  327. if (point_count>0) {
  328. AABB aabb;
  329. Transform t;
  330. for(int i=0;i<point_count;i++) {
  331. if (i==0)
  332. aabb.pos=points[i];
  333. else
  334. aabb.expand_to(points[i]);
  335. t.origin=points[i];
  336. VisualServer::get_singleton()->multimesh_instance_set_transform(contact_3d_debug_multimesh,i,t);
  337. }
  338. aabb.grow(aabb.get_longest_axis_size()*0.01);
  339. VisualServer::get_singleton()->multimesh_set_aabb(contact_3d_debug_multimesh,aabb);
  340. }
  341. }
  342. if (physics_object_picking && (render_target || Input::get_singleton()->get_mouse_mode()!=Input::MOUSE_MODE_CAPTURED)) {
  343. Vector2 last_pos(1e20,1e20);
  344. CollisionObject *last_object;
  345. ObjectID last_id=0;
  346. PhysicsDirectSpaceState::RayResult result;
  347. Physics2DDirectSpaceState *ss2d=Physics2DServer::get_singleton()->space_get_direct_state(find_world_2d()->get_space());
  348. bool motion_tested=false;
  349. while(physics_picking_events.size()) {
  350. InputEvent ev = physics_picking_events.front()->get();
  351. physics_picking_events.pop_front();
  352. Vector2 pos;
  353. switch(ev.type) {
  354. case InputEvent::MOUSE_MOTION: {
  355. pos.x=ev.mouse_motion.x;
  356. pos.y=ev.mouse_motion.y;
  357. motion_tested=true;
  358. physics_last_mousepos=pos;
  359. } break;
  360. case InputEvent::MOUSE_BUTTON: {
  361. pos.x=ev.mouse_button.x;
  362. pos.y=ev.mouse_button.y;
  363. } break;
  364. case InputEvent::SCREEN_DRAG: {
  365. pos.x=ev.screen_drag.x;
  366. pos.y=ev.screen_drag.y;
  367. } break;
  368. case InputEvent::SCREEN_TOUCH: {
  369. pos.x=ev.screen_touch.x;
  370. pos.y=ev.screen_touch.y;
  371. } break;
  372. }
  373. if (ss2d) {
  374. //send to 2D
  375. uint64_t frame = get_tree()->get_frame();
  376. Vector2 point = get_canvas_transform().affine_inverse().xform(pos);
  377. Physics2DDirectSpaceState::ShapeResult res[64];
  378. int rc = ss2d->intersect_point(point,res,64,Set<RID>(),0xFFFFFFFF,0xFFFFFFFF,true);
  379. for(int i=0;i<rc;i++) {
  380. if (res[i].collider_id && res[i].collider) {
  381. CollisionObject2D *co=res[i].collider->cast_to<CollisionObject2D>();
  382. if (co) {
  383. Map<ObjectID,uint64_t>::Element *E=physics_2d_mouseover.find(res[i].collider_id);
  384. if (!E) {
  385. E=physics_2d_mouseover.insert(res[i].collider_id,frame);
  386. co->_mouse_enter();
  387. } else {
  388. E->get()=frame;
  389. }
  390. co->_input_event(this,ev,res[i].shape);
  391. }
  392. }
  393. }
  394. List<Map<ObjectID,uint64_t>::Element*> to_erase;
  395. for (Map<ObjectID,uint64_t>::Element*E=physics_2d_mouseover.front();E;E=E->next()) {
  396. if (E->get()!=frame) {
  397. Object *o=ObjectDB::get_instance(E->key());
  398. if (o) {
  399. CollisionObject2D *co=o->cast_to<CollisionObject2D>();
  400. if (co) {
  401. co->_mouse_exit();
  402. }
  403. }
  404. to_erase.push_back(E);
  405. }
  406. }
  407. while(to_erase.size()) {
  408. physics_2d_mouseover.erase(to_erase.front()->get());
  409. to_erase.pop_front();
  410. }
  411. }
  412. #ifndef _3D_DISABLED
  413. bool captured=false;
  414. if (physics_object_capture!=0) {
  415. Object *obj = ObjectDB::get_instance(physics_object_capture);
  416. if (obj) {
  417. CollisionObject *co = obj->cast_to<CollisionObject>();
  418. if (co) {
  419. co->_input_event(camera,ev,Vector3(),Vector3(),0);
  420. captured=true;
  421. if (ev.type==InputEvent::MOUSE_BUTTON && ev.mouse_button.button_index==1 && !ev.mouse_button.pressed) {
  422. physics_object_capture=0;
  423. }
  424. } else {
  425. physics_object_capture=0;
  426. }
  427. } else {
  428. physics_object_capture=0;
  429. }
  430. }
  431. if (captured) {
  432. //none
  433. } else if (pos==last_pos) {
  434. if (last_id) {
  435. if (ObjectDB::get_instance(last_id)) {
  436. //good, exists
  437. last_object->_input_event(camera,ev,result.position,result.normal,result.shape);
  438. if (last_object->get_capture_input_on_drag() && ev.type==InputEvent::MOUSE_BUTTON && ev.mouse_button.button_index==1 && ev.mouse_button.pressed) {
  439. physics_object_capture=last_id;
  440. }
  441. }
  442. }
  443. } else {
  444. if (camera) {
  445. Vector3 from = camera->project_ray_origin(pos);
  446. Vector3 dir = camera->project_ray_normal(pos);
  447. PhysicsDirectSpaceState *space = PhysicsServer::get_singleton()->space_get_direct_state(find_world()->get_space());
  448. if (space) {
  449. bool col = space->intersect_ray(from,from+dir*10000,result,Set<RID>(),0xFFFFFFFF,0xFFFFFFFF,true);
  450. ObjectID new_collider=0;
  451. if (col) {
  452. if (result.collider) {
  453. CollisionObject *co = result.collider->cast_to<CollisionObject>();
  454. if (co) {
  455. co->_input_event(camera,ev,result.position,result.normal,result.shape);
  456. last_object=co;
  457. last_id=result.collider_id;
  458. new_collider=last_id;
  459. if (co->get_capture_input_on_drag() && ev.type==InputEvent::MOUSE_BUTTON && ev.mouse_button.button_index==1 && ev.mouse_button.pressed) {
  460. physics_object_capture=last_id;
  461. }
  462. }
  463. }
  464. }
  465. if (ev.type==InputEvent::MOUSE_MOTION) {
  466. _test_new_mouseover(new_collider);
  467. }
  468. }
  469. last_pos=pos;
  470. }
  471. }
  472. }
  473. if (!motion_tested && camera && physics_last_mousepos!=Vector2(1e20,1e20)) {
  474. //test anyway for mouseenter/exit because objects might move
  475. Vector3 from = camera->project_ray_origin(physics_last_mousepos);
  476. Vector3 dir = camera->project_ray_normal(physics_last_mousepos);
  477. PhysicsDirectSpaceState *space = PhysicsServer::get_singleton()->space_get_direct_state(find_world()->get_space());
  478. if (space) {
  479. bool col = space->intersect_ray(from,from+dir*10000,result,Set<RID>(),0xFFFFFFFF,0xFFFFFFFF,true);
  480. ObjectID new_collider=0;
  481. if (col) {
  482. if (result.collider) {
  483. CollisionObject *co = result.collider->cast_to<CollisionObject>();
  484. if (co) {
  485. new_collider=result.collider_id;
  486. }
  487. }
  488. }
  489. _test_new_mouseover(new_collider);
  490. }
  491. #endif
  492. }
  493. }
  494. } break;
  495. }
  496. }
  497. RID Viewport::get_viewport() const {
  498. return viewport;
  499. }
  500. void Viewport::set_rect(const Rect2& p_rect) {
  501. if (rect==p_rect)
  502. return;
  503. rect=p_rect;
  504. _update_rect();
  505. _update_stretch_transform();
  506. }
  507. Rect2 Viewport::get_visible_rect() const {
  508. Rect2 r;
  509. if (rect.pos==Vector2() && rect.size==Size2()) {
  510. r=Rect2( Point2(), Size2( OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height ) );
  511. } else {
  512. r=Rect2( rect.pos , rect.size );
  513. }
  514. if (size_override) {
  515. r.size=size_override_size;
  516. }
  517. return r;
  518. }
  519. Rect2 Viewport::get_rect() const {
  520. return rect;
  521. }
  522. void Viewport::_update_listener() {
  523. if (is_inside_tree() && audio_listener && camera && (!get_parent() || (get_parent()->cast_to<Control>() && get_parent()->cast_to<Control>()->is_visible()))) {
  524. SpatialSoundServer::get_singleton()->listener_set_space(listener,find_world()->get_sound_space());
  525. } else {
  526. SpatialSoundServer::get_singleton()->listener_set_space(listener,RID());
  527. }
  528. }
  529. void Viewport::_update_listener_2d() {
  530. if (is_inside_tree() && audio_listener && (!get_parent() || (get_parent()->cast_to<Control>() && get_parent()->cast_to<Control>()->is_visible())))
  531. SpatialSound2DServer::get_singleton()->listener_set_space(listener_2d,find_world_2d()->get_sound_space());
  532. else
  533. SpatialSound2DServer::get_singleton()->listener_set_space(listener_2d,RID());
  534. }
  535. void Viewport::set_as_audio_listener(bool p_enable) {
  536. if (p_enable==audio_listener)
  537. return;
  538. audio_listener=p_enable;
  539. _update_listener();
  540. }
  541. bool Viewport::is_audio_listener() const {
  542. return audio_listener;
  543. }
  544. void Viewport::set_as_audio_listener_2d(bool p_enable) {
  545. if (p_enable==audio_listener_2d)
  546. return;
  547. audio_listener_2d=p_enable;
  548. _update_listener_2d();
  549. }
  550. bool Viewport::is_audio_listener_2d() const {
  551. return audio_listener_2d;
  552. }
  553. void Viewport::set_canvas_transform(const Matrix32& p_transform) {
  554. canvas_transform=p_transform;
  555. VisualServer::get_singleton()->viewport_set_canvas_transform(viewport,find_world_2d()->get_canvas(),canvas_transform);
  556. Matrix32 xform = (global_canvas_transform * canvas_transform).affine_inverse();
  557. Size2 ss = get_visible_rect().size;
  558. SpatialSound2DServer::get_singleton()->listener_set_transform(listener_2d,Matrix32(0,xform.xform(ss*0.5)));
  559. Vector2 ss2 = ss*xform.get_scale();
  560. float panrange = MAX(ss2.x,ss2.y);
  561. SpatialSound2DServer::get_singleton()->listener_set_param(listener_2d,SpatialSound2DServer::LISTENER_PARAM_PAN_RANGE,panrange);
  562. }
  563. Matrix32 Viewport::get_canvas_transform() const{
  564. return canvas_transform;
  565. }
  566. void Viewport::_update_global_transform() {
  567. Matrix32 sxform = stretch_transform * global_canvas_transform;
  568. VisualServer::get_singleton()->viewport_set_global_canvas_transform(viewport,sxform);
  569. Matrix32 xform = (sxform * canvas_transform).affine_inverse();
  570. Size2 ss = get_visible_rect().size;
  571. SpatialSound2DServer::get_singleton()->listener_set_transform(listener_2d,Matrix32(0,xform.xform(ss*0.5)));
  572. Vector2 ss2 = ss*xform.get_scale();
  573. float panrange = MAX(ss2.x,ss2.y);
  574. SpatialSound2DServer::get_singleton()->listener_set_param(listener_2d,SpatialSound2DServer::LISTENER_PARAM_PAN_RANGE,panrange);
  575. }
  576. void Viewport::set_global_canvas_transform(const Matrix32& p_transform) {
  577. global_canvas_transform=p_transform;
  578. _update_global_transform();
  579. }
  580. Matrix32 Viewport::get_global_canvas_transform() const{
  581. return global_canvas_transform;
  582. }
  583. void Viewport::_camera_transform_changed_notify() {
  584. #ifndef _3D_DISABLED
  585. if (camera)
  586. SpatialSoundServer::get_singleton()->listener_set_transform(listener,camera->get_camera_transform());
  587. #endif
  588. }
  589. void Viewport::_camera_set(Camera* p_camera) {
  590. #ifndef _3D_DISABLED
  591. if (camera==p_camera)
  592. return;
  593. if (camera && find_world().is_valid()) {
  594. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  595. }
  596. camera=p_camera;
  597. if (camera)
  598. VisualServer::get_singleton()->viewport_attach_camera(viewport,camera->get_camera());
  599. else
  600. VisualServer::get_singleton()->viewport_attach_camera(viewport,RID());
  601. if (camera && find_world().is_valid()) {
  602. camera->notification(Camera::NOTIFICATION_BECAME_CURRENT);
  603. }
  604. _update_listener();
  605. _camera_transform_changed_notify();
  606. #endif
  607. }
  608. bool Viewport::_camera_add(Camera* p_camera) {
  609. cameras.insert(p_camera);
  610. return cameras.size()==1;
  611. }
  612. void Viewport::_camera_remove(Camera* p_camera) {
  613. cameras.erase(p_camera);
  614. if (camera==p_camera) {
  615. camera=NULL;
  616. }
  617. }
  618. #ifndef _3D_DISABLED
  619. void Viewport::_camera_make_next_current(Camera* p_exclude) {
  620. for(Set<Camera*>::Element *E=cameras.front();E;E=E->next()) {
  621. if (p_exclude==E->get())
  622. continue;
  623. if (!E->get()->is_inside_tree())
  624. continue;
  625. if (camera!=NULL)
  626. return;
  627. E->get()->make_current();
  628. }
  629. }
  630. #endif
  631. void Viewport::set_transparent_background(bool p_enable) {
  632. transparent_bg=p_enable;
  633. VS::get_singleton()->viewport_set_transparent_background(viewport,p_enable);
  634. }
  635. bool Viewport::has_transparent_background() const {
  636. return transparent_bg;
  637. }
  638. #if 0
  639. void Viewport::set_world_2d(const Ref<World2D>& p_world_2d) {
  640. world_2d=p_world_2d;
  641. _update_listener_2d();
  642. if (is_inside_scene()) {
  643. if (current_canvas.is_valid())
  644. VisualServer::get_singleton()->viewport_remove_canvas(viewport,current_canvas);
  645. current_canvas=find_world_2d()->get_canvas();
  646. VisualServer::get_singleton()->viewport_attach_canvas(viewport,current_canvas);
  647. }
  648. }
  649. Ref<World2D> Viewport::find_world_2d() const{
  650. if (world_2d.is_valid())
  651. return world_2d;
  652. else if (parent)
  653. return parent->find_world_2d();
  654. else
  655. return Ref<World2D>();
  656. }
  657. #endif
  658. Ref<World2D> Viewport::find_world_2d() const{
  659. return world_2d;
  660. }
  661. void Viewport::_propagate_enter_world(Node *p_node) {
  662. if (p_node!=this) {
  663. if (!p_node->is_inside_tree()) //may not have entered scene yet
  664. return;
  665. Spatial *s = p_node->cast_to<Spatial>();
  666. if (s) {
  667. s->notification(Spatial::NOTIFICATION_ENTER_WORLD);
  668. } else {
  669. Viewport *v = p_node->cast_to<Viewport>();
  670. if (v) {
  671. if (v->world.is_valid())
  672. return;
  673. }
  674. }
  675. }
  676. for(int i=0;i<p_node->get_child_count();i++) {
  677. _propagate_enter_world(p_node->get_child(i));
  678. }
  679. }
  680. void Viewport::_propagate_exit_world(Node *p_node) {
  681. if (p_node!=this) {
  682. if (!p_node->is_inside_tree()) //may have exited scene already
  683. return;
  684. Spatial *s = p_node->cast_to<Spatial>();
  685. if (s) {
  686. s->notification(Spatial::NOTIFICATION_EXIT_WORLD, true);
  687. } else {
  688. Viewport *v = p_node->cast_to<Viewport>();
  689. if (v) {
  690. if (v->world.is_valid())
  691. return;
  692. }
  693. }
  694. }
  695. for(int i=0;i<p_node->get_child_count();i++) {
  696. _propagate_exit_world(p_node->get_child(i));
  697. }
  698. }
  699. void Viewport::set_world(const Ref<World>& p_world) {
  700. if (world==p_world)
  701. return;
  702. if (is_inside_tree())
  703. _propagate_exit_world(this);
  704. #ifndef _3D_DISABLED
  705. if (find_world().is_valid() && camera)
  706. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  707. #endif
  708. world=p_world;
  709. if (is_inside_tree())
  710. _propagate_enter_world(this);
  711. #ifndef _3D_DISABLED
  712. if (find_world().is_valid() && camera)
  713. camera->notification(Camera::NOTIFICATION_BECAME_CURRENT);
  714. #endif
  715. //propagate exit
  716. if (is_inside_tree()) {
  717. VisualServer::get_singleton()->viewport_set_scenario(viewport,find_world()->get_scenario());
  718. }
  719. _update_listener();
  720. }
  721. Ref<World> Viewport::get_world() const{
  722. return world;
  723. }
  724. Ref<World> Viewport::find_world() const{
  725. if (own_world.is_valid())
  726. return own_world;
  727. else if (world.is_valid())
  728. return world;
  729. else if (parent)
  730. return parent->find_world();
  731. else
  732. return Ref<World>();
  733. }
  734. Camera* Viewport::get_camera() const {
  735. return camera;
  736. }
  737. Matrix32 Viewport::get_final_transform() const {
  738. return stretch_transform * global_canvas_transform;
  739. }
  740. void Viewport::set_size_override(bool p_enable, const Size2& p_size, const Vector2 &p_margin) {
  741. if (size_override==p_enable && p_size==size_override_size)
  742. return;
  743. size_override=p_enable;
  744. if (p_size.x>=0 || p_size.y>=0) {
  745. size_override_size=p_size;
  746. }
  747. size_override_margin=p_margin;
  748. _update_rect();
  749. _update_stretch_transform();
  750. }
  751. Size2 Viewport::get_size_override() const {
  752. return size_override_size;
  753. }
  754. bool Viewport::is_size_override_enabled() const {
  755. return size_override;
  756. }
  757. void Viewport::set_size_override_stretch(bool p_enable) {
  758. if (p_enable==size_override_stretch)
  759. return;
  760. size_override_stretch=p_enable;
  761. if (size_override) {
  762. _update_rect();
  763. }
  764. _update_stretch_transform();
  765. }
  766. bool Viewport::is_size_override_stretch_enabled() const {
  767. return size_override_stretch;
  768. }
  769. void Viewport::set_as_render_target(bool p_enable){
  770. if (render_target==p_enable)
  771. return;
  772. render_target=p_enable;
  773. VS::get_singleton()->viewport_set_as_render_target(viewport,p_enable);
  774. if (is_inside_tree()) {
  775. if (p_enable)
  776. _vp_exit_tree();
  777. else
  778. _vp_enter_tree();
  779. }
  780. if (p_enable) {
  781. render_target_texture_rid = VS::get_singleton()->viewport_get_render_target_texture(viewport);
  782. } else {
  783. render_target_texture_rid=RID();
  784. }
  785. render_target_texture->set_flags(render_target_texture->flags);
  786. render_target_texture->emit_changed();
  787. }
  788. bool Viewport::is_set_as_render_target() const{
  789. return render_target;
  790. }
  791. void Viewport::set_render_target_update_mode(RenderTargetUpdateMode p_mode){
  792. render_target_update_mode=p_mode;
  793. VS::get_singleton()->viewport_set_render_target_update_mode(viewport,VS::RenderTargetUpdateMode(p_mode));
  794. }
  795. Viewport::RenderTargetUpdateMode Viewport::get_render_target_update_mode() const{
  796. return render_target_update_mode;
  797. }
  798. //RID get_render_target_texture() const;
  799. void Viewport::queue_screen_capture(){
  800. VS::get_singleton()->viewport_queue_screen_capture(viewport);
  801. }
  802. Image Viewport::get_screen_capture() const {
  803. return VS::get_singleton()->viewport_get_screen_capture(viewport);
  804. }
  805. Ref<RenderTargetTexture> Viewport::get_render_target_texture() const {
  806. return render_target_texture;
  807. }
  808. void Viewport::set_render_target_vflip(bool p_enable) {
  809. render_target_vflip=p_enable;
  810. VisualServer::get_singleton()->viewport_set_render_target_vflip(viewport,p_enable);
  811. }
  812. bool Viewport::get_render_target_vflip() const{
  813. return render_target_vflip;
  814. }
  815. void Viewport::set_render_target_clear_on_new_frame(bool p_enable) {
  816. render_target_clear_on_new_frame=p_enable;
  817. VisualServer::get_singleton()->viewport_set_render_target_clear_on_new_frame(viewport,p_enable);
  818. }
  819. bool Viewport::get_render_target_clear_on_new_frame() const{
  820. return render_target_clear_on_new_frame;
  821. }
  822. void Viewport::render_target_clear() {
  823. //render_target_clear=true;
  824. VisualServer::get_singleton()->viewport_render_target_clear(viewport);
  825. }
  826. void Viewport::set_render_target_filter(bool p_enable) {
  827. render_target_texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0));
  828. }
  829. bool Viewport::get_render_target_filter() const{
  830. return (render_target_texture->get_flags()&Texture::FLAG_FILTER)!=0;
  831. }
  832. void Viewport::set_render_target_gen_mipmaps(bool p_enable) {
  833. //render_target_texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0));
  834. render_target_gen_mipmaps=p_enable;
  835. }
  836. bool Viewport::get_render_target_gen_mipmaps() const{
  837. //return (render_target_texture->get_flags()&Texture::FLAG_FILTER)!=0;
  838. return render_target_gen_mipmaps;
  839. }
  840. Matrix32 Viewport::_get_input_pre_xform() const {
  841. Matrix32 pre_xf;
  842. if (render_target) {
  843. ERR_FAIL_COND_V(to_screen_rect.size.x==0,pre_xf);
  844. ERR_FAIL_COND_V(to_screen_rect.size.y==0,pre_xf);
  845. pre_xf.elements[2]=-to_screen_rect.pos;
  846. pre_xf.scale(rect.size/to_screen_rect.size);
  847. } else {
  848. pre_xf.elements[2]=-rect.pos;
  849. }
  850. return pre_xf;
  851. }
  852. void Viewport::_make_input_local(InputEvent& ev) {
  853. switch(ev.type) {
  854. case InputEvent::MOUSE_BUTTON: {
  855. Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
  856. Vector2 g = ai.xform(Vector2(ev.mouse_button.global_x,ev.mouse_button.global_y));
  857. Vector2 l = ai.xform(Vector2(ev.mouse_button.x,ev.mouse_button.y));
  858. ev.mouse_button.x=l.x;
  859. ev.mouse_button.y=l.y;
  860. ev.mouse_button.global_x=g.x;
  861. ev.mouse_button.global_y=g.y;
  862. } break;
  863. case InputEvent::MOUSE_MOTION: {
  864. Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
  865. Vector2 g = ai.xform(Vector2(ev.mouse_motion.global_x,ev.mouse_motion.global_y));
  866. Vector2 l = ai.xform(Vector2(ev.mouse_motion.x,ev.mouse_motion.y));
  867. Vector2 r = ai.basis_xform(Vector2(ev.mouse_motion.relative_x,ev.mouse_motion.relative_y));
  868. Vector2 s = ai.basis_xform(Vector2(ev.mouse_motion.speed_x,ev.mouse_motion.speed_y));
  869. ev.mouse_motion.x=l.x;
  870. ev.mouse_motion.y=l.y;
  871. ev.mouse_motion.global_x=g.x;
  872. ev.mouse_motion.global_y=g.y;
  873. ev.mouse_motion.relative_x=r.x;
  874. ev.mouse_motion.relative_y=r.y;
  875. ev.mouse_motion.speed_x=s.x;
  876. ev.mouse_motion.speed_y=s.y;
  877. } break;
  878. case InputEvent::SCREEN_TOUCH: {
  879. Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
  880. Vector2 t = ai.xform(Vector2(ev.screen_touch.x,ev.screen_touch.y));
  881. ev.screen_touch.x=t.x;
  882. ev.screen_touch.y=t.y;
  883. } break;
  884. case InputEvent::SCREEN_DRAG: {
  885. Matrix32 ai = get_final_transform().affine_inverse() * _get_input_pre_xform();
  886. Vector2 t = ai.xform(Vector2(ev.screen_drag.x,ev.screen_drag.y));
  887. Vector2 r = ai.basis_xform(Vector2(ev.screen_drag.relative_x,ev.screen_drag.relative_y));
  888. Vector2 s = ai.basis_xform(Vector2(ev.screen_drag.speed_x,ev.screen_drag.speed_y));
  889. ev.screen_drag.x=t.x;
  890. ev.screen_drag.y=t.y;
  891. ev.screen_drag.relative_x=r.x;
  892. ev.screen_drag.relative_y=r.y;
  893. ev.screen_drag.speed_x=s.x;
  894. ev.screen_drag.speed_y=s.y;
  895. } break;
  896. }
  897. }
  898. void Viewport::_vp_input_text(const String& p_text) {
  899. if (gui.key_focus) {
  900. gui.key_focus->call("set_text",p_text);
  901. }
  902. }
  903. void Viewport::_vp_input(const InputEvent& p_ev) {
  904. if (disable_input)
  905. return;
  906. #ifdef TOOLS_ENABLED
  907. if (get_tree()->is_editor_hint() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
  908. return;
  909. }
  910. #endif
  911. if (parent_control && !parent_control->is_visible())
  912. return;
  913. if (render_target && to_screen_rect==Rect2())
  914. return; //if render target, can't get input events
  915. //this one handles system input, p_ev are in system coordinates
  916. //they are converted to viewport coordinates
  917. InputEvent ev = p_ev;
  918. _make_input_local(ev);
  919. input(ev);
  920. }
  921. void Viewport::_vp_unhandled_input(const InputEvent& p_ev) {
  922. if (disable_input)
  923. return;
  924. #ifdef TOOLS_ENABLED
  925. if (get_tree()->is_editor_hint() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
  926. return;
  927. }
  928. #endif
  929. if (parent_control && !parent_control->is_visible())
  930. return;
  931. if (render_target && to_screen_rect==Rect2())
  932. return; //if render target, can't get input events
  933. //this one handles system input, p_ev are in system coordinates
  934. //they are converted to viewport coordinates
  935. InputEvent ev = p_ev;
  936. _make_input_local(ev);
  937. unhandled_input(ev);
  938. }
  939. Vector2 Viewport::get_mouse_pos() const {
  940. return (get_final_transform().affine_inverse() * _get_input_pre_xform()).xform(Input::get_singleton()->get_mouse_pos());
  941. }
  942. void Viewport::warp_mouse(const Vector2& p_pos) {
  943. Vector2 gpos = (get_final_transform().affine_inverse() * _get_input_pre_xform()).affine_inverse().xform(p_pos);
  944. Input::get_singleton()->warp_mouse_pos(gpos);
  945. }
  946. void Viewport::_gui_sort_subwindows() {
  947. if (!gui.subwindow_order_dirty)
  948. return;
  949. gui.modal_stack.sort_custom<Control::CComparator>();
  950. gui.subwindows.sort_custom<Control::CComparator>();
  951. gui.subwindow_order_dirty=false;
  952. }
  953. void Viewport::_gui_sort_modal_stack() {
  954. gui.modal_stack.sort_custom<Control::CComparator>();
  955. }
  956. void Viewport::_gui_sort_roots() {
  957. if (!gui.roots_order_dirty)
  958. return;
  959. gui.roots.sort_custom<Control::CComparator>();
  960. gui.roots_order_dirty=false;
  961. }
  962. void Viewport::_gui_cancel_tooltip() {
  963. gui.tooltip=NULL;
  964. gui.tooltip_timer=-1;
  965. if (gui.tooltip_popup) {
  966. gui.tooltip_popup->queue_delete();
  967. gui.tooltip_popup=NULL;
  968. }
  969. }
  970. void Viewport::_gui_show_tooltip() {
  971. if (!gui.tooltip) {
  972. return;
  973. }
  974. String tooltip = gui.tooltip->get_tooltip( gui.tooltip->get_global_transform().xform_inv(gui.tooltip_pos) );
  975. if (tooltip.length()==0)
  976. return; // bye
  977. if (gui.tooltip_popup) {
  978. memdelete(gui.tooltip_popup);
  979. gui.tooltip_popup=NULL;
  980. }
  981. Control *rp = gui.tooltip->get_root_parent_control();
  982. if (!rp)
  983. return;
  984. gui.tooltip_popup = memnew( TooltipPanel );
  985. rp->add_child(gui.tooltip_popup);
  986. gui.tooltip_popup->force_parent_owned();
  987. gui.tooltip_label = memnew( TooltipLabel );
  988. gui.tooltip_popup->add_child(gui.tooltip_label);
  989. gui.tooltip_popup->set_as_toplevel(true);
  990. gui.tooltip_popup->hide();
  991. Ref<StyleBox> ttp = gui.tooltip_label->get_stylebox("panel","TooltipPanel");
  992. gui.tooltip_label->set_anchor_and_margin(MARGIN_LEFT,Control::ANCHOR_BEGIN,ttp->get_margin(MARGIN_LEFT));
  993. gui.tooltip_label->set_anchor_and_margin(MARGIN_TOP,Control::ANCHOR_BEGIN,ttp->get_margin(MARGIN_TOP));
  994. gui.tooltip_label->set_anchor_and_margin(MARGIN_RIGHT,Control::ANCHOR_END,ttp->get_margin(MARGIN_RIGHT));
  995. gui.tooltip_label->set_anchor_and_margin(MARGIN_BOTTOM,Control::ANCHOR_END,ttp->get_margin(MARGIN_BOTTOM));
  996. gui.tooltip_label->set_text(tooltip);
  997. Rect2 r(gui.tooltip_pos+Point2(10,10),gui.tooltip_label->get_combined_minimum_size()+ttp->get_minimum_size());
  998. Rect2 vr = gui.tooltip_label->get_viewport_rect();
  999. if (r.size.x+r.pos.x>vr.size.x)
  1000. r.pos.x=vr.size.x-r.size.x;
  1001. else if (r.pos.x<0)
  1002. r.pos.x=0;
  1003. if (r.size.y+r.pos.y>vr.size.y)
  1004. r.pos.y=vr.size.y-r.size.y;
  1005. else if (r.pos.y<0)
  1006. r.pos.y=0;
  1007. gui.tooltip_popup->set_global_pos(r.pos);
  1008. gui.tooltip_popup->set_size(r.size);
  1009. gui.tooltip_popup->raise();
  1010. gui.tooltip_popup->show();
  1011. }
  1012. void Viewport::_gui_call_input(Control *p_control,const InputEvent& p_input) {
  1013. // _block();
  1014. InputEvent ev = p_input;
  1015. //mouse wheel events can't be stopped
  1016. bool cant_stop_me_now = (ev.type==InputEvent::MOUSE_BUTTON &&
  1017. (ev.mouse_button.button_index==BUTTON_WHEEL_DOWN ||
  1018. ev.mouse_button.button_index==BUTTON_WHEEL_UP ||
  1019. ev.mouse_button.button_index==BUTTON_WHEEL_LEFT ||
  1020. ev.mouse_button.button_index==BUTTON_WHEEL_RIGHT ) );
  1021. CanvasItem *ci=p_control;
  1022. while(ci) {
  1023. Control *control = ci->cast_to<Control>();
  1024. if (control) {
  1025. control->call_multilevel(SceneStringNames::get_singleton()->_input_event,ev);
  1026. if (gui.key_event_accepted)
  1027. break;
  1028. if (!control->is_inside_tree())
  1029. break;
  1030. control->emit_signal(SceneStringNames::get_singleton()->input_event,ev);
  1031. if (!control->is_inside_tree() || control->is_set_as_toplevel())
  1032. break;
  1033. if (gui.key_event_accepted)
  1034. break;
  1035. if (!cant_stop_me_now && control->data.stop_mouse && (ev.type==InputEvent::MOUSE_BUTTON || ev.type==InputEvent::MOUSE_MOTION))
  1036. break;
  1037. }
  1038. if (ci->is_set_as_toplevel())
  1039. break;
  1040. ev=ev.xform_by(ci->get_transform()); //transform event upwards
  1041. ci=ci->get_parent_item();
  1042. }
  1043. //_unblock();
  1044. }
  1045. Control* Viewport::_gui_find_control(const Point2& p_global) {
  1046. _gui_sort_subwindows();
  1047. for (List<Control*>::Element *E=gui.subwindows.back();E;E=E->prev()) {
  1048. Control *sw = E->get();
  1049. if (!sw->is_visible())
  1050. continue;
  1051. Matrix32 xform;
  1052. CanvasItem *pci = sw->get_parent_item();
  1053. if (pci)
  1054. xform=pci->get_global_transform_with_canvas();
  1055. else
  1056. xform=sw->get_canvas_transform();
  1057. Control *ret = _gui_find_control_at_pos(sw,p_global,xform,gui.focus_inv_xform);
  1058. if (ret)
  1059. return ret;
  1060. }
  1061. _gui_sort_roots();
  1062. for (List<Control*>::Element *E=gui.roots.back();E;E=E->prev()) {
  1063. Control *sw = E->get();
  1064. if (!sw->is_visible())
  1065. continue;
  1066. Matrix32 xform;
  1067. CanvasItem *pci = sw->get_parent_item();
  1068. if (pci)
  1069. xform=pci->get_global_transform_with_canvas();
  1070. else
  1071. xform=sw->get_canvas_transform();
  1072. Control *ret = _gui_find_control_at_pos(sw,p_global,xform,gui.focus_inv_xform);
  1073. if (ret)
  1074. return ret;
  1075. }
  1076. return NULL;
  1077. }
  1078. Control* Viewport::_gui_find_control_at_pos(CanvasItem* p_node,const Point2& p_global,const Matrix32& p_xform,Matrix32& r_inv_xform) {
  1079. if (p_node->cast_to<Viewport>())
  1080. return NULL;
  1081. Control *c=p_node->cast_to<Control>();
  1082. if (c) {
  1083. // print_line("at "+String(c->get_path())+" POS "+c->get_pos()+" bt "+p_xform);
  1084. }
  1085. //subwindows first!!
  1086. if (p_node->is_hidden()) {
  1087. //return _find_next_visible_control_at_pos(p_node,p_global,r_inv_xform);
  1088. return NULL; //canvas item hidden, discard
  1089. }
  1090. Matrix32 matrix = p_xform * p_node->get_transform();
  1091. if (!c || !c->clips_input() || c->has_point(matrix.affine_inverse().xform(p_global))) {
  1092. for(int i=p_node->get_child_count()-1;i>=0;i--) {
  1093. if (p_node==gui.tooltip_popup)
  1094. continue;
  1095. CanvasItem *ci = p_node->get_child(i)->cast_to<CanvasItem>();
  1096. if (!ci || ci->is_set_as_toplevel())
  1097. continue;
  1098. Control *ret=_gui_find_control_at_pos(ci,p_global,matrix,r_inv_xform);;
  1099. if (ret)
  1100. return ret;
  1101. }
  1102. }
  1103. if (!c)
  1104. return NULL;
  1105. matrix.affine_invert();
  1106. //conditions for considering this as a valid control for return
  1107. if (!c->data.ignore_mouse && c->has_point(matrix.xform(p_global)) && (!gui.drag_preview || (c!=gui.drag_preview && !gui.drag_preview->is_a_parent_of(c)))) {
  1108. r_inv_xform=matrix;
  1109. return c;
  1110. } else
  1111. return NULL;
  1112. }
  1113. void Viewport::_gui_input_event(InputEvent p_event) {
  1114. if (p_event.ID==gui.cancelled_input_ID) {
  1115. return;
  1116. }
  1117. //?
  1118. // if (!is_visible()) {
  1119. // return; //simple and plain
  1120. // }
  1121. switch(p_event.type) {
  1122. case InputEvent::MOUSE_BUTTON: {
  1123. gui.key_event_accepted=false;
  1124. Point2 mpos=Point2(p_event.mouse_button.x,p_event.mouse_button.y);
  1125. if (p_event.mouse_button.pressed) {
  1126. Size2 pos = mpos;
  1127. if (gui.mouse_focus && p_event.mouse_button.button_index!=gui.mouse_focus_button) {
  1128. //do not steal mouse focus and stuff
  1129. } else {
  1130. _gui_sort_modal_stack();
  1131. while (!gui.modal_stack.empty()) {
  1132. Control *top = gui.modal_stack.back()->get();
  1133. Vector2 pos = top->get_global_transform_with_canvas().affine_inverse().xform(mpos);
  1134. if (!top->has_point(pos)) {
  1135. if (top->data.modal_exclusive) {
  1136. //cancel event, sorry, modal exclusive EATS UP ALL
  1137. //get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  1138. get_tree()->set_input_as_handled();
  1139. return; // no one gets the event if exclusive NO ONE
  1140. }
  1141. top->notification(Control::NOTIFICATION_MODAL_CLOSE);
  1142. top->_modal_stack_remove();
  1143. top->hide();
  1144. } else {
  1145. break;
  1146. }
  1147. }
  1148. //Matrix32 parent_xform;
  1149. //if (data.parent_canvas_item)
  1150. // parent_xform=data.parent_canvas_item->get_global_transform();
  1151. gui.mouse_focus = _gui_find_control(pos);
  1152. //print_line("has mf "+itos(gui.mouse_focus!=NULL));
  1153. gui.mouse_focus_button=p_event.mouse_button.button_index;
  1154. if (!gui.mouse_focus) {
  1155. break;
  1156. }
  1157. if (p_event.mouse_button.button_index==BUTTON_LEFT) {
  1158. gui.drag_accum=Vector2();
  1159. gui.drag_attempted=false;
  1160. gui.drag_data=Variant();
  1161. }
  1162. }
  1163. p_event.mouse_button.global_x = pos.x;
  1164. p_event.mouse_button.global_y = pos.y;
  1165. pos = gui.focus_inv_xform.xform(pos);
  1166. p_event.mouse_button.x = pos.x;
  1167. p_event.mouse_button.y = pos.y;
  1168. #ifdef DEBUG_ENABLED
  1169. if (ScriptDebugger::get_singleton()) {
  1170. Array arr;
  1171. arr.push_back(gui.mouse_focus->get_path());
  1172. arr.push_back(gui.mouse_focus->get_type());
  1173. ScriptDebugger::get_singleton()->send_message("click_ctrl",arr);
  1174. }
  1175. /*if (bool(GLOBAL_DEF("debug/print_clicked_control",false))) {
  1176. print_line(String(gui.mouse_focus->get_path())+" - "+pos);
  1177. }*/
  1178. #endif
  1179. if (gui.mouse_focus->get_focus_mode()!=Control::FOCUS_NONE && gui.mouse_focus!=gui.key_focus && p_event.mouse_button.button_index==BUTTON_LEFT) {
  1180. // also get keyboard focus
  1181. gui.mouse_focus->grab_focus();
  1182. }
  1183. if (gui.mouse_focus->can_process()) {
  1184. _gui_call_input(gui.mouse_focus,p_event);
  1185. }
  1186. get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  1187. get_tree()->set_input_as_handled();
  1188. _gui_cancel_tooltip();
  1189. //gui.tooltip_popup->hide();
  1190. } else {
  1191. if (gui.drag_preview && p_event.mouse_button.button_index==BUTTON_LEFT) {
  1192. memdelete( gui.drag_preview );
  1193. gui.drag_preview=NULL;
  1194. }
  1195. if (!gui.mouse_focus) {
  1196. if (gui.mouse_over && gui.drag_data.get_type()!=Variant::NIL && p_event.mouse_button.button_index==BUTTON_LEFT) {
  1197. Size2 pos = mpos;
  1198. pos = gui.focus_inv_xform.xform(pos);
  1199. if (gui.mouse_over->can_drop_data(pos,gui.drag_data)) {
  1200. gui.mouse_over->drop_data(pos,gui.drag_data);
  1201. }
  1202. gui.drag_data=Variant();
  1203. //change mouse accordingly
  1204. }
  1205. break;
  1206. }
  1207. Size2 pos = mpos;
  1208. p_event.mouse_button.global_x = pos.x;
  1209. p_event.mouse_button.global_y = pos.y;
  1210. pos = gui.focus_inv_xform.xform(pos);
  1211. p_event.mouse_button.x = pos.x;
  1212. p_event.mouse_button.y = pos.y;
  1213. if (gui.mouse_focus->can_process()) {
  1214. _gui_call_input(gui.mouse_focus,p_event);
  1215. }
  1216. if (p_event.mouse_button.button_index==gui.mouse_focus_button) {
  1217. gui.mouse_focus=NULL;
  1218. gui.mouse_focus_button=-1;
  1219. }
  1220. if (gui.drag_data.get_type()!=Variant::NIL && p_event.mouse_button.button_index==BUTTON_LEFT) {
  1221. gui.drag_data=Variant(); //always clear
  1222. }
  1223. get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  1224. get_tree()->set_input_as_handled();
  1225. }
  1226. } break;
  1227. case InputEvent::MOUSE_MOTION: {
  1228. gui.key_event_accepted=false;
  1229. Point2 mpos=Point2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  1230. gui.last_mouse_pos=mpos;
  1231. Control *over = NULL;
  1232. // D&D
  1233. if (!gui.drag_attempted && gui.mouse_focus && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) {
  1234. gui.drag_accum+=Point2(p_event.mouse_motion.relative_x,p_event.mouse_motion.relative_y);;
  1235. float len = gui.drag_accum.length();
  1236. if (len>10) {
  1237. gui.drag_data=gui.mouse_focus->get_drag_data(gui.focus_inv_xform.xform(mpos)-gui.drag_accum);
  1238. if (gui.drag_data.get_type()!=Variant::NIL) {
  1239. gui.mouse_focus=NULL;
  1240. }
  1241. gui.drag_attempted=true;
  1242. }
  1243. }
  1244. if (gui.mouse_focus) {
  1245. over=gui.mouse_focus;
  1246. //recompute focus_inv_xform again here
  1247. } else {
  1248. over = _gui_find_control(mpos);
  1249. }
  1250. if (gui.drag_data.get_type()==Variant::NIL && over && !gui.modal_stack.empty()) {
  1251. Control *top = gui.modal_stack.back()->get();
  1252. if (over!=top && !top->is_a_parent_of(over)) {
  1253. break; // don't send motion event to anything below modal stack top
  1254. }
  1255. }
  1256. if (over!=gui.mouse_over) {
  1257. if (gui.mouse_over)
  1258. gui.mouse_over->notification(Control::NOTIFICATION_MOUSE_EXIT);
  1259. _gui_cancel_tooltip();
  1260. if (over)
  1261. over->notification(Control::NOTIFICATION_MOUSE_ENTER);
  1262. }
  1263. gui.mouse_over=over;
  1264. if (gui.drag_preview) {
  1265. gui.drag_preview->set_pos(mpos);
  1266. }
  1267. if (!over) {
  1268. OS::get_singleton()->set_cursor_shape(OS::CURSOR_ARROW);
  1269. break;
  1270. }
  1271. Matrix32 localizer = over->get_global_transform_with_canvas().affine_inverse();
  1272. Size2 pos = localizer.xform(mpos);
  1273. Vector2 speed = localizer.basis_xform(Point2(p_event.mouse_motion.speed_x,p_event.mouse_motion.speed_y));
  1274. Vector2 rel = localizer.basis_xform(Point2(p_event.mouse_motion.relative_x,p_event.mouse_motion.relative_y));
  1275. p_event.mouse_motion.global_x = mpos.x;
  1276. p_event.mouse_motion.global_y = mpos.y;
  1277. p_event.mouse_motion.speed_x=speed.x;
  1278. p_event.mouse_motion.speed_y=speed.y;
  1279. p_event.mouse_motion.relative_x=rel.x;
  1280. p_event.mouse_motion.relative_y=rel.y;
  1281. if (p_event.mouse_motion.button_mask==0) {
  1282. //nothing pressed
  1283. bool can_tooltip=true;
  1284. if (!gui.modal_stack.empty()) {
  1285. if (gui.modal_stack.back()->get()!=over && !gui.modal_stack.back()->get()->is_a_parent_of(over))
  1286. can_tooltip=false;
  1287. }
  1288. if (can_tooltip) {
  1289. gui.tooltip=over;
  1290. gui.tooltip_pos=mpos;//(parent_xform * get_transform()).affine_inverse().xform(pos);
  1291. gui.tooltip_timer=gui.tooltip_delay;
  1292. }
  1293. }
  1294. //pos = gui.focus_inv_xform.xform(pos);
  1295. p_event.mouse_motion.x = pos.x;
  1296. p_event.mouse_motion.y = pos.y;
  1297. Control::CursorShape cursor_shape = over->get_cursor_shape(pos);
  1298. OS::get_singleton()->set_cursor_shape( (OS::CursorShape)cursor_shape );
  1299. if (over->can_process()) {
  1300. _gui_call_input(over,p_event);
  1301. }
  1302. //get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  1303. get_tree()->set_input_as_handled();
  1304. if (gui.drag_data.get_type()!=Variant::NIL && p_event.mouse_motion.button_mask&BUTTON_MASK_LEFT) {
  1305. /*bool can_drop =*/ over->can_drop_data(pos,gui.drag_data);
  1306. //change mouse accordingly i guess
  1307. }
  1308. } break;
  1309. case InputEvent::ACTION:
  1310. case InputEvent::JOYSTICK_BUTTON:
  1311. case InputEvent::KEY: {
  1312. if (gui.key_focus) {
  1313. gui.key_event_accepted=false;
  1314. if (gui.key_focus->can_process()) {
  1315. gui.key_focus->call_multilevel("_input_event",p_event);
  1316. if (gui.key_focus) //maybe lost it
  1317. gui.key_focus->emit_signal(SceneStringNames::get_singleton()->input_event,p_event);
  1318. }
  1319. if (gui.key_event_accepted) {
  1320. get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  1321. break;
  1322. }
  1323. }
  1324. if (p_event.is_pressed() && p_event.is_action("ui_cancel") && !gui.modal_stack.empty()) {
  1325. _gui_sort_modal_stack();
  1326. Control *top = gui.modal_stack.back()->get();
  1327. if (!top->data.modal_exclusive) {
  1328. top->notification(Control::NOTIFICATION_MODAL_CLOSE);
  1329. top->_modal_stack_remove();
  1330. top->hide();
  1331. }
  1332. }
  1333. Control * from = gui.key_focus ? gui.key_focus : NULL; //hmm
  1334. //keyboard focus
  1335. //if (from && p_event.key.pressed && !p_event.key.mod.alt && !p_event.key.mod.meta && !p_event.key.mod.command) {
  1336. if (from && p_event.is_pressed()) {
  1337. Control * next=NULL;
  1338. if (p_event.is_action("ui_focus_next")) {
  1339. next = from->find_next_valid_focus();
  1340. }
  1341. if (p_event.is_action("ui_focus_prev")) {
  1342. next = from->find_prev_valid_focus();
  1343. }
  1344. if (p_event.is_action("ui_up")) {
  1345. next = from->_get_focus_neighbour(MARGIN_TOP);
  1346. }
  1347. if (p_event.is_action("ui_left")) {
  1348. next = from->_get_focus_neighbour(MARGIN_LEFT);
  1349. }
  1350. if (p_event.is_action("ui_right")) {
  1351. next = from->_get_focus_neighbour(MARGIN_RIGHT);
  1352. }
  1353. if (p_event.is_action("ui_down")) {
  1354. next = from->_get_focus_neighbour(MARGIN_BOTTOM);
  1355. }
  1356. if (next) {
  1357. next->grab_focus();
  1358. get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_cancel_input_ID",p_event.ID);
  1359. }
  1360. }
  1361. } break;
  1362. }
  1363. }
  1364. List<Control*>::Element* Viewport::_gui_add_root_control(Control* p_control) {
  1365. gui.roots_order_dirty=true;
  1366. return gui.roots.push_back(p_control);
  1367. }
  1368. List<Control*>::Element* Viewport::_gui_add_subwindow_control(Control* p_control) {
  1369. gui.subwindow_order_dirty=true;
  1370. return gui.subwindows.push_back(p_control);
  1371. }
  1372. void Viewport::_gui_set_subwindow_order_dirty() {
  1373. gui.subwindow_order_dirty=true;
  1374. }
  1375. void Viewport::_gui_set_root_order_dirty() {
  1376. gui.roots_order_dirty=true;
  1377. }
  1378. void Viewport::_gui_remove_modal_control(List<Control*>::Element *MI) {
  1379. gui.modal_stack.erase(MI);
  1380. }
  1381. void Viewport::_gui_remove_from_modal_stack(List<Control*>::Element *MI,ObjectID p_prev_focus_owner) {
  1382. //transfer the focus stack to the next
  1383. List<Control*>::Element *next = MI->next();
  1384. gui.modal_stack.erase(MI);
  1385. MI=NULL;
  1386. if (p_prev_focus_owner) {
  1387. // for previous window in stack, pass the focus so it feels more
  1388. // natural
  1389. if (!next) { //top of stack
  1390. Object *pfo = ObjectDB::get_instance(p_prev_focus_owner);
  1391. Control *pfoc = pfo->cast_to<Control>();
  1392. if (!pfoc)
  1393. return;
  1394. if (!pfoc->is_inside_tree() || !pfoc->is_visible())
  1395. return;
  1396. pfoc->grab_focus();
  1397. } else {
  1398. next->get()->_modal_set_prev_focus_owner(p_prev_focus_owner);
  1399. }
  1400. }
  1401. }
  1402. void Viewport::_gui_force_drag(Control *p_base, const Variant& p_data, Control *p_control) {
  1403. gui.drag_data=p_data;
  1404. gui.mouse_focus=NULL;
  1405. if (p_control) {
  1406. _gui_set_drag_preview(p_base,p_control);
  1407. }
  1408. }
  1409. void Viewport::_gui_set_drag_preview(Control *p_base, Control *p_control) {
  1410. ERR_FAIL_NULL(p_control);
  1411. ERR_FAIL_COND( !((Object*)p_control)->cast_to<Control>());
  1412. ERR_FAIL_COND(p_control->is_inside_tree());
  1413. ERR_FAIL_COND(p_control->get_parent()!=NULL);
  1414. if (gui.drag_preview) {
  1415. memdelete(gui.drag_preview);
  1416. }
  1417. p_control->set_as_toplevel(true);
  1418. p_control->set_pos(gui.last_mouse_pos);
  1419. p_base->get_root_parent_control()->add_child(p_control); //add as child of viewport
  1420. p_control->raise();
  1421. if (gui.drag_preview) {
  1422. memdelete( gui.drag_preview );
  1423. }
  1424. gui.drag_preview=p_control;
  1425. }
  1426. void Viewport::_gui_remove_root_control(List<Control*>::Element *RI) {
  1427. gui.roots.erase(RI);
  1428. }
  1429. void Viewport::_gui_remove_subwindow_control(List<Control*>::Element* SI){
  1430. gui.subwindows.erase(SI);
  1431. }
  1432. void Viewport::_gui_unfocus_control(Control *p_control) {
  1433. if (gui.key_focus==p_control) {
  1434. gui.key_focus->release_focus();
  1435. }
  1436. }
  1437. void Viewport::_gui_hid_control(Control *p_control) {
  1438. if (gui.mouse_focus == p_control) {
  1439. gui.mouse_focus=NULL;
  1440. }
  1441. /* ???
  1442. if (data.window==p_control) {
  1443. window->drag_data=Variant();
  1444. if (window->drag_preview) {
  1445. memdelete( window->drag_preview);
  1446. window->drag_preview=NULL;
  1447. }
  1448. }
  1449. */
  1450. if (gui.key_focus == p_control)
  1451. gui.key_focus=NULL;
  1452. if (gui.mouse_over == p_control)
  1453. gui.mouse_over=NULL;
  1454. if (gui.tooltip == p_control)
  1455. gui.tooltip=NULL;
  1456. if (gui.tooltip == p_control) {
  1457. gui.tooltip=NULL;
  1458. _gui_cancel_tooltip();
  1459. }
  1460. }
  1461. void Viewport::_gui_remove_control(Control *p_control) {
  1462. if (gui.mouse_focus == p_control)
  1463. gui.mouse_focus=NULL;
  1464. if (gui.key_focus == p_control)
  1465. gui.key_focus=NULL;
  1466. if (gui.mouse_over == p_control)
  1467. gui.mouse_over=NULL;
  1468. if (gui.tooltip == p_control)
  1469. gui.tooltip=NULL;
  1470. if (gui.tooltip_popup == p_control) {
  1471. _gui_cancel_tooltip();
  1472. }
  1473. }
  1474. void Viewport::_gui_remove_focus() {
  1475. if (gui.key_focus) {
  1476. Node *f=gui.key_focus;
  1477. gui.key_focus=NULL;
  1478. f->notification( Control::NOTIFICATION_FOCUS_EXIT,true );
  1479. }
  1480. }
  1481. bool Viewport::_gui_is_modal_on_top(const Control* p_control) {
  1482. return (gui.modal_stack.size() && gui.modal_stack.back()->get()==p_control);
  1483. }
  1484. bool Viewport::_gui_control_has_focus(const Control* p_control) {
  1485. return gui.key_focus==p_control;
  1486. }
  1487. void Viewport::_gui_control_grab_focus(Control* p_control) {
  1488. //no need for change
  1489. if (gui.key_focus && gui.key_focus==p_control)
  1490. return;
  1491. _gui_remove_focus();
  1492. get_tree()->call_group(SceneTree::GROUP_CALL_REALTIME,"windows","_gui_remove_focus");
  1493. gui.key_focus=p_control;
  1494. p_control->notification(Control::NOTIFICATION_FOCUS_ENTER);
  1495. p_control->update();
  1496. }
  1497. void Viewport::_gui_accept_event() {
  1498. gui.key_event_accepted=true;
  1499. if (is_inside_tree())
  1500. get_tree()->set_input_as_handled();
  1501. }
  1502. List<Control*>::Element* Viewport::_gui_show_modal(Control* p_control) {
  1503. gui.modal_stack.push_back(p_control);
  1504. if (gui.key_focus)
  1505. p_control->_modal_set_prev_focus_owner(gui.key_focus->get_instance_ID());
  1506. else
  1507. p_control->_modal_set_prev_focus_owner(0);
  1508. return gui.modal_stack.back();
  1509. }
  1510. Control *Viewport::_gui_get_focus_owner() {
  1511. return gui.key_focus;
  1512. }
  1513. void Viewport::_gui_grab_click_focus(Control *p_control) {
  1514. if (gui.mouse_focus) {
  1515. if (gui.mouse_focus==p_control)
  1516. return;
  1517. InputEvent ie;
  1518. ie.type=InputEvent::MOUSE_BUTTON;
  1519. InputEventMouseButton &mb=ie.mouse_button;
  1520. //send unclic
  1521. Point2 click =gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  1522. mb.x=click.x;
  1523. mb.y=click.y;
  1524. mb.button_index=gui.mouse_focus_button;
  1525. mb.pressed=false;
  1526. gui.mouse_focus->call_deferred("_input_event",ie);
  1527. gui.mouse_focus=p_control;
  1528. gui.focus_inv_xform=gui.mouse_focus->get_global_transform_with_canvas().affine_inverse();
  1529. click =gui.mouse_focus->get_global_transform_with_canvas().affine_inverse().xform(gui.last_mouse_pos);
  1530. mb.x=click.x;
  1531. mb.y=click.y;
  1532. mb.button_index=gui.mouse_focus_button;
  1533. mb.pressed=true;
  1534. gui.mouse_focus->call_deferred("_input_event",ie);
  1535. }
  1536. }
  1537. ///////////////////////////////
  1538. void Viewport::input(const InputEvent& p_event) {
  1539. ERR_FAIL_COND(!is_inside_tree());
  1540. get_tree()->_call_input_pause(input_group,"_input",p_event);
  1541. _gui_input_event(p_event);
  1542. //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
  1543. }
  1544. void Viewport::unhandled_input(const InputEvent& p_event) {
  1545. ERR_FAIL_COND(!is_inside_tree());
  1546. get_tree()->_call_input_pause(unhandled_input_group,"_unhandled_input",p_event);
  1547. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev);
  1548. if (!get_tree()->input_handled && p_event.type==InputEvent::KEY) {
  1549. get_tree()->_call_input_pause(unhandled_key_input_group,"_unhandled_key_input",p_event);
  1550. //call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev);
  1551. }
  1552. if (physics_object_picking && !get_tree()->input_handled) {
  1553. if (p_event.type==InputEvent::MOUSE_BUTTON || p_event.type==InputEvent::MOUSE_MOTION || p_event.type==InputEvent::SCREEN_DRAG || p_event.type==InputEvent::SCREEN_TOUCH) {
  1554. physics_picking_events.push_back(p_event);
  1555. }
  1556. }
  1557. }
  1558. void Viewport::set_use_own_world(bool p_world) {
  1559. if (p_world==own_world.is_valid())
  1560. return;
  1561. if (is_inside_tree())
  1562. _propagate_exit_world(this);
  1563. #ifndef _3D_DISABLED
  1564. if (find_world().is_valid() && camera)
  1565. camera->notification(Camera::NOTIFICATION_LOST_CURRENT);
  1566. #endif
  1567. if (!p_world)
  1568. own_world=Ref<World>();
  1569. else
  1570. own_world=Ref<World>( memnew( World ));
  1571. if (is_inside_tree())
  1572. _propagate_enter_world(this);
  1573. #ifndef _3D_DISABLED
  1574. if (find_world().is_valid() && camera)
  1575. camera->notification(Camera::NOTIFICATION_BECAME_CURRENT);
  1576. #endif
  1577. //propagate exit
  1578. if (is_inside_tree()) {
  1579. VisualServer::get_singleton()->viewport_set_scenario(viewport,find_world()->get_scenario());
  1580. }
  1581. _update_listener();
  1582. }
  1583. bool Viewport::is_using_own_world() const {
  1584. return own_world.is_valid();
  1585. }
  1586. void Viewport::set_render_target_to_screen_rect(const Rect2& p_rect) {
  1587. to_screen_rect=p_rect;
  1588. VisualServer::get_singleton()->viewport_set_render_target_to_screen_rect(viewport,to_screen_rect);
  1589. }
  1590. Rect2 Viewport::get_render_target_to_screen_rect() const{
  1591. return to_screen_rect;
  1592. }
  1593. void Viewport::set_physics_object_picking(bool p_enable) {
  1594. physics_object_picking=p_enable;
  1595. set_fixed_process(physics_object_picking);
  1596. if (!physics_object_picking)
  1597. physics_picking_events.clear();
  1598. }
  1599. Vector2 Viewport::get_camera_coords(const Vector2 &p_viewport_coords) const {
  1600. Matrix32 xf = get_final_transform();
  1601. return xf.xform(p_viewport_coords);
  1602. }
  1603. Vector2 Viewport::get_camera_rect_size() const {
  1604. return last_vp_rect.size;
  1605. }
  1606. bool Viewport::get_physics_object_picking() {
  1607. return physics_object_picking;
  1608. }
  1609. bool Viewport::gui_has_modal_stack() const {
  1610. return gui.modal_stack.size();
  1611. }
  1612. void Viewport::set_disable_input(bool p_disable) {
  1613. disable_input=p_disable;
  1614. }
  1615. bool Viewport::is_input_disabled() const {
  1616. return disable_input;
  1617. }
  1618. void Viewport::_bind_methods() {
  1619. ObjectTypeDB::bind_method(_MD("set_rect","rect"), &Viewport::set_rect);
  1620. ObjectTypeDB::bind_method(_MD("get_rect"), &Viewport::get_rect);
  1621. //ObjectTypeDB::bind_method(_MD("set_world_2d","world_2d:World2D"), &Viewport::set_world_2d);
  1622. //ObjectTypeDB::bind_method(_MD("get_world_2d:World2D"), &Viewport::get_world_2d);
  1623. ObjectTypeDB::bind_method(_MD("find_world_2d:World2D"), &Viewport::find_world_2d);
  1624. ObjectTypeDB::bind_method(_MD("set_world","world:World"), &Viewport::set_world);
  1625. ObjectTypeDB::bind_method(_MD("get_world:World"), &Viewport::get_world);
  1626. ObjectTypeDB::bind_method(_MD("find_world:World"), &Viewport::find_world);
  1627. ObjectTypeDB::bind_method(_MD("set_canvas_transform","xform"), &Viewport::set_canvas_transform);
  1628. ObjectTypeDB::bind_method(_MD("get_canvas_transform"), &Viewport::get_canvas_transform);
  1629. ObjectTypeDB::bind_method(_MD("set_global_canvas_transform","xform"), &Viewport::set_global_canvas_transform);
  1630. ObjectTypeDB::bind_method(_MD("get_global_canvas_transform"), &Viewport::get_global_canvas_transform);
  1631. ObjectTypeDB::bind_method(_MD("get_final_transform"), &Viewport::get_final_transform);
  1632. ObjectTypeDB::bind_method(_MD("get_visible_rect"), &Viewport::get_visible_rect);
  1633. ObjectTypeDB::bind_method(_MD("set_transparent_background","enable"), &Viewport::set_transparent_background);
  1634. ObjectTypeDB::bind_method(_MD("has_transparent_background"), &Viewport::has_transparent_background);
  1635. ObjectTypeDB::bind_method(_MD("_parent_visibility_changed"), &Viewport::_parent_visibility_changed);
  1636. ObjectTypeDB::bind_method(_MD("_parent_resized"), &Viewport::_parent_resized);
  1637. ObjectTypeDB::bind_method(_MD("_vp_input"), &Viewport::_vp_input);
  1638. ObjectTypeDB::bind_method(_MD("_vp_input_text","text"), &Viewport::_vp_input_text);
  1639. ObjectTypeDB::bind_method(_MD("_vp_unhandled_input"), &Viewport::_vp_unhandled_input);
  1640. ObjectTypeDB::bind_method(_MD("set_size_override","enable","size","margin"), &Viewport::set_size_override,DEFVAL(Size2(-1,-1)),DEFVAL(Size2(0,0)));
  1641. ObjectTypeDB::bind_method(_MD("get_size_override"), &Viewport::get_size_override);
  1642. ObjectTypeDB::bind_method(_MD("is_size_override_enabled"), &Viewport::is_size_override_enabled);
  1643. ObjectTypeDB::bind_method(_MD("set_size_override_stretch","enabled"), &Viewport::set_size_override_stretch);
  1644. ObjectTypeDB::bind_method(_MD("is_size_override_stretch_enabled"), &Viewport::is_size_override_stretch_enabled);
  1645. ObjectTypeDB::bind_method(_MD("queue_screen_capture"), &Viewport::queue_screen_capture);
  1646. ObjectTypeDB::bind_method(_MD("get_screen_capture"), &Viewport::get_screen_capture);
  1647. ObjectTypeDB::bind_method(_MD("set_as_render_target","enable"), &Viewport::set_as_render_target);
  1648. ObjectTypeDB::bind_method(_MD("is_set_as_render_target"), &Viewport::is_set_as_render_target);
  1649. ObjectTypeDB::bind_method(_MD("set_render_target_vflip","enable"), &Viewport::set_render_target_vflip);
  1650. ObjectTypeDB::bind_method(_MD("get_render_target_vflip"), &Viewport::get_render_target_vflip);
  1651. ObjectTypeDB::bind_method(_MD("set_render_target_clear_on_new_frame","enable"), &Viewport::set_render_target_clear_on_new_frame);
  1652. ObjectTypeDB::bind_method(_MD("get_render_target_clear_on_new_frame"), &Viewport::get_render_target_clear_on_new_frame);
  1653. ObjectTypeDB::bind_method(_MD("render_target_clear"), &Viewport::render_target_clear);
  1654. ObjectTypeDB::bind_method(_MD("set_render_target_filter","enable"), &Viewport::set_render_target_filter);
  1655. ObjectTypeDB::bind_method(_MD("get_render_target_filter"), &Viewport::get_render_target_filter);
  1656. ObjectTypeDB::bind_method(_MD("set_render_target_gen_mipmaps","enable"), &Viewport::set_render_target_gen_mipmaps);
  1657. ObjectTypeDB::bind_method(_MD("get_render_target_gen_mipmaps"), &Viewport::get_render_target_gen_mipmaps);
  1658. ObjectTypeDB::bind_method(_MD("set_render_target_update_mode","mode"), &Viewport::set_render_target_update_mode);
  1659. ObjectTypeDB::bind_method(_MD("get_render_target_update_mode"), &Viewport::get_render_target_update_mode);
  1660. ObjectTypeDB::bind_method(_MD("get_render_target_texture:RenderTargetTexture"), &Viewport::get_render_target_texture);
  1661. ObjectTypeDB::bind_method(_MD("set_physics_object_picking","enable"), &Viewport::set_physics_object_picking);
  1662. ObjectTypeDB::bind_method(_MD("get_physics_object_picking"), &Viewport::get_physics_object_picking);
  1663. ObjectTypeDB::bind_method(_MD("get_viewport"), &Viewport::get_viewport);
  1664. ObjectTypeDB::bind_method(_MD("input","local_event"), &Viewport::input);
  1665. ObjectTypeDB::bind_method(_MD("unhandled_input","local_event"), &Viewport::unhandled_input);
  1666. ObjectTypeDB::bind_method(_MD("update_worlds"), &Viewport::update_worlds);
  1667. ObjectTypeDB::bind_method(_MD("set_use_own_world","enable"), &Viewport::set_use_own_world);
  1668. ObjectTypeDB::bind_method(_MD("is_using_own_world"), &Viewport::is_using_own_world);
  1669. ObjectTypeDB::bind_method(_MD("get_camera:Camera"), &Viewport::get_camera);
  1670. ObjectTypeDB::bind_method(_MD("set_as_audio_listener","enable"), &Viewport::set_as_audio_listener);
  1671. ObjectTypeDB::bind_method(_MD("is_audio_listener","enable"), &Viewport::is_audio_listener);
  1672. ObjectTypeDB::bind_method(_MD("set_as_audio_listener_2d","enable"), &Viewport::set_as_audio_listener_2d);
  1673. ObjectTypeDB::bind_method(_MD("is_audio_listener_2d","enable"), &Viewport::is_audio_listener_2d);
  1674. ObjectTypeDB::bind_method(_MD("set_render_target_to_screen_rect","rect"), &Viewport::set_render_target_to_screen_rect);
  1675. ObjectTypeDB::bind_method(_MD("get_mouse_pos"), &Viewport::get_mouse_pos);
  1676. ObjectTypeDB::bind_method(_MD("warp_mouse","to_pos"), &Viewport::warp_mouse);
  1677. ObjectTypeDB::bind_method(_MD("gui_has_modal_stack"), &Viewport::gui_has_modal_stack);
  1678. ObjectTypeDB::bind_method(_MD("set_disable_input","disable"), &Viewport::set_disable_input);
  1679. ObjectTypeDB::bind_method(_MD("is_input_disabled"), &Viewport::is_input_disabled);
  1680. ObjectTypeDB::bind_method(_MD("_gui_show_tooltip"), &Viewport::_gui_show_tooltip);
  1681. ADD_PROPERTY( PropertyInfo(Variant::RECT2,"rect"), _SCS("set_rect"), _SCS("get_rect") );
  1682. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"own_world"), _SCS("set_use_own_world"), _SCS("is_using_own_world") );
  1683. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"world",PROPERTY_HINT_RESOURCE_TYPE,"World"), _SCS("set_world"), _SCS("get_world") );
  1684. // ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"world_2d",PROPERTY_HINT_RESOURCE_TYPE,"World2D"), _SCS("set_world_2d"), _SCS("get_world_2d") );
  1685. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"transparent_bg"), _SCS("set_transparent_background"), _SCS("has_transparent_background") );
  1686. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/enabled"), _SCS("set_as_render_target"), _SCS("is_set_as_render_target") );
  1687. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/v_flip"), _SCS("set_render_target_vflip"), _SCS("get_render_target_vflip") );
  1688. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/clear_on_new_frame"), _SCS("set_render_target_clear_on_new_frame"), _SCS("get_render_target_clear_on_new_frame") );
  1689. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/filter"), _SCS("set_render_target_filter"), _SCS("get_render_target_filter") );
  1690. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/gen_mipmaps"), _SCS("set_render_target_gen_mipmaps"), _SCS("get_render_target_gen_mipmaps") );
  1691. ADD_PROPERTY( PropertyInfo(Variant::INT,"render_target/update_mode",PROPERTY_HINT_ENUM,"Disabled,Once,When Visible,Always"), _SCS("set_render_target_update_mode"), _SCS("get_render_target_update_mode") );
  1692. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"audio_listener/enable_2d"), _SCS("set_as_audio_listener_2d"), _SCS("is_audio_listener_2d") );
  1693. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"audio_listener/enable_3d"), _SCS("set_as_audio_listener"), _SCS("is_audio_listener") );
  1694. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"physics/object_picking"), _SCS("set_physics_object_picking"), _SCS("get_physics_object_picking") );
  1695. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"gui/disable_input"), _SCS("set_disable_input"), _SCS("is_input_disabled") );
  1696. ADD_SIGNAL(MethodInfo("size_changed"));
  1697. BIND_CONSTANT( RENDER_TARGET_UPDATE_DISABLED );
  1698. BIND_CONSTANT( RENDER_TARGET_UPDATE_ONCE );
  1699. BIND_CONSTANT( RENDER_TARGET_UPDATE_WHEN_VISIBLE );
  1700. BIND_CONSTANT( RENDER_TARGET_UPDATE_ALWAYS );
  1701. }
  1702. Viewport::Viewport() {
  1703. world_2d = Ref<World2D>( memnew( World2D ));
  1704. viewport = VisualServer::get_singleton()->viewport_create();
  1705. listener=SpatialSoundServer::get_singleton()->listener_create();
  1706. audio_listener=false;
  1707. listener_2d=SpatialSound2DServer::get_singleton()->listener_create();
  1708. audio_listener_2d=false;
  1709. transparent_bg=false;
  1710. parent=NULL;
  1711. camera=NULL;
  1712. size_override=false;
  1713. size_override_stretch=false;
  1714. size_override_size=Size2(1,1);
  1715. render_target_gen_mipmaps=false;
  1716. render_target=false;
  1717. render_target_vflip=false;
  1718. render_target_clear_on_new_frame=true;
  1719. //render_target_clear=true;
  1720. render_target_update_mode=RENDER_TARGET_UPDATE_WHEN_VISIBLE;
  1721. render_target_texture = Ref<RenderTargetTexture>( memnew( RenderTargetTexture(this) ) );
  1722. physics_object_picking=false;
  1723. physics_object_capture=0;
  1724. physics_object_over=0;
  1725. physics_last_mousepos=Vector2(1e20,1e20);
  1726. String id=itos(get_instance_ID());
  1727. input_group = "_vp_input"+id;
  1728. gui_input_group = "_vp_gui_input"+id;
  1729. unhandled_input_group = "_vp_unhandled_input"+id;
  1730. unhandled_key_input_group = "_vp_unhandled_key_input"+id;
  1731. disable_input=false;
  1732. //window tooltip
  1733. gui.tooltip_timer = -1;
  1734. //gui.tooltip_timer->force_parent_owned();
  1735. gui.tooltip_delay=GLOBAL_DEF("display/tooltip_delay",0.7);
  1736. gui.tooltip=NULL;
  1737. gui.tooltip_label=NULL;
  1738. gui.drag_preview=NULL;
  1739. gui.drag_attempted=false;
  1740. parent_control=NULL;
  1741. }
  1742. Viewport::~Viewport() {
  1743. VisualServer::get_singleton()->free( viewport );
  1744. SpatialSoundServer::get_singleton()->free(listener);
  1745. SpatialSound2DServer::get_singleton()->free(listener_2d);
  1746. if (render_target_texture.is_valid())
  1747. render_target_texture->vp=NULL; //so if used, will crash
  1748. }