ofxVariableLab/src/LayerComposition.cpp

128 lines
4.1 KiB
C++
Raw Normal View History

2023-03-25 18:59:58 +01:00
#include "LayerComposition.h"
#include "Atlas.h"
#include "MsdfLayer.h"
#include "Utils.h"
2023-03-26 19:42:57 +02:00
#include "ofUtils.h"
2023-03-26 21:19:37 +02:00
#include <memory>
2023-03-25 18:59:58 +01:00
namespace ofxVariableLab {
void MsdfAtlasLayerCombo::setup(const ComboIdentifier & layerIdentifier){
this->identifier = layerIdentifier;
ofxMsdfgen::AtlasSettings atlasSettings;
2023-03-26 21:19:37 +02:00
//settings.characters = "ABCDEFGHIJKL";
atlasSettings.scale = 64;
atlasSettings.minimumScale = 64;
atlasSettings.characters = "";
atlasSettings.maxInterpolationStepSize = 50.0;
2023-03-25 18:59:58 +01:00
//string fontName = "RobotoFlex.ttf";
//string fontPath = "data/fonts/" + fontName;
//string fontPath = "data/celines-fonts/testing2VF.ttf";
string fontPath = "data/celines-fonts/Version-2-var.ttf";
//string fontPath = "data/celines-fonts/Version-2-var.ttf";
//string fontPath = "data/celines-fonts/Version-3-var.ttf";
//string fontPath = "data/celines-fonts/Version-4-var.ttf";
2023-03-28 17:48:18 +02:00
//string fontPath = "data/celines-fonts/Alfarn2.otf";
2023-03-25 18:59:58 +01:00
//string fontPath = "data/celines-fonts/Cottagecore.ttf";
2023-03-26 21:19:37 +02:00
atlas = make_shared <ofxMsdfgen::Atlas>();
2023-03-25 18:59:58 +01:00
msdfShader = make_shared <ofShader>();
#ifdef TARGET_EMSCRIPTEN
2023-03-30 16:22:35 +02:00
msdfShader->load("ofxMsdfgen/shaders/mix/ES3/shader");
2023-03-25 18:59:58 +01:00
#else
2023-03-30 16:22:35 +02:00
msdfShader->load("ofxMsdfgen/shaders/mix/GL3/shader");
2023-03-25 18:59:58 +01:00
#endif
cout << "settings scale before " << atlasSettings.scale << endl
<< "minimum scale before " << atlasSettings.minimumScale << endl;
atlas->setup(fontPath, atlasSettings);
}
2023-03-26 21:19:37 +02:00
void MsdfAtlasLayerCombo::update(){
if(isDirty){
atlas->generate();
isDirty = false;
}
}
2023-03-25 18:59:58 +01:00
void MsdfAtlasLayerCombo::careForChild(shared_ptr <Layer> layer){
shared_ptr <MsdfLayer> msdfLayer = dynamic_pointer_cast <MsdfLayer>(layer);
msdfLayer->setAtlas(this->atlas);
msdfLayer->setShader(msdfShader);
auto & as = atlas->settings;
for(const char c : layer->getProps().text){
if(as.characters.find(c) == std::string::npos){
// not found
as.characters += c;
isDirty = true;
}
}
2023-04-07 16:17:07 +02:00
layers.push_back(msdfLayer);
}
2023-03-26 21:19:37 +02:00
const ComboIdentifier & MsdfAtlasLayerCombo::getIdentifier() const {
return identifier;
}
2023-03-29 20:47:48 +02:00
void LayerComposition::setup(){
2023-03-25 18:59:58 +01:00
}
void LayerComposition::update(){
for(const auto & atlasLayerCombo : atlasLayerCombos){
atlasLayerCombo.second->update();
}
2023-03-25 18:59:58 +01:00
}
2023-04-07 16:17:07 +02:00
LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
const string & text,
const std::vector <FontVariation> & variations){
string layerID = "";
if(atlasLayerCombos.count(identifier) <= 0){
switch(identifier.type){
case Layer::GPUFONT: {
ofLogError("LayerComposition::addLayer")
<< "GPUFont not implemented" << endl;
break;
}
default:
case Layer::MSDFGEN: {
// TODO: put most stuff in combo setup
auto combo = make_shared <MsdfAtlasLayerCombo>();
combo->setup(identifier); // TODO: add here text and variations
auto layer = make_shared <MsdfLayer>();
auto props = Layer::Props();
props.text = text;
2023-04-07 16:17:07 +02:00
props.fontPath = identifier.fontPath;
layer->setProps(props);
2023-04-07 16:17:07 +02:00
layer->setup();
std::vector <ofxMsdfgen::FontVariation> msdfVariations;
for(const auto & v : variations){
msdfVariations.push_back({v.name, v.value});
}
// TODO: do not add Variation to atlas, but to combo,
// so we know it's dirty
combo->atlas->addVariations(msdfVariations);
combo->careForChild(layer);
2023-04-07 16:17:07 +02:00
layers[layer->getId()] = layer;
atlasLayerCombos[identifier] = std::move(combo);
2023-04-07 16:17:07 +02:00
layerID = layer->getId();
}
}
2023-03-25 18:59:58 +01:00
}
2023-04-07 16:17:07 +02:00
return layerID;
}
shared_ptr <Layer> LayerComposition::getLayer(const LayerID & layerID){
return layers[layerID];
2023-03-25 18:59:58 +01:00
}
void LayerComposition::draw() const {
for(const auto & layer_it : layers){
2023-04-07 16:17:07 +02:00
layer_it.second->draw(glm::vec3(0, 0, 0));
}
2023-03-25 18:59:58 +01:00
}
}