MyGUI 3.4.2
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 mTexture(nullptr),
19 mTexResizeMode( TRM_PT_CONST_SIZE ),
20 mTexData(nullptr),
21 mTexManaged(true),
22 mFrameAdvise(false),
23 mInvalidateData(false)
24 {
25 mGenTexName = utility::toString((size_t)this, "_Canvas");
26 }
27
29 {
30 int width = std::max(1, getWidth());
31 int height = std::max(1, getHeight());
32
33 createTexture( width, height, _resizeMode, _usage, _format );
34 }
35
37 {
38 int width = std::max(1, _size.width);
39 int height = std::max(1, _size.height);
40
41 createTexture( width, height, _resizeMode, _usage, _format );
42 }
43
45 {
46 int width = std::max(1, _width);
47 int height = std::max(1, _height);
48
50
53 mTexture->createManual( width, height, _usage, _format );
54
55 mTexManaged = true;
56
58 correctUV();
59 requestUpdateCanvas( this, Event( true, true, mInvalidateData ) );
60 }
61
63 {
64 if ( _size.width <= 0 || _size.height <= 0 || ! mTexManaged )
65 return;
66
68
69 frameAdvise( true );
70 }
71
73 {
75
76 int width = std::max(1, _width);
77 int height = std::max(1, _height);
78
80 {
81 mReqTexSize = IntSize(width, height);
82 }
83 else
84 {
85 mReqTexSize = IntSize(std::max(1, getWidth()), std::max(1, getHeight()));
86 }
87
88 bool create = checkCreate( width, height );
89
90 width = Bitwise::firstPO2From(width);
91 height = Bitwise::firstPO2From(height);
92
93 if ( create )
94 createExactTexture( width, height, _usage, _format );
95 }
96
98 {
99 resize( _size );
100
102 }
103
105 {
106 resize( _coord.size() );
107
109 }
110
112 {
113 mInvalidateData = true;
114 frameAdvise( true );
115 }
116
117 bool Canvas::checkCreate( int _width, int _height ) const
118 {
119 if ( mTexture == nullptr )
120 return true;
121
122 if ( mTexture->getWidth() >= _width && mTexture->getHeight() >= _height )
123 return false;
124
125 return true;
126 }
127
129 {
130 _width = std::max(1, _width);
131 _height = std::max(1, _height);
132
135
136 // restore usage and format
137 if ( mTexture != nullptr )
138 {
141
144 }
145 }
146
148 {
149 _destroyTexture( true );
150 }
151
153 {
154 _destroyTexture(false);
155 frameAdvise(false);
156 }
157
159 {
160 }
161
163 {
164 if ( mTexture != nullptr )
165 {
166 if ( _sendEvent )
167 {
169 }
170
171 RenderManager::getInstance().destroyTexture( mTexture );
172 mTexture = nullptr;
173 }
174
175 }
176
178 {
180 {
181 _setUVSet(
182 FloatRect(
183 0,
184 0,
185 (float) mReqTexSize.width / (float) getTextureRealWidth(),
186 (float) mReqTexSize.height / (float) getTextureRealHeight()));
187 }
188
190 {
191 _setUVSet( FloatRect( 0, 0, 1, 1 ) );
192 }
193 }
194
196 {
197 void* data = mTexture->lock(_usage);
198
199 mTexData = reinterpret_cast< uint8* >( data );
200
201 return data;
202 }
203
205 {
206 mTexture->unlock();
207 }
208
210 {
212 }
213
215 {
216 if ( _advise )
217 {
218 if ( ! mFrameAdvise )
219 {
221 mFrameAdvise = true;
222 }
223 }
224 else
225 {
226 if ( mFrameAdvise )
227 {
229 mFrameAdvise = false;
230 }
231 }
232 }
233
235 {
236 int width = mReqTexSize.width;
237 int height = mReqTexSize.height;
240
241 validate( width, height, usage, format );
242
243 bool create = checkCreate( width, height );
244
246 create = false;
247
248 if ( create )
249 {
250 createExactTexture( width, height, usage, format );
251 correctUV();
252 }
253 else // I thought order is important
254 {
255 correctUV();
256 requestUpdateCanvas( this, Event( false, true, mInvalidateData ) );
257 }
258
259 mInvalidateData = false;
260 frameAdvise( false );
261 }
262
264 {
266 }
267
269 {
270 if (nullptr != getSubWidgetMain())
272 }
273
274 bool Canvas::isLocked() const
275 {
276 return mTexture->isLocked();
277 }
278
280 {
281 return (int) mTexture->getWidth();
282 }
283
285 {
286 return (int) mTexture->getHeight();
287 }
288
290 {
292 }
293
295 {
296 return mReqTexSize.width;
297 }
298
300 {
301 return mReqTexSize.height;
302 }
303
305 {
306 return mReqTexSize;
307 }
308
310 {
311 return mTexture->getFormat();
312 }
313
314 const std::string& Canvas::getTextureName() const
315 {
316 return mTexture->getName();
317 }
318
320 {
321 return mTexResizeMode;
322 }
323
325 {
327 }
328
330 {
331 return mTexture != nullptr;
332 }
333
335 {
336 return mTexManaged;
337 }
338
340 {
341 return mTexture;
342 }
343
345 {
347 }
348
350 {
352 }
353
355 {
357 }
358
359} // namespace MyGUI
IntSize mReqTexSize
Requested bu user sizes.
void setCoord(const IntCoord &_value) override
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.
int getTextureSrcHeight() const
Returns needed height while creating texture.
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.
void setSize(const IntSize &_value) override
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()
virtual void _setUVSet(const FloatRect &)
virtual void setInvalidateListener(ITextureInvalidateListener *)
virtual PixelFormat getFormat() const =0
virtual int getWidth() const =0
virtual const std::string & getName() const =0
virtual TextureUsage getUsage() const =0
virtual void unlock()=0
virtual bool isLocked() const =0
virtual int getHeight() const =0
virtual void * lock(TextureUsage _access)=0
virtual void createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format)=0
static RenderManager & getInstance()
ISubWidgetRect * getSubWidgetMain() const
void _setTextureName(const std::string &_texture)
static Type firstPO2From(Type _value)
std::string toString(T p)
uint8_t uint8
Definition MyGUI_Types.h:46
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
types::TRect< float > FloatRect
Definition MyGUI_Types.h:34