Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
NinePatch.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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
29namespace gf {
30#ifndef DOXYGEN_SHOULD_SKIP_THIS
31inline namespace v1 {
32#endif
33
34 class Texture;
35
46 class GF_GRAPHICS_API NinePatch : public Transformable {
47 public:
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
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
244 return m_size;
245 }
246
259
270 void setAnchor(Anchor anchor);
271
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
A nine-patch.
Definition: NinePatch.h:46
void unsetTexture()
Unset the source texture of the nine-patch.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
virtual void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
void setColor(const Color4f &color)
Set the global color of the nine-patch.
NinePatch(const Texture &texture)
Construct the nine-patch from a source texture.
void setVerticalLimits(float top, float bottom)
Set the vertical limits of the stretchable area.
void setSize(Vector2f size)
Set the size of the stretched area.
NinePatch(const Texture &texture, const RectF &textureRect)
Construct the nine-patch from a sub-rectangle of a source texture.
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
void setTexture(const Texture &texture, bool resetRect)
Change the source texture of the nine-patch.
const Color4f & getColor() const
Get the global color of the nine-patch.
void setTextureRect(const RectF &rect)
Set the sub-rectangle of the texture that the nine-patch will display.
Vector2f getSize() const
Get the size of the stretched area.
Definition: NinePatch.h:243
void setTexture(const Texture &texture, const RectF &textureRect=RectF::fromSize({ 1.0f, 1.0f }))
Change the source texture of the nine-patch.
const Texture & getTexture() const
Get the source texture of the nine-patch.
Definition: NinePatch.h:123
bool hasTexture() const
Check if a texture is set.
Definition: NinePatch.h:134
void setHorizontalLimits(float left, float right)
Set the horizontal limits of the stretchable area.
void setLimits(float top, float bottom, float left, float right)
Set the limits of the stretchable area.
const RectF & getTextureRect() const
Get the sub-rectangle of the texture displayed by the nine-patch.
Definition: NinePatch.h:171
NinePatch()
Default constructor.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
A texture for colored images.
Definition: Texture.h:313
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
Data in the graphics memory.
Definition: VertexBuffer.h:81
Anchor
An anchor of a box.
Definition: Anchor.h:38
@ Texture
A GPU texture.
The namespace for gf classes.
static constexpr Rect< float > fromSize(Vector< float, 2 > size) noexcept
Create a rectangle from a size.
Definition: Rect.h:114
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A 4D vector.
Definition: Vector.h:852
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75