Gamedev Framework (gf)  0.20.0
A C++17 framework for 2D games
NinePatch.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 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 "GraphicsApi.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 
46  class GF_GRAPHICS_API NinePatch : public Transformable {
47  public:
53  NinePatch();
54 
62  NinePatch(const Texture& texture);
63 
72  NinePatch(const Texture& texture, const RectF& textureRect);
73 
92  [[deprecated("You should use setTexture(const Texture&, const RectF&) instead")]]
93  void setTexture(const Texture& texture, bool resetRect);
94 
111  void setTexture(const Texture& texture, const RectF& textureRect = RectF::fromSize({ 1.0f, 1.0f }));
112 
123  const Texture& getTexture() const {
124  return *m_texture;
125  }
126 
134  bool hasTexture() const {
135  return m_texture != nullptr;
136  }
137 
145  void unsetTexture();
146 
162  void setTextureRect(const RectF& rect);
163 
171  const RectF& getTextureRect() const {
172  return m_textureRect;
173  }
174 
188  void setColor(const Color4f& color);
189 
197  const Color4f& getColor() const;
198 
209  void setLimits(float top, float bottom, float left, float right);
210 
219  void setVerticalLimits(float top, float bottom);
220 
229  void setHorizontalLimits(float left, float right);
230 
236  void setSize(Vector2f size);
237 
243  Vector2f getSize() const {
244  return m_size;
245  }
246 
258  RectF getLocalBounds() const;
259 
270  void setAnchor(Anchor anchor);
271 
280  VertexBuffer commitGeometry() const;
281 
282  virtual void draw(RenderTarget& target, const RenderStates& states) override;
283 
284  private:
285  void updatePositions();
286  void updateTexCoords();
287 
288  private:
289  const Texture *m_texture;
290  RectF m_textureRect;
291 
292  float m_top;
293  float m_bottom;
294  float m_left;
295  float m_right;
296 
297  Vector2f m_size;
298 
299  Vertex m_vertices[16];
300  };
301 
302 
303 #ifndef DOXYGEN_SHOULD_SKIP_THIS
304 }
305 #endif
306 }
307 
308 #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:171
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
static constexpr Rect< float > fromSize(Vector< float, 2 > size) noexcept
Create a rectangle from a size.
Definition: Rect.h:129
A nine-patch.
Definition: NinePatch.h:46
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
const Texture & getTexture() const
Get the source texture of the nine-patch.
Definition: NinePatch.h:123
Data in the graphics memory.
Definition: VertexBuffer.h:81
A texture for colored images.
Definition: Texture.h:313
The namespace for gf classes.
Definition: Action.h:35
Vector2f getSize() const
Get the size of the stretched area.
Definition: NinePatch.h:243
A 4D vector.
Definition: Vector.h:852
bool hasTexture() const
Check if a texture is set.
Definition: NinePatch.h:134
Anchor
An anchor of a box.
Definition: Anchor.h:38
General purpose math vector.
Definition: Vector.h:61