From b43f1b43d3c6e596aabd741e50b3bb038bc31e27 Mon Sep 17 00:00:00 2001 From: John Rassa Date: Sat, 31 Aug 2019 09:06:23 -0400 Subject: [PATCH] clean up opacity handling for ImageComponent, RatingComponent --- es-app/src/components/RatingComponent.cpp | 34 +++++++-------- es-core/src/components/ImageComponent.cpp | 43 +++++++------------ es-core/src/components/NinePatchComponent.cpp | 2 +- 3 files changed, 32 insertions(+), 47 deletions(-) diff --git a/es-app/src/components/RatingComponent.cpp b/es-app/src/components/RatingComponent.cpp index 4a06cf142..915eba392 100644 --- a/es-app/src/components/RatingComponent.cpp +++ b/es-app/src/components/RatingComponent.cpp @@ -41,16 +41,12 @@ std::string RatingComponent::getValue() const void RatingComponent::setOpacity(unsigned char opacity) { mOpacity = opacity; - mColorShift = (mColorShift >> 8 << 8) | mOpacity; updateColors(); } void RatingComponent::setColorShift(unsigned int color) { mColorShift = color; - // Grab the opacity from the color shift because we may need to apply it if - // fading textures in - mOpacity = color & 0xff; updateColors(); } @@ -75,21 +71,22 @@ void RatingComponent::onSizeChanged() void RatingComponent::updateVertices() { - const float numStars = NUM_RATING_STARS; - const float h = getSize().y(); // is the same as a single star's width - const float w = getSize().y() * mValue * numStars; - const float fw = getSize().y() * numStars; - const unsigned int color = Renderer::convertColor(mColorShift); + const float numStars = NUM_RATING_STARS; + const float h = getSize().y(); // is the same as a single star's width + const float w = getSize().y() * mValue * numStars; + const float fw = getSize().y() * numStars; - mVertices[0] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, color }; - mVertices[1] = { { 0.0f, h }, { 0.0f, 0.0f }, color }; - mVertices[2] = { { w, 0.0f }, { mValue * numStars, 1.0f }, color }; - mVertices[3] = { { w, h }, { mValue * numStars, 0.0f }, color }; + mVertices[0] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, 0 }; + mVertices[1] = { { 0.0f, h }, { 0.0f, 0.0f }, 0 }; + mVertices[2] = { { w, 0.0f }, { mValue * numStars, 1.0f }, 0 }; + mVertices[3] = { { w, h }, { mValue * numStars, 0.0f }, 0 }; - mVertices[4] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, color }; - mVertices[5] = { { 0.0f, h }, { 0.0f, 0.0f }, color }; - mVertices[6] = { { fw, 0.0f }, { numStars, 1.0f }, color }; - mVertices[7] = { { fw, h }, { numStars, 0.0f }, color }; + mVertices[4] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, 0 }; + mVertices[5] = { { 0.0f, h }, { 0.0f, 0.0f }, 0 }; + mVertices[6] = { { fw, 0.0f }, { numStars, 1.0f }, 0 }; + mVertices[7] = { { fw, h }, { numStars, 0.0f }, 0 }; + + updateColors(); // round vertices for(int i = 0; i < 8; ++i) @@ -98,7 +95,8 @@ void RatingComponent::updateVertices() void RatingComponent::updateColors() { - const unsigned int color = Renderer::convertColor(mColorShift); + const float opacity = mOpacity / 255.0; + const unsigned int color = Renderer::convertColor(mColorShift & 0xFFFFFF00 | (unsigned char)((mColorShift & 0xFF) * opacity)); for(int i = 0; i < 8; ++i) mVertices[i].col = color; diff --git a/es-core/src/components/ImageComponent.cpp b/es-core/src/components/ImageComponent.cpp index 4f1a6f0be..823ceb570 100644 --- a/es-core/src/components/ImageComponent.cpp +++ b/es-core/src/components/ImageComponent.cpp @@ -247,18 +247,13 @@ void ImageComponent::setFlipY(bool flip) void ImageComponent::setColorShift(unsigned int color) { mColorShift = color; - // Grab the opacity from the color shift because we may need to apply it if - // fading textures in - mOpacity = color & 0xff; + mColorShiftEnd = color; updateColors(); } void ImageComponent::setColorShiftEnd(unsigned int color) { mColorShiftEnd = color; - // Grab the opacity from the color shift because we may need to apply it if - // fading textures in - mOpacity = color & 0xff; updateColors(); } @@ -271,7 +266,6 @@ void ImageComponent::setColorGradientHorizontal(bool horizontal) void ImageComponent::setOpacity(unsigned char opacity) { mOpacity = opacity; - mColorShift = (mColorShift >> 8 << 8) | mOpacity; updateColors(); } @@ -282,17 +276,17 @@ void ImageComponent::updateVertices() // we go through this mess to make sure everything is properly rounded // if we just round vertices at the end, edge cases occur near sizes of 0.5 - const Vector2f topLeft = { mSize * mTopLeftCrop }; - const Vector2f bottomRight = { mSize * mBottomRightCrop }; - const float px = mTexture->isTiled() ? mSize.x() / getTextureSize().x() : 1.0f; - const float py = mTexture->isTiled() ? mSize.y() / getTextureSize().y() : 1.0f; - const unsigned int color = Renderer::convertColor(mColorShift); - const unsigned int colorEnd = Renderer::convertColor(mColorShiftEnd); + const Vector2f topLeft = { mSize * mTopLeftCrop }; + const Vector2f bottomRight = { mSize * mBottomRightCrop }; + const float px = mTexture->isTiled() ? mSize.x() / getTextureSize().x() : 1.0f; + const float py = mTexture->isTiled() ? mSize.y() / getTextureSize().y() : 1.0f; - mVertices[0] = { { topLeft.x(), topLeft.y() }, { mTopLeftCrop.x(), py - mTopLeftCrop.y() }, color }; - mVertices[1] = { { topLeft.x(), bottomRight.y() }, { mTopLeftCrop.x(), 1.0f - mBottomRightCrop.y() }, mColorGradientHorizontal ? colorEnd : color }; - mVertices[2] = { { bottomRight.x(), topLeft.y() }, { mBottomRightCrop.x() * px, py - mTopLeftCrop.y() }, mColorGradientHorizontal ? color : colorEnd }; - mVertices[3] = { { bottomRight.x(), bottomRight.y() }, { mBottomRightCrop.x() * px, 1.0f - mBottomRightCrop.y() }, color }; + mVertices[0] = { { topLeft.x(), topLeft.y() }, { mTopLeftCrop.x(), py - mTopLeftCrop.y() }, 0 }; + mVertices[1] = { { topLeft.x(), bottomRight.y() }, { mTopLeftCrop.x(), 1.0f - mBottomRightCrop.y() }, 0 }; + mVertices[2] = { { bottomRight.x(), topLeft.y() }, { mBottomRightCrop.x() * px, py - mTopLeftCrop.y() }, 0 }; + mVertices[3] = { { bottomRight.x(), bottomRight.y() }, { mBottomRightCrop.x() * px, 1.0f - mBottomRightCrop.y() }, 0 }; + + updateColors(); // round vertices for(int i = 0; i < 4; ++i) @@ -313,12 +307,13 @@ void ImageComponent::updateVertices() void ImageComponent::updateColors() { - const unsigned int color = Renderer::convertColor(mColorShift); - const unsigned int colorEnd = Renderer::convertColor(mColorShiftEnd); + const float opacity = (mOpacity * (mFading ? mFadeOpacity / 255.0 : 1.0)) / 255.0; + const unsigned int color = Renderer::convertColor(mColorShift & 0xFFFFFF00 | (unsigned char)((mColorShift & 0xFF) * opacity)); + const unsigned int colorEnd = Renderer::convertColor(mColorShiftEnd & 0xFFFFFF00 | (unsigned char)((mColorShiftEnd & 0xFF) * opacity)); mVertices[0].col = color; mVertices[1].col = mColorGradientHorizontal ? colorEnd : color; - mVertices[2].col = mColorGradientHorizontal ? color : colorEnd; + mVertices[2].col = mColorGradientHorizontal ? color : colorEnd; mVertices[3].col = colorEnd; } @@ -367,8 +362,6 @@ void ImageComponent::fadeIn(bool textureLoaded) // Start with a zero opacity and flag it as fading mFadeOpacity = 0; mFading = true; - // Set the colours to be translucent - mColorShift = (mColorShift >> 8 << 8) | 0; updateColors(); } } @@ -388,9 +381,6 @@ void ImageComponent::fadeIn(bool textureLoaded) { mFadeOpacity = (unsigned char)opacity; } - // Apply the combination of the target opacity and current fade - float newOpacity = (float)mOpacity * ((float)mFadeOpacity / 255.0f); - mColorShift = (mColorShift >> 8 << 8) | (unsigned char)newOpacity; updateColors(); } } @@ -435,10 +425,7 @@ void ImageComponent::applyTheme(const std::shared_ptr& theme, const s if(properties & COLOR) { if(elem->has("color")) - { setColorShift(elem->get("color")); - setColorShiftEnd(elem->get("color")); - } if (elem->has("colorEnd")) setColorShiftEnd(elem->get("colorEnd")); diff --git a/es-core/src/components/NinePatchComponent.cpp b/es-core/src/components/NinePatchComponent.cpp index 6ab1c89a0..50cec7642 100644 --- a/es-core/src/components/NinePatchComponent.cpp +++ b/es-core/src/components/NinePatchComponent.cpp @@ -29,7 +29,7 @@ void NinePatchComponent::updateColors() mVertices[i].col = edgeColor; for(int i = 6*4; i < 6; ++i) - mVertices[(6*4)+i].col = mCenterColor; + mVertices[(6*4)+i].col = centerColor; } void NinePatchComponent::buildVertices()