MyGUI 3.4.3
MyGUI_Canvas.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_Canvas.h"
10#include "MyGUI_Gui.h"
11#include "MyGUI_RenderManager.h"
12#include "MyGUI_Bitwise.h"
13
14namespace MyGUI
15{
16
18 {
19 mGenTexName = utility::toString((size_t)this, "_Canvas");
20 }
21
23 {
24 int width = std::max(1, getWidth());
25 int height = std::max(1, getHeight());
26
27 createTexture(width, height, _resizeMode, _usage, _format);
28 }
29
31 const IntSize& _size,
32 TextureResizeMode _resizeMode,
33 TextureUsage _usage,
34 PixelFormat _format)
35 {
36 int width = std::max(1, _size.width);
37 int height = std::max(1, _size.height);
38
39 createTexture(width, height, _resizeMode, _usage, _format);
40 }
41
42 void Canvas::createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
43 {
44 int width = std::max(1, _width);
45 int height = std::max(1, _height);
46
48
50 mTexture->setInvalidateListener(this);
51 mTexture->createManual(width, height, _usage, _format);
52
53 mTexManaged = true;
54
56 correctUV();
57 requestUpdateCanvas(this, Event(true, true, mInvalidateData));
58 }
59
60 void Canvas::resize(const IntSize& _size)
61 {
62 if (_size.width <= 0 || _size.height <= 0 || !mTexManaged)
63 return;
64
65 mReqTexSize = _size;
66
67 frameAdvise(true);
68 }
69
71 int _width,
72 int _height,
73 TextureResizeMode _resizeMode,
74 TextureUsage _usage,
75 PixelFormat _format)
76 {
77 mTexResizeMode = _resizeMode;
78
79 int width = std::max(1, _width);
80 int height = std::max(1, _height);
81
82 if (_resizeMode == TRM_PT_CONST_SIZE)
83 {
84 mReqTexSize = IntSize(width, height);
85 }
86 else
87 {
88 mReqTexSize = IntSize(std::max(1, getWidth()), std::max(1, getHeight()));
89 }
90
91 bool create = checkCreate(width, height);
92
93 width = Bitwise::firstPO2From(width);
94 height = Bitwise::firstPO2From(height);
95
96 if (create)
97 createExactTexture(width, height, _usage, _format);
98 }
99
100 void Canvas::setSize(const IntSize& _size)
101 {
102 resize(_size);
103
104 Base::setSize(_size);
105 }
106
107 void Canvas::setCoord(const IntCoord& _coord)
108 {
109 resize(_coord.size());
110
111 Base::setCoord(_coord);
112 }
113
115 {
116 mInvalidateData = true;
117 frameAdvise(true);
118 }
119
120 bool Canvas::checkCreate(int _width, int _height) const
121 {
122 if (mTexture == nullptr)
123 return true;
124
125 if (mTexture->getWidth() >= _width && mTexture->getHeight() >= _height)
126 return false;
127
128 return true;
129 }
130
131 void Canvas::validate(int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format) const
132 {
133 _width = std::max(1, _width);
134 _height = std::max(1, _height);
135
136 _width = Bitwise::firstPO2From(_width);
137 _height = Bitwise::firstPO2From(_height);
138
139 // restore usage and format
140 if (mTexture != nullptr)
141 {
142 if (_usage == getDefaultTextureUsage())
143 _usage = mTexture->getUsage();
144
145 if (_format == getDefaultTextureFormat())
146 _format = mTexture->getFormat();
147 }
148 }
149
151 {
152 _destroyTexture(true);
153 }
154
156 {
157 _destroyTexture(false);
158 frameAdvise(false);
159 }
160
162 {
163 }
164
165 void Canvas::_destroyTexture(bool _sendEvent)
166 {
167 if (mTexture != nullptr)
168 {
169 if (_sendEvent)
170 {
172 }
173
175 mTexture = nullptr;
176 }
177 }
178
180 {
182 {
184 0,
185 0,
186 (float)mReqTexSize.width / (float)getTextureRealWidth(),
187 (float)mReqTexSize.height / (float)getTextureRealHeight()));
188 }
189
191 {
192 _setUVSet(FloatRect(0, 0, 1, 1));
193 }
194 }
195
197 {
198 void* data = mTexture->lock(_usage);
199
200 mTexData = reinterpret_cast<uint8*>(data);
201
202 return data;
203 }
204
206 {
207 mTexture->unlock();
208 }
209
211 {
213 }
214
215 void Canvas::frameAdvise(bool _advise)
216 {
217 if (_advise)
218 {
219 if (!mFrameAdvise)
220 {
222 mFrameAdvise = true;
223 }
224 }
225 else
226 {
227 if (mFrameAdvise)
228 {
230 mFrameAdvise = false;
231 }
232 }
233 }
234
235 void Canvas::frameEntered(float _time)
236 {
237 int width = mReqTexSize.width;
238 int height = mReqTexSize.height;
241
242 validate(width, height, usage, format);
243
244 bool create = checkCreate(width, height);
245
247 create = false;
248
249 if (create)
250 {
251 createExactTexture(width, height, usage, format);
252 correctUV();
253 }
254 else // I thought order is important
255 {
256 correctUV();
257 requestUpdateCanvas(this, Event(false, true, mInvalidateData));
258 }
259
260 mInvalidateData = false;
261 frameAdvise(false);
262 }
263
265 {
267 }
268
269 void Canvas::_setUVSet(const FloatRect& _rect)
270 {
271 if (nullptr != getSubWidgetMain())
272 getSubWidgetMain()->_setUVSet(_rect);
273 }
274
275 bool Canvas::isLocked() const
276 {
277 return mTexture->isLocked();
278 }
279
281 {
282 return (int)mTexture->getWidth();
283 }
284
286 {
287 return (int)mTexture->getHeight();
288 }
289
294
296 {
297 return mReqTexSize.width;
298 }
299
301 {
302 return mReqTexSize.height;
303 }
304
306 {
307 return mReqTexSize;
308 }
309
311 {
312 return mTexture->getFormat();
313 }
314
315 const std::string& Canvas::getTextureName() const
316 {
317 return mTexture->getName();
318 }
319
324
326 {
327 mTexResizeMode = _value;
328 }
329
331 {
332 return mTexture != nullptr;
333 }
334
336 {
337 return mTexManaged;
338 }
339
341 {
342 return mTexture;
343 }
344
346 {
347 mTexManaged = _value;
348 }
349
354
359
360} // namespace MyGUI
IntSize mReqTexSize
Requested bu user sizes.
void frameAdvise(bool _advise)
For updating once per frame.
void correctUV()
Correct texture uv-coordinates.
void frameEntered(float _time)
For updating once per frame.
void createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
Creates the texture itself.
bool isTextureManaged() const
Returns true if we own the texture, otherwise false.
bool mFrameAdvise
For updating once per frame. True state means updating before next frame starts.
bool isTextureCreated() const
Returns true if the texture was created (and exists), otherwise false.
void unlock()
Unlocks hardware pixel buffer.
bool isTextureSrcSize() const
Checks if the texture has the source (required by user) size, otherwise real texture size are bigger.
EventHandle_CanvasPtrEvent requestUpdateCanvas
bool mTexManaged
true if we own the texture (can delete it or replace by another instance), otherwise false
void createTexture(TextureResizeMode _resizeMode, TextureUsage _usage=getDefaultTextureUsage(), PixelFormat _format=getDefaultTextureFormat())
Creates texture.
PixelFormat getTextureFormat() const
Returns needed sizes while creating texture.
int getTextureRealHeight() const
Returns real height of texture.
static TextureUsage getDefaultTextureUsage()
Returns default GUI texture usage.
void initialiseOverride() override
TextureResizeMode mTexResizeMode
Texture resize mode.
void resize(const IntSize &_size)
Calls when resize widget.
void destroyTexture()
Destroys texture.
void _setUVSet(const FloatRect &_rect)
void _destroyTexture(bool _sendEvent)
Destroys texture.
void textureInvalidate(ITexture *_texture) override
uint8 * mTexData
Saved pointer from last calling lock.
IntSize getTextureSrcSize() const
Returns needed sizes while creating texture.
int getTextureSrcWidth() const
Returns needed width while creating texture.
void updateTexture()
Call user delegate update and removes old texture if it isn't original.
void shutdownOverride() override
TextureResizeMode getResizeMode() const
Returns resize mode.
ITexture * mTexture
Current texture.
EventHandle_CanvasPtr eventPreTextureChanges
IntSize getTextureRealSize() const
Returns real _size of texture.
int getTextureRealWidth() const
Returns real width of texture.
void validate(int &_width, int &_height, TextureUsage &_usage, PixelFormat &_format) const
Update entered parameters according to current texture resize mode(size) and restore (if can) paramet...
void setResizeMode(TextureResizeMode _value)
Sets resize mode of texture.
void setSize(const IntSize &_size) override
int getTextureSrcHeight() const
Returns needed height while creating texture.
void setCoord(const IntCoord &_coord) override
std::string mGenTexName
Generated texture name.
void * lock(TextureUsage _usage=TextureUsage::Write)
Locks hardware pixel buffer.
void setTextureManaged(bool _value)
Sets the texture managed.
static PixelFormat getDefaultTextureFormat()
Returns default GUI texture format.
bool checkCreate(int _width, int _height) const
Checks if we need to create a texture with such sizes.
const std::string & getTextureName() const
Returns name of the current texture.
ITexture * getTexture() const
Reurns interface texture.
bool isLocked() const
Checks lockness of hardware _pixel buffer.
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
EventHandle_FrameEventDelegate eventFrameStart
Definition MyGUI_Gui.h:215
virtual void _setUVSet(const FloatRect &)
virtual ITexture * createTexture(const std::string &_name)=0
virtual void destroyTexture(ITexture *_texture)=0
static RenderManager & getInstance()
ISubWidgetRect * getSubWidgetMain() const
void _setTextureName(std::string_view _texture)
static Type firstPO2From(Type _value)
std::string toString(T _value)
uint8_t uint8
Definition MyGUI_Types.h:46
types::TRect< float > FloatRect
Definition MyGUI_Types.h:34
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
TSize< T > size() const