1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsPrerequisites.h"
6#include "2D/BsSprite.h"
7#include "Math/BsVector2.h"
8#include "Image/BsColor.h"
9
10namespace bs
11{
12 /** @addtogroup 2D
13 * @{
14 */
15
16 /** Image sprite description structure used for initializing or updating an image sprite. */
17 struct IMAGE_SPRITE_DESC
18 {
19 IMAGE_SPRITE_DESC() = default;
20
21 UINT32 width = 0; /**< Width of the image in pixels. */
22 UINT32 height = 0; /**< Height of the image in pixels. */
23 SpriteAnchor anchor = SA_TopLeft; /**< Determines where in the provided bounds will the sprite be placed. */
24 Vector2 uvScale = Vector2(1.0f, 1.0f); /**< Scale applied to UV width/height used for rendering the sprite. */
25 Vector2 uvOffset = Vector2(0.0f, 0.0f); /**< Offset applied to UV coordinates when rendering the sprite. */
26 bool transparent = true; /**< Should the sprite be rendered with transparency. */
27
28 HSpriteTexture texture; /**< Texture to overlay on the sprite. */
29 Color color; /**< Color tint to apply to the sprite. */
30 /**
31 * Time (since application start) at which the sprite texture's 0th frame is played. Used if the sprite texture
32 * has sprite sheet animation defined.
33 */
34 float animationStartTime = 0.0f;
35
36 /**
37 * Borders (in texels) that allow you to control how is the texture scaled. If borders are 0 the texture will be
38 * scaled uniformly. If they are not null only the area inside the borders will be scaled and the outside are will
39 * remain the original size as in the texture. This allows you to implement "Scale9Grid" functionality.
40 */
41 UINT32 borderLeft = 0;
42 UINT32 borderRight = 0;
43 UINT32 borderTop = 0;
44 UINT32 borderBottom = 0;
45 };
46
47 /** A sprite consisting of a single image represented by a sprite texture. */
48 class BS_EXPORT ImageSprite : public Sprite
49 {
50 public:
51 ImageSprite() = default;
52 ~ImageSprite();
53
54 /**
55 * Recreates internal sprite data according the specified description structure.
56 *
57 * @param[in] desc Describes the geometry and material of the sprite.
58 * @param[in] groupId Group identifier that forces different materials to be used for different groups (for
59 * example you don't want the sprites to share the same material if they use different world
60 * transform matrices).
61 */
62 void update(const IMAGE_SPRITE_DESC& desc, UINT64 groupId);
63
64 /**
65 * Calculates the required UV scale in order for a texture of size @p sourceSize to be placed on the surface
66 * of @p destSize size, while respecting the chosen scale mode.
67 */
68 static Vector2 getTextureUVScale(Vector2I sourceSize, Vector2I destSize, TextureScaleMode scaleMode);
69
70 private:
71 /** Clears internal geometry buffers. */
72 void clearMesh();
73 };
74
75 /** @} */
76}