camera_2d.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* camera_2d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 CAMERA_2D_H
  31. #define CAMERA_2D_H
  32. #include "scene/2d/node_2d.h"
  33. class Camera2D : public Node2D {
  34. GDCLASS(Camera2D, Node2D);
  35. public:
  36. enum AnchorMode {
  37. ANCHOR_MODE_FIXED_TOP_LEFT,
  38. ANCHOR_MODE_DRAG_CENTER
  39. };
  40. enum Camera2DProcessCallback {
  41. CAMERA2D_PROCESS_PHYSICS,
  42. CAMERA2D_PROCESS_IDLE
  43. };
  44. protected:
  45. Point2 camera_pos;
  46. Point2 smoothed_camera_pos;
  47. bool first = true;
  48. bool just_exited_tree = false;
  49. ObjectID custom_viewport_id; // to check validity
  50. Viewport *custom_viewport = nullptr;
  51. Viewport *viewport = nullptr;
  52. StringName group_name;
  53. StringName canvas_group_name;
  54. RID canvas;
  55. Vector2 offset;
  56. Vector2 zoom = Vector2(1, 1);
  57. Vector2 zoom_scale = Vector2(1, 1);
  58. AnchorMode anchor_mode = ANCHOR_MODE_DRAG_CENTER;
  59. bool ignore_rotation = true;
  60. bool enabled = true;
  61. real_t position_smoothing_speed = 5.0;
  62. bool position_smoothing_enabled = false;
  63. real_t camera_angle = 0.0;
  64. real_t rotation_smoothing_speed = 5.0;
  65. bool rotation_smoothing_enabled = false;
  66. int limit[4];
  67. bool limit_smoothing_enabled = false;
  68. real_t drag_margin[4];
  69. bool drag_horizontal_enabled = false;
  70. bool drag_vertical_enabled = false;
  71. real_t drag_horizontal_offset = 0.0;
  72. real_t drag_vertical_offset = 0.0;
  73. bool drag_horizontal_offset_changed = false;
  74. bool drag_vertical_offset_changed = false;
  75. Point2 camera_screen_center;
  76. bool _is_editing_in_editor() const;
  77. void _update_process_callback();
  78. void _update_scroll();
  79. #ifdef TOOLS_ENABLED
  80. void _project_settings_changed();
  81. #endif
  82. void _make_current(Object *p_which);
  83. void _reset_just_exited() { just_exited_tree = false; }
  84. void _set_old_smoothing(real_t p_enable);
  85. void _update_process_internal_for_smoothing();
  86. bool screen_drawing_enabled = true;
  87. bool limit_drawing_enabled = false;
  88. bool margin_drawing_enabled = false;
  89. Camera2DProcessCallback process_callback = CAMERA2D_PROCESS_IDLE;
  90. struct InterpolationData {
  91. Transform2D xform_curr;
  92. Transform2D xform_prev;
  93. uint32_t last_update_physics_tick = UINT32_MAX; // Ensure tick 0 is detected as a change.
  94. } _interpolation_data;
  95. void _ensure_update_interpolation_data();
  96. Size2 _get_camera_screen_size() const;
  97. protected:
  98. virtual Transform2D get_camera_transform();
  99. void _notification(int p_what);
  100. static void _bind_methods();
  101. void _validate_property(PropertyInfo &p_property) const;
  102. public:
  103. void set_offset(const Vector2 &p_offset);
  104. Vector2 get_offset() const;
  105. void set_anchor_mode(AnchorMode p_anchor_mode);
  106. AnchorMode get_anchor_mode() const;
  107. void set_ignore_rotation(bool p_ignore);
  108. bool is_ignoring_rotation() const;
  109. void set_limit(Side p_side, int p_limit);
  110. int get_limit(Side p_side) const;
  111. void set_limit_smoothing_enabled(bool enable);
  112. bool is_limit_smoothing_enabled() const;
  113. void set_drag_horizontal_enabled(bool p_enabled);
  114. bool is_drag_horizontal_enabled() const;
  115. void set_drag_vertical_enabled(bool p_enabled);
  116. bool is_drag_vertical_enabled() const;
  117. void set_drag_margin(Side p_side, real_t p_drag_margin);
  118. real_t get_drag_margin(Side p_side) const;
  119. void set_drag_horizontal_offset(real_t p_offset);
  120. real_t get_drag_horizontal_offset() const;
  121. void set_drag_vertical_offset(real_t p_offset);
  122. real_t get_drag_vertical_offset() const;
  123. void set_position_smoothing_enabled(bool p_enabled);
  124. bool is_position_smoothing_enabled() const;
  125. void set_position_smoothing_speed(real_t p_speed);
  126. real_t get_position_smoothing_speed() const;
  127. void set_rotation_smoothing_speed(real_t p_speed);
  128. real_t get_rotation_smoothing_speed() const;
  129. void set_rotation_smoothing_enabled(bool p_enabled);
  130. bool is_rotation_smoothing_enabled() const;
  131. void set_process_callback(Camera2DProcessCallback p_mode);
  132. Camera2DProcessCallback get_process_callback() const;
  133. void set_enabled(bool p_enabled);
  134. bool is_enabled() const;
  135. void make_current();
  136. void clear_current();
  137. bool is_current() const;
  138. void set_zoom(const Vector2 &p_zoom);
  139. Vector2 get_zoom() const;
  140. Point2 get_camera_screen_center() const;
  141. void set_custom_viewport(Node *p_viewport);
  142. Node *get_custom_viewport() const;
  143. Vector2 get_camera_position() const;
  144. void force_update_scroll();
  145. void reset_smoothing();
  146. void align();
  147. void set_screen_drawing_enabled(bool enable);
  148. bool is_screen_drawing_enabled() const;
  149. void set_limit_drawing_enabled(bool enable);
  150. bool is_limit_drawing_enabled() const;
  151. void set_margin_drawing_enabled(bool enable);
  152. bool is_margin_drawing_enabled() const;
  153. Camera2D();
  154. };
  155. VARIANT_ENUM_CAST(Camera2D::AnchorMode);
  156. VARIANT_ENUM_CAST(Camera2D::Camera2DProcessCallback);
  157. #endif // CAMERA_2D_H