From 3008011acd35257e0c398bd7e63ba763a08db8bc Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Wed, 5 Jun 2024 18:46:27 +0200 Subject: [PATCH] Added a 'cropPos' property to the video element --- es-core/src/ThemeData.cpp | 3 ++- es-core/src/components/VideoComponent.cpp | 6 ++++- es-core/src/components/VideoComponent.h | 4 +++- .../src/components/VideoFFmpegComponent.cpp | 22 ++++++++++++++----- es-core/src/components/VideoFFmpegComponent.h | 2 +- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index c4568572c..40ad04961 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -323,8 +323,9 @@ std::map> {"video", {{"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/VideoComponent.cpp b/es-core/src/components/VideoComponent.cpp index af20c72e9..28de18508 100644 --- a/es-core/src/components/VideoComponent.cpp +++ b/es-core/src/components/VideoComponent.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // VideoComponent.cpp // // Base class for playing videos. @@ -26,6 +26,8 @@ VideoComponent::VideoComponent() , mVideoCornerRadius {0.0f} , mColorGradientHorizontal {true} , mTargetSize {0.0f, 0.0f} + , mCropPos {0.5f, 0.5f} + , mCropOffset {0.0f, 0.0f} , mVideoAreaPos {0.0f, 0.0f} , mVideoAreaSize {0.0f, 0.0f} , mTopLeftCrop {0.0f, 0.0f} @@ -161,6 +163,8 @@ void VideoComponent::applyTheme(const std::shared_ptr& theme, glm::vec2 videoCropSize {elem->get("cropSize")}; videoCropSize.x = glm::clamp(videoCropSize.x, 0.01f, 2.0f); videoCropSize.y = glm::clamp(videoCropSize.y, 0.01f, 2.0f); + if (elem->has("cropPos")) + mCropPos = glm::clamp(elem->get("cropPos"), 0.0f, 1.0f); setCroppedSize(videoCropSize * scale); mVideoAreaSize = videoCropSize * scale; } diff --git a/es-core/src/components/VideoComponent.h b/es-core/src/components/VideoComponent.h index a9347c567..f23879861 100644 --- a/es-core/src/components/VideoComponent.h +++ b/es-core/src/components/VideoComponent.h @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // VideoComponent.h // // Base class for playing videos. @@ -115,6 +115,8 @@ protected: float mVideoCornerRadius; bool mColorGradientHorizontal; glm::vec2 mTargetSize; + glm::vec2 mCropPos; + glm::vec2 mCropOffset; glm::vec2 mVideoAreaPos; glm::vec2 mVideoAreaSize; glm::vec2 mTopLeftCrop; diff --git a/es-core/src/components/VideoFFmpegComponent.cpp b/es-core/src/components/VideoFFmpegComponent.cpp index a6838231e..566d1b48a 100644 --- a/es-core/src/components/VideoFFmpegComponent.cpp +++ b/es-core/src/components/VideoFFmpegComponent.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // VideoFFmpegComponent.cpp // // Video player based on FFmpeg. @@ -88,6 +88,7 @@ void VideoFFmpegComponent::setCroppedSize(const glm::vec2& size) mTargetSize = size; mTargetIsMax = false; mTargetIsCrop = true; + mStaticImage.setCropPos(mCropPos); mStaticImage.setCroppedSize(size); resize(); } @@ -122,6 +123,9 @@ void VideoFFmpegComponent::resize() // Size texture to allow for cropped video to fill the entire area. const float cropFactor { std::max(mTargetSize.x / textureSize.x, mTargetSize.y / textureSize.y)}; + mTopLeftCrop = {0.0f, 0.0f}; + mBottomRightCrop = {1.0f, 1.0f}; + mCropOffset = {0.0f, 0.0f}; mSize = textureSize * cropFactor; if (std::round(mSize.y) > std::round(mTargetSize.y)) { @@ -129,12 +133,20 @@ void VideoFFmpegComponent::resize() mTopLeftCrop.y = cropSize / 2.0f; mBottomRightCrop.y = 1.0f - (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 - (mTargetSize.x / std::round(mSize.x))}; mTopLeftCrop.x = cropSize / 2.0f; mBottomRightCrop.x = 1.0f - (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 - (cropSize * cropPosX); + } } } else { @@ -201,10 +213,10 @@ void VideoFFmpegComponent::render(const glm::mat4& parentTrans) return; // clang-format off - vertices[0] = {{0.0f, 0.0f }, {mTopLeftCrop.x, 1.0f - mBottomRightCrop.y}, 0xFFFFFFFF}; - vertices[1] = {{0.0f, mSize.y}, {mTopLeftCrop.x, 1.0f - mTopLeftCrop.y }, 0xFFFFFFFF}; - vertices[2] = {{mSize.x, 0.0f }, {mBottomRightCrop.x * 1.0f, 1.0f - mBottomRightCrop.y}, 0xFFFFFFFF}; - vertices[3] = {{mSize.x, mSize.y}, {mBottomRightCrop.x * 1.0f, 1.0f - mTopLeftCrop.y }, 0xFFFFFFFF}; + vertices[0] = {{0.0f, 0.0f }, {mTopLeftCrop.x - mCropOffset.x, 1.0f - mBottomRightCrop.y + mCropOffset.y}, 0xFFFFFFFF}; + vertices[1] = {{0.0f, mSize.y}, {mTopLeftCrop.x - mCropOffset.x, 1.0f - mTopLeftCrop.y + mCropOffset.y }, 0xFFFFFFFF}; + vertices[2] = {{mSize.x, 0.0f }, {(mBottomRightCrop.x * 1.0f) - mCropOffset.x, 1.0f - mBottomRightCrop.y + mCropOffset.y}, 0xFFFFFFFF}; + vertices[3] = {{mSize.x, mSize.y}, {(mBottomRightCrop.x * 1.0f) - mCropOffset.x, 1.0f - mTopLeftCrop.y + mCropOffset.y }, 0xFFFFFFFF}; // clang-format on vertices[0].color = mColorShift; diff --git a/es-core/src/components/VideoFFmpegComponent.h b/es-core/src/components/VideoFFmpegComponent.h index 85ccdcf39..24691dc17 100644 --- a/es-core/src/components/VideoFFmpegComponent.h +++ b/es-core/src/components/VideoFFmpegComponent.h @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT // -// ES-DE +// ES-DE Frontend // VideoFFmpegComponent.h // // Video player based on FFmpeg.