camera_2d.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*************************************************************************/
  2. /* camera_2d.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 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. protected:
  42. Point2 camera_pos;
  43. Point2 smoothed_camera_pos;
  44. bool first;
  45. ObjectID custom_viewport_id; // to check validity
  46. Viewport *custom_viewport;
  47. Viewport *viewport;
  48. StringName group_name;
  49. StringName canvas_group_name;
  50. RID canvas;
  51. Vector2 offset;
  52. Vector2 zoom;
  53. AnchorMode anchor_mode;
  54. bool rotating;
  55. bool current;
  56. float smoothing;
  57. bool smoothing_enabled;
  58. int limit[4];
  59. bool limit_smoothing_enabled;
  60. float drag_margin[4];
  61. bool h_drag_enabled;
  62. bool v_drag_enabled;
  63. float h_ofs;
  64. float v_ofs;
  65. Point2 camera_screen_center;
  66. void _update_scroll();
  67. void _make_current(Object *p_which);
  68. void _set_current(bool p_current);
  69. void _set_old_smoothing(float p_enable);
  70. bool screen_drawing_enabled;
  71. bool limit_drawing_enabled;
  72. bool margin_drawing_enabled;
  73. protected:
  74. virtual Transform2D get_camera_transform();
  75. void _notification(int p_what);
  76. static void _bind_methods();
  77. public:
  78. void set_offset(const Vector2 &p_offset);
  79. Vector2 get_offset() const;
  80. void set_anchor_mode(AnchorMode p_anchor_mode);
  81. AnchorMode get_anchor_mode() const;
  82. void set_rotating(bool p_rotating);
  83. bool is_rotating() const;
  84. void set_limit(Margin p_margin, int p_limit);
  85. int get_limit(Margin p_margin) const;
  86. void set_limit_smoothing_enabled(bool enable);
  87. bool is_limit_smoothing_enabled() const;
  88. void set_h_drag_enabled(bool p_enabled);
  89. bool is_h_drag_enabled() const;
  90. void set_v_drag_enabled(bool p_enabled);
  91. bool is_v_drag_enabled() const;
  92. void set_drag_margin(Margin p_margin, float p_drag_margin);
  93. float get_drag_margin(Margin p_margin) const;
  94. void set_v_offset(float p_offset);
  95. float get_v_offset() const;
  96. void set_h_offset(float p_offset);
  97. float get_h_offset() const;
  98. void set_enable_follow_smoothing(bool p_enabled);
  99. bool is_follow_smoothing_enabled() const;
  100. void set_follow_smoothing(float p_speed);
  101. float get_follow_smoothing() const;
  102. void make_current();
  103. void clear_current();
  104. bool is_current() const;
  105. void set_zoom(const Vector2 &p_zoom);
  106. Vector2 get_zoom() const;
  107. Point2 get_camera_screen_center() const;
  108. void set_custom_viewport(Node *p_viewport);
  109. Node *get_custom_viewport() const;
  110. Vector2 get_camera_position() const;
  111. void force_update_scroll();
  112. void reset_smoothing();
  113. void align();
  114. void set_screen_drawing_enabled(bool enable);
  115. bool is_screen_drawing_enabled() const;
  116. void set_limit_drawing_enabled(bool enable);
  117. bool is_limit_drawing_enabled() const;
  118. void set_margin_drawing_enabled(bool enable);
  119. bool is_margin_drawing_enabled() const;
  120. Camera2D();
  121. };
  122. VARIANT_ENUM_CAST(Camera2D::AnchorMode);
  123. #endif // CAMERA_2D_H