summaryrefslogtreecommitdiff
path: root/assetFactory/style.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-02-24 19:30:30 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-02-24 19:30:30 +0000
commit7d0decccaac3aa564b549d91a36279e7aca0814e (patch)
tree749ceff26556ed201a055b8d8a70636b50c562f7 /assetFactory/style.cpp
parentSupport for model colours mixed with textures (diff)
downloadilt-7d0decccaac3aa564b549d91a36279e7aca0814e.tar.bz2
ilt-7d0decccaac3aa564b549d91a36279e7aca0814e.tar.xz
ilt-7d0decccaac3aa564b549d91a36279e7aca0814e.zip
Support for recursive colouring of asset factory faces
Updates colours in sample model.
Diffstat (limited to 'assetFactory/style.cpp')
-rw-r--r--assetFactory/style.cpp59
1 files changed, 59 insertions, 0 deletions
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<float>(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);
+}