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 31 #ifndef VIEWPORT_H 32 #define VIEWPORT_H 33 34 #include "core/math/transform_2d.h" 35 #include "scene/main/node.h" 36 #include "scene/resources/texture.h" 37 #include "scene/resources/world_2d.h" 38 #include "servers/visual_server.h" 39 40 class Camera; 41 class Camera2D; 42 class Listener; 43 class Control; 44 class CanvasItem; 45 class CanvasLayer; 46 class Panel; 47 class Label; 48 class Timer; 49 class Viewport; 50 class CollisionObject; 51 52 class ViewportTexture : public Texture { 53 54 GDCLASS(ViewportTexture, Texture); 55 56 NodePath path; 57 58 friend class Viewport; 59 Viewport *vp; 60 uint32_t flags; 61 62 RID proxy; 63 64 protected: 65 static void _bind_methods(); 66 67 public: 68 void set_viewport_path_in_scene(const NodePath &p_path); 69 NodePath get_viewport_path_in_scene() const; 70 71 virtual void setup_local_to_scene(); 72 73 virtual int get_width() const; 74 virtual int get_height() const; 75 virtual Size2 get_size() const; 76 virtual RID get_rid() const; 77 78 virtual bool has_alpha() const; 79 80 virtual void set_flags(uint32_t p_flags); 81 virtual uint32_t get_flags() const; 82 83 virtual Ref<Image> get_data() const; 84 85 ViewportTexture(); 86 ~ViewportTexture(); 87 }; 88 89 class Viewport : public Node { 90 91 GDCLASS(Viewport, Node); 92 93 public: 94 enum UpdateMode { 95 UPDATE_DISABLED, 96 UPDATE_ONCE, //then goes to disabled 97 UPDATE_WHEN_VISIBLE, // default 98 UPDATE_ALWAYS 99 }; 100 101 enum ShadowAtlasQuadrantSubdiv { 102 SHADOW_ATLAS_QUADRANT_SUBDIV_DISABLED, 103 SHADOW_ATLAS_QUADRANT_SUBDIV_1, 104 SHADOW_ATLAS_QUADRANT_SUBDIV_4, 105 SHADOW_ATLAS_QUADRANT_SUBDIV_16, 106 SHADOW_ATLAS_QUADRANT_SUBDIV_64, 107 SHADOW_ATLAS_QUADRANT_SUBDIV_256, 108 SHADOW_ATLAS_QUADRANT_SUBDIV_1024, 109 SHADOW_ATLAS_QUADRANT_SUBDIV_MAX, 110 111 }; 112 113 enum MSAA { 114 MSAA_DISABLED, 115 MSAA_2X, 116 MSAA_4X, 117 MSAA_8X, 118 MSAA_16X, 119 }; 120 121 enum Usage { 122 USAGE_2D, 123 USAGE_2D_NO_SAMPLING, 124 USAGE_3D, 125 USAGE_3D_NO_EFFECTS, 126 }; 127 128 enum RenderInfo { 129 130 RENDER_INFO_OBJECTS_IN_FRAME, 131 RENDER_INFO_VERTICES_IN_FRAME, 132 RENDER_INFO_MATERIAL_CHANGES_IN_FRAME, 133 RENDER_INFO_SHADER_CHANGES_IN_FRAME, 134 RENDER_INFO_SURFACE_CHANGES_IN_FRAME, 135 RENDER_INFO_DRAW_CALLS_IN_FRAME, 136 RENDER_INFO_2D_ITEMS_IN_FRAME, 137 RENDER_INFO_2D_DRAW_CALLS_IN_FRAME, 138 RENDER_INFO_MAX 139 }; 140 141 enum DebugDraw { 142 DEBUG_DRAW_DISABLED, 143 DEBUG_DRAW_UNSHADED, 144 DEBUG_DRAW_OVERDRAW, 145 DEBUG_DRAW_WIREFRAME, 146 }; 147 148 enum ClearMode { 149 150 CLEAR_MODE_ALWAYS, 151 CLEAR_MODE_NEVER, 152 CLEAR_MODE_ONLY_NEXT_FRAME 153 }; 154 155 private: 156 friend class ViewportTexture; 157 158 Viewport *parent; 159 160 Listener *listener; 161 Set<Listener *> listeners; 162 163 bool arvr; 164 165 struct CameraOverrideData { 166 Transform transform; 167 enum Projection { 168 PROJECTION_PERSPECTIVE, 169 PROJECTION_ORTHOGONAL 170 }; 171 Projection projection; 172 float fov; 173 float size; 174 float z_near; 175 float z_far; 176 RID rid; 177 178 operator bool() const { 179 return rid != RID(); 180 } 181 } camera_override; 182 183 Camera *camera; 184 Set<Camera *> cameras; 185 Set<CanvasLayer *> canvas_layers; 186 187 RID viewport; 188 RID current_canvas; 189 190 bool audio_listener; 191 RID internal_listener; 192 193 bool audio_listener_2d; 194 RID internal_listener_2d; 195 196 bool override_canvas_transform; 197 198 Transform2D canvas_transform_override; 199 Transform2D canvas_transform; 200 Transform2D global_canvas_transform; 201 Transform2D stretch_transform; 202 203 Size2 size; 204 Rect2 to_screen_rect; 205 bool render_direct_to_screen; 206 207 RID contact_2d_debug; 208 RID contact_3d_debug_multimesh; 209 RID contact_3d_debug_instance; 210 211 bool size_override; 212 bool size_override_stretch; 213 Size2 size_override_size; 214 Size2 size_override_margin; 215 216 Rect2 last_vp_rect; 217 218 bool transparent_bg; 219 bool vflip; 220 ClearMode clear_mode; 221 bool filter; 222 bool gen_mipmaps; 223 224 bool snap_controls_to_pixels; 225 226 bool physics_object_picking; 227 List<Ref<InputEvent> > physics_picking_events; 228 ObjectID physics_object_capture; 229 ObjectID physics_object_over; 230 Transform physics_last_object_transform; 231 Transform physics_last_camera_transform; 232 ObjectID physics_last_id; 233 bool physics_has_last_mousepos; 234 Vector2 physics_last_mousepos; 235 struct { 236 237 bool alt; 238 bool control; 239 bool shift; 240 bool meta; 241 int mouse_mask; 242 243 } physics_last_mouse_state; 244 245 void _collision_object_input_event(CollisionObject *p_object, Camera *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape); 246 247 bool handle_input_locally; 248 bool local_input_handled; 249 250 Map<ObjectID, uint64_t> physics_2d_mouseover; 251 252 Ref<World2D> world_2d; 253 Ref<World> world; 254 Ref<World> own_world; 255 256 StringName input_group; 257 StringName gui_input_group; 258 StringName unhandled_input_group; 259 StringName unhandled_key_input_group; 260 261 void _update_listener(); 262 void _update_listener_2d(); 263 264 void _propagate_enter_world(Node *p_node); 265 void _propagate_exit_world(Node *p_node); 266 void _propagate_viewport_notification(Node *p_node, int p_what); 267 268 void _update_stretch_transform(); 269 void _update_global_transform(); 270 271 bool disable_3d; 272 bool keep_3d_linear; 273 UpdateMode update_mode; 274 RID texture_rid; 275 uint32_t texture_flags; 276 277 DebugDraw debug_draw; 278 279 Usage usage; 280 281 int shadow_atlas_size; 282 ShadowAtlasQuadrantSubdiv shadow_atlas_quadrant_subdiv[4]; 283 284 MSAA msaa; 285 bool hdr; 286 287 Ref<ViewportTexture> default_texture; 288 Set<ViewportTexture *> viewport_textures; 289 290 struct GUI { 291 // info used when this is a window 292 293 bool key_event_accepted; 294 Control *mouse_focus; 295 Control *last_mouse_focus; 296 Control *mouse_click_grabber; 297 int mouse_focus_mask; 298 Control *key_focus; 299 Control *mouse_over; 300 Control *tooltip; 301 Control *tooltip_popup; 302 Label *tooltip_label; 303 Point2 tooltip_pos; 304 Point2 last_mouse_pos; 305 Point2 drag_accum; 306 bool drag_attempted; 307 Variant drag_data; 308 Control *drag_preview; 309 float tooltip_timer; 310 float tooltip_delay; 311 List<Control *> modal_stack; 312 Transform2D focus_inv_xform; 313 bool subwindow_order_dirty; 314 bool subwindow_visibility_dirty; 315 List<Control *> subwindows; // visible subwindows 316 List<Control *> all_known_subwindows; 317 bool roots_order_dirty; 318 List<Control *> roots; 319 int canvas_sort_index; //for sorting items with canvas as root 320 bool dragging; 321 322 GUI(); 323 } gui; 324 325 bool disable_input; 326 327 void _gui_call_input(Control *p_control, const Ref<InputEvent> &p_input); 328 void _gui_call_notification(Control *p_control, int p_what); 329 330 void _gui_prepare_subwindows(); 331 void _gui_sort_subwindows(); 332 void _gui_sort_roots(); 333 void _gui_sort_modal_stack(); 334 Control *_gui_find_control(const Point2 &p_global); 335 Control *_gui_find_control_at_pos(CanvasItem *p_node, const Point2 &p_global, const Transform2D &p_xform, Transform2D &r_inv_xform); 336 337 void _gui_input_event(Ref<InputEvent> p_event); 338 339 void update_worlds(); 340 341 _FORCE_INLINE_ Transform2D _get_input_pre_xform() const; 342 343 void _vp_input(const Ref<InputEvent> &p_ev); 344 void _vp_input_text(const String &p_text); 345 void _vp_unhandled_input(const Ref<InputEvent> &p_ev); 346 Ref<InputEvent> _make_input_local(const Ref<InputEvent> &ev); 347 348 friend class Control; 349 350 List<Control *>::Element *_gui_add_root_control(Control *p_control); 351 List<Control *>::Element *_gui_add_subwindow_control(Control *p_control); 352 353 void _gui_set_subwindow_order_dirty(); 354 void _gui_set_root_order_dirty(); 355 356 void _gui_remove_modal_control(List<Control *>::Element *MI); 357 void _gui_remove_from_modal_stack(List<Control *>::Element *MI, ObjectID p_prev_focus_owner); 358 void _gui_remove_root_control(List<Control *>::Element *RI); 359 void _gui_remove_subwindow_control(List<Control *>::Element *SI); 360 361 String _gui_get_tooltip(Control *p_control, const Vector2 &p_pos, Control **r_which = NULL); 362 void _gui_cancel_tooltip(); 363 void _gui_show_tooltip(); 364 365 void _gui_remove_control(Control *p_control); 366 void _gui_hid_control(Control *p_control); 367 368 void _gui_force_drag(Control *p_base, const Variant &p_data, Control *p_control); 369 void _gui_set_drag_preview(Control *p_base, Control *p_control); 370 371 bool _gui_is_modal_on_top(const Control *p_control); 372 List<Control *>::Element *_gui_show_modal(Control *p_control); 373 374 void _gui_remove_focus(); 375 void _gui_unfocus_control(Control *p_control); 376 bool _gui_control_has_focus(const Control *p_control); 377 void _gui_control_grab_focus(Control *p_control); 378 void _gui_grab_click_focus(Control *p_control); 379 void _post_gui_grab_click_focus(); 380 void _gui_accept_event(); 381 382 Control *_gui_get_focus_owner(); 383 384 Vector2 _get_window_offset() const; 385 386 bool _gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check); 387 388 friend class Listener; 389 void _listener_transform_changed_notify(); 390 void _listener_set(Listener *p_listener); 391 bool _listener_add(Listener *p_listener); //true if first 392 void _listener_remove(Listener *p_listener); 393 void _listener_make_next_current(Listener *p_exclude); 394 395 friend class Camera; 396 void _camera_transform_changed_notify(); 397 void _camera_set(Camera *p_camera); 398 bool _camera_add(Camera *p_camera); //true if first 399 void _camera_remove(Camera *p_camera); 400 void _camera_make_next_current(Camera *p_exclude); 401 402 friend class CanvasLayer; 403 void _canvas_layer_add(CanvasLayer *p_canvas_layer); 404 void _canvas_layer_remove(CanvasLayer *p_canvas_layer); 405 406 void _drop_mouse_focus(); 407 void _drop_physics_mouseover(); 408 409 void _update_canvas_items(Node *p_node); 410 411 void _own_world_changed(); 412 413 protected: 414 void _notification(int p_what); 415 static void _bind_methods(); 416 virtual void _validate_property(PropertyInfo &property) const; 417 418 public: 419 Listener *get_listener() const; 420 Camera *get_camera() const; 421 422 void enable_camera_override(bool p_enable); 423 bool is_camera_override_enabled() const; 424 425 void set_camera_override_transform(const Transform &p_transform); 426 Transform get_camera_override_transform() const; 427 428 void set_camera_override_perspective(float p_fovy_degrees, float p_z_near, float p_z_far); 429 void set_camera_override_orthogonal(float p_size, float p_z_near, float p_z_far); 430 431 void set_use_arvr(bool p_use_arvr); 432 bool use_arvr(); 433 434 void set_as_audio_listener(bool p_enable); 435 bool is_audio_listener() const; 436 437 void set_as_audio_listener_2d(bool p_enable); 438 bool is_audio_listener_2d() const; 439 440 void set_size(const Size2 &p_size); 441 void update_canvas_items(); 442 443 Size2 get_size() const; 444 Rect2 get_visible_rect() const; 445 RID get_viewport_rid() const; 446 447 void set_world(const Ref<World> &p_world); 448 void set_world_2d(const Ref<World2D> &p_world_2d); 449 Ref<World> get_world() const; 450 Ref<World> find_world() const; 451 452 Ref<World2D> get_world_2d() const; 453 Ref<World2D> find_world_2d() const; 454 455 void enable_canvas_transform_override(bool p_enable); 456 bool is_canvas_transform_override_enbled() const; 457 458 void set_canvas_transform_override(const Transform2D &p_transform); 459 Transform2D get_canvas_transform_override() const; 460 461 void set_canvas_transform(const Transform2D &p_transform); 462 Transform2D get_canvas_transform() const; 463 464 void set_global_canvas_transform(const Transform2D &p_transform); 465 Transform2D get_global_canvas_transform() const; 466 467 Transform2D get_final_transform() const; 468 469 void set_transparent_background(bool p_enable); 470 bool has_transparent_background() const; 471 472 void set_size_override(bool p_enable, const Size2 &p_size = Size2(-1, -1), const Vector2 &p_margin = Vector2()); 473 Size2 get_size_override() const; 474 475 bool is_size_override_enabled() const; 476 void set_size_override_stretch(bool p_enable); 477 bool is_size_override_stretch_enabled() const; 478 479 void set_vflip(bool p_enable); 480 bool get_vflip() const; 481 482 void set_clear_mode(ClearMode p_mode); 483 ClearMode get_clear_mode() const; 484 485 void set_update_mode(UpdateMode p_mode); 486 UpdateMode get_update_mode() const; 487 Ref<ViewportTexture> get_texture() const; 488 489 void set_shadow_atlas_size(int p_size); 490 int get_shadow_atlas_size() const; 491 492 void set_shadow_atlas_quadrant_subdiv(int p_quadrant, ShadowAtlasQuadrantSubdiv p_subdiv); 493 ShadowAtlasQuadrantSubdiv get_shadow_atlas_quadrant_subdiv(int p_quadrant) const; 494 495 void set_msaa(MSAA p_msaa); 496 MSAA get_msaa() const; 497 498 void set_hdr(bool p_hdr); 499 bool get_hdr() const; 500 501 Vector2 get_camera_coords(const Vector2 &p_viewport_coords) const; 502 Vector2 get_camera_rect_size() const; 503 504 void set_use_own_world(bool p_world); 505 bool is_using_own_world() const; 506 507 void input(const Ref<InputEvent> &p_event); 508 void unhandled_input(const Ref<InputEvent> &p_event); 509 510 void set_disable_input(bool p_disable); 511 bool is_input_disabled() const; 512 513 void set_disable_3d(bool p_disable); 514 bool is_3d_disabled() const; 515 516 void set_keep_3d_linear(bool p_keep_3d_linear); 517 bool get_keep_3d_linear() const; 518 519 void set_attach_to_screen_rect(const Rect2 &p_rect); 520 Rect2 get_attach_to_screen_rect() const; 521 522 void set_use_render_direct_to_screen(bool p_render_direct_to_screen); 523 bool is_using_render_direct_to_screen() const; 524 525 Vector2 get_mouse_position() const; 526 void warp_mouse(const Vector2 &p_pos); 527 528 void set_physics_object_picking(bool p_enable); 529 bool get_physics_object_picking(); 530 531 bool gui_has_modal_stack() const; 532 533 Variant gui_get_drag_data() const; 534 Control *get_modal_stack_top() const; 535 536 void gui_reset_canvas_sort_index(); 537 int gui_get_canvas_sort_index(); 538 539 virtual String get_configuration_warning() const; 540 541 void set_usage(Usage p_usage); 542 Usage get_usage() const; 543 544 void set_debug_draw(DebugDraw p_debug_draw); 545 DebugDraw get_debug_draw() const; 546 547 int get_render_info(RenderInfo p_info); 548 549 void set_snap_controls_to_pixels(bool p_enable); 550 bool is_snap_controls_to_pixels_enabled() const; 551 552 void _subwindow_visibility_changed(); 553 554 void set_input_as_handled(); 555 bool is_input_handled() const; 556 557 void set_handle_input_locally(bool p_enable); 558 bool is_handling_input_locally() const; 559 560 bool gui_is_dragging() const; 561 562 Viewport(); 563 ~Viewport(); 564 }; 565 566 VARIANT_ENUM_CAST(Viewport::UpdateMode); 567 VARIANT_ENUM_CAST(Viewport::ShadowAtlasQuadrantSubdiv); 568 VARIANT_ENUM_CAST(Viewport::MSAA); 569 VARIANT_ENUM_CAST(Viewport::Usage); 570 VARIANT_ENUM_CAST(Viewport::DebugDraw); 571 VARIANT_ENUM_CAST(Viewport::ClearMode); 572 VARIANT_ENUM_CAST(Viewport::RenderInfo); 573 574 #endif 575