Gamedev Framework (gf)  0.7.0
A C++14 framework for 2D games
View.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
383  class GF_API AdaptativeView : public View {
384  public:
391  : View()
392  {
393 
394  }
395 
401  explicit AdaptativeView(const RectF& rect)
402  : View(rect)
403  {
404 
405  }
406 
414  : View(center, size)
415  {
416 
417  }
418 
424  void setInitialScreenSize(Vector2u screenSize);
425 
431  virtual void onScreenSizeChange(Vector2u screenSize) = 0;
432 
433  };
434 
439  class GF_API ZoomingViewAdaptor {
440  public:
447  ZoomingViewAdaptor(const RenderTarget& target, View& view);
448 
454  void processEvent(const Event& event);
455 
456  private:
457  const RenderTarget& m_target;
458  View& m_view;
459  gf::Vector2i m_mousePosition;
460 
461  enum class State {
462  Stationary,
463  Moving,
464  };
465 
466  State m_state;
467  };
468 
469 #ifndef DOXYGEN_SHOULD_SKIP_THIS
470 }
471 #endif
472 }
473 
474 #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:439
AdaptativeView(const RectF &rect)
Construct the view from a rectangle.
Definition: View.h:401
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:390
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:383
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:413
void setCenter(Vector2f center)
Set the center of the view.
Definition: View.h:130