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