From 7d0decccaac3aa564b549d91a36279e7aca0814e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 24 Feb 2023 19:30:30 +0000 Subject: Support for recursive colouring of asset factory faces Updates colours in sample model. --- assetFactory/style.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 assetFactory/style.cpp (limited to 'assetFactory/style.cpp') diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp new file mode 100644 index 0000000..099d81a --- /dev/null +++ b/assetFactory/style.cpp @@ -0,0 +1,59 @@ +#include "style.h" + +ModelFactoryMesh::Color +Style::parseColour(const std::string_view & in) +{ + if (in.empty()) { + return {}; + } + if (in[0] == '#') { + ModelFactoryMesh::Color out {0, 0, 0, 1}; + std::generate_n(out.begin(), (in.length() - 1) / 2, [in = in.data() + 1]() mutable { + uint8_t channel; + std::from_chars(in, in + 2, channel, 16); + in += 2; + return static_cast(channel) / 256.F; + }); + return out; + } + return {}; +} + +void +Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const Shape::CreatedFaces & faces) const +{ + if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { + const auto parsedColour = parseColour(effectiveColour); + for (const auto & face : faces) { + mesh.set_color(face.second, parsedColour); + } + } +} + +void +Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const ModelFactoryMesh::FaceHandle & face) const +{ + if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { + const auto parsedColour = parseColour(effectiveColour); + mesh.set_color(face, parsedColour); + } +} + +std::string_view +Style::getProperty(const StyleStack & parents, std::string Style::*member) +{ + if (const auto itr = std::find_if(parents.rbegin(), parents.rend(), + [&member](auto && s) { + return !(s->*member).empty(); + }); + itr != parents.rend()) { + return (*itr)->*member; + } + return {}; +} + +bool +Style::persist(Persistence::PersistenceStore & store) +{ + return STORE_MEMBER(colour); +} -- cgit v1.2.3 From 5c703a1549b88339ca48ac3c48f67ab7503d223a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 27 Feb 2023 23:56:03 +0000 Subject: Support for named colours in assets Fixes up some error handling in colour parser. --- assetFactory/style.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'assetFactory/style.cpp') diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index 099d81a..d2977a7 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -1,12 +1,16 @@ #include "style.h" +#include "assetFactory.h" ModelFactoryMesh::Color Style::parseColour(const std::string_view & in) { if (in.empty()) { - return {}; + throw std::runtime_error("Empty colour specification"); } if (in[0] == '#') { + if (in.length() > 9 || in.length() % 2 == 0) { + throw std::runtime_error("Invalid hex colour specification"); + } ModelFactoryMesh::Color out {0, 0, 0, 1}; std::generate_n(out.begin(), (in.length() - 1) / 2, [in = in.data() + 1]() mutable { uint8_t channel; @@ -16,7 +20,13 @@ Style::parseColour(const std::string_view & in) }); return out; } - return {}; + if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { + const auto out = glm::vec3 {colour->second} / 256.F; + return {out.r, out.g, out.b, 1.f}; + } + } + throw std::runtime_error("No such asset factory colour"); } void -- cgit v1.2.3 From 98df33e0a52e086b68df2a62ce2f41cc6b67db63 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Mar 2023 18:18:38 +0000 Subject: Parse colour values as they're read --- assetFactory/style.cpp | 72 ++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 47 deletions(-) (limited to 'assetFactory/style.cpp') diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index d2977a7..b2a2cf7 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -1,41 +1,16 @@ #include "style.h" #include "assetFactory.h" -ModelFactoryMesh::Color -Style::parseColour(const std::string_view & in) -{ - if (in.empty()) { - throw std::runtime_error("Empty colour specification"); - } - if (in[0] == '#') { - if (in.length() > 9 || in.length() % 2 == 0) { - throw std::runtime_error("Invalid hex colour specification"); - } - ModelFactoryMesh::Color out {0, 0, 0, 1}; - std::generate_n(out.begin(), (in.length() - 1) / 2, [in = in.data() + 1]() mutable { - uint8_t channel; - std::from_chars(in, in + 2, channel, 16); - in += 2; - return static_cast(channel) / 256.F; - }); - return out; - } - if (auto mf = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { - if (const auto colour = mf->colours.find(in); colour != mf->colours.end()) { - const auto out = glm::vec3 {colour->second} / 256.F; - return {out.r, out.g, out.b, 1.f}; - } - } - throw std::runtime_error("No such asset factory colour"); -} - void Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const Shape::CreatedFaces & faces) const { - if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { - const auto parsedColour = parseColour(effectiveColour); + if (const auto effectiveColour = getProperty(parents, &Style::colour, + [](auto && style) { + return style->colour.a > 0; + }); + effectiveColour.has_value()) { for (const auto & face : faces) { - mesh.set_color(face.second, parsedColour); + mesh.set_color(face.second, effectiveColour->get()); } } } @@ -43,27 +18,30 @@ Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const Sha void Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const ModelFactoryMesh::FaceHandle & face) const { - if (const auto effectiveColour = getProperty(parents, &Style::colour); !effectiveColour.empty()) { - const auto parsedColour = parseColour(effectiveColour); - mesh.set_color(face, parsedColour); - } -} - -std::string_view -Style::getProperty(const StyleStack & parents, std::string Style::*member) -{ - if (const auto itr = std::find_if(parents.rbegin(), parents.rend(), - [&member](auto && s) { - return !(s->*member).empty(); + if (const auto effectiveColour = getProperty(parents, &Style::colour, + [](auto && style) { + return style->colour.a > 0; }); - itr != parents.rend()) { - return (*itr)->*member; + effectiveColour.has_value()) { + mesh.set_color(face, effectiveColour->get()); } - return {}; } bool Style::persist(Persistence::PersistenceStore & store) { - return STORE_MEMBER(colour); + struct ColourParser : public Persistence::SelectionV { + using Persistence::SelectionV::SelectionV; + using Persistence::SelectionV::setValue; + void + setValue(std::string && str) override + { + if (auto mf + = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + v = mf->parseColour(str); + } + } + }; + + return STORE_HELPER(colour, ColourParser); } -- cgit v1.2.3 From 17ac090dd1dd245cf1e24b62b7333ba9be571bde Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 2 Mar 2023 18:36:34 +0000 Subject: Add ParseBase Acts as a base class for persistence parser, encompasses the parse stack and manages shared objects --- assetFactory/style.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'assetFactory/style.cpp') diff --git a/assetFactory/style.cpp b/assetFactory/style.cpp index b2a2cf7..fc5c34e 100644 --- a/assetFactory/style.cpp +++ b/assetFactory/style.cpp @@ -36,8 +36,7 @@ Style::persist(Persistence::PersistenceStore & store) void setValue(std::string && str) override { - if (auto mf - = std::dynamic_pointer_cast(Persistence::sharedObjects.at("assetFactory"))) { + if (auto mf = Persistence::ParseBase::getShared("assetFactory")) { v = mf->parseColour(str); } } -- cgit v1.2.3