From 76228f2d0c789f740251c98d946c22e24cc3c8b4 Mon Sep 17 00:00:00 2001 From: Azaezel Date: Wed, 9 Mar 2016 20:02:39 -0600 Subject: [PATCH] alpha masking for buttons. original attribution @dottools --- .../gui/buttons/guiBitmapButtonCtrl.cpp | 41 +++++++++++++++++++ .../source/gui/buttons/guiBitmapButtonCtrl.h | 4 ++ 2 files changed, 45 insertions(+) diff --git a/Engine/source/gui/buttons/guiBitmapButtonCtrl.cpp b/Engine/source/gui/buttons/guiBitmapButtonCtrl.cpp index ce95475f2..29d81231f 100644 --- a/Engine/source/gui/buttons/guiBitmapButtonCtrl.cpp +++ b/Engine/source/gui/buttons/guiBitmapButtonCtrl.cpp @@ -124,6 +124,7 @@ GuiBitmapButtonCtrl::GuiBitmapButtonCtrl() mUseModifiers = false; mUseStates = true; setExtent( 140, 30 ); + mMasked = false; } //----------------------------------------------------------------------------- @@ -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" ); @@ -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); +} \ No newline at end of file diff --git a/Engine/source/gui/buttons/guiBitmapButtonCtrl.h b/Engine/source/gui/buttons/guiBitmapButtonCtrl.h index c42b4f3f4..2a9d464e7 100644 --- a/Engine/source/gui/buttons/guiBitmapButtonCtrl.h +++ b/Engine/source/gui/buttons/guiBitmapButtonCtrl.h @@ -113,6 +113,9 @@ class GuiBitmapButtonCtrl : public GuiButtonCtrl /// File name for bitmap. String mBitmapName; + /// alpha masking + bool mMasked; + /// Textures mTextures[ NumModifiers ]; @@ -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"