1 | // SuperTux |
2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
3 | // |
4 | // This program is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // This program is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | |
17 | #ifndef HEADER_SUPERTUX_OBJECT_CAMERA_HPP |
18 | #define |
19 | |
20 | #include <memory> |
21 | #include <string> |
22 | |
23 | #include "math/size.hpp" |
24 | #include "math/vector.hpp" |
25 | #include "object/path_object.hpp" |
26 | #include "scripting/camera.hpp" |
27 | #include "squirrel/exposed_object.hpp" |
28 | #include "supertux/game_object.hpp" |
29 | #include "supertux/timer.hpp" |
30 | |
31 | class Path; |
32 | class PathWalker; |
33 | class ReaderMapping; |
34 | class CameraConfig; |
35 | |
36 | class Camera final : public GameObject, |
37 | public ExposedObject<Camera, scripting::Camera>, |
38 | public PathObject |
39 | { |
40 | public: |
41 | enum class Mode |
42 | { |
43 | NORMAL, MANUAL, AUTOSCROLL, SCROLLTO |
44 | }; |
45 | |
46 | private: |
47 | /** The camera basically provides lookahead on the left or right |
48 | side or is undecided. */ |
49 | enum class LookaheadMode { |
50 | NONE, LEFT, RIGHT |
51 | }; |
52 | |
53 | public: |
54 | Camera(const std::string& name); |
55 | Camera(const ReaderMapping& reader); |
56 | virtual ~Camera(); |
57 | |
58 | /** \addtogroup GameObject |
59 | @{ */ |
60 | virtual void update(float dt_sec) override; |
61 | virtual void draw(DrawingContext& ) override; |
62 | |
63 | virtual bool is_singleton() const override { return true; } |
64 | virtual bool is_saveable() const override; |
65 | |
66 | virtual std::string get_class() const override { return "camera" ; } |
67 | virtual std::string get_display_name() const override { return _("Camera" ); } |
68 | |
69 | virtual ObjectSettings get_settings() override; |
70 | virtual void after_editor_set() override; |
71 | |
72 | virtual const std::string get_icon_path() const override { return "images/engine/editor/camera.png" ; } |
73 | /** @} */ |
74 | |
75 | /** \addtogroup CameraAPI |
76 | @{ */ |
77 | |
78 | /** reset camera position */ |
79 | void reset(const Vector& tuxpos); |
80 | |
81 | /** return camera position */ |
82 | const Vector& get_translation() const; |
83 | void set_translation(const Vector& translation) { m_translation = translation; } |
84 | |
85 | /** shake camera in a direction 1 time */ |
86 | void shake(float duration, float x, float y); |
87 | |
88 | /** scroll the upper left edge of the camera in scrolltime seconds |
89 | to the position goal */ |
90 | void scroll_to(const Vector& goal, float scrolltime); |
91 | void move(const int dx, const int dy); |
92 | |
93 | void reload_config(); |
94 | |
95 | /** get the coordinates of the point directly in the center of this |
96 | camera */ |
97 | Vector get_center() const; |
98 | |
99 | void set_mode(Mode mode_) { m_mode = mode_; } |
100 | /** @} */ |
101 | |
102 | private: |
103 | void update_scroll_normal(float dt_sec); |
104 | void update_scroll_autoscroll(float dt_sec); |
105 | void update_scroll_to(float dt_sec); |
106 | void keep_in_bounds(Vector& vector); |
107 | void shake(); |
108 | |
109 | private: |
110 | Mode m_mode; |
111 | Mode m_defaultmode; |
112 | |
113 | Size m_screen_size; |
114 | |
115 | Vector m_translation; |
116 | |
117 | // normal mode |
118 | LookaheadMode m_lookahead_mode; |
119 | float m_changetime; |
120 | Vector m_lookahead_pos; |
121 | Vector m_peek_pos; |
122 | Vector m_cached_translation; |
123 | |
124 | // shaking |
125 | Timer m_shaketimer; |
126 | float m_shakespeed; |
127 | float m_shakedepth_x; |
128 | float m_shakedepth_y; |
129 | |
130 | // scrollto mode |
131 | Vector m_scroll_from; |
132 | Vector m_scroll_goal; |
133 | float m_scroll_to_pos; |
134 | float m_scrollspeed; |
135 | |
136 | std::unique_ptr<CameraConfig> m_config; |
137 | |
138 | private: |
139 | Camera(const Camera&) = delete; |
140 | Camera& operator=(const Camera&) = delete; |
141 | }; |
142 | |
143 | #endif |
144 | |
145 | /* EOF */ |
146 | |