viewport.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* viewport.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef VIEWPORT_H
  31. #define VIEWPORT_H
  32. #include "math_2d.h"
  33. #include "scene/main/node.h"
  34. #include "scene/resources/texture.h"
  35. #include "scene/resources/world_2d.h"
  36. #include "servers/visual_server.h"
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. class Camera;
  41. class Camera2D;
  42. class Listener;
  43. class Control;
  44. class CanvasItem;
  45. class Panel;
  46. class Label;
  47. class Timer;
  48. class Viewport;
  49. class RenderTargetTexture : public Texture {
  50. OBJ_TYPE(RenderTargetTexture, Texture);
  51. int flags;
  52. friend class Viewport;
  53. Viewport *vp;
  54. public:
  55. virtual int get_width() const;
  56. virtual int get_height() const;
  57. virtual Size2 get_size() const;
  58. virtual RID get_rid() const;
  59. virtual bool has_alpha() const;
  60. virtual void set_flags(uint32_t p_flags);
  61. virtual uint32_t get_flags() const;
  62. RenderTargetTexture(Viewport *p_vp = NULL);
  63. };
  64. class Viewport : public Node {
  65. OBJ_TYPE(Viewport, Node);
  66. public:
  67. enum RenderTargetUpdateMode {
  68. RENDER_TARGET_UPDATE_DISABLED,
  69. RENDER_TARGET_UPDATE_ONCE, //then goes to disabled
  70. RENDER_TARGET_UPDATE_WHEN_VISIBLE, // default
  71. RENDER_TARGET_UPDATE_ALWAYS
  72. };
  73. private:
  74. friend class RenderTargetTexture;
  75. Control *parent_control;
  76. Viewport *parent;
  77. Listener *listener;
  78. Set<Listener *> listeners;
  79. Camera *camera;
  80. Set<Camera *> cameras;
  81. RID viewport;
  82. RID canvas_item;
  83. RID current_canvas;
  84. bool audio_listener;
  85. RID internal_listener;
  86. bool audio_listener_2d;
  87. RID internal_listener_2d;
  88. Matrix32 canvas_transform;
  89. Matrix32 global_canvas_transform;
  90. Matrix32 stretch_transform;
  91. Rect2 rect;
  92. Rect2 to_screen_rect;
  93. RID contact_2d_debug;
  94. RID contact_3d_debug_multimesh;
  95. RID contact_3d_debug_instance;
  96. bool size_override;
  97. bool size_override_stretch;
  98. Size2 size_override_size;
  99. Size2 size_override_margin;
  100. Rect2 last_vp_rect;
  101. bool transparent_bg;
  102. bool render_target_vflip;
  103. bool render_target_clear_on_new_frame;
  104. bool render_target_filter;
  105. bool render_target_gen_mipmaps;
  106. bool physics_object_picking;
  107. List<InputEvent> physics_picking_events;
  108. ObjectID physics_object_capture;
  109. ObjectID physics_object_over;
  110. Vector2 physics_last_mousepos;
  111. void _test_new_mouseover(ObjectID new_collider);
  112. Map<ObjectID, uint64_t> physics_2d_mouseover;
  113. void _update_rect();
  114. void _parent_resized();
  115. void _parent_draw();
  116. void _parent_visibility_changed();
  117. Ref<World2D> world_2d;
  118. Ref<World> world;
  119. Ref<World> own_world;
  120. StringName input_group;
  121. StringName gui_input_group;
  122. StringName unhandled_input_group;
  123. StringName unhandled_key_input_group;
  124. void _update_listener();
  125. void _update_listener_2d();
  126. void _propagate_enter_world(Node *p_node);
  127. void _propagate_exit_world(Node *p_node);
  128. void _propagate_viewport_notification(Node *p_node, int p_what);
  129. void _update_stretch_transform();
  130. void _update_global_transform();
  131. bool render_target;
  132. RenderTargetUpdateMode render_target_update_mode;
  133. RID render_target_texture_rid;
  134. Ref<RenderTargetTexture> render_target_texture;
  135. struct GUI {
  136. // info used when this is a window
  137. bool key_event_accepted;
  138. Control *mouse_focus;
  139. int mouse_focus_button;
  140. Control *key_focus;
  141. Control *mouse_over;
  142. Control *tooltip;
  143. Panel *tooltip_popup;
  144. Label *tooltip_label;
  145. Point2 tooltip_pos;
  146. Point2 last_mouse_pos;
  147. Point2 drag_accum;
  148. bool drag_attempted;
  149. Variant drag_data;
  150. Control *drag_preview;
  151. float tooltip_timer;
  152. float tooltip_delay;
  153. List<Control *> modal_stack;
  154. unsigned int cancelled_input_ID;
  155. Matrix32 focus_inv_xform;
  156. bool subwindow_order_dirty;
  157. List<Control *> subwindows;
  158. bool roots_order_dirty;
  159. List<Control *> roots;
  160. GUI();
  161. } gui;
  162. bool disable_input;
  163. void _gui_call_input(Control *p_control, const InputEvent &p_input);
  164. void _gui_sort_subwindows();
  165. void _gui_sort_roots();
  166. void _gui_sort_modal_stack();
  167. Control *_gui_find_control(const Point2 &p_global);
  168. Control *_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Matrix32 &p_xform, Matrix32 &r_inv_xform);
  169. void _gui_input_event(InputEvent p_event);
  170. void update_worlds();
  171. _FORCE_INLINE_ Matrix32 _get_input_pre_xform() const;
  172. void _vp_enter_tree();
  173. void _vp_exit_tree();
  174. void _vp_input(const InputEvent &p_ev);
  175. void _vp_input_text(const String &p_text);
  176. void _vp_unhandled_input(const InputEvent &p_ev);
  177. void _make_input_local(InputEvent &ev);
  178. friend class Control;
  179. List<Control *>::Element *_gui_add_root_control(Control *p_control);
  180. List<Control *>::Element *_gui_add_subwindow_control(Control *p_control);
  181. void _gui_set_subwindow_order_dirty();
  182. void _gui_set_root_order_dirty();
  183. void _gui_remove_modal_control(List<Control *>::Element *MI);
  184. void _gui_remove_from_modal_stack(List<Control *>::Element *MI, ObjectID p_prev_focus_owner);
  185. void _gui_remove_root_control(List<Control *>::Element *RI);
  186. void _gui_remove_subwindow_control(List<Control *>::Element *SI);
  187. void _gui_cancel_tooltip();
  188. void _gui_show_tooltip();
  189. void _gui_remove_control(Control *p_control);
  190. void _gui_hid_control(Control *p_control);
  191. void _gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control);
  192. void _gui_set_drag_preview(Control *p_base, Control *p_control);
  193. bool _gui_is_modal_on_top(const Control *p_control);
  194. List<Control *>::Element *_gui_show_modal(Control *p_control);
  195. void _gui_remove_focus();
  196. void _gui_unfocus_control(Control *p_control);
  197. bool _gui_control_has_focus(const Control *p_control);
  198. void _gui_control_grab_focus(Control *p_control);
  199. void _gui_grab_click_focus(Control *p_control);
  200. void _gui_accept_event();
  201. Control *_gui_get_focus_owner();
  202. Vector2 _get_window_offset() const;
  203. friend class Listener;
  204. void _listener_transform_changed_notify();
  205. void _listener_set(Listener *p_listener);
  206. bool _listener_add(Listener *p_listener); //true if first
  207. void _listener_remove(Listener *p_listener);
  208. void _listener_make_next_current(Listener *p_exclude);
  209. friend class Camera;
  210. void _camera_transform_changed_notify();
  211. void _camera_set(Camera *p_camera);
  212. bool _camera_add(Camera *p_camera); //true if first
  213. void _camera_remove(Camera *p_camera);
  214. void _camera_make_next_current(Camera *p_exclude);
  215. protected:
  216. void _notification(int p_what);
  217. static void _bind_methods();
  218. public:
  219. Listener *get_listener() const;
  220. Camera *get_camera() const;
  221. void set_as_audio_listener(bool p_enable);
  222. bool is_audio_listener() const;
  223. void set_as_audio_listener_2d(bool p_enable);
  224. bool is_audio_listener_2d() const;
  225. void set_rect(const Rect2 &p_rect);
  226. Rect2 get_rect() const;
  227. Rect2 get_visible_rect() const;
  228. RID get_viewport() const;
  229. void set_world(const Ref<World> &p_world);
  230. void set_world_2d(const Ref<World2D> &p_world_2d);
  231. Ref<World> get_world() const;
  232. Ref<World> find_world() const;
  233. Ref<World2D> get_world_2d() const;
  234. Ref<World2D> find_world_2d() const;
  235. void set_canvas_transform(const Matrix32 &p_transform);
  236. Matrix32 get_canvas_transform() const;
  237. void set_global_canvas_transform(const Matrix32 &p_transform);
  238. Matrix32 get_global_canvas_transform() const;
  239. Matrix32 get_final_transform() const;
  240. void set_transparent_background(bool p_enable);
  241. bool has_transparent_background() const;
  242. void set_size_override(bool p_enable, const Size2 &p_size = Size2(-1, -1), const Vector2 &p_margin = Vector2());
  243. Size2 get_size_override() const;
  244. bool is_size_override_enabled() const;
  245. void set_size_override_stretch(bool p_enable);
  246. bool is_size_override_stretch_enabled() const;
  247. void set_as_render_target(bool p_enable);
  248. bool is_set_as_render_target() const;
  249. void set_render_target_vflip(bool p_enable);
  250. bool get_render_target_vflip() const;
  251. void set_render_target_clear_on_new_frame(bool p_enable);
  252. bool get_render_target_clear_on_new_frame() const;
  253. void render_target_clear();
  254. void set_render_target_filter(bool p_enable);
  255. bool get_render_target_filter() const;
  256. void set_render_target_gen_mipmaps(bool p_enable);
  257. bool get_render_target_gen_mipmaps() const;
  258. void set_render_target_update_mode(RenderTargetUpdateMode p_mode);
  259. RenderTargetUpdateMode get_render_target_update_mode() const;
  260. Ref<RenderTargetTexture> get_render_target_texture() const;
  261. Vector2 get_camera_coords(const Vector2 &p_viewport_coords) const;
  262. Vector2 get_camera_rect_size() const;
  263. void queue_screen_capture();
  264. Image get_screen_capture() const;
  265. void set_use_own_world(bool p_world);
  266. bool is_using_own_world() const;
  267. void input(const InputEvent &p_event);
  268. void unhandled_input(const InputEvent &p_event);
  269. void set_disable_input(bool p_disable);
  270. bool is_input_disabled() const;
  271. void set_render_target_to_screen_rect(const Rect2 &p_rect);
  272. Rect2 get_render_target_to_screen_rect() const;
  273. Vector2 get_mouse_pos() const;
  274. void warp_mouse(const Vector2 &p_pos);
  275. void set_physics_object_picking(bool p_enable);
  276. bool get_physics_object_picking();
  277. bool gui_has_modal_stack() const;
  278. Variant gui_get_drag_data() const;
  279. Control *get_modal_stack_top() const;
  280. virtual String get_configuration_warning() const;
  281. Viewport();
  282. ~Viewport();
  283. };
  284. VARIANT_ENUM_CAST(Viewport::RenderTargetUpdateMode);
  285. #endif