MyGUI  3.2.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 
14 namespace MyGUI
15 {
16 
18  mTexture( nullptr ),
19  mTexResizeMode( TRM_PT_CONST_SIZE ),
20  mTexData( 0 ),
21  mTexManaged( true ),
22  mFrameAdvise( false ),
23  mInvalidateData(false)
24  {
25  mGenTexName = utility::toString((size_t)this, "_Canvas");
26  }
27 
28  void Canvas::createTexture( TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
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  {
168  eventPreTextureChanges( this );
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  {
265  updateTexture();
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 
319  void Canvas::setSize(int _width, int _height)
320  {
321  setSize(IntSize(_width, _height));
322  }
323 
324  void Canvas::setCoord(int _left, int _top, int _width, int _height)
325  {
326  setCoord(IntCoord(_left, _top, _width, _height));
327  }
328 
330  {
331  return mTexResizeMode;
332  }
333 
335  {
336  mTexResizeMode = _value;
337  }
338 
340  {
341  return mTexture != nullptr;
342  }
343 
345  {
346  return mTexManaged;
347  }
348 
350  {
351  return mTexture;
352  }
353 
354  void Canvas::setTextureManaged(bool _value)
355  {
356  mTexManaged = _value;
357  }
358 
360  {
362  }
363 
365  {
366  return PixelFormat::R8G8B8A8;
367  }
368 
369 } // namespace MyGUI
int getTextureSrcWidth() const
Returns needed width while creating texture.
void _setUVSet(const FloatRect &_rect)
EventHandle_CanvasPtrEvent requestUpdateCanvas
Definition: MyGUI_Canvas.h:189
bool isLocked() const
Checks lockness of hardware _pixel buffer.
bool checkCreate(int _width, int _height) const
Checks if we need to create a texture with such sizes.
virtual void createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format)=0
void frameEntered(float _time)
For updating once per frame.
void * lock(TextureUsage _usage=TextureUsage::Write)
Locks hardware pixel buffer.
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:150
int getTextureRealHeight() const
Returns real height of texture.
void unlock()
Unlocks hardware pixel buffer.
void frameAdvise(bool _advise)
For updating once per frame.
types::TSize< int > IntSize
Definition: MyGUI_Types.h:29
static PixelFormat getDefaultTextureFormat()
Returns default GUI texture format.
delegates::IDelegate0 * newDelegate(void(*_func)())
static RenderManager & getInstance()
TSize< T > size() const
Definition: MyGUI_TCoord.h:190
bool mFrameAdvise
For updating once per frame. True state means updating before next frame starts.
Definition: MyGUI_Canvas.h:243
EventHandle_CanvasPtr eventPreTextureChanges
Definition: MyGUI_Canvas.h:182
virtual void setSize(const IntSize &_value)
ITexture * mTexture
Current texture.
Definition: MyGUI_Canvas.h:225
void correctUV()
Correct texture uv-coordinates.
void createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
Creates the texture itself.
virtual void * lock(TextureUsage _access)=0
virtual bool isLocked()=0
#define nullptr
virtual void _setUVSet(const FloatRect &_rect)
virtual void unlock()=0
void _setTextureName(const std::string &_texture)
TextureResizeMode getResizeMode() const
Returns resize mode.
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:35
int getTextureSrcHeight() const
Returns needed height while creating texture.
void updateTexture()
Call user delegate update and removes old texture if it isn&#39;t original.
ISubWidgetRect * getSubWidgetMain()
PixelFormat getTextureFormat() const
Returns needed sizes while creating texture.
virtual void initialiseOverride()
virtual void textureInvalidate(ITexture *_texture)
virtual void setCoord(const IntCoord &_value)
virtual const std::string & getName() const =0
void createTexture(TextureResizeMode _resizeMode, TextureUsage _usage=getDefaultTextureUsage(), PixelFormat _format=getDefaultTextureFormat())
Creates texture.
static __inline Type firstPO2From(Type _value)
Definition: MyGUI_Bitwise.h:21
IntSize mReqTexSize
Requested bu user sizes.
Definition: MyGUI_Canvas.h:228
virtual int getHeight()=0
IntSize getTextureSrcSize() const
Returns needed sizes while creating texture.
bool isTextureCreated() const
Returns true if the texture was created (and exists), otherwise false.
std::string toString(T p)
static TextureUsage getDefaultTextureUsage()
Returns default GUI texture usage.
void resize(const IntSize &_size)
Calls when resize widget.
TextureResizeMode mTexResizeMode
Texture resize mode.
Definition: MyGUI_Canvas.h:234
void setTextureManaged(bool _value)
Sets the texture managed.
bool isTextureSrcSize() const
Checks if the texture has the source (required by user) size, otherwise real texture size are bigger...
IntSize getTextureRealSize() const
Returns real _size of texture.
types::TRect< float > FloatRect
Definition: MyGUI_Types.h:33
int getTextureRealWidth() const
Returns real width of texture.
virtual PixelFormat getFormat()=0
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...
virtual TextureUsage getUsage()=0
void _destroyTexture(bool _sendEvent)
Destroys texture.
bool isTextureManaged() const
Returns true if we own the texture, otherwise false.
void setResizeMode(TextureResizeMode _value)
Sets resize mode of texture.
const std::string & getTextureName() const
Returns name of the current texture.
virtual void shutdownOverride()
bool mTexManaged
true if we own the texture (can delete it or replace by another instance), otherwise false ...
Definition: MyGUI_Canvas.h:240
virtual void setInvalidateListener(ITextureInvalidateListener *_listener)
unsigned char uint8
Definition: MyGUI_Types.h:46
virtual ITexture * createTexture(const std::string &_name)=0
ITexture * getTexture() const
Reurns interface texture.
std::string mGenTexName
Generated texture name.
Definition: MyGUI_Canvas.h:231
bool mInvalidateData
Definition: MyGUI_Canvas.h:245
void destroyTexture()
Destroys texture.
virtual int getWidth()=0
uint8 * mTexData
Saved pointer from last calling lock.
Definition: MyGUI_Canvas.h:237
virtual void destroyTexture(ITexture *_texture)=0