From f53650072b0d4b544f4be4d3e924692b37498592 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 2 Jun 2024 17:01:38 +0200 Subject: [PATCH] Added a 'selectedItemOffset' property to the carousel element --- es-core/src/ThemeData.cpp | 1 + .../components/primary/CarouselComponent.h | 28 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index 7cedb242d..76e149d0f 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -127,6 +127,7 @@ std::map> {"itemsAfterCenter", UNSIGNED_INTEGER}, {"itemStacking", STRING}, {"selectedItemMargins", NORMALIZED_PAIR}, + {"selectedItemOffset", NORMALIZED_PAIR}, {"itemSize", NORMALIZED_PAIR}, {"itemScale", FLOAT}, {"itemRotation", FLOAT}, diff --git a/es-core/src/components/primary/CarouselComponent.h b/es-core/src/components/primary/CarouselComponent.h index 76ba383de..1e950788e 100644 --- a/es-core/src/components/primary/CarouselComponent.h +++ b/es-core/src/components/primary/CarouselComponent.h @@ -159,6 +159,7 @@ private: int mItemsAfterCenter; ItemStacking mItemStacking; glm::vec2 mSelectedItemMargins; + glm::vec2 mSelectedItemOffset; glm::vec2 mItemSize; float mItemScale; float mItemRotation; @@ -234,6 +235,7 @@ CarouselComponent::CarouselComponent() , mItemsAfterCenter {8} , mItemStacking {ItemStacking::CENTERED} , mSelectedItemMargins {0.0f, 0.0f} + , mSelectedItemOffset {0.0f, 0.0f} , mItemSize {glm::vec2 {Renderer::getScreenWidth() * 0.25f, Renderer::getScreenHeight() * 0.155f}} , mItemScale {1.2f} @@ -512,7 +514,7 @@ template void CarouselComponent::onDemandTextureLoad() itemInclusion += 1; } - for (int i = center - itemInclusion - itemInclusionBefore; + for (int i {center - itemInclusion - itemInclusionBefore}; i < center + itemInclusion + itemInclusionAfter; ++i) { int cursor {i}; @@ -918,13 +920,25 @@ template void CarouselComponent::render(const glm::mat4& parentT selectedItemMargins *= std::fabs(distance); } + float itemHorizontalOffset {0.0f}; + float itemVerticallOffset {0.0f}; + + if ((mSelectedItemOffset.x != 0.0f || mSelectedItemOffset.y != 0.0f) && + std::fabs(distance) < 1.0f) { + itemHorizontalOffset = (1.0f - std::fabs(distance)) * mSelectedItemOffset.x; + itemVerticallOffset = (1.0f - std::fabs(distance)) * mSelectedItemOffset.y; + } + glm::mat4 itemTrans {carouselTrans}; if (singleEntry) itemTrans = glm::translate(carouselTrans, glm::vec3 {xOff, yOff, 0.0f}); else itemTrans = glm::translate( - itemTrans, glm::vec3 {(i * itemSpacing.x) + xOff + selectedItemMargins.x, - (i * itemSpacing.y) + yOff + selectedItemMargins.y, 0.0f}); + itemTrans, + glm::vec3 {(i * itemSpacing.x) + xOff + itemHorizontalOffset + + selectedItemMargins.x, + (i * itemSpacing.y) + yOff + itemVerticallOffset + selectedItemMargins.y, + 0.0f}); if (mType == CarouselType::HORIZONTAL_WHEEL) itemTrans = glm::rotate(itemTrans, glm::radians(-90.0f), glm::vec3 {0.0f, 0.0f, 1.0f}); @@ -1377,6 +1391,14 @@ void CarouselComponent::applyTheme(const std::shared_ptr& theme, else mSelectedItemMargins = selectedItemMargins * Renderer::getScreenHeight(); } + if (elem->has("selectedItemOffset")) { + const glm::vec2 selectedItemOffset { + glm::clamp(elem->get("selectedItemOffset"), -1.0f, 1.0f)}; + if (mType == CarouselType::HORIZONTAL) + mSelectedItemOffset = selectedItemOffset * Renderer::getScreenWidth(); + else + mSelectedItemOffset = selectedItemOffset * Renderer::getScreenHeight(); + } } if (elem->has("itemSize")) {