• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

MyGUI_Canvas.cpp

Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_Canvas.h"
00025 #include "MyGUI_ResourceManager.h"
00026 #include "MyGUI_Gui.h"
00027 #include "MyGUI_RenderManager.h"
00028 #include "MyGUI_Bitwise.h"
00029 
00030 namespace MyGUI
00031 {
00032 
00033     Canvas::Canvas() :
00034         mTexture( nullptr ),
00035         mTexResizeMode( TRM_PT_CONST_SIZE ),
00036         mTexData( 0 ),
00037         mTexManaged( true ),
00038         mFrameAdvise( false )
00039     {
00040         mGenTexName = utility::toString( this, "_Canvas" );
00041     }
00042 
00043     void Canvas::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, WidgetPtr _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
00044     {
00045         Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
00046     }
00047 
00048     Canvas::~Canvas()
00049     {
00050         _destroyTexture( false );
00051     }
00052 
00053     void Canvas::createTexture( TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
00054     {
00055         createTexture( getSize(), _resizeMode, _usage, _format );
00056     }
00057 
00058     void Canvas::createTexture( const IntSize& _size, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
00059     {
00060         if ( _size.width <= 0 || _size.height <= 0 )
00061         {
00062             MYGUI_ASSERT( 0, "At least one of dimensions isn't positive!" );
00063             return;
00064         }
00065 
00066         createTexture( _size.width, _size.height, _resizeMode, _usage, _format );
00067     }
00068 
00069     void Canvas::createExactTexture( int _width, int _height, TextureUsage _usage, PixelFormat _format )
00070     {
00071         MYGUI_ASSERT( _width >= 0 && _height >= 0, "negative size" );
00072 
00073         destroyTexture();
00074 
00075         mTexture = RenderManager::getInstance().createTexture(mGenTexName);
00076         mTexture->createManual( _width, _height, _usage, _format );
00077 
00078         mTexManaged = true;
00079 
00080         _setTextureName( mGenTexName );
00081         correctUV();
00082         requestUpdateCanvas( this, Event( true, true, false ) );
00083     }
00084 
00085     void Canvas::resize( const IntSize& _size )
00086     {
00087         if ( _size.width <= 0 || _size.height <= 0 || ! mTexManaged )
00088             return;
00089 
00090         mReqTexSize = _size;
00091 
00092         frameAdvise( true );
00093     }
00094 
00095     void Canvas::createTexture( int _width, int _height, TextureResizeMode _resizeMode, TextureUsage _usage, PixelFormat _format )
00096     {
00097         MYGUI_ASSERT( _width >= 0 && _height >= 0, "negative size" );
00098 
00099         if ( mReqTexSize.empty() )
00100             mReqTexSize = IntSize( _width, _height );
00101 
00102         mTexResizeMode = _resizeMode;
00103 
00104         bool create = checkCreate( _width, _height );
00105 
00106         _width = Bitwise::firstPO2From(_width);
00107         _height = Bitwise::firstPO2From(_height);
00108 
00109         if ( create )
00110             createExactTexture( _width, _height, _usage, _format );
00111     }
00112 
00113     void Canvas::setSize( const IntSize& _size )
00114     {
00115         resize( _size );
00116 
00117         Base::setSize( _size );
00118     }
00119 
00120     void Canvas::setCoord( const IntCoord& _coord )
00121     {
00122         resize( _coord.size() );
00123 
00124         Base::setCoord( _coord );
00125     }
00126 
00127     void Canvas::updateTexture()
00128     {
00129         requestUpdateCanvas( this, Event( false, false, true ) );
00130     }
00131 
00132     bool Canvas::checkCreate( int _width, int _height ) const
00133     {
00134         if ( mTexture == nullptr )
00135             return true;
00136 
00137         if ( mTexture->getWidth() >= _width && mTexture->getHeight() >= _height )
00138             return false;
00139 
00140         return true;
00141     }
00142 
00143     void Canvas::validate( int& _width, int& _height, TextureUsage& _usage, PixelFormat& _format ) const
00144     {
00145         _width = Bitwise::firstPO2From(_width);
00146         _height = Bitwise::firstPO2From(_height);
00147 
00148         // restore usage and format
00149         if ( mTexture != nullptr )
00150         {
00151             if ( _usage == getDefaultTextureUsage() )
00152                 _usage = mTexture->getUsage();
00153 
00154             if ( _format == getDefaultTextureFormat() )
00155                 _format = mTexture->getFormat();
00156         }
00157     }
00158 
00159     void Canvas::destroyTexture()
00160     {
00161         _destroyTexture( true );
00162     }
00163 
00164     void Canvas::_destroyTexture( bool _sendEvent )
00165     {
00166         if ( mTexture != nullptr )
00167         {
00168             if ( _sendEvent )
00169             {
00170                 eventPreTextureChanges( this );
00171             }
00172 
00173             RenderManager::getInstance().destroyTexture( mTexture );
00174             mTexture = nullptr;
00175         }
00176 
00177     }
00178 
00179     void Canvas::correctUV()
00180     {
00181         if ( mTexResizeMode == TRM_PT_VIEW_REQUESTED )
00182         {
00183             _setUVSet( FloatRect( 0, 0,
00184                 (float) mReqTexSize.width  / (float) getTextureRealWidth(),
00185                 (float) mReqTexSize.height / (float) getTextureRealHeight()
00186                 ) );
00187         }
00188 
00189         if ( mTexResizeMode == TRM_PT_CONST_SIZE || mTexResizeMode == TRM_PT_VIEW_ALL )
00190         {
00191             _setUVSet( FloatRect( 0, 0, 1, 1 ) );
00192         }
00193     }
00194 
00195     void* Canvas::lock(TextureUsage _usage)
00196     {
00197         void* data = mTexture->lock(_usage);
00198 
00199         mTexData = reinterpret_cast< uint8* >( data );
00200 
00201         return data;
00202     }
00203 
00204     void Canvas::unlock()
00205     {
00206         mTexture->unlock();
00207     }
00208 
00209     void Canvas::baseChangeWidgetSkin( ResourceSkin* _info )
00210     {
00211         Base::baseChangeWidgetSkin( _info );
00212     }
00213 
00214     void Canvas::initialiseWidgetSkin( ResourceSkin* _info )
00215     {
00216     }
00217 
00218     void Canvas::shutdownWidgetSkin()
00219     {
00220     }
00221 
00222     bool Canvas::isTextureSrcSize() const
00223     {
00224         return getTextureSrcSize() == getTextureRealSize();
00225     }
00226 
00227     void Canvas::frameAdvise( bool _advise )
00228     {
00229         if ( _advise )
00230         {
00231             if ( ! mFrameAdvise )
00232             {
00233                 MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate( this, &Canvas::frameEntered );
00234                 mFrameAdvise = true;
00235             }
00236         }
00237         else
00238         {
00239             if ( mFrameAdvise )
00240             {
00241                 MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate( this, &Canvas::frameEntered );
00242                 mFrameAdvise = false;
00243             }
00244         }
00245     }
00246 
00247     void Canvas::frameEntered( float _time )
00248     {
00249         int width = mReqTexSize.width;
00250         int height = mReqTexSize.height;
00251         TextureUsage usage = getDefaultTextureUsage();
00252         PixelFormat format = getDefaultTextureFormat();
00253 
00254         validate( width, height, usage, format );
00255 
00256         bool create = checkCreate( width, height );
00257 
00258         if ( mTexResizeMode == TRM_PT_CONST_SIZE )
00259             create = false;
00260 
00261         if ( create )
00262         {
00263             createExactTexture( width, height, usage, format );
00264             correctUV();
00265         }
00266         else // I thought order is important
00267         {
00268             correctUV();
00269             requestUpdateCanvas( this, Event( false, true, false ) );
00270         }
00271 
00272         frameAdvise( false );
00273     }
00274 
00275 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1