MyGUI 3.4.1
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
36 void Canvas::createTexture( const IntSize& _size, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
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
44 void Canvas::createExactTexture( int _width, int _height, TextureUsage _usage, PixelFormat _format )
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
62 void Canvas::resize( const IntSize& _size )
63 {
64 if ( _size.width <= 0 || _size.height <= 0 || ! mTexManaged )
65 return;
66
67 mReqTexSize = _size;
68
69 frameAdvise( true );
70 }
71
72 void Canvas::createTexture( int _width, int _height, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
73 {
74 mTexResizeMode = _resizeMode;
75
76 int width = std::max(1, _width);
77 int height = std::max(1, _height);
78
79 if (_resizeMode == TRM_PT_CONST_SIZE)
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
97 void Canvas::setSize( const IntSize& _size )
98 {
99 resize( _size );
100
101 Base::setSize( _size );
102 }
103
104 void Canvas::setCoord( const IntCoord& _coord )
105 {
106 resize( _coord.size() );
107
108 Base::setCoord( _coord );
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
128 void Canvas::validate( int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format ) const
129 {
130 _width = std::max(1, _width);
131 _height = std::max(1, _height);
132
133 _width = Bitwise::firstPO2From(_width);
134 _height = Bitwise::firstPO2From(_height);
135
136 // restore usage and format
137 if ( mTexture != nullptr )
138 {
139 if ( _usage == getDefaultTextureUsage() )
140 _usage = mTexture->getUsage();
141
142 if ( _format == getDefaultTextureFormat() )
143 _format = mTexture->getFormat();
144 }
145 }
146
148 {
149 _destroyTexture( true );
150 }
151
153 {
154 _destroyTexture(false);
155 frameAdvise(false);
156 }
157
159 {
160 }
161
162 void Canvas::_destroyTexture( bool _sendEvent )
163 {
164 if ( mTexture != nullptr )
165 {
166 if ( _sendEvent )
167 {
169 }
170
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
214 void Canvas::frameAdvise( bool _advise )
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
234 void Canvas::frameEntered( float _time )
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
268 void Canvas::_setUVSet(const FloatRect& _rect)
269 {
270 if (nullptr != getSubWidgetMain())
271 getSubWidgetMain()->_setUVSet(_rect);
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 {
326 mTexResizeMode = _value;
327 }
328
330 {
331 return mTexture != nullptr;
332 }
333
335 {
336 return mTexManaged;
337 }
338
340 {
341 return mTexture;
342 }
343
345 {
346 mTexManaged = _value;
347 }
348
350 {
352 }
353
355 {
357 }
358
359} // namespace MyGUI
static Type firstPO2From(Type _value)
Definition: MyGUI_Bitwise.h:21
IntSize mReqTexSize
Requested bu user sizes.
Definition: MyGUI_Canvas.h:227
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.
Definition: MyGUI_Canvas.h:242
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
Definition: MyGUI_Canvas.h:188
bool mTexManaged
true if we own the texture (can delete it or replace by another instance), otherwise false
Definition: MyGUI_Canvas.h:239
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.
Definition: MyGUI_Canvas.h:233
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.
Definition: MyGUI_Canvas.h:236
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
bool mInvalidateData
Definition: MyGUI_Canvas.h:244
TextureResizeMode getResizeMode() const
Returns resize mode.
ITexture * mTexture
Current texture.
Definition: MyGUI_Canvas.h:224
EventHandle_CanvasPtr eventPreTextureChanges
Definition: MyGUI_Canvas.h:181
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.
Definition: MyGUI_Canvas.h:230
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()
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:150
virtual void _setUVSet(const FloatRect &_rect)
virtual PixelFormat getFormat() const =0
virtual void setInvalidateListener(ITextureInvalidateListener *_listener)
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
virtual ITexture * createTexture(const std::string &_name)=0
virtual void destroyTexture(ITexture *_texture)=0
static RenderManager & getInstance()
ISubWidgetRect * getSubWidgetMain() const
void _setTextureName(const std::string &_texture)
std::string toString(T p)
uint8_t uint8
Definition: MyGUI_Types.h:45
types::TSize< int > IntSize
Definition: MyGUI_Types.h:29
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
types::TRect< float > FloatRect
Definition: MyGUI_Types.h:33
TSize< T > size() const
Definition: MyGUI_TCoord.h:190