MyGUI  3.0.1
MyGUI_Canvas.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_Canvas.h"
25 #include "MyGUI_ResourceManager.h"
26 #include "MyGUI_Gui.h"
27 #include "MyGUI_RenderManager.h"
28 #include "MyGUI_Bitwise.h"
29 
30 namespace MyGUI
31 {
32 
34  mTexture( nullptr ),
35  mTexResizeMode( TRM_PT_CONST_SIZE ),
36  mTexData( 0 ),
37  mTexManaged( true ),
38  mFrameAdvise( false )
39  {
40  mGenTexName = utility::toString( this, "_Canvas" );
41  }
42 
43  void Canvas::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, Widget* _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
44  {
45  Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
46  }
47 
49  {
50  _destroyTexture( false );
51  }
52 
53  void Canvas::createTexture( TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
54  {
55  createTexture( getSize(), _resizeMode, _usage, _format );
56  }
57 
58  void Canvas::createTexture( const IntSize& _size, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
59  {
60  if ( _size.width <= 0 || _size.height <= 0 )
61  {
62  MYGUI_ASSERT( 0, "At least one of dimensions isn't positive!" );
63  return;
64  }
65 
66  createTexture( _size.width, _size.height, _resizeMode, _usage, _format );
67  }
68 
69  void Canvas::createExactTexture( int _width, int _height, TextureUsage _usage, PixelFormat _format )
70  {
71  MYGUI_ASSERT( _width >= 0 && _height >= 0, "negative size" );
72 
74 
77  mTexture->createManual( _width, _height, _usage, _format );
78 
79  mTexManaged = true;
80 
82  correctUV();
83  requestUpdateCanvas( this, Event( true, true, false ) );
84  }
85 
86  void Canvas::resize( const IntSize& _size )
87  {
88  if ( _size.width <= 0 || _size.height <= 0 || ! mTexManaged )
89  return;
90 
91  mReqTexSize = _size;
92 
93  frameAdvise( true );
94  }
95 
96  void Canvas::createTexture( int _width, int _height, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
97  {
98  MYGUI_ASSERT( _width >= 0 && _height >= 0, "negative size" );
99 
100  if ( mReqTexSize.empty() )
101  mReqTexSize = IntSize( _width, _height );
102 
103  mTexResizeMode = _resizeMode;
104 
105  bool create = checkCreate( _width, _height );
106 
107  _width = Bitwise::firstPO2From(_width);
108  _height = Bitwise::firstPO2From(_height);
109 
110  if ( create )
111  createExactTexture( _width, _height, _usage, _format );
112  }
113 
114  void Canvas::setSize( const IntSize& _size )
115  {
116  resize( _size );
117 
118  Base::setSize( _size );
119  }
120 
121  void Canvas::setCoord( const IntCoord& _coord )
122  {
123  resize( _coord.size() );
124 
125  Base::setCoord( _coord );
126  }
127 
129  {
130  requestUpdateCanvas( this, Event( false, false, true ) );
131  }
132 
133  bool Canvas::checkCreate( int _width, int _height ) const
134  {
135  if ( mTexture == nullptr )
136  return true;
137 
138  if ( mTexture->getWidth() >= _width && mTexture->getHeight() >= _height )
139  return false;
140 
141  return true;
142  }
143 
144  void Canvas::validate( int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format ) const
145  {
146  _width = Bitwise::firstPO2From(_width);
147  _height = Bitwise::firstPO2From(_height);
148 
149  // restore usage and format
150  if ( mTexture != nullptr )
151  {
152  if ( _usage == getDefaultTextureUsage() )
153  _usage = mTexture->getUsage();
154 
155  if ( _format == getDefaultTextureFormat() )
156  _format = mTexture->getFormat();
157  }
158  }
159 
161  {
162  _destroyTexture( true );
163  }
164 
165  void Canvas::_destroyTexture( bool _sendEvent )
166  {
167  if ( mTexture != nullptr )
168  {
169  if ( _sendEvent )
170  {
171  eventPreTextureChanges( this );
172  }
173 
175  mTexture = nullptr;
176  }
177 
178  }
179 
181  {
183  {
184  _setUVSet( FloatRect( 0, 0,
185  (float) mReqTexSize.width / (float) getTextureRealWidth(),
186  (float) mReqTexSize.height / (float) getTextureRealHeight()
187  ) );
188  }
189 
191  {
192  _setUVSet( FloatRect( 0, 0, 1, 1 ) );
193  }
194  }
195 
197  {
198  void* data = mTexture->lock(_usage);
199 
200  mTexData = reinterpret_cast< uint8* >( data );
201 
202  return data;
203  }
204 
206  {
207  mTexture->unlock();
208  }
209 
211  {
213  }
214 
216  {
217  }
218 
220  {
221  }
222 
224  {
226  }
227 
228  void Canvas::frameAdvise( bool _advise )
229  {
230  if ( _advise )
231  {
232  if ( ! mFrameAdvise )
233  {
235  mFrameAdvise = true;
236  }
237  }
238  else
239  {
240  if ( mFrameAdvise )
241  {
243  mFrameAdvise = false;
244  }
245  }
246  }
247 
248  void Canvas::frameEntered( float _time )
249  {
250  int width = mReqTexSize.width;
251  int height = mReqTexSize.height;
254 
255  validate( width, height, usage, format );
256 
257  bool create = checkCreate( width, height );
258 
260  create = false;
261 
262  if ( create )
263  {
264  createExactTexture( width, height, usage, format );
265  correctUV();
266  }
267  else // I thought order is important
268  {
269  correctUV();
270  requestUpdateCanvas( this, Event( false, true, false ) );
271  }
272 
273  frameAdvise( false );
274  }
275 
277  {
278  updateTexture();
279  }
280 
281 } // namespace MyGUI