MyGUI  3.4.0
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(nullptr),
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 
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 
344  void Canvas::setTextureManaged(bool _value)
345  {
346  mTexManaged = _value;
347  }
348 
350  {
352  }
353 
355  {
356  return PixelFormat::R8G8B8A8;
357  }
358 
359 } // namespace MyGUI
MyGUI::Singleton< RenderManager >::getInstance
static RenderManager & getInstance()
Definition: MyGUI_Singleton.h:44
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition: MyGUI_Gui.h:150
MyGUI::utility::toString
std::string toString(T p)
Definition: MyGUI_StringUtility.h:27
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:209
MyGUI::ITexture::setInvalidateListener
virtual void setInvalidateListener(ITextureInvalidateListener *_listener)
Definition: MyGUI_ITexture.h:38
MyGUI_Gui.h
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:21
MyGUI::Canvas::lock
void * lock(TextureUsage _usage=TextureUsage::Write)
Locks hardware pixel buffer.
Definition: MyGUI_Canvas.cpp:195
MyGUI::Canvas::getTextureSrcWidth
int getTextureSrcWidth() const
Returns needed width while creating texture.
Definition: MyGUI_Canvas.cpp:294
MyGUI::Canvas::mInvalidateData
bool mInvalidateData
Definition: MyGUI_Canvas.h:244
MyGUI::TextureUsage::Write
@ Write
Definition: MyGUI_RenderFormat.h:93
MyGUI::Canvas::frameEntered
void frameEntered(float _time)
For updating once per frame.
Definition: MyGUI_Canvas.cpp:234
MyGUI::ITexture::getWidth
virtual int getWidth()=0
MyGUI_Canvas.h
MyGUI::Canvas::frameAdvise
void frameAdvise(bool _advise)
For updating once per frame.
Definition: MyGUI_Canvas.cpp:214
MyGUI::Bitwise::firstPO2From
static Type firstPO2From(Type _value)
Definition: MyGUI_Bitwise.h:21
MyGUI::Canvas::getTextureRealWidth
int getTextureRealWidth() const
Returns real width of texture.
Definition: MyGUI_Canvas.cpp:279
MyGUI::ITexture::createManual
virtual void createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format)=0
MyGUI::Canvas::Canvas
Canvas()
Definition: MyGUI_Canvas.cpp:17
MyGUI_ResourceManager.h
MyGUI::Canvas::createExactTexture
void createExactTexture(int _width, int _height, TextureUsage _usage, PixelFormat _format)
Creates the texture itself.
Definition: MyGUI_Canvas.cpp:44
MyGUI::Canvas::getDefaultTextureFormat
static PixelFormat getDefaultTextureFormat()
Returns default GUI texture format.
Definition: MyGUI_Canvas.cpp:354
MyGUI::ITexture::unlock
virtual void unlock()=0
MyGUI::Canvas::initialiseOverride
void initialiseOverride() override
Definition: MyGUI_Canvas.cpp:158
MyGUI::uint8
unsigned char uint8
Definition: MyGUI_Types.h:44
MyGUI::types::TRect< float >
MyGUI::Canvas::eventPreTextureChanges
EventHandle_CanvasPtr eventPreTextureChanges
Definition: MyGUI_Canvas.h:181
MyGUI::ITexture::lock
virtual void * lock(TextureUsage _access)=0
MyGUI::ITexture::isLocked
virtual bool isLocked()=0
MyGUI::Canvas::getTextureName
const std::string & getTextureName() const
Returns name of the current texture.
Definition: MyGUI_Canvas.cpp:314
MyGUI::Canvas::textureInvalidate
void textureInvalidate(ITexture *_texture) override
Definition: MyGUI_Canvas.cpp:263
MyGUI::Canvas::getTextureRealSize
IntSize getTextureRealSize() const
Returns real _size of texture.
Definition: MyGUI_Canvas.cpp:289
MyGUI::Canvas::updateTexture
void updateTexture()
Call user delegate update and removes old texture if it isn't original.
Definition: MyGUI_Canvas.cpp:111
MyGUI::Canvas::getTexture
ITexture * getTexture() const
Reurns interface texture.
Definition: MyGUI_Canvas.cpp:339
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:20
MyGUI::Canvas::isTextureManaged
bool isTextureManaged() const
Returns true if we own the texture, otherwise false.
Definition: MyGUI_Canvas.cpp:334
MyGUI::RenderManager::createTexture
virtual ITexture * createTexture(const std::string &_name)=0
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:128
MyGUI::SkinItem::_setTextureName
void _setTextureName(const std::string &_texture)
Definition: MyGUI_SkinItem.cpp:133
MyGUI::Canvas::correctUV
void correctUV()
Correct texture uv-coordinates.
Definition: MyGUI_Canvas.cpp:177
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition: MyGUI_Delegate.h:99
MyGUI_Precompiled.h
MyGUI::RenderManager::destroyTexture
virtual void destroyTexture(ITexture *_texture)=0
MyGUI::Canvas::mFrameAdvise
bool mFrameAdvise
For updating once per frame. True state means updating before next frame starts.
Definition: MyGUI_Canvas.h:242
MyGUI::Canvas::getTextureSrcSize
IntSize getTextureSrcSize() const
Returns needed sizes while creating texture.
Definition: MyGUI_Canvas.cpp:304
MyGUI::Canvas::isLocked
bool isLocked() const
Checks lockness of hardware _pixel buffer.
Definition: MyGUI_Canvas.cpp:274
MyGUI::ITexture::getName
virtual const std::string & getName() const =0
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:117
MyGUI::Canvas::resize
void resize(const IntSize &_size)
Calls when resize widget.
Definition: MyGUI_Canvas.cpp:62
MyGUI::ITexture::getHeight
virtual int getHeight()=0
MyGUI::Canvas::mTexture
ITexture * mTexture
Current texture.
Definition: MyGUI_Canvas.h:224
MyGUI::ITexture::getUsage
virtual TextureUsage getUsage()=0
MyGUI::PixelFormat::R8G8B8A8
@ R8G8B8A8
Definition: MyGUI_RenderFormat.h:57
MyGUI::Canvas::setCoord
void setCoord(const IntCoord &_value) override
Definition: MyGUI_Widget.cpp:684
MyGUI::Canvas::_destroyTexture
void _destroyTexture(bool _sendEvent)
Destroys texture.
Definition: MyGUI_Canvas.cpp:162
MyGUI::SkinItem::getSubWidgetMain
ISubWidgetRect * getSubWidgetMain()
Definition: MyGUI_SkinItem.cpp:160
MyGUI::ISubWidgetRect::_setUVSet
virtual void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_ISubWidgetRect.h:23
MyGUI::Canvas::setTextureManaged
void setTextureManaged(bool _value)
Sets the texture managed.
Definition: MyGUI_Canvas.cpp:344
MyGUI::FloatRect
types::TRect< float > FloatRect
Definition: MyGUI_Types.h:33
MyGUI::Canvas::isTextureCreated
bool isTextureCreated() const
Returns true if the texture was created (and exists), otherwise false.
Definition: MyGUI_Canvas.cpp:329
MyGUI::Canvas::setResizeMode
void setResizeMode(TextureResizeMode _value)
Sets resize mode of texture.
Definition: MyGUI_Canvas.cpp:324
MyGUI::types::TSize< int >
MyGUI::Canvas::TRM_PT_CONST_SIZE
@ TRM_PT_CONST_SIZE
Definition: MyGUI_Canvas.h:61
MyGUI::Canvas::createTexture
void createTexture(TextureResizeMode _resizeMode, TextureUsage _usage=getDefaultTextureUsage(), PixelFormat _format=getDefaultTextureFormat())
Creates texture.
Definition: MyGUI_Canvas.cpp:28
MyGUI::Canvas::mReqTexSize
IntSize mReqTexSize
Requested bu user sizes.
Definition: MyGUI_Canvas.h:227
MyGUI::ITexture
Definition: MyGUI_ITexture.h:28
MyGUI::ITexture::getFormat
virtual PixelFormat getFormat()=0
MyGUI::TextureUsage::Stream
@ Stream
Definition: MyGUI_RenderFormat.h:91
MyGUI_RenderManager.h
MyGUI::Canvas::destroyTexture
void destroyTexture()
Destroys texture.
Definition: MyGUI_Canvas.cpp:147
MyGUI::ICroppedRectangle::getHeight
int getHeight() const
Definition: MyGUI_ICroppedRectangle.h:119
MyGUI::Canvas::getResizeMode
TextureResizeMode getResizeMode() const
Returns resize mode.
Definition: MyGUI_Canvas.cpp:319
MyGUI::IntSize
types::TSize< int > IntSize
Definition: MyGUI_Types.h:29
MyGUI::Canvas::mTexData
uint8 * mTexData
Saved pointer from last calling lock.
Definition: MyGUI_Canvas.h:236
MyGUI::Canvas::TRM_PT_VIEW_REQUESTED
@ TRM_PT_VIEW_REQUESTED
Definition: MyGUI_Canvas.h:75
MyGUI::Canvas::TextureResizeMode
TextureResizeMode
Definition: MyGUI_Canvas.h:54
MyGUI::Canvas::Event
Definition: MyGUI_Canvas.h:31
MyGUI::types::TCoord< int >
MyGUI::Canvas::TRM_PT_VIEW_ALL
@ TRM_PT_VIEW_ALL
Definition: MyGUI_Canvas.h:88
MyGUI::ICroppedRectangle::getWidth
int getWidth() const
Definition: MyGUI_ICroppedRectangle.h:114
MyGUI::Canvas::getDefaultTextureUsage
static TextureUsage getDefaultTextureUsage()
Returns default GUI texture usage.
Definition: MyGUI_Canvas.cpp:349
MyGUI::Canvas::getTextureRealHeight
int getTextureRealHeight() const
Returns real height of texture.
Definition: MyGUI_Canvas.cpp:284
MyGUI::PixelFormat
Definition: MyGUI_RenderFormat.h:50
MyGUI::Canvas::requestUpdateCanvas
EventHandle_CanvasPtrEvent requestUpdateCanvas
Definition: MyGUI_Canvas.h:188
MyGUI::Canvas::mTexResizeMode
TextureResizeMode mTexResizeMode
Texture resize mode.
Definition: MyGUI_Canvas.h:233
MyGUI::Canvas::mGenTexName
std::string mGenTexName
Generated texture name.
Definition: MyGUI_Canvas.h:230
MyGUI
Definition: MyGUI_ActionController.h:15
MyGUI::Canvas::_setUVSet
void _setUVSet(const FloatRect &_rect)
Definition: MyGUI_Canvas.cpp:268
MyGUI::Canvas::getTextureSrcHeight
int getTextureSrcHeight() const
Returns needed height while creating texture.
Definition: MyGUI_Canvas.cpp:299
MyGUI_Bitwise.h
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:239
MyGUI::Canvas::shutdownOverride
void shutdownOverride() override
Definition: MyGUI_Canvas.cpp:152
MyGUI::types::TCoord::size
TSize< T > size() const
Definition: MyGUI_TCoord.h:190
MyGUI::Canvas::unlock
void unlock()
Unlocks hardware pixel buffer.
Definition: MyGUI_Canvas.cpp:204
MyGUI::Canvas::getTextureFormat
PixelFormat getTextureFormat() const
Returns needed sizes while creating texture.
Definition: MyGUI_Canvas.cpp:309
MyGUI::TextureUsage
Definition: MyGUI_RenderFormat.h:85
MyGUI::Canvas::setSize
void setSize(const IntSize &_value) override
Definition: MyGUI_Widget.cpp:647