diff options
author | crupest <crupest@outlook.com> | 2023-10-08 12:23:17 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-10-08 12:23:17 +0800 |
commit | 1907d4fffd24e6d6bd1b7db7d903da60e53888dc (patch) | |
tree | 5c5575e7e2584b901e554d736c37f61e6263b904 | |
parent | c216f37a9dd3c360d390a3e134881539731186d1 (diff) | |
download | cru-1907d4fffd24e6d6bd1b7db7d903da60e53888dc.tar.gz cru-1907d4fffd24e6d6bd1b7db7d903da60e53888dc.tar.bz2 cru-1907d4fffd24e6d6bd1b7db7d903da60e53888dc.zip |
...
-rw-r--r-- | include/cru/platform/graphics/web_canvas/WebCanvasPainter.h | 20 | ||||
-rw-r--r-- | src/platform/graphics/web_canvas/Painter.cpp | 32 |
2 files changed, 48 insertions, 4 deletions
diff --git a/include/cru/platform/graphics/web_canvas/WebCanvasPainter.h b/include/cru/platform/graphics/web_canvas/WebCanvasPainter.h index f85f93b0..cd885aa4 100644 --- a/include/cru/platform/graphics/web_canvas/WebCanvasPainter.h +++ b/include/cru/platform/graphics/web_canvas/WebCanvasPainter.h @@ -3,10 +3,22 @@ #include "WebCanvasResource.h" #include <emscripten/val.h> +#include <optional> namespace cru::platform::graphics::web_canvas { -class WebCanvasPainter: public WebCanvasResource, public virtual IPainter { - public: - WebCanvasPainter(WebCanvasGraphicsFactory* factory, emscripten::val context); +class WebCanvasPainter : public WebCanvasResource, public virtual IPainter { + public: + WebCanvasPainter(WebCanvasGraphicsFactory* factory, emscripten::val context, + std::optional<Matrix> init_transform = std::nullopt); + + ~WebCanvasPainter() override; + + public: + Matrix GetTransform() override; + void SetTransform(const Matrix& transform) override; + + private: + Matrix current_transform_; + emscripten::val context_; }; -} +} // namespace cru::platform::graphics::web_canvas diff --git a/src/platform/graphics/web_canvas/Painter.cpp b/src/platform/graphics/web_canvas/Painter.cpp index fd196256..cd04a725 100644 --- a/src/platform/graphics/web_canvas/Painter.cpp +++ b/src/platform/graphics/web_canvas/Painter.cpp @@ -1 +1,33 @@ +#include "cru/platform/graphics/web_canvas/WebCanvasGraphicsFactory.h" #include "cru/platform/graphics/web_canvas/WebCanvasPainter.h" +#include "cru/platform/graphics/web_canvas/WebCanvasResource.h" + +#include <optional> + +namespace cru::platform::graphics::web_canvas { + +namespace { +void contextSetTransform(emscripten::val context, const Matrix& matrix) { + context.call<void>("setTransform", matrix.m11, matrix.m12, matrix.m21, + matrix.m22, matrix.m31, matrix.m32); +} +} // namespace + +WebCanvasPainter::WebCanvasPainter(WebCanvasGraphicsFactory* factory, + emscripten::val context, + std::optional<Matrix> current_transform) + : WebCanvasResource(factory), + context_(context), + current_transform_(current_transform.value_or(Matrix::Identity())) { + contextSetTransform(context_, current_transform_); +} + +WebCanvasPainter::~WebCanvasPainter() {} + +Matrix WebCanvasPainter::GetTransform() { return current_transform_; } + +void WebCanvasPainter::SetTransform(const Matrix& transform) { + current_transform_ = transform; + contextSetTransform(context_, transform); +} +} // namespace cru::platform::graphics::web_canvas |