From fa7b317982e7c4f51cd77bd81f0d9cba784952d9 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Wed, 1 Mar 2023 20:10:03 +0100 Subject: [PATCH] Added a new imageFit property to the carousel. --- .../components/primary/CarouselComponent.h | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/es-core/src/components/primary/CarouselComponent.h b/es-core/src/components/primary/CarouselComponent.h index d215261c1..caac2ac57 100644 --- a/es-core/src/components/primary/CarouselComponent.h +++ b/es-core/src/components/primary/CarouselComponent.h @@ -130,6 +130,12 @@ private: const size_t getNumEntries() override { return mEntries.size(); } const bool getFadeAbovePrimary() const override { return mFadeAbovePrimary; } + enum class ImageFit { + CONTAIN, + FILL, + COVER + }; + Renderer* mRenderer; std::function mCursorChangedCallback; std::function mCancelTransitionsCallback; @@ -180,6 +186,7 @@ private: float mReflectionsOpacity; float mReflectionsFalloff; float mUnfocusedItemOpacity; + ImageFit mImagefit; unsigned int mCarouselColor; unsigned int mCarouselColorEnd; bool mColorGradientHorizontal; @@ -247,6 +254,7 @@ CarouselComponent::CarouselComponent() , mReflectionsOpacity {0.5f} , mReflectionsFalloff {1.0f} , mUnfocusedItemOpacity {0.5f} + , mImagefit {ImageFit::CONTAIN} , mCarouselColor {0} , mCarouselColorEnd {0} , mColorGradientHorizontal {true} @@ -302,7 +310,13 @@ void CarouselComponent::addEntry(Entry& entry, const std::shared_ptr(false, dynamic); item->setLinearInterpolation(mLinearInterpolation); item->setMipmapping(true); - item->setMaxSize(glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + if (mImagefit == ImageFit::CONTAIN) + item->setMaxSize(glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + else if (mImagefit == ImageFit::FILL) + item->setResize(glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + else if (mImagefit == ImageFit::COVER) + item->setCroppedSize( + glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); item->setImage(entry.data.imagePath); item->applyTheme(theme, "system", "", ThemeFlags::ALL); if (mImageBrightness != 0.0) @@ -324,8 +338,15 @@ void CarouselComponent::addEntry(Entry& entry, const std::shared_ptr(false, dynamic); mDefaultImage->setLinearInterpolation(mLinearInterpolation); mDefaultImage->setMipmapping(true); - mDefaultImage->setMaxSize( - glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + if (mImagefit == ImageFit::CONTAIN) + mDefaultImage->setMaxSize( + glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + else if (mImagefit == ImageFit::FILL) + mDefaultImage->setResize( + glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + else if (mImagefit == ImageFit::COVER) + mDefaultImage->setCroppedSize( + glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); mDefaultImage->setImage(entry.data.defaultImagePath); mDefaultImage->applyTheme(theme, "system", "", ThemeFlags::ALL); if (mImageBrightness != 0.0) @@ -400,7 +421,12 @@ void CarouselComponent::updateEntry(Entry& entry, const std::shared_ptr(false, true); item->setLinearInterpolation(mLinearInterpolation); item->setMipmapping(true); - item->setMaxSize(glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + if (mImagefit == ImageFit::CONTAIN) + item->setMaxSize(glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + else if (mImagefit == ImageFit::FILL) + item->setResize(glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); + else if (mImagefit == ImageFit::COVER) + item->setCroppedSize(glm::round(mItemSize * (mItemScale >= 1.0f ? mItemScale : 1.0f))); item->setImage(entry.data.imagePath); item->applyTheme(theme, "system", "", ThemeFlags::ALL); if (mImageBrightness != 0.0) @@ -1401,6 +1427,25 @@ void CarouselComponent::applyTheme(const std::shared_ptr& theme, if (elem->has("itemScale")) mItemScale = glm::clamp(elem->get("itemScale"), 0.2f, 3.0f); + if (elem->has("imageFit")) { + const std::string& imageFit {elem->get("imageFit")}; + if (imageFit == "contain") { + mImagefit = ImageFit::CONTAIN; + } + else if (imageFit == "fill") { + mImagefit = ImageFit::FILL; + } + else if (imageFit == "cover") { + mImagefit = ImageFit::COVER; + } + else { + mImagefit = ImageFit::CONTAIN; + LOG(LogWarning) << "CarouselComponent: Invalid theme configuration, property " + "\"imageFit\" for element \"" + << element.substr(9) << "\" defined as \"" << imageFit << "\""; + } + } + mImageSelectedColor = mImageColorShift; mImageSelectedColorEnd = mImageColorShiftEnd;