clean up opacity handling for ImageComponent, RatingComponent

This commit is contained in:
John Rassa 2019-08-31 09:06:23 -04:00
parent c384beedb0
commit b43f1b43d3
3 changed files with 32 additions and 47 deletions

View file

@ -41,16 +41,12 @@ std::string RatingComponent::getValue() const
void RatingComponent::setOpacity(unsigned char opacity) void RatingComponent::setOpacity(unsigned char opacity)
{ {
mOpacity = opacity; mOpacity = opacity;
mColorShift = (mColorShift >> 8 << 8) | mOpacity;
updateColors(); updateColors();
} }
void RatingComponent::setColorShift(unsigned int color) void RatingComponent::setColorShift(unsigned int color)
{ {
mColorShift = 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(); updateColors();
} }
@ -75,21 +71,22 @@ void RatingComponent::onSizeChanged()
void RatingComponent::updateVertices() void RatingComponent::updateVertices()
{ {
const float numStars = NUM_RATING_STARS; const float numStars = NUM_RATING_STARS;
const float h = getSize().y(); // is the same as a single star's width const float h = getSize().y(); // is the same as a single star's width
const float w = getSize().y() * mValue * numStars; const float w = getSize().y() * mValue * numStars;
const float fw = getSize().y() * numStars; const float fw = getSize().y() * numStars;
const unsigned int color = Renderer::convertColor(mColorShift);
mVertices[0] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, color }; mVertices[0] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, 0 };
mVertices[1] = { { 0.0f, h }, { 0.0f, 0.0f }, color }; mVertices[1] = { { 0.0f, h }, { 0.0f, 0.0f }, 0 };
mVertices[2] = { { w, 0.0f }, { mValue * numStars, 1.0f }, color }; mVertices[2] = { { w, 0.0f }, { mValue * numStars, 1.0f }, 0 };
mVertices[3] = { { w, h }, { mValue * numStars, 0.0f }, color }; mVertices[3] = { { w, h }, { mValue * numStars, 0.0f }, 0 };
mVertices[4] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, color }; mVertices[4] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, 0 };
mVertices[5] = { { 0.0f, h }, { 0.0f, 0.0f }, color }; mVertices[5] = { { 0.0f, h }, { 0.0f, 0.0f }, 0 };
mVertices[6] = { { fw, 0.0f }, { numStars, 1.0f }, color }; mVertices[6] = { { fw, 0.0f }, { numStars, 1.0f }, 0 };
mVertices[7] = { { fw, h }, { numStars, 0.0f }, color }; mVertices[7] = { { fw, h }, { numStars, 0.0f }, 0 };
updateColors();
// round vertices // round vertices
for(int i = 0; i < 8; ++i) for(int i = 0; i < 8; ++i)
@ -98,7 +95,8 @@ void RatingComponent::updateVertices()
void RatingComponent::updateColors() 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) for(int i = 0; i < 8; ++i)
mVertices[i].col = color; mVertices[i].col = color;

View file

@ -247,18 +247,13 @@ void ImageComponent::setFlipY(bool flip)
void ImageComponent::setColorShift(unsigned int color) void ImageComponent::setColorShift(unsigned int color)
{ {
mColorShift = color; mColorShift = color;
// Grab the opacity from the color shift because we may need to apply it if mColorShiftEnd = color;
// fading textures in
mOpacity = color & 0xff;
updateColors(); updateColors();
} }
void ImageComponent::setColorShiftEnd(unsigned int color) void ImageComponent::setColorShiftEnd(unsigned int color)
{ {
mColorShiftEnd = 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(); updateColors();
} }
@ -271,7 +266,6 @@ void ImageComponent::setColorGradientHorizontal(bool horizontal)
void ImageComponent::setOpacity(unsigned char opacity) void ImageComponent::setOpacity(unsigned char opacity)
{ {
mOpacity = opacity; mOpacity = opacity;
mColorShift = (mColorShift >> 8 << 8) | mOpacity;
updateColors(); updateColors();
} }
@ -282,17 +276,17 @@ void ImageComponent::updateVertices()
// we go through this mess to make sure everything is properly rounded // 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 // if we just round vertices at the end, edge cases occur near sizes of 0.5
const Vector2f topLeft = { mSize * mTopLeftCrop }; const Vector2f topLeft = { mSize * mTopLeftCrop };
const Vector2f bottomRight = { mSize * mBottomRightCrop }; const Vector2f bottomRight = { mSize * mBottomRightCrop };
const float px = mTexture->isTiled() ? mSize.x() / getTextureSize().x() : 1.0f; const float px = mTexture->isTiled() ? mSize.x() / getTextureSize().x() : 1.0f;
const float py = mTexture->isTiled() ? mSize.y() / getTextureSize().y() : 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);
mVertices[0] = { { topLeft.x(), topLeft.y() }, { mTopLeftCrop.x(), py - mTopLeftCrop.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() }, mColorGradientHorizontal ? colorEnd : color }; 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() }, mColorGradientHorizontal ? color : colorEnd }; 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() }, color }; mVertices[3] = { { bottomRight.x(), bottomRight.y() }, { mBottomRightCrop.x() * px, 1.0f - mBottomRightCrop.y() }, 0 };
updateColors();
// round vertices // round vertices
for(int i = 0; i < 4; ++i) for(int i = 0; i < 4; ++i)
@ -313,12 +307,13 @@ void ImageComponent::updateVertices()
void ImageComponent::updateColors() void ImageComponent::updateColors()
{ {
const unsigned int color = Renderer::convertColor(mColorShift); const float opacity = (mOpacity * (mFading ? mFadeOpacity / 255.0 : 1.0)) / 255.0;
const unsigned int colorEnd = Renderer::convertColor(mColorShiftEnd); 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[0].col = color;
mVertices[1].col = mColorGradientHorizontal ? colorEnd : color; mVertices[1].col = mColorGradientHorizontal ? colorEnd : color;
mVertices[2].col = mColorGradientHorizontal ? color : colorEnd; mVertices[2].col = mColorGradientHorizontal ? color : colorEnd;
mVertices[3].col = colorEnd; mVertices[3].col = colorEnd;
} }
@ -367,8 +362,6 @@ void ImageComponent::fadeIn(bool textureLoaded)
// Start with a zero opacity and flag it as fading // Start with a zero opacity and flag it as fading
mFadeOpacity = 0; mFadeOpacity = 0;
mFading = true; mFading = true;
// Set the colours to be translucent
mColorShift = (mColorShift >> 8 << 8) | 0;
updateColors(); updateColors();
} }
} }
@ -388,9 +381,6 @@ void ImageComponent::fadeIn(bool textureLoaded)
{ {
mFadeOpacity = (unsigned char)opacity; 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(); updateColors();
} }
} }
@ -435,10 +425,7 @@ void ImageComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const s
if(properties & COLOR) if(properties & COLOR)
{ {
if(elem->has("color")) if(elem->has("color"))
{
setColorShift(elem->get<unsigned int>("color")); setColorShift(elem->get<unsigned int>("color"));
setColorShiftEnd(elem->get<unsigned int>("color"));
}
if (elem->has("colorEnd")) if (elem->has("colorEnd"))
setColorShiftEnd(elem->get<unsigned int>("colorEnd")); setColorShiftEnd(elem->get<unsigned int>("colorEnd"));

View file

@ -29,7 +29,7 @@ void NinePatchComponent::updateColors()
mVertices[i].col = edgeColor; mVertices[i].col = edgeColor;
for(int i = 6*4; i < 6; ++i) for(int i = 6*4; i < 6; ++i)
mVertices[(6*4)+i].col = mCenterColor; mVertices[(6*4)+i].col = centerColor;
} }
void NinePatchComponent::buildVertices() void NinePatchComponent::buildVertices()