2023-03-25 18:59:58 +01:00
|
|
|
#include "LayerComposition.h"
|
2023-04-06 11:17:22 +02:00
|
|
|
#include "Atlas.h"
|
|
|
|
#include "MsdfLayer.h"
|
2023-04-07 09:48:37 +02:00
|
|
|
#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 {
|
|
|
|
|
2023-04-06 11:17:22 +02:00
|
|
|
void MsdfAtlasLayerCombo::setup(const ComboIdentifier & layerIdentifier){
|
|
|
|
this->identifier = layerIdentifier;
|
|
|
|
ofxMsdfgen::AtlasSettings atlasSettings;
|
2023-03-26 21:19:37 +02:00
|
|
|
//settings.characters = "ABCDEFGHIJKL";
|
2023-04-06 11:17:22 +02:00
|
|
|
atlasSettings.scale = 64;
|
|
|
|
atlasSettings.minimumScale = 64;
|
|
|
|
atlasSettings.characters = "";
|
2023-04-07 09:48:37 +02:00
|
|
|
atlasSettings.maxInterpolationStepSize = 50.0;
|
2023-04-06 11:17:22 +02:00
|
|
|
|
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
|
|
|
|
|
2023-04-08 17:16:06 +02:00
|
|
|
atlas->setup(this->identifier.fontPath, atlasSettings);
|
2023-04-06 11:17:22 +02:00
|
|
|
}
|
2023-03-26 21:19:37 +02:00
|
|
|
|
2023-04-06 11:17:22 +02:00
|
|
|
void MsdfAtlasLayerCombo::update(){
|
|
|
|
if(isDirty){
|
|
|
|
atlas->generate();
|
|
|
|
isDirty = false;
|
|
|
|
}
|
|
|
|
}
|
2023-03-25 18:59:58 +01:00
|
|
|
|
2023-04-06 11:17:22 +02: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-04-06 11:17:22 +02:00
|
|
|
}
|
2023-03-26 21:19:37 +02:00
|
|
|
|
2023-04-06 11:17:22 +02:00
|
|
|
const ComboIdentifier & MsdfAtlasLayerCombo::getIdentifier() const {
|
|
|
|
return identifier;
|
|
|
|
}
|
2023-03-29 20:47:48 +02:00
|
|
|
|
2023-04-11 12:23:14 +02:00
|
|
|
const ofImage & MsdfAtlasLayerCombo::getAtlasImage(){
|
|
|
|
return atlas->getAtlasImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
string MsdfAtlasLayerCombo::getAtlasImagePath(){
|
|
|
|
return atlas->getAtlasPath();
|
|
|
|
}
|
|
|
|
|
2023-04-06 11:17:22 +02:00
|
|
|
void LayerComposition::setup(){
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerComposition::update(){
|
2023-04-06 11:17:22 +02:00
|
|
|
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){
|
2023-04-11 12:23:14 +02:00
|
|
|
auto props = Layer::Props();
|
|
|
|
props.text = text;
|
|
|
|
props.fontPath = identifier.fontPath;
|
|
|
|
return addLayer(identifier,
|
|
|
|
props,
|
|
|
|
variations);
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
|
|
|
|
const Layer::Props & props,
|
|
|
|
const std::vector <FontVariation> & variations){
|
2023-04-07 16:17:07 +02:00
|
|
|
string layerID = "";
|
2023-04-06 11:17:22 +02:00
|
|
|
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>();
|
|
|
|
layer->setProps(props);
|
2023-04-07 16:17:07 +02:00
|
|
|
layer->setup();
|
2023-04-06 11:17:22 +02:00
|
|
|
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;
|
2023-04-06 11:17:22 +02:00
|
|
|
atlasLayerCombos[identifier] = std::move(combo);
|
2023-04-07 16:17:07 +02:00
|
|
|
layerID = layer->getId();
|
2023-04-06 11:17:22 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-04-11 12:23:14 +02:00
|
|
|
const ofImage & LayerComposition::getAtlasImage(const ComboIdentifier & identifier){
|
|
|
|
return atlasLayerCombos[identifier]->getAtlasImage();
|
|
|
|
}
|
|
|
|
|
2023-04-06 11:17:22 +02: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-04-06 11:17:22 +02:00
|
|
|
}
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:23:14 +02:00
|
|
|
const std::vector <ComboIdentifier> LayerComposition::getAvailableComboIdentifiers() const {
|
|
|
|
std::vector <ComboIdentifier> combos;
|
|
|
|
for(const auto & combo : atlasLayerCombos){
|
|
|
|
combos.push_back(combo.first);
|
|
|
|
}
|
|
|
|
return combos;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > & LayerComposition::getAtlasLayerCombos() const {
|
|
|
|
return atlasLayerCombos;
|
|
|
|
}
|
|
|
|
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|