camera.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2023 Vankata453
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef HEADER_SUPERTUX_OBJECT_CAMERA_HPP
  18. #define HEADER_SUPERTUX_OBJECT_CAMERA_HPP
  19. #include <string>
  20. #include "math/anchor_point.hpp"
  21. #include "math/size.hpp"
  22. #include "math/vector.hpp"
  23. #include "object/path_object.hpp"
  24. #include "scripting/camera.hpp"
  25. #include "squirrel/exposed_object.hpp"
  26. #include "supertux/game_object.hpp"
  27. #include "supertux/timer.hpp"
  28. class Path;
  29. class PathWalker;
  30. class ReaderMapping;
  31. class Camera final : public GameObject,
  32. public ExposedObject<Camera, scripting::Camera>,
  33. public PathObject
  34. {
  35. public:
  36. enum class Mode
  37. {
  38. NORMAL, MANUAL, AUTOSCROLL, SCROLLTO
  39. };
  40. public:
  41. Camera(const std::string& name);
  42. Camera(const ReaderMapping& reader);
  43. ~Camera() override;
  44. /** \addtogroup GameObject
  45. @{ */
  46. virtual void update(float dt_sec) override;
  47. virtual void draw(DrawingContext& ) override;
  48. virtual bool is_singleton() const override { return true; }
  49. virtual bool is_saveable() const override;
  50. static std::string class_name() { return "camera"; }
  51. virtual std::string get_class_name() const override { return class_name(); }
  52. static std::string display_name() { return _("Camera"); }
  53. virtual std::string get_display_name() const override { return display_name(); }
  54. virtual ObjectSettings get_settings() override;
  55. virtual void after_editor_set() override;
  56. void save_state() override;
  57. void check_state() override;
  58. virtual const std::string get_icon_path() const override { return "images/engine/editor/camera.png"; }
  59. /** @} */
  60. Rectf get_rect() const;
  61. /** \addtogroup CameraAPI
  62. @{ */
  63. /** reset camera position */
  64. void reset(const Vector& tuxpos);
  65. /** return camera position */
  66. const Vector get_translation() const;
  67. void set_translation(const Vector& translation) { m_translation = translation; }
  68. void set_translation_centered(const Vector& translation);
  69. void keep_in_bounds(const Rectf& bounds);
  70. /** shake camera in a direction 1 time */
  71. void shake(float duration, float x, float y);
  72. /** Shake the camera vertically with a specified average strength, at a certain minimal delay, until stopped. */
  73. void start_earthquake(float strength, float delay);
  74. void stop_earthquake();
  75. /** scroll the upper left edge of the camera in scrolltime seconds
  76. to the position goal */
  77. void scroll_to(const Vector& goal, float scrolltime);
  78. void move(const Vector& offset);
  79. /** get the coordinates of the point directly in the center of this
  80. camera */
  81. Vector get_center() const;
  82. /** get the coordinates of the point directly in the top left of this
  83. camera */
  84. const Vector& get_position() const;
  85. /** get the width and height of the screen*/
  86. const Sizef& get_screen_size() const;
  87. void set_mode(Mode mode_) { m_mode = mode_; }
  88. Mode get_mode() const { return m_mode; }
  89. /** get the exact scale at this exact moment */
  90. float get_current_scale() const { return m_enfore_minimum_scale ? std::min(m_minimum_scale, m_scale) : m_scale; }
  91. /** get the scale towards which the camera is moving */
  92. float get_target_scale() const { return m_scale_target; }
  93. /** Instantly set the scale of the camera */
  94. void set_scale(float scale) { m_scale = scale; }
  95. /** smoothly slide the scale and anchor position of the camera towards a new value */
  96. void ease_scale(float scale, float time, easing ease, AnchorPoint anchor = AnchorPoint::ANCHOR_MIDDLE);
  97. /** @} */
  98. private:
  99. void keep_in_bounds(Vector& vector);
  100. void update_scroll_normal(float dt_sec);
  101. void update_scroll_normal_multiplayer(float dt_sec);
  102. void update_scroll_autoscroll(float dt_sec);
  103. void update_scroll_to(float dt_sec);
  104. void update_scale(float dt_sec);
  105. void update_shake();
  106. void update_earthquake();
  107. Vector get_scale_anchor_target() const;
  108. void reload_scale();
  109. private:
  110. Mode m_mode;
  111. Mode m_defaultmode;
  112. Sizef m_screen_size;
  113. Vector m_translation;
  114. // normal mode
  115. Vector m_lookahead_pos;
  116. Vector m_peek_pos;
  117. Vector m_cached_translation;
  118. // shaking
  119. Timer m_shaketimer;
  120. float m_shakespeed;
  121. float m_shakedepth_x;
  122. float m_shakedepth_y;
  123. // Earthquake
  124. bool m_earthquake;
  125. float m_earthquake_strength,
  126. m_earthquake_delay,
  127. m_earthquake_last_offset;
  128. Timer m_earthquake_delay_timer;
  129. // scrollto mode
  130. Vector m_scroll_from;
  131. Vector m_scroll_goal;
  132. float m_scroll_to_pos;
  133. float m_scrollspeed;
  134. float m_scale,
  135. m_scale_origin,
  136. m_scale_target,
  137. m_scale_time_total,
  138. m_scale_time_remaining;
  139. Vector m_scale_origin_translation,
  140. m_scale_target_translation;
  141. easing m_scale_easing;
  142. AnchorPoint m_scale_anchor;
  143. // Minimum scale is used in certain circumstances where a fixed minimum scale
  144. // should be used, regardless of the scriping-accessible `m_scale` property.
  145. float m_minimum_scale;
  146. bool m_enfore_minimum_scale;
  147. private:
  148. Camera(const Camera&) = delete;
  149. Camera& operator=(const Camera&) = delete;
  150. };
  151. #endif
  152. /* EOF */