MyGUI  3.2.0
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 
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 
65 
68  mTexture->createManual( width, height, _usage, _format );
69 
70  mTexManaged = true;
71 
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 
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 
157  {
158  _destroyTexture( true );
159  }
160 
162  {
163  _destroyTexture(false);
164  frameAdvise(false);
165  }
166 
168  {
169  }
170 
171  void Canvas::_destroyTexture( bool _sendEvent )
172  {
173  if ( mTexture != nullptr )
174  {
175  if ( _sendEvent )
176  {
177  eventPreTextureChanges( this );
178  }
179 
181  mTexture = nullptr;
182  }
183 
184  }
185 
187  {
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 
201  {
202  _setUVSet( FloatRect( 0, 0, 1, 1 ) );
203  }
204  }
205 
207  {
208  void* data = mTexture->lock(_usage);
209 
210  mTexData = reinterpret_cast< uint8* >( data );
211 
212  return data;
213  }
214 
216  {
217  mTexture->unlock();
218  }
219 
221  {
223  }
224 
225  void Canvas::frameAdvise( bool _advise )
226  {
227  if ( _advise )
228  {
229  if ( ! mFrameAdvise )
230  {
232  mFrameAdvise = true;
233  }
234  }
235  else
236  {
237  if ( mFrameAdvise )
238  {
240  mFrameAdvise = false;
241  }
242  }
243  }
244 
245  void Canvas::frameEntered( float _time )
246  {
247  int width = mReqTexSize.width;
248  int height = mReqTexSize.height;
251 
252  validate( width, height, usage, format );
253 
254  bool create = checkCreate( width, height );
255 
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 
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 
291  {
292  return (int) mTexture->getWidth();
293  }
294 
296  {
297  return (int) mTexture->getHeight();
298  }
299 
301  {
303  }
304 
306  {
307  return mReqTexSize.width;
308  }
309 
311  {
312  return mReqTexSize.height;
313  }
314 
316  {
317  return mReqTexSize;
318  }
319 
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 
341  {
342  return mTexResizeMode;
343  }
344 
346  {
347  mTexResizeMode = _value;
348  }
349 
351  {
352  return mTexture != nullptr;
353  }
354 
356  {
357  return mTexManaged;
358  }
359 
361  {
362  return mTexture;
363  }
364 
365  void Canvas::setTextureManaged(bool _value)
366  {
367  mTexManaged = _value;
368  }
369 
371  {
373  }
374 
376  {
377  return PixelFormat::R8G8B8A8;
378  }
379 
380 } // namespace MyGUI
int getTextureSrcWidth() const
Returns needed width while creating texture.
bool empty() const
Definition: MyGUI_TSize.h:123
void _setUVSet(const FloatRect &_rect)
EventHandle_CanvasPtrEvent requestUpdateCanvas
Definition: MyGUI_Canvas.h:175
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:166
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:44
static PixelFormat getDefaultTextureFormat()
Returns default GUI texture format.
delegates::IDelegate0 * newDelegate(void(*_func)())
static RenderManager & getInstance()
TSize< T > size() const
Definition: MyGUI_TCoord.h:205
bool mFrameAdvise
For updating once per frame. True state means updating before next frame starts.
Definition: MyGUI_Canvas.h:229
EventHandle_CanvasPtr eventPreTextureChanges
Definition: MyGUI_Canvas.h:168
virtual void setSize(const IntSize &_value)
ITexture * mTexture
Current texture.
Definition: MyGUI_Canvas.h:211
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)
virtual const std::string & getName() const =0
TextureResizeMode getResizeMode() const
Returns resize mode.
types::TCoord< int > IntCoord
Definition: MyGUI_Types.h:50
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)
void createTexture(TextureResizeMode _resizeMode, TextureUsage _usage=getDefaultTextureUsage(), PixelFormat _format=getDefaultTextureFormat())
Creates texture.
static __inline Type firstPO2From(Type _value)
Definition: MyGUI_Bitwise.h:36
IntSize mReqTexSize
Requested bu user sizes.
Definition: MyGUI_Canvas.h:214
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:220
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:48
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
You can view all pixels of texture, texture cropped by sizes of widget.
Definition: MyGUI_Canvas.h:74
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:226
virtual void setInvalidateListener(ITextureInvalidateListener *_listener)
unsigned char uint8
Definition: MyGUI_Types.h:61
virtual ITexture * createTexture(const std::string &_name)=0
ITexture * getTexture() const
Reurns interface texture.
std::string mGenTexName
Generated texture name.
Definition: MyGUI_Canvas.h:217
bool mInvalidateData
Definition: MyGUI_Canvas.h:231
void destroyTexture()
Destroys texture.
virtual int getWidth()=0
uint8 * mTexData
Saved pointer from last calling lock.
Definition: MyGUI_Canvas.h:223
Texture doesn&#39;t resizes and fills all widget space.
Definition: MyGUI_Canvas.h:73
virtual void destroyTexture(ITexture *_texture)=0