viewport.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*************************************************************************/
  2. /* viewport.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 ViewportTexture : public Texture {
  50. GDCLASS(ViewportTexture, Texture);
  51. NodePath path;
  52. friend class Viewport;
  53. Viewport *vp;
  54. protected:
  55. static void _bind_methods();
  56. public:
  57. void set_viewport_path_in_scene(const NodePath &p_path);
  58. NodePath get_viewport_path_in_scene() const;
  59. virtual void setup_local_to_scene();
  60. virtual int get_width() const;
  61. virtual int get_height() const;
  62. virtual Size2 get_size() const;
  63. virtual RID get_rid() const;
  64. virtual bool has_alpha() const;
  65. virtual void set_flags(uint32_t p_flags);
  66. virtual uint32_t get_flags() const;
  67. virtual Ref<Image> get_data() const;
  68. ViewportTexture();
  69. ~ViewportTexture();
  70. };
  71. class Viewport : public Node {
  72. GDCLASS(Viewport, Node);
  73. public:
  74. enum UpdateMode {
  75. UPDATE_DISABLED,
  76. UPDATE_ONCE, //then goes to disabled
  77. UPDATE_WHEN_VISIBLE, // default
  78. UPDATE_ALWAYS
  79. };
  80. enum ShadowAtlasQuadrantSubdiv {
  81. SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED,
  82. SHADOW_ATLAS_QUADRANT_SUBDIV_1,
  83. SHADOW_ATLAS_QUADRANT_SUBDIV_4,
  84. SHADOW_ATLAS_QUADRANT_SUBDIV_16,
  85. SHADOW_ATLAS_QUADRANT_SUBDIV_64,
  86. SHADOW_ATLAS_QUADRANT_SUBDIV_256,
  87. SHADOW_ATLAS_QUADRANT_SUBDIV_1024,
  88. SHADOW_ATLAS_QUADRANT_SUBDIV_MAX,
  89. };
  90. enum MSAA {
  91. MSAA_DISABLED,
  92. MSAA_2X,
  93. MSAA_4X,
  94. MSAA_8X,
  95. MSAA_16X,
  96. };
  97. enum Usage {
  98. USAGE_2D,
  99. USAGE_2D_NO_SAMPLING,
  100. USAGE_3D,
  101. USAGE_3D_NO_EFFECTS,
  102. };
  103. enum RenderInfo {
  104. RENDER_INFO_OBJECTS_IN_FRAME,
  105. RENDER_INFO_VERTICES_IN_FRAME,
  106. RENDER_INFO_MATERIAL_CHANGES_IN_FRAME,
  107. RENDER_INFO_SHADER_CHANGES_IN_FRAME,
  108. RENDER_INFO_SURFACE_CHANGES_IN_FRAME,
  109. RENDER_INFO_DRAW_CALLS_IN_FRAME,
  110. RENDER_INFO_MAX
  111. };
  112. enum DebugDraw {
  113. DEBUG_DRAW_DISABLED,
  114. DEBUG_DRAW_UNSHADED,
  115. DEBUG_DRAW_OVERDRAW,
  116. DEBUG_DRAW_WIREFRAME,
  117. };
  118. enum ClearMode {
  119. CLEAR_MODE_ALWAYS,
  120. CLEAR_MODE_NEVER,
  121. CLEAR_MODE_ONLY_NEXT_FRAME
  122. };
  123. private:
  124. friend class ViewportTexture;
  125. Viewport *parent;
  126. Listener *listener;
  127. Set<Listener *> listeners;
  128. bool arvr;
  129. Camera *camera;
  130. Set<Camera *> cameras;
  131. RID viewport;
  132. RID current_canvas;
  133. bool audio_listener;
  134. RID internal_listener;
  135. bool audio_listener_2d;
  136. RID internal_listener_2d;
  137. Transform2D canvas_transform;
  138. Transform2D global_canvas_transform;
  139. Transform2D stretch_transform;
  140. Size2 size;
  141. Rect2 to_screen_rect;
  142. RID contact_2d_debug;
  143. RID contact_3d_debug_multimesh;
  144. RID contact_3d_debug_instance;
  145. bool size_override;
  146. bool size_override_stretch;
  147. Size2 size_override_size;
  148. Size2 size_override_margin;
  149. Rect2 last_vp_rect;
  150. bool transparent_bg;
  151. bool vflip;
  152. ClearMode clear_mode;
  153. bool filter;
  154. bool gen_mipmaps;
  155. bool snap_controls_to_pixels;
  156. bool physics_object_picking;
  157. List<Ref<InputEvent> > physics_picking_events;
  158. ObjectID physics_object_capture;
  159. ObjectID physics_object_over;
  160. Vector2 physics_last_mousepos;
  161. void _test_new_mouseover(ObjectID new_collider);
  162. Map<ObjectID, uint64_t> physics_2d_mouseover;
  163. void _update_rect();
  164. void _parent_resized();
  165. void _parent_draw();
  166. void _parent_visibility_changed();
  167. Ref<World2D> world_2d;
  168. Ref<World> world;
  169. Ref<World> own_world;
  170. StringName input_group;
  171. StringName gui_input_group;
  172. StringName unhandled_input_group;
  173. StringName unhandled_key_input_group;
  174. void _update_listener();
  175. void _update_listener_2d();
  176. void _propagate_enter_world(Node *p_node);
  177. void _propagate_exit_world(Node *p_node);
  178. void _propagate_viewport_notification(Node *p_node, int p_what);
  179. void _update_stretch_transform();
  180. void _update_global_transform();
  181. bool disable_3d;
  182. UpdateMode update_mode;
  183. RID texture_rid;
  184. uint32_t texture_flags;
  185. DebugDraw debug_draw;
  186. Usage usage;
  187. int shadow_atlas_size;
  188. ShadowAtlasQuadrantSubdiv shadow_atlas_quadrant_subdiv[4];
  189. MSAA msaa;
  190. bool hdr;
  191. Ref<ViewportTexture> default_texture;
  192. Set<ViewportTexture *> viewport_textures;
  193. struct GUI {
  194. // info used when this is a window
  195. bool key_event_accepted;
  196. Control *mouse_focus;
  197. int mouse_focus_button;
  198. Control *key_focus;
  199. Control *mouse_over;
  200. Control *tooltip;
  201. Panel *tooltip_popup;
  202. Label *tooltip_label;
  203. Point2 tooltip_pos;
  204. Point2 last_mouse_pos;
  205. Point2 drag_accum;
  206. bool drag_attempted;
  207. Variant drag_data;
  208. Control *drag_preview;
  209. float tooltip_timer;
  210. float tooltip_delay;
  211. List<Control *> modal_stack;
  212. unsigned int cancelled_input_ID;
  213. Transform2D focus_inv_xform;
  214. bool subwindow_order_dirty;
  215. List<Control *> subwindows;
  216. bool roots_order_dirty;
  217. List<Control *> roots;
  218. int canvas_sort_index; //for sorting items with canvas as root
  219. GUI();
  220. } gui;
  221. bool disable_input;
  222. void _gui_call_input(Control *p_control, const Ref<InputEvent> &p_input);
  223. void _gui_sort_subwindows();
  224. void _gui_sort_roots();
  225. void _gui_sort_modal_stack();
  226. Control *_gui_find_control(const Point2 &p_global);
  227. Control *_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Transform2D &p_xform, Transform2D &r_inv_xform);
  228. void _gui_input_event(Ref<InputEvent> p_event);
  229. void update_worlds();
  230. _FORCE_INLINE_ Transform2D _get_input_pre_xform() const;
  231. void _vp_enter_tree();
  232. void _vp_exit_tree();
  233. void _vp_input(const Ref<InputEvent> &p_ev);
  234. void _vp_input_text(const String &p_text);
  235. void _vp_unhandled_input(const Ref<InputEvent> &p_ev);
  236. Ref<InputEvent> _make_input_local(const Ref<InputEvent> &ev);
  237. friend class Control;
  238. List<Control *>::Element *_gui_add_root_control(Control *p_control);
  239. List<Control *>::Element *_gui_add_subwindow_control(Control *p_control);
  240. void _gui_set_subwindow_order_dirty();
  241. void _gui_set_root_order_dirty();
  242. void _gui_remove_modal_control(List<Control *>::Element *MI);
  243. void _gui_remove_from_modal_stack(List<Control *>::Element *MI, ObjectID p_prev_focus_owner);
  244. void _gui_remove_root_control(List<Control *>::Element *RI);
  245. void _gui_remove_subwindow_control(List<Control *>::Element *SI);
  246. void _gui_cancel_tooltip();
  247. void _gui_show_tooltip();
  248. void _gui_remove_control(Control *p_control);
  249. void _gui_hid_control(Control *p_control);
  250. void _gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control);
  251. void _gui_set_drag_preview(Control *p_base, Control *p_control);
  252. bool _gui_is_modal_on_top(const Control *p_control);
  253. List<Control *>::Element *_gui_show_modal(Control *p_control);
  254. void _gui_remove_focus();
  255. void _gui_unfocus_control(Control *p_control);
  256. bool _gui_control_has_focus(const Control *p_control);
  257. void _gui_control_grab_focus(Control *p_control);
  258. void _gui_grab_click_focus(Control *p_control);
  259. void _gui_accept_event();
  260. Control *_gui_get_focus_owner();
  261. Vector2 _get_window_offset() const;
  262. bool _gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check);
  263. friend class Listener;
  264. void _listener_transform_changed_notify();
  265. void _listener_set(Listener *p_listener);
  266. bool _listener_add(Listener *p_listener); //true if first
  267. void _listener_remove(Listener *p_listener);
  268. void _listener_make_next_current(Listener *p_exclude);
  269. friend class Camera;
  270. void _camera_transform_changed_notify();
  271. void _camera_set(Camera *p_camera);
  272. bool _camera_add(Camera *p_camera); //true if first
  273. void _camera_remove(Camera *p_camera);
  274. void _camera_make_next_current(Camera *p_exclude);
  275. protected:
  276. void _notification(int p_what);
  277. static void _bind_methods();
  278. public:
  279. Listener *get_listener() const;
  280. Camera *get_camera() const;
  281. void set_use_arvr(bool p_use_arvr);
  282. bool use_arvr();
  283. void set_as_audio_listener(bool p_enable);
  284. bool is_audio_listener() const;
  285. void set_as_audio_listener_2d(bool p_enable);
  286. bool is_audio_listener_2d() const;
  287. void set_size(const Size2 &p_size);
  288. Size2 get_size() const;
  289. Rect2 get_visible_rect() const;
  290. RID get_viewport_rid() const;
  291. void set_world(const Ref<World> &p_world);
  292. void set_world_2d(const Ref<World2D> &p_world_2d);
  293. Ref<World> get_world() const;
  294. Ref<World> find_world() const;
  295. Ref<World2D> get_world_2d() const;
  296. Ref<World2D> find_world_2d() const;
  297. void set_canvas_transform(const Transform2D &p_transform);
  298. Transform2D get_canvas_transform() const;
  299. void set_global_canvas_transform(const Transform2D &p_transform);
  300. Transform2D get_global_canvas_transform() const;
  301. Transform2D get_final_transform() const;
  302. void set_transparent_background(bool p_enable);
  303. bool has_transparent_background() const;
  304. void set_size_override(bool p_enable, const Size2 &p_size = Size2(-1, -1), const Vector2 &p_margin = Vector2());
  305. Size2 get_size_override() const;
  306. bool is_size_override_enabled() const;
  307. void set_size_override_stretch(bool p_enable);
  308. bool is_size_override_stretch_enabled() const;
  309. void set_vflip(bool p_enable);
  310. bool get_vflip() const;
  311. void set_clear_mode(ClearMode p_mode);
  312. ClearMode get_clear_mode() const;
  313. void set_update_mode(UpdateMode p_mode);
  314. UpdateMode get_update_mode() const;
  315. Ref<ViewportTexture> get_texture() const;
  316. void set_shadow_atlas_size(int p_size);
  317. int get_shadow_atlas_size() const;
  318. void set_shadow_atlas_quadrant_subdiv(int p_quadrant, ShadowAtlasQuadrantSubdiv p_subdiv);
  319. ShadowAtlasQuadrantSubdiv get_shadow_atlas_quadrant_subdiv(int p_quadrant) const;
  320. void set_msaa(MSAA p_msaa);
  321. MSAA get_msaa() const;
  322. void set_hdr(bool p_hdr);
  323. bool get_hdr() const;
  324. Vector2 get_camera_coords(const Vector2 &p_viewport_coords) const;
  325. Vector2 get_camera_rect_size() const;
  326. void set_use_own_world(bool p_world);
  327. bool is_using_own_world() const;
  328. void input(const Ref<InputEvent> &p_event);
  329. void unhandled_input(const Ref<InputEvent> &p_event);
  330. void set_disable_input(bool p_disable);
  331. bool is_input_disabled() const;
  332. void set_disable_3d(bool p_disable);
  333. bool is_3d_disabled() const;
  334. void set_attach_to_screen_rect(const Rect2 &p_rect);
  335. Rect2 get_attach_to_screen_rect() const;
  336. Vector2 get_mouse_position() const;
  337. void warp_mouse(const Vector2 &p_pos);
  338. void set_physics_object_picking(bool p_enable);
  339. bool get_physics_object_picking();
  340. bool gui_has_modal_stack() const;
  341. Variant gui_get_drag_data() const;
  342. Control *get_modal_stack_top() const;
  343. void gui_reset_canvas_sort_index();
  344. int gui_get_canvas_sort_index();
  345. virtual String get_configuration_warning() const;
  346. void set_usage(Usage p_usage);
  347. Usage get_usage() const;
  348. void set_debug_draw(DebugDraw p_debug_draw);
  349. DebugDraw get_debug_draw() const;
  350. int get_render_info(RenderInfo p_info);
  351. void set_snap_controls_to_pixels(bool p_enable);
  352. bool is_snap_controls_to_pixels_enabled() const;
  353. Viewport();
  354. ~Viewport();
  355. };
  356. VARIANT_ENUM_CAST(Viewport::UpdateMode);
  357. VARIANT_ENUM_CAST(Viewport::ShadowAtlasQuadrantSubdiv);
  358. VARIANT_ENUM_CAST(Viewport::MSAA);
  359. VARIANT_ENUM_CAST(Viewport::Usage);
  360. VARIANT_ENUM_CAST(Viewport::DebugDraw);
  361. VARIANT_ENUM_CAST(Viewport::ClearMode);
  362. VARIANT_ENUM_CAST(Viewport::RenderInfo);
  363. #endif