From 98d9ce42d29f2b5306f953b98a4ddb29ee056322 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 1 Nov 2022 17:08:51 +0100 Subject: [PATCH] Added support for automatic theme aspect ratio detection. --- es-core/src/ThemeData.cpp | 41 +++++++++++++++++++++++++++++++++++++++ es-core/src/ThemeData.h | 1 + 2 files changed, 42 insertions(+) diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index 151fa4441..208a507e5 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -57,6 +57,7 @@ std::vector ThemeData::sLegacyElements { {"maxLogoCount"}}; std::vector> ThemeData::sSupportedAspectRatios { + {"automatic", "automatic"}, {"16:9", "16:9"}, {"16:9_vertical", "16:9 vertical"}, {"16:10", "16:10"}, @@ -72,6 +73,22 @@ std::vector> ThemeData::sSupportedAspectRati {"32:9", "32:0"}, {"32:9_vertical", "32:9 vertical"}}; +std::map ThemeData::sAspectRatioMap { + {"16:9", 1.7777f}, + {"16:9_vertical", 0.5625f}, + {"16:10", 1.6f}, + {"16:10_vertical", 0.625f}, + {"3:2", 1.5f}, + {"3:2_vertical", 0.6667f}, + {"4:3", 1.3333f}, + {"4:3_vertical", 0.75f}, + {"5:4", 1.25f}, + {"5:4_vertical", 0.8f}, + {"21:9", 2.3703f}, + {"21:9_vertical", 0.4219f}, + {"32:9", 3.5555f}, + {"32:9_vertical", 0.2813f}}; + std::map> ThemeData::sPropertyAttributeMap // The data type is defined by the parent property. { @@ -490,6 +507,27 @@ void ThemeData::loadFile(const std::map& sysDataMap, mSelectedAspectRatio = Settings::getInstance()->getString("ThemeAspectRatio"); else mSelectedAspectRatio = mCurrentThemeSet->second.capabilities.aspectRatios.front(); + + if (mSelectedAspectRatio == "automatic") { + // Auto-detect the closest aspect ratio based on what's available in the theme set. + mSelectedAspectRatio = "16:9"; + const float screenAspectRatio {Renderer::getScreenAspectRatio()}; + float diff {std::fabs(sAspectRatioMap["16:9"] - screenAspectRatio)}; + + for (auto& aspectRatio : mCurrentThemeSet->second.capabilities.aspectRatios) { + if (aspectRatio == "automatic") + continue; + + if (sAspectRatioMap.find(aspectRatio) != sAspectRatioMap.end()) { + const float newDiff { + std::fabs(sAspectRatioMap[aspectRatio] - screenAspectRatio)}; + if (newDiff < diff) { + diff = newDiff; + mSelectedAspectRatio = aspectRatio; + } + } + } + } } } @@ -948,6 +986,9 @@ ThemeData::ThemeCapability ThemeData::parseThemeCapabilities(const std::string& // Add the aspect ratios in the order they are defined in sSupportedAspectRatios so they // always show up in the same order in the UI Settings menu. if (!aspectRatiosTemp.empty()) { + // Add the "automatic" aspect ratio if there is more than one entry. + if (aspectRatiosTemp.size() > 1) + capabilities.aspectRatios.emplace_back(sSupportedAspectRatios.front().first); for (auto& aspectRatio : sSupportedAspectRatios) { if (std::find(aspectRatiosTemp.cbegin(), aspectRatiosTemp.cend(), aspectRatio.first) != aspectRatiosTemp.cend()) { diff --git a/es-core/src/ThemeData.h b/es-core/src/ThemeData.h index 3a18b2b8f..910979470 100644 --- a/es-core/src/ThemeData.h +++ b/es-core/src/ThemeData.h @@ -277,6 +277,7 @@ private: static std::vector sLegacySupportedFeatures; static std::vector sLegacyElements; static std::vector> sSupportedAspectRatios; + static std::map sAspectRatioMap; static std::map> sPropertyAttributeMap; static std::map> sElementMap;