Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
WindowGeometryTracker.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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 #ifndef GF_WINDOW_GEOMETRY_TRACKER_H
22 #define GF_WINDOW_GEOMETRY_TRACKER_H
23 
24 #include "Portability.h"
25 #include "Vector.h"
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31  struct Event;
32 
33  /**
34  * @ingroup window
35  * @brief A window geometry tracker
36  *
37  * The window geometry tracker computes position relative to the borders of
38  * the screen. It tracks the changes of the window geometry (especially the
39  * size of the window).
40  *
41  * This class is very useful for computing HUD elements' position.
42  */
44  public:
45  /**
46  * @brief Default constructor
47  */
49 
50  /**
51  * @brief Compute `x` for a centered element
52  *
53  * Compute the x coordinate of an element of size `width` that must be
54  * centered on the window.
55  *
56  * @param width The width of the element
57  * @return The computed `x` coordinate
58  * @sa getXRatio()
59  */
60  float getXCentered(float width) const;
61 
62  /**
63  * @brief Compute `x` for a right aligned element
64  *
65  * Compute the x coordinate of an element of size `width` that must be
66  * right aligned on the window.
67  *
68  * @param width The width of the element
69  * @return The computed `x` coordinate
70  */
71  float getXFromRight(float width) const;
72 
73  /**
74  * @brief Compute `x` for an element at some percent from the left
75  *
76  * Compute the x coordinate of an element of size `width` that must be
77  * at a given percentage from the left. @f$ 0 @f$ puts the element on the
78  * left, @f$ 0.5 @f$ puts the element in the center (see getXCentered()),
79  * @f$ 1 @f$ puts the element on the right (see getXFromRight()).
80  *
81  * @param ratio The percentage, in the interval @f$ [0,1] @f$
82  * @param width The width of the element
83  * @return The computed `x` coordinate
84  */
85  float getXRatio(float ratio, float width) const;
86 
87 
88  /**
89  * @brief Compute `y` for a centered element
90  *
91  * Compute the y coordinate of an element of size `height` that must be
92  * centered on the window.
93  *
94  * @param height The height of the element
95  * @sa getYRatio()
96  * @return The computed `y` coordinate
97  */
98  float getYCentered(float height) const;
99 
100  /**
101  * @brief Compute `y` for a bottom aligned element
102  *
103  * Compute the x coordinate of an element of size `height` that must be
104  * bottom aligned on the window.
105  *
106  * @param height The height of the element
107  * @return The computed `y` coordinate
108  */
109  float getYFromBottom(float height) const;
110 
111  /**
112  * @brief Compute `y` for an element at some percent from the top
113  *
114  * Compute the y coordinate of an element of size `height` that must be
115  * at a given percentage from the top. @f$ 0 @f$ puts the element on the
116  * top, @f$ 0.5 @f$ puts the element in the center (see getYCentered()),
117  * @f$ 1 @f$ puts the element on the bottom (see getYFromBottom()).
118  *
119  * @param ratio The percentage, in the interval @f$ [0,1] @f$
120  * @param height The height of the element
121  * @return The computed `y` coordinate
122  */
123  float getYRatio(float ratio, float height) const;
124 
125  /**
126  * @brief Get a position relative to a corner
127  *
128  * If the position has a negative coordinate, it is considered on the
129  * other side than a positive coordinate.
130  *
131  * | `pos.x` | `pos.y` | Final position |
132  * |-------------|-------------|---------------------|
133  * | @f$ > 0 @f$ | @f$ > 0 @f$ | top-left corner |
134  * | @f$ < 0 @f$ | @f$ > 0 @f$ | top-right corner |
135  * | @f$ > 0 @f$ | @f$ < 0 @f$ | bottom-left corner |
136  * | @f$ < 0 @f$ | @f$ < 0 @f$ | bottom-right corner |
137  *
138  * @param pos The position
139  * @return The computed position
140  */
141  Vector2f getCornerPosition(const Vector2f& pos) const;
142 
143  /**
144  * @brief Update the geometry thanks to the event
145  *
146  * Internally it calls onScreenResize() if a resize event occurs.
147  *
148  * @param event An event
149  */
150  void processEvent(const Event& event);
151 
152  /**
153  * @brief Update the geometry with the new screen size
154  *
155  * @param screenSize The new size of the screen
156  */
157  void onScreenResize(Vector2u screenSize);
158 
159  /**
160  * @brief Set the initial screen size
161  *
162  * @param screenSize The initial size of the screen
163  */
164  void setInitialScreenSize(Vector2u screenSize);
165 
166  private:
167  Vector2u m_screenSize;
168  };
169 
170 #ifndef DOXYGEN_SHOULD_SKIP_THIS
171 }
172 #endif
173 }
174 
175 #endif // GL_WINDOW_GEOMETRY_TRACKER_H
float getXFromRight(float width) const
Compute x for a right aligned element.
void setInitialScreenSize(Vector2u screenSize)
Set the initial screen size.
float getYFromBottom(float height) const
Compute y for a bottom aligned element.
void onScreenResize(Vector2u screenSize)
Update the geometry with the new screen size.
WindowGeometryTracker()
Default constructor.
Vector2f getCornerPosition(const Vector2f &pos) const
Get a position relative to a corner.
Definition: Action.h:34
void processEvent(const Event &event)
Update the geometry thanks to the event.
float getXRatio(float ratio, float width) const
Compute x for an element at some percent from the left.
float getYRatio(float ratio, float height) const
Compute y for an element at some percent from the top.
float getYCentered(float height) const
Compute y for a centered element.
Defines a system event and its parameters.
Definition: Event.h:115
#define GF_API
Definition: Portability.h:35
float getXCentered(float width) const
Compute x for a centered element.
A window geometry tracker.
Definition: WindowGeometryTracker.h:43