summaryrefslogtreecommitdiff
path: root/assetFactory/style.cpp
blob: 81a5441b61bae303068557e479015620d6258edb (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
65
66
67
68
69
70
71
72
73
74
75
76
#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 (smooth.has_value()) {
		mesh.property(mesh.smoothFaceProperty, face) = smooth.value();
	}
	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")) {
			const auto material = mf->getMaterialIndex(texture);
			mesh.property(mesh.materialFaceProperty, face) = material;
			static constexpr std::array<ModelFactoryTraits::TexCoord2D, 4> coords {{
					{0.f, 0.f},
					{1.f, 0.f},
					{1.f, 1.f},
					{0.f, 1.f},
			}};
			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(smooth) && STORE_MEMBER(texture)
			&& STORE_MEMBER(textureRotation);
}