Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
View.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_VIEW_H
25 #define GF_VIEW_H
26 
27 #include "Matrix.h"
28 #include "Portability.h"
29 #include "Rect.h"
30 #include "Vector.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36  struct Event;
37  class RenderTarget;
38 
94  class GF_API View {
95  public:
101  View();
102 
108  explicit View(const RectF& rect);
109 
116  View(Vector2f center, Vector2f size);
117 
121  virtual ~View();
122 
130  void setCenter(Vector2f center) {
131  m_center = center;
132  }
133 
141  Vector2f getCenter() const {
142  return m_center;
143  }
144 
152  void setSize(Vector2f size) {
153  m_size = size;
154  onSizeChange(m_size);
155  }
156 
164  Vector2f getSize() const {
165  return m_size;
166  }
167 
177  void setRotation(float rotation) {
178  m_rotation = rotation;
179  }
180 
188  float getRotation() const {
189  return m_rotation;
190  }
191 
212  void setViewport(const RectF& viewport);
213 
221  const RectF& getViewport() const {
222  return m_viewport;
223  }
224 
234  void reset(const RectF& rect);
235 
243  void move(Vector2f offset);
244 
252  void rotate(float angle);
253 
268  void zoom(float factor);
269 
288  void zoom(float factor, Vector2f fixed);
289 
299  Matrix3f getTransform() const;
300 
310  Matrix3f getInverseTransform() const;
311 
312  protected:
322  m_size = size;
323  }
324 
332  virtual void onSizeChange(Vector2f size);
333 
342  void setViewportNoCallback(const RectF& viewport);
343 
349  virtual void onViewportChange(const RectF& viewport);
350 
351  private:
352  Vector2f m_center;
353  Vector2f m_size;
354  float m_rotation;
355  RectF m_viewport;
356  };
357 
358 
382  class GF_API AdaptativeView : public View {
383  public:
390  : View()
391  {
392 
393  }
394 
400  explicit AdaptativeView(const RectF& rect)
401  : View(rect)
402  {
403 
404  }
405 
413  : View(center, size)
414  {
415 
416  }
417 
423  void setInitialScreenSize(Vector2u screenSize);
424 
430  virtual void onScreenSizeChange(Vector2u screenSize) = 0;
431 
432  };
433 
438  class GF_API ZoomingViewAdaptor {
439  public:
446  ZoomingViewAdaptor(const RenderTarget& target, View& view);
447 
453  void processEvent(const Event& event);
454 
455  private:
456  const RenderTarget& m_target;
457  View& m_view;
458  gf::Vector2i m_mousePosition;
459 
460  enum class State {
461  Stationary,
462  Moving,
463  };
464 
465  State m_state;
466  };
467 
468 #ifndef DOXYGEN_SHOULD_SKIP_THIS
469 }
470 #endif
471 }
472 
473 #endif // GF_VIEW_H
2D camera that defines what region is shown on screen
Definition: View.h:94
A view adaptor for zooming/moving with the mouse.
Definition: View.h:438
AdaptativeView(const RectF &rect)
Construct the view from a rectangle.
Definition: View.h:400
float getRotation() const
Get the current orientation of the view.
Definition: View.h:188
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
const RectF & getViewport() const
Get the target viewport rectangle of the view.
Definition: View.h:221
The namespace for gf classes.
Definition: Action.h:34
Matrix3f rotation(float angle)
Get a rotation matrix.
Definition: Transform.h:391
float angle(Direction direction)
Get an angle from a direction.
void rotate(Matrix3f &mat, float angle)
Combine the current transform with a rotation.
void setSizeNoCallback(Vector2f size)
Set the world size, without calling onSizeChange()
Definition: View.h:321
Vector2f getCenter() const
Get the center of the view.
Definition: View.h:141
void setSize(Vector2f size)
Set the size of the view.
Definition: View.h:152
AdaptativeView()
Default constructor.
Definition: View.h:389
Vector2f getSize() const
Get the size of the view.
Definition: View.h:164
Defines a system event and its parameters.
Definition: Event.h:118
Adaptative view.
Definition: View.h:382
void setRotation(float rotation)
Set the orientation of the view.
Definition: View.h:177
AdaptativeView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: View.h:412
void setCenter(Vector2f center)
Set the center of the view.
Definition: View.h:130