Initial Commit
This commit is contained in:
commit
6088fc29ed
978 changed files with 353647 additions and 0 deletions
65
src/ResourceDisplay/FileResourceDisplay.cpp
Normal file
65
src/ResourceDisplay/FileResourceDisplay.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#include "FileResourceDisplay.hpp"
|
||||
#include "../FontData.hpp"
|
||||
#include "../GUIStuff/Elements/Element.hpp"
|
||||
#include "../DrawData.hpp"
|
||||
#include "../Toolbar.hpp"
|
||||
#include "../MainProgram.hpp"
|
||||
#include <include/core/SkStream.h>
|
||||
#include <include/core/SkPaint.h>
|
||||
#include <include/core/SkFontMetrics.h>
|
||||
|
||||
std::string FILE_ICON_PATH = "data/icons/file.svg";
|
||||
|
||||
bool FileResourceDisplay::update_draw() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
void FileResourceDisplay::update(World& w) {
|
||||
}
|
||||
|
||||
bool FileResourceDisplay::load(const std::string& fileName, const std::string& fileData) {
|
||||
this->fileName = fileName;
|
||||
return true;
|
||||
}
|
||||
|
||||
void FileResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) {
|
||||
std::shared_ptr<GUIStuff::UpdateInputData>& io = drawData.main->toolbar.io;
|
||||
auto findSVGData = io->svgData.find(FILE_ICON_PATH);
|
||||
sk_sp<SkSVGDOM> svgDom;
|
||||
if(findSVGData == io->svgData.end()) {
|
||||
auto stream = SkStream::MakeFromFile(FILE_ICON_PATH.c_str());
|
||||
if(!stream)
|
||||
throw std::runtime_error("[FileResourceDisplay::draw] Could not open file " + FILE_ICON_PATH);
|
||||
svgDom = SkSVGDOM::Builder().make(*stream);
|
||||
if(!svgDom)
|
||||
throw std::runtime_error("[SVGIcon::update] Could not parse SVG " + FILE_ICON_PATH);
|
||||
}
|
||||
else
|
||||
svgDom = findSVGData->second;
|
||||
|
||||
SkPaint blendingPaint;
|
||||
blendingPaint.setColorFilter(SkColorFilters::Blend(drawData.main->canvasTheme.toolFrontColor, SkColorSpace::MakeSRGB(), SkBlendMode::kSrcIn));
|
||||
canvas->saveLayer(imRect, &blendingPaint);
|
||||
canvas->translate(imRect.x(), imRect.y());
|
||||
canvas->scale(imRect.width() / svgDom->containerSize().width(), imRect.height() / svgDom->containerSize().height());
|
||||
svgDom->render(canvas);
|
||||
canvas->restore();
|
||||
|
||||
SkFont f = io->get_font(40.0f);
|
||||
SkFontMetrics metrics;
|
||||
f.getMetrics(&metrics);
|
||||
|
||||
float nextText = f.measureText(fileName.c_str(), fileName.length(), SkTextEncoding::kUTF8, nullptr);
|
||||
Vector2f bounds{nextText, - metrics.fAscent + metrics.fDescent};
|
||||
|
||||
SkPaint fontPaint(drawData.main->canvasTheme.toolFrontColor);
|
||||
canvas->drawSimpleText(fileName.c_str(), fileName.length(), SkTextEncoding::kUTF8, imRect.x() + imRect.width() * 0.5f - bounds.x() * 0.5f, imRect.y() + imRect.height() + bounds.y(), f, fontPaint);
|
||||
}
|
||||
|
||||
Vector2f FileResourceDisplay::get_dimensions() const {
|
||||
return {100.0f, 100.0f};
|
||||
}
|
||||
|
||||
float FileResourceDisplay::get_dimension_scale() const {
|
||||
return 0.1f;
|
||||
}
|
||||
15
src/ResourceDisplay/FileResourceDisplay.hpp
Normal file
15
src/ResourceDisplay/FileResourceDisplay.hpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
#include "ResourceDisplay.hpp"
|
||||
#include <modules/svg/include/SkSVGDOM.h>
|
||||
|
||||
class FileResourceDisplay : public ResourceDisplay {
|
||||
public:
|
||||
virtual void update(World& w) override;
|
||||
virtual bool load(const std::string& fileName, const std::string& fileData) override;
|
||||
virtual bool update_draw() const override;
|
||||
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
||||
virtual Vector2f get_dimensions() const override;
|
||||
virtual float get_dimension_scale() const override;
|
||||
private:
|
||||
std::string fileName;
|
||||
};
|
||||
82
src/ResourceDisplay/ImageResourceDisplay.cpp
Normal file
82
src/ResourceDisplay/ImageResourceDisplay.cpp
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include "ImageResourceDisplay.hpp"
|
||||
#include <include/codec/SkPngDecoder.h>
|
||||
#include <include/codec/SkJpegDecoder.h>
|
||||
#include <include/codec/SkBmpDecoder.h>
|
||||
#include <include/codec/SkIcoDecoder.h>
|
||||
#include <include/codec/SkGifDecoder.h>
|
||||
#include "../MainProgram.hpp"
|
||||
#include <Helpers/Logger.hpp>
|
||||
|
||||
std::array<const SkCodecs::Decoder, 5> decoders = {
|
||||
SkPngDecoder::Decoder(),
|
||||
SkJpegDecoder::Decoder(),
|
||||
SkBmpDecoder::Decoder(),
|
||||
SkIcoDecoder::Decoder(),
|
||||
SkGifDecoder::Decoder()
|
||||
};
|
||||
|
||||
bool ImageResourceDisplay::update_draw() const {
|
||||
return mustUpdateDraw;
|
||||
}
|
||||
|
||||
void ImageResourceDisplay::update(World& w) {
|
||||
if(frames.size() <= 1)
|
||||
return;
|
||||
currentTime += w.main.deltaTime * 1000.0;
|
||||
mustUpdateDraw = false;
|
||||
for(;;) {
|
||||
if(currentTime >= frames[frameIndex].duration) {
|
||||
currentTime -= frames[frameIndex].duration;
|
||||
frameIndex++;
|
||||
if(frameIndex >= frames.size())
|
||||
frameIndex = 0;
|
||||
mustUpdateDraw = true;
|
||||
}
|
||||
else
|
||||
return;
|
||||
if(frames[frameIndex].duration <= 0.0f)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool ImageResourceDisplay::load(const std::string& fileName, const std::string& fileData) {
|
||||
sk_sp<SkData> newData = SkData::MakeWithoutCopy(fileData.c_str(), fileData.size());
|
||||
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(newData, decoders);
|
||||
if(codec) {
|
||||
auto origin = codec->getOrigin();
|
||||
bool rotate = (origin == kLeftTop_SkEncodedOrigin || origin == kRightTop_SkEncodedOrigin || origin == kRightBottom_SkEncodedOrigin || origin == kLeftBottom_SkEncodedOrigin);
|
||||
int frameCount = codec->getFrameCount();
|
||||
SkImageInfo imageInfo = codec->getInfo();
|
||||
if(rotate) {
|
||||
Logger::get().log("INFO", "Image loaded is rotated, so must rotate dimensions");
|
||||
imageInfo = imageInfo.makeDimensions(SkISize(imageInfo.height(), imageInfo.width()));
|
||||
}
|
||||
for(int i = 0; i < frameCount; i++) {
|
||||
SkCodec::Options imageOptions;
|
||||
imageOptions.fFrameIndex = i;
|
||||
SkCodec::FrameInfo frameInfo;
|
||||
float frameTime = -1.0f;
|
||||
if(codec->getFrameInfo(i, &frameInfo))
|
||||
frameTime = frameInfo.fDuration;
|
||||
auto decodeResult = codec->getImage(imageInfo, &imageOptions);
|
||||
if(std::get<1>(decodeResult) == SkCodec::Result::kSuccess)
|
||||
frames.emplace_back(std::get<0>(decodeResult)->withDefaultMipmaps(), frameTime);
|
||||
else
|
||||
throw std::runtime_error("Could not decode image, got error code " + std::to_string(std::get<1>(decodeResult)) + ". Image has dimensions " + std::to_string(imageInfo.width()) + " " + std::to_string(imageInfo.height()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ImageResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) {
|
||||
canvas->drawImageRect(frames[frameIndex].data, imRect, {SkFilterMode::kLinear, SkMipmapMode::kLinear});
|
||||
}
|
||||
|
||||
Vector2f ImageResourceDisplay::get_dimensions() const {
|
||||
return {frames.front().data->width(), frames.front().data->height()};
|
||||
}
|
||||
|
||||
float ImageResourceDisplay::get_dimension_scale() const {
|
||||
return 0.3f;
|
||||
}
|
||||
23
src/ResourceDisplay/ImageResourceDisplay.hpp
Normal file
23
src/ResourceDisplay/ImageResourceDisplay.hpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
#include "ResourceDisplay.hpp"
|
||||
#include <include/core/SkImage.h>
|
||||
#include <vector>
|
||||
|
||||
class ImageResourceDisplay : public ResourceDisplay {
|
||||
public:
|
||||
virtual void update(World& w) override;
|
||||
virtual bool load(const std::string& fileName, const std::string& fileData) override;
|
||||
virtual bool update_draw() const override;
|
||||
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) override;
|
||||
virtual Vector2f get_dimensions() const override;
|
||||
virtual float get_dimension_scale() const override;
|
||||
private:
|
||||
struct FrameData {
|
||||
sk_sp<SkImage> data;
|
||||
float duration;
|
||||
};
|
||||
std::vector<FrameData> frames;
|
||||
float currentTime = 0.0f;
|
||||
size_t frameIndex = 0;
|
||||
bool mustUpdateDraw = false;
|
||||
};
|
||||
3
src/ResourceDisplay/ResourceDisplay.cpp
Normal file
3
src/ResourceDisplay/ResourceDisplay.cpp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#include "ResourceDisplay.hpp"
|
||||
|
||||
ResourceDisplay::~ResourceDisplay() {}
|
||||
20
src/ResourceDisplay/ResourceDisplay.hpp
Normal file
20
src/ResourceDisplay/ResourceDisplay.hpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
#include <include/core/SkCanvas.h>
|
||||
#include <include/core/SkRect.h>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
using namespace Eigen;
|
||||
|
||||
struct DrawData;
|
||||
class World;
|
||||
|
||||
class ResourceDisplay {
|
||||
public:
|
||||
virtual void update(World& w) = 0;
|
||||
virtual bool load(const std::string& fileName, const std::string& fileData) = 0;
|
||||
virtual bool update_draw() const = 0;
|
||||
virtual void draw(SkCanvas* canvas, const DrawData& drawData, const SkRect& imRect) = 0;
|
||||
virtual Vector2f get_dimensions() const = 0;
|
||||
virtual float get_dimension_scale() const = 0;
|
||||
virtual ~ResourceDisplay();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue