From b037f0feac66be2e039f5ed562e1e64ec6f31014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Tue, 26 Sep 2017 01:05:49 -0300 Subject: [PATCH] Background and Border of UIControls now supports alpha transparency. --HG-- branch : dev --- include/eepp/ui/uibackground.hpp | 2 +- include/eepp/ui/uiborder.hpp | 2 +- src/eepp/ui/uibackground.cpp | 44 +++++++++++++++++++++++++------- src/eepp/ui/uiborder.cpp | 4 +-- src/eepp/ui/uicontrol.cpp | 4 +-- src/eepp/ui/uicontrolanim.cpp | 4 +-- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/include/eepp/ui/uibackground.hpp b/include/eepp/ui/uibackground.hpp index 8e94e8cce..a09ce6b91 100644 --- a/include/eepp/ui/uibackground.hpp +++ b/include/eepp/ui/uibackground.hpp @@ -32,7 +32,7 @@ class EE_API UIBackground { UIBackground * setCorners( const unsigned int& corners ); - void draw( Rectf R ); + void draw( Rectf R, const Float& alpha ); Drawable * getDrawable() const; diff --git a/include/eepp/ui/uiborder.hpp b/include/eepp/ui/uiborder.hpp index 066df2a3b..589075112 100644 --- a/include/eepp/ui/uiborder.hpp +++ b/include/eepp/ui/uiborder.hpp @@ -19,7 +19,7 @@ class EE_API UIBorder { UIBorder * setWidth( const unsigned int& width ); - void draw( Rectf R, const int& corners, const bool& clipping ); + void draw( Rectf R, const Float& alpha, const int& corners, const bool& clipping ); protected: Color mColor; unsigned int mWidth; diff --git a/src/eepp/ui/uibackground.cpp b/src/eepp/ui/uibackground.cpp index 25fbbccd8..373d13044 100644 --- a/src/eepp/ui/uibackground.cpp +++ b/src/eepp/ui/uibackground.cpp @@ -85,23 +85,49 @@ UIBackground * UIBackground::setCorners( const unsigned int& corners ) { return this; } -void UIBackground::draw( Rectf R ) { +void UIBackground::draw( Rectf R, const Float& alpha ) { if ( mColor[0] != Color::Transparent || mColor.size() > 1 ) { Primitives P; P.setBlendMode( mBlendMode ); - P.setColor( mColor[0] ); - if ( 4 == mColor.size() ) { - if ( mCorners ) { - P.drawRoundedRectangle( R, mColor[0], mColor[1], mColor[2], mColor[3], mCorners ); + if ( 255 == alpha ) { + P.setColor( mColor[0] ); + + if ( 4 == mColor.size() ) { + if ( mCorners ) { + P.drawRoundedRectangle( R, mColor[0], mColor[1], mColor[2], mColor[3], mCorners ); + } else { + P.drawRectangle( R, mColor[0], mColor[1], mColor[2], mColor[3] ); + } } else { - P.drawRectangle( R, mColor[0], mColor[1], mColor[2], mColor[3] ); + if ( mCorners ) { + P.drawRoundedRectangle( R, 0.f, Vector2f::One, mCorners ); + } else { + P.drawRectangle( R ); + } } } else { - if ( mCorners ) { - P.drawRoundedRectangle( R, 0.f, Vector2f::One, mCorners ); + std::vector color( mColor ); + + color[0].a = static_cast( (Float)color[0].a * ( alpha / 255.f ) ); + + P.setColor( color[0] ); + + if ( 4 == mColor.size() ) { + for ( size_t i = 0; i < color.size(); i++ ) + color[i].a = static_cast( (Float)color[i].a * ( alpha / 255.f ) ); + + if ( mCorners ) { + P.drawRoundedRectangle( R, color[0], color[1], color[2], color[3], mCorners ); + } else { + P.drawRectangle( R, color[0], color[1], color[2], color[3] ); + } } else { - P.drawRectangle( R ); + if ( mCorners ) { + P.drawRoundedRectangle( R, 0.f, Vector2f::One, mCorners ); + } else { + P.drawRectangle( R ); + } } } } diff --git a/src/eepp/ui/uiborder.cpp b/src/eepp/ui/uiborder.cpp index 0052c17b6..144deeb0f 100644 --- a/src/eepp/ui/uiborder.cpp +++ b/src/eepp/ui/uiborder.cpp @@ -32,11 +32,11 @@ UIBorder * UIBorder::setWidth( const unsigned int& width ) { return this; } -void UIBorder::draw( Rectf R , const int& corners, const bool& clipping ) { +void UIBorder::draw( Rectf R , const Float& alpha, const int& corners, const bool& clipping ) { Primitives P; P.setFillMode( DRAW_LINE ); P.setLineWidth( PixelDensity::dpToPx( mWidth ) ); - P.setColor( mColor ); + P.setColor( 255 == alpha ? mColor : Color( mColor.r, mColor.g, mColor.b, static_cast( (Float)mColor.a * alpha / 255.f ) ) ); if ( clipping ) { Rectf r( Vector2f( R.Left + 0.1f, R.Top + 0.1f ), Sizef( R.getWidth() - 0.1f, R.getHeight() - 0.1f ) ); diff --git a/src/eepp/ui/uicontrol.cpp b/src/eepp/ui/uicontrol.cpp index a22d14065..d2fe72654 100644 --- a/src/eepp/ui/uicontrol.cpp +++ b/src/eepp/ui/uicontrol.cpp @@ -717,13 +717,13 @@ Rectf UIControl::getRectf() { void UIControl::drawBackground() { if ( mFlags & UI_FILL_BACKGROUND ) { - mBackground->draw( getRectf() ); + mBackground->draw( getRectf(), 255.f ); } } void UIControl::drawBorder() { if ( mFlags & UI_BORDER ) { - mBorder->draw( getRectf(), mBackground->getCorners(), ( mFlags & UI_CLIP_ENABLE ) != 0 ); + mBorder->draw( getRectf(), 255.f, mBackground->getCorners(), ( mFlags & UI_CLIP_ENABLE ) != 0 ); } } diff --git a/src/eepp/ui/uicontrolanim.cpp b/src/eepp/ui/uicontrolanim.cpp index 49c7d4fb8..2cf15defc 100644 --- a/src/eepp/ui/uicontrolanim.cpp +++ b/src/eepp/ui/uicontrolanim.cpp @@ -369,13 +369,13 @@ Interpolation1d * UIControlAnim::disableFadeOut( const Time& Time, const bool& A void UIControlAnim::drawBackground() { if ( mFlags & UI_FILL_BACKGROUND ) { - mBackground->draw( getRectf() ); + mBackground->draw( getRectf(), mAlpha ); } } void UIControlAnim::drawBorder() { if ( mFlags & UI_BORDER ) { - mBorder->draw( getRectf(), mBackground->getCorners(), ( mFlags & UI_CLIP_ENABLE ) != 0 ); + mBorder->draw( getRectf(), mAlpha, mBackground->getCorners(), ( mFlags & UI_CLIP_ENABLE ) != 0 ); } }