From 86e5b94ef61d7d87b3779cb3b73049985f78b30b Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 30 Jun 2023 23:18:29 +0200 Subject: [PATCH] Convert setting value int to CollisionShapeType on initialization --- apps/launcher/settingspage.cpp | 10 +++++++++ components/settings/categories/game.hpp | 5 +++-- components/settings/settings.cpp | 5 +++++ components/settings/settings.hpp | 10 +++++++++ components/settings/settingvalue.hpp | 29 ++++++++++++++++++++++--- 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/apps/launcher/settingspage.cpp b/apps/launcher/settingspage.cpp index f8711e6d3d..cf7b208d53 100644 --- a/apps/launcher/settingspage.cpp +++ b/apps/launcher/settingspage.cpp @@ -31,11 +31,21 @@ namespace comboBox.setCurrentIndex(value); } + void loadSettingInt(const Settings::SettingValue& value, QComboBox& comboBox) + { + comboBox.setCurrentIndex(static_cast(value.get())); + } + void saveSettingInt(const QComboBox& comboBox, Settings::SettingValue& value) { value.set(comboBox.currentIndex()); } + void saveSettingInt(const QComboBox& comboBox, Settings::SettingValue& value) + { + value.set(static_cast(comboBox.currentIndex())); + } + void loadSettingInt(const Settings::SettingValue& value, QSpinBox& spinBox) { spinBox.setValue(value); diff --git a/components/settings/categories/game.hpp b/components/settings/categories/game.hpp index 3eec91dd22..c96ff1e63c 100644 --- a/components/settings/categories/game.hpp +++ b/components/settings/categories/game.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_COMPONENTS_SETTINGS_CATEGORIES_GAME_H #define OPENMW_COMPONENTS_SETTINGS_CATEGORIES_GAME_H +#include "components/detournavigator/collisionshapetype.hpp" #include "components/settings/sanitizerimpl.hpp" #include "components/settings/settingvalue.hpp" @@ -69,8 +70,8 @@ namespace Settings SettingValue mDayNightSwitches{ mIndex, "Game", "day night switches" }; SettingValue mUnarmedCreatureAttacksDamageArmor{ mIndex, "Game", "unarmed creature attacks damage armor" }; - SettingValue mActorCollisionShapeType{ mIndex, "Game", "actor collision shape type", - makeEnumSanitizerInt({ 0, 1, 2 }) }; + SettingValue mActorCollisionShapeType{ mIndex, "Game", + "actor collision shape type" }; }; } diff --git a/components/settings/settings.cpp b/components/settings/settings.cpp index c9e0daccad..6d35bab046 100644 --- a/components/settings/settings.cpp +++ b/components/settings/settings.cpp @@ -444,6 +444,11 @@ namespace Settings setVector3(setting, category, value); } + void Manager::set(std::string_view setting, std::string_view category, DetourNavigator::CollisionShapeType value) + { + setInt(setting, category, static_cast(value)); + } + void Manager::recordInit(std::string_view setting, std::string_view category) { sInitialized.emplace(category, setting); diff --git a/components/settings/settings.hpp b/components/settings/settings.hpp index 5f3fb65aba..536977399f 100644 --- a/components/settings/settings.hpp +++ b/components/settings/settings.hpp @@ -3,6 +3,8 @@ #include "categories.hpp" +#include "components/detournavigator/collisionshapetype.hpp" + #include #include #include @@ -101,6 +103,7 @@ namespace Settings static void set(std::string_view setting, std::string_view category, bool value); static void set(std::string_view setting, std::string_view category, const osg::Vec2f& value); static void set(std::string_view setting, std::string_view category, const osg::Vec3f& value); + static void set(std::string_view setting, std::string_view category, DetourNavigator::CollisionShapeType value); private: static std::set> sInitialized; @@ -170,6 +173,13 @@ namespace Settings { return getVector3(setting, category); } + + template <> + inline DetourNavigator::CollisionShapeType Manager::getImpl( + std::string_view setting, std::string_view category) + { + return DetourNavigator::toCollisionShapeType(getInt(setting, category)); + } } #endif // COMPONENTS_SETTINGS_H diff --git a/components/settings/settingvalue.hpp b/components/settings/settingvalue.hpp index 1ad92fe6a7..435f82ab1b 100644 --- a/components/settings/settingvalue.hpp +++ b/components/settings/settingvalue.hpp @@ -5,6 +5,7 @@ #include "settings.hpp" #include "components/debug/debuglog.hpp" +#include "components/detournavigator/collisionshapetype.hpp" #include @@ -31,10 +32,11 @@ namespace Settings String, Vec2f, Vec3f, + CollisionShapeType, }; template - constexpr SettingValueType getSettingValueType(); + constexpr SettingValueType getSettingValueType() = delete; template <> inline constexpr SettingValueType getSettingValueType() @@ -108,6 +110,12 @@ namespace Settings return SettingValueType::Vec3f; } + template <> + inline constexpr SettingValueType getSettingValueType() + { + return SettingValueType::CollisionShapeType; + } + inline constexpr std::string_view getSettingValueTypeName(SettingValueType type) { switch (type) @@ -136,6 +144,8 @@ namespace Settings return "vec2f"; case SettingValueType::Vec3f: return "vec3f"; + case SettingValueType::CollisionShapeType: + return "collision shape type"; } return "unsupported"; } @@ -274,6 +284,19 @@ namespace Settings T mDefaultValue{}; T mValue{}; + struct WriteValue + { + const T& mValue; + + friend std::ostream& operator<<(std::ostream& stream, const WriteValue& value) + { + if constexpr (std::is_enum_v) + return stream << static_cast>(value.mValue); + else + return stream << value.mValue; + } + }; + T sanitize(const T& value) const { if (mSanitizer == nullptr) @@ -283,8 +306,8 @@ namespace Settings T sanitizedValue = mSanitizer->apply(value); if (sanitizedValue != value) Log(Debug::Warning) << getSettingDescription(mCategory, mName) - << " value is out of allowed values set: " << value << ", sanitized to " - << sanitizedValue; + << " value is out of allowed values set: " << WriteValue{ value } + << ", sanitized to " << WriteValue{ sanitizedValue }; return sanitizedValue; } catch (const std::exception& e)