Added a 'selectedItemOffset' property to the carousel element

This commit is contained in:
Leon Styhre 2024-06-02 17:01:38 +02:00
parent bb546c366c
commit f53650072b
2 changed files with 26 additions and 3 deletions

View file

@ -127,6 +127,7 @@ std::map<std::string, std::map<std::string, ThemeData::ElementPropertyType>>
{"itemsAfterCenter", UNSIGNED_INTEGER}, {"itemsAfterCenter", UNSIGNED_INTEGER},
{"itemStacking", STRING}, {"itemStacking", STRING},
{"selectedItemMargins", NORMALIZED_PAIR}, {"selectedItemMargins", NORMALIZED_PAIR},
{"selectedItemOffset", NORMALIZED_PAIR},
{"itemSize", NORMALIZED_PAIR}, {"itemSize", NORMALIZED_PAIR},
{"itemScale", FLOAT}, {"itemScale", FLOAT},
{"itemRotation", FLOAT}, {"itemRotation", FLOAT},

View file

@ -159,6 +159,7 @@ private:
int mItemsAfterCenter; int mItemsAfterCenter;
ItemStacking mItemStacking; ItemStacking mItemStacking;
glm::vec2 mSelectedItemMargins; glm::vec2 mSelectedItemMargins;
glm::vec2 mSelectedItemOffset;
glm::vec2 mItemSize; glm::vec2 mItemSize;
float mItemScale; float mItemScale;
float mItemRotation; float mItemRotation;
@ -234,6 +235,7 @@ CarouselComponent<T>::CarouselComponent()
, mItemsAfterCenter {8} , mItemsAfterCenter {8}
, mItemStacking {ItemStacking::CENTERED} , mItemStacking {ItemStacking::CENTERED}
, mSelectedItemMargins {0.0f, 0.0f} , mSelectedItemMargins {0.0f, 0.0f}
, mSelectedItemOffset {0.0f, 0.0f}
, mItemSize {glm::vec2 {Renderer::getScreenWidth() * 0.25f, , mItemSize {glm::vec2 {Renderer::getScreenWidth() * 0.25f,
Renderer::getScreenHeight() * 0.155f}} Renderer::getScreenHeight() * 0.155f}}
, mItemScale {1.2f} , mItemScale {1.2f}
@ -512,7 +514,7 @@ template <typename T> void CarouselComponent<T>::onDemandTextureLoad()
itemInclusion += 1; itemInclusion += 1;
} }
for (int i = center - itemInclusion - itemInclusionBefore; for (int i {center - itemInclusion - itemInclusionBefore};
i < center + itemInclusion + itemInclusionAfter; ++i) { i < center + itemInclusion + itemInclusionAfter; ++i) {
int cursor {i}; int cursor {i};
@ -918,13 +920,25 @@ template <typename T> void CarouselComponent<T>::render(const glm::mat4& parentT
selectedItemMargins *= std::fabs(distance); 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}; glm::mat4 itemTrans {carouselTrans};
if (singleEntry) if (singleEntry)
itemTrans = glm::translate(carouselTrans, glm::vec3 {xOff, yOff, 0.0f}); itemTrans = glm::translate(carouselTrans, glm::vec3 {xOff, yOff, 0.0f});
else else
itemTrans = glm::translate( itemTrans = glm::translate(
itemTrans, glm::vec3 {(i * itemSpacing.x) + xOff + selectedItemMargins.x, itemTrans,
(i * itemSpacing.y) + yOff + selectedItemMargins.y, 0.0f}); glm::vec3 {(i * itemSpacing.x) + xOff + itemHorizontalOffset +
selectedItemMargins.x,
(i * itemSpacing.y) + yOff + itemVerticallOffset + selectedItemMargins.y,
0.0f});
if (mType == CarouselType::HORIZONTAL_WHEEL) if (mType == CarouselType::HORIZONTAL_WHEEL)
itemTrans = glm::rotate(itemTrans, glm::radians(-90.0f), glm::vec3 {0.0f, 0.0f, 1.0f}); itemTrans = glm::rotate(itemTrans, glm::radians(-90.0f), glm::vec3 {0.0f, 0.0f, 1.0f});
@ -1377,6 +1391,14 @@ void CarouselComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
else else
mSelectedItemMargins = selectedItemMargins * Renderer::getScreenHeight(); mSelectedItemMargins = selectedItemMargins * Renderer::getScreenHeight();
} }
if (elem->has("selectedItemOffset")) {
const glm::vec2 selectedItemOffset {
glm::clamp(elem->get<glm::vec2>("selectedItemOffset"), -1.0f, 1.0f)};
if (mType == CarouselType::HORIZONTAL)
mSelectedItemOffset = selectedItemOffset * Renderer::getScreenWidth();
else
mSelectedItemOffset = selectedItemOffset * Renderer::getScreenHeight();
}
} }
if (elem->has("itemSize")) { if (elem->has("itemSize")) {