From 96452849e72cc05966361f77a61105eb76a10838 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Wed, 5 Jun 2024 18:43:36 +0200 Subject: [PATCH] Added a 'cropPos' property to the image element --- es-core/src/ThemeData.cpp | 3 ++- es-core/src/components/ImageComponent.cpp | 23 ++++++++++++++++++----- es-core/src/components/ImageComponent.h | 7 ++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index 3eb548d72..c4568572c 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -288,8 +288,9 @@ std::map> {"image", {{"pos", NORMALIZED_PAIR}, {"size", NORMALIZED_PAIR}, - {"cropSize", NORMALIZED_PAIR}, {"maxSize", NORMALIZED_PAIR}, + {"cropSize", NORMALIZED_PAIR}, + {"cropPos", NORMALIZED_PAIR}, {"origin", NORMALIZED_PAIR}, {"rotation", FLOAT}, {"rotationOrigin", NORMALIZED_PAIR}, diff --git a/es-core/src/components/ImageComponent.cpp b/es-core/src/components/ImageComponent.cpp index 37f59a7e0..7d9370117 100644 --- a/es-core/src/components/ImageComponent.cpp +++ b/es-core/src/components/ImageComponent.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // ImageComponent.cpp // // Handles images: loading, resizing, cropping, color shifting etc. @@ -23,6 +23,8 @@ ImageComponent::ImageComponent(bool forceLoad, bool dynamic) , mFlipY {false} , mTargetIsMax {false} , mTargetIsCrop {false} + , mCropPos {0.5f, 0.5f} + , mCropOffset {0.0f, 0.0f} , mTileWidth {0.0f} , mTileHeight {0.0f} , mColorShift {0xFFFFFFFF} @@ -231,18 +233,27 @@ void ImageComponent::coverFitCrop() mTopLeftCrop = {0.0f, 0.0f}; mBottomRightCrop = {1.0f, 1.0f}; + mCropOffset = {0.0f, 0.0f}; if (std::round(mSize.y) > std::round(mTargetSize.y)) { const float cropSize {1.0f - (std::round(mTargetSize.y) / std::round(mSize.y))}; cropTop(cropSize / 2.0f); cropBottom(cropSize / 2.0f); mSize.y = mSize.y - (mSize.y * cropSize); + if (mCropPos.y != 0.5f) { + const float cropPosY {mCropPos.y + 0.5f}; + mCropOffset.y = (cropSize * cropPosY) - cropSize; + } } else { const float cropSize {1.0f - (std::round(mTargetSize.x) / std::round(mSize.x))}; cropLeft(cropSize / 2.0f); cropRight(cropSize / 2.0f); mSize.x = mSize.x - (mSize.x * cropSize); + if (mCropPos.x != 0.5f) { + const float cropPosX {mCropPos.x + 0.5f}; + mCropOffset.x = (cropSize * cropPosX) - cropSize; + } } } @@ -537,6 +548,8 @@ void ImageComponent::applyTheme(const std::shared_ptr& theme, glm::vec2 imageCropSize {elem->get("cropSize")}; imageCropSize.x = glm::clamp(imageCropSize.x, 0.001f, 3.0f); imageCropSize.y = glm::clamp(imageCropSize.y, 0.001f, 3.0f); + if (elem->has("cropPos")) + mCropPos = glm::clamp(elem->get("cropPos"), 0.0f, 1.0f); setCroppedSize(imageCropSize * scale); } } @@ -840,10 +853,10 @@ void ImageComponent::updateVertices() if (mTileHeight == 0.0f) { // clang-format off - mVertices[0] = {{topLeft.x, topLeft.y }, {mTopLeftCrop.x, py - mTopLeftCrop.y }, 0}; - mVertices[1] = {{topLeft.x, bottomRight.y}, {mTopLeftCrop.x, 1.0f - mBottomRightCrop.y}, 0}; - mVertices[2] = {{bottomRight.x, topLeft.y }, {mBottomRightCrop.x * px, py - mTopLeftCrop.y }, 0}; - mVertices[3] = {{bottomRight.x, bottomRight.y}, {mBottomRightCrop.x * px, 1.0f - mBottomRightCrop.y}, 0}; + mVertices[0] = {{topLeft.x, topLeft.y }, {mTopLeftCrop.x + mCropOffset.x, py - mTopLeftCrop.y - mCropOffset.y }, 0}; + mVertices[1] = {{topLeft.x, bottomRight.y}, {mTopLeftCrop.x + mCropOffset.x, 1.0f - mBottomRightCrop.y - mCropOffset.y}, 0}; + mVertices[2] = {{bottomRight.x, topLeft.y }, {(mBottomRightCrop.x * px) + mCropOffset.x, py - mTopLeftCrop.y - mCropOffset.y }, 0}; + mVertices[3] = {{bottomRight.x, bottomRight.y}, {(mBottomRightCrop.x * px) + mCropOffset.x, 1.0f - mBottomRightCrop.y - mCropOffset.y}, 0}; // clang-format on } else { diff --git a/es-core/src/components/ImageComponent.h b/es-core/src/components/ImageComponent.h index 90c7a385b..6de0ca70e 100644 --- a/es-core/src/components/ImageComponent.h +++ b/es-core/src/components/ImageComponent.h @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // ImageComponent.h // // Handles images: loading, resizing, cropping, color shifting etc. @@ -74,6 +74,9 @@ public: // cover image type (as the name may seem to imply). void coverFitCrop(); + // Texture position when using cover fit (cropping). + void setCropPos(const glm::vec2 cropPos) { mCropPos = cropPos; } + // This crops any entirely transparent areas around the actual image. // The arguments restrict how much the end result is allowed to be scaled. void cropTransparentPadding(const float maxSizeX, const float maxSizeY); @@ -129,6 +132,8 @@ private: bool mTargetIsMax; bool mTargetIsCrop; + glm::vec2 mCropPos; + glm::vec2 mCropOffset; float mTileWidth; float mTileHeight;