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 "GUI/BsGUIElement.h"
7#include "2D/BsImageSprite.h"
8
9namespace bs
10{
11 /** @addtogroup GUI
12 * @{
13 */
14
15 /** A GUI element that displays a texture. */
16 class BS_EXPORT GUITexture : public GUIElement
17 {
18 public:
19 /** Returns type name of the GUI element used for finding GUI element styles. */
20 static const String& getGUITypeName();
21
22 /**
23 * Creates a new GUI texture element.
24 *
25 * @param[in] texture Texture element to display. If this is null then the texture specified by the style
26 * will be used.
27 * @param[in] scale Scale mode to use when sizing the texture.
28 * @param[in] transparent Determines should the texture be rendered with transparency active.
29 * @param[in] options Options that allow you to control how is the element positioned and sized. This will
30 * override any similar options set by style.
31 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
32 * GUIWidget the element is used on. If not specified default style is used.
33 */
34 static GUITexture* create(const HSpriteTexture& texture, TextureScaleMode scale, bool transparent,
35 const GUIOptions& options, const String& styleName = StringUtil::BLANK);
36
37 /**
38 * Creates a new GUI texture element.
39 *
40 * @param[in] texture Texture element to display. If this is null then the texture specified by the style
41 * will be used.
42 * @param[in] scale Scale mode to use when sizing the texture.
43 * @param[in] transparent Determines should the texture be rendered with transparency active.
44 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
45 * GUIWidget the element is used on. If not specified default style is used.
46 */
47 static GUITexture* create(const HSpriteTexture& texture, TextureScaleMode scale, bool transparent,
48 const String& styleName = StringUtil::BLANK);
49
50 /**
51 * Creates a new GUI texture element.
52 *
53 * @param[in] texture Texture element to display. If this is null then the texture specified by the style
54 * will be used.
55 * @param[in] scale Scale mode to use when sizing the texture.
56 * @param[in] options Options that allow you to control how is the element positioned and sized. This will
57 * override any similar options set by style.
58 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
59 * GUIWidget the element is used on. If not specified default style is used.
60 */
61 static GUITexture* create(const HSpriteTexture& texture, TextureScaleMode scale,
62 const GUIOptions& options, const String& styleName = StringUtil::BLANK);
63
64 /**
65 * Creates a new GUI texture element.
66 *
67 * @param[in] texture Texture element to display. If this is null then the texture specified by the style
68 * will be used.
69 * @param[in] scale Scale mode to use when sizing the texture.
70 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
71 * GUIWidget the element is used on. If not specified default style is used.
72 */
73 static GUITexture* create(const HSpriteTexture& texture, TextureScaleMode scale,
74 const String& styleName = StringUtil::BLANK);
75
76 /**
77 * Creates a new GUI texture element. Uses the default StretchToFit scale mode.
78 *
79 * @param[in] texture Texture element to display. If this is null then the texture specified by the style
80 * will be used.
81 * @param[in] options Options that allow you to control how is the element positioned and sized. This will
82 * override any similar options set by style.
83 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
84 * GUIWidget the element is used on. If not specified default style is used.
85 */
86 static GUITexture* create(const HSpriteTexture& texture,
87 const GUIOptions& options, const String& styleName = StringUtil::BLANK);
88
89 /**
90 * Creates a new GUI texture element. Uses the default StretchToFit scale mode.
91 *
92 * @param[in] texture Texture element to display. If this is null then the texture specified by the style
93 * will be used.
94 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
95 * GUIWidget the element is used on. If not specified default style is used.
96 */
97 static GUITexture* create(const HSpriteTexture& texture, const String& styleName = StringUtil::BLANK);
98
99 /**
100 * Creates a new GUI texture element. Uses the "normal" texture from the active GUI element style.
101 *
102 * @param[in] scale Scale mode to use when sizing the texture.
103 * @param[in] options Options that allow you to control how is the element positioned and sized.
104 * This will override any similar options set by style.
105 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
106 * GUIWidget the element is used on. If not specified default style is used.
107 */
108 static GUITexture* create(TextureScaleMode scale, const GUIOptions& options,
109 const String& styleName = StringUtil::BLANK);
110
111 /**
112 * Creates a new GUI texture element. Uses the "normal" texture from the active GUI element style.
113 *
114 * @param[in] scale Scale mode to use when sizing the texture.
115 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
116 * GUIWidget the element is used on. If not specified default style is used.
117 */
118 static GUITexture* create(TextureScaleMode scale, const String& styleName = StringUtil::BLANK);
119
120 /**
121 * Creates a new GUI texture element. Uses the "normal" texture from the active GUI element style.
122 * Uses the default StretchToFit scale mode.
123 *
124 * @param[in] options Options that allow you to control how is the element positioned and sized. This will
125 * override any similar options set by style.
126 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
127 * GUIWidget the element is used on. If not specified default style is used.
128 */
129 static GUITexture* create(const GUIOptions& options, const String& styleName = StringUtil::BLANK);
130
131 /**
132 * Creates a new GUI texture element. Uses the "normal" texture from the active GUI element style.
133 * Uses the default StretchToFit scale mode.
134 *
135 * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the
136 * GUIWidget the element is used on. If not specified default style is used.
137 */
138 static GUITexture* create(const String& styleName = StringUtil::BLANK);
139
140 /**
141 * Changes the active texture. If the provided texture is null then the texture specified by the style will be used.
142 */
143 void setTexture(const HSpriteTexture& texture);
144
145 public: // ***** INTERNAL ******
146 /** @name Internal
147 * @{
148 */
149
150 /** @copydoc GUIElement::_getElementType */
151 ElementType _getElementType() const override { return ElementType::Texture; }
152
153 /** @copydoc GUIElement::_getOptimalSize */
154 Vector2I _getOptimalSize() const override;
155
156 /** @} */
157 protected:
158 GUITexture(const String& styleName, const HSpriteTexture& texture, TextureScaleMode scale,
159 bool transparent, const GUIDimensions& dimensions);
160 virtual ~GUITexture();
161
162 /** @copydoc GUIElement::_getNumRenderElements */
163 UINT32 _getNumRenderElements() const override;
164
165 /** @copydoc GUIElement::_getMaterial */
166 const SpriteMaterialInfo& _getMaterial(UINT32 renderElementIdx, SpriteMaterial** material) const override;
167
168 /** @copydoc GUIElement::_getMeshInfo() */
169 void _getMeshInfo(UINT32 renderElementIdx, UINT32& numVertices, UINT32& numIndices, GUIMeshType& type) const override;
170
171 /** @copydoc GUIElement::_fillBuffer */
172 void _fillBuffer(UINT8* vertices, UINT32* indices, UINT32 vertexOffset, UINT32 indexOffset,
173 UINT32 maxNumVerts, UINT32 maxNumIndices, UINT32 renderElementIdx) const override;
174
175 /** @copydoc GUIElement::updateRenderElementsInternal */
176 void updateRenderElementsInternal() override;
177
178 /** @copydoc GUIElement::styleUpdated */
179 void styleUpdated() override;
180
181 ImageSprite* mImageSprite;
182 Vector2I mImageSpriteOffset;
183 HSpriteTexture mActiveTexture;
184 UINT32 mActiveTextureWidth = 0;
185 UINT32 mActiveTextureHeight = 0;
186 IMAGE_SPRITE_DESC mDesc;
187 TextureScaleMode mScaleMode;
188 bool mTransparent;
189 bool mUsingStyleTexture;
190 };
191
192 /** @} */
193}