Skip to content

Commit

Permalink
Merge pull request #1548 from Azaezel/alphamaskedButtons
Browse files Browse the repository at this point in the history
alpha masking for buttons. original attribution @dottools
  • Loading branch information
Areloch committed Jun 8, 2016
2 parents 3996150 + 76228f2 commit db41a0b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Engine/source/gui/buttons/guiBitmapButtonCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ GuiBitmapButtonCtrl::GuiBitmapButtonCtrl()
mUseModifiers = false;
mUseStates = true;
setExtent( 140, 30 );
mMasked = false;
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -157,6 +158,7 @@ void GuiBitmapButtonCtrl::initPersistFields()
"Defaults to true.\n\n"
"If you do not use per-state images on this button set this to false to speed up the loading process "
"by inhibiting searches for the individual images." );
addField("masked", TypeBool, Offset(mMasked, GuiBitmapButtonCtrl),"Use alpha masking for interaction.");

endGroup( "Bitmap" );

Expand Down Expand Up @@ -551,3 +553,42 @@ void GuiBitmapButtonTextCtrl::renderButton( GFXTexHandle &texture, const Point2I
GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
renderJustifiedText(textPos, getExtent(), mButtonText);
}

bool GuiBitmapButtonCtrl::pointInControl(const Point2I& parentCoordPoint)
{
if (mMasked && getTextureForCurrentState())
{
ColorI rColor(0, 0, 0, 0);
GBitmap* bmp;

const RectI &bounds = getBounds();
S32 xt = parentCoordPoint.x - bounds.point.x;
S32 yt = parentCoordPoint.y - bounds.point.y;

bmp = getTextureForCurrentState().getBitmap();
if (!bmp)
{
setBitmap(mBitmapName);
bmp = getTextureForCurrentState().getBitmap();
}

S32 relativeXRange = this->getExtent().x;
S32 relativeYRange = this->getExtent().y;
S32 fileXRange = bmp->getHeight(0);
S32 fileYRange = bmp->getWidth(0);
//Con::errorf("xRange:[%i -- %i], Range:[%i -- %i] pos:(%i,%i)",relativeXRange,fileXRange,relativeYRange,fileYRange,xt,yt);

S32 fileX = (xt*fileXRange) / relativeXRange;
S32 fileY = (yt*fileYRange) / relativeYRange;
//Con::errorf("Checking %s @ (%i,%i)",this->getName(),fileX,fileY);

bmp->getColor(fileX, fileY, rColor);

if (rColor.alpha)
return true;
else
return false;
}
else
return Parent::pointInControl(parentCoordPoint);
}
4 changes: 4 additions & 0 deletions Engine/source/gui/buttons/guiBitmapButtonCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class GuiBitmapButtonCtrl : public GuiButtonCtrl
/// File name for bitmap.
String mBitmapName;

/// alpha masking
bool mMasked;

///
Textures mTextures[ NumModifiers ];

Expand Down Expand Up @@ -163,6 +166,7 @@ class GuiBitmapButtonCtrl : public GuiButtonCtrl
virtual void onRender(Point2I offset, const RectI &updateRect);

static void initPersistFields();
bool pointInControl(const Point2I& parentCoordPoint);

DECLARE_CONOBJECT(GuiBitmapButtonCtrl);
DECLARE_DESCRIPTION( "A button control rendered entirely from bitmaps.\n"
Expand Down

0 comments on commit db41a0b

Please sign in to comment.