📄 guibitmapbuttonctrl.cc
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
//
// Copyright (c) 2001 GarageGames.Com
//-----------------------------------------------------------------------------
//-------------------------------------
//
// Bitmap Button Contrl
// Set 'bitmap' comsole field to base name of bitmaps to use. This control will
// append '_n' for normal
// append '_h' for hilighted
// append '_d' for depressed
//
// if bitmap cannot be found it will use the default bitmap to render.
//
// if the extent is set to (0,0) in the gui editor and appy hit, this control will
// set it's extent to be exactly the size of the normal bitmap (if present)
//
#include "console/console.h"
#include "dgl/dgl.h"
#include "console/consoleTypes.h"
#include "platform/platformAudio.h"
#include "gui/core/guiCanvas.h"
#include "gui/core/guiDefaultControlRender.h"
#include "gui/controls/guiBitmapButtonCtrl.h"
IMPLEMENT_CONOBJECT(GuiBitmapButtonCtrl);
//-------------------------------------
GuiBitmapButtonCtrl::GuiBitmapButtonCtrl()
{
mBitmapName = StringTable->insert("");
mBounds.extent.set(140, 30);
#ifdef TGE_RPG_UI
m_bFullDraw = false;
m_bDrawText = false;
m_bDrawBorder = false;
m_bFloatIcon = true;
#endif
}
//-------------------------------------
void GuiBitmapButtonCtrl::initPersistFields()
{
Parent::initPersistFields();
addField("bitmap", TypeFilename, Offset(mBitmapName, GuiBitmapButtonCtrl));
addField("fullDraw", TypeBool, Offset(m_bFullDraw, GuiBitmapButtonCtrl));
addField("drawText", TypeBool, Offset(m_bDrawText, GuiBitmapButtonCtrl));
addField("drawBorder", TypeBool, Offset(m_bDrawBorder, GuiBitmapButtonCtrl));
}
//-------------------------------------
bool GuiBitmapButtonCtrl::onWake()
{
if (! Parent::onWake())
return false;
setActive(true);
setBitmap(mBitmapName);
return true;
}
//-------------------------------------
void GuiBitmapButtonCtrl::onSleep()
{
mTextureNormal = NULL;
mTextureHilight = NULL;
mTextureDepressed = NULL;
Parent::onSleep();
}
//-------------------------------------
ConsoleMethod( GuiBitmapButtonCtrl, setBitmap, void, 3, 3, "(filepath name)")
{
object->setBitmap(argv[2]);
}
//-------------------------------------
void GuiBitmapButtonCtrl::inspectPostApply()
{
// if the extent is set to (0,0) in the gui editor and appy hit, this control will
// set it's extent to be exactly the size of the normal bitmap (if present)
Parent::inspectPostApply();
if ((mBounds.extent.x == 0) && (mBounds.extent.y == 0) && mTextureNormal)
{
TextureObject *texture = (TextureObject *) mTextureNormal;
mBounds.extent.x = texture->bitmapWidth;
mBounds.extent.y = texture->bitmapHeight;
}
}
//-------------------------------------
void GuiBitmapButtonCtrl::setBitmap(const char *name)
{
mBitmapName = StringTable->insert(name);
if(!isAwake())
return;
if (*mBitmapName)
{
char buffer[1024];
char *p;
dStrcpy(buffer, name);
p = buffer + dStrlen(buffer);
#ifdef TGE_RPG_UI
mTextureNormal = TextureHandle(buffer, BitmapTexture, true);
if(!bool(mTextureNormal))
{
dStrcpy(p, "_n");
mTextureNormal = TextureHandle(buffer, BitmapTexture, true);
}
#else
dStrcpy(p, "_n");
mTextureNormal = TextureHandle(buffer, BitmapTexture, true);
#endif
dStrcpy(p, "_h");
mTextureHilight = TextureHandle(buffer, BitmapTexture, true);
if (!mTextureHilight)
mTextureHilight = mTextureNormal;
dStrcpy(p, "_d");
mTextureDepressed = TextureHandle(buffer, BitmapTexture, true);
if (!mTextureDepressed)
mTextureDepressed = mTextureHilight;
dStrcpy(p, "_i");
mTextureInactive = TextureHandle(buffer, BitmapTexture, true);
if (!mTextureInactive)
mTextureInactive = mTextureNormal;
}
else
{
mTextureNormal = NULL;
mTextureHilight = NULL;
mTextureDepressed = NULL;
mTextureInactive = NULL;
}
setUpdate();
}
//-------------------------------------
void GuiBitmapButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
{
U32 state = NORMAL;
if (mActive)
{
if (mMouseOver)
state = HILIGHT;
if (mDepressed || mStateOn)
state = DEPRESSED;
}
else
state = INACTIVE;
#ifdef TGE_RPG_UI
if(m_bDrawBorder && mProfile->mBorder)
{
RectI rect(offset, mBounds.extent);
switch (state)
{
case HILIGHT:
dglDrawRectFill(rect,mProfile->mFillColorHL);
dglDrawRect(rect,mProfile->mBorderColorHL);
break;
case INACTIVE:
dglDrawRectFill(rect,mProfile->mFillColorNA);
dglDrawRect(rect,mProfile->mBorderColorNA);
break;
default:
dglDrawRectFill(rect,mProfile->mFillColor);
dglDrawRect(rect,mProfile->mBorderColor);
break;
}
}
if(m_bDrawText)
{
RectI rect(offset, mBounds.extent);
dglClearBitmapModulation();
switch (state)
{
case HILIGHT:
dglSetBitmapModulation( mProfile->mFontColorHL );
break;
case INACTIVE:
dglSetBitmapModulation( mProfile->mFontColorNA );
break;
default:
dglSetBitmapModulation( mProfile->mFontColor );
break;
}
//dglDrawBitmapStretch(texture, rect);
Point2I textPos;
textPos.y = offset.y + ((mBounds.extent.y - mProfile->mFont->getHeight()) >> 1);
textPos.x = offset.x + mTextureNormal.getWidth() + 2;
dglDrawText(mProfile->mFont,textPos, mButtonText);
}
#endif
switch (state)
{
case NORMAL:
renderButton(mTextureNormal, offset, updateRect,state);
break;
case HILIGHT:
renderButton(mTextureHilight ? mTextureHilight : mTextureNormal, offset, updateRect,state);
break;
case DEPRESSED:
renderButton(mTextureDepressed, offset, updateRect,state);
break;
case INACTIVE:
renderButton(mTextureInactive ? mTextureInactive : mTextureNormal, offset, updateRect,state);
break;
}
}
//------------------------------------------------------------------------------
void GuiBitmapButtonCtrl::renderButton(TextureHandle &texture, Point2I &offset, const RectI& updateRect,U32 nState)
{
if (texture)
{
#ifdef TGE_RPG_UI
RectI rect;
if(!m_bFullDraw)
{
S32 nOffY = offset.y + ((mBounds.extent.y - texture.getHeight()) >> 1);
if(nState == HILIGHT && m_bFloatIcon)
nOffY--;
rect.set(offset.x+1,nOffY, texture.getWidth(),texture.getHeight() );
}
else
rect.set(offset, mBounds.extent);
#else
RectI rect(offset, mBounds.extent);
#endif
dglClearBitmapModulation();
dglDrawBitmapStretch(texture, rect);
renderChildControls( offset, updateRect);
}
else
Parent::onRender(offset, updateRect);
}
//------------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(GuiBitmapButtonTextCtrl);
void GuiBitmapButtonTextCtrl::onRender(Point2I offset, const RectI& updateRect)
{
enum {
NORMAL,
HILIGHT,
DEPRESSED,
INACTIVE
} state = NORMAL;
if (mActive)
{
if (mMouseOver) state = HILIGHT;
if (mDepressed || mStateOn) state = DEPRESSED;
}
else
state = INACTIVE;
TextureHandle texture;
switch (state)
{
case NORMAL:
texture = mTextureNormal;
break;
case HILIGHT:
texture = mTextureHilight;
break;
case DEPRESSED:
texture = mTextureDepressed;
break;
case INACTIVE:
texture = mTextureInactive;
if(!texture)
texture = mTextureNormal;
break;
}
if (texture)
{
RectI rect(offset, mBounds.extent);
dglClearBitmapModulation();
dglDrawBitmapStretch(texture, rect);
Point2I textPos = offset;
if(mDepressed)
textPos += Point2I(1,1);
// Make sure we take the profile's textOffset into account.
textPos += mProfile->mTextOffset;
dglSetBitmapModulation( mProfile->mFontColor );
renderJustifiedText(textPos, mBounds.extent, mButtonText);
renderChildControls( offset, updateRect);
}
else
Parent::onRender(offset, updateRect);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -