Can render SVGs that have relative sizes, also clip SVGs that go out of bounds
This commit is contained in:
parent
91603e1fc1
commit
d91c7e987f
10 changed files with 23 additions and 12 deletions
|
|
@ -87,7 +87,7 @@ class DrawingProgram {
|
||||||
struct GlobalControls {
|
struct GlobalControls {
|
||||||
float relativeWidth = 15.0f;
|
float relativeWidth = 15.0f;
|
||||||
|
|
||||||
Vector4f foregroundColor{0.9f, 0.9f, 0.9f, 1.0f};
|
Vector4f foregroundColor{1.0f, 1.0f, 1.0f, 1.0f};
|
||||||
Vector4f backgroundColor{0.0f, 0.0f, 0.0f, 1.0f};
|
Vector4f backgroundColor{0.0f, 0.0f, 0.0f, 1.0f};
|
||||||
WorldVec previousMouseWorldPos = {0, 0};
|
WorldVec previousMouseWorldPos = {0, 0};
|
||||||
WorldVec currentMouseWorldPos = {0, 0};
|
WorldVec currentMouseWorldPos = {0, 0};
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ bool FileResourceDisplay::update_draw() const {
|
||||||
void FileResourceDisplay::update(World& w) {
|
void FileResourceDisplay::update(World& w) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileResourceDisplay::load(const std::string& fileName, const std::string& fileData) {
|
bool FileResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) {
|
||||||
this->fileName = fileName;
|
this->fileName = fileName;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
class FileResourceDisplay : public ResourceDisplay {
|
class FileResourceDisplay : public ResourceDisplay {
|
||||||
public:
|
public:
|
||||||
virtual void update(World& w) override;
|
virtual void update(World& w) override;
|
||||||
virtual bool load(const std::string& fileName, const std::string& fileData) override;
|
virtual bool load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) override;
|
||||||
virtual bool update_draw() const override;
|
virtual bool update_draw() const override;
|
||||||
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
||||||
virtual Vector2f get_dimensions() const override;
|
virtual Vector2f get_dimensions() const override;
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ void ImageResourceDisplay::update(World& w) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ImageResourceDisplay::load(const std::string& fileName, const std::string& fileData) {
|
bool ImageResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) {
|
||||||
sk_sp<SkData> newData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size());
|
sk_sp<SkData> newData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size());
|
||||||
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(newData, decoders);
|
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(newData, decoders);
|
||||||
mustUpdateDraw = true;
|
mustUpdateDraw = true;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
class ImageResourceDisplay : public ResourceDisplay {
|
class ImageResourceDisplay : public ResourceDisplay {
|
||||||
public:
|
public:
|
||||||
virtual void update(World& w) override;
|
virtual void update(World& w) override;
|
||||||
virtual bool load(const std::string& fileName, const std::string& fileData) override;
|
virtual bool load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) override;
|
||||||
virtual bool update_draw() const override;
|
virtual bool update_draw() const override;
|
||||||
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
||||||
virtual Vector2f get_dimensions() const override;
|
virtual Vector2f get_dimensions() const override;
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,12 @@ using namespace Eigen;
|
||||||
|
|
||||||
struct DrawData;
|
struct DrawData;
|
||||||
class World;
|
class World;
|
||||||
|
class ResourceManager;
|
||||||
|
|
||||||
class ResourceDisplay {
|
class ResourceDisplay {
|
||||||
public:
|
public:
|
||||||
virtual void update(World& w) = 0;
|
virtual void update(World& w) = 0;
|
||||||
virtual bool load(const std::string& fileName, const std::string& fileData) = 0;
|
virtual bool load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) = 0;
|
||||||
virtual bool update_draw() const = 0;
|
virtual bool update_draw() const = 0;
|
||||||
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) = 0;
|
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) = 0;
|
||||||
virtual Vector2f get_dimensions() const = 0;
|
virtual Vector2f get_dimensions() const = 0;
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ void SvgResourceDisplay::update(World& w) {
|
||||||
mustUpdateDraw = false;
|
mustUpdateDraw = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SvgResourceDisplay::load(const std::string& fileName, const std::string& fileData) {
|
bool SvgResourceDisplay::load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) {
|
||||||
SkMemoryStream strm(fileData.c_str(), fileData.size());
|
SkMemoryStream strm(fileData.c_str(), fileData.size());
|
||||||
svgDom = SkSVGDOM::Builder().make(strm);
|
svgDom = SkSVGDOM::Builder().make(strm);
|
||||||
|
|
||||||
|
|
@ -23,11 +23,20 @@ bool SvgResourceDisplay::load(const std::string& fileName, const std::string& fi
|
||||||
|
|
||||||
svgRootSize = svgDom->containerSize();
|
svgRootSize = svgDom->containerSize();
|
||||||
|
|
||||||
|
if(svgRootSize.width() == 0 || svgRootSize.height() == 0) {
|
||||||
|
svgDom->setContainerSize({1000, 1000});
|
||||||
|
svgRootSize = svgDom->containerSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(svgRootSize.width() == 0 || svgRootSize.height() == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SvgResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) {
|
void SvgResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) {
|
||||||
canvas->save();
|
canvas->save();
|
||||||
|
canvas->clipRect(imRect);
|
||||||
canvas->translate(imRect.x(), imRect.y());
|
canvas->translate(imRect.x(), imRect.y());
|
||||||
canvas->scale(imRect.width() / svgRootSize.width(), imRect.height() / svgRootSize.height());
|
canvas->scale(imRect.width() / svgRootSize.width(), imRect.height() / svgRootSize.height());
|
||||||
svgDom->render(canvas);
|
svgDom->render(canvas);
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
class SvgResourceDisplay : public ResourceDisplay {
|
class SvgResourceDisplay : public ResourceDisplay {
|
||||||
public:
|
public:
|
||||||
virtual void update(World& w) override;
|
virtual void update(World& w) override;
|
||||||
virtual bool load(const std::string& fileName, const std::string& fileData) override;
|
virtual bool load(ResourceManager& rMan, const std::string& fileName, const std::string& fileData) override;
|
||||||
virtual bool update_draw() const override;
|
virtual bool update_draw() const override;
|
||||||
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
||||||
virtual Vector2f get_dimensions() const override;
|
virtual Vector2f get_dimensions() const override;
|
||||||
|
|
|
||||||
|
|
@ -117,19 +117,19 @@ std::shared_ptr<ResourceDisplay> ResourceManager::get_display_data(const ServerC
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto imgResource(std::make_shared<ImageResourceDisplay>());
|
auto imgResource(std::make_shared<ImageResourceDisplay>());
|
||||||
if(imgResource->load(resourceIt->second.name, *resourceIt->second.data)) {
|
if(imgResource->load(*this, resourceIt->second.name, *resourceIt->second.data)) {
|
||||||
displays[fileID] = imgResource;
|
displays[fileID] = imgResource;
|
||||||
return imgResource;
|
return imgResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto svgResource(std::make_shared<SvgResourceDisplay>());
|
auto svgResource(std::make_shared<SvgResourceDisplay>());
|
||||||
if(svgResource->load(resourceIt->second.name, *resourceIt->second.data)) {
|
if(svgResource->load(*this, resourceIt->second.name, *resourceIt->second.data)) {
|
||||||
displays[fileID] = svgResource;
|
displays[fileID] = svgResource;
|
||||||
return svgResource;
|
return svgResource;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto fileResource(std::make_shared<FileResourceDisplay>());
|
auto fileResource(std::make_shared<FileResourceDisplay>());
|
||||||
fileResource->load(resourceIt->second.name, *resourceIt->second.data);
|
fileResource->load(*this, resourceIt->second.name, *resourceIt->second.data);
|
||||||
displays[fileID] = fileResource;
|
displays[fileID] = fileResource;
|
||||||
return fileResource;
|
return fileResource;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,11 +44,12 @@ class ResourceManager {
|
||||||
std::optional<ResourceData> get_resource(const ServerClientID& id) const;
|
std::optional<ResourceData> get_resource(const ServerClientID& id) const;
|
||||||
ClientPortionID get_max_id(ServerPortionID serverID) const;
|
ClientPortionID get_max_id(ServerPortionID serverID) const;
|
||||||
const std::unordered_map<ServerClientID, ResourceData>& resource_list();
|
const std::unordered_map<ServerClientID, ResourceData>& resource_list();
|
||||||
|
|
||||||
|
World& world;
|
||||||
private:
|
private:
|
||||||
std::unordered_map<ServerClientID, ResourceData> resources;
|
std::unordered_map<ServerClientID, ResourceData> resources;
|
||||||
std::unordered_map<ServerClientID, std::shared_ptr<ResourceDisplay>> displays;
|
std::unordered_map<ServerClientID, std::shared_ptr<ResourceDisplay>> displays;
|
||||||
|
|
||||||
static std::array<const SkCodecs::Decoder, 5> decoders;
|
static std::array<const SkCodecs::Decoder, 5> decoders;
|
||||||
World& world;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue