camera_2d.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "scene/main/viewport.h"
  34. class Camera2D : public Node2D {
  35. GDCLASS(Camera2D, Node2D);
  36. public:
  37. enum AnchorMode {
  38. ANCHOR_MODE_FIXED_TOP_LEFT,
  39. ANCHOR_MODE_DRAG_CENTER
  40. };
  41. enum Camera2DProcessMode {
  42. CAMERA2D_PROCESS_PHYSICS,
  43. CAMERA2D_PROCESS_IDLE
  44. };
  45. protected:
  46. Point2 camera_pos;
  47. Point2 smoothed_camera_pos;
  48. bool first;
  49. ObjectID custom_viewport_id; // to check validity
  50. Viewport *custom_viewport;
  51. Viewport *viewport;
  52. StringName group_name;
  53. StringName canvas_group_name;
  54. RID canvas;
  55. Vector2 offset;
  56. Vector2 zoom;
  57. AnchorMode anchor_mode;
  58. bool rotating;
  59. bool current;
  60. float smoothing;
  61. bool smoothing_enabled;
  62. bool smoothing_active; // smoothing can be enabled but not active in the editor
  63. int limit[4];
  64. bool limit_smoothing_enabled;
  65. float drag_margin[4];
  66. bool h_drag_enabled;
  67. bool v_drag_enabled;
  68. float h_ofs;
  69. float v_ofs;
  70. bool h_offset_changed;
  71. bool v_offset_changed;
  72. Point2 camera_screen_center;
  73. void _update_process_mode();
  74. void _update_scroll();
  75. void _setup_viewport();
  76. void _make_current(Object *p_which);
  77. void _set_current(bool p_current);
  78. bool screen_drawing_enabled;
  79. bool limit_drawing_enabled;
  80. bool margin_drawing_enabled;
  81. Camera2DProcessMode process_mode;
  82. protected:
  83. virtual Transform2D get_camera_transform();
  84. void _notification(int p_what);
  85. static void _bind_methods();
  86. public:
  87. void set_offset(const Vector2 &p_offset);
  88. Vector2 get_offset() const;
  89. void set_anchor_mode(AnchorMode p_anchor_mode);
  90. AnchorMode get_anchor_mode() const;
  91. void set_rotating(bool p_rotating);
  92. bool is_rotating() const;
  93. void set_limit(Margin p_margin, int p_limit);
  94. int get_limit(Margin p_margin) const;
  95. void set_limit_smoothing_enabled(bool enable);
  96. bool is_limit_smoothing_enabled() const;
  97. void set_h_drag_enabled(bool p_enabled);
  98. bool is_h_drag_enabled() const;
  99. void set_v_drag_enabled(bool p_enabled);
  100. bool is_v_drag_enabled() const;
  101. void set_drag_margin(Margin p_margin, float p_drag_margin);
  102. float get_drag_margin(Margin p_margin) const;
  103. void set_v_offset(float p_offset);
  104. float get_v_offset() const;
  105. void set_h_offset(float p_offset);
  106. float get_h_offset() const;
  107. void set_enable_follow_smoothing(bool p_enabled);
  108. bool is_follow_smoothing_enabled() const;
  109. void set_follow_smoothing(float p_speed);
  110. float get_follow_smoothing() const;
  111. void set_process_mode(Camera2DProcessMode p_mode);
  112. Camera2DProcessMode get_process_mode() const;
  113. void make_current();
  114. void clear_current();
  115. bool is_current() const;
  116. void set_zoom(const Vector2 &p_zoom);
  117. Vector2 get_zoom() const;
  118. Point2 get_camera_screen_center() const;
  119. void set_custom_viewport(Node *p_viewport);
  120. Node *get_custom_viewport() const;
  121. Vector2 get_camera_position() const;
  122. void force_update_scroll();
  123. void reset_smoothing();
  124. void align();
  125. void set_screen_drawing_enabled(bool enable);
  126. bool is_screen_drawing_enabled() const;
  127. void set_limit_drawing_enabled(bool enable);
  128. bool is_limit_drawing_enabled() const;
  129. void set_margin_drawing_enabled(bool enable);
  130. bool is_margin_drawing_enabled() const;
  131. Camera2D();
  132. };
  133. VARIANT_ENUM_CAST(Camera2D::AnchorMode);
  134. VARIANT_ENUM_CAST(Camera2D::Camera2DProcessMode);
  135. #endif // CAMERA_2D_H