Added a selectedItemMargins property to CarouselComponent.

This commit is contained in:
Leon Styhre 2023-01-05 22:10:45 +01:00
parent 52ebc9098b
commit bcc74db584
2 changed files with 38 additions and 3 deletions

View file

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

View file

@ -139,6 +139,7 @@ private:
int mItemsBeforeCenter; int mItemsBeforeCenter;
int mItemsAfterCenter; int mItemsAfterCenter;
ItemStacking mItemStacking; ItemStacking mItemStacking;
glm::vec2 mSelectedItemMargins;
glm::vec2 mItemSize; glm::vec2 mItemSize;
float mItemScale; float mItemScale;
float mItemRotation; float mItemRotation;
@ -192,6 +193,7 @@ CarouselComponent<T>::CarouselComponent()
, mItemsBeforeCenter {8} , mItemsBeforeCenter {8}
, mItemsAfterCenter {8} , mItemsAfterCenter {8}
, mItemStacking {ItemStacking::CENTERED} , mItemStacking {ItemStacking::CENTERED}
, mSelectedItemMargins {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}
@ -812,7 +814,7 @@ template <typename T> void CarouselComponent<T>::render(const glm::mat4& parentT
std::vector<renderStruct> renderItems; std::vector<renderStruct> renderItems;
std::vector<renderStruct> renderItemsSorted; std::vector<renderStruct> renderItemsSorted;
for (int i = center - itemInclusion - itemInclusionBefore; for (int i {center - itemInclusion - itemInclusionBefore};
i < center + itemInclusion + itemInclusionAfter; ++i) { i < center + itemInclusion + itemInclusionAfter; ++i) {
int index {i}; int index {i};
@ -839,12 +841,33 @@ template <typename T> void CarouselComponent<T>::render(const glm::mat4& parentT
scale = std::max(mItemScale, std::min(1.0f, scale)); scale = std::max(mItemScale, std::min(1.0f, scale));
} }
glm::vec2 selectedItemMargins {0.0f, 0.0f};
if (mSelectedItemMargins != glm::vec2 {0.0f, 0.0f}) {
if (i < camOffset) {
if (mType == CarouselType::HORIZONTAL)
selectedItemMargins.x = -mSelectedItemMargins.x;
else
selectedItemMargins.y = -mSelectedItemMargins.x;
}
else if (i > camOffset) {
if (mType == CarouselType::HORIZONTAL)
selectedItemMargins.x = mSelectedItemMargins.y;
else
selectedItemMargins.y = mSelectedItemMargins.y;
}
if (std::fabs(distance) < 1.0f)
selectedItemMargins *= std::fabs(distance);
}
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::vec3 {(i * itemSpacing.x) + xOff, itemTrans = glm::translate(
(i * itemSpacing.y) + yOff, 0.0f}); itemTrans, glm::vec3 {(i * itemSpacing.x) + xOff + selectedItemMargins.x,
(i * itemSpacing.y) + yOff + selectedItemMargins.y, 0.0f});
float opacity {0.0f}; float opacity {0.0f};
@ -1140,6 +1163,17 @@ void CarouselComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme,
mItemsAfterCenter = mItemsAfterCenter =
glm::clamp(static_cast<int>(elem->get<unsigned int>("itemsAfterCenter")), 0, 20); glm::clamp(static_cast<int>(elem->get<unsigned int>("itemsAfterCenter")), 0, 20);
if (mType == CarouselType::HORIZONTAL || mType == CarouselType::VERTICAL) {
if (elem->has("selectedItemMargins")) {
const glm::vec2 selectedItemMargins {
glm::clamp(elem->get<glm::vec2>("selectedItemMargins"), 0.0f, 1.0f)};
if (mType == CarouselType::HORIZONTAL)
mSelectedItemMargins = selectedItemMargins * Renderer::getScreenWidth();
else
mSelectedItemMargins = selectedItemMargins * Renderer::getScreenHeight();
}
}
if (elem->has("itemSize")) { if (elem->has("itemSize")) {
const glm::vec2 itemSize {glm::clamp(elem->get<glm::vec2>("itemSize"), 0.05f, 1.0f)}; const glm::vec2 itemSize {glm::clamp(elem->get<glm::vec2>("itemSize"), 0.05f, 1.0f)};
mItemSize = mItemSize =