ofxVariableLab/src/LayerComposition.h

98 lines
2.7 KiB
C++

#pragma once
#include "Utils.h"
#include "ofMain.h"
#include "ofxMsdfgen.h"
#include "Layer.h"
#include "MsdfLayer.h"
#include "Utils.h"
#include <type_traits>
#include <unordered_map>
namespace ofxVariableLab {
struct ComboIdentifier {
std::string fontPath = "data/celines-fonts/Version-2-var.ttf";
Layer::Type type = Layer::MSDFGEN;
bool operator==(const ComboIdentifier & other) const {
return (this->fontPath == other.fontPath
&& this->type == other.type);
}
};
class AtlasLayerCombo {
public:
virtual void setup(const ComboIdentifier & identifier) = 0;
virtual void update() = 0;
virtual void careForChild(shared_ptr <Layer> layer) = 0;
virtual const ComboIdentifier & getIdentifier() const = 0;
bool operator==(const AtlasLayerCombo & other) const {
return (this->getIdentifier() == other.getIdentifier());
}
};
}
namespace std {
template <>
struct hash <ofxVariableLab::ComboIdentifier> {
std::size_t operator()(const ofxVariableLab::ComboIdentifier & k) const {
using std::size_t;
using std::hash;
using std::string;
size_t seed = 0;
ofxVariableLab::hash_combine(seed, k.fontPath);
ofxVariableLab::hash_combine(seed, k.type);
return seed;
}
};
template <>
struct hash <ofxVariableLab::AtlasLayerCombo> {
std::size_t operator()(const ofxVariableLab::AtlasLayerCombo & k) const {
using std::size_t;
using std::hash;
using std::string;
size_t seed = 0;
ofxVariableLab::hash_combine(seed, k.getIdentifier());
return seed;
}
};
}
namespace ofxVariableLab {
class MsdfAtlasLayerCombo : public AtlasLayerCombo {
public:
void setup(const ComboIdentifier & identifier) override;
void update() override;
void careForChild(shared_ptr <Layer> layer) override;
const ComboIdentifier & getIdentifier() const override;
shared_ptr <ofxMsdfgen::Atlas> atlas;
private:
vector <ofxMsdfgen::GlyphGeometry> glyphGeometries;
shared_ptr <ofShader> msdfShader;
ComboIdentifier identifier;
bool isDirty = false;
};
class LayerComposition {
public:
void setup();
void update();
void draw() const;
void addLayer(const ComboIdentifier & identifier,
const string & text,
const std::vector <FontVariation> & variations);
private:
vector <shared_ptr <ofxVariableLab::Layer> > layers;
unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > atlasLayerCombos;
//unordered_map <LayerIdentifier, shared_ptr <ofxVariableLab::Layer> > layers;
};
}