viewport.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*************************************************************************/
  2. /* viewport.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef VIEWPORT_H
  30. #define VIEWPORT_H
  31. #include "scene/main/node.h"
  32. #include "servers/visual_server.h"
  33. #include "scene/resources/world_2d.h"
  34. #include "math_2d.h"
  35. #include "scene/resources/texture.h"
  36. /**
  37. @author Juan Linietsky <[email protected]>
  38. */
  39. class Camera;
  40. class Viewport;
  41. class RenderTargetTexture : public Texture {
  42. OBJ_TYPE( RenderTargetTexture, Texture );
  43. int flags;
  44. friend class Viewport;
  45. Viewport *vp;
  46. public:
  47. virtual int get_width() const;
  48. virtual int get_height() const;
  49. virtual Size2 get_size() const;
  50. virtual RID get_rid() const;
  51. virtual bool has_alpha() const;
  52. virtual void set_flags(uint32_t p_flags);
  53. virtual uint32_t get_flags() const;
  54. RenderTargetTexture(Viewport *p_vp=NULL);
  55. };
  56. class Viewport : public Node {
  57. OBJ_TYPE( Viewport, Node );
  58. public:
  59. enum RenderTargetUpdateMode {
  60. RENDER_TARGET_UPDATE_DISABLED,
  61. RENDER_TARGET_UPDATE_ONCE, //then goes to disabled
  62. RENDER_TARGET_UPDATE_WHEN_VISIBLE, // default
  63. RENDER_TARGET_UPDATE_ALWAYS
  64. };
  65. private:
  66. friend class RenderTargetTexture;
  67. Viewport *parent;
  68. Camera *camera;
  69. Set<Camera*> cameras;
  70. RID viewport;
  71. RID canvas_item;
  72. RID current_canvas;
  73. bool audio_listener;
  74. RID listener;
  75. bool audio_listener_2d;
  76. RID listener_2d;
  77. Matrix32 canvas_transform;
  78. Matrix32 global_canvas_transform;
  79. Matrix32 stretch_transform;
  80. Rect2 rect;
  81. Rect2 to_screen_rect;
  82. bool size_override;
  83. bool size_override_stretch;
  84. Size2 size_override_size;
  85. Size2 size_override_margin;
  86. bool transparent_bg;
  87. bool render_target_vflip;
  88. bool render_target_filter;
  89. bool render_target_gen_mipmaps;
  90. void _update_rect();
  91. void _parent_resized();
  92. void _parent_draw();
  93. void _parent_visibility_changed();
  94. Ref<World2D> world_2d;
  95. Ref<World> world;
  96. Ref<World> own_world;
  97. StringName input_group;
  98. StringName gui_input_group;
  99. StringName unhandled_input_group;
  100. StringName unhandled_key_input_group;
  101. void _update_listener();
  102. void _update_listener_2d();
  103. void _propagate_enter_world(Node *p_node);
  104. void _propagate_exit_world(Node *p_node);
  105. void _update_stretch_transform();
  106. void _update_global_transform();
  107. bool render_target;
  108. RenderTargetUpdateMode render_target_update_mode;
  109. RID render_target_texture_rid;
  110. Ref<RenderTargetTexture> render_target_texture;
  111. void update_worlds();
  112. _FORCE_INLINE_ Matrix32 _get_input_pre_xform() const;
  113. void _vp_enter_scene();
  114. void _vp_exit_scene();
  115. void _vp_input(const InputEvent& p_ev);
  116. void _vp_unhandled_input(const InputEvent& p_ev);
  117. void _make_input_local(InputEvent& ev);
  118. friend class Camera;
  119. void _camera_transform_changed_notify();
  120. void _set_camera(Camera* p_camera);
  121. protected:
  122. void _notification(int p_what);
  123. static void _bind_methods();
  124. public:
  125. Camera* get_camera() const;
  126. void set_as_audio_listener(bool p_enable);
  127. bool is_audio_listener() const;
  128. void set_as_audio_listener_2d(bool p_enable);
  129. bool is_audio_listener_2d() const;
  130. void set_rect(const Rect2& p_rect);
  131. Rect2 get_rect() const;
  132. Rect2 get_visible_rect() const;
  133. RID get_viewport() const;
  134. void set_world(const Ref<World>& p_world);
  135. Ref<World> get_world() const;
  136. Ref<World> find_world() const;
  137. Ref<World2D> find_world_2d() const;
  138. void set_canvas_transform(const Matrix32& p_transform);
  139. Matrix32 get_canvas_transform() const;
  140. void set_global_canvas_transform(const Matrix32& p_transform);
  141. Matrix32 get_global_canvas_transform() const;
  142. Matrix32 get_final_transform() const;
  143. void set_transparent_background(bool p_enable);
  144. bool has_transparent_background() const;
  145. void set_size_override(bool p_enable,const Size2& p_size=Size2(-1,-1),const Vector2& p_margin=Vector2());
  146. Size2 get_size_override() const;
  147. bool is_size_override_enabled() const;
  148. void set_size_override_stretch(bool p_enable);
  149. bool is_size_override_stretch_enabled() const;
  150. void set_as_render_target(bool p_enable);
  151. bool is_set_as_render_target() const;
  152. void set_render_target_vflip(bool p_enable);
  153. bool get_render_target_vflip() const;
  154. void set_render_target_filter(bool p_enable);
  155. bool get_render_target_filter() const;
  156. void set_render_target_gen_mipmaps(bool p_enable);
  157. bool get_render_target_gen_mipmaps() const;
  158. void set_render_target_update_mode(RenderTargetUpdateMode p_mode);
  159. RenderTargetUpdateMode get_render_target_update_mode() const;
  160. Ref<RenderTargetTexture> get_render_target_texture() const;
  161. void queue_screen_capture();
  162. Image get_screen_capture() const;
  163. void set_use_own_world(bool p_world);
  164. bool is_using_own_world() const;
  165. void input(const InputEvent& p_event);
  166. void unhandled_input(const InputEvent& p_event);
  167. void set_render_target_to_screen_rect(const Rect2& p_rect);
  168. Rect2 get_render_target_to_screen_rect() const;
  169. Viewport();
  170. ~Viewport();
  171. };
  172. VARIANT_ENUM_CAST(Viewport::RenderTargetUpdateMode);
  173. #endif