From aa3be65daeae59ae38401c06a2095890886a16d0 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Sat, 25 Mar 2023 13:51:04 +0100 Subject: [PATCH] rudimentary Layer --- src/Layer.cpp | 53 ++++++++++++++++++++++++++++++++ src/Layer.h | 72 +++++++++++++++++++++++++++++++++++++++++++- src/ofxVariableLab.h | 1 + src/random_id.h | 29 ++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/random_id.h diff --git a/src/Layer.cpp b/src/Layer.cpp index dcca60d..0afba6b 100644 --- a/src/Layer.cpp +++ b/src/Layer.cpp @@ -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; +} +} diff --git a/src/Layer.h b/src/Layer.h index 63be2a4..6797073 100644 --- a/src/Layer.h +++ b/src/Layer.h @@ -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 propsBuffer; + shared_ptr 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 atlasImage; +}; + } diff --git a/src/ofxVariableLab.h b/src/ofxVariableLab.h index 542d891..3cc010a 100644 --- a/src/ofxVariableLab.h +++ b/src/ofxVariableLab.h @@ -1,3 +1,4 @@ #pragma once +#include "random_id.h" #include "Layer.h" diff --git a/src/random_id.h b/src/random_id.h new file mode 100644 index 0000000..caede8c --- /dev/null +++ b/src/random_id.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +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; +} + +}