diff options
-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 |