From cf5778e51a883936ffc896231da8259e5ebabc0a Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 5 Oct 2016 01:26:35 -0300 Subject: -Added ViewportContainer, this is the only way to make viewports show up in GUI now -2D editing now seems to work -Added some functions and refactoring to Viewport --- scene/gui/viewport_container.cpp | 103 ++++++++++++++++++++++++++++++++ scene/gui/viewport_container.h | 25 ++++++++ scene/main/viewport.cpp | 125 +++++++++++++++------------------------ scene/main/viewport.h | 24 ++++---- scene/register_scene_types.cpp | 4 +- 5 files changed, 189 insertions(+), 92 deletions(-) create mode 100644 scene/gui/viewport_container.cpp create mode 100644 scene/gui/viewport_container.h (limited to 'scene') diff --git a/scene/gui/viewport_container.cpp b/scene/gui/viewport_container.cpp new file mode 100644 index 0000000000..3418a495e9 --- /dev/null +++ b/scene/gui/viewport_container.cpp @@ -0,0 +1,103 @@ +#include "viewport_container.h" +#include "scene/main/viewport.h" +Size2 ViewportContainer::get_minimum_size() const { + + + if (stretch) + return Size2(); + Size2 ms; + for(int i=0;icast_to(); + if (!c) + continue; + + Size2 minsize = c->get_size(); + ms.width = MAX(ms.width , minsize.width); + ms.height = MAX(ms.height , minsize.height); + } + + return ms; + +} + + +void ViewportContainer::set_stretch(bool p_enable) { + + stretch=p_enable; + queue_sort(); + update(); + +} + +bool ViewportContainer::is_stretch_enabled() const { + + return stretch; +} + + +void ViewportContainer::_notification(int p_what) { + + + if (p_what==NOTIFICATION_RESIZED) { + + if (!stretch) + return; + + for(int i=0;icast_to(); + if (!c) + continue; + + c->set_size(get_size()); + } + } + + if (p_what==NOTIFICATION_ENTER_TREE || p_what==NOTIFICATION_VISIBILITY_CHANGED) { + + for(int i=0;icast_to(); + if (!c) + continue; + + + if (is_visible()) + c->set_update_mode(Viewport::UPDATE_ALWAYS); + else + c->set_update_mode(Viewport::UPDATE_DISABLED); + } + + } + + if (p_what==NOTIFICATION_DRAW) { + + for(int i=0;icast_to(); + if (!c) + continue; + + if (stretch) + draw_texture_rect(c->get_texture(),Rect2(Vector2(),get_size()*Size2(1,-1))); + else + draw_texture_rect(c->get_texture(),Rect2(Vector2(),c->get_size()*Size2(1,-1))); + } + } + +} + +void ViewportContainer::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_stretch","enable"),&ViewportContainer::set_stretch); + ObjectTypeDB::bind_method(_MD("is_stretch_enabled"),&ViewportContainer::is_stretch_enabled); + + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"stretch"),_SCS("set_stretch"),_SCS("is_stretch_enabled")); +} + +ViewportContainer::ViewportContainer() { + + stretch=false; +} diff --git a/scene/gui/viewport_container.h b/scene/gui/viewport_container.h new file mode 100644 index 0000000000..cfc58f7d6e --- /dev/null +++ b/scene/gui/viewport_container.h @@ -0,0 +1,25 @@ +#ifndef VIEWPORTCONTAINER_H +#define VIEWPORTCONTAINER_H + +#include "scene/gui/container.h" + +class ViewportContainer : public Container { + + OBJ_TYPE( ViewportContainer, Container ); + + bool stretch; +protected: + + void _notification(int p_what); + static void _bind_methods(); +public: + + void set_stretch(bool p_enable); + bool is_stretch_enabled() const; + + virtual Size2 get_minimum_size() const; + + ViewportContainer(); +}; + +#endif // VIEWPORTCONTAINER_H diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 44a3f41a4a..e93a16371d 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -51,35 +51,37 @@ #include "globals.h" -int RenderTargetTexture::get_width() const { +int ViewportTexture::get_width() const { ERR_FAIL_COND_V(!vp,0); return vp->size.width; } -int RenderTargetTexture::get_height() const{ +int ViewportTexture::get_height() const{ ERR_FAIL_COND_V(!vp,0); return vp->size.height; } -Size2 RenderTargetTexture::get_size() const{ +Size2 ViewportTexture::get_size() const{ ERR_FAIL_COND_V(!vp,Size2()); return vp->size; } -RID RenderTargetTexture::get_rid() const{ +RID ViewportTexture::get_rid() const{ ERR_FAIL_COND_V(!vp,RID()); return vp->texture_rid; } -bool RenderTargetTexture::has_alpha() const{ +bool ViewportTexture::has_alpha() const{ return false; } -void RenderTargetTexture::set_flags(uint32_t p_flags){ +void ViewportTexture::set_flags(uint32_t p_flags){ + + if (!vp) + return; - ERR_FAIL_COND(!vp); if (p_flags&FLAG_FILTER) flags=FLAG_FILTER; else @@ -89,12 +91,12 @@ void RenderTargetTexture::set_flags(uint32_t p_flags){ } -uint32_t RenderTargetTexture::get_flags() const{ +uint32_t ViewportTexture::get_flags() const{ return flags; } -RenderTargetTexture::RenderTargetTexture(Viewport *p_vp){ +ViewportTexture::ViewportTexture(Viewport *p_vp){ vp=p_vp; flags=0; @@ -207,7 +209,7 @@ void Viewport::_parent_draw() { void Viewport::_parent_visibility_changed() { - +/* if (parent_control) { Control *c = parent_control; @@ -216,14 +218,14 @@ void Viewport::_parent_visibility_changed() { _update_listener(); _update_listener_2d(); } - +*/ } void Viewport::_vp_enter_tree() { - if (parent_control) { +/* if (parent_control) { Control *cparent=parent_control; RID parent_ci = cparent->get_canvas_item(); @@ -240,7 +242,7 @@ void Viewport::_vp_enter_tree() { // VisualServer::get_singleton()->viewport_attach_to_screen(viewport,0); } - +*/ } @@ -327,24 +329,10 @@ void Viewport::_notification(int p_what) { case NOTIFICATION_ENTER_TREE: { if (get_parent()) { - Node *parent=get_parent(); - if (parent) { - parent_control=parent->cast_to(); - } - } - - - parent=NULL; - Node *parent_node=get_parent(); - - - while(parent_node) { - - parent = parent_node->cast_to(); - if (parent) - break; - - parent_node=parent_node->get_parent(); + parent = get_parent()->get_viewport(); + VisualServer::get_singleton()->viewport_set_parent_viewport(viewport,parent->get_viewport()); + } else { + parent=NULL; } current_canvas=find_world_2d()->get_canvas(); @@ -434,7 +422,7 @@ void Viewport::_notification(int p_what) { } remove_from_group("_viewports"); - parent_control=NULL; + VS::get_singleton()->viewport_set_active(viewport,false); @@ -1263,7 +1251,7 @@ bool Viewport::is_set_as_render_target() const{ void Viewport::set_update_mode(UpdateMode p_mode){ update_mode=p_mode; -// VS::get_singleton()->viewport_set_update_mode(viewport,VS::RenderTargetUpdateMode(p_mode)); + VS::get_singleton()->viewport_set_update_mode(viewport,VS::ViewportUpdateMode(p_mode)); } Viewport::UpdateMode Viewport::get_update_mode() const{ @@ -1282,7 +1270,7 @@ Image Viewport::get_screen_capture() const { return Image(); } -Ref Viewport::get_texture() const { +Ref Viewport::get_texture() const { return texture; } @@ -1290,7 +1278,7 @@ Ref Viewport::get_texture() const { void Viewport::set_vflip(bool p_enable) { vflip=p_enable; -// VisualServer::get_singleton()->viewport_set_vflip(viewport,p_enable); + VisualServer::get_singleton()->viewport_set_vflip(viewport,p_enable); } bool Viewport::get_vflip() const{ @@ -1315,30 +1303,6 @@ void Viewport::clear() { // VisualServer::get_singleton()->viewport_clear(viewport); } -void Viewport::set_filter(bool p_enable) { - - texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0)); - -} - -bool Viewport::get_filter() const{ - - return (texture->get_flags()&Texture::FLAG_FILTER)!=0; -} - -void Viewport::set_gen_mipmaps(bool p_enable) { - - //texture->set_flags(p_enable?int(Texture::FLAG_FILTER):int(0)); - gen_mipmaps=p_enable; - -} - -bool Viewport::get_gen_mipmaps() const{ - - //return (texture->get_flags()&Texture::FLAG_FILTER)!=0; - return gen_mipmaps; -} - Matrix32 Viewport::_get_input_pre_xform() const { @@ -1356,9 +1320,9 @@ Matrix32 Viewport::_get_input_pre_xform() const { 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(); - } +// if (parent_control) { +// return (parent_control->get_viewport()->get_final_transform() * parent_control->get_global_transform_with_canvas()).get_origin(); +// } return Vector2(); } @@ -1454,8 +1418,6 @@ void Viewport::_vp_input(const InputEvent& p_ev) { } #endif - if (parent_control && !parent_control->is_visible()) - return; if (to_screen_rect==Rect2()) return; //if render target, can't get input events @@ -1480,8 +1442,8 @@ void Viewport::_vp_unhandled_input(const InputEvent& p_ev) { } #endif - if (parent_control && !parent_control->is_visible()) - return; +// if (parent_control && !parent_control->is_visible()) +// return; if (to_screen_rect==Rect2()) return; //if render target, can't get input events @@ -2575,6 +2537,16 @@ bool Viewport::is_input_disabled() const { return disable_input; } +void Viewport::set_disable_3d(bool p_disable) { + disable_3d=p_disable; + VS::get_singleton()->viewport_set_disable_3d(viewport,p_disable); +} + +bool Viewport::is_3d_disabled() const { + + return disable_3d; +} + Variant Viewport::gui_get_drag_data() const { return gui.drag_data; } @@ -2648,17 +2620,10 @@ void Viewport::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_clear_on_new_frame"), &Viewport::get_clear_on_new_frame); ObjectTypeDB::bind_method(_MD("clear"), &Viewport::clear); - - ObjectTypeDB::bind_method(_MD("set_filter","enable"), &Viewport::set_filter); - ObjectTypeDB::bind_method(_MD("get_filter"), &Viewport::get_filter); - - ObjectTypeDB::bind_method(_MD("set_gen_mipmaps","enable"), &Viewport::set_gen_mipmaps); - ObjectTypeDB::bind_method(_MD("get_gen_mipmaps"), &Viewport::get_gen_mipmaps); - ObjectTypeDB::bind_method(_MD("set_update_mode","mode"), &Viewport::set_update_mode); ObjectTypeDB::bind_method(_MD("get_update_mode"), &Viewport::get_update_mode); - ObjectTypeDB::bind_method(_MD("get_texture:RenderTargetTexture"), &Viewport::get_texture); + ObjectTypeDB::bind_method(_MD("get_texture:ViewportTexture"), &Viewport::get_texture); ObjectTypeDB::bind_method(_MD("set_physics_object_picking","enable"), &Viewport::set_physics_object_picking); ObjectTypeDB::bind_method(_MD("get_physics_object_picking"), &Viewport::get_physics_object_picking); @@ -2690,6 +2655,9 @@ void Viewport::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_disable_input","disable"), &Viewport::set_disable_input); ObjectTypeDB::bind_method(_MD("is_input_disabled"), &Viewport::is_input_disabled); + ObjectTypeDB::bind_method(_MD("set_disable_3d","disable"), &Viewport::set_disable_3d); + ObjectTypeDB::bind_method(_MD("is_3d_disabled"), &Viewport::is_3d_disabled); + ObjectTypeDB::bind_method(_MD("_gui_show_tooltip"), &Viewport::_gui_show_tooltip); ObjectTypeDB::bind_method(_MD("_gui_remove_focus"), &Viewport::_gui_remove_focus); @@ -2700,13 +2668,12 @@ void Viewport::_bind_methods() { ADD_PROPERTY( PropertyInfo(Variant::BOOL,"transparent_bg"), _SCS("set_transparent_background"), _SCS("has_transparent_background") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/v_flip"), _SCS("set_vflip"), _SCS("get_vflip") ); ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/clear_on_new_frame"), _SCS("set_clear_on_new_frame"), _SCS("get_clear_on_new_frame") ); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/filter"), _SCS("set_filter"), _SCS("get_filter") ); - ADD_PROPERTY( PropertyInfo(Variant::BOOL,"render_target/gen_mipmaps"), _SCS("set_gen_mipmaps"), _SCS("get_gen_mipmaps") ); ADD_PROPERTY( PropertyInfo(Variant::INT,"render_target/update_mode",PROPERTY_HINT_ENUM,"Disabled,Once,When Visible,Always"), _SCS("set_update_mode"), _SCS("get_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") ); + ADD_PROPERTY( PropertyInfo(Variant::BOOL,"3d/disable_3d"), _SCS("set_disable_3d"), _SCS("is_3d_disabled") ); ADD_SIGNAL(MethodInfo("size_changed")); @@ -2727,6 +2694,7 @@ Viewport::Viewport() { world_2d = Ref( memnew( World2D )); viewport = VisualServer::get_singleton()->viewport_create(); + texture_rid=VisualServer::get_singleton()->viewport_get_texture(viewport); internal_listener = SpatialSoundServer::get_singleton()->listener_create(); audio_listener=false; internal_listener_2d = SpatialSound2DServer::get_singleton()->listener_create(); @@ -2744,7 +2712,7 @@ Viewport::Viewport() { clear_on_new_frame=true; //clear=true; update_mode=UPDATE_WHEN_VISIBLE; - texture = Ref( memnew( RenderTargetTexture(this) ) ); + texture = Ref( memnew( ViewportTexture(this) ) ); physics_object_picking=false; physics_object_capture=0; @@ -2759,6 +2727,7 @@ Viewport::Viewport() { unhandled_key_input_group = "_vp_unhandled_key_input"+id; disable_input=false; + disable_3d=false; //window tooltip gui.tooltip_timer = -1; @@ -2773,7 +2742,7 @@ Viewport::Viewport() { gui.canvas_sort_index=0; - parent_control=NULL; + } diff --git a/scene/main/viewport.h b/scene/main/viewport.h index d610237bec..c5d61469dd 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -48,9 +48,9 @@ class Label; class Timer; class Viewport; -class RenderTargetTexture : public Texture { +class ViewportTexture : public Texture { - OBJ_TYPE( RenderTargetTexture, Texture ); + OBJ_TYPE( ViewportTexture, Texture ); int flags; friend class Viewport; @@ -70,7 +70,7 @@ public: virtual void set_flags(uint32_t p_flags); virtual uint32_t get_flags() const; - RenderTargetTexture(Viewport *p_vp=NULL); + ViewportTexture(Viewport *p_vp=NULL); }; @@ -88,10 +88,10 @@ public: private: -friend class RenderTargetTexture; +friend class ViewportTexture; + - Control *parent_control; Viewport *parent; Listener *listener; @@ -101,7 +101,6 @@ friend class RenderTargetTexture; Set cameras; RID viewport; - RID canvas_item; RID current_canvas; bool audio_listener; @@ -171,9 +170,10 @@ friend class RenderTargetTexture; void _update_global_transform(); + bool disable_3d; UpdateMode update_mode; RID texture_rid; - Ref texture; + Ref texture; struct GUI { @@ -342,15 +342,10 @@ public: bool get_clear_on_new_frame() const; void clear(); - void set_filter(bool p_enable); - bool get_filter() const; - - void set_gen_mipmaps(bool p_enable); - bool get_gen_mipmaps() const; void set_update_mode(UpdateMode p_mode); UpdateMode get_update_mode() const; - Ref get_texture() const; + Ref get_texture() const; Vector2 get_camera_coords(const Vector2& p_viewport_coords) const; @@ -368,6 +363,9 @@ public: void set_disable_input(bool p_disable); bool is_input_disabled() const; + void set_disable_3d(bool p_disable); + bool is_3d_disabled() const; + void set_attach_to_screen_rect(const Rect2& p_rect); Rect2 get_attach_to_screen_rect() const; diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index dcb2edf872..1bff6801fe 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -66,6 +66,7 @@ #include "scene/gui/center_container.h" #include "scene/gui/scroll_container.h" #include "scene/gui/margin_container.h" +#include "scene/gui/viewport_container.h" #include "scene/gui/panel.h" #include "scene/gui/spin_box.h" #include "scene/gui/file_dialog.h" @@ -300,7 +301,7 @@ void register_scene_types() { ObjectTypeDB::register_virtual_type(); ObjectTypeDB::register_type(); - ObjectTypeDB::register_virtual_type(); + ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); @@ -390,6 +391,7 @@ void register_scene_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::register_type(); OS::get_singleton()->yield(); //may take time to init -- cgit v1.2.3