summaryrefslogtreecommitdiff
path: root/assetFactory/style.cpp
blob: d1d4adcda0e886100e14b38f8bf8bbd74279dc5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "style.h"
#include "assetFactory.h"

void
Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const Shape::CreatedFaces & faces) const
{
	for (const auto & face : faces) {
		applyStyle(mesh, face.second, getColour(parents));
	}
}

void
Style::applyStyle(ModelFactoryMesh & mesh, const StyleStack & parents, const ModelFactoryMesh::FaceHandle & face) const
{
	applyStyle(mesh, face, getColour(parents));
}

void
Style::applyStyle(
		ModelFactoryMesh & mesh, const ModelFactoryMesh::FaceHandle & face, EffectiveColour effectiveColour) const
{
	if (texture.empty()) {
		if (effectiveColour.has_value()) {
			mesh.set_color(face, effectiveColour->get());
		}
	}
	else {
		mesh.set_color(face, {});
		if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) {
			auto coords = mf->getTextureCoords(texture);
			auto coord = coords.begin();
			// Wild assumption that face is a quad and the texture should apply linearly
			for (const auto & heh : mesh.fh_range(face)) {
				mesh.set_texcoord2D(heh, *coord++);
			}
		}
	}
}

Style::EffectiveColour
Style::getColour(const StyleStack & parents)
{
	return getProperty(parents, &Style::colour, [](auto && style) {
		return style->colour.a > 0;
	});
}

bool
Style::persist(Persistence::PersistenceStore & store)
{
	struct ColourParser : public Persistence::SelectionV<ColourAlpha> {
		using Persistence::SelectionV<ColourAlpha>::SelectionV;
		using Persistence::SelectionV<ColourAlpha>::setValue;
		void
		setValue(std::string && str) override
		{
			if (auto mf = Persistence::ParseBase::getShared<const AssetFactory>("assetFactory")) {
				v = mf->parseColour(str);
			}
		}
	};

	return STORE_HELPER(colour, ColourParser) && STORE_MEMBER(texture) && STORE_MEMBER(textureRotation);
}