Browse Source

- Fixed the guiSpriteCtrl to use SpriteProxyBase rather than duplicate all its functionality.

MelvMay-GG 12 years ago
parent
commit
2a9dbfc

+ 33 - 0
engine/source/2d/core/SpriteProxyBase.cc

@@ -28,6 +28,10 @@
 #include "graphics/dgl.h"
 #endif
 
+#ifndef _RENDER_PROXY_H_
+#include "2d/core/RenderProxy.h"
+#endif
+
 #ifndef _STRINGBUFFER_H_
 #include "string/stringBuffer.h"
 #endif
@@ -208,6 +212,11 @@ void SpriteProxyBase::renderGui( GuiControl& owner, Point2I offset, const RectI
             dglClearBitmapModulation();
             dglDrawBitmapStretchSR( mImageAsset->getImageTexture(), destinationRegion, sourceRegion );
         }
+        else
+        {
+            // No, so render no-image render-proxy.
+            renderNoImage( owner, offset, updateRect );
+        }
     }
     else
     {
@@ -228,12 +237,36 @@ void SpriteProxyBase::renderGui( GuiControl& owner, Point2I offset, const RectI
             // Update control.
             owner.setUpdate();
         }
+        else
+        {
+            // No, so render no-image render-proxy.
+            renderNoImage( owner, offset, updateRect );
+        }
     }
 
     // Render child controls.
     owner.renderChildControls(offset, updateRect);
 }
 
+
+//------------------------------------------------------------------------------
+
+void SpriteProxyBase::renderNoImage( GuiControl& owner, Point2I &offset, const RectI& updateRect ) const
+{
+    // Fetch the 'cannot render' proxy.
+    RenderProxy* pNoImageRenderProxy = Sim::findObject<RenderProxy>( CANNOT_RENDER_PROXY_NAME );
+
+    // Finish if no render proxy available or it can't render.
+    if ( pNoImageRenderProxy == NULL || !pNoImageRenderProxy->validRender() )
+        return;
+
+    // Render using render-proxy.
+    pNoImageRenderProxy->renderGui( owner, offset, updateRect );
+
+    // Update control.
+    owner.setUpdate();
+}
+
 //------------------------------------------------------------------------------
 
 void SpriteProxyBase::copyTo(SpriteProxyBase* pSpriteProxyBase) const

+ 5 - 3
engine/source/2d/core/SpriteProxyBase.h

@@ -87,20 +87,22 @@ public:
 
     void renderGui( GuiControl& owner, Point2I offset, const RectI &updateRect ) const;
 
+	void renderNoImage( GuiControl& owner, Point2I &offset, const RectI& updateRect ) const;
+
     virtual void copyTo(SpriteProxyBase* pSpriteProxyBase) const;
 
     void clearAsset( void );
 
     // Image.
     inline bool setImage( const char* pImageAssetId ) { return setImage( pImageAssetId, mImageFrame ); }
-    bool setImage( const char* pImageAssetId, const U32 frame );
+    virtual bool setImage( const char* pImageAssetId, const U32 frame );
     inline StringTableEntry getImage( void ) const { return mImageAsset.getAssetId(); }
-    bool setImageFrame( const U32 frame );
+    virtual bool setImageFrame( const U32 frame );
     inline U32 getImageFrame( void ) const { return mImageFrame; }
     inline StringTableEntry getAnimation( void ) const { return mAnimationAsset.getAssetId(); }
 
     /// Animation.
-    bool setAnimation( const char* pAnimationAssetId, const bool autoRestore = false );
+    virtual bool setAnimation( const char* pAnimationAssetId, const bool autoRestore = false );
     inline AnimationController* getAnimationController( void ) const { return mpAnimationController; }
     inline bool isStaticMode( void ) const { return mStaticMode; }
     inline void pauseAnimation( const bool animationPaused ) { mAnimationPaused = animationPaused; }

+ 40 - 246
engine/source/2d/gui/guiSpriteCtrl.cc

@@ -36,10 +36,6 @@
 #include "graphics/dgl.h"
 #endif
 
-#ifndef _RENDER_PROXY_H_
-#include "2d/core/RenderProxy.h"
-#endif
-
 #include "guiSpriteCtrl_ScriptBindings.h"
 
 //-----------------------------------------------------------------------------
@@ -50,23 +46,16 @@ IMPLEMENT_CONOBJECT(GuiSpriteCtrl);
 
 GuiSpriteCtrl::GuiSpriteCtrl( void ) :
     mImageAssetId( StringTable->EmptyString ),
-    mAnimationAssetId( StringTable->EmptyString ),
-    mImageFrame( 0 ),
-    mAnimationPaused( false ),
-    mpAnimationController(NULL)
+    mAnimationAssetId( StringTable->EmptyString )
 {
+	// Set to self ticking.
+	mSelfTick = true;
 }
 
 //-----------------------------------------------------------------------------
 
 GuiSpriteCtrl::~GuiSpriteCtrl()
 {
-    // Destroy animation controller if required.
-    if ( mpAnimationController != NULL )
-    {
-        delete mpAnimationController;
-        mpAnimationController = NULL;
-    }
 }
 
 //-----------------------------------------------------------------------------
@@ -90,27 +79,15 @@ bool GuiSpriteCtrl::onWake()
         return false;
 
     // Are we in static mode?
-    if ( isStaticMode() )
+    if ( mImageAssetId != StringTable->EmptyString )
     {
-        // Set image asset if it's valid.
-        if ( mImageAssetId != StringTable->EmptyString )
-            mImageAsset = mImageAssetId;
+        // Set image asset.
+		SpriteProxyBase::setImage( mImageAssetId );
     }
-    else
+    else if ( mAnimationAssetId != StringTable->EmptyString )
     {
-        // Set animation asset if it's valid.
-        if ( mAnimationAssetId != StringTable->EmptyString )
-        {
-            // Create animation controller if required.
-            if ( mpAnimationController == NULL )
-                mpAnimationController = new AnimationController();
-
-            // Play animation.
-            mpAnimationController->playAnimation( mAnimationAssetId, false );
-
-            // Turn-on tick processing.
-            setProcessTicks( true );
-        }
+        // Play animation asset.
+		SpriteProxyBase::setAnimation( mAnimationAssetId, false );
     }
 
     return true;
@@ -121,106 +98,59 @@ bool GuiSpriteCtrl::onWake()
 void GuiSpriteCtrl::onSleep()
 {
     // Clear assets.
-    mImageAsset.clear();
-
-    // Destroy animation controller if required.
-    if ( mpAnimationController != NULL )
-    {
-        delete mpAnimationController;
-        mpAnimationController = NULL;
-    }    
-
-    // Turn-off tick processing.
-    setProcessTicks( false );
+	SpriteProxyBase::clearAsset();
 
     // Call parent.
     Parent::onSleep();
 }
 
-//------------------------------------------------------------------------------
-
-void GuiSpriteCtrl::processTick( void )
-{
-    // Are we in static mode?
-    if ( isStaticMode() )
-    {
-        // Yes, so turn-off tick processing.
-        setProcessTicks( false );
-
-        return;
-    }
-
-    // Finish if no animation controller.
-    if ( mpAnimationController == NULL )
-        return;
-
-    // Finish if the animation has finished.
-    if ( mpAnimationController->isAnimationFinished() )
-        return;
-
-    // Finish if animation is paused.
-    if ( mAnimationPaused )
-        return;
-
-    // Update the animation.
-    mpAnimationController->updateAnimation( Tickable::smTickSec );
-
-    // Finish if the animation has not finished.
-    if ( !mpAnimationController->isAnimationFinished() )
-        return;
-
-    // Turn-off tick processing.
-    setProcessTicks( false );
-}
-
 //-----------------------------------------------------------------------------
 
-void GuiSpriteCtrl::setImage( const char* pImageAssetId )
+bool GuiSpriteCtrl::setImage( const char* pImageAssetId )
 {
     // Sanity!
     AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
 
+    // Reset animation.
+    mAnimationAssetId = StringTable->EmptyString;
+
     // Fetch the asset Id.
     mImageAssetId = StringTable->insert(pImageAssetId);
 
     // Reset image frame.
-    mImageFrame = 0;
+	mImageFrame = 0;
 
-    // Assign asset if awake.
-    if ( isAwake() )
-        mImageAsset = mImageAssetId;
-    
-    // Set static mode.
-    mStaticMode = true;
+	// Finish if not awake.
+    if ( !isAwake() )
+		return true;
 
-    // Reset animation.
-    mAnimationAssetId = StringTable->EmptyString;
-
-    // Destroy animation controller if required.
-    if ( mpAnimationController != NULL )
-    {
-        delete mpAnimationController;
-        mpAnimationController = NULL;
-    }
+	// Call parent.
+	if ( !SpriteProxyBase::setImage( pImageAssetId, 0 ) )
+		return false;
 
     // Update control.
     setUpdate();
+
+	return true;
 }
 
 //-----------------------------------------------------------------------------
 
-void GuiSpriteCtrl::setImageFrame( const U32 imageFrame )
+bool GuiSpriteCtrl::setImageFrame( const U32 frame )
 {
-    // Set image frame.
-    mImageFrame = imageFrame;
+	// Call parent.
+	if ( !SpriteProxyBase::setImageFrame( frame ) )
+		return false;
 
     // Update control.
     setUpdate();
+
+	return true;
 }
 
 //-----------------------------------------------------------------------------
 
-void GuiSpriteCtrl::setAnimation( const char* pAnimationAssetId )
+bool GuiSpriteCtrl::setAnimation( const char* pAnimationAssetId )
 {
     // Sanity!
     AssertFatal( pAnimationAssetId != NULL, "Cannot use a NULL asset Id." );
@@ -231,165 +161,29 @@ void GuiSpriteCtrl::setAnimation( const char* pAnimationAssetId )
     // Reset the image asset Id.
     mImageAssetId = StringTable->EmptyString;
 
-    // Reset image frame.
-    mImageFrame = 0;
-
-    // Reset static mode.
-    mStaticMode = false;
-
-    // Assign asset if awake.
-    if ( isAwake() )
-    {
-        // Set animation asset if it's valid.
-        if ( mAnimationAssetId != StringTable->EmptyString )
-        {
-            // Create animation controller if required.
-            if ( mpAnimationController == NULL )
-                mpAnimationController = new AnimationController();
-
-            // Play animation.
-            mpAnimationController->playAnimation( mAnimationAssetId, false );
-
-            // Turn-on tick processing.
-            setProcessTicks( true );
-        }
-    }
-}
-
-//-----------------------------------------------------------------------------
-
-void GuiSpriteCtrl::play(void)
-{
-    // Sanity!
-    if (mStaticMode)
-        return;
-
-    // Reset animation pause.
-    mAnimationPaused = false;
-
-    // Assign asset if awake.
-    if ( isAwake() )
-    {
-        // Set animation asset if it's valid.
-        if ( mAnimationAssetId != StringTable->EmptyString )
-        {
-            // Play animation.
-            mpAnimationController->playAnimation( mAnimationAssetId, false );
-
-            // Turn-on tick processing.
-            setProcessTicks( true );
-        }
-    }
-}
-
-//-----------------------------------------------------------------------------
-
-void GuiSpriteCtrl::pause(bool flag)
-{
-    // Finish if not awake.
-    if ( !isAwake() )
-        return;
-
-    // Finish if no animation.
-    if ( mpAnimationController == NULL )
-        return;
-
-    // Pause the animation.
-    mAnimationPaused = flag;
-}
-
-//-----------------------------------------------------------------------------
-
-void GuiSpriteCtrl::stop(void)
-{
     // Finish if not awake.
     if ( !isAwake() )
-        return;
+		return true;
 
-    // Finish if no animation.
-    if ( mpAnimationController == NULL )
-        return;
+    // Play animation asset if it's valid.
+    if ( mAnimationAssetId != StringTable->EmptyString )
+		SpriteProxyBase::setAnimation( mAnimationAssetId, false );
 
-    // Stop the animation.
-    mAnimationPaused = false;
-    mpAnimationController->stopAnimation();
+	return true;
 }
 
 //-----------------------------------------------------------------------------
 
 void GuiSpriteCtrl::onRender( Point2I offset, const RectI &updateRect)
 {
-    // Are we in static mode?
-    if ( isStaticMode() )
-    {
-        // Do we have a valid image to render?
-        if ( mImageAsset.notNull() && mImageAsset->isAssetValid() && mImageFrame < mImageAsset->getFrameCount() )
-        {
-            // Yes, so calculate source region.
-            const ImageAsset::FrameArea::PixelArea& pixelArea = mImageAsset->getImageFrameArea( mImageFrame ).mPixelArea;
-            RectI sourceRegion( pixelArea.mPixelOffset, Point2I(pixelArea.mPixelWidth, pixelArea.mPixelHeight) );
-
-            // Calculate destination region.
-            RectI destinationRegion(offset, mBounds.extent);
-
-            // Render image.
-            dglSetBitmapModulation( mProfile->mFillColor );
-            dglDrawBitmapStretchSR( mImageAsset->getImageTexture(), destinationRegion, sourceRegion );
-            dglClearBitmapModulation();
-        }
-        else
-        {
-            // No, so render no-image render-proxy.
-            renderNoImage( offset, updateRect );
-        }
-    }
-    else
-    {
-        // Do we have a valid animation to render?
-        if ( mpAnimationController != NULL && mpAnimationController->isAnimationValid() )
-        {
-            // Yes, so calculate source region.
-            const ImageAsset::FrameArea::PixelArea& pixelArea = mpAnimationController->getCurrentImageFrameArea().mPixelArea;
-            RectI sourceRegion( pixelArea.mPixelOffset, Point2I(pixelArea.mPixelWidth, pixelArea.mPixelHeight) );
-
-            // Calculate destination region.
-            RectI destinationRegion(offset, mBounds.extent);
-
-            // Render animation image.
-            dglSetBitmapModulation( mProfile->mFillColor );
-            dglDrawBitmapStretchSR( mpAnimationController->getImageTexture(), destinationRegion, sourceRegion );
-            dglClearBitmapModulation();
-
-            // Update control.
-            setUpdate();
-        }
-        else
-        {
-            // No, so render no-image render-proxy.
-            renderNoImage( offset, updateRect );
-        }
-    }
-
-    // Render child controls.
-    renderChildControls(offset, updateRect);
+	// Call parent.
+	SpriteProxyBase::renderGui( *this, offset, updateRect );
 }
 
 //------------------------------------------------------------------------------
 
-void GuiSpriteCtrl::renderNoImage( Point2I &offset, const RectI& updateRect )
+void GuiSpriteCtrl::onAnimationEnd( void )
 {
-    // Fetch the 'cannot render' proxy.
-    RenderProxy* pNoImageRenderProxy = Sim::findObject<RenderProxy>( CANNOT_RENDER_PROXY_NAME );
-
-    // Finish if no render proxy available or it can't render.
-    if ( pNoImageRenderProxy == NULL || !pNoImageRenderProxy->validRender() )
-        return;
-
-    // Render using render-proxy.
-    pNoImageRenderProxy->renderGui( *this, offset, updateRect );
-
-    // Update control.
-    setUpdate();
+    // Clear assets.
+	SpriteProxyBase::clearAsset();
 }
-
-

+ 12 - 43
engine/source/2d/gui/guiSpriteCtrl.h

@@ -27,25 +27,13 @@
 #include "gui/guiControl.h"
 #endif
 
-#ifndef _IMAGE_ASSET_H_
-#include "2d/assets/ImageAsset.h"
-#endif
-
-#ifndef _ANIMATION_CONTROLLER_H_
-#include "2d/assets/AnimationController.h"
-#endif
-
-#ifndef _ASSET_PTR_H_
-#include "assets/assetPtr.h"
-#endif
-
-#ifndef _TICKABLE_H_
-#include "platform/Tickable.h"
+#ifndef _SPRITE_PROXY_BASE_H_
+#include "2d/core/SpriteProxyBase.h"
 #endif
 
 //-----------------------------------------------------------------------------
 
-class GuiSpriteCtrl : public GuiControl, public virtual Tickable
+class GuiSpriteCtrl : public GuiControl, public SpriteProxyBase
 {
 private:
     typedef GuiControl Parent;
@@ -53,13 +41,6 @@ private:
 protected:
     StringTableEntry                mImageAssetId;
     StringTableEntry                mAnimationAssetId;
-    U32                             mImageFrame;
-    bool                            mAnimationPaused;
-
-    AssetPtr<ImageAsset>            mImageAsset;
-
-    bool                            mStaticMode;
-    AnimationController*            mpAnimationController;
 
 public:
     GuiSpriteCtrl();
@@ -67,39 +48,27 @@ public:
     bool onWake();
     void onSleep();
     void onRender(Point2I offset, const RectI &updateRect);
-    void renderNoImage( Point2I &offset, const RectI& updateRect );
     static void initPersistFields();
 
-    /// Integration.
-    virtual void processTick();
-    virtual void interpolateTick( F32 delta ) {};
-    virtual void advanceTime( F32 timeDelta ) {};
-
-    /// Static.
-    void setImage( const char* pImageAssetId );
-    inline StringTableEntry getImage( void ) const { if ( isStaticMode() ) return mImageAssetId; else return StringTable->EmptyString; };
-    void setImageFrame( const U32 imageFrame );
-    inline U32 getImageFrame( void ) const { return mImageFrame; };
-
-    /// Animation.
-    void setAnimation( const char* pAnimationAssetId );
-    inline StringTableEntry getAnimation( void ) const { if ( isStaticMode() ) return StringTable->EmptyString; else return mAnimationAssetId; };
-    inline bool isStaticMode( void ) const { return mStaticMode; }
-    void play(void);
-    void pause(bool);
-    void stop(void);
+	/// Static and Animated Assets.
+    virtual bool setImage( const char* pImageAssetId );
+    virtual bool setImageFrame( const U32 frame );
+    virtual bool setAnimation( const char* pAnimationAssetId );
 
     // Declare type.
     DECLARE_CONOBJECT(GuiSpriteCtrl);
 
+protected:
+    virtual void onAnimationEnd( void );
+
 protected:
     static bool setImage(void* obj, const char* data) { static_cast<GuiSpriteCtrl*>(obj)->setImage( data ); return false; }
-    static const char* getImage(void* obj, const char* data) { return static_cast<GuiSpriteCtrl*>(obj)->getImage(); }
+    static const char* getImage(void* obj, const char* data) { return DYNAMIC_VOID_CAST_TO(GuiSpriteCtrl, SpriteProxyBase, obj)->getImage(); }
     static bool writeImage( void* obj, StringTableEntry pFieldName ) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); if ( !pCastObject->isStaticMode() ) return false; return pCastObject->mImageAssetId != StringTable->EmptyString; }
     static bool setImageFrame(void* obj, const char* data) { static_cast<GuiSpriteCtrl*>(obj)->setImageFrame( dAtoi(data) ); return false; }
     static bool writeImageFrame( void* obj, StringTableEntry pFieldName ) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return pCastObject->isStaticMode() && pCastObject->getImageFrame() > 0; }
     static bool setAnimation(void* obj, const char* data) { static_cast<GuiSpriteCtrl*>(obj)->setAnimation(data); return false; };
-    static const char* getAnimation(void* obj, const char* data) { return static_cast<GuiSpriteCtrl*>(obj)->getAnimation(); }
+    static const char* getAnimation(void* obj, const char* data) { return DYNAMIC_VOID_CAST_TO(GuiSpriteCtrl, SpriteProxyBase, obj)->getAnimation(); }
     static bool writeAnimation( void* obj, StringTableEntry pFieldName ) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); if ( pCastObject->isStaticMode() ) return false; return pCastObject->mAnimationAssetId != StringTable->EmptyString; }
 };
 

+ 2 - 51
engine/source/2d/gui/guiSpriteCtrl_ScriptBindings.h

@@ -49,7 +49,7 @@ ConsoleMethod( GuiSpriteCtrl, getImage, const char*, 2, 2,  "() - Gets current i
     }
 
     // Get image.
-    return object->getImage();
+    return DYNAMIC_VOID_CAST_TO(GuiSpriteCtrl, SpriteProxyBase, object)->getImage();
 }
 
 //-----------------------------------------------------------------------------
@@ -102,54 +102,5 @@ ConsoleMethod( GuiSpriteCtrl, getAnimation, const char*, 2, 2,  "() - Gets the c
     }
 
     // Get animation.
-    return object->getAnimation();
-}
-
-//------------------------------------------------------------------------------
-
-ConsoleMethod( GuiSpriteCtrl, play, void, 2, 2,  "() - plays the control's current animation.  Resumes playback if paused.\n")
-{
-    // Are we in static mode?
-    if ( object->isStaticMode() )
-    {
-        // Yes, so warn.
-        Con::warnf( "GuiSpriteCtrl::play() - Method invalid, in static mode." );
-        return;
-    }
-
-    // play animation.
-    object->play();
-}
-
-//------------------------------------------------------------------------------
-
-ConsoleMethod( GuiSpriteCtrl, pause, void, 3, 3,  "(bool flag) - pauses/unpauses the control's current animation.\n"
-                                                    "@param flag true to pause, false to resume.")
-{
-    // Are we in static mode?
-    if ( object->isStaticMode() )
-    {
-        // Yes, so warn.
-        Con::warnf( "GuiSpriteCtrl::pause() - Method invalid, in static mode." );
-        return;
-    }
-
-    // pause animation.
-    object->pause( dAtob(argv[2]) );
-}
-
-//------------------------------------------------------------------------------
-
-ConsoleMethod( GuiSpriteCtrl, stop, void, 2, 2,  "() - stops the control's current animation.\n")
-{
-    // Are we in static mode?
-    if ( object->isStaticMode() )
-    {
-        // Yes, so warn.
-        Con::warnf( "GuiSpriteCtrl::stop() - Method invalid, in static mode." );
-        return;
-    }
-
-    // stop animation.
-    object->stop();
+    return DYNAMIC_VOID_CAST_TO(GuiSpriteCtrl, SpriteProxyBase, object)->getAnimation();
 }