MyGUI 3.2.0

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_Canvas.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_Canvas.h"
24 #include "MyGUI_ResourceManager.h"
25 #include "MyGUI_Gui.h"
26 #include "MyGUI_RenderManager.h"
27 #include "MyGUI_Bitwise.h"
28
29 namespace MyGUI
30 {
31
32 Canvas::Canvas() :
33 mTexture( nullptr ),
34 mTexResizeMode( TRM_PT_CONST_SIZE ),
35 mTexData( 0 ),
36 mTexManaged( true ),
37 mFrameAdvise( false ),
38 mInvalidateData(false)
39 {
40 mGenTexName = utility::toString( this, "_Canvas" );
41 }
42
43 void Canvas::createTexture( TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
44 {
45 int width = std::max(1, getWidth());
46 int height = std::max(1, getHeight());
47
48 createTexture( width, height, _resizeMode, _usage, _format );
49 }
50
51 void Canvas::createTexture( const IntSize& _size, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
52 {
53 int width = std::max(1, _size.width);
54 int height = std::max(1, _size.height);
55
56 createTexture( width, height, _resizeMode, _usage, _format );
57 }
58
59 void Canvas::createExactTexture( int _width, int _height, TextureUsage _usage, PixelFormat _format )
60 {
61 int width = std::max(1, _width);
62 int height = std::max(1, _height);
63
64 destroyTexture();
65
66 mTexture = RenderManager::getInstance().createTexture(mGenTexName);
67 mTexture->setInvalidateListener(this);
68 mTexture->createManual( width, height, _usage, _format );
69
70 mTexManaged = true;
71
72 _setTextureName( mGenTexName );
73 correctUV();
74 requestUpdateCanvas( this, Event( true, true, mInvalidateData ) );
75 }
76
77 void Canvas::resize( const IntSize& _size )
78 {
79 if ( _size.width <= 0 || _size.height <= 0 || ! mTexManaged )
80 return;
81
82 mReqTexSize = _size;
83
84 frameAdvise( true );
85 }
86
87 void Canvas::createTexture( int _width, int _height, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
88 {
89 int width = std::max(1, _width);
90 int height = std::max(1, _height);
91
92 if ( mReqTexSize.empty() )
93 mReqTexSize = IntSize( width, height );
94
95 mTexResizeMode = _resizeMode;
96
97 bool create = checkCreate( width, height );
98
99 width = Bitwise::firstPO2From(width);
100 height = Bitwise::firstPO2From(height);
101
102 if ( create )
103 createExactTexture( width, height, _usage, _format );
104 }
105
106 void Canvas::setSize( const IntSize& _size )
107 {
108 resize( _size );
109
110 Base::setSize( _size );
111 }
112
113 void Canvas::setCoord( const IntCoord& _coord )
114 {
115 resize( _coord.size() );
116
117 Base::setCoord( _coord );
118 }
119
120 void Canvas::updateTexture()
121 {
122 mInvalidateData = true;
123 frameAdvise( true );
124 }
125
126 bool Canvas::checkCreate( int _width, int _height ) const
127 {
128 if ( mTexture == nullptr )
129 return true;
130
131 if ( mTexture->getWidth() >= _width && mTexture->getHeight() >= _height )
132 return false;
133
134 return true;
135 }
136
137 void Canvas::validate( int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format ) const
138 {
139 _width = std::max(1, _width);
140 _height = std::max(1, _height);
141
142 _width = Bitwise::firstPO2From(_width);
143 _height = Bitwise::firstPO2From(_height);
144
145 // restore usage and format
146 if ( mTexture != nullptr )
147 {
148 if ( _usage == getDefaultTextureUsage() )
149 _usage = mTexture->getUsage();
150
151 if ( _format == getDefaultTextureFormat() )
152 _format = mTexture->getFormat();
153 }
154 }
155
156 void Canvas::destroyTexture()
157 {
158 _destroyTexture( true );
159 }
160
161 void Canvas::shutdownOverride()
162 {
163 _destroyTexture(false);
164 frameAdvise(false);
165 }
166
167 void Canvas::initialiseOverride()
168 {
169 }
170
171 void Canvas::_destroyTexture( bool _sendEvent )
172 {
173 if ( mTexture != nullptr )
174 {
175 if ( _sendEvent )
176 {
177 eventPreTextureChanges( this );
178 }
179
180 RenderManager::getInstance().destroyTexture( mTexture );
181 mTexture = nullptr;
182 }
183
184 }
185
186 void Canvas::correctUV()
187 {
188 if ( mTexResizeMode == TRM_PT_VIEW_REQUESTED )
189 {
190 _setUVSet(
191 FloatRect(
192 0,
193 0,
194 (float) mReqTexSize.width / (float) getTextureRealWidth(),
195 (float) mReqTexSize.height / (float) getTextureRealHeight()
196 )
197 );
198 }
199
200 if ( mTexResizeMode == TRM_PT_CONST_SIZE || mTexResizeMode == TRM_PT_VIEW_ALL )
201 {
202 _setUVSet( FloatRect( 0, 0, 1, 1 ) );
203 }
204 }
205
206 void* Canvas::lock(TextureUsage _usage)
207 {
208 void* data = mTexture->lock(_usage);
209
210 mTexData = reinterpret_cast< uint8* >( data );
211
212 return data;
213 }
214
215 void Canvas::unlock()
216 {
217 mTexture->unlock();
218 }
219
220 bool Canvas::isTextureSrcSize() const
221 {
222 return getTextureSrcSize() == getTextureRealSize();
223 }
224
225 void Canvas::frameAdvise( bool _advise )
226 {
227 if ( _advise )
228 {
229 if ( ! mFrameAdvise )
230 {
231 MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate( this, &Canvas::frameEntered );
232 mFrameAdvise = true;
233 }
234 }
235 else
236 {
237 if ( mFrameAdvise )
238 {
239 MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate( this, &Canvas::frameEntered );
240 mFrameAdvise = false;
241 }
242 }
243 }
244
245 void Canvas::frameEntered( float _time )
246 {
247 int width = mReqTexSize.width;
248 int height = mReqTexSize.height;
249 TextureUsage usage = getDefaultTextureUsage();
250 PixelFormat format = getDefaultTextureFormat();
251
252 validate( width, height, usage, format );
253
254 bool create = checkCreate( width, height );
255
256 if ( mTexResizeMode == TRM_PT_CONST_SIZE )
257 create = false;
258
259 if ( create )
260 {
261 createExactTexture( width, height, usage, format );
262 correctUV();
263 }
264 else // I thought order is important
265 {
266 correctUV();
267 requestUpdateCanvas( this, Event( false, true, mInvalidateData ) );
268 }
269
270 mInvalidateData = false;
271 frameAdvise( false );
272 }
273
274 void Canvas::textureInvalidate(ITexture* _texture)
275 {
276 updateTexture();
277 }
278
279 void Canvas::_setUVSet(const FloatRect& _rect)
280 {
281 if (nullptr != getSubWidgetMain())
282 getSubWidgetMain()->_setUVSet(_rect);
283 }
284
285 bool Canvas::isLocked() const
286 {
287 return mTexture->isLocked();
288 }
289
290 int Canvas::getTextureRealWidth() const
291 {
292 return (int) mTexture->getWidth();
293 }
294
295 int Canvas::getTextureRealHeight() const
296 {
297 return (int) mTexture->getHeight();
298 }
299
300 IntSize Canvas::getTextureRealSize() const
301 {
302 return IntSize( getTextureRealWidth(), getTextureRealHeight() );
303 }
304
305 int Canvas::getTextureSrcWidth() const
306 {
307 return mReqTexSize.width;
308 }
309
310 int Canvas::getTextureSrcHeight() const
311 {
312 return mReqTexSize.height;
313 }
314
315 IntSize Canvas::getTextureSrcSize() const
316 {
317 return mReqTexSize;
318 }
319
320 PixelFormat Canvas::getTextureFormat() const
321 {
322 return mTexture->getFormat();
323 }
324
325 const std::string& Canvas::getTextureName() const
326 {
327 return mTexture->getName();
328 }
329
330 void Canvas::setSize(int _width, int _height)
331 {
332 setSize(IntSize(_width, _height));
333 }
334
335 void Canvas::setCoord(int _left, int _top, int _width, int _height)
336 {
337 setCoord(IntCoord(_left, _top, _width, _height));
338 }
339
340 Canvas::TextureResizeMode Canvas::getResizeMode() const
341 {
342 return mTexResizeMode;
343 }
344
345 void Canvas::setResizeMode(TextureResizeMode _value)
346 {
347 mTexResizeMode = _value;
348 }
349
350 bool Canvas::isTextureCreated() const
351 {
352 return mTexture != nullptr;
353 }
354
355 bool Canvas::isTextureManaged() const
356 {
357 return mTexManaged;
358 }
359
360 ITexture* Canvas::getTexture() const
361 {
362 return mTexture;
363 }
364
365 void Canvas::setTextureManaged(bool _value)
366 {
367 mTexManaged = _value;
368 }
369
370 TextureUsage Canvas::getDefaultTextureUsage()
371 {
372 return TextureUsage::Stream | TextureUsage::Write;
373 }
374
375 PixelFormat Canvas::getDefaultTextureFormat()
376 {
377 return PixelFormat::R8G8B8A8;
378 }
379
380 } // namespace MyGUI
MyGUI::types::TCoord< int >
MyGUI::Canvas::getTextureSrcWidth
int getTextureSrcWidth() const
Returns needed width while creating texture.
Definition: MyGUI_Canvas.cpp:305
MyGUI::types::TSize::empty
bool empty() const
Definition: MyGUI_TSize.h:123
MyGUI::Canvas::_setUVSet
void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_Canvas.cpp:279
MyGUI::Canvas::requestUpdateCanvas
EventHandle_CanvasPtrEvent requestUpdateCanvas
Definition: MyGUI_Canvas.h:175
MyGUI_Canvas.h
MyGUI::Canvas::isLocked
bool isLocked() const
Checks lockness of hardware _pixel buffer.
Definition: MyGUI_Canvas.cpp:285
MyGUI::Canvas::checkCreate
bool checkCreate(int _width, int _height) const
Checks if we need to create a texture with such sizes.
Definition: MyGUI_Canvas.cpp:126
MyGUI::ITexture::createManual
virtual void createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format)=0
MyGUI::Canvas::frameEntered
void frameEntered(float _time)
For updating once per frame.
Definition: MyGUI_Canvas.cpp:245
MyGUI::Canvas::lock
void * lock(TextureUsage _usage=TextureUsage::Write)
Locks hardware pixel buffer.
Definition: MyGUI_Canvas.cpp:206
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:166
MyGUI::Canvas::getTextureRealHeight
int getTextureRealHeight() const
Returns real height of texture.
Definition: MyGUI_Canvas.cpp:295
MyGUI::Canvas::unlock
void unlock()
Unlocks hardware pixel buffer.
Definition: MyGUI_Canvas.cpp:215
MyGUI::Canvas::frameAdvise
void frameAdvise(bool _advise)
For updating once per frame.
Definition: MyGUI_Canvas.cpp:225
MyGUI::IntSize
types::TSize< int > IntSize
Definition: MyGUI_Types.h:44
MyGUI_Precompiled.h
MyGUI::ICroppedRectangle::getHeight
int getHeight() const
Definition: MyGUI_ICroppedRectangle.h:134
MyGUI::Canvas::getDefaultTextureFormat
static PixelFormat getDefaultTextureFormat()
Returns default GUI texture format.
Definition: MyGUI_Canvas.cpp:375
MyGUI::types::TSize< int >
MyGUI::newDelegate
delegates::IDelegate0 * newDelegate(void(*_func)())
Definition: MyGUI_Delegate.h:150
MyGUI::Singleton< RenderManager >::getInstance
static RenderManager & getInstance()
Definition: MyGUI_Singleton.h:53
MyGUI::types::TCoord::size
TSize< T > size() const
Definition: MyGUI_TCoord.h:205
MyGUI::Canvas::mFrameAdvise
bool mFrameAdvise
For updating once per frame. True state means updating before next frame starts.
Definition: MyGUI_Canvas.h:229
MyGUI::TextureUsage::Write
Definition: MyGUI_RenderFormat.h:98
MyGUI::Canvas::eventPreTextureChanges
EventHandle_CanvasPtr eventPreTextureChanges
Definition: MyGUI_Canvas.h:168
MyGUI::Canvas::Canvas
Canvas()
Definition: MyGUI_Canvas.cpp:32
MyGUI::Canvas::setSize
virtual void setSize(const IntSize &_value)
Definition: MyGUI_Canvas.cpp:106
MyGUI::Canvas::mTexture
ITexture * mTexture
Current texture.
Definition: MyGUI_Canvas.h:211
MyGUI::Canvas::correctUV
void correctUV()
Correct texture uv-coordinates.
Definition: MyGUI_Canvas.cpp:186
MyGUI::Canvas::createExactTexture
void createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
Creates the texture itself.
Definition: MyGUI_Canvas.cpp:59
MyGUI::ITexture::lock
virtual void * lock(TextureUsage _access)=0
MyGUI::ITexture::isLocked
virtual bool isLocked()=0
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:47
MyGUI::ISubWidgetRect::_setUVSet
virtual void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_ISubWidgetRect.h:40
MyGUI::ITexture::unlock
virtual void unlock()=0
MyGUI::SkinItem::_setTextureName
void _setTextureName(const std::string &_texture)
Definition: MyGUI_SkinItem.cpp:147
MyGUI::ITexture::getName
virtual const std::string & getName() const =0
MyGUI::Canvas::getResizeMode
TextureResizeMode getResizeMode() const
Returns resize mode.
Definition: MyGUI_Canvas.cpp:340
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:50
MyGUI::Canvas::getTextureSrcHeight
int getTextureSrcHeight() const
Returns needed height while creating texture.
Definition: MyGUI_Canvas.cpp:310
MyGUI::Canvas::updateTexture
void updateTexture()
Call user delegate update and removes old texture if it isn&#39;t original.
Definition: MyGUI_Canvas.cpp:120
MyGUI::SkinItem::getSubWidgetMain
ISubWidgetRect * getSubWidgetMain()
Definition: MyGUI_SkinItem.cpp:174
MyGUI::Canvas::getTextureFormat
PixelFormat getTextureFormat() const
Returns needed sizes while creating texture.
Definition: MyGUI_Canvas.cpp:320
MyGUI::Canvas::initialiseOverride
virtual void initialiseOverride()
Definition: MyGUI_Canvas.cpp:167
MyGUI::Canvas::textureInvalidate
virtual void textureInvalidate(ITexture *_texture)
Definition: MyGUI_Canvas.cpp:274
MyGUI_RenderManager.h
MyGUI::Canvas::setCoord
virtual void setCoord(const IntCoord &_value)
Definition: MyGUI_Canvas.cpp:113
MyGUI::TextureUsage
Definition: MyGUI_RenderFormat.h:89
MyGUI::ICroppedRectangle::getWidth
int getWidth() const
Definition: MyGUI_ICroppedRectangle.h:129
MyGUI::types::TRect< float >
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:35
MyGUI::Canvas::createTexture
void createTexture(TextureResizeMode _resizeMode, TextureUsage _usage=getDefaultTextureUsage(), PixelFormat _format=getDefaultTextureFormat())
Creates texture.
Definition: MyGUI_Canvas.cpp:43
MyGUI::Bitwise::firstPO2From
static __inline Type firstPO2From(Type _value)
Definition: MyGUI_Bitwise.h:36
MyGUI::Canvas::mReqTexSize
IntSize mReqTexSize
Requested bu user sizes.
Definition: MyGUI_Canvas.h:214
MyGUI::ITexture::getHeight
virtual int getHeight()=0
MyGUI::Canvas::getTextureSrcSize
IntSize getTextureSrcSize() const
Returns needed sizes while creating texture.
Definition: MyGUI_Canvas.cpp:315
MyGUI::Canvas::isTextureCreated
bool isTextureCreated() const
Returns true if the texture was created (and exists), otherwise false.
Definition: MyGUI_Canvas.cpp:350
MyGUI::utility::toString
std::string toString(T p)
Definition: MyGUI_StringUtility.h:42
MyGUI::ITexture
Definition: MyGUI_ITexture.h:41
MyGUI::Canvas::getDefaultTextureUsage
static TextureUsage getDefaultTextureUsage()
Returns default GUI texture usage.
Definition: MyGUI_Canvas.cpp:370
MyGUI::Canvas::resize
void resize(const IntSize &_size)
Calls when resize widget.
Definition: MyGUI_Canvas.cpp:77
MyGUI::Canvas::mTexResizeMode
TextureResizeMode mTexResizeMode
Texture resize mode.
Definition: MyGUI_Canvas.h:220
MyGUI::Canvas::setTextureManaged
void setTextureManaged(bool _value)
Sets the texture managed.
Definition: MyGUI_Canvas.cpp:365
MyGUI::Canvas::isTextureSrcSize
bool isTextureSrcSize() const
Checks if the texture has the source (required by user) size, otherwise real texture size are bigger...
Definition: MyGUI_Canvas.cpp:220
MyGUI_ResourceManager.h
MyGUI::Canvas::getTextureRealSize
IntSize getTextureRealSize() const
Returns real _size of texture.
Definition: MyGUI_Canvas.cpp:300
MyGUI::FloatRect
types::TRect< float > FloatRect
Definition: MyGUI_Types.h:48
MyGUI::Canvas::getTextureRealWidth
int getTextureRealWidth() const
Returns real width of texture.
Definition: MyGUI_Canvas.cpp:290
MyGUI::ITexture::getFormat
virtual PixelFormat getFormat()=0
MyGUI::Canvas::Event
Definition: MyGUI_Canvas.h:45
MyGUI::Canvas::validate
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...
Definition: MyGUI_Canvas.cpp:137
MyGUI::ITexture::getUsage
virtual TextureUsage getUsage()=0
MyGUI::Canvas::TRM_PT_VIEW_ALL
You can view all pixels of texture, texture cropped by sizes of widget.
Definition: MyGUI_Canvas.h:74
MyGUI::Canvas::TextureResizeMode
TextureResizeMode
Definition: MyGUI_Canvas.h:69
MyGUI::TextureUsage::Stream
Definition: MyGUI_RenderFormat.h:96
MyGUI::Canvas::_destroyTexture
void _destroyTexture(bool _sendEvent)
Destroys texture.
Definition: MyGUI_Canvas.cpp:171
MyGUI::PixelFormat
Definition: MyGUI_RenderFormat.h:59
MyGUI::Canvas::TRM_PT_CONST_SIZE
Definition: MyGUI_Canvas.h:72
MyGUI::Canvas::isTextureManaged
bool isTextureManaged() const
Returns true if we own the texture, otherwise false.
Definition: MyGUI_Canvas.cpp:355
MyGUI_Gui.h
MyGUI::Canvas::setResizeMode
void setResizeMode(TextureResizeMode _value)
Sets resize mode of texture.
Definition: MyGUI_Canvas.cpp:345
MyGUI_Bitwise.h
MyGUI::Canvas::getTextureName
const std::string & getTextureName() const
Returns name of the current texture.
Definition: MyGUI_Canvas.cpp:325
MyGUI::Canvas::shutdownOverride
virtual void shutdownOverride()
Definition: MyGUI_Canvas.cpp:161
MyGUI::Canvas::mTexManaged
bool mTexManaged
true if we own the texture (can delete it or replace by another instance), otherwise false ...
Definition: MyGUI_Canvas.h:226
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:36
MyGUI::PixelFormat::R8G8B8A8
Definition: MyGUI_RenderFormat.h:67
MyGUI::ITexture::setInvalidateListener
virtual void setInvalidateListener(ITextureInvalidateListener *_listener)
Definition: MyGUI_ITexture.h:52
MyGUI::uint8
unsigned char uint8
Definition: MyGUI_Types.h:61
MyGUI::RenderManager::createTexture
virtual ITexture * createTexture(const std::string &_name)=0
MyGUI::Canvas::getTexture
ITexture * getTexture() const
Reurns interface texture.
Definition: MyGUI_Canvas.cpp:360
MyGUI::Canvas::mGenTexName
std::string mGenTexName
Generated texture name.
Definition: MyGUI_Canvas.h:217
MyGUI::Canvas::mInvalidateData
bool mInvalidateData
Definition: MyGUI_Canvas.h:231
MyGUI::Canvas::destroyTexture
void destroyTexture()
Destroys texture.
Definition: MyGUI_Canvas.cpp:156
MyGUI::ITexture::getWidth
virtual int getWidth()=0
MyGUI::Canvas::mTexData
uint8 * mTexData
Saved pointer from last calling lock.
Definition: MyGUI_Canvas.h:223
MyGUI::Canvas::TRM_PT_VIEW_REQUESTED
Texture doesn&#39;t resizes and fills all widget space.
Definition: MyGUI_Canvas.h:73
MyGUI::RenderManager::destroyTexture
virtual void destroyTexture(ITexture *_texture)=0