Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
NinePatch.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_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  * If the nine-patch has no source texture, a `nullptr` pointer is returned.
98  * The returned pointer is const, which means that you can't
99  * modify the texture when you retrieve it with this function.
100  *
101  * @return Pointer to the nine-patch's texture
102  *
103  * @sa setTexture()
104  */
105  const Texture *getTexture() const {
106  return m_texture;
107  }
108 
109  /**
110  * @brief Unset the source texture of the nine-patch
111  *
112  * After a call to this function, the nine-patch has no source texture.
113  *
114  * @sa setTexture()
115  */
116  void unsetTexture();
117 
118  /**
119  * @brief Set the sub-rectangle of the texture that the nine-patch will display
120  *
121  * The texture rect is useful when you don't want to display
122  * the whole texture, but rather a part of it.
123  * By default, the texture rect covers the entire texture.
124  *
125  * The rectangle is given in texture coordinates, meaning that
126  * @f$(0,0)@f$ is the top left corner and @f$(1,1)@f$ is the
127  * bottom right corner.
128  *
129  * @param rect Rectangle defining the region of the texture to display
130  *
131  * @sa getTextureRect(), setTexture()
132  */
133  void setTextureRect(const RectF& rect);
134 
135  /**
136  * @brief Get the sub-rectangle of the texture displayed by the nine-patch
137  *
138  * @return Texture rectangle of the nine-patch in texture coordinates
139  *
140  * @sa setTextureRect()
141  */
142  const RectF& getTextureRect() const {
143  return m_textureRect;
144  }
145 
146  /**
147  * @brief Set the global color of the nine-patch
148  *
149  * This color is modulated (multiplied) with the nine-patch's
150  * texture. It can be used to colorize the nine-patch, or change
151  * its global opacity.
152  *
153  * By default, the nine-patch's color is opaque white.
154  *
155  * @param color New color of the nine-patch
156  *
157  * @sa getColor()
158  */
159  void setColor(const Color4f& color);
160 
161  /**
162  * @brief Get the global color of the nine-patch
163  *
164  * @return Global color of the nine-patch
165  *
166  * @sa setColor()
167  */
168  const Color4f& getColor() const;
169 
170  /**
171  * @brief Set the limits of the stretchable area
172  *
173  * The limits are given in normalized coordinates
174  *
175  * @param top The top limit
176  * @param bottom The bottom limit
177  * @param left The left limit
178  * @param right The right limit
179  */
180  void setLimits(float top, float bottom, float left, float right);
181 
182  /**
183  * @brief Set the vertical limits of the stretchable area
184  *
185  * The limits are given in normalized coordinates
186  *
187  * @param top The top limit
188  * @param bottom The bottom limit
189  */
190  void setVerticalLimits(float top, float bottom);
191 
192  /**
193  * @brief Set the horizontal limits of the stretchable area
194  *
195  * The limits are given in normalized coordinates
196  *
197  * @param left The left limit
198  * @param right The right limit
199  */
200  void setHorizontalLimits(float left, float right);
201 
202  /**
203  * @brief Set the size of the stretched area
204  *
205  * @param size The size of the stretched area
206  */
207  void setSize(Vector2f size);
208 
209  /**
210  * @brief Get the size of the stretched area
211  *
212  * @return The size of the stretched area
213  */
214  Vector2f getSize() const {
215  return m_size;
216  }
217 
218  /**
219  * @brief Get the local bounding rectangle of the entity
220  *
221  * The returned rectangle is in local coordinates, which means
222  * that it ignores the transformations (translation, rotation,
223  * scale, ...) that are applied to the entity.
224  * In other words, this function returns the bounds of the
225  * entity in the entity's coordinate system.
226  *
227  * @return Local bounding rectangle of the entity
228  */
229  RectF getLocalBounds() const;
230 
231  /**
232  * @brief Set the anchor origin of the entity
233  *
234  * Compute the origin of the entity based on the local bounds and
235  * the specified anchor. Internally, this function calls
236  * `Transformable::setOrigin()`.
237  *
238  * @param anchor The anchor of the entity
239  * @sa getLocalBounds(), Transformable::setOrigin()
240  */
241  void setAnchor(Anchor anchor);
242 
243  /**
244  * @brief Create a buffer with the current geometry
245  *
246  * The geometry is uploaded in the graphics memory so that it's faster
247  * to draw.
248  *
249  * @return A buffer with the current geometry
250  */
252 
253  virtual void draw(RenderTarget& target, RenderStates states) override;
254 
255  private:
256  void updatePositions();
257  void updateTexCoords();
258 
259  private:
260  const Texture *m_texture;
261  RectF m_textureRect;
262 
263  float m_top;
264  float m_bottom;
265  float m_left;
266  float m_right;
267 
268  Vector2f m_size;
269 
270  Vertex m_vertices[16];
271  };
272 
273 
274 #ifndef DOXYGEN_SHOULD_SKIP_THIS
275 }
276 #endif
277 }
278 
279 #endif // GF_NINE_PATCH_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:113
void setTexture(const Texture &texture, bool resetRect=false)
Change the source texture of the nine-patch.
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the nine-patch.
Definition: NinePatch.h:142
const Texture * getTexture() const
Get the source texture of the nine-patch.
Definition: NinePatch.h:105
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
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.
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
Definition: Action.h:34
void setSize(Vector2f size)
Set the size of the stretched area.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
void setHorizontalLimits(float left, float right)
Set the horizontal limits of the stretchable area.
const Color4f & getColor() const
Get the global color of the nine-patch.
Anchor
The origin anchor of the transformable object.
Definition: Transformable.h:45
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.
NinePatch(const Texture &texture)
Construct the nine-patch from a source texture.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Vector2f getSize() const
Get the size of the stretched area.
Definition: NinePatch.h:214
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.