Gamedev Framework (gf)  0.10.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 
128  RectF getBounds() const;
129 
137  void setCenter(Vector2f center) {
138  m_center = center;
139  }
140 
148  Vector2f getCenter() const {
149  return m_center;
150  }
151 
159  void setSize(Vector2f size) {
160  m_size = size;
161  onSizeChange(m_size);
162  }
163 
171  Vector2f getSize() const {
172  return m_size;
173  }
174 
184  void setRotation(float angle) {
185  m_rotation = angle;
186  }
187 
195  float getRotation() const {
196  return m_rotation;
197  }
198 
219  void setViewport(const RectF& viewport);
220 
228  const RectF& getViewport() const {
229  return m_viewport;
230  }
231 
241  void reset(const RectF& rect);
242 
250  void move(Vector2f offset);
251 
259  void rotate(float angle);
260 
275  void zoom(float factor);
276 
295  void zoom(float factor, Vector2f fixed);
296 
306  Matrix3f getTransform() const;
307 
317  Matrix3f getInverseTransform() const;
318 
319  protected:
329  m_size = size;
330  }
331 
339  virtual void onSizeChange(Vector2f size);
340 
349  void setViewportNoCallback(const RectF& viewport);
350 
356  virtual void onViewportChange(const RectF& viewport);
357 
358  private:
359  Vector2f m_center;
360  Vector2f m_size;
361  float m_rotation;
362  RectF m_viewport;
363  };
364 
365 
390  class GF_API AdaptativeView : public View {
391  public:
398  : View()
399  {
400 
401  }
402 
408  explicit AdaptativeView(const RectF& rect)
409  : View(rect)
410  {
411 
412  }
413 
421  : View(center, size)
422  {
423 
424  }
425 
431  void setInitialScreenSize(Vector2u screenSize);
432 
438  virtual void onScreenSizeChange(Vector2u screenSize) = 0;
439 
440  };
441 
446  class GF_API ZoomingViewAdaptor {
447  public:
454  ZoomingViewAdaptor(const RenderTarget& target, View& view);
455 
461  void processEvent(const Event& event);
462 
463  private:
464  const RenderTarget& m_target;
465  View& m_view;
466  Vector2i m_mousePosition;
467 
468  enum class State {
469  Stationary,
470  Moving,
471  };
472 
473  State m_state;
474  };
475 
476 #ifndef DOXYGEN_SHOULD_SKIP_THIS
477 }
478 #endif
479 }
480 
481 #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:446
AdaptativeView(const RectF &rect)
Construct the view from a rectangle.
Definition: View.h:408
float getRotation() const
Get the current orientation of the view.
Definition: View.h:195
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:228
The namespace for gf classes.
Definition: Action.h:34
float angle(Direction direction)
Get an angle from a direction.
void setRotation(float angle)
Set the orientation of the view.
Definition: View.h:184
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:328
Vector2f getCenter() const
Get the center of the view.
Definition: View.h:148
void setSize(Vector2f size)
Set the size of the view.
Definition: View.h:159
AdaptativeView()
Default constructor.
Definition: View.h:397
Vector2f getSize() const
Get the size of the view.
Definition: View.h:171
Defines a system event and its parameters.
Definition: Event.h:118
Adaptative view.
Definition: View.h:390
AdaptativeView(Vector2f center, Vector2f size)
Construct the view from its center and size.
Definition: View.h:420
void setCenter(Vector2f center)
Set the center of the view.
Definition: View.h:137