2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* viewport.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
2017-01-01 21:01:57 +00:00
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
2017-04-07 22:45:00 +00:00
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
# include "viewport.h"
2017-03-18 23:36:26 +00:00
# include "os/input.h"
2014-02-10 01:10:30 +00:00
# include "os/os.h"
# include "scene/3d/spatial.h"
2015-03-22 04:46:18 +00:00
# include "servers/physics_2d_server.h"
2014-02-10 01:10:30 +00:00
//#include "scene/3d/camera.h"
# include "scene/3d/camera.h"
2017-03-18 23:36:26 +00:00
# include "scene/3d/collision_object.h"
2016-03-20 02:10:04 +00:00
# include "scene/3d/listener.h"
2014-02-10 01:10:30 +00:00
# include "scene/3d/spatial_indexer.h"
2017-03-18 23:36:26 +00:00
# include "scene/gui/control.h"
# include "scene/resources/mesh.h"
# include "servers/spatial_sound_2d_server.h"
# include "servers/spatial_sound_server.h"
2014-02-10 01:10:30 +00:00
2015-03-22 04:46:18 +00:00
# include "scene/2d/collision_object_2d.h"
2014-02-10 01:10:30 +00:00
2016-01-17 01:41:10 +00:00
# include "scene/gui/label.h"
2017-03-18 23:36:26 +00:00
# include "scene/gui/panel.h"
2016-01-17 01:41:10 +00:00
# include "scene/main/timer.h"
# include "scene/scene_string_names.h"
# include "globals.h"
2014-02-10 01:10:30 +00:00
int RenderTargetTexture : : get_width ( ) const {
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( ! vp , 0 ) ;
2014-02-10 01:10:30 +00:00
return vp - > rect . size . width ;
}
2017-03-18 23:36:26 +00:00
int RenderTargetTexture : : get_height ( ) const {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( ! vp , 0 ) ;
2014-02-10 01:10:30 +00:00
return vp - > rect . size . height ;
}
2017-03-18 23:36:26 +00:00
Size2 RenderTargetTexture : : get_size ( ) const {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( ! vp , Size2 ( ) ) ;
2014-02-10 01:10:30 +00:00
return vp - > rect . size ;
}
2017-03-18 23:36:26 +00:00
RID RenderTargetTexture : : get_rid ( ) const {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND_V ( ! vp , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
return vp - > render_target_texture_rid ;
}
2017-03-18 23:36:26 +00:00
bool RenderTargetTexture : : has_alpha ( ) const {
2014-02-10 01:10:30 +00:00
return false ;
}
2017-03-18 23:36:26 +00:00
void RenderTargetTexture : : set_flags ( uint32_t p_flags ) {
2014-02-10 01:10:30 +00:00
2014-04-19 19:46:52 +00:00
ERR_FAIL_COND ( ! vp ) ;
2017-03-18 23:36:26 +00:00
if ( p_flags & FLAG_FILTER )
flags = FLAG_FILTER ;
2014-04-19 19:46:52 +00:00
else
2017-03-18 23:36:26 +00:00
flags = 0 ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
VS : : get_singleton ( ) - > texture_set_flags ( vp - > render_target_texture_rid , flags ) ;
2014-02-10 01:10:30 +00:00
}
2014-04-19 19:46:52 +00:00
2017-03-18 23:36:26 +00:00
uint32_t RenderTargetTexture : : get_flags ( ) const {
2014-02-10 01:10:30 +00:00
2014-04-19 19:46:52 +00:00
return flags ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
RenderTargetTexture : : RenderTargetTexture ( Viewport * p_vp ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
vp = p_vp ;
flags = 0 ;
2014-02-10 01:10:30 +00:00
}
2016-01-17 01:41:10 +00:00
/////////////////////////////////////
class TooltipPanel : public Panel {
2017-03-18 23:36:26 +00:00
OBJ_TYPE ( TooltipPanel , Panel )
2016-01-17 01:41:10 +00:00
public :
2017-03-18 23:36:26 +00:00
TooltipPanel ( ) { } ;
2016-01-17 01:41:10 +00:00
} ;
class TooltipLabel : public Label {
2017-03-18 23:36:26 +00:00
OBJ_TYPE ( TooltipLabel , Label )
2016-01-17 01:41:10 +00:00
public :
2017-03-18 23:36:26 +00:00
TooltipLabel ( ) { } ;
2016-01-17 01:41:10 +00:00
} ;
Viewport : : GUI : : GUI ( ) {
2017-03-18 23:36:26 +00:00
mouse_focus = NULL ;
mouse_focus_button = - 1 ;
key_focus = NULL ;
mouse_over = NULL ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
cancelled_input_ID = 0 ;
tooltip = NULL ;
tooltip_popup = NULL ;
tooltip_label = NULL ;
subwindow_order_dirty = false ;
2016-01-17 01:41:10 +00:00
}
2014-02-10 01:10:30 +00:00
2016-01-17 01:41:10 +00:00
/////////////////////////////////////
2014-02-10 01:10:30 +00:00
void Viewport : : _update_stretch_transform ( ) {
if ( size_override_stretch & & size_override ) {
2014-10-28 01:54:32 +00:00
//print_line("sive override size "+size_override_size);
//print_line("rect size "+rect.size);
2017-03-18 23:36:26 +00:00
stretch_transform = Matrix32 ( ) ;
Size2 scale = rect . size / ( size_override_size + size_override_margin * 2 ) ;
2014-04-15 01:43:44 +00:00
stretch_transform . scale ( scale ) ;
2017-03-18 23:36:26 +00:00
stretch_transform . elements [ 2 ] = size_override_margin * scale ;
2015-03-22 12:40:26 +00:00
2014-02-10 01:10:30 +00:00
} else {
2017-03-18 23:36:26 +00:00
stretch_transform = Matrix32 ( ) ;
2014-02-10 01:10:30 +00:00
}
_update_global_transform ( ) ;
}
void Viewport : : _update_rect ( ) {
2014-11-06 00:20:42 +00:00
if ( ! is_inside_tree ( ) )
2014-02-10 01:10:30 +00:00
return ;
2016-01-18 22:49:11 +00:00
if ( ! render_target & & parent_control ) {
2014-02-10 01:10:30 +00:00
2016-01-18 22:49:11 +00:00
Control * c = parent_control ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
rect . pos = Point2 ( ) ;
rect . size = c - > get_size ( ) ;
2014-02-10 01:10:30 +00:00
}
VisualServer : : ViewportRect vr ;
2017-03-18 23:36:26 +00:00
vr . x = rect . pos . x ;
vr . y = rect . pos . y ;
2016-01-18 22:49:11 +00:00
2014-02-10 01:10:30 +00:00
if ( render_target ) {
2017-03-18 23:36:26 +00:00
vr . x = 0 ;
vr . y = 0 ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
vr . width = rect . size . width ;
vr . height = rect . size . height ;
2014-10-28 01:54:32 +00:00
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_set_rect ( viewport , vr ) ;
last_vp_rect = rect ;
2014-02-10 01:10:30 +00:00
if ( canvas_item . is_valid ( ) ) {
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > canvas_item_set_custom_rect ( canvas_item , true , rect ) ;
2014-02-10 01:10:30 +00:00
}
emit_signal ( " size_changed " ) ;
2014-04-10 03:18:27 +00:00
render_target_texture - > emit_changed ( ) ;
2014-02-10 01:10:30 +00:00
}
void Viewport : : _parent_resized ( ) {
_update_rect ( ) ;
}
void Viewport : : _parent_draw ( ) {
}
void Viewport : : _parent_visibility_changed ( ) {
2016-01-18 22:49:11 +00:00
if ( parent_control ) {
2014-02-10 01:10:30 +00:00
2016-01-18 22:49:11 +00:00
Control * c = parent_control ;
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > canvas_item_set_visible ( canvas_item , c - > is_visible ( ) ) ;
2014-10-03 03:10:51 +00:00
_update_listener ( ) ;
_update_listener_2d ( ) ;
2014-02-10 01:10:30 +00:00
}
}
2014-11-06 00:20:42 +00:00
void Viewport : : _vp_enter_tree ( ) {
2014-02-10 01:10:30 +00:00
2016-01-18 22:49:11 +00:00
if ( parent_control ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
Control * cparent = parent_control ;
2014-02-10 01:10:30 +00:00
RID parent_ci = cparent - > get_canvas_item ( ) ;
ERR_FAIL_COND ( ! parent_ci . is_valid ( ) ) ;
canvas_item = VisualServer : : get_singleton ( ) - > canvas_item_create ( ) ;
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > canvas_item_set_parent ( canvas_item , parent_ci ) ;
VisualServer : : get_singleton ( ) - > canvas_item_set_visible ( canvas_item , false ) ;
VisualServer : : get_singleton ( ) - > canvas_item_attach_viewport ( canvas_item , viewport ) ;
parent_control - > connect ( " resized " , this , " _parent_resized " ) ;
parent_control - > connect ( " visibility_changed " , this , " _parent_visibility_changed " ) ;
} else if ( ! parent ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_attach_to_screen ( viewport , 0 ) ;
2014-02-10 01:10:30 +00:00
}
}
2014-11-06 00:20:42 +00:00
void Viewport : : _vp_exit_tree ( ) {
2014-02-10 01:10:30 +00:00
2016-01-18 22:49:11 +00:00
if ( parent_control ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
parent_control - > disconnect ( " resized " , this , " _parent_resized " ) ;
2014-02-10 01:10:30 +00:00
}
2016-01-18 22:49:11 +00:00
if ( parent_control ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
parent_control - > disconnect ( " visibility_changed " , this , " _parent_visibility_changed " ) ;
2014-02-10 01:10:30 +00:00
}
if ( canvas_item . is_valid ( ) ) {
VisualServer : : get_singleton ( ) - > free ( canvas_item ) ;
2017-03-18 23:36:26 +00:00
canvas_item = RID ( ) ;
2014-02-10 01:10:30 +00:00
}
if ( ! parent ) {
VisualServer : : get_singleton ( ) - > viewport_detach ( viewport ) ;
}
}
void Viewport : : update_worlds ( ) {
2014-11-06 00:20:42 +00:00
if ( ! is_inside_tree ( ) )
2014-02-10 01:10:30 +00:00
return ;
Rect2 xformed_rect = ( global_canvas_transform * canvas_transform ) . affine_inverse ( ) . xform ( get_visible_rect ( ) ) ;
2017-03-18 23:36:26 +00:00
find_world_2d ( ) - > _update_viewport ( this , xformed_rect ) ;
2014-02-10 01:10:30 +00:00
find_world_2d ( ) - > _update ( ) ;
2014-11-06 00:20:42 +00:00
find_world ( ) - > _update ( get_tree ( ) - > get_frame ( ) ) ;
2014-02-10 01:10:30 +00:00
}
2014-09-15 14:33:30 +00:00
void Viewport : : _test_new_mouseover ( ObjectID new_collider ) {
2014-11-02 14:31:01 +00:00
# ifndef _3D_DISABLED
2017-03-18 23:36:26 +00:00
if ( new_collider ! = physics_object_over ) {
2014-09-15 14:33:30 +00:00
if ( physics_object_over ) {
Object * obj = ObjectDB : : get_instance ( physics_object_over ) ;
if ( obj ) {
CollisionObject * co = obj - > cast_to < CollisionObject > ( ) ;
if ( co ) {
co - > _mouse_exit ( ) ;
}
}
}
if ( new_collider ) {
Object * obj = ObjectDB : : get_instance ( new_collider ) ;
if ( obj ) {
CollisionObject * co = obj - > cast_to < CollisionObject > ( ) ;
if ( co ) {
co - > _mouse_enter ( ) ;
}
}
}
2017-03-18 23:36:26 +00:00
physics_object_over = new_collider ;
2014-09-15 14:33:30 +00:00
}
2014-11-02 14:31:01 +00:00
# endif
2014-09-15 14:33:30 +00:00
}
2014-02-10 01:10:30 +00:00
void Viewport : : _notification ( int p_what ) {
2016-03-08 23:00:52 +00:00
2017-03-18 23:36:26 +00:00
switch ( p_what ) {
2016-03-08 23:00:52 +00:00
2014-11-06 00:20:42 +00:00
case NOTIFICATION_ENTER_TREE : {
2014-02-10 01:10:30 +00:00
2016-01-18 22:49:11 +00:00
if ( get_parent ( ) ) {
2017-03-18 23:36:26 +00:00
Node * parent = get_parent ( ) ;
2016-01-18 22:49:11 +00:00
if ( parent ) {
2017-03-18 23:36:26 +00:00
parent_control = parent - > cast_to < Control > ( ) ;
2016-01-18 22:49:11 +00:00
}
}
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
parent = NULL ;
Node * parent_node = get_parent ( ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
while ( parent_node ) {
2014-02-10 01:10:30 +00:00
2016-01-22 21:54:43 +00:00
parent = parent_node - > cast_to < Viewport > ( ) ;
if ( parent )
break ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
parent_node = parent_node - > get_parent ( ) ;
2014-02-10 01:10:30 +00:00
}
2016-01-22 21:54:43 +00:00
if ( ! render_target )
_vp_enter_tree ( ) ;
2017-03-18 23:36:26 +00:00
current_canvas = find_world_2d ( ) - > get_canvas ( ) ;
VisualServer : : get_singleton ( ) - > viewport_set_scenario ( viewport , find_world ( ) - > get_scenario ( ) ) ;
VisualServer : : get_singleton ( ) - > viewport_attach_canvas ( viewport , current_canvas ) ;
2014-02-10 01:10:30 +00:00
_update_listener ( ) ;
_update_listener_2d ( ) ;
_update_rect ( ) ;
2017-03-18 23:36:26 +00:00
find_world_2d ( ) - > _register_viewport ( this , Rect2 ( ) ) ;
2014-02-10 01:10:30 +00:00
2015-09-20 20:29:36 +00:00
add_to_group ( " _viewports " ) ;
2015-09-20 16:03:46 +00:00
if ( get_tree ( ) - > is_debugging_collisions_hint ( ) ) {
//2D
2017-03-18 23:36:26 +00:00
Physics2DServer : : get_singleton ( ) - > space_set_debug_contacts ( find_world_2d ( ) - > get_space ( ) , get_tree ( ) - > get_collision_debug_contact_count ( ) ) ;
contact_2d_debug = VisualServer : : get_singleton ( ) - > canvas_item_create ( ) ;
VisualServer : : get_singleton ( ) - > canvas_item_set_parent ( contact_2d_debug , find_world_2d ( ) - > get_canvas ( ) ) ;
2015-09-20 16:03:46 +00:00
//3D
2017-03-18 23:36:26 +00:00
PhysicsServer : : get_singleton ( ) - > space_set_debug_contacts ( find_world ( ) - > get_space ( ) , get_tree ( ) - > get_collision_debug_contact_count ( ) ) ;
contact_3d_debug_multimesh = VisualServer : : get_singleton ( ) - > multimesh_create ( ) ;
VisualServer : : get_singleton ( ) - > multimesh_set_instance_count ( contact_3d_debug_multimesh , get_tree ( ) - > get_collision_debug_contact_count ( ) ) ;
VisualServer : : get_singleton ( ) - > multimesh_set_visible_instances ( contact_3d_debug_multimesh , 0 ) ;
VisualServer : : get_singleton ( ) - > multimesh_set_mesh ( contact_3d_debug_multimesh , get_tree ( ) - > get_debug_contact_mesh ( ) - > get_rid ( ) ) ;
contact_3d_debug_instance = VisualServer : : get_singleton ( ) - > instance_create ( ) ;
VisualServer : : get_singleton ( ) - > instance_set_base ( contact_3d_debug_instance , contact_3d_debug_multimesh ) ;
VisualServer : : get_singleton ( ) - > instance_set_scenario ( contact_3d_debug_instance , find_world ( ) - > get_scenario ( ) ) ;
VisualServer : : get_singleton ( ) - > instance_geometry_set_flag ( contact_3d_debug_instance , VS : : INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS , true ) ;
2015-09-20 16:03:46 +00:00
}
2014-02-10 01:10:30 +00:00
} break ;
case NOTIFICATION_READY : {
2014-07-03 08:06:23 +00:00
# ifndef _3D_DISABLED
2016-03-20 02:10:04 +00:00
if ( listeners . size ( ) & & ! listener ) {
2017-03-18 23:36:26 +00:00
Listener * first = NULL ;
for ( Set < Listener * > : : Element * E = listeners . front ( ) ; E ; E = E - > next ( ) ) {
2016-03-20 02:10:04 +00:00
2017-03-18 23:36:26 +00:00
if ( first = = NULL | | first - > is_greater_than ( E - > get ( ) ) ) {
first = E - > get ( ) ;
2016-03-20 02:10:04 +00:00
}
}
if ( first )
first - > make_current ( ) ;
}
2014-02-10 01:10:30 +00:00
if ( cameras . size ( ) & & ! camera ) {
//there are cameras but no current camera, pick first in tree and make it current
2017-03-18 23:36:26 +00:00
Camera * first = NULL ;
for ( Set < Camera * > : : Element * E = cameras . front ( ) ; E ; E = E - > next ( ) ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( first = = NULL | | first - > is_greater_than ( E - > get ( ) ) ) {
first = E - > get ( ) ;
2014-02-10 01:10:30 +00:00
}
}
if ( first )
first - > make_current ( ) ;
}
2014-07-03 08:06:23 +00:00
# endif
2014-02-10 01:10:30 +00:00
} break ;
2014-11-06 00:20:42 +00:00
case NOTIFICATION_EXIT_TREE : {
2014-02-10 01:10:30 +00:00
2016-01-25 13:30:03 +00:00
_gui_cancel_tooltip ( ) ;
2014-02-10 01:10:30 +00:00
if ( world_2d . is_valid ( ) )
world_2d - > _remove_viewport ( this ) ;
if ( ! render_target )
2014-11-06 00:20:42 +00:00
_vp_exit_tree ( ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_set_scenario ( viewport , RID ( ) ) ;
2016-03-20 02:10:04 +00:00
SpatialSoundServer : : get_singleton ( ) - > listener_set_space ( internal_listener , RID ( ) ) ;
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_remove_canvas ( viewport , current_canvas ) ;
2015-09-20 16:03:46 +00:00
if ( contact_2d_debug . is_valid ( ) ) {
VisualServer : : get_singleton ( ) - > free ( contact_2d_debug ) ;
2017-03-18 23:36:26 +00:00
contact_2d_debug = RID ( ) ;
2015-09-20 16:03:46 +00:00
}
if ( contact_3d_debug_multimesh . is_valid ( ) ) {
VisualServer : : get_singleton ( ) - > free ( contact_3d_debug_multimesh ) ;
VisualServer : : get_singleton ( ) - > free ( contact_3d_debug_instance ) ;
2017-03-18 23:36:26 +00:00
contact_3d_debug_instance = RID ( ) ;
contact_3d_debug_multimesh = RID ( ) ;
2015-09-20 16:03:46 +00:00
}
2014-02-10 01:10:30 +00:00
remove_from_group ( " _viewports " ) ;
2017-03-18 23:36:26 +00:00
parent_control = NULL ;
2014-02-10 01:10:30 +00:00
} break ;
2014-09-15 14:33:30 +00:00
case NOTIFICATION_FIXED_PROCESS : {
2017-03-18 23:36:26 +00:00
if ( gui . tooltip_timer > = 0 ) {
gui . tooltip_timer - = get_fixed_process_delta_time ( ) ;
if ( gui . tooltip_timer < 0 ) {
2016-01-25 13:30:03 +00:00
_gui_show_tooltip ( ) ;
}
}
2015-09-20 16:03:46 +00:00
if ( get_tree ( ) - > is_debugging_collisions_hint ( ) & & contact_2d_debug . is_valid ( ) ) {
VisualServer : : get_singleton ( ) - > canvas_item_clear ( contact_2d_debug ) ;
VisualServer : : get_singleton ( ) - > canvas_item_raise ( contact_2d_debug ) ;
Vector < Vector2 > points = Physics2DServer : : get_singleton ( ) - > space_get_contacts ( find_world_2d ( ) - > get_space ( ) ) ;
int point_count = Physics2DServer : : get_singleton ( ) - > space_get_contact_count ( find_world_2d ( ) - > get_space ( ) ) ;
Color ccol = get_tree ( ) - > get_debug_collision_contact_color ( ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < point_count ; i + + ) {
2015-09-20 16:03:46 +00:00
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > canvas_item_add_rect ( contact_2d_debug , Rect2 ( points [ i ] - Vector2 ( 2 , 2 ) , Vector2 ( 5 , 5 ) ) , ccol ) ;
2015-09-20 16:03:46 +00:00
}
}
if ( get_tree ( ) - > is_debugging_collisions_hint ( ) & & contact_3d_debug_multimesh . is_valid ( ) ) {
Vector < Vector3 > points = PhysicsServer : : get_singleton ( ) - > space_get_contacts ( find_world ( ) - > get_space ( ) ) ;
int point_count = PhysicsServer : : get_singleton ( ) - > space_get_contact_count ( find_world ( ) - > get_space ( ) ) ;
2017-03-18 23:36:26 +00:00
VS : : get_singleton ( ) - > multimesh_set_visible_instances ( contact_3d_debug_multimesh , point_count ) ;
2015-09-20 16:03:46 +00:00
2017-03-18 23:36:26 +00:00
if ( point_count > 0 ) {
2015-09-20 16:03:46 +00:00
AABB aabb ;
Transform t ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < point_count ; i + + ) {
2015-09-20 16:03:46 +00:00
2017-03-18 23:36:26 +00:00
if ( i = = 0 )
aabb . pos = points [ i ] ;
2015-09-20 16:03:46 +00:00
else
aabb . expand_to ( points [ i ] ) ;
2017-03-18 23:36:26 +00:00
t . origin = points [ i ] ;
VisualServer : : get_singleton ( ) - > multimesh_instance_set_transform ( contact_3d_debug_multimesh , i , t ) ;
2015-09-20 16:03:46 +00:00
}
2017-03-18 23:36:26 +00:00
aabb . grow ( aabb . get_longest_axis_size ( ) * 0.01 ) ;
VisualServer : : get_singleton ( ) - > multimesh_set_aabb ( contact_3d_debug_multimesh , aabb ) ;
2015-09-20 16:03:46 +00:00
}
}
2017-03-18 23:36:26 +00:00
if ( physics_object_picking & & ( render_target | | Input : : get_singleton ( ) - > get_mouse_mode ( ) ! = Input : : MOUSE_MODE_CAPTURED ) ) {
2015-09-20 16:03:46 +00:00
2017-03-18 23:36:26 +00:00
Vector2 last_pos ( 1e20 , 1e20 ) ;
2014-09-15 14:33:30 +00:00
CollisionObject * last_object ;
2017-03-18 23:36:26 +00:00
ObjectID last_id = 0 ;
2014-09-15 14:33:30 +00:00
PhysicsDirectSpaceState : : RayResult result ;
2017-03-18 23:36:26 +00:00
Physics2DDirectSpaceState * ss2d = Physics2DServer : : get_singleton ( ) - > space_get_direct_state ( find_world_2d ( ) - > get_space ( ) ) ;
2014-09-15 14:33:30 +00:00
2017-03-18 23:36:26 +00:00
bool motion_tested = false ;
2014-09-15 14:33:30 +00:00
2017-03-18 23:36:26 +00:00
while ( physics_picking_events . size ( ) ) {
2014-09-15 14:33:30 +00:00
InputEvent ev = physics_picking_events . front ( ) - > get ( ) ;
physics_picking_events . pop_front ( ) ;
Vector2 pos ;
2017-03-18 23:36:26 +00:00
switch ( ev . type ) {
2014-09-15 14:33:30 +00:00
case InputEvent : : MOUSE_MOTION : {
2017-03-18 23:36:26 +00:00
pos . x = ev . mouse_motion . x ;
pos . y = ev . mouse_motion . y ;
motion_tested = true ;
physics_last_mousepos = pos ;
2014-09-15 14:33:30 +00:00
} break ;
case InputEvent : : MOUSE_BUTTON : {
2017-03-18 23:36:26 +00:00
pos . x = ev . mouse_button . x ;
pos . y = ev . mouse_button . y ;
2014-09-15 14:33:30 +00:00
} break ;
case InputEvent : : SCREEN_DRAG : {
2017-03-18 23:36:26 +00:00
pos . x = ev . screen_drag . x ;
pos . y = ev . screen_drag . y ;
2014-09-15 14:33:30 +00:00
} break ;
case InputEvent : : SCREEN_TOUCH : {
2017-03-18 23:36:26 +00:00
pos . x = ev . screen_touch . x ;
pos . y = ev . screen_touch . y ;
2014-09-15 14:33:30 +00:00
} break ;
}
2015-03-22 04:46:18 +00:00
if ( ss2d ) {
//send to 2D
uint64_t frame = get_tree ( ) - > get_frame ( ) ;
Vector2 point = get_canvas_transform ( ) . affine_inverse ( ) . xform ( pos ) ;
Physics2DDirectSpaceState : : ShapeResult res [ 64 ] ;
2017-03-18 23:36:26 +00:00
int rc = ss2d - > intersect_point ( point , res , 64 , Set < RID > ( ) , 0xFFFFFFFF , 0xFFFFFFFF , true ) ;
for ( int i = 0 ; i < rc ; i + + ) {
2015-03-22 04:46:18 +00:00
2015-04-03 12:42:05 +00:00
if ( res [ i ] . collider_id & & res [ i ] . collider ) {
2017-03-18 23:36:26 +00:00
CollisionObject2D * co = res [ i ] . collider - > cast_to < CollisionObject2D > ( ) ;
2015-03-22 04:46:18 +00:00
if ( co ) {
2017-03-18 23:36:26 +00:00
Map < ObjectID , uint64_t > : : Element * E = physics_2d_mouseover . find ( res [ i ] . collider_id ) ;
2015-03-22 04:46:18 +00:00
if ( ! E ) {
2017-03-18 23:36:26 +00:00
E = physics_2d_mouseover . insert ( res [ i ] . collider_id , frame ) ;
2015-03-22 04:46:18 +00:00
co - > _mouse_enter ( ) ;
} else {
2017-03-18 23:36:26 +00:00
E - > get ( ) = frame ;
2015-03-22 04:46:18 +00:00
}
2017-03-18 23:36:26 +00:00
co - > _input_event ( this , ev , res [ i ] . shape ) ;
2015-03-22 04:46:18 +00:00
}
}
}
2017-03-18 23:36:26 +00:00
List < Map < ObjectID , uint64_t > : : Element * > to_erase ;
2015-03-22 04:46:18 +00:00
2017-03-18 23:36:26 +00:00
for ( Map < ObjectID , uint64_t > : : Element * E = physics_2d_mouseover . front ( ) ; E ; E = E - > next ( ) ) {
if ( E - > get ( ) ! = frame ) {
Object * o = ObjectDB : : get_instance ( E - > key ( ) ) ;
2015-03-22 04:46:18 +00:00
if ( o ) {
2017-03-18 23:36:26 +00:00
CollisionObject2D * co = o - > cast_to < CollisionObject2D > ( ) ;
2015-03-22 04:46:18 +00:00
if ( co ) {
co - > _mouse_exit ( ) ;
}
}
to_erase . push_back ( E ) ;
}
}
2017-03-18 23:36:26 +00:00
while ( to_erase . size ( ) ) {
2015-03-22 04:46:18 +00:00
physics_2d_mouseover . erase ( to_erase . front ( ) - > get ( ) ) ;
to_erase . pop_front ( ) ;
}
}
# ifndef _3D_DISABLED
2017-03-18 23:36:26 +00:00
bool captured = false ;
2014-09-15 14:33:30 +00:00
2017-03-18 23:36:26 +00:00
if ( physics_object_capture ! = 0 ) {
2014-09-15 14:33:30 +00:00
Object * obj = ObjectDB : : get_instance ( physics_object_capture ) ;
if ( obj ) {
CollisionObject * co = obj - > cast_to < CollisionObject > ( ) ;
if ( co ) {
2017-03-18 23:36:26 +00:00
co - > _input_event ( camera , ev , Vector3 ( ) , Vector3 ( ) , 0 ) ;
captured = true ;
if ( ev . type = = InputEvent : : MOUSE_BUTTON & & ev . mouse_button . button_index = = 1 & & ! ev . mouse_button . pressed ) {
physics_object_capture = 0 ;
2014-09-15 14:33:30 +00:00
}
} else {
2017-03-18 23:36:26 +00:00
physics_object_capture = 0 ;
2014-09-15 14:33:30 +00:00
}
} else {
2017-03-18 23:36:26 +00:00
physics_object_capture = 0 ;
2014-09-15 14:33:30 +00:00
}
}
if ( captured ) {
//none
2017-03-18 23:36:26 +00:00
} else if ( pos = = last_pos ) {
2014-09-15 14:33:30 +00:00
if ( last_id ) {
if ( ObjectDB : : get_instance ( last_id ) ) {
//good, exists
2017-03-18 23:36:26 +00:00
last_object - > _input_event ( camera , ev , result . position , result . normal , result . shape ) ;
if ( last_object - > get_capture_input_on_drag ( ) & & ev . type = = InputEvent : : MOUSE_BUTTON & & ev . mouse_button . button_index = = 1 & & ev . mouse_button . pressed ) {
physics_object_capture = last_id ;
2014-09-15 14:33:30 +00:00
}
}
}
} else {
if ( camera ) {
Vector3 from = camera - > project_ray_origin ( pos ) ;
Vector3 dir = camera - > project_ray_normal ( pos ) ;
PhysicsDirectSpaceState * space = PhysicsServer : : get_singleton ( ) - > space_get_direct_state ( find_world ( ) - > get_space ( ) ) ;
if ( space ) {
2017-03-18 23:36:26 +00:00
bool col = space - > intersect_ray ( from , from + dir * 10000 , result , Set < RID > ( ) , 0xFFFFFFFF , 0xFFFFFFFF , true ) ;
ObjectID new_collider = 0 ;
2014-09-15 14:33:30 +00:00
if ( col ) {
2014-10-03 03:10:51 +00:00
2014-09-15 14:33:30 +00:00
if ( result . collider ) {
2014-10-03 03:10:51 +00:00
2014-09-15 14:33:30 +00:00
CollisionObject * co = result . collider - > cast_to < CollisionObject > ( ) ;
if ( co ) {
2014-10-03 03:10:51 +00:00
2017-03-18 23:36:26 +00:00
co - > _input_event ( camera , ev , result . position , result . normal , result . shape ) ;
last_object = co ;
last_id = result . collider_id ;
new_collider = last_id ;
if ( co - > get_capture_input_on_drag ( ) & & ev . type = = InputEvent : : MOUSE_BUTTON & & ev . mouse_button . button_index = = 1 & & ev . mouse_button . pressed ) {
physics_object_capture = last_id ;
2014-09-15 14:33:30 +00:00
}
}
}
}
2017-03-18 23:36:26 +00:00
if ( ev . type = = InputEvent : : MOUSE_MOTION ) {
2014-09-15 14:33:30 +00:00
_test_new_mouseover ( new_collider ) ;
}
}
2017-03-18 23:36:26 +00:00
last_pos = pos ;
2014-09-15 14:33:30 +00:00
}
}
}
2017-03-18 23:36:26 +00:00
if ( ! motion_tested & & camera & & physics_last_mousepos ! = Vector2 ( 1e20 , 1e20 ) ) {
2014-09-15 14:33:30 +00:00
//test anyway for mouseenter/exit because objects might move
Vector3 from = camera - > project_ray_origin ( physics_last_mousepos ) ;
Vector3 dir = camera - > project_ray_normal ( physics_last_mousepos ) ;
PhysicsDirectSpaceState * space = PhysicsServer : : get_singleton ( ) - > space_get_direct_state ( find_world ( ) - > get_space ( ) ) ;
if ( space ) {
2017-03-18 23:36:26 +00:00
bool col = space - > intersect_ray ( from , from + dir * 10000 , result , Set < RID > ( ) , 0xFFFFFFFF , 0xFFFFFFFF , true ) ;
ObjectID new_collider = 0 ;
2014-09-15 14:33:30 +00:00
if ( col ) {
if ( result . collider ) {
CollisionObject * co = result . collider - > cast_to < CollisionObject > ( ) ;
if ( co ) {
2017-03-18 23:36:26 +00:00
new_collider = result . collider_id ;
2014-09-15 14:33:30 +00:00
}
}
}
_test_new_mouseover ( new_collider ) ;
}
2014-11-02 14:31:01 +00:00
# endif
2015-03-22 04:46:18 +00:00
}
2014-09-15 14:33:30 +00:00
}
} break ;
2014-02-10 01:10:30 +00:00
}
}
RID Viewport : : get_viewport ( ) const {
2016-03-08 23:00:52 +00:00
return viewport ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_rect ( const Rect2 & p_rect ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( rect = = p_rect )
2014-02-10 01:10:30 +00:00
return ;
2017-03-18 23:36:26 +00:00
rect = p_rect ;
2014-10-28 01:54:32 +00:00
2014-02-10 01:10:30 +00:00
_update_rect ( ) ;
_update_stretch_transform ( ) ;
}
Rect2 Viewport : : get_visible_rect ( ) const {
Rect2 r ;
2016-03-08 23:00:52 +00:00
2017-03-18 23:36:26 +00:00
if ( rect . pos = = Vector2 ( ) & & rect . size = = Size2 ( ) ) {
2016-03-08 23:00:52 +00:00
2017-03-18 23:36:26 +00:00
r = Rect2 ( Point2 ( ) , Size2 ( OS : : get_singleton ( ) - > get_video_mode ( ) . width , OS : : get_singleton ( ) - > get_video_mode ( ) . height ) ) ;
2014-02-10 01:10:30 +00:00
} else {
2016-03-08 23:00:52 +00:00
2017-03-18 23:36:26 +00:00
r = Rect2 ( rect . pos , rect . size ) ;
2014-02-10 01:10:30 +00:00
}
if ( size_override ) {
2017-03-18 23:36:26 +00:00
r . size = size_override_size ;
2014-02-10 01:10:30 +00:00
}
return r ;
}
Rect2 Viewport : : get_rect ( ) const {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
return rect ;
}
void Viewport : : _update_listener ( ) {
2017-03-18 23:36:26 +00:00
if ( is_inside_tree ( ) & & audio_listener & & ( camera | | listener ) & & ( ! get_parent ( ) | | ( get_parent ( ) - > cast_to < Control > ( ) & & get_parent ( ) - > cast_to < Control > ( ) - > is_visible ( ) ) ) ) {
2016-03-20 02:10:04 +00:00
SpatialSoundServer : : get_singleton ( ) - > listener_set_space ( internal_listener , find_world ( ) - > get_sound_space ( ) ) ;
2014-02-10 01:10:30 +00:00
} else {
2016-03-20 02:10:04 +00:00
SpatialSoundServer : : get_singleton ( ) - > listener_set_space ( internal_listener , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
}
}
void Viewport : : _update_listener_2d ( ) {
2017-05-04 23:21:45 +00:00
if ( is_inside_tree ( ) & & audio_listener_2d & & ( ! get_parent ( ) | | ( get_parent ( ) - > cast_to < Control > ( ) & & get_parent ( ) - > cast_to < Control > ( ) - > is_visible ( ) ) ) )
2016-03-20 02:10:04 +00:00
SpatialSound2DServer : : get_singleton ( ) - > listener_set_space ( internal_listener_2d , find_world_2d ( ) - > get_sound_space ( ) ) ;
2014-02-10 01:10:30 +00:00
else
2016-03-20 02:10:04 +00:00
SpatialSound2DServer : : get_singleton ( ) - > listener_set_space ( internal_listener_2d , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
}
void Viewport : : set_as_audio_listener ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
if ( p_enable = = audio_listener )
2014-02-10 01:10:30 +00:00
return ;
2017-03-18 23:36:26 +00:00
audio_listener = p_enable ;
2014-02-10 01:10:30 +00:00
_update_listener ( ) ;
}
bool Viewport : : is_audio_listener ( ) const {
2017-03-18 23:36:26 +00:00
return audio_listener ;
2014-02-10 01:10:30 +00:00
}
void Viewport : : set_as_audio_listener_2d ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
if ( p_enable = = audio_listener_2d )
2014-02-10 01:10:30 +00:00
return ;
2017-03-18 23:36:26 +00:00
audio_listener_2d = p_enable ;
2014-02-10 01:10:30 +00:00
_update_listener_2d ( ) ;
}
bool Viewport : : is_audio_listener_2d ( ) const {
2017-03-18 23:36:26 +00:00
return audio_listener_2d ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_canvas_transform ( const Matrix32 & p_transform ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
canvas_transform = p_transform ;
VisualServer : : get_singleton ( ) - > viewport_set_canvas_transform ( viewport , find_world_2d ( ) - > get_canvas ( ) , canvas_transform ) ;
2014-02-10 01:10:30 +00:00
Matrix32 xform = ( global_canvas_transform * canvas_transform ) . affine_inverse ( ) ;
Size2 ss = get_visible_rect ( ) . size ;
2017-03-18 23:36:26 +00:00
SpatialSound2DServer : : get_singleton ( ) - > listener_set_transform ( internal_listener_2d , Matrix32 ( 0 , xform . xform ( ss * 0.5 ) ) ) ;
Vector2 ss2 = ss * xform . get_scale ( ) ;
float panrange = MAX ( ss2 . x , ss2 . y ) ;
2014-02-10 01:10:30 +00:00
2016-03-20 02:10:04 +00:00
SpatialSound2DServer : : get_singleton ( ) - > listener_set_param ( internal_listener_2d , SpatialSound2DServer : : LISTENER_PARAM_PAN_RANGE , panrange ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
Matrix32 Viewport : : get_canvas_transform ( ) const {
2014-02-10 01:10:30 +00:00
return canvas_transform ;
}
void Viewport : : _update_global_transform ( ) {
Matrix32 sxform = stretch_transform * global_canvas_transform ;
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_set_global_canvas_transform ( viewport , sxform ) ;
2014-02-10 01:10:30 +00:00
Matrix32 xform = ( sxform * canvas_transform ) . affine_inverse ( ) ;
Size2 ss = get_visible_rect ( ) . size ;
2017-03-18 23:36:26 +00:00
SpatialSound2DServer : : get_singleton ( ) - > listener_set_transform ( internal_listener_2d , Matrix32 ( 0 , xform . xform ( ss * 0.5 ) ) ) ;
Vector2 ss2 = ss * xform . get_scale ( ) ;
float panrange = MAX ( ss2 . x , ss2 . y ) ;
2014-02-10 01:10:30 +00:00
2016-03-20 02:10:04 +00:00
SpatialSound2DServer : : get_singleton ( ) - > listener_set_param ( internal_listener_2d , SpatialSound2DServer : : LISTENER_PARAM_PAN_RANGE , panrange ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_global_canvas_transform ( const Matrix32 & p_transform ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
global_canvas_transform = p_transform ;
2014-02-10 01:10:30 +00:00
_update_global_transform ( ) ;
}
2017-03-18 23:36:26 +00:00
Matrix32 Viewport : : get_global_canvas_transform ( ) const {
2014-02-10 01:10:30 +00:00
return global_canvas_transform ;
}
2016-03-20 02:10:04 +00:00
void Viewport : : _listener_transform_changed_notify ( ) {
# ifndef _3D_DISABLED
if ( listener )
SpatialSoundServer : : get_singleton ( ) - > listener_set_transform ( internal_listener , listener - > get_listener_transform ( ) ) ;
# endif
}
2017-03-18 23:36:26 +00:00
void Viewport : : _listener_set ( Listener * p_listener ) {
2016-03-20 02:10:04 +00:00
# ifndef _3D_DISABLED
if ( listener = = p_listener )
return ;
listener = p_listener ;
_update_listener ( ) ;
_listener_transform_changed_notify ( ) ;
# endif
}
2017-03-18 23:36:26 +00:00
bool Viewport : : _listener_add ( Listener * p_listener ) {
2016-03-20 02:10:04 +00:00
listeners . insert ( p_listener ) ;
return listeners . size ( ) = = 1 ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : _listener_remove ( Listener * p_listener ) {
2016-03-20 02:10:04 +00:00
listeners . erase ( p_listener ) ;
if ( listener = = p_listener ) {
listener = NULL ;
}
}
# ifndef _3D_DISABLED
2017-03-18 23:36:26 +00:00
void Viewport : : _listener_make_next_current ( Listener * p_exclude ) {
2016-03-20 02:10:04 +00:00
if ( listeners . size ( ) > 0 ) {
2017-03-18 23:36:26 +00:00
for ( Set < Listener * > : : Element * E = listeners . front ( ) ; E ; E = E - > next ( ) ) {
2016-03-20 02:10:04 +00:00
if ( p_exclude = = E - > get ( ) )
continue ;
if ( ! E - > get ( ) - > is_inside_tree ( ) )
continue ;
if ( listener ! = NULL )
return ;
E - > get ( ) - > make_current ( ) ;
}
2017-03-18 23:36:26 +00:00
} else {
2016-03-20 02:10:04 +00:00
// Attempt to reset listener to the camera position
if ( camera ! = NULL ) {
_update_listener ( ) ;
_camera_transform_changed_notify ( ) ;
}
}
}
# endif
2014-02-10 01:10:30 +00:00
void Viewport : : _camera_transform_changed_notify ( ) {
# ifndef _3D_DISABLED
2016-03-20 02:10:04 +00:00
// If there is an active listener in the scene, it takes priority over the camera
if ( camera & & ! listener )
SpatialSoundServer : : get_singleton ( ) - > listener_set_transform ( internal_listener , camera - > get_camera_transform ( ) ) ;
2014-02-10 01:10:30 +00:00
# endif
}
2017-03-18 23:36:26 +00:00
void Viewport : : _camera_set ( Camera * p_camera ) {
2014-02-10 01:10:30 +00:00
# ifndef _3D_DISABLED
2017-03-18 23:36:26 +00:00
if ( camera = = p_camera )
2014-02-10 01:10:30 +00:00
return ;
if ( camera & & find_world ( ) . is_valid ( ) ) {
camera - > notification ( Camera : : NOTIFICATION_LOST_CURRENT ) ;
}
2017-03-18 23:36:26 +00:00
camera = p_camera ;
2014-02-10 01:10:30 +00:00
if ( camera )
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_attach_camera ( viewport , camera - > get_camera ( ) ) ;
2014-02-10 01:10:30 +00:00
else
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_attach_camera ( viewport , RID ( ) ) ;
2014-02-10 01:10:30 +00:00
if ( camera & & find_world ( ) . is_valid ( ) ) {
camera - > notification ( Camera : : NOTIFICATION_BECAME_CURRENT ) ;
}
_update_listener ( ) ;
_camera_transform_changed_notify ( ) ;
# endif
}
2017-03-18 23:36:26 +00:00
bool Viewport : : _camera_add ( Camera * p_camera ) {
2016-01-31 18:09:52 +00:00
cameras . insert ( p_camera ) ;
2017-03-18 23:36:26 +00:00
return cameras . size ( ) = = 1 ;
2016-01-31 18:09:52 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : _camera_remove ( Camera * p_camera ) {
2016-01-31 18:09:52 +00:00
cameras . erase ( p_camera ) ;
2017-03-18 23:36:26 +00:00
if ( camera = = p_camera ) {
camera = NULL ;
2016-01-31 18:09:52 +00:00
}
}
2016-03-11 02:01:56 +00:00
# ifndef _3D_DISABLED
2017-03-18 23:36:26 +00:00
void Viewport : : _camera_make_next_current ( Camera * p_exclude ) {
2016-01-31 18:09:52 +00:00
2017-03-18 23:36:26 +00:00
for ( Set < Camera * > : : Element * E = cameras . front ( ) ; E ; E = E - > next ( ) ) {
2016-01-31 18:09:52 +00:00
2017-03-18 23:36:26 +00:00
if ( p_exclude = = E - > get ( ) )
2016-01-31 18:09:52 +00:00
continue ;
if ( ! E - > get ( ) - > is_inside_tree ( ) )
continue ;
2017-03-18 23:36:26 +00:00
if ( camera ! = NULL )
2016-01-31 18:09:52 +00:00
return ;
E - > get ( ) - > make_current ( ) ;
}
}
2016-03-11 02:01:56 +00:00
# endif
2014-02-10 01:10:30 +00:00
void Viewport : : set_transparent_background ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
transparent_bg = p_enable ;
VS : : get_singleton ( ) - > viewport_set_transparent_background ( viewport , p_enable ) ;
2014-02-10 01:10:30 +00:00
}
bool Viewport : : has_transparent_background ( ) const {
return transparent_bg ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_world_2d ( const Ref < World2D > & p_world_2d ) {
if ( world_2d = = p_world_2d )
2016-07-13 18:51:38 +00:00
return ;
2017-03-18 23:36:26 +00:00
if ( parent & & parent - > find_world_2d ( ) = = p_world_2d ) {
2016-07-13 18:51:38 +00:00
WARN_PRINT ( " Unable to use parent world as world_2d " ) ;
return ;
}
if ( is_inside_tree ( ) ) {
find_world_2d ( ) - > _remove_viewport ( this ) ;
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_remove_canvas ( viewport , current_canvas ) ;
2016-07-13 18:51:38 +00:00
}
if ( p_world_2d . is_valid ( ) )
2017-03-18 23:36:26 +00:00
world_2d = p_world_2d ;
2016-07-13 18:51:38 +00:00
else {
WARN_PRINT ( " Invalid world " ) ;
2017-03-18 23:36:26 +00:00
world_2d = Ref < World2D > ( memnew ( World2D ) ) ;
2016-07-13 18:51:38 +00:00
}
2014-02-10 01:10:30 +00:00
_update_listener_2d ( ) ;
2016-07-13 18:51:38 +00:00
if ( is_inside_tree ( ) ) {
2017-03-18 23:36:26 +00:00
current_canvas = find_world_2d ( ) - > get_canvas ( ) ;
VisualServer : : get_singleton ( ) - > viewport_attach_canvas ( viewport , current_canvas ) ;
find_world_2d ( ) - > _register_viewport ( this , Rect2 ( ) ) ;
2014-02-10 01:10:30 +00:00
}
}
2017-03-18 23:36:26 +00:00
Ref < World2D > Viewport : : find_world_2d ( ) const {
2014-02-10 01:10:30 +00:00
if ( world_2d . is_valid ( ) )
return world_2d ;
else if ( parent )
return parent - > find_world_2d ( ) ;
else
return Ref < World2D > ( ) ;
}
void Viewport : : _propagate_enter_world ( Node * p_node ) {
2017-03-18 23:36:26 +00:00
if ( p_node ! = this ) {
2014-02-10 01:10:30 +00:00
2014-11-06 00:20:42 +00:00
if ( ! p_node - > is_inside_tree ( ) ) //may not have entered scene yet
2014-02-10 01:10:30 +00:00
return ;
Spatial * s = p_node - > cast_to < Spatial > ( ) ;
if ( s ) {
s - > notification ( Spatial : : NOTIFICATION_ENTER_WORLD ) ;
} else {
Viewport * v = p_node - > cast_to < Viewport > ( ) ;
if ( v ) {
if ( v - > world . is_valid ( ) )
return ;
}
}
}
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
_propagate_enter_world ( p_node - > get_child ( i ) ) ;
}
}
2017-03-18 23:36:26 +00:00
void Viewport : : _propagate_viewport_notification ( Node * p_node , int p_what ) {
2016-05-11 14:46:08 +00:00
p_node - > notification ( p_what ) ;
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + ) {
2016-05-11 14:46:08 +00:00
Node * c = p_node - > get_child ( i ) ;
if ( c - > cast_to < Viewport > ( ) )
continue ;
2017-03-18 23:36:26 +00:00
_propagate_viewport_notification ( c , p_what ) ;
2016-05-11 14:46:08 +00:00
}
}
2014-02-10 01:10:30 +00:00
void Viewport : : _propagate_exit_world ( Node * p_node ) {
2017-03-18 23:36:26 +00:00
if ( p_node ! = this ) {
2014-02-10 01:10:30 +00:00
2014-11-06 00:20:42 +00:00
if ( ! p_node - > is_inside_tree ( ) ) //may have exited scene already
2014-02-10 01:10:30 +00:00
return ;
Spatial * s = p_node - > cast_to < Spatial > ( ) ;
if ( s ) {
2016-07-08 17:27:19 +00:00
s - > notification ( Spatial : : NOTIFICATION_EXIT_WORLD , true ) ;
2014-02-10 01:10:30 +00:00
} else {
Viewport * v = p_node - > cast_to < Viewport > ( ) ;
if ( v ) {
if ( v - > world . is_valid ( ) )
return ;
}
}
}
2017-03-18 23:36:26 +00:00
for ( int i = 0 ; i < p_node - > get_child_count ( ) ; i + + ) {
2014-02-10 01:10:30 +00:00
_propagate_exit_world ( p_node - > get_child ( i ) ) ;
}
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_world ( const Ref < World > & p_world ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( world = = p_world )
2014-02-10 01:10:30 +00:00
return ;
2014-11-06 00:20:42 +00:00
if ( is_inside_tree ( ) )
2014-02-10 01:10:30 +00:00
_propagate_exit_world ( this ) ;
# ifndef _3D_DISABLED
if ( find_world ( ) . is_valid ( ) & & camera )
camera - > notification ( Camera : : NOTIFICATION_LOST_CURRENT ) ;
# endif
2017-03-18 23:36:26 +00:00
world = p_world ;
2014-02-10 01:10:30 +00:00
2014-11-06 00:20:42 +00:00
if ( is_inside_tree ( ) )
2014-02-10 01:10:30 +00:00
_propagate_enter_world ( this ) ;
# ifndef _3D_DISABLED
if ( find_world ( ) . is_valid ( ) & & camera )
camera - > notification ( Camera : : NOTIFICATION_BECAME_CURRENT ) ;
# endif
//propagate exit
2014-11-06 00:20:42 +00:00
if ( is_inside_tree ( ) ) {
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_set_scenario ( viewport , find_world ( ) - > get_scenario ( ) ) ;
2014-02-10 01:10:30 +00:00
}
_update_listener ( ) ;
}
2017-03-18 23:36:26 +00:00
Ref < World > Viewport : : get_world ( ) const {
2014-02-10 01:10:30 +00:00
return world ;
}
2017-03-18 23:36:26 +00:00
Ref < World2D > Viewport : : get_world_2d ( ) const {
2016-07-13 18:51:38 +00:00
return world_2d ;
}
2017-03-18 23:36:26 +00:00
Ref < World > Viewport : : find_world ( ) const {
2014-02-10 01:10:30 +00:00
2014-04-15 01:43:44 +00:00
if ( own_world . is_valid ( ) )
return own_world ;
else if ( world . is_valid ( ) )
2014-02-10 01:10:30 +00:00
return world ;
else if ( parent )
return parent - > find_world ( ) ;
else
return Ref < World > ( ) ;
}
2017-03-18 23:36:26 +00:00
Listener * Viewport : : get_listener ( ) const {
2016-03-20 02:10:04 +00:00
return listener ;
}
2017-03-18 23:36:26 +00:00
Camera * Viewport : : get_camera ( ) const {
2014-02-10 01:10:30 +00:00
return camera ;
}
Matrix32 Viewport : : get_final_transform ( ) const {
return stretch_transform * global_canvas_transform ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_size_override ( bool p_enable , const Size2 & p_size , const Vector2 & p_margin ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( size_override = = p_enable & & p_size = = size_override_size )
2014-02-10 01:10:30 +00:00
return ;
2017-03-18 23:36:26 +00:00
size_override = p_enable ;
if ( p_size . x > = 0 | | p_size . y > = 0 ) {
size_override_size = p_size ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
size_override_margin = p_margin ;
2014-02-10 01:10:30 +00:00
_update_rect ( ) ;
_update_stretch_transform ( ) ;
}
Size2 Viewport : : get_size_override ( ) const {
return size_override_size ;
}
bool Viewport : : is_size_override_enabled ( ) const {
return size_override ;
}
void Viewport : : set_size_override_stretch ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
if ( p_enable = = size_override_stretch )
2014-02-10 01:10:30 +00:00
return ;
2017-03-18 23:36:26 +00:00
size_override_stretch = p_enable ;
2014-02-10 01:10:30 +00:00
if ( size_override ) {
_update_rect ( ) ;
}
_update_stretch_transform ( ) ;
}
bool Viewport : : is_size_override_stretch_enabled ( ) const {
2016-06-10 01:30:34 +00:00
return size_override_stretch ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_as_render_target ( bool p_enable ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
if ( render_target = = p_enable )
2014-02-10 01:10:30 +00:00
return ;
2017-03-18 23:36:26 +00:00
render_target = p_enable ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
VS : : get_singleton ( ) - > viewport_set_as_render_target ( viewport , p_enable ) ;
2014-11-06 00:20:42 +00:00
if ( is_inside_tree ( ) ) {
2014-02-10 01:10:30 +00:00
if ( p_enable )
2014-11-06 00:20:42 +00:00
_vp_exit_tree ( ) ;
2014-02-10 01:10:30 +00:00
else
2014-11-06 00:20:42 +00:00
_vp_enter_tree ( ) ;
2014-02-10 01:10:30 +00:00
}
if ( p_enable ) {
render_target_texture_rid = VS : : get_singleton ( ) - > viewport_get_render_target_texture ( viewport ) ;
} else {
2017-03-18 23:36:26 +00:00
render_target_texture_rid = RID ( ) ;
2014-02-10 01:10:30 +00:00
}
2014-04-10 03:18:27 +00:00
2014-05-14 04:22:15 +00:00
render_target_texture - > set_flags ( render_target_texture - > flags ) ;
2014-04-10 03:18:27 +00:00
render_target_texture - > emit_changed ( ) ;
2016-05-17 21:27:15 +00:00
update_configuration_warning ( ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
bool Viewport : : is_set_as_render_target ( ) const {
2014-02-10 01:10:30 +00:00
return render_target ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_render_target_update_mode ( RenderTargetUpdateMode p_mode ) {
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
render_target_update_mode = p_mode ;
VS : : get_singleton ( ) - > viewport_set_render_target_update_mode ( viewport , VS : : RenderTargetUpdateMode ( p_mode ) ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-18 23:36:26 +00:00
Viewport : : RenderTargetUpdateMode Viewport : : get_render_target_update_mode ( ) const {
2014-02-10 01:10:30 +00:00
return render_target_update_mode ;
}
//RID get_render_target_texture() const;
2017-03-18 23:36:26 +00:00
void Viewport : : queue_screen_capture ( ) {
2014-02-10 01:10:30 +00:00
VS : : get_singleton ( ) - > viewport_queue_screen_capture ( viewport ) ;
}
Image Viewport : : get_screen_capture ( ) const {
return VS : : get_singleton ( ) - > viewport_get_screen_capture ( viewport ) ;
}
Ref < RenderTargetTexture > Viewport : : get_render_target_texture ( ) const {
return render_target_texture ;
}
2014-04-10 03:18:27 +00:00
void Viewport : : set_render_target_vflip ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
render_target_vflip = p_enable ;
VisualServer : : get_singleton ( ) - > viewport_set_render_target_vflip ( viewport , p_enable ) ;
2014-04-10 03:18:27 +00:00
}
2017-03-18 23:36:26 +00:00
bool Viewport : : get_render_target_vflip ( ) const {
2014-04-10 03:18:27 +00:00
return render_target_vflip ;
}
2015-02-21 09:35:06 +00:00
void Viewport : : set_render_target_clear_on_new_frame ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
render_target_clear_on_new_frame = p_enable ;
VisualServer : : get_singleton ( ) - > viewport_set_render_target_clear_on_new_frame ( viewport , p_enable ) ;
2015-02-21 09:35:06 +00:00
}
2017-03-18 23:36:26 +00:00
bool Viewport : : get_render_target_clear_on_new_frame ( ) const {
2015-02-21 09:35:06 +00:00
return render_target_clear_on_new_frame ;
}
void Viewport : : render_target_clear ( ) {
//render_target_clear=true;
VisualServer : : get_singleton ( ) - > viewport_render_target_clear ( viewport ) ;
}
2014-04-10 03:18:27 +00:00
2014-05-14 04:22:15 +00:00
void Viewport : : set_render_target_filter ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
if ( ! render_target )
2016-07-13 18:51:38 +00:00
return ;
2017-03-18 23:36:26 +00:00
render_target_texture - > set_flags ( p_enable ? int ( Texture : : FLAG_FILTER ) : int ( 0 ) ) ;
2014-05-14 04:22:15 +00:00
}
2017-03-18 23:36:26 +00:00
bool Viewport : : get_render_target_filter ( ) const {
2014-05-14 04:22:15 +00:00
2017-03-18 23:36:26 +00:00
return ( render_target_texture - > get_flags ( ) & Texture : : FLAG_FILTER ) ! = 0 ;
2014-05-14 04:22:15 +00:00
}
2014-08-14 13:31:38 +00:00
void Viewport : : set_render_target_gen_mipmaps ( bool p_enable ) {
//render_target_texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0));
2017-03-18 23:36:26 +00:00
render_target_gen_mipmaps = p_enable ;
2014-08-14 13:31:38 +00:00
}
2017-03-18 23:36:26 +00:00
bool Viewport : : get_render_target_gen_mipmaps ( ) const {
2014-08-14 13:31:38 +00:00
//return (render_target_texture->get_flags()&Texture::FLAG_FILTER)!=0;
return render_target_gen_mipmaps ;
}
2014-04-18 14:43:54 +00:00
Matrix32 Viewport : : _get_input_pre_xform ( ) const {
Matrix32 pre_xf ;
if ( render_target ) {
2017-03-18 23:36:26 +00:00
if ( to_screen_rect ! = Rect2 ( ) ) {
2015-03-22 12:40:26 +00:00
2017-03-18 23:36:26 +00:00
pre_xf . elements [ 2 ] = - to_screen_rect . pos ;
pre_xf . scale ( rect . size / to_screen_rect . size ) ;
2016-06-29 21:27:12 +00:00
}
2014-04-18 14:43:54 +00:00
} else {
2017-03-18 23:36:26 +00:00
pre_xf . elements [ 2 ] = - rect . pos ;
2014-04-18 14:43:54 +00:00
}
return pre_xf ;
}
2016-09-10 16:29:07 +00:00
Vector2 Viewport : : _get_window_offset ( ) const {
if ( parent_control ) {
return ( parent_control - > get_viewport ( ) - > get_final_transform ( ) * parent_control - > get_global_transform_with_canvas ( ) ) . get_origin ( ) ;
}
return Vector2 ( ) ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : _make_input_local ( InputEvent & ev ) {
2016-07-18 20:16:49 +00:00
2017-03-18 23:36:26 +00:00
switch ( ev . type ) {
2014-04-10 03:18:27 +00:00
case InputEvent : : MOUSE_BUTTON : {
2016-09-10 16:29:07 +00:00
Vector2 vp_ofs = _get_window_offset ( ) ;
2016-07-18 20:16:49 +00:00
2014-04-18 14:43:54 +00:00
Matrix32 ai = get_final_transform ( ) . affine_inverse ( ) * _get_input_pre_xform ( ) ;
2017-03-18 23:36:26 +00:00
Vector2 g = ai . xform ( Vector2 ( ev . mouse_button . global_x , ev . mouse_button . global_y ) ) ;
Vector2 l = ai . xform ( Vector2 ( ev . mouse_button . x , ev . mouse_button . y ) - vp_ofs ) ;
2016-07-18 20:16:49 +00:00
2017-03-18 23:36:26 +00:00
ev . mouse_button . x = l . x ;
ev . mouse_button . y = l . y ;
ev . mouse_button . global_x = g . x ;
ev . mouse_button . global_y = g . y ;
2014-04-10 03:18:27 +00:00
} break ;
case InputEvent : : MOUSE_MOTION : {
2016-09-10 16:29:07 +00:00
Vector2 vp_ofs = _get_window_offset ( ) ;
2016-07-18 20:16:49 +00:00
2014-04-18 14:43:54 +00:00
Matrix32 ai = get_final_transform ( ) . affine_inverse ( ) * _get_input_pre_xform ( ) ;
2017-03-18 23:36:26 +00:00
Vector2 g = ai . xform ( Vector2 ( ev . mouse_motion . global_x , ev . mouse_motion . global_y ) ) ;
Vector2 l = ai . xform ( Vector2 ( ev . mouse_motion . x , ev . mouse_motion . y ) - vp_ofs ) ;
Vector2 r = ai . basis_xform ( Vector2 ( ev . mouse_motion . relative_x , ev . mouse_motion . relative_y ) ) ;
Vector2 s = ai . basis_xform ( Vector2 ( ev . mouse_motion . speed_x , ev . mouse_motion . speed_y ) ) ;
ev . mouse_motion . x = l . x ;
ev . mouse_motion . y = l . y ;
ev . mouse_motion . global_x = g . x ;
ev . mouse_motion . global_y = g . y ;
ev . mouse_motion . relative_x = r . x ;
ev . mouse_motion . relative_y = r . y ;
ev . mouse_motion . speed_x = s . x ;
ev . mouse_motion . speed_y = s . y ;
2014-04-10 03:18:27 +00:00
} break ;
case InputEvent : : SCREEN_TOUCH : {
2016-09-10 16:29:07 +00:00
Vector2 vp_ofs = _get_window_offset ( ) ;
2016-07-18 20:16:49 +00:00
2014-04-18 14:43:54 +00:00
Matrix32 ai = get_final_transform ( ) . affine_inverse ( ) * _get_input_pre_xform ( ) ;
2017-03-18 23:36:26 +00:00
Vector2 t = ai . xform ( Vector2 ( ev . screen_touch . x , ev . screen_touch . y ) - vp_ofs ) ;
2016-07-18 20:16:49 +00:00
2017-03-18 23:36:26 +00:00
ev . screen_touch . x = t . x ;
ev . screen_touch . y = t . y ;
2014-04-10 03:18:27 +00:00
} break ;
case InputEvent : : SCREEN_DRAG : {
2016-09-10 16:29:07 +00:00
Vector2 vp_ofs = _get_window_offset ( ) ;
2016-07-18 20:16:49 +00:00
2014-04-18 14:43:54 +00:00
Matrix32 ai = get_final_transform ( ) . affine_inverse ( ) * _get_input_pre_xform ( ) ;
2017-03-18 23:36:26 +00:00
Vector2 t = ai . xform ( Vector2 ( ev . screen_drag . x , ev . screen_drag . y ) - vp_ofs ) ;
Vector2 r = ai . basis_xform ( Vector2 ( ev . screen_drag . relative_x , ev . screen_drag . relative_y ) ) ;
Vector2 s = ai . basis_xform ( Vector2 ( ev . screen_drag . speed_x , ev . screen_drag . speed_y ) ) ;
ev . screen_drag . x = t . x ;
ev . screen_drag . y = t . y ;
ev . screen_drag . relative_x = r . x ;
ev . screen_drag . relative_y = r . y ;
ev . screen_drag . speed_x = s . x ;
ev . screen_drag . speed_y = s . y ;
2014-04-10 03:18:27 +00:00
} break ;
}
}
2017-03-18 23:36:26 +00:00
void Viewport : : _vp_input_text ( const String & p_text ) {
2016-01-17 01:41:10 +00:00
if ( gui . key_focus ) {
2017-03-18 23:36:26 +00:00
gui . key_focus - > call ( " set_text " , p_text ) ;
2016-01-17 01:41:10 +00:00
}
}
2014-04-10 03:18:27 +00:00
2017-03-18 23:36:26 +00:00
void Viewport : : _vp_input ( const InputEvent & p_ev ) {
2014-04-10 03:18:27 +00:00
2016-01-17 01:41:10 +00:00
if ( disable_input )
return ;
2016-01-19 09:45:12 +00:00
# ifdef TOOLS_ENABLED
2017-04-02 07:46:51 +00:00
if ( get_tree ( ) - > is_editor_hint ( ) & & get_tree ( ) - > get_edited_scene_root ( ) & & get_tree ( ) - > get_edited_scene_root ( ) - > is_a_parent_of ( this ) ) {
2016-01-18 22:49:11 +00:00
return ;
}
2016-01-19 09:45:12 +00:00
# endif
2016-01-18 22:49:11 +00:00
if ( parent_control & & ! parent_control - > is_visible ( ) )
return ;
2017-03-18 23:36:26 +00:00
if ( render_target & & to_screen_rect = = Rect2 ( ) )
2014-04-10 03:18:27 +00:00
return ; //if render target, can't get input events
//this one handles system input, p_ev are in system coordinates
//they are converted to viewport coordinates
2016-03-08 23:00:52 +00:00
InputEvent ev = p_ev ;
2014-04-10 03:18:27 +00:00
_make_input_local ( ev ) ;
input ( ev ) ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : _vp_unhandled_input ( const InputEvent & p_ev ) {
2014-04-10 03:18:27 +00:00
2016-01-18 22:49:11 +00:00
if ( disable_input )
return ;
2016-01-19 09:45:12 +00:00
# ifdef TOOLS_ENABLED
2017-04-02 07:46:51 +00:00
if ( get_tree ( ) - > is_editor_hint ( ) & & get_tree ( ) - > get_edited_scene_root ( ) & & get_tree ( ) - > get_edited_scene_root ( ) - > is_a_parent_of ( this ) ) {
2016-01-18 22:49:11 +00:00
return ;
}
2016-01-19 09:45:12 +00:00
# endif
2016-01-18 22:49:11 +00:00
if ( parent_control & & ! parent_control - > is_visible ( ) )
return ;
2014-09-15 14:33:30 +00:00
2017-03-18 23:36:26 +00:00
if ( render_target & & to_screen_rect = = Rect2 ( ) )
2014-04-10 03:18:27 +00:00
return ; //if render target, can't get input events
//this one handles system input, p_ev are in system coordinates
//they are converted to viewport coordinates
InputEvent ev = p_ev ;
_make_input_local ( ev ) ;
unhandled_input ( ev ) ;
}
2015-05-12 11:17:09 +00:00
Vector2 Viewport : : get_mouse_pos ( ) const {
2016-09-10 16:29:07 +00:00
return ( get_final_transform ( ) . affine_inverse ( ) * _get_input_pre_xform ( ) ) . xform ( Input : : get_singleton ( ) - > get_mouse_pos ( ) - _get_window_offset ( ) ) ;
2015-05-12 11:17:09 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : warp_mouse ( const Vector2 & p_pos ) {
2015-02-14 22:22:06 +00:00
Vector2 gpos = ( get_final_transform ( ) . affine_inverse ( ) * _get_input_pre_xform ( ) ) . affine_inverse ( ) . xform ( p_pos ) ;
Input : : get_singleton ( ) - > warp_mouse_pos ( gpos ) ;
}
2016-01-17 01:41:10 +00:00
void Viewport : : _gui_sort_subwindows ( ) {
if ( ! gui . subwindow_order_dirty )
return ;
gui . modal_stack . sort_custom < Control : : CComparator > ( ) ;
gui . subwindows . sort_custom < Control : : CComparator > ( ) ;
2017-03-18 23:36:26 +00:00
gui . subwindow_order_dirty = false ;
2016-01-17 01:41:10 +00:00
}
void Viewport : : _gui_sort_modal_stack ( ) {
gui . modal_stack . sort_custom < Control : : CComparator > ( ) ;
}
void Viewport : : _gui_sort_roots ( ) {
if ( ! gui . roots_order_dirty )
return ;
gui . roots . sort_custom < Control : : CComparator > ( ) ;
2017-03-18 23:36:26 +00:00
gui . roots_order_dirty = false ;
2016-01-17 01:41:10 +00:00
}
void Viewport : : _gui_cancel_tooltip ( ) {
2017-03-18 23:36:26 +00:00
gui . tooltip = NULL ;
gui . tooltip_timer = - 1 ;
2016-01-25 13:30:03 +00:00
if ( gui . tooltip_popup ) {
gui . tooltip_popup - > queue_delete ( ) ;
2017-03-18 23:36:26 +00:00
gui . tooltip_popup = NULL ;
2016-01-25 13:30:03 +00:00
}
2016-01-17 01:41:10 +00:00
}
void Viewport : : _gui_show_tooltip ( ) {
if ( ! gui . tooltip ) {
return ;
}
2017-03-18 23:36:26 +00:00
String tooltip = gui . tooltip - > get_tooltip ( gui . tooltip - > get_global_transform ( ) . xform_inv ( gui . tooltip_pos ) ) ;
if ( tooltip . length ( ) = = 0 )
2016-01-17 01:41:10 +00:00
return ; // bye
2016-01-25 13:30:03 +00:00
if ( gui . tooltip_popup ) {
memdelete ( gui . tooltip_popup ) ;
2017-03-18 23:36:26 +00:00
gui . tooltip_popup = NULL ;
2016-01-25 13:30:03 +00:00
}
2016-01-17 01:41:10 +00:00
2016-01-25 13:30:03 +00:00
Control * rp = gui . tooltip - > get_root_parent_control ( ) ;
if ( ! rp )
2016-01-17 01:41:10 +00:00
return ;
2016-01-25 13:30:03 +00:00
2017-03-18 23:36:26 +00:00
gui . tooltip_popup = memnew ( TooltipPanel ) ;
2016-01-25 13:30:03 +00:00
rp - > add_child ( gui . tooltip_popup ) ;
gui . tooltip_popup - > force_parent_owned ( ) ;
2017-03-18 23:36:26 +00:00
gui . tooltip_label = memnew ( TooltipLabel ) ;
2016-01-25 13:30:03 +00:00
gui . tooltip_popup - > add_child ( gui . tooltip_label ) ;
gui . tooltip_popup - > set_as_toplevel ( true ) ;
gui . tooltip_popup - > hide ( ) ;
2017-03-18 23:36:26 +00:00
Ref < StyleBox > ttp = gui . tooltip_label - > get_stylebox ( " panel " , " TooltipPanel " ) ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . tooltip_label - > set_anchor_and_margin ( MARGIN_LEFT , Control : : ANCHOR_BEGIN , ttp - > get_margin ( MARGIN_LEFT ) ) ;
gui . tooltip_label - > set_anchor_and_margin ( MARGIN_TOP , Control : : ANCHOR_BEGIN , ttp - > get_margin ( MARGIN_TOP ) ) ;
gui . tooltip_label - > set_anchor_and_margin ( MARGIN_RIGHT , Control : : ANCHOR_END , ttp - > get_margin ( MARGIN_RIGHT ) ) ;
gui . tooltip_label - > set_anchor_and_margin ( MARGIN_BOTTOM , Control : : ANCHOR_END , ttp - > get_margin ( MARGIN_BOTTOM ) ) ;
2016-01-17 01:41:10 +00:00
gui . tooltip_label - > set_text ( tooltip ) ;
2017-03-18 23:36:26 +00:00
Rect2 r ( gui . tooltip_pos + Point2 ( 10 , 10 ) , gui . tooltip_label - > get_combined_minimum_size ( ) + ttp - > get_minimum_size ( ) ) ;
2016-01-17 01:41:10 +00:00
Rect2 vr = gui . tooltip_label - > get_viewport_rect ( ) ;
2017-03-18 23:36:26 +00:00
if ( r . size . x + r . pos . x > vr . size . x )
r . pos . x = vr . size . x - r . size . x ;
else if ( r . pos . x < 0 )
r . pos . x = 0 ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
if ( r . size . y + r . pos . y > vr . size . y )
r . pos . y = vr . size . y - r . size . y ;
else if ( r . pos . y < 0 )
r . pos . y = 0 ;
2016-01-17 01:41:10 +00:00
2016-01-25 13:30:03 +00:00
gui . tooltip_popup - > set_global_pos ( r . pos ) ;
2016-01-17 01:41:10 +00:00
gui . tooltip_popup - > set_size ( r . size ) ;
gui . tooltip_popup - > raise ( ) ;
gui . tooltip_popup - > show ( ) ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : _gui_call_input ( Control * p_control , const InputEvent & p_input ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
// _block();
2016-06-20 20:16:52 +00:00
2016-06-27 12:59:43 +00:00
InputEvent ev = p_input ;
2016-06-20 20:16:52 +00:00
//mouse wheel events can't be stopped
2017-03-18 23:36:26 +00:00
bool cant_stop_me_now = ( ev . type = = InputEvent : : MOUSE_BUTTON & &
( ev . mouse_button . button_index = = BUTTON_WHEEL_DOWN | |
ev . mouse_button . button_index = = BUTTON_WHEEL_UP | |
ev . mouse_button . button_index = = BUTTON_WHEEL_LEFT | |
ev . mouse_button . button_index = = BUTTON_WHEEL_RIGHT ) ) ;
2016-06-20 20:16:52 +00:00
2017-03-18 23:36:26 +00:00
CanvasItem * ci = p_control ;
while ( ci ) {
2016-06-07 05:39:40 +00:00
Control * control = ci - > cast_to < Control > ( ) ;
if ( control ) {
2017-03-18 23:36:26 +00:00
control - > call_multilevel ( SceneStringNames : : get_singleton ( ) - > _input_event , ev ) ;
2016-06-07 05:39:40 +00:00
if ( gui . key_event_accepted )
break ;
if ( ! control - > is_inside_tree ( ) )
break ;
2017-03-18 23:36:26 +00:00
control - > emit_signal ( SceneStringNames : : get_singleton ( ) - > input_event , ev ) ;
2016-06-07 05:39:40 +00:00
if ( ! control - > is_inside_tree ( ) | | control - > is_set_as_toplevel ( ) )
break ;
if ( gui . key_event_accepted )
break ;
2017-03-18 23:36:26 +00:00
if ( ! cant_stop_me_now & & control - > data . stop_mouse & & ( ev . type = = InputEvent : : MOUSE_BUTTON | | ev . type = = InputEvent : : MOUSE_MOTION ) )
2016-06-07 05:39:40 +00:00
break ;
}
if ( ci - > is_set_as_toplevel ( ) )
break ;
2017-03-18 23:36:26 +00:00
ev = ev . xform_by ( ci - > get_transform ( ) ) ; //transform event upwards
ci = ci - > get_parent_item ( ) ;
2016-01-17 01:41:10 +00:00
}
//_unblock();
}
2017-03-18 23:36:26 +00:00
Control * Viewport : : _gui_find_control ( const Point2 & p_global ) {
2016-01-17 01:41:10 +00:00
_gui_sort_subwindows ( ) ;
2017-03-18 23:36:26 +00:00
for ( List < Control * > : : Element * E = gui . subwindows . back ( ) ; E ; E = E - > prev ( ) ) {
2016-01-17 01:41:10 +00:00
Control * sw = E - > get ( ) ;
if ( ! sw - > is_visible ( ) )
continue ;
Matrix32 xform ;
CanvasItem * pci = sw - > get_parent_item ( ) ;
if ( pci )
2017-03-18 23:36:26 +00:00
xform = pci - > get_global_transform_with_canvas ( ) ;
2016-02-03 12:31:18 +00:00
else
2017-03-18 23:36:26 +00:00
xform = sw - > get_canvas_transform ( ) ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
Control * ret = _gui_find_control_at_pos ( sw , p_global , xform , gui . focus_inv_xform ) ;
2016-01-17 01:41:10 +00:00
if ( ret )
return ret ;
}
2016-01-25 13:39:55 +00:00
_gui_sort_roots ( ) ;
2017-03-18 23:36:26 +00:00
for ( List < Control * > : : Element * E = gui . roots . back ( ) ; E ; E = E - > prev ( ) ) {
2016-01-17 01:41:10 +00:00
Control * sw = E - > get ( ) ;
if ( ! sw - > is_visible ( ) )
continue ;
Matrix32 xform ;
CanvasItem * pci = sw - > get_parent_item ( ) ;
if ( pci )
2017-03-18 23:36:26 +00:00
xform = pci - > get_global_transform_with_canvas ( ) ;
2016-02-03 12:31:18 +00:00
else
2017-03-18 23:36:26 +00:00
xform = sw - > get_canvas_transform ( ) ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
Control * ret = _gui_find_control_at_pos ( sw , p_global , xform , gui . focus_inv_xform ) ;
2016-01-17 01:41:10 +00:00
if ( ret )
return ret ;
}
return NULL ;
}
2017-03-18 23:36:26 +00:00
Control * Viewport : : _gui_find_control_at_pos ( CanvasItem * p_node , const Point2 & p_global , const Matrix32 & p_xform , Matrix32 & r_inv_xform ) {
2016-01-17 01:41:10 +00:00
if ( p_node - > cast_to < Viewport > ( ) )
return NULL ;
2017-03-18 23:36:26 +00:00
Control * c = p_node - > cast_to < Control > ( ) ;
2016-01-17 01:41:10 +00:00
if ( c ) {
2017-03-18 23:36:26 +00:00
// print_line("at "+String(c->get_path())+" POS "+c->get_pos()+" bt "+p_xform);
2016-01-17 01:41:10 +00:00
}
//subwindows first!!
if ( p_node - > is_hidden ( ) ) {
//return _find_next_visible_control_at_pos(p_node,p_global,r_inv_xform);
return NULL ; //canvas item hidden, discard
}
Matrix32 matrix = p_xform * p_node - > get_transform ( ) ;
2016-10-08 10:33:10 +00:00
// matrix.basis_determinant() == 0.0f implies that node does not exist on scene
2017-03-18 23:36:26 +00:00
if ( matrix . basis_determinant ( ) = = 0.0f )
2016-10-08 10:33:10 +00:00
return NULL ;
2016-01-17 01:41:10 +00:00
if ( ! c | | ! c - > clips_input ( ) | | c - > has_point ( matrix . affine_inverse ( ) . xform ( p_global ) ) ) {
2017-03-18 23:36:26 +00:00
for ( int i = p_node - > get_child_count ( ) - 1 ; i > = 0 ; i - - ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
if ( p_node = = gui . tooltip_popup )
2016-01-17 01:41:10 +00:00
continue ;
CanvasItem * ci = p_node - > get_child ( i ) - > cast_to < CanvasItem > ( ) ;
if ( ! ci | | ci - > is_set_as_toplevel ( ) )
continue ;
2017-03-18 23:36:26 +00:00
Control * ret = _gui_find_control_at_pos ( ci , p_global , matrix , r_inv_xform ) ;
2016-01-17 01:41:10 +00:00
if ( ret )
return ret ;
}
}
if ( ! c )
return NULL ;
matrix . affine_invert ( ) ;
//conditions for considering this as a valid control for return
2017-03-18 23:36:26 +00:00
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 ) ) ) ) {
r_inv_xform = matrix ;
2016-01-17 01:41:10 +00:00
return c ;
} else
return NULL ;
}
void Viewport : : _gui_input_event ( InputEvent p_event ) {
2017-03-18 23:36:26 +00:00
if ( p_event . ID = = gui . cancelled_input_ID ) {
2016-01-17 01:41:10 +00:00
return ;
}
//?
2017-03-18 23:36:26 +00:00
// if (!is_visible()) {
// return; //simple and plain
// }
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
switch ( p_event . type ) {
2016-01-17 01:41:10 +00:00
case InputEvent : : MOUSE_BUTTON : {
2017-03-18 23:36:26 +00:00
gui . key_event_accepted = false ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
Point2 mpos = Point2 ( p_event . mouse_button . x , p_event . mouse_button . y ) ;
2016-01-17 01:41:10 +00:00
if ( p_event . mouse_button . pressed ) {
Size2 pos = mpos ;
2017-03-18 23:36:26 +00:00
if ( gui . mouse_focus & & p_event . mouse_button . button_index ! = gui . mouse_focus_button ) {
2016-01-17 01:41:10 +00:00
//do not steal mouse focus and stuff
} else {
_gui_sort_modal_stack ( ) ;
while ( ! gui . modal_stack . empty ( ) ) {
Control * top = gui . modal_stack . back ( ) - > get ( ) ;
Vector2 pos = top - > get_global_transform_with_canvas ( ) . affine_inverse ( ) . xform ( mpos ) ;
if ( ! top - > has_point ( pos ) ) {
2017-03-18 23:36:26 +00:00
if ( top - > data . modal_exclusive | | top - > data . modal_frame = = OS : : get_singleton ( ) - > get_frames_drawn ( ) ) {
2016-01-17 01:41:10 +00:00
//cancel event, sorry, modal exclusive EATS UP ALL
2016-06-27 14:22:13 +00:00
//alternative, you can't pop out a window the same frame it was made modal (fixes many issues)
2016-01-17 01:41:10 +00:00
get_tree ( ) - > set_input_as_handled ( ) ;
return ; // no one gets the event if exclusive NO ONE
}
top - > notification ( Control : : NOTIFICATION_MODAL_CLOSE ) ;
top - > _modal_stack_remove ( ) ;
top - > hide ( ) ;
} else {
break ;
}
}
2016-02-03 12:31:18 +00:00
//Matrix32 parent_xform;
2016-01-17 01:41:10 +00:00
//if (data.parent_canvas_item)
// parent_xform=data.parent_canvas_item->get_global_transform();
gui . mouse_focus = _gui_find_control ( pos ) ;
//print_line("has mf "+itos(gui.mouse_focus!=NULL));
2017-03-18 23:36:26 +00:00
gui . mouse_focus_button = p_event . mouse_button . button_index ;
2016-01-17 01:41:10 +00:00
if ( ! gui . mouse_focus ) {
break ;
}
2017-03-18 23:36:26 +00:00
if ( p_event . mouse_button . button_index = = BUTTON_LEFT ) {
gui . drag_accum = Vector2 ( ) ;
gui . drag_attempted = false ;
2016-01-17 01:41:10 +00:00
}
}
p_event . mouse_button . global_x = pos . x ;
p_event . mouse_button . global_y = pos . y ;
pos = gui . focus_inv_xform . xform ( pos ) ;
p_event . mouse_button . x = pos . x ;
p_event . mouse_button . y = pos . y ;
# ifdef DEBUG_ENABLED
if ( ScriptDebugger : : get_singleton ( ) ) {
Array arr ;
arr . push_back ( gui . mouse_focus - > get_path ( ) ) ;
arr . push_back ( gui . mouse_focus - > get_type ( ) ) ;
2017-03-18 23:36:26 +00:00
ScriptDebugger : : get_singleton ( ) - > send_message ( " click_ctrl " , arr ) ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
/*if (bool(GLOBAL_DEF("debug/print_clicked_control",false))) {
2016-01-17 01:41:10 +00:00
print_line ( String ( gui . mouse_focus - > get_path ( ) ) + " - " + pos ) ;
} */
# endif
2017-03-18 23:36:26 +00:00
if ( gui . mouse_focus - > get_focus_mode ( ) ! = Control : : FOCUS_NONE & & gui . mouse_focus ! = gui . key_focus & & p_event . mouse_button . button_index = = BUTTON_LEFT ) {
2016-01-17 01:41:10 +00:00
// also get keyboard focus
gui . mouse_focus - > grab_focus ( ) ;
}
if ( gui . mouse_focus - > can_process ( ) ) {
2017-03-18 23:36:26 +00:00
_gui_call_input ( gui . mouse_focus , p_event ) ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
get_tree ( ) - > call_group ( SceneTree : : GROUP_CALL_REALTIME , " windows " , " _cancel_input_ID " , p_event . ID ) ;
2016-01-17 01:41:10 +00:00
get_tree ( ) - > set_input_as_handled ( ) ;
2017-03-18 23:36:26 +00:00
if ( gui . drag_data . get_type ( ) ! = Variant : : NIL & & p_event . mouse_button . button_index = = BUTTON_LEFT ) {
2016-07-01 13:34:38 +00:00
//alternate drop use (when using force_drag(), as proposed by #5342
2017-03-18 23:36:26 +00:00
if ( gui . mouse_focus & & gui . mouse_focus - > can_drop_data ( pos , gui . drag_data ) ) {
gui . mouse_focus - > drop_data ( pos , gui . drag_data ) ;
2016-07-01 13:34:38 +00:00
}
2017-03-18 23:36:26 +00:00
gui . drag_data = Variant ( ) ;
2016-07-01 13:34:38 +00:00
if ( gui . drag_preview ) {
2017-03-18 23:36:26 +00:00
memdelete ( gui . drag_preview ) ;
gui . drag_preview = NULL ;
2016-07-01 13:34:38 +00:00
}
2017-03-18 23:36:26 +00:00
_propagate_viewport_notification ( this , NOTIFICATION_DRAG_END ) ;
2016-07-01 13:34:38 +00:00
//change mouse accordingly
}
2016-01-25 13:30:03 +00:00
_gui_cancel_tooltip ( ) ;
//gui.tooltip_popup->hide();
2016-01-17 01:41:10 +00:00
} else {
2017-03-18 23:36:26 +00:00
if ( gui . drag_data . get_type ( ) ! = Variant : : NIL & & p_event . mouse_button . button_index = = BUTTON_LEFT ) {
2016-01-17 01:41:10 +00:00
2016-07-01 13:34:38 +00:00
if ( gui . mouse_over ) {
2016-01-17 01:41:10 +00:00
Size2 pos = mpos ;
pos = gui . focus_inv_xform . xform ( pos ) ;
2017-03-18 23:36:26 +00:00
if ( gui . mouse_over - > can_drop_data ( pos , gui . drag_data ) ) {
gui . mouse_over - > drop_data ( pos , gui . drag_data ) ;
2016-06-20 13:28:24 +00:00
}
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
if ( gui . drag_preview & & p_event . mouse_button . button_index = = BUTTON_LEFT ) {
memdelete ( gui . drag_preview ) ;
gui . drag_preview = NULL ;
2016-07-01 13:34:38 +00:00
}
2017-03-18 23:36:26 +00:00
gui . drag_data = Variant ( ) ;
_propagate_viewport_notification ( this , NOTIFICATION_DRAG_END ) ;
2016-07-01 13:34:38 +00:00
//change mouse accordingly
}
if ( ! gui . mouse_focus ) {
//release event is only sent if a mouse focus (previously pressed button) exists
2016-01-17 01:41:10 +00:00
break ;
}
Size2 pos = mpos ;
p_event . mouse_button . global_x = pos . x ;
p_event . mouse_button . global_y = pos . y ;
pos = gui . focus_inv_xform . xform ( pos ) ;
p_event . mouse_button . x = pos . x ;
p_event . mouse_button . y = pos . y ;
if ( gui . mouse_focus - > can_process ( ) ) {
2017-03-18 23:36:26 +00:00
_gui_call_input ( gui . mouse_focus , p_event ) ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
if ( p_event . mouse_button . button_index = = gui . mouse_focus_button ) {
gui . mouse_focus = NULL ;
gui . mouse_focus_button = - 1 ;
2016-01-17 01:41:10 +00:00
}
2016-07-01 13:34:38 +00:00
/*if (gui.drag_data.get_type()!=Variant::NIL && p_event.mouse_button.button_index==BUTTON_LEFT) {
2016-05-11 14:46:08 +00:00
_propagate_viewport_notification ( this , NOTIFICATION_DRAG_END ) ;
2016-01-17 01:41:10 +00:00
gui . drag_data = Variant ( ) ; //always clear
2016-07-01 13:34:38 +00:00
} */
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
get_tree ( ) - > call_group ( SceneTree : : GROUP_CALL_REALTIME , " windows " , " _cancel_input_ID " , p_event . ID ) ;
2016-01-17 01:41:10 +00:00
get_tree ( ) - > set_input_as_handled ( ) ;
}
} break ;
case InputEvent : : MOUSE_MOTION : {
2017-03-18 23:36:26 +00:00
gui . key_event_accepted = false ;
Point2 mpos = Point2 ( p_event . mouse_motion . x , p_event . mouse_motion . y ) ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . last_mouse_pos = mpos ;
2016-01-17 01:41:10 +00:00
Control * over = NULL ;
// D&D
2017-03-18 23:36:26 +00:00
if ( ! gui . drag_attempted & & gui . mouse_focus & & p_event . mouse_motion . button_mask & BUTTON_MASK_LEFT ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . drag_accum + = Point2 ( p_event . mouse_motion . relative_x , p_event . mouse_motion . relative_y ) ;
2016-01-17 01:41:10 +00:00
float len = gui . drag_accum . length ( ) ;
2017-03-18 23:36:26 +00:00
if ( len > 10 ) {
gui . drag_data = gui . mouse_focus - > get_drag_data ( gui . focus_inv_xform . xform ( mpos ) - gui . drag_accum ) ;
if ( gui . drag_data . get_type ( ) ! = Variant : : NIL ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . mouse_focus = NULL ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
gui . drag_attempted = true ;
if ( gui . drag_data . get_type ( ) ! = Variant : : NIL ) {
2016-05-11 14:46:08 +00:00
2017-03-18 23:36:26 +00:00
_propagate_viewport_notification ( this , NOTIFICATION_DRAG_BEGIN ) ;
2016-05-11 14:46:08 +00:00
}
2016-01-17 01:41:10 +00:00
}
}
if ( gui . mouse_focus ) {
2017-03-18 23:36:26 +00:00
over = gui . mouse_focus ;
2016-01-17 01:41:10 +00:00
//recompute focus_inv_xform again here
} else {
over = _gui_find_control ( mpos ) ;
}
2017-03-18 23:36:26 +00:00
if ( gui . drag_data . get_type ( ) = = Variant : : NIL & & over & & ! gui . modal_stack . empty ( ) ) {
2016-01-17 01:41:10 +00:00
Control * top = gui . modal_stack . back ( ) - > get ( ) ;
2017-03-18 23:36:26 +00:00
if ( over ! = top & & ! top - > is_a_parent_of ( over ) ) {
2016-01-17 01:41:10 +00:00
break ; // don't send motion event to anything below modal stack top
}
}
2017-03-18 23:36:26 +00:00
if ( over ! = gui . mouse_over ) {
2016-01-17 01:41:10 +00:00
if ( gui . mouse_over )
gui . mouse_over - > notification ( Control : : NOTIFICATION_MOUSE_EXIT ) ;
2016-05-21 13:29:25 +00:00
2016-05-20 11:56:42 +00:00
_gui_cancel_tooltip ( ) ;
2016-01-17 01:41:10 +00:00
if ( over )
over - > notification ( Control : : NOTIFICATION_MOUSE_ENTER ) ;
}
2017-03-18 23:36:26 +00:00
gui . mouse_over = over ;
2016-01-17 01:41:10 +00:00
if ( gui . drag_preview ) {
gui . drag_preview - > set_pos ( mpos ) ;
}
if ( ! over ) {
OS : : get_singleton ( ) - > set_cursor_shape ( OS : : CURSOR_ARROW ) ;
break ;
}
2016-02-27 12:47:39 +00:00
Matrix32 localizer = over - > get_global_transform_with_canvas ( ) . affine_inverse ( ) ;
2016-01-17 01:41:10 +00:00
Size2 pos = localizer . xform ( mpos ) ;
2017-03-18 23:36:26 +00:00
Vector2 speed = localizer . basis_xform ( Point2 ( p_event . mouse_motion . speed_x , p_event . mouse_motion . speed_y ) ) ;
Vector2 rel = localizer . basis_xform ( Point2 ( p_event . mouse_motion . relative_x , p_event . mouse_motion . relative_y ) ) ;
2016-01-17 01:41:10 +00:00
p_event . mouse_motion . global_x = mpos . x ;
p_event . mouse_motion . global_y = mpos . y ;
2017-03-18 23:36:26 +00:00
p_event . mouse_motion . speed_x = speed . x ;
p_event . mouse_motion . speed_y = speed . y ;
p_event . mouse_motion . relative_x = rel . x ;
p_event . mouse_motion . relative_y = rel . y ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
if ( p_event . mouse_motion . button_mask = = 0 ) {
2016-01-17 01:41:10 +00:00
//nothing pressed
2017-03-18 23:36:26 +00:00
bool can_tooltip = true ;
2016-01-17 01:41:10 +00:00
if ( ! gui . modal_stack . empty ( ) ) {
2017-03-18 23:36:26 +00:00
if ( gui . modal_stack . back ( ) - > get ( ) ! = over & & ! gui . modal_stack . back ( ) - > get ( ) - > is_a_parent_of ( over ) )
can_tooltip = false ;
2016-01-17 01:41:10 +00:00
}
2016-07-18 20:14:57 +00:00
bool is_tooltip_shown = false ;
2016-01-17 01:41:10 +00:00
2016-07-19 04:27:12 +00:00
if ( gui . tooltip_popup ) {
if ( can_tooltip ) {
String tooltip = over - > get_tooltip ( gui . tooltip - > get_global_transform ( ) . xform_inv ( mpos ) ) ;
if ( tooltip . length ( ) = = 0 )
_gui_cancel_tooltip ( ) ;
else if ( tooltip = = gui . tooltip_label - > get_text ( ) )
is_tooltip_shown = true ;
2017-03-18 23:36:26 +00:00
} else
2016-07-19 04:27:12 +00:00
_gui_cancel_tooltip ( ) ;
2016-07-18 20:14:57 +00:00
}
if ( can_tooltip & & ! is_tooltip_shown ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . tooltip = over ;
gui . tooltip_pos = mpos ; //(parent_xform * get_transform()).affine_inverse().xform(pos);
gui . tooltip_timer = gui . tooltip_delay ;
2016-01-17 01:41:10 +00:00
}
}
2016-02-27 12:47:39 +00:00
//pos = gui.focus_inv_xform.xform(pos);
2016-01-17 01:41:10 +00:00
p_event . mouse_motion . x = pos . x ;
p_event . mouse_motion . y = pos . y ;
Control : : CursorShape cursor_shape = over - > get_cursor_shape ( pos ) ;
2017-03-18 23:36:26 +00:00
OS : : get_singleton ( ) - > set_cursor_shape ( ( OS : : CursorShape ) cursor_shape ) ;
2016-01-17 01:41:10 +00:00
if ( over - > can_process ( ) ) {
2017-03-18 23:36:26 +00:00
_gui_call_input ( over , p_event ) ;
2016-01-17 01:41:10 +00:00
}
get_tree ( ) - > set_input_as_handled ( ) ;
2017-03-18 23:36:26 +00:00
if ( gui . drag_data . get_type ( ) ! = Variant : : NIL & & p_event . mouse_motion . button_mask & BUTTON_MASK_LEFT ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
bool can_drop = over - > can_drop_data ( pos , gui . drag_data ) ;
2016-05-11 14:46:08 +00:00
if ( ! can_drop ) {
2017-03-18 23:36:26 +00:00
OS : : get_singleton ( ) - > set_cursor_shape ( OS : : CURSOR_FORBIDDEN ) ;
2016-05-11 14:46:08 +00:00
} else {
2017-03-18 23:36:26 +00:00
OS : : get_singleton ( ) - > set_cursor_shape ( OS : : CURSOR_CAN_DROP ) ;
2016-05-11 14:46:08 +00:00
}
2016-01-17 01:41:10 +00:00
//change mouse accordingly i guess
}
} break ;
case InputEvent : : ACTION :
case InputEvent : : JOYSTICK_BUTTON :
2016-07-30 10:38:42 +00:00
case InputEvent : : JOYSTICK_MOTION :
2016-01-17 01:41:10 +00:00
case InputEvent : : KEY : {
2016-07-01 13:42:33 +00:00
if ( gui . key_focus & & ! gui . key_focus - > is_visible ( ) ) {
gui . key_focus - > release_focus ( ) ;
}
2017-03-18 23:36:26 +00:00
if ( gui . key_focus ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . key_event_accepted = false ;
2016-01-17 01:41:10 +00:00
if ( gui . key_focus - > can_process ( ) ) {
2017-03-18 23:36:26 +00:00
gui . key_focus - > call_multilevel ( " _input_event " , p_event ) ;
2016-01-17 01:41:10 +00:00
if ( gui . key_focus ) //maybe lost it
2017-03-18 23:36:26 +00:00
gui . key_focus - > emit_signal ( SceneStringNames : : get_singleton ( ) - > input_event , p_event ) ;
2016-01-17 01:41:10 +00:00
}
if ( gui . key_event_accepted ) {
2016-07-23 19:25:00 +00:00
get_tree ( ) - > set_input_as_handled ( ) ;
2016-01-17 01:41:10 +00:00
break ;
}
}
if ( p_event . is_pressed ( ) & & p_event . is_action ( " ui_cancel " ) & & ! gui . modal_stack . empty ( ) ) {
_gui_sort_modal_stack ( ) ;
Control * top = gui . modal_stack . back ( ) - > get ( ) ;
if ( ! top - > data . modal_exclusive ) {
top - > notification ( Control : : NOTIFICATION_MODAL_CLOSE ) ;
top - > _modal_stack_remove ( ) ;
top - > hide ( ) ;
}
}
2017-03-18 23:36:26 +00:00
Control * from = gui . key_focus ? gui . key_focus : NULL ; //hmm
2016-01-17 01:41:10 +00:00
//keyboard focus
//if (from && p_event.key.pressed && !p_event.key.mod.alt && !p_event.key.mod.meta && !p_event.key.mod.command) {
if ( from & & p_event . is_pressed ( ) ) {
2017-03-18 23:36:26 +00:00
Control * next = NULL ;
2016-01-17 01:41:10 +00:00
if ( p_event . is_action ( " ui_focus_next " ) ) {
next = from - > find_next_valid_focus ( ) ;
}
if ( p_event . is_action ( " ui_focus_prev " ) ) {
next = from - > find_prev_valid_focus ( ) ;
}
if ( p_event . is_action ( " ui_up " ) ) {
next = from - > _get_focus_neighbour ( MARGIN_TOP ) ;
}
if ( p_event . is_action ( " ui_left " ) ) {
next = from - > _get_focus_neighbour ( MARGIN_LEFT ) ;
}
if ( p_event . is_action ( " ui_right " ) ) {
next = from - > _get_focus_neighbour ( MARGIN_RIGHT ) ;
}
if ( p_event . is_action ( " ui_down " ) ) {
next = from - > _get_focus_neighbour ( MARGIN_BOTTOM ) ;
}
if ( next ) {
next - > grab_focus ( ) ;
2016-07-23 19:25:00 +00:00
get_tree ( ) - > set_input_as_handled ( ) ;
2016-01-17 01:41:10 +00:00
}
}
} break ;
}
}
2017-03-18 23:36:26 +00:00
List < Control * > : : Element * Viewport : : _gui_add_root_control ( Control * p_control ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . roots_order_dirty = true ;
2016-01-17 01:41:10 +00:00
return gui . roots . push_back ( p_control ) ;
}
2017-03-18 23:36:26 +00:00
List < Control * > : : Element * Viewport : : _gui_add_subwindow_control ( Control * p_control ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . subwindow_order_dirty = true ;
2016-01-17 01:41:10 +00:00
return gui . subwindows . push_back ( p_control ) ;
}
void Viewport : : _gui_set_subwindow_order_dirty ( ) {
2017-03-18 23:36:26 +00:00
gui . subwindow_order_dirty = true ;
2016-01-17 01:41:10 +00:00
}
void Viewport : : _gui_set_root_order_dirty ( ) {
2017-03-18 23:36:26 +00:00
gui . roots_order_dirty = true ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : _gui_remove_modal_control ( List < Control * > : : Element * MI ) {
2016-01-17 01:41:10 +00:00
gui . modal_stack . erase ( MI ) ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : _gui_remove_from_modal_stack ( List < Control * > : : Element * MI , ObjectID p_prev_focus_owner ) {
2016-01-17 01:41:10 +00:00
//transfer the focus stack to the next
2017-03-18 23:36:26 +00:00
List < Control * > : : Element * next = MI - > next ( ) ;
2016-01-17 01:41:10 +00:00
gui . modal_stack . erase ( MI ) ;
2017-03-18 23:36:26 +00:00
MI = NULL ;
2016-01-17 01:41:10 +00:00
if ( p_prev_focus_owner ) {
// for previous window in stack, pass the focus so it feels more
// natural
if ( ! next ) { //top of stack
Object * pfo = ObjectDB : : get_instance ( p_prev_focus_owner ) ;
Control * pfoc = pfo - > cast_to < Control > ( ) ;
if ( ! pfoc )
return ;
if ( ! pfoc - > is_inside_tree ( ) | | ! pfoc - > is_visible ( ) )
return ;
pfoc - > grab_focus ( ) ;
} else {
next - > get ( ) - > _modal_set_prev_focus_owner ( p_prev_focus_owner ) ;
}
}
}
2017-03-18 23:36:26 +00:00
void Viewport : : _gui_force_drag ( Control * p_base , const Variant & p_data , Control * p_control ) {
2016-01-17 01:41:10 +00:00
2016-07-01 13:34:38 +00:00
ERR_EXPLAIN ( " Drag data must be a value " ) ;
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND ( p_data . get_type ( ) = = Variant : : NIL ) ;
2016-07-01 13:34:38 +00:00
2017-03-18 23:36:26 +00:00
gui . drag_data = p_data ;
gui . mouse_focus = NULL ;
2016-01-17 01:41:10 +00:00
if ( p_control ) {
2017-03-18 23:36:26 +00:00
_gui_set_drag_preview ( p_base , p_control ) ;
2016-01-17 01:41:10 +00:00
}
}
2016-01-19 23:27:27 +00:00
void Viewport : : _gui_set_drag_preview ( Control * p_base , Control * p_control ) {
2016-01-17 01:41:10 +00:00
ERR_FAIL_NULL ( p_control ) ;
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND ( ! ( ( Object * ) p_control ) - > cast_to < Control > ( ) ) ;
2016-01-17 01:41:10 +00:00
ERR_FAIL_COND ( p_control - > is_inside_tree ( ) ) ;
2017-03-18 23:36:26 +00:00
ERR_FAIL_COND ( p_control - > get_parent ( ) ! = NULL ) ;
2016-01-17 01:41:10 +00:00
if ( gui . drag_preview ) {
memdelete ( gui . drag_preview ) ;
}
p_control - > set_as_toplevel ( true ) ;
p_control - > set_pos ( gui . last_mouse_pos ) ;
2016-01-19 23:27:27 +00:00
p_base - > get_root_parent_control ( ) - > add_child ( p_control ) ; //add as child of viewport
2016-01-17 01:41:10 +00:00
p_control - > raise ( ) ;
if ( gui . drag_preview ) {
2017-03-18 23:36:26 +00:00
memdelete ( gui . drag_preview ) ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
gui . drag_preview = p_control ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : _gui_remove_root_control ( List < Control * > : : Element * RI ) {
2016-01-17 01:41:10 +00:00
gui . roots . erase ( RI ) ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : _gui_remove_subwindow_control ( List < Control * > : : Element * SI ) {
2016-01-17 01:41:10 +00:00
gui . subwindows . erase ( SI ) ;
}
void Viewport : : _gui_unfocus_control ( Control * p_control ) {
2017-03-18 23:36:26 +00:00
if ( gui . key_focus = = p_control ) {
2016-01-17 01:41:10 +00:00
gui . key_focus - > release_focus ( ) ;
}
}
void Viewport : : _gui_hid_control ( Control * p_control ) {
if ( gui . mouse_focus = = p_control ) {
2017-03-18 23:36:26 +00:00
gui . mouse_focus = NULL ;
2016-01-17 01:41:10 +00:00
}
/* ???
if ( data . window = = p_control ) {
window - > drag_data = Variant ( ) ;
if ( window - > drag_preview ) {
memdelete ( window - > drag_preview ) ;
window - > drag_preview = NULL ;
}
}
*/
if ( gui . key_focus = = p_control )
2017-03-18 23:36:26 +00:00
gui . key_focus = NULL ;
2016-01-17 01:41:10 +00:00
if ( gui . mouse_over = = p_control )
2017-03-18 23:36:26 +00:00
gui . mouse_over = NULL ;
2016-01-17 01:41:10 +00:00
if ( gui . tooltip = = p_control )
2017-03-18 23:36:26 +00:00
gui . tooltip = NULL ;
2016-01-25 13:30:03 +00:00
if ( gui . tooltip = = p_control ) {
2017-03-18 23:36:26 +00:00
gui . tooltip = NULL ;
2016-01-25 13:30:03 +00:00
_gui_cancel_tooltip ( ) ;
}
2016-01-17 01:41:10 +00:00
}
void Viewport : : _gui_remove_control ( Control * p_control ) {
if ( gui . mouse_focus = = p_control )
2017-03-18 23:36:26 +00:00
gui . mouse_focus = NULL ;
2016-01-17 01:41:10 +00:00
if ( gui . key_focus = = p_control )
2017-03-18 23:36:26 +00:00
gui . key_focus = NULL ;
2016-01-17 01:41:10 +00:00
if ( gui . mouse_over = = p_control )
2017-03-18 23:36:26 +00:00
gui . mouse_over = NULL ;
2016-01-17 01:41:10 +00:00
if ( gui . tooltip = = p_control )
2017-03-18 23:36:26 +00:00
gui . tooltip = NULL ;
2016-01-25 13:30:03 +00:00
if ( gui . tooltip_popup = = p_control ) {
_gui_cancel_tooltip ( ) ;
}
2016-01-17 01:41:10 +00:00
}
void Viewport : : _gui_remove_focus ( ) {
if ( gui . key_focus ) {
2017-03-18 23:36:26 +00:00
Node * f = gui . key_focus ;
gui . key_focus = NULL ;
f - > notification ( Control : : NOTIFICATION_FOCUS_EXIT , true ) ;
2016-01-17 01:41:10 +00:00
}
}
2017-03-18 23:36:26 +00:00
bool Viewport : : _gui_is_modal_on_top ( const Control * p_control ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
return ( gui . modal_stack . size ( ) & & gui . modal_stack . back ( ) - > get ( ) = = p_control ) ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
bool Viewport : : _gui_control_has_focus ( const Control * p_control ) {
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
return gui . key_focus = = p_control ;
2016-01-17 01:41:10 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : _gui_control_grab_focus ( Control * p_control ) {
2016-01-17 01:41:10 +00:00
//no need for change
2017-03-18 23:36:26 +00:00
if ( gui . key_focus & & gui . key_focus = = p_control )
2016-01-17 01:41:10 +00:00
return ;
2017-03-18 23:36:26 +00:00
get_tree ( ) - > call_group ( SceneTree : : GROUP_CALL_REALTIME , " _viewports " , " _gui_remove_focus " ) ;
gui . key_focus = p_control ;
2016-01-17 01:41:10 +00:00
p_control - > notification ( Control : : NOTIFICATION_FOCUS_ENTER ) ;
p_control - > update ( ) ;
}
void Viewport : : _gui_accept_event ( ) {
2017-03-18 23:36:26 +00:00
gui . key_event_accepted = true ;
2016-01-17 01:41:10 +00:00
if ( is_inside_tree ( ) )
get_tree ( ) - > set_input_as_handled ( ) ;
}
2017-03-18 23:36:26 +00:00
List < Control * > : : Element * Viewport : : _gui_show_modal ( Control * p_control ) {
2016-01-17 01:41:10 +00:00
gui . modal_stack . push_back ( p_control ) ;
if ( gui . key_focus )
p_control - > _modal_set_prev_focus_owner ( gui . key_focus - > get_instance_ID ( ) ) ;
else
p_control - > _modal_set_prev_focus_owner ( 0 ) ;
return gui . modal_stack . back ( ) ;
}
Control * Viewport : : _gui_get_focus_owner ( ) {
return gui . key_focus ;
}
void Viewport : : _gui_grab_click_focus ( Control * p_control ) {
if ( gui . mouse_focus ) {
2017-03-18 23:36:26 +00:00
if ( gui . mouse_focus = = p_control )
2016-01-17 01:41:10 +00:00
return ;
InputEvent ie ;
2017-03-18 23:36:26 +00:00
ie . type = InputEvent : : MOUSE_BUTTON ;
InputEventMouseButton & mb = ie . mouse_button ;
2016-01-17 01:41:10 +00:00
//send unclic
2017-03-18 23:36:26 +00:00
Point2 click = gui . mouse_focus - > get_global_transform_with_canvas ( ) . affine_inverse ( ) . xform ( gui . last_mouse_pos ) ;
mb . x = click . x ;
mb . y = click . y ;
mb . button_index = gui . mouse_focus_button ;
mb . pressed = false ;
gui . mouse_focus - > call_deferred ( " _input_event " , ie ) ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
gui . mouse_focus = p_control ;
gui . focus_inv_xform = gui . mouse_focus - > get_global_transform_with_canvas ( ) . affine_inverse ( ) ;
click = gui . mouse_focus - > get_global_transform_with_canvas ( ) . affine_inverse ( ) . xform ( gui . last_mouse_pos ) ;
mb . x = click . x ;
mb . y = click . y ;
mb . button_index = gui . mouse_focus_button ;
mb . pressed = true ;
gui . mouse_focus - > call_deferred ( " _input_event " , ie ) ;
2016-01-17 01:41:10 +00:00
}
}
///////////////////////////////
2017-03-18 23:36:26 +00:00
void Viewport : : input ( const InputEvent & p_event ) {
2014-04-10 03:18:27 +00:00
2014-11-06 00:20:42 +00:00
ERR_FAIL_COND ( ! is_inside_tree ( ) ) ;
2016-01-18 22:49:11 +00:00
2017-03-18 23:36:26 +00:00
get_tree ( ) - > _call_input_pause ( input_group , " _input " , p_event ) ; //not a bug, must happen before GUI, order is _input -> gui input -> _unhandled input
2016-01-17 01:41:10 +00:00
_gui_input_event ( p_event ) ;
//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
2014-04-10 03:18:27 +00:00
}
2017-03-18 23:36:26 +00:00
void Viewport : : unhandled_input ( const InputEvent & p_event ) {
2014-04-10 03:18:27 +00:00
2014-11-06 00:20:42 +00:00
ERR_FAIL_COND ( ! is_inside_tree ( ) ) ;
2014-04-10 03:18:27 +00:00
2017-03-18 23:36:26 +00:00
get_tree ( ) - > _call_input_pause ( unhandled_input_group , " _unhandled_input " , p_event ) ;
2014-04-10 03:18:27 +00:00
//call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_input","_unhandled_input",ev);
2017-03-18 23:36:26 +00:00
if ( ! get_tree ( ) - > input_handled & & p_event . type = = InputEvent : : KEY ) {
get_tree ( ) - > _call_input_pause ( unhandled_key_input_group , " _unhandled_key_input " , p_event ) ;
2014-04-10 03:18:27 +00:00
//call_group(GROUP_CALL_REVERSE|GROUP_CALL_REALTIME|GROUP_CALL_MULIILEVEL,"unhandled_key_input","_unhandled_key_input",ev);
}
2014-09-15 14:33:30 +00:00
2014-11-06 00:20:42 +00:00
if ( physics_object_picking & & ! get_tree ( ) - > input_handled ) {
2014-09-15 14:33:30 +00:00
2017-03-18 23:36:26 +00:00
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 ) {
2014-09-15 14:33:30 +00:00
physics_picking_events . push_back ( p_event ) ;
}
}
2014-04-10 03:18:27 +00:00
}
2014-04-15 01:43:44 +00:00
void Viewport : : set_use_own_world ( bool p_world ) {
2017-03-18 23:36:26 +00:00
if ( p_world = = own_world . is_valid ( ) )
2014-04-15 01:43:44 +00:00
return ;
2014-11-06 00:20:42 +00:00
if ( is_inside_tree ( ) )
2014-04-15 01:43:44 +00:00
_propagate_exit_world ( this ) ;
# ifndef _3D_DISABLED
if ( find_world ( ) . is_valid ( ) & & camera )
camera - > notification ( Camera : : NOTIFICATION_LOST_CURRENT ) ;
# endif
if ( ! p_world )
2017-03-18 23:36:26 +00:00
own_world = Ref < World > ( ) ;
2014-04-15 01:43:44 +00:00
else
2017-03-18 23:36:26 +00:00
own_world = Ref < World > ( memnew ( World ) ) ;
2014-04-15 01:43:44 +00:00
2014-11-06 00:20:42 +00:00
if ( is_inside_tree ( ) )
2014-04-15 01:43:44 +00:00
_propagate_enter_world ( this ) ;
# ifndef _3D_DISABLED
if ( find_world ( ) . is_valid ( ) & & camera )
camera - > notification ( Camera : : NOTIFICATION_BECAME_CURRENT ) ;
# endif
//propagate exit
2014-11-06 00:20:42 +00:00
if ( is_inside_tree ( ) ) {
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > viewport_set_scenario ( viewport , find_world ( ) - > get_scenario ( ) ) ;
2014-04-15 01:43:44 +00:00
}
_update_listener ( ) ;
}
bool Viewport : : is_using_own_world ( ) const {
return own_world . is_valid ( ) ;
}
2017-03-18 23:36:26 +00:00
void Viewport : : set_render_target_to_screen_rect ( const Rect2 & p_rect ) {
2014-04-15 01:43:44 +00:00
2017-03-18 23:36:26 +00:00
to_screen_rect = p_rect ;
VisualServer : : get_singleton ( ) - > viewport_set_render_target_to_screen_rect ( viewport , to_screen_rect ) ;
2014-04-15 01:43:44 +00:00
}
2017-03-18 23:36:26 +00:00
Rect2 Viewport : : get_render_target_to_screen_rect ( ) const {
2014-04-15 01:43:44 +00:00
return to_screen_rect ;
}
2014-09-15 14:33:30 +00:00
void Viewport : : set_physics_object_picking ( bool p_enable ) {
2017-03-18 23:36:26 +00:00
physics_object_picking = p_enable ;
2014-09-15 14:33:30 +00:00
set_fixed_process ( physics_object_picking ) ;
if ( ! physics_object_picking )
physics_picking_events . clear ( ) ;
}
2014-10-28 01:54:32 +00:00
Vector2 Viewport : : get_camera_coords ( const Vector2 & p_viewport_coords ) const {
Matrix32 xf = get_final_transform ( ) ;
return xf . xform ( p_viewport_coords ) ;
}
Vector2 Viewport : : get_camera_rect_size ( ) const {
return last_vp_rect . size ;
}
2014-09-15 14:33:30 +00:00
bool Viewport : : get_physics_object_picking ( ) {
return physics_object_picking ;
}
2016-01-17 01:41:10 +00:00
bool Viewport : : gui_has_modal_stack ( ) const {
return gui . modal_stack . size ( ) ;
}
void Viewport : : set_disable_input ( bool p_disable ) {
2017-03-18 23:36:26 +00:00
disable_input = p_disable ;
2016-01-17 01:41:10 +00:00
}
bool Viewport : : is_input_disabled ( ) const {
return disable_input ;
}
2014-02-10 01:10:30 +00:00
2016-05-11 14:46:08 +00:00
Variant Viewport : : gui_get_drag_data ( ) const {
return gui . drag_data ;
}
2016-05-17 21:27:15 +00:00
2016-06-27 23:14:59 +00:00
Control * Viewport : : get_modal_stack_top ( ) const {
2017-03-18 23:36:26 +00:00
return gui . modal_stack . size ( ) ? gui . modal_stack . back ( ) - > get ( ) : NULL ;
2016-06-27 23:14:59 +00:00
}
2016-05-17 21:27:15 +00:00
String Viewport : : get_configuration_warning ( ) const {
if ( get_parent ( ) & & ! get_parent ( ) - > cast_to < Control > ( ) & & ! render_target ) {
2016-05-18 22:08:12 +00:00
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. " ) ;
2016-05-17 21:27:15 +00:00
}
return String ( ) ;
}
2014-02-10 01:10:30 +00:00
void Viewport : : _bind_methods ( ) {
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_rect " , " rect " ) , & Viewport : : set_rect ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_rect " ) , & Viewport : : get_rect ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_world_2d " , " world_2d:World2D " ) , & Viewport : : set_world_2d ) ;
2016-07-13 18:51:38 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_world_2d:World2D " ) , & Viewport : : get_world_2d ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " find_world_2d:World2D " ) , & Viewport : : find_world_2d ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_world " , " world:World " ) , & Viewport : : set_world ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_world:World " ) , & Viewport : : get_world ) ;
ObjectTypeDB : : bind_method ( _MD ( " find_world:World " ) , & Viewport : : find_world ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_canvas_transform " , " xform " ) , & Viewport : : set_canvas_transform ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_canvas_transform " ) , & Viewport : : get_canvas_transform ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_global_canvas_transform " , " xform " ) , & Viewport : : set_global_canvas_transform ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_global_canvas_transform " ) , & Viewport : : get_global_canvas_transform ) ;
ObjectTypeDB : : bind_method ( _MD ( " get_final_transform " ) , & Viewport : : get_final_transform ) ;
ObjectTypeDB : : bind_method ( _MD ( " get_visible_rect " ) , & Viewport : : get_visible_rect ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_transparent_background " , " enable " ) , & Viewport : : set_transparent_background ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " has_transparent_background " ) , & Viewport : : has_transparent_background ) ;
ObjectTypeDB : : bind_method ( _MD ( " _parent_visibility_changed " ) , & Viewport : : _parent_visibility_changed ) ;
ObjectTypeDB : : bind_method ( _MD ( " _parent_resized " ) , & Viewport : : _parent_resized ) ;
2014-04-10 03:18:27 +00:00
ObjectTypeDB : : bind_method ( _MD ( " _vp_input " ) , & Viewport : : _vp_input ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " _vp_input_text " , " text " ) , & Viewport : : _vp_input_text ) ;
2014-04-10 03:18:27 +00:00
ObjectTypeDB : : bind_method ( _MD ( " _vp_unhandled_input " ) , & Viewport : : _vp_unhandled_input ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_size_override " , " enable " , " size " , " margin " ) , & Viewport : : set_size_override , DEFVAL ( Size2 ( - 1 , - 1 ) ) , DEFVAL ( Size2 ( 0 , 0 ) ) ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_size_override " ) , & Viewport : : get_size_override ) ;
ObjectTypeDB : : bind_method ( _MD ( " is_size_override_enabled " ) , & Viewport : : is_size_override_enabled ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_size_override_stretch " , " enabled " ) , & Viewport : : set_size_override_stretch ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " is_size_override_stretch_enabled " ) , & Viewport : : is_size_override_stretch_enabled ) ;
ObjectTypeDB : : bind_method ( _MD ( " queue_screen_capture " ) , & Viewport : : queue_screen_capture ) ;
ObjectTypeDB : : bind_method ( _MD ( " get_screen_capture " ) , & Viewport : : get_screen_capture ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_as_render_target " , " enable " ) , & Viewport : : set_as_render_target ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " is_set_as_render_target " ) , & Viewport : : is_set_as_render_target ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_render_target_vflip " , " enable " ) , & Viewport : : set_render_target_vflip ) ;
2014-04-10 03:18:27 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_render_target_vflip " ) , & Viewport : : get_render_target_vflip ) ;
2016-03-08 23:00:52 +00:00
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_render_target_clear_on_new_frame " , " enable " ) , & Viewport : : set_render_target_clear_on_new_frame ) ;
2015-02-21 09:35:06 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_render_target_clear_on_new_frame " ) , & Viewport : : get_render_target_clear_on_new_frame ) ;
2016-03-08 23:00:52 +00:00
2015-02-21 09:35:06 +00:00
ObjectTypeDB : : bind_method ( _MD ( " render_target_clear " ) , & Viewport : : render_target_clear ) ;
2014-04-10 03:18:27 +00:00
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_render_target_filter " , " enable " ) , & Viewport : : set_render_target_filter ) ;
2014-05-14 04:22:15 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_render_target_filter " ) , & Viewport : : get_render_target_filter ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_render_target_gen_mipmaps " , " enable " ) , & Viewport : : set_render_target_gen_mipmaps ) ;
2014-08-14 13:31:38 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_render_target_gen_mipmaps " ) , & Viewport : : get_render_target_gen_mipmaps ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_render_target_update_mode " , " mode " ) , & Viewport : : set_render_target_update_mode ) ;
2014-02-26 13:08:17 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_render_target_update_mode " ) , & Viewport : : get_render_target_update_mode ) ;
ObjectTypeDB : : bind_method ( _MD ( " get_render_target_texture:RenderTargetTexture " ) , & Viewport : : get_render_target_texture ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_physics_object_picking " , " enable " ) , & Viewport : : set_physics_object_picking ) ;
2014-09-15 14:33:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_physics_object_picking " ) , & Viewport : : get_physics_object_picking ) ;
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_viewport " ) , & Viewport : : get_viewport ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " input " , " local_event " ) , & Viewport : : input ) ;
ObjectTypeDB : : bind_method ( _MD ( " unhandled_input " , " local_event " ) , & Viewport : : unhandled_input ) ;
2014-02-26 13:08:17 +00:00
2014-02-10 01:10:30 +00:00
ObjectTypeDB : : bind_method ( _MD ( " update_worlds " ) , & Viewport : : update_worlds ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_use_own_world " , " enable " ) , & Viewport : : set_use_own_world ) ;
2014-04-15 01:43:44 +00:00
ObjectTypeDB : : bind_method ( _MD ( " is_using_own_world " ) , & Viewport : : is_using_own_world ) ;
2014-09-17 00:19:54 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_camera:Camera " ) , & Viewport : : get_camera ) ;
2014-02-26 13:08:17 +00:00
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_as_audio_listener " , " enable " ) , & Viewport : : set_as_audio_listener ) ;
ObjectTypeDB : : bind_method ( _MD ( " is_audio_listener " , " enable " ) , & Viewport : : is_audio_listener ) ;
2014-02-26 13:08:17 +00:00
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_as_audio_listener_2d " , " enable " ) , & Viewport : : set_as_audio_listener_2d ) ;
ObjectTypeDB : : bind_method ( _MD ( " is_audio_listener_2d " , " enable " ) , & Viewport : : is_audio_listener_2d ) ;
ObjectTypeDB : : bind_method ( _MD ( " set_render_target_to_screen_rect " , " rect " ) , & Viewport : : set_render_target_to_screen_rect ) ;
2014-02-26 13:08:17 +00:00
2015-05-12 11:17:09 +00:00
ObjectTypeDB : : bind_method ( _MD ( " get_mouse_pos " ) , & Viewport : : get_mouse_pos ) ;
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " warp_mouse " , " to_pos " ) , & Viewport : : warp_mouse ) ;
2014-02-26 13:08:17 +00:00
2016-01-17 01:41:10 +00:00
ObjectTypeDB : : bind_method ( _MD ( " gui_has_modal_stack " ) , & Viewport : : gui_has_modal_stack ) ;
2016-05-11 14:46:08 +00:00
ObjectTypeDB : : bind_method ( _MD ( " gui_get_drag_data:Variant " ) , & Viewport : : gui_get_drag_data ) ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
ObjectTypeDB : : bind_method ( _MD ( " set_disable_input " , " disable " ) , & Viewport : : set_disable_input ) ;
2016-01-17 01:41:10 +00:00
ObjectTypeDB : : bind_method ( _MD ( " is_input_disabled " ) , & Viewport : : is_input_disabled ) ;
ObjectTypeDB : : bind_method ( _MD ( " _gui_show_tooltip " ) , & Viewport : : _gui_show_tooltip ) ;
2016-07-23 19:25:00 +00:00
ObjectTypeDB : : bind_method ( _MD ( " _gui_remove_focus " ) , & Viewport : : _gui_remove_focus ) ;
2016-01-17 01:41:10 +00:00
2017-03-18 23:36:26 +00:00
ADD_PROPERTY ( PropertyInfo ( Variant : : RECT2 , " rect " ) , _SCS ( " set_rect " ) , _SCS ( " get_rect " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " own_world " ) , _SCS ( " set_use_own_world " ) , _SCS ( " is_using_own_world " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : OBJECT , " world " , PROPERTY_HINT_RESOURCE_TYPE , " World " ) , _SCS ( " set_world " ) , _SCS ( " get_world " ) ) ;
// ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"world_2d",PROPERTY_HINT_RESOURCE_TYPE,"World2D"), _SCS("set_world_2d"), _SCS("get_world_2d") );
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " transparent_bg " ) , _SCS ( " set_transparent_background " ) , _SCS ( " has_transparent_background " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " render_target/enabled " ) , _SCS ( " set_as_render_target " ) , _SCS ( " is_set_as_render_target " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " render_target/v_flip " ) , _SCS ( " set_render_target_vflip " ) , _SCS ( " get_render_target_vflip " ) ) ;
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 " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " render_target/filter " ) , _SCS ( " set_render_target_filter " ) , _SCS ( " get_render_target_filter " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " render_target/gen_mipmaps " ) , _SCS ( " set_render_target_gen_mipmaps " ) , _SCS ( " get_render_target_gen_mipmaps " ) ) ;
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 " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " audio_listener/enable_2d " ) , _SCS ( " set_as_audio_listener_2d " ) , _SCS ( " is_audio_listener_2d " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " audio_listener/enable_3d " ) , _SCS ( " set_as_audio_listener " ) , _SCS ( " is_audio_listener " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " physics/object_picking " ) , _SCS ( " set_physics_object_picking " ) , _SCS ( " get_physics_object_picking " ) ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " gui/disable_input " ) , _SCS ( " set_disable_input " ) , _SCS ( " is_input_disabled " ) ) ;
2014-02-10 01:10:30 +00:00
ADD_SIGNAL ( MethodInfo ( " size_changed " ) ) ;
2014-02-26 13:08:17 +00:00
2017-03-18 23:36:26 +00:00
BIND_CONSTANT ( RENDER_TARGET_UPDATE_DISABLED ) ;
BIND_CONSTANT ( RENDER_TARGET_UPDATE_ONCE ) ;
BIND_CONSTANT ( RENDER_TARGET_UPDATE_WHEN_VISIBLE ) ;
BIND_CONSTANT ( RENDER_TARGET_UPDATE_ALWAYS ) ;
2014-02-10 01:10:30 +00:00
}
Viewport : : Viewport ( ) {
2017-03-18 23:36:26 +00:00
world_2d = Ref < World2D > ( memnew ( World2D ) ) ;
2014-02-10 01:10:30 +00:00
viewport = VisualServer : : get_singleton ( ) - > viewport_create ( ) ;
2016-03-20 02:10:04 +00:00
internal_listener = SpatialSoundServer : : get_singleton ( ) - > listener_create ( ) ;
2017-03-18 23:36:26 +00:00
audio_listener = false ;
2016-03-20 02:10:04 +00:00
internal_listener_2d = SpatialSound2DServer : : get_singleton ( ) - > listener_create ( ) ;
2017-03-18 23:36:26 +00:00
audio_listener_2d = false ;
transparent_bg = false ;
parent = NULL ;
listener = NULL ;
camera = NULL ;
size_override = false ;
size_override_stretch = false ;
size_override_size = Size2 ( 1 , 1 ) ;
render_target_gen_mipmaps = false ;
render_target = false ;
render_target_vflip = false ;
render_target_clear_on_new_frame = true ;
2015-02-21 09:35:06 +00:00
//render_target_clear=true;
2017-03-18 23:36:26 +00:00
render_target_update_mode = RENDER_TARGET_UPDATE_WHEN_VISIBLE ;
render_target_texture = Ref < RenderTargetTexture > ( memnew ( RenderTargetTexture ( this ) ) ) ;
2014-02-10 01:10:30 +00:00
2017-03-18 23:36:26 +00:00
physics_object_picking = false ;
physics_object_capture = 0 ;
physics_object_over = 0 ;
physics_last_mousepos = Vector2 ( 1e20 , 1e20 ) ;
2014-09-15 14:33:30 +00:00
2017-03-18 23:36:26 +00:00
String id = itos ( get_instance_ID ( ) ) ;
input_group = " _vp_input " + id ;
gui_input_group = " _vp_gui_input " + id ;
unhandled_input_group = " _vp_unhandled_input " + id ;
unhandled_key_input_group = " _vp_unhandled_key_input " + id ;
2014-04-15 01:43:44 +00:00
2017-03-18 23:36:26 +00:00
disable_input = false ;
2016-01-17 01:41:10 +00:00
//window tooltip
2016-01-25 13:30:03 +00:00
gui . tooltip_timer = - 1 ;
//gui.tooltip_timer->force_parent_owned();
2017-03-18 23:36:26 +00:00
gui . tooltip_delay = GLOBAL_DEF ( " display/tooltip_delay " , 0.7 ) ;
2016-01-18 22:49:11 +00:00
2017-03-18 23:36:26 +00:00
gui . tooltip = NULL ;
gui . tooltip_label = NULL ;
gui . drag_preview = NULL ;
gui . drag_attempted = false ;
2015-09-20 16:03:46 +00:00
2017-03-18 23:36:26 +00:00
parent_control = NULL ;
2014-02-10 01:10:30 +00:00
}
Viewport : : ~ Viewport ( ) {
2017-03-18 23:36:26 +00:00
VisualServer : : get_singleton ( ) - > free ( viewport ) ;
2016-03-20 02:10:04 +00:00
SpatialSoundServer : : get_singleton ( ) - > free ( internal_listener ) ;
SpatialSound2DServer : : get_singleton ( ) - > free ( internal_listener_2d ) ;
2014-02-10 01:10:30 +00:00
if ( render_target_texture . is_valid ( ) )
2017-03-18 23:36:26 +00:00
render_target_texture - > vp = NULL ; //so if used, will crash
2014-02-10 01:10:30 +00:00
}