supertux
camera.hpp
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 HEADER_SUPERTUX_OBJECT_CAMERA_HPP
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:
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 
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"; }
79  void reset(const Vector& tuxpos);
80 
82  const Vector& get_translation() const;
83  void set_translation(const Vector& translation) { m_translation = translation; }
84 
86  void shake(float duration, float x, float y);
87 
90  void scroll_to(const Vector& goal, float scrolltime);
91  void move(const int dx, const int dy);
92 
93  void reload_config();
94 
97  Vector get_center() const;
98 
99  void set_mode(Mode mode_) { m_mode = mode_; }
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 */
A walker that travels along a path.
Definition: path_walker.hpp:29
virtual bool is_singleton() const override
If true only a single object of this type is allowed in a given GameObjectManager.
Definition: camera.hpp:63
Simple two dimensional vector.
Definition: vector.hpp:24
virtual bool is_saveable() const override
Indicates if the object will be saved.
Definition: camera.cpp:686
Vector get_center() const
get the coordinates of the point directly in the center of this camera
Definition: camera.cpp:672
This class binds a certain GameObject class to a scripting class.
Definition: exposed_object.hpp:45
Definition: object_settings.hpp:35
Definition: size.hpp:24
Definition: path.hpp:44
Base class for all the things that make up Levels&#39; Sectors.
Definition: game_object.hpp:46
Definition: camera.cpp:38
virtual void draw(DrawingContext &) override
The GameObject should draw itself onto the provided DrawingContext if this function is called...
Definition: camera.cpp:278
virtual void update(float dt_sec) override
This function is called once per frame and allows the object to update it&#39;s state.
Definition: camera.cpp:285
const Vector & get_translation() const
return camera position
Definition: camera.cpp:236
A class for all objects that contain / make use of a path.
Definition: path_object.hpp:28
void scroll_to(const Vector &goal, float scrolltime)
scroll the upper left edge of the camera in scrolltime seconds to the position goal ...
Definition: camera.cpp:264
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
Definition: camera.hpp:36
Definition: reader_mapping.hpp:31
void reset(const Vector &tuxpos)
reset camera position
Definition: camera.cpp:242
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42