123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #ifndef _GUIIMAGEBUTTON_H_
- #include "2d/gui/guiImageButtonCtrl.h"
- #endif
- #ifndef _RENDER_PROXY_H_
- #include "2d/core/RenderProxy.h"
- #endif
- #ifndef _DGL_H_
- #include "graphics/dgl.h"
- #endif
- #ifndef _CONSOLE_H_
- #include "console/console.h"
- #endif
- #ifndef _CONSOLETYPES_H_
- #include "console/consoleTypes.h"
- #endif
- #ifndef _GUICANVAS_H_
- #include "gui/guiCanvas.h"
- #endif
- #ifndef _H_GUIDEFAULTCONTROLRENDER_
- #include "gui/guiDefaultControlRender.h"
- #endif
- /// Script bindings.
- #include "guiImageButtonCtrl_ScriptBindings.h"
- //-----------------------------------------------------------------------------
- IMPLEMENT_CONOBJECT(GuiImageButtonCtrl);
- //-----------------------------------------------------------------------------
- GuiImageButtonCtrl::GuiImageButtonCtrl() :
- mNormalAssetId( StringTable->EmptyString ),
- mHoverAssetId( StringTable->EmptyString ),
- mDownAssetId( StringTable->EmptyString ),
- mInactiveAssetId( StringTable->EmptyString )
- {
- mBounds.extent.set(140, 30);
- }
- //-----------------------------------------------------------------------------
- void GuiImageButtonCtrl::initPersistFields()
- {
- // Call parent.
- Parent::initPersistFields();
- addProtectedField("NormalImage", TypeAssetId, Offset(mNormalAssetId, GuiImageButtonCtrl), &setNormalImage, &getNormalImage, "The image asset Id used for the normal button state.");
- addProtectedField("HoverImage", TypeAssetId, Offset(mHoverAssetId, GuiImageButtonCtrl), &setHoverImage, &getHoverImage, "The image asset Id used for the hover button state.");
- addProtectedField("DownImage", TypeAssetId, Offset(mDownAssetId, GuiImageButtonCtrl), &setDownImage, &getDownImage, "The image asset Id used for the Down button state.");
- addProtectedField("InactiveImage", TypeAssetId, Offset(mInactiveAssetId, GuiImageButtonCtrl), &setInactiveImage, &getInactiveImage, "The image asset Id used for the inactive button state.");
- }
- //-----------------------------------------------------------------------------
- bool GuiImageButtonCtrl::onWake()
- {
- // Call parent.
- if (!Parent::onWake())
- return false;
- // Is only the "normal" image specified?
- if ( mNormalAssetId != StringTable->EmptyString &&
- mHoverAssetId == StringTable->EmptyString &&
- mDownAssetId == StringTable->EmptyString &&
- mInactiveAssetId == StringTable->EmptyString )
- {
- // Yes, so use it for all states.
- mImageNormalAsset = mNormalAssetId;
- mImageHoverAsset = mNormalAssetId;
- mImageDownAsset = mNormalAssetId;
- mImageInactiveAsset = mNormalAssetId;
- }
- else
- {
- // No, so assign individual states.
- mImageNormalAsset = mNormalAssetId;
- mImageHoverAsset = mHoverAssetId;
- mImageDownAsset = mDownAssetId;
- mImageInactiveAsset = mInactiveAssetId;
- }
-
- return true;
- }
- //-----------------------------------------------------------------------------
- void GuiImageButtonCtrl::onSleep()
- {
- // Clear assets.
- mImageNormalAsset.clear();
- mImageHoverAsset.clear();
- mImageDownAsset.clear();
- mImageInactiveAsset.clear();
- // Call parent.
- Parent::onSleep();
- }
- //-----------------------------------------------------------------------------
- void GuiImageButtonCtrl::setNormalImage( const char* pImageAssetId )
- {
- // Sanity!
- AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
- // Fetch the asset Id.
- mNormalAssetId = StringTable->insert(pImageAssetId);
- // Assign asset if awake.
- if ( isAwake() )
- mImageNormalAsset = mNormalAssetId;
- // Update control.
- setUpdate();
- }
- //-----------------------------------------------------------------------------
- void GuiImageButtonCtrl::setHoverImage( const char* pImageAssetId )
- {
- // Sanity!
- AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
- // Fetch the asset Id.
- mHoverAssetId = StringTable->insert(pImageAssetId);
- // Assign asset if awake.
- if ( isAwake() )
- mImageHoverAsset = mHoverAssetId;
- // Update control.
- setUpdate();
- }
- //-----------------------------------------------------------------------------
- void GuiImageButtonCtrl::setDownImage( const char* pImageAssetId )
- {
- // Sanity!
- AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
- // Fetch the asset Id.
- mDownAssetId = StringTable->insert(pImageAssetId);
- // Assign asset if awake.
- if ( isAwake() )
- mImageDownAsset = mDownAssetId;
- // Update control.
- setUpdate();
- }
- //-----------------------------------------------------------------------------
- void GuiImageButtonCtrl::setInactiveImage( const char* pImageAssetId )
- {
- // Sanity!
- AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
- // Fetch the asset Id.
- mInactiveAssetId = StringTable->insert(pImageAssetId);
- // Assign asset if awake.
- if ( isAwake() )
- mImageInactiveAsset = mInactiveAssetId;
- // Update control.
- setUpdate();
- }
- //-----------------------------------------------------------------------------
- void GuiImageButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
- {
- // Reset button state.
- ButtonState state = NORMAL;
- // Calculate button state.
- if ( mActive )
- {
- if ( mMouseOver )
- state = HOVER;
- if ( mDepressed || mStateOn )
- state = DOWN;
- }
- else
- {
- state = INACTIVE;
- }
- switch (state)
- {
- case NORMAL:
- {
- // Render the "normal" asset.
- renderButton(mImageNormalAsset, 0, offset, updateRect);
- } break;
- case HOVER:
- {
- // Render the "hover" asset.
- renderButton(mImageHoverAsset, 0, offset, updateRect);
- } break;
- case DOWN:
- {
- // Render the "down" asset.
- renderButton(mImageDownAsset, 0, offset, updateRect);
- } break;
- case INACTIVE:
- {
- // Render the "inactive" asset.
- renderButton(mImageInactiveAsset, 0, offset, updateRect);
- } break;
- }
- }
- //------------------------------------------------------------------------------
- void GuiImageButtonCtrl::renderButton( ImageAsset* pImageAsset, const U32 frame, Point2I &offset, const RectI& updateRect )
- {
- // Ignore an invalid datablock.
- if ( pImageAsset == NULL )
- return;
- // Is the asset valid and has the specified frame?
- if ( pImageAsset->isAssetValid() && frame < pImageAsset->getFrameCount() )
- {
- // Yes, so calculate the source region.
- const ImageAsset::FrameArea::PixelArea& pixelArea = pImageAsset->getImageFrameArea( frame ).mPixelArea;
- RectI sourceRegion( pixelArea.mPixelOffset, Point2I(pixelArea.mPixelWidth, pixelArea.mPixelHeight) );
- // Calculate destination region.
- RectI destinationRegion(offset, mBounds.extent);
- // Render image.
- dglSetBitmapModulation( mProfile->mFillColor );
- dglDrawBitmapStretchSR( pImageAsset->getImageTexture(), destinationRegion, sourceRegion );
- dglClearBitmapModulation();
- renderChildControls( offset, updateRect);
- }
- else
- {
- // No, so 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 the control.
- setUpdate();
- }
|