Torque3D/Engine/source/gui/buttons/guiBitmapButtonCtrl.h

239 lines
7.8 KiB
C
Raw Normal View History

2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 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 _GUIBITMAPBUTTON_H_
#define _GUIBITMAPBUTTON_H_
#ifndef _GUIBUTTONCTRL_H_
#include "gui/buttons/guiButtonCtrl.h"
#endif
#ifndef _GFXTEXTUREMANAGER_H_
#include "gfx/gfxTextureManager.h"
#endif
/// A button control that uses bitmaps as its different button states.
///
/// Set 'bitmap' console field to base name of bitmaps to use. This control will
///
/// append '_n' for normal
/// append '_h' for highlighted
/// append '_d' for depressed
/// append '_i' for inactive
///
/// If a bitmap cannot be found it will use the default bitmap to render.
///
/// Additionally, a bitmap button can be made to react to keyboard modifiers. These can be
/// either CTRL/CMD, ALT, or SHIFT (but no combination of them.) To assign a different bitmap
/// for a modifier state, prepend "_ctrl", _"alt", or "_shift" to the state postfix.
///
/// To implement different handlers for the modifier states, use the "onDefaultClick",
/// "onCtrlClick", "onAltClick", and "onShiftClick" methods.
///
class GuiBitmapButtonCtrl : public GuiButtonCtrl, protected AssetPtrCallback
2012-09-19 15:15:01 +00:00
{
public:
typedef GuiButtonCtrl Parent;
enum BitmapMode
{
BitmapStretched,
BitmapCentered,
};
protected:
enum Modifier
{
ModifierNone,
ModifierCtrl,
ModifierAlt,
ModifierShift,
NumModifiers
};
enum State
{
NORMAL,
HILIGHT,
DEPRESSED,
INACTIVE
};
struct Textures
{
/// Texture for normal state.
StringTableEntry mTextureNormalAssetId;
AssetPtr<ImageAsset> mTextureNormalAsset;
2012-09-19 15:15:01 +00:00
GFXTexHandle mTextureNormal;
/// Texture for highlight state.
StringTableEntry mTextureHilightAssetId;
AssetPtr<ImageAsset> mTextureHilightAsset;
2012-09-19 15:15:01 +00:00
GFXTexHandle mTextureHilight;
/// Texture for depressed state.
StringTableEntry mTextureDepressedAssetId;
AssetPtr<ImageAsset> mTextureDepressedAsset;
2012-09-19 15:15:01 +00:00
GFXTexHandle mTextureDepressed;
/// Texture for inactive state.
StringTableEntry mTextureInactiveAssetId;
AssetPtr<ImageAsset> mTextureInactiveAsset;
2012-09-19 15:15:01 +00:00
GFXTexHandle mTextureInactive;
};
/// Make control extents equal to bitmap size.
bool mAutoFitExtents;
/// Allow switching out images according to modifier presses.
bool mUseModifiers;
/// Allow switching images according to mouse states. On by default.
/// Switch off when not needed as it otherwise results in a lot of costly
/// texture loads.
bool mUseStates;
///
BitmapMode mBitmapMode;
private:
AssetPtr<ImageAsset> mBitmapAsset;
String mBitmapFile;
public:
void _setBitmap(StringTableEntry _in) {
if (mBitmapAsset.getAssetId() == _in) return; if (!AssetDatabase.isDeclaredAsset(_in)) {
StringTableEntry imageAssetId = ImageAsset::smNoImageAssetFallback; AssetQuery query; S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); if (foundAssetcount != 0) {
imageAssetId = query.mAssetList[0];
} mBitmapAsset = imageAssetId;
}
else {
mBitmapAsset = _in;
mBitmapName = _in;
mBitmap = getBitmap();
}
}; inline StringTableEntry _getBitmap(void) const {
return mBitmapAsset.getAssetId();
} GFXTexHandle getBitmap() {
return mBitmapAsset.notNull() ? mBitmapAsset->getTexture(&GFXDefaultGUIProfile) : 0;
} AssetPtr<ImageAsset> getBitmapAsset(void) {
return mBitmapAsset;
} static bool _setBitmapData(void* obj, const char* index, const char* data) {
static_cast<GuiBitmapButtonCtrl*>(obj)->_setBitmap(_getStringTable()->insert(data)); return false;
}
StringTableEntry getBitmapFile() { return mBitmapAsset.notNull() ? mBitmapAsset->getImageFile() : ""; }
protected:
void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
{
setBitmap(mBitmapName);
}
GFXTexHandle mBitmap;
StringTableEntry mBitmapName;
/// alpha masking
bool mMasked;
2012-09-19 15:15:01 +00:00
///
Textures mTextures[ NumModifiers ];
ColorI mColor;
2012-09-19 15:15:01 +00:00
virtual void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect );
static bool _setAutoFitExtents( void *object, const char *index, const char *data );
//static bool _setBitmap( void *object, const char *index, const char *data );
2012-09-19 15:15:01 +00:00
State getState() const
{
if( mActive )
{
if( mDepressed || mStateOn ) return DEPRESSED;
2022-02-18 00:04:31 +00:00
if( mHighlighted ) return HILIGHT;
2012-09-19 15:15:01 +00:00
return NORMAL;
}
else
return INACTIVE;
}
Modifier getCurrentModifier();
GFXTexHandle& getTextureForCurrentState();
/// @name Callbacks
/// @{
DECLARE_CALLBACK( void, onDefaultClick, () );
DECLARE_CALLBACK( void, onCtrlClick, () );
DECLARE_CALLBACK( void, onAltClick, () );
DECLARE_CALLBACK( void, onShiftClick, () );
/// @}
public:
GuiBitmapButtonCtrl();
void setAutoFitExtents( bool state );
void setBitmap( StringTableEntry name );
2012-09-19 15:15:01 +00:00
void setBitmapHandles( GFXTexHandle normal, GFXTexHandle highlighted, GFXTexHandle depressed, GFXTexHandle inactive );
//Parent methods
bool onWake() override;
void onSleep() override;
void onAction() override;
void inspectPostApply() override;
2012-09-19 15:15:01 +00:00
void onRender(Point2I offset, const RectI &updateRect) override;
2012-09-19 15:15:01 +00:00
static void initPersistFields();
bool pointInControl(const Point2I& parentCoordPoint) override;
2012-09-19 15:15:01 +00:00
DECLARE_CONOBJECT(GuiBitmapButtonCtrl);
DECLARE_DESCRIPTION( "A button control rendered entirely from bitmaps.\n"
"The individual button states are represented with separate bitmaps." );
};
typedef GuiBitmapButtonCtrl::BitmapMode GuiBitmapMode;
DefineEnumType( GuiBitmapMode );
/// Extension of GuiBitmapButtonCtrl that also display a text label on the button.
class GuiBitmapButtonTextCtrl : public GuiBitmapButtonCtrl
{
public:
typedef GuiBitmapButtonCtrl Parent;
protected:
void renderButton( GFXTexHandle &texture, const Point2I& offset, const RectI& updateRect ) override;
2012-09-19 15:15:01 +00:00
public:
DECLARE_CONOBJECT( GuiBitmapButtonTextCtrl );
DECLARE_DESCRIPTION( "An extension of GuiBitmapButtonCtrl that also renders a text\n"
"label on the button." );
};
#endif //_GUI_BITMAP_BUTTON_CTRL_H