Skip to content

Commit

Permalink
Merge pull request Aloshi#574 from jrassa/gradients
Browse files Browse the repository at this point in the history
support for color gradients in drawRect and ImageComponent
  • Loading branch information
tomaz82 authored Aug 30, 2019
2 parents 79cc5a7 + fc58af9 commit eadbfdf
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 46 deletions.
8 changes: 4 additions & 4 deletions es-app/src/SystemScreenSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void SystemScreenSaver::renderScreenSaver()
{
// Render black background
Renderer::setMatrix(Transform4x4f::Identity());
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), (unsigned char)(255));
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x000000FF, 0x000000FF);

// Only render the video if the state requires it
if ((int)mState >= STATE_FADE_IN_VIDEO)
Expand All @@ -222,7 +222,7 @@ void SystemScreenSaver::renderScreenSaver()
{
// Render black background
Renderer::setMatrix(Transform4x4f::Identity());
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), (unsigned char)(255));
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x000000FF, 0x000000FF);

// Only render the video if the state requires it
if ((int)mState >= STATE_FADE_IN_VIDEO)
Expand All @@ -248,8 +248,8 @@ void SystemScreenSaver::renderScreenSaver()
else if (mState != STATE_INACTIVE)
{
Renderer::setMatrix(Transform4x4f::Identity());
unsigned char opacity = screensaver_behavior == "dim" ? 0xA0 : 0xFF;
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x00000000 | opacity);
unsigned char color = screensaver_behavior == "dim" ? 0x000000A0 : 0x000000FF;
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), color, color);
}
}

Expand Down
2 changes: 1 addition & 1 deletion es-app/src/components/AsyncReqComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void AsyncReqComponent::render(const Transform4x4f& /*parentTrans*/)
Renderer::setMatrix(trans);

Vector3f point(Math::cosf(mTime * 0.01f) * 12, Math::sinf(mTime * 0.01f) * 12, 0);
Renderer::drawRect((int)point.x(), (int)point.y(), 8, 8, 0x0000FFFF);
Renderer::drawRect((int)point.x(), (int)point.y(), 8, 8, 0x0000FFFF, 0x0000FFFF);
}

std::vector<HelpPrompt> AsyncReqComponent::getHelpPrompts()
Expand Down
4 changes: 2 additions & 2 deletions es-app/src/components/ScraperSearchComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ void ScraperSearchComponent::render(const Transform4x4f& parentTrans)
if(mBlockAccept)
{
Renderer::setMatrix(trans);
Renderer::drawRect(0.f, 0.f, mSize.x(), mSize.y(), 0x00000011);
Renderer::drawRect(0.f, 0.f, mSize.x(), mSize.y(), 0x00000011, 0x00000011);
//Renderer::drawRect((int)mResultList->getPosition().x(), (int)mResultList->getPosition().y(),
// (int)mResultList->getSize().x(), (int)mResultList->getSize().y(), 0x00000011);
// (int)mResultList->getSize().x(), (int)mResultList->getSize().y(), 0x0000011, 0x00000011);

mBusyAnim.render(trans);
}
Expand Down
17 changes: 16 additions & 1 deletion es-app/src/components/TextListComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class TextListComponent : public IList<TextListData, T>
inline void setSelectorHeight(float selectorScale) { mSelectorHeight = selectorScale; }
inline void setSelectorOffsetY(float selectorOffsetY) { mSelectorOffsetY = selectorOffsetY; }
inline void setSelectorColor(unsigned int color) { mSelectorColor = color; }
inline void setSelectorColorEnd(unsigned int color) { mSelectorColorEnd = color; }
inline void setSelectorColorGradientHorizontal(bool horizontal) { mSelectorColorGradientHorizontal = horizontal; }
inline void setSelectedColor(unsigned int color) { mSelectedColor = color; }
inline void setColor(unsigned int id, unsigned int color) { mColors[id] = color; }
inline void setLineSpacing(float lineSpacing) { mLineSpacing = lineSpacing; }
Expand All @@ -97,6 +99,8 @@ class TextListComponent : public IList<TextListData, T>
float mSelectorHeight;
float mSelectorOffsetY;
unsigned int mSelectorColor;
unsigned int mSelectorColorEnd;
bool mSelectorColorGradientHorizontal = true;
unsigned int mSelectedColor;
std::string mScrollSound;
static const unsigned int COLOR_ID_COUNT = 2;
Expand All @@ -122,6 +126,8 @@ TextListComponent<T>::TextListComponent(Window* window) :
mSelectorHeight = mFont->getSize() * 1.5f;
mSelectorOffsetY = 0;
mSelectorColor = 0x000000FF;
mSelectorColorEnd = 0x000000FF;
mSelectorColorGradientHorizontal = true;
mSelectedColor = 0;
mColors[0] = 0x0000FFFF;
mColors[1] = 0x00FF00FF;
Expand Down Expand Up @@ -167,7 +173,8 @@ void TextListComponent<T>::render(const Transform4x4f& parentTrans)
mSelectorImage.render(trans);
} else {
Renderer::setMatrix(trans);
Renderer::drawRect(0.f, (mCursor - startEntry)*entrySize + mSelectorOffsetY, mSize.x(), mSelectorHeight, mSelectorColor);
Renderer::drawRect(0.f, (mCursor - startEntry)*entrySize + mSelectorOffsetY, mSize.x(),
mSelectorHeight, mSelectorColor, mSelectorColorEnd, mSelectorColorGradientHorizontal);
}
}

Expand Down Expand Up @@ -363,7 +370,14 @@ void TextListComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme, c
if(properties & COLOR)
{
if(elem->has("selectorColor"))
{
setSelectorColor(elem->get<unsigned int>("selectorColor"));
setSelectorColorEnd(elem->get<unsigned int>("selectorColor"));
}
if (elem->has("selectorColorEnd"))
setSelectorColorEnd(elem->get<unsigned int>("selectorColorEnd"));
if (elem->has("selectorGradientType"))
setSelectorColorGradientHorizontal(!(elem->get<std::string>("selectorGradientType").compare("horizontal")));
if(elem->has("selectedColor"))
setSelectedColor(elem->get<unsigned int>("selectedColor"));
if(elem->has("primaryColor"))
Expand Down Expand Up @@ -426,6 +440,7 @@ void TextListComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme, c
mSelectorImage.setImage(path, tile);
mSelectorImage.setSize(mSize.x(), mSelectorHeight);
mSelectorImage.setColorShift(mSelectorColor);
mSelectorImage.setColorShiftEnd(mSelectorColorEnd);
} else {
mSelectorImage.setImage("");
}
Expand Down
14 changes: 12 additions & 2 deletions es-app/src/views/SystemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ void SystemView::renderCarousel(const Transform4x4f& trans)
Renderer::pushClipRect(Vector2i((int)clipPos.x(), (int)clipPos.y()), Vector2i((int)mCarousel.size.x(), (int)mCarousel.size.y()));

Renderer::setMatrix(carouselTrans);
Renderer::drawRect(0.0, 0.0, mCarousel.size.x(), mCarousel.size.y(), mCarousel.color);
Renderer::drawRect(0.0, 0.0, mCarousel.size.x(), mCarousel.size.y(), mCarousel.color, mCarousel.colorEnd, mCarousel.colorGradientHorizontal);

// draw logos
Vector2f logoSpacing(0.0, 0.0); // NB: logoSpacing will include the size of the logo itself as well!
Expand Down Expand Up @@ -584,8 +584,9 @@ void SystemView::renderFade(const Transform4x4f& trans)
// fade extras if necessary
if (mExtrasFadeOpacity)
{
unsigned int fadeColor = 0x00000000 | (unsigned char)(mExtrasFadeOpacity * 255);
Renderer::setMatrix(trans);
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0x00000000 | (unsigned char)(mExtrasFadeOpacity * 255));
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), fadeColor, fadeColor);
}
}

Expand All @@ -602,6 +603,8 @@ void SystemView::getDefaultElements(void)
mCarousel.origin.x() = 0.0f;
mCarousel.origin.y() = 0.0f;
mCarousel.color = 0xFFFFFFD8;
mCarousel.colorEnd = 0xFFFFFFD8;
mCarousel.colorGradientHorizontal = true;
mCarousel.logoScale = 1.2f;
mCarousel.logoRotation = 7.5;
mCarousel.logoRotationOrigin.x() = -5;
Expand Down Expand Up @@ -642,7 +645,14 @@ void SystemView::getCarouselFromTheme(const ThemeData::ThemeElement* elem)
if (elem->has("origin"))
mCarousel.origin = elem->get<Vector2f>("origin");
if (elem->has("color"))
{
mCarousel.color = elem->get<unsigned int>("color");
mCarousel.colorEnd = mCarousel.color;
}
if (elem->has("colorEnd"))
mCarousel.colorEnd = elem->get<unsigned int>("colorEnd");
if (elem->has("gradientType"))
mCarousel.colorGradientHorizontal = !(elem->get<std::string>("gradientType").compare("horizontal"));
if (elem->has("logoScale"))
mCarousel.logoScale = elem->get<float>("logoScale");
if (elem->has("logoSize"))
Expand Down
2 changes: 2 additions & 0 deletions es-app/src/views/SystemView.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct SystemViewCarousel
Vector2f logoRotationOrigin;
Alignment logoAlignment;
unsigned int color;
unsigned int colorEnd;
bool colorGradientHorizontal;
int maxLogoCount; // number of logos shown on the carousel
Vector2f logoSize;
float zIndex;
Expand Down
3 changes: 2 additions & 1 deletion es-app/src/views/ViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ void ViewController::render(const Transform4x4f& parentTrans)
// fade out
if(mFadeOpacity)
{
unsigned int fadeColor = 0x00000000 | (unsigned char)(mFadeOpacity * 255);
Renderer::setMatrix(parentTrans);
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x00000000 | (unsigned char)(mFadeOpacity * 255));
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), fadeColor, fadeColor);
}
}

Expand Down
6 changes: 6 additions & 0 deletions es-core/src/ThemeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>> The
{ "default", PATH },
{ "tile", BOOLEAN },
{ "color", COLOR },
{ "colorEnd", COLOR },
{ "gradientType", STRING },
{ "visible", BOOLEAN },
{ "zIndex", FLOAT } } },
{ "imagegrid", {
Expand Down Expand Up @@ -66,6 +68,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>> The
{ "selectorHeight", FLOAT },
{ "selectorOffsetY", FLOAT },
{ "selectorColor", COLOR },
{ "selectorColorEnd", COLOR },
{ "selectorGradientType", STRING },
{ "selectorImagePath", PATH },
{ "selectorImageTile", BOOLEAN },
{ "selectedColor", COLOR },
Expand Down Expand Up @@ -148,6 +152,8 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>> The
{ "pos", NORMALIZED_PAIR },
{ "origin", NORMALIZED_PAIR },
{ "color", COLOR },
{ "colorEnd", COLOR },
{ "gradientType", STRING },
{ "logoScale", FLOAT },
{ "logoRotation", FLOAT },
{ "logoRotationOrigin", NORMALIZED_PAIR },
Expand Down
2 changes: 1 addition & 1 deletion es-core/src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void Window::renderLoadingScreen(std::string text)
{
Transform4x4f trans = Transform4x4f::Identity();
Renderer::setMatrix(trans);
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x000000FF);
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x000000FF, 0x000000FF);

ImageComponent splash(this, true);
splash.setResize(Renderer::getScreenWidth() * 0.6f, 0.0f);
Expand Down
12 changes: 6 additions & 6 deletions es-core/src/components/ComponentList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ void ComponentList::render(const Transform4x4f& parentTrans)
// (1 - dst) + 0x77

const float selectedRowHeight = getRowHeight(mEntries.at(mCursor).data);
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight, 0xFFFFFFFF, Renderer::Blend::ONE_MINUS_DST_COLOR, Renderer::Blend::ZERO);
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight, 0x777777FF, Renderer::Blend::ONE, Renderer::Blend::ONE);
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight, 0xFFFFFFFF, 0xFFFFFFFF, false, Renderer::Blend::ONE_MINUS_DST_COLOR, Renderer::Blend::ZERO);
Renderer::drawRect(0.0f, mSelectorBarOffset, mSize.x(), selectedRowHeight, 0x777777FF, 0x777777FF, false, Renderer::Blend::ONE, Renderer::Blend::ONE);

// hack to draw 2px dark on left/right of the bar
Renderer::drawRect(0.0f, mSelectorBarOffset, 2.0f, selectedRowHeight, 0x878787FF);
Renderer::drawRect(mSize.x() - 2.0f, mSelectorBarOffset, 2.0f, selectedRowHeight, 0x878787FF);
Renderer::drawRect(0.0f, mSelectorBarOffset, 2.0f, selectedRowHeight, 0x878787FF, 0x878787FF);
Renderer::drawRect(mSize.x() - 2.0f, mSelectorBarOffset, 2.0f, selectedRowHeight, 0x878787FF, 0x878787FF);

for(auto it = drawAfterCursor.cbegin(); it != drawAfterCursor.cend(); it++)
(*it)->render(trans);
Expand All @@ -221,10 +221,10 @@ void ComponentList::render(const Transform4x4f& parentTrans)
float y = 0;
for(unsigned int i = 0; i < mEntries.size(); i++)
{
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF);
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF, 0xC6C7C6FF);
y += getRowHeight(mEntries.at(i).data);
}
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF);
Renderer::drawRect(0.0f, y, mSize.x(), 1.0f, 0xC6C7C6FF, 0xC6C7C6FF);

Renderer::popClipRect();
}
Expand Down
2 changes: 1 addition & 1 deletion es-core/src/components/DateTimeEditComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void DateTimeEditComponent::render(const Transform4x4f& parentTrans)
if(mEditIndex >= 0 && (unsigned int)mEditIndex < mCursorBoxes.size())
{
Renderer::drawRect((int)mCursorBoxes[mEditIndex][0], (int)mCursorBoxes[mEditIndex][1],
(int)mCursorBoxes[mEditIndex][2], (int)mCursorBoxes[mEditIndex][3], 0x00000022);
(int)mCursorBoxes[mEditIndex][2], (int)mCursorBoxes[mEditIndex][3], 0x00000022, 0x00000022);
}
}
}
Expand Down
53 changes: 42 additions & 11 deletions es-core/src/components/ImageComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Vector2f ImageComponent::getSize() const

ImageComponent::ImageComponent(Window* window, bool forceLoad, bool dynamic) : GuiComponent(window),
mTargetIsMax(false), mTargetIsMin(false), mFlipX(false), mFlipY(false), mTargetSize(0, 0), mColorShift(0xFFFFFFFF),
mForceLoad(forceLoad), mDynamic(dynamic), mFadeOpacity(0), mFading(false), mRotateByTargetSize(false),
mTopLeftCrop(0.0f, 0.0f), mBottomRightCrop(1.0f, 1.0f)
mColorShiftEnd(0xFFFFFFFF), mColorGradientHorizontal(true), mForceLoad(forceLoad), mDynamic(dynamic),
mFadeOpacity(0), mFading(false), mRotateByTargetSize(false), mTopLeftCrop(0.0f, 0.0f), mBottomRightCrop(1.0f, 1.0f)
{
updateColors();
}
Expand Down Expand Up @@ -253,6 +253,21 @@ void ImageComponent::setColorShift(unsigned int 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();
}

void ImageComponent::setColorGradientHorizontal(bool horizontal)
{
mColorGradientHorizontal = horizontal;
updateColors();
}

void ImageComponent::setOpacity(unsigned char opacity)
{
mOpacity = opacity;
Expand All @@ -273,10 +288,11 @@ void ImageComponent::updateVertices()
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);

mVertices[0] = { { topLeft.x(), topLeft.y() }, { mTopLeftCrop.x(), py - mTopLeftCrop.y() }, color };
mVertices[1] = { { topLeft.x(), bottomRight.y() }, { mTopLeftCrop.x(), 1.0f - mBottomRightCrop.y() }, color };
mVertices[2] = { { bottomRight.x(), topLeft.y() }, { mBottomRightCrop.x() * px, 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 };

if(mFlipX)
Expand All @@ -293,10 +309,13 @@ void ImageComponent::updateVertices()

void ImageComponent::updateColors()
{
const unsigned int color = Renderer::convertColor(mColorShift);
const unsigned int color = Renderer::convertColor(mColorShift);
const unsigned int colorEnd = Renderer::convertColor(mColorShiftEnd);

for(int i = 0; i < 4; ++i)
mVertices[i].col = color;
mVertices[0].col = color;
mVertices[1].col = mColorGradientHorizontal ? colorEnd : color;
mVertices[2].col = mColorGradientHorizontal ? color : colorEnd;
mVertices[3].col = colorEnd;
}

void ImageComponent::render(const Transform4x4f& parentTrans)
Expand All @@ -311,8 +330,8 @@ void ImageComponent::render(const Transform4x4f& parentTrans)
{
if(Settings::getInstance()->getBool("DebugImage")) {
Vector2f targetSizePos = (mTargetSize - mSize) * mOrigin * -1;
Renderer::drawRect(targetSizePos.x(), targetSizePos.y(), mTargetSize.x(), mTargetSize.y(), 0xFF000033);
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0x00000033);
Renderer::drawRect(targetSizePos.x(), targetSizePos.y(), mTargetSize.x(), mTargetSize.y(), 0xFF000033, 0xFF000033);
Renderer::drawRect(0.0f, 0.0f, mSize.x(), mSize.y(), 0x00000033, 0x00000033);
}
if(mTexture->isInitialized())
{
Expand Down Expand Up @@ -420,8 +439,20 @@ void ImageComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const s
setImage(elem->get<std::string>("path"), tile);
}

if(properties & COLOR && elem->has("color"))
setColorShift(elem->get<unsigned int>("color"));
if(properties & COLOR)
{
if(elem->has("color"))
{
setColorShift(elem->get<unsigned int>("color"));
setColorShiftEnd(elem->get<unsigned int>("color"));
}

if (elem->has("colorEnd"))
setColorShiftEnd(elem->get<unsigned int>("colorEnd"));

if (elem->has("gradientType"))
setColorGradientHorizontal(!(elem->get<std::string>("gradientType").compare("horizontal")));
}

if(properties & ThemeFlags::ROTATION) {
if(elem->has("rotation"))
Expand Down
4 changes: 4 additions & 0 deletions es-core/src/components/ImageComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class ImageComponent : public GuiComponent

// Multiply all pixels in the image by this color when rendering.
void setColorShift(unsigned int color);
void setColorShiftEnd(unsigned int color);
void setColorGradientHorizontal(bool horizontal);

void setFlipX(bool flip); // Mirror on the X axis.
void setFlipY(bool flip); // Mirror on the Y axis.
Expand Down Expand Up @@ -89,6 +91,8 @@ class ImageComponent : public GuiComponent
void fadeIn(bool textureLoaded);

unsigned int mColorShift;
unsigned int mColorShiftEnd;
bool mColorGradientHorizontal;

std::string mDefaultPath;

Expand Down
2 changes: 1 addition & 1 deletion es-core/src/components/SliderComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void SliderComponent::render(const Transform4x4f& parentTrans)

//render line
const float lineWidth = 2;
Renderer::drawRect(mKnob.getSize().x() / 2, mSize.y() / 2 - lineWidth / 2, width, lineWidth, 0x777777FF);
Renderer::drawRect(mKnob.getSize().x() / 2, mSize.y() / 2 - lineWidth / 2, width, lineWidth, 0x777777FF, 0x777777FF);

//render knob
mKnob.render(trans);
Expand Down
Loading

0 comments on commit eadbfdf

Please sign in to comment.