From a872b8454c0af28ac2bc4a4f7cd1ae8305a99d82 Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Mon, 28 Oct 2024 16:30:58 +0100 Subject: [PATCH] Integrate MuscleColorSourceScaling into OpenSimDecorationOptions (#933) --- .../Graphics/CustomRenderingOptions.cpp | 2 +- .../Graphics/OpenSimDecorationOptions.cpp | 30 +++++++++++--- .../Graphics/OpenSimDecorationOptions.h | 5 +++ tests/TestOpenSimCreator/CMakeLists.txt | 1 + .../Graphics/TestOpenSimDecorationOptions.cpp | 39 +++++++++++++++++++ 5 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 tests/TestOpenSimCreator/Graphics/TestOpenSimDecorationOptions.cpp diff --git a/src/OpenSimCreator/Graphics/CustomRenderingOptions.cpp b/src/OpenSimCreator/Graphics/CustomRenderingOptions.cpp index b643b33b19..606020444e 100644 --- a/src/OpenSimCreator/Graphics/CustomRenderingOptions.cpp +++ b/src/OpenSimCreator/Graphics/CustomRenderingOptions.cpp @@ -88,7 +88,7 @@ void osc::CustomRenderingOptions::tryUpdFromValues(std::string_view keyPrefix, c for (const auto& metadata : GetAllCustomRenderingOptionFlagsMetadata()) { std::string key = std::string{keyPrefix} + metadata.id; - if (const auto* v = lookup_or_nullptr(lut, key); v->type() == VariantType::Bool) { + if (const auto* v = lookup_or_nullptr(lut, key); v and v->type() == VariantType::Bool) { SetOption(m_Flags, metadata.value, to(*v)); } } diff --git a/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.cpp b/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.cpp index 626f293ff4..68b2381f15 100644 --- a/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.cpp +++ b/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.cpp @@ -17,6 +17,7 @@ osc::OpenSimDecorationOptions::OpenSimDecorationOptions() : m_MuscleDecorationStyle{MuscleDecorationStyle::Default}, m_MuscleColorSource{MuscleColorSource::Default}, m_MuscleSizingStyle{MuscleSizingStyle::Default}, + m_MuscleColourSourceScaling{MuscleColorSourceScaling::Default}, m_Flags{OpenSimDecorationOptionFlags::Default} {} @@ -50,6 +51,16 @@ void osc::OpenSimDecorationOptions::setMuscleSizingStyle(MuscleSizingStyle s) m_MuscleSizingStyle = s; } +MuscleColorSourceScaling osc::OpenSimDecorationOptions::getMuscleColorSourceScaling() const +{ + return m_MuscleColourSourceScaling; +} + +void osc::OpenSimDecorationOptions::setMuscleColorSourceScaling(MuscleColorSourceScaling s) +{ + m_MuscleColourSourceScaling = s; +} + size_t osc::OpenSimDecorationOptions::getNumOptions() const { return num_flags(); @@ -190,6 +201,7 @@ void osc::OpenSimDecorationOptions::forEachOptionAsAppSettingValue(const std::fu callback("muscle_decoration_style", GetMuscleDecorationStyleMetadata(m_MuscleDecorationStyle).id); callback("muscle_coloring_style", GetMuscleColoringStyleMetadata(m_MuscleColorSource).id); callback("muscle_sizing_style", GetMuscleSizingStyleMetadata(m_MuscleSizingStyle).id); + callback("muscle_color_scaling", GetMuscleColorSourceScalingMetadata(m_MuscleColourSourceScaling).id); for (size_t i = 0; i < num_flags(); ++i) { const auto& meta = GetIthOptionMetadata(i); callback(meta.id, static_cast(m_Flags & GetIthOption(i))); @@ -212,7 +224,7 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues( return lookup_or_nullptr(lut, buf); }; - if (auto* appVal = lookup("muscle_decoration_style"); appVal->type() == VariantType::String) + if (auto* appVal = lookup("muscle_decoration_style"); appVal and appVal->type() == VariantType::String) { const auto metadata = GetAllMuscleDecorationStyleMetadata(); const auto it = rgs::find(metadata, to(*appVal), [](const auto& m) { return m.id; }); @@ -221,7 +233,7 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues( } } - if (auto* appVal = lookup("muscle_coloring_style"); appVal->type() == VariantType::String) + if (auto* appVal = lookup("muscle_coloring_style"); appVal and appVal->type() == VariantType::String) { const auto metadata = GetAllPossibleMuscleColoringSourcesMetadata(); const auto it = rgs::find(metadata, to(*appVal), [](const auto& m) { return m.id; }); @@ -230,7 +242,7 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues( } } - if (auto* appVal = lookup("muscle_sizing_style"); appVal->type() == VariantType::String) + if (auto* appVal = lookup("muscle_sizing_style"); appVal and appVal->type() == VariantType::String) { const auto metadata = GetAllMuscleSizingStyleMetadata(); const auto it = rgs::find(metadata, to(*appVal), [](const auto& m) { return m.id; }); @@ -239,10 +251,18 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues( } } + if (auto* appVal = lookup("muscle_color_scaling"); appVal and appVal->type() == VariantType::String) + { + const auto metadata = GetAllPossibleMuscleColorSourceScalingMetadata(); + const auto it = rgs::find(metadata, to(*appVal), [](const auto& m) { return m.id; }); + if (it != metadata.end()) { + m_MuscleColourSourceScaling = it->value; + } + } + for (size_t i = 0; i < num_flags(); ++i) { const auto& metadata = GetIthOptionMetadata(i); - if (auto* appVal = lookup(metadata.id); appVal->type() == VariantType::Bool) - { + if (auto* appVal = lookup(metadata.id); appVal and appVal->type() == VariantType::Bool) { SetOption(m_Flags, GetIthOption(i), to(*appVal)); } } diff --git a/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.h b/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.h index 01b3cd6080..017f0681ef 100644 --- a/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.h +++ b/src/OpenSimCreator/Graphics/OpenSimDecorationOptions.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -32,6 +33,9 @@ namespace osc MuscleSizingStyle getMuscleSizingStyle() const; void setMuscleSizingStyle(MuscleSizingStyle); + MuscleColorSourceScaling getMuscleColorSourceScaling() const; + void setMuscleColorSourceScaling(MuscleColorSourceScaling); + // the ones below here are toggle-able options with user-facing strings etc size_t getNumOptions() const; bool getOptionValue(ptrdiff_t) const; @@ -81,6 +85,7 @@ namespace osc MuscleDecorationStyle m_MuscleDecorationStyle; MuscleColorSource m_MuscleColorSource; MuscleSizingStyle m_MuscleSizingStyle; + MuscleColorSourceScaling m_MuscleColourSourceScaling; OpenSimDecorationOptionFlags m_Flags; }; } diff --git a/tests/TestOpenSimCreator/CMakeLists.txt b/tests/TestOpenSimCreator/CMakeLists.txt index 0de46f7ac7..85a05ef6f2 100644 --- a/tests/TestOpenSimCreator/CMakeLists.txt +++ b/tests/TestOpenSimCreator/CMakeLists.txt @@ -21,6 +21,7 @@ add_executable(TestOpenSimCreator Graphics/TestMuscleColorSourceScaling.cpp Graphics/TestOpenSimDecorationGenerator.cpp Graphics/TestSimTKDecorationGenerator.cpp + Graphics/TestOpenSimDecorationOptions.cpp MetaTests/TestOpenSimLibraryAPI.cpp Platform/TestRecentFiles.cpp UI/Shared/TestFunctionCurveViewerPopup.cpp diff --git a/tests/TestOpenSimCreator/Graphics/TestOpenSimDecorationOptions.cpp b/tests/TestOpenSimCreator/Graphics/TestOpenSimDecorationOptions.cpp new file mode 100644 index 0000000000..f7697e0686 --- /dev/null +++ b/tests/TestOpenSimCreator/Graphics/TestOpenSimDecorationOptions.cpp @@ -0,0 +1,39 @@ +#include + +#include +#include +#include +#include + +#include +#include + +using namespace osc; + +TEST(OpenSimDecorationOptions, RemembersColorScaling) +{ + OpenSimDecorationOptions opts; + opts.setMuscleColorSourceScaling(MuscleColorSourceScaling::ModelWide); + bool emitted = false; + opts.forEachOptionAsAppSettingValue([&emitted](std::string_view k, const Variant& v) + { + // Yep, this is hard-coded: it's just here as a sanity check: change/remove + // it if it's causing trouble. + if (k == "muscle_color_scaling" and to(v) == "model_wide") { + emitted = true; + } + }); + ASSERT_TRUE(emitted); +} + +TEST(OpenSimDecorationOptions, ReadsColorScalingFromDict) +{ + const std::unordered_map lookup = { + {"muscle_color_scaling", "model_wide"}, + }; + + OpenSimDecorationOptions opts; + ASSERT_NE(opts.getMuscleColorSourceScaling(), MuscleColorSourceScaling::ModelWide); + opts.tryUpdFromValues("", lookup); + ASSERT_EQ(opts.getMuscleColorSourceScaling(), MuscleColorSourceScaling::ModelWide); +}