rudimentary Layer

This commit is contained in:
jrkb 2023-03-25 13:51:04 +01:00
parent cd5715eedc
commit aa3be65dae
4 changed files with 154 additions and 1 deletions

View file

@ -1 +1,54 @@
#include "Layer.h"
#include "ofAppRunner.h"
#include "ofGraphics.h"
namespace ofxVariableLab {
Layer::Layer(){
}
Layer::~Layer(){
}
MsdfLayer::MsdfLayer(){
}
MsdfLayer::~MsdfLayer(){
}
void Layer::update(){
}
void MsdfLayer::draw(float x, float y) const {
ofDisableAlphaBlending();
ofEnableDepthTest();
shader->begin();
shader->setUniformTexture("msdf", atlasImage->getTexture(), 0);
shader->setUniform4f("fontColor", ofFloatColor::white);
float pixelRange = 2;
int atlas_w = atlasImage->getWidth();
int atlas_h = atlasImage->getHeight();
int atlas_x = 0;
int atlas_y = 0;
glm::vec2 unitRange = glm::vec2(pixelRange) / glm::vec2(atlas_w, atlas_h);
shader->setUniform2f("unitRange", unitRange);
//atlasImage->drawSubsection(0, 0, w, h, x, y, w, h);
atlasImage->draw(x, y);
shader->end();
ofDisableDepthTest();
ofEnableAlphaBlending();
}
void Layer::setProps(const Props & props){
while(propsBuffer.size() > max(0, int(settings.maxBufferSize - 1))){
propsBuffer.pop_back();
}
propsBuffer.push_front(props);
}
void Layer::clearPropsBuffer(){
propsBuffer.clear();
}
void Layer::setId(const string & id){
this->id = id;
}
const string & Layer::getId(){
return id;
}
}

View file

@ -2,7 +2,77 @@
#include "ofMain.h"
//uuid
#include "random_id.h"
namespace ofxVariableLab {
class Layer {
}
protected:
Layer();
public:
~Layer();
enum TransformOrigin {
TOP_LEFT,
TOP_RIGHT,
CENTER,
BOTTOM_LEFT,
BOTTOM_RIGHT
};
struct Props {
float x = 0;
float y = 0;
float rotation = 0;
float wght = 100;
float fontSize_px = 24;
float * color[4];
TransformOrigin transformOrigin = TransformOrigin::CENTER;
bool mirror_x = false;
float mirror_x_distance = 0;
bool mirror_y = false;
float mirror_y_distance = 0;
float letterDelay = 0;
string text = "toast";
};
struct Settings {
uint32_t maxBufferSize = 100;
};
void update();
virtual void draw(float x = 0, float y = 0) const = 0;
void setProps(const Props & props);
//void setProps(float _x,
//float _y,
//float _rotation,
//float _wght,
//float _fontSize_px,
//float * _color[4],
//int _transformOrigin,
//bool _mirror_x,
//float _mirror_x_distance,
//bool _mirror_y,
//float _mirror_y_distance,
//float _letterDelay,
//string _text);
void clearPropsBuffer();
void setId(const string & id);
const string & getId();
Settings settings;
std::deque <Props> propsBuffer;
shared_ptr <ofShader> shader;
/// \brief are props updated but not drawn yet
bool isDirty = true;
/// \brief hashed id
string id;
};
class MsdfLayer : public Layer {
public:
MsdfLayer();
~MsdfLayer();
void draw(float x = 0, float y = 0) const override;
shared_ptr <ofImage> atlasImage;
};
}

View file

@ -1,3 +1,4 @@
#pragma once
#include "random_id.h"
#include "Layer.h"

29
src/random_id.h Normal file
View file

@ -0,0 +1,29 @@
#pragma once
#include <iostream>
#include <random>
namespace ofxVariableLab {
/// \brief create pseudo-random id
static std::string random_id(std::size_t len = 24){
static const std::string_view hex_chars = "0123456789abcdef";
using Generator = std::mt19937;
Generator gen{std::random_device{}()};
std::string uuid;
uuid.reserve(len);
while(uuid.size() < len){
auto n = gen();
for(auto i = Generator::max(); i & 0x8 && uuid.size() < len; i >>= 4){
uuid += hex_chars[n & 0xf];
n >>= 4;
}
}
return uuid;
}
}