Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
NinePatch.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 #ifndef GF_NINE_PATCH_H
22 #define GF_NINE_PATCH_H
23 
24 #include "Portability.h"
25 #include "Transformable.h"
26 #include "Vertex.h"
27 #include "VertexBuffer.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  class Texture;
35 
36  /**
37  * @ingroup graphics
38  * @brief A nine-patch
39  *
40  * A nine-patch is a stretchable image that can be automatically resized. You
41  * just have to indicate the position of the stretchable area and that's it.
42  *
43  * @sa [Android Developers: Draw 9-patch](http://developer.android.com/tools/help/draw9patch.html)
44  * @sa [Android Developers: Nine-patch](http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch)
45  */
46  class GF_API NinePatch : public Transformable {
47  public:
48  /**
49  * @brief Default constructor
50  *
51  * Creates an empty nine-patch with no source texture
52  */
53  NinePatch();
54 
55  /**
56  * @brief Construct the nine-patch from a source texture
57  *
58  * @param texture Source texture
59  *
60  * @sa setTexture()
61  */
62  NinePatch(const Texture& texture);
63 
64  /**
65  * @brief Construct the nine-patch from a sub-rectangle of a source texture
66  *
67  * @param texture Source texture
68  * @param textureRect Sub-rectangle of the texture to assign to the nine-patch
69  *
70  * @sa setTexture(), setTextureRect()
71  */
72  NinePatch(const Texture& texture, const RectF& textureRect);
73 
74  /**
75  * @brief Change the source texture of the nine-patch
76  *
77  * The texture must exist as long as the nine-patch uses it. Indeed, the
78  * nine-patch doesn't store its own copy of the texture, but rather keeps
79  * a pointer to the one that you passed to this function.
80  * If the source texture is destroyed and the nine-patch tries to
81  * use it, the behavior is undefined.
82  *
83  * If `resetRect` is true, the texture rect property of
84  * the nine-patch is automatically adjusted to the size of the new
85  * texture. If it is false, the texture rect is left unchanged.
86  *
87  * @param texture New texture
88  * @param resetRect Should the texture rect be reset to the size of the new texture?
89  *
90  * @sa getTexture(), setTextureRect()
91  */
92  void setTexture(const Texture& texture, bool resetRect = false);
93 
94  /**
95  * @brief Get the source texture of the nine-patch
96  *
97  * The returned reference is const, which means that you can't
98  * modify the texture when you retrieve it with this function.
99  *
100  * @return Reference to the nine-patch's texture
101  *
102  * @sa setTexture()
103  */
104  const Texture& getTexture() const {
105  return *m_texture;
106  }
107 
108  /**
109  * @brief Check if a texture is set
110  *
111  * @returns True if a texture is already set
112  *
113  * @sa setTexture(), getTexture()
114  */
115  bool hasTexture() const {
116  return m_texture != nullptr;
117  }
118 
119  /**
120  * @brief Unset the source texture of the nine-patch
121  *
122  * After a call to this function, the nine-patch has no source texture.
123  *
124  * @sa setTexture()
125  */
126  void unsetTexture();
127 
128  /**
129  * @brief Set the sub-rectangle of the texture that the nine-patch will display
130  *
131  * The texture rect is useful when you don't want to display
132  * the whole texture, but rather a part of it.
133  * By default, the texture rect covers the entire texture.
134  *
135  * The rectangle is given in texture coordinates, meaning that
136  * @f$(0,0)@f$ is the top left corner and @f$(1,1)@f$ is the
137  * bottom right corner.
138  *
139  * @param rect Rectangle defining the region of the texture to display
140  *
141  * @sa getTextureRect(), setTexture()
142  */
143  void setTextureRect(const RectF& rect);
144 
145  /**
146  * @brief Get the sub-rectangle of the texture displayed by the nine-patch
147  *
148  * @return Texture rectangle of the nine-patch in texture coordinates
149  *
150  * @sa setTextureRect()
151  */
152  const RectF& getTextureRect() const {
153  return m_textureRect;
154  }
155 
156  /**
157  * @brief Set the global color of the nine-patch
158  *
159  * This color is modulated (multiplied) with the nine-patch's
160  * texture. It can be used to colorize the nine-patch, or change
161  * its global opacity.
162  *
163  * By default, the nine-patch's color is opaque white.
164  *
165  * @param color New color of the nine-patch
166  *
167  * @sa getColor()
168  */
169  void setColor(const Color4f& color);
170 
171  /**
172  * @brief Get the global color of the nine-patch
173  *
174  * @return Global color of the nine-patch
175  *
176  * @sa setColor()
177  */
178  const Color4f& getColor() const;
179 
180  /**
181  * @brief Set the limits of the stretchable area
182  *
183  * The limits are given in normalized coordinates
184  *
185  * @param top The top limit
186  * @param bottom The bottom limit
187  * @param left The left limit
188  * @param right The right limit
189  */
190  void setLimits(float top, float bottom, float left, float right);
191 
192  /**
193  * @brief Set the vertical limits of the stretchable area
194  *
195  * The limits are given in normalized coordinates
196  *
197  * @param top The top limit
198  * @param bottom The bottom limit
199  */
200  void setVerticalLimits(float top, float bottom);
201 
202  /**
203  * @brief Set the horizontal limits of the stretchable area
204  *
205  * The limits are given in normalized coordinates
206  *
207  * @param left The left limit
208  * @param right The right limit
209  */
210  void setHorizontalLimits(float left, float right);
211 
212  /**
213  * @brief Set the size of the stretched area
214  *
215  * @param size The size of the stretched area
216  */
217  void setSize(Vector2f size);
218 
219  /**
220  * @brief Get the size of the stretched area
221  *
222  * @return The size of the stretched area
223  */
224  Vector2f getSize() const {
225  return m_size;
226  }
227 
228  /**
229  * @brief Get the local bounding rectangle of the entity
230  *
231  * The returned rectangle is in local coordinates, which means
232  * that it ignores the transformations (translation, rotation,
233  * scale, ...) that are applied to the entity.
234  * In other words, this function returns the bounds of the
235  * entity in the entity's coordinate system.
236  *
237  * @return Local bounding rectangle of the entity
238  */
239  RectF getLocalBounds() const;
240 
241  /**
242  * @brief Set the anchor origin of the entity
243  *
244  * Compute the origin of the entity based on the local bounds and
245  * the specified anchor. Internally, this function calls
246  * `Transformable::setOrigin()`.
247  *
248  * @param anchor The anchor of the entity
249  * @sa getLocalBounds(), Transformable::setOrigin()
250  */
251  void setAnchor(Anchor anchor);
252 
253  /**
254  * @brief Create a buffer with the current geometry
255  *
256  * The geometry is uploaded in the graphics memory so that it's faster
257  * to draw.
258  *
259  * @return A buffer with the current geometry
260  */
262 
263  virtual void draw(RenderTarget& target, RenderStates states) override;
264 
265  private:
266  void updatePositions();
267  void updateTexCoords();
268 
269  private:
270  const Texture *m_texture;
271  RectF m_textureRect;
272 
273  float m_top;
274  float m_bottom;
275  float m_left;
276  float m_right;
277 
278  Vector2f m_size;
279 
280  Vertex m_vertices[16];
281  };
282 
283 
284 #ifndef DOXYGEN_SHOULD_SKIP_THIS
285 }
286 #endif
287 }
288 
289 #endif // GF_NINE_PATCH_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the nine-patch.
Definition: NinePatch.h:152
void setTexture(const Texture &texture, bool resetRect=false)
Change the source texture of the nine-patch.
const Color4f & getColor() const
Get the global color of the nine-patch.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A nine-patch.
Definition: NinePatch.h:46
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
void setColor(const Color4f &color)
Set the global color of the nine-patch.
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
const Texture & getTexture() const
Get the source texture of the nine-patch.
Definition: NinePatch.h:104
Data in the graphics memory.
Definition: VertexBuffer.h:70
void setVerticalLimits(float top, float bottom)
Set the vertical limits of the stretchable area.
NinePatch()
Default constructor.
A texture for colored images.
Definition: Texture.h:339
The namespace for gf classes.
Definition: Action.h:34
Vector2f getSize() const
Get the size of the stretched area.
Definition: NinePatch.h:224
void setSize(Vector2f size)
Set the size of the stretched area.
void setHorizontalLimits(float left, float right)
Set the horizontal limits of the stretchable area.
bool hasTexture() const
Check if a texture is set.
Definition: NinePatch.h:115
Anchor
An anchor of a box.
Definition: Anchor.h:41
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
void setTextureRect(const RectF &rect)
Set the sub-rectangle of the texture that the nine-patch will display.
#define GF_API
Definition: Portability.h:35
void setLimits(float top, float bottom, float left, float right)
Set the limits of the stretchable area.
void unsetTexture()
Unset the source texture of the nine-patch.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
NinePatch(const Texture &texture)
Construct the nine-patch from a source texture.
NinePatch(const Texture &texture, const RectF &textureRect)
Construct the nine-patch from a sub-rectangle of a source texture.
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.