2023-03-25 18:59:58 +01:00
|
|
|
#include "LayerComposition.h"
|
2023-04-06 11:17:22 +02:00
|
|
|
#include "Atlas.h"
|
2023-04-13 17:15:35 +02:00
|
|
|
#include "GPUFontAtlasLayerCombo.h"
|
2023-05-14 08:33:23 +02:00
|
|
|
#include "GPUFontLayer.h"
|
2023-04-06 11:17:22 +02:00
|
|
|
#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 LayerComposition::setup(){
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerComposition::update(){
|
2023-04-16 17:20:54 +02:00
|
|
|
for(auto & l: layers){
|
|
|
|
if(l.second->wantsNewMom()){
|
|
|
|
const auto momIdentifier = l.second->getMomsComboIdentifier();
|
|
|
|
atlasLayerCombos[momIdentifier]->abandonChild(l.second);
|
2023-04-18 13:19:28 +02:00
|
|
|
if(!atlasLayerCombos[momIdentifier]->hasChildren()){
|
|
|
|
atlasLayerCombos.erase(momIdentifier);
|
|
|
|
}
|
2023-04-16 17:20:54 +02:00
|
|
|
ComboIdentifier idealMom{
|
|
|
|
l.second->getProps().fontPath,
|
|
|
|
momIdentifier.type
|
|
|
|
};
|
2023-05-14 08:33:23 +02:00
|
|
|
//cout << "ideal mom looks like this: " << l.second->getProps().fontPath
|
|
|
|
//<< (momIdentifier.type == LayerType::GPUFONT ? " gpufont" : " msdfgen")
|
|
|
|
//<< endl;
|
2023-04-16 17:20:54 +02:00
|
|
|
findOrCreateNewMomForLayer(l.second, idealMom);
|
|
|
|
}
|
|
|
|
}
|
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-16 17:20:54 +02:00
|
|
|
void LayerComposition::findOrCreateNewMomForLayer(shared_ptr <Layer> layer,
|
|
|
|
ComboIdentifier idealMom){
|
|
|
|
auto comboIterator = atlasLayerCombos.find(idealMom);
|
|
|
|
if(comboIterator != atlasLayerCombos.end()){
|
|
|
|
comboIterator->second->careForChild(layer);
|
|
|
|
}else{
|
|
|
|
switch(idealMom.type){
|
|
|
|
case LayerType::GPUFONT: {
|
|
|
|
auto combo = make_shared <GPUFontAtlasLayerCombo>();
|
|
|
|
GPUFontAtlasLayerComboSettings settings;
|
|
|
|
combo->setup(idealMom, settings);
|
|
|
|
combo->careForChild(layer);
|
|
|
|
atlasLayerCombos[idealMom] = std::move(combo);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
case LayerType::MSDFGEN: {
|
|
|
|
auto combo = make_shared <MsdfAtlasLayerCombo>();
|
|
|
|
combo->setup(idealMom);
|
|
|
|
std::vector <ofxMsdfgen::FontVariation> msdfVariations;
|
|
|
|
for(const auto & v : layer->getProps().fontVariations){
|
|
|
|
msdfVariations.push_back({v.name, v.value});
|
|
|
|
}
|
|
|
|
combo->atlas->addVariations(msdfVariations);
|
|
|
|
combo->careForChild(layer);
|
|
|
|
atlasLayerCombos[idealMom] = std::move(combo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 16:55:15 +02:00
|
|
|
LayerID LayerComposition::addLayer(const Layer::Props & props,
|
|
|
|
LayerID layerID){
|
|
|
|
ComboIdentifier identifier;
|
|
|
|
identifier.fontPath = props.fontPath;
|
2023-04-16 17:20:54 +02:00
|
|
|
identifier.type = LayerType::GPUFONT;
|
2023-04-14 16:55:15 +02:00
|
|
|
return addLayer(identifier,
|
2023-04-16 17:20:54 +02:00
|
|
|
props,
|
|
|
|
props.fontVariations,
|
2023-04-14 16:55:15 +02:00
|
|
|
layerID);
|
|
|
|
|
|
|
|
}
|
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,
|
2023-04-14 16:55:15 +02:00
|
|
|
const std::vector <FontVariation> & variations,
|
|
|
|
LayerID layerID){
|
2023-04-11 12:23:14 +02:00
|
|
|
auto props = Layer::Props();
|
|
|
|
props.text = text;
|
|
|
|
props.fontPath = identifier.fontPath;
|
|
|
|
return addLayer(identifier,
|
|
|
|
props,
|
2023-04-16 17:20:54 +02:00
|
|
|
variations,
|
|
|
|
layerID);
|
2023-04-11 12:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LayerID LayerComposition::addLayer(const ComboIdentifier & identifier,
|
|
|
|
const Layer::Props & props,
|
2023-04-14 16:55:15 +02:00
|
|
|
const std::vector <FontVariation> & variations,
|
|
|
|
LayerID layerID){
|
2023-04-13 17:15:35 +02:00
|
|
|
switch(identifier.type){
|
2023-04-16 17:20:54 +02:00
|
|
|
case LayerType::GPUFONT: {
|
2023-04-13 17:15:35 +02:00
|
|
|
shared_ptr <GPUFontAtlasLayerCombo> combo;
|
|
|
|
auto comboIterator = atlasLayerCombos.find(identifier);
|
|
|
|
if(comboIterator == atlasLayerCombos.end()){
|
|
|
|
// we don't have one yet
|
|
|
|
// so let's create it
|
|
|
|
combo = make_shared <GPUFontAtlasLayerCombo>();
|
|
|
|
GPUFontAtlasLayerComboSettings settings;
|
|
|
|
combo->setup(identifier, settings);
|
|
|
|
atlasLayerCombos[identifier] = combo;
|
|
|
|
}else{
|
|
|
|
// use existing combo
|
|
|
|
combo = dynamic_pointer_cast <GPUFontAtlasLayerCombo>(comboIterator->second);
|
2023-04-06 11:17:22 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:15:35 +02:00
|
|
|
auto layer = make_shared <GPUFontLayer>();
|
2023-04-14 16:55:15 +02:00
|
|
|
layer->vFlip = vFlipState;
|
2023-04-13 17:15:35 +02:00
|
|
|
layer->setProps(props);
|
2023-04-14 16:55:15 +02:00
|
|
|
layer->setup();
|
|
|
|
if(layerID == ""){
|
|
|
|
layerID = layer->getId();
|
|
|
|
}else{
|
|
|
|
layer->setId(layerID);
|
|
|
|
}
|
2023-04-13 17:15:35 +02:00
|
|
|
combo->careForChild(layer);
|
|
|
|
layers[layerID] = layer;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2023-04-16 17:20:54 +02:00
|
|
|
case LayerType::MSDFGEN: {
|
2023-04-13 17:15:35 +02:00
|
|
|
// 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->vFlip = vFlipState;
|
|
|
|
layer->setProps(props);
|
|
|
|
layer->setup();
|
2023-04-14 16:55:15 +02:00
|
|
|
if(layerID == ""){
|
|
|
|
layerID = layer->getId();
|
|
|
|
}else{
|
|
|
|
layer->setId(layerID);
|
|
|
|
}
|
2023-04-13 17:15:35 +02:00
|
|
|
std::vector <ofxMsdfgen::FontVariation> msdfVariations;
|
|
|
|
for(const auto & v : variations){
|
|
|
|
msdfVariations.push_back({v.name, v.value});
|
2023-04-06 11:17:22 +02:00
|
|
|
}
|
2023-04-13 17:15:35 +02:00
|
|
|
// TODO: do not add Variation to atlas, but to combo,
|
|
|
|
// so we know it's dirty
|
|
|
|
combo->atlas->addVariations(msdfVariations);
|
|
|
|
combo->careForChild(layer);
|
|
|
|
layers[layer->getId()] = layer;
|
|
|
|
atlasLayerCombos[identifier] = std::move(combo);
|
|
|
|
layerID = layer->getId();
|
|
|
|
}
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|
2023-04-07 16:17:07 +02:00
|
|
|
return layerID;
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:15:35 +02:00
|
|
|
void LayerComposition::removeLayer(const LayerID & id){
|
|
|
|
auto layer = getLayer(id);
|
|
|
|
for(auto & it : atlasLayerCombos){
|
|
|
|
it.second->abandonChild(layer);
|
|
|
|
}
|
|
|
|
layers.erase(id);
|
2023-05-05 15:01:52 +02:00
|
|
|
Layer::n_layers--;
|
2023-04-13 17:15:35 +02:00
|
|
|
}
|
|
|
|
|
2023-04-22 13:48:02 +02:00
|
|
|
const unordered_map <LayerID, shared_ptr <Layer> > & LayerComposition::getLayers(){
|
|
|
|
return layers;
|
|
|
|
}
|
2023-04-07 16:17:07 +02:00
|
|
|
shared_ptr <Layer> LayerComposition::getLayer(const LayerID & layerID){
|
|
|
|
return layers[layerID];
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:40:45 +02:00
|
|
|
void LayerComposition::draw(int width, int height) const {
|
2023-05-14 08:33:23 +02:00
|
|
|
for(unsigned int i = layerOrder.size(); i-- > 0;){
|
|
|
|
LayerID layerID = layerOrder[i];
|
|
|
|
if(layers.count(layerID) > 0){
|
|
|
|
auto layer = layers.at(layerID);
|
|
|
|
if(layers.at(layerID)->getType() == LayerType::MSDFGEN){
|
|
|
|
layer->draw(glm::vec3(200, 200, 0));
|
|
|
|
}else{
|
|
|
|
auto combo =
|
|
|
|
static_pointer_cast <GPUFontAtlasLayerCombo>(atlasLayerCombos.at(layer->getMomsComboIdentifier()));
|
|
|
|
auto gpulayer =
|
|
|
|
static_pointer_cast <GPUFontLayer>(layer);
|
|
|
|
combo->draw(width, height, gpulayer);
|
|
|
|
}
|
2023-04-12 13:59:09 +02:00
|
|
|
}
|
2023-04-06 11:17:22 +02:00
|
|
|
}
|
2023-05-14 08:33:23 +02:00
|
|
|
//for(const auto & layer_it : layers){
|
|
|
|
//if(layer_it.second->getType() == LayerType::MSDFGEN){
|
|
|
|
//layer_it.second->draw(glm::vec3(200, 200, 0));
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
//for(const auto & combo_it : atlasLayerCombos){
|
|
|
|
//if(combo_it.first.type == LayerType::GPUFONT){
|
|
|
|
//auto combo =
|
|
|
|
//static_pointer_cast <GPUFontAtlasLayerCombo>(combo_it.second);
|
|
|
|
//combo->draw(width, height);
|
|
|
|
//}
|
|
|
|
//}
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:23:14 +02:00
|
|
|
const unordered_map <ComboIdentifier, shared_ptr <AtlasLayerCombo> > & LayerComposition::getAtlasLayerCombos() const {
|
|
|
|
return atlasLayerCombos;
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:17:30 +02:00
|
|
|
void LayerComposition::setVFlip(bool vFlip){
|
|
|
|
vFlipState = vFlip ? V_FLIP_ON : V_FLIP_OFF;
|
|
|
|
for(auto & layer : layers){
|
|
|
|
layer.second->setVFlip(vFlipState);
|
|
|
|
}
|
2023-04-12 15:47:01 +02:00
|
|
|
for(const auto & combo_it : atlasLayerCombos){
|
|
|
|
combo_it.second->setVFlip(vFlipState);
|
|
|
|
}
|
2023-04-11 13:17:30 +02:00
|
|
|
}
|
|
|
|
|
2023-05-14 08:33:23 +02:00
|
|
|
void LayerComposition::setLayerOrder(const vector <LayerID> & layerOrder){
|
|
|
|
this->layerOrder = layerOrder;
|
|
|
|
}
|
|
|
|
|
2023-03-25 18:59:58 +01:00
|
|
|
}
|