From 1a6beb552024990469f666e0dbb9f9870db1a446 Mon Sep 17 00:00:00 2001 From: jrassa Date: Thu, 8 Jun 2017 19:18:27 -0400 Subject: [PATCH] make color themable for ratings like normal images --- es-app/src/components/RatingComponent.cpp | 34 +++++++++++++++++++---- es-app/src/components/RatingComponent.h | 11 ++++++++ es-core/src/ThemeData.cpp | 1 + 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/es-app/src/components/RatingComponent.cpp b/es-app/src/components/RatingComponent.cpp index 3dbc36a0a..56923807e 100644 --- a/es-app/src/components/RatingComponent.cpp +++ b/es-app/src/components/RatingComponent.cpp @@ -3,7 +3,7 @@ #include "Window.h" #include "Util.h" -RatingComponent::RatingComponent(Window* window) : GuiComponent(window) +RatingComponent::RatingComponent(Window* window) : GuiComponent(window), mColorShift(0xFFFFFFFF) { mFilledTexture = TextureResource::get(":/star_filled.svg", true); mUnfilledTexture = TextureResource::get(":/star_unfilled.svg", true); @@ -37,6 +37,22 @@ std::string RatingComponent::getValue() const return ss.str(); } +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(); +} + void RatingComponent::onSizeChanged() { if(mSize.y() == 0) @@ -87,6 +103,11 @@ void RatingComponent::updateVertices() mVertices[11] = mVertices[7]; } +void RatingComponent::updateColors() +{ + Renderer::buildGLColorArray(mColors, mColorShift, 12); +} + void RatingComponent::render(const Eigen::Affine3f& parentTrans) { Eigen::Affine3f trans = roundMatrix(parentTrans * getTransform()); @@ -96,13 +117,13 @@ void RatingComponent::render(const Eigen::Affine3f& parentTrans) glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glColor4ub(255, 255, 255, getOpacity()); - glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &mVertices[0].pos); glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &mVertices[0].tex); + glColorPointer(4, GL_UNSIGNED_BYTE, 0, mColors); mFilledTexture->bind(); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -112,12 +133,11 @@ void RatingComponent::render(const Eigen::Affine3f& parentTrans) glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); - glColor4ub(255, 255, 255, 255); - renderChildren(trans); } @@ -157,6 +177,10 @@ void RatingComponent::applyTheme(const std::shared_ptr& theme, const imgChanged = true; } + + if(properties & COLOR && elem->has("color")) + setColorShift(elem->get("color")); + if(imgChanged) onSizeChanged(); } diff --git a/es-app/src/components/RatingComponent.h b/es-app/src/components/RatingComponent.h index a275c2701..5723aef9b 100644 --- a/es-app/src/components/RatingComponent.h +++ b/es-app/src/components/RatingComponent.h @@ -23,12 +23,18 @@ public: void onSizeChanged() override; + void setOpacity(unsigned char opacity) override; + + // Multiply all pixels in the image by this color when rendering. + void setColorShift(unsigned int color); + virtual void applyTheme(const std::shared_ptr& theme, const std::string& view, const std::string& element, unsigned int properties) override; virtual std::vector getHelpPrompts() override; private: void updateVertices(); + void updateColors(); float mValue; @@ -38,6 +44,11 @@ private: Eigen::Vector2f tex; } mVertices[12]; + + GLubyte mColors[12*4]; + + unsigned int mColorShift; + std::shared_ptr mFilledTexture; std::shared_ptr mUnfilledTexture; }; diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index ed6980613..ff92d6028 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -88,6 +88,7 @@ std::map< std::string, ElementMapType > ThemeData::sElementMap = boost::assign:: ("rating", makeMap(boost::assign::map_list_of ("pos", NORMALIZED_PAIR) ("size", NORMALIZED_PAIR) + ("color", COLOR) ("filledPath", PATH) ("unfilledPath", PATH) ("zIndex", FLOAT)))