Mercurial > hg > orthanc-stone
changeset 228:210c1ce8e1a6 am
WasmViewport is no more a singleton
author | am@osimis.io |
---|---|
date | Thu, 14 Jun 2018 15:06:29 +0200 |
parents | c8f11437a6fd |
children | b0ba3b38a23c |
files | Platforms/WebAssembly/Defaults.cpp Platforms/WebAssembly/Defaults.h Platforms/WebAssembly/wasm-application.js Platforms/WebAssembly/wasm-viewport.ts |
diffstat | 4 files changed, 85 insertions(+), 133 deletions(-) [+] |
line wrap: on
line diff
--- a/Platforms/WebAssembly/Defaults.cpp Thu Jun 14 13:28:40 2018 +0200 +++ b/Platforms/WebAssembly/Defaults.cpp Thu Jun 14 15:06:29 2018 +0200 @@ -5,6 +5,7 @@ #include "Framework/Widgets/TestCairoWidget.h" #include <Framework/Viewport/WidgetViewport.h> #include <Framework/Widgets/LayerWidget.h> +#include <algorithm> static unsigned int width_ = 0; static unsigned int height_ = 0; @@ -12,13 +13,21 @@ /**********************************/ static std::auto_ptr<OrthancStone::BasicWasmApplication> application; -static std::shared_ptr<OrthancStone::WidgetViewport> viewport_; static OrthancStone::ChangeObserver changeObserver_; static OrthancStone::StatusBar statusBar_; static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_; +std::shared_ptr<OrthancStone::WidgetViewport> FindViewportSharedPtr(ViewportHandle viewport) { + for (const auto& v : viewports_) { + if (v.get() == viewport) { + return v; + } + assert(false); + } +} + #ifdef __cplusplus extern "C" { #endif @@ -38,8 +47,6 @@ viewport->SetStatusBar(statusBar_); viewport->Register(changeObserver_); - // TODO: remove once we really want to handle multiple viewports - viewport_ = viewport; return viewport.get(); } @@ -50,7 +57,6 @@ printf("There are now %d viewports in C++\n", viewports_.size()); } - void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) { printf("CreateWasmApplication\n"); @@ -66,7 +72,7 @@ application->SetStartupParameter(keyc, value); } - void EMSCRIPTEN_KEEPALIVE StartWasmApplication() { + void EMSCRIPTEN_KEEPALIVE StartWasmApplication(ViewportHandle viewport) { printf("StartWasmApplication\n"); @@ -74,79 +80,36 @@ boost::program_options::variables_map parameters; application->GetStartupParameters(parameters); - BasicWasmApplicationContext& context = dynamic_cast<BasicWasmApplicationContext&>(application->CreateApplicationContext(OrthancStone::WasmWebService::GetInstance(), viewport_));; + BasicWasmApplicationContext& context = dynamic_cast<BasicWasmApplicationContext&>(application->CreateApplicationContext(OrthancStone::WasmWebService::GetInstance(), FindViewportSharedPtr(viewport))); application->Initialize(statusBar_, parameters); - viewport_->SetSize(width_, height_); + viewport->SetSize(width_, height_); printf("StartWasmApplication - completed\n"); } - - // void EMSCRIPTEN_KEEPALIVE ViewportUpdate(const char* _instanceId) { - // printf("updating viewport content, Instance = [%s]\n", instanceId.c_str()); - - // layerSource->LoadFrame(instanceId, 0); - // printf("frame loaded\n"); - // instanceWidget->UpdateContent(); - - // printf("update should be done\n"); - // } - // void EMSCRIPTEN_KEEPALIVE ViewportStart() - // { - - // viewport_.reset(new OrthancStone::WidgetViewport); - // viewport_->SetStatusBar(statusBar_); - // viewport_->Register(changeObserver_); - // instanceWidget.reset(new OrthancStone::LayerWidget); - // layerSource = new OrthancStone::OrthancFrameLayerSource(OrthancStone::WasmWebService::GetInstance()); - - // if (!instanceId.empty()) - // { - // layerSource->LoadFrame(instanceId, 0); - // } else { - // printf("No instance provided so far\n"); - // } - - // instanceWidget->AddLayer(layerSource); - - // { - // OrthancStone::RenderStyle s; - // //s.drawGrid_ = true; - // s.alpha_ = 1; - // s.windowing_ = OrthancStone::ImageWindowing_Bone; - // instanceWidget->SetLayerStyle(0, s); - // } - - // viewport_->SetCentralWidget(instanceWidget.release()); - // viewport_->SetSize(width_, height_); - - - // } - void EMSCRIPTEN_KEEPALIVE NotifyUpdateContent() { - // TODO Only launch the JavaScript timer if "HasUpdateContent()" - if (viewport_.get() != NULL && - viewport_->HasUpdateContent()) - { - viewport_->UpdateContent(); + for (auto viewport : viewports_) { + // TODO Only launch the JavaScript timer if "HasUpdateContent()" + if (viewport->HasUpdateContent()) + { + viewport->UpdateContent(); + } + } } - void EMSCRIPTEN_KEEPALIVE ViewportSetSize(unsigned int width, unsigned int height) + void EMSCRIPTEN_KEEPALIVE ViewportSetSize(ViewportHandle viewport, unsigned int width, unsigned int height) { width_ = width; height_ = height; - if (viewport_.get() != NULL) - { - viewport_->SetSize(width, height); - } + viewport->SetSize(width, height); } - int EMSCRIPTEN_KEEPALIVE ViewportRender(OrthancStone::WidgetViewport* viewport, + int EMSCRIPTEN_KEEPALIVE ViewportRender(ViewportHandle viewport, unsigned int width, unsigned int height, uint8_t* data) @@ -184,7 +147,8 @@ } - void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(unsigned int rawButton, + void EMSCRIPTEN_KEEPALIVE ViewportMouseDown(ViewportHandle viewport, + unsigned int rawButton, int x, int y, unsigned int rawModifiers) @@ -208,20 +172,17 @@ return; // Unknown button } - if (viewport_.get() != NULL) - { - viewport_->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */); - } + viewport->MouseDown(button, x, y, OrthancStone::KeyboardModifiers_None /* TODO */); } - void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(int deltaY, + void EMSCRIPTEN_KEEPALIVE ViewportMouseWheel(ViewportHandle viewport, + int deltaY, int x, int y, int isControl) { - if (viewport_.get() != NULL && - deltaY != 0) + if (deltaY != 0) { OrthancStone::MouseWheelDirection direction = (deltaY < 0 ? OrthancStone::MouseWheelDirection_Up : @@ -233,68 +194,55 @@ modifiers = OrthancStone::KeyboardModifiers_Control; } - viewport_->MouseWheel(direction, x, y, modifiers); + viewport->MouseWheel(direction, x, y, modifiers); } } - void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(int x, + void EMSCRIPTEN_KEEPALIVE ViewportMouseMove(ViewportHandle viewport, + int x, int y) { - if (viewport_.get() != NULL) - { - viewport_->MouseMove(x, y); - } + viewport->MouseMove(x, y); } - void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(const char* key, + void EMSCRIPTEN_KEEPALIVE ViewportKeyPressed(ViewportHandle viewport, + const char* key, bool isShiftPressed, bool isControlPressed, bool isAltPressed) { - if (viewport_.get() != NULL) - { - OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; - if (isShiftPressed) { - modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); - } - if (isControlPressed) { - modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); - } - if (isAltPressed) { - modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); - } - printf("key pressed : %c\n", key[0]); - viewport_->KeyPressed(key[0], modifiers); + OrthancStone::KeyboardModifiers modifiers = OrthancStone::KeyboardModifiers_None; + if (isShiftPressed) { + modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Shift); } + if (isControlPressed) { + modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Control); + } + if (isAltPressed) { + modifiers = static_cast<OrthancStone::KeyboardModifiers>(modifiers + OrthancStone::KeyboardModifiers_Alt); + } + printf("key pressed : %c\n", key[0]); + viewport->KeyPressed(key[0], modifiers); } - void EMSCRIPTEN_KEEPALIVE ViewportMouseUp() + void EMSCRIPTEN_KEEPALIVE ViewportMouseUp(ViewportHandle viewport) { - if (viewport_.get() != NULL) - { - viewport_->MouseUp(); - } + viewport->MouseUp(); } - void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter() + void EMSCRIPTEN_KEEPALIVE ViewportMouseEnter(ViewportHandle viewport) { - if (viewport_.get() != NULL) - { - viewport_->MouseEnter(); - } + viewport->MouseEnter(); } - void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave() + void EMSCRIPTEN_KEEPALIVE ViewportMouseLeave(ViewportHandle viewport) { - if (viewport_.get() != NULL) - { - viewport_->MouseLeave(); - } + viewport->MouseLeave(); }
--- a/Platforms/WebAssembly/Defaults.h Thu Jun 14 13:28:40 2018 +0200 +++ b/Platforms/WebAssembly/Defaults.h Thu Jun 14 15:06:29 2018 +0200 @@ -20,8 +20,6 @@ // C++ methods accessible from JS extern void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport); -// extern void EMSCRIPTEN_KEEPALIVE SetStartupParameter(const char* keyc, const char* value); -// extern void EMSCRIPTEN_KEEPALIVE StartWasmApplication(); #ifdef __cplusplus }
--- a/Platforms/WebAssembly/wasm-application.js Thu Jun 14 13:28:40 2018 +0200 +++ b/Platforms/WebAssembly/wasm-application.js Thu Jun 14 15:06:29 2018 +0200 @@ -87,7 +87,7 @@ } } - StartWasmApplication(); + StartWasmApplication(viewport.GetCppViewport()); /************************************** */ UpdateContentThread();
--- a/Platforms/WebAssembly/wasm-viewport.ts Thu Jun 14 13:28:40 2018 +0200 +++ b/Platforms/WebAssembly/wasm-viewport.ts Thu Jun 14 15:06:29 2018 +0200 @@ -37,15 +37,19 @@ this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement; this.context_ = this.htmlCanvas_.getContext('2d'); - this.ViewportSetSize = this.module_.cwrap('ViewportSetSize', null, [ 'number', 'number' ]); + this.ViewportSetSize = this.module_.cwrap('ViewportSetSize', null, [ 'any', 'number', 'number' ]); this.ViewportRender = this.module_.cwrap('ViewportRender', null, [ 'any', 'number', 'number', 'number' ]); - this.ViewportMouseDown = this.module_.cwrap('ViewportMouseDown', null, [ 'number', 'number', 'number', 'number' ]); - this.ViewportMouseMove = this.module_.cwrap('ViewportMouseMove', null, [ 'number', 'number' ]); - this.ViewportMouseUp = this.module_.cwrap('ViewportMouseUp', null, [ ]); - this.ViewportMouseEnter = this.module_.cwrap('ViewportMouseEnter', null, []); - this.ViewportMouseLeave = this.module_.cwrap('ViewportMouseLeave', null, []); - this.ViewportMouseWheel = this.module_.cwrap('ViewportMouseWheel', null, [ 'number', 'number', 'number', 'number' ]); - this.ViewportKeyPressed = this.module_.cwrap('ViewportKeyPressed', null, [ 'string', 'number', 'number' ]); + this.ViewportMouseDown = this.module_.cwrap('ViewportMouseDown', null, [ 'any', 'number', 'number', 'number', 'number' ]); + this.ViewportMouseMove = this.module_.cwrap('ViewportMouseMove', null, [ 'any', 'number', 'number' ]); + this.ViewportMouseUp = this.module_.cwrap('ViewportMouseUp', null, [ 'any' ]); + this.ViewportMouseEnter = this.module_.cwrap('ViewportMouseEnter', null, [ 'any' ]); + this.ViewportMouseLeave = this.module_.cwrap('ViewportMouseLeave', null, [ 'any' ]); + this.ViewportMouseWheel = this.module_.cwrap('ViewportMouseWheel', null, [ 'any', 'number', 'number', 'number', 'number' ]); + this.ViewportKeyPressed = this.module_.cwrap('ViewportKeyPressed', null, [ 'any', 'string', 'number', 'number' ]); + } + + public GetCppViewport() : any { + return this.pimpl_; } public Redraw() { @@ -76,12 +80,12 @@ this.imageData_ = null; } - this.htmlCanvas_.width = window.innerWidth; + this.htmlCanvas_.width = window.innerWidth; this.htmlCanvas_.height = window.innerHeight; if (this.imageData_ === null) { this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height); - this.ViewportSetSize(this.htmlCanvas_.width, this.htmlCanvas_.height); + this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); if (this.renderingBuffer_ != null) { this.module_._free(this.renderingBuffer_); @@ -100,53 +104,53 @@ // Force the rendering of the viewport for the first time this.Resize(); + var that : WasmViewport = this; // Register an event listener to call the Resize() function // each time the window is resized. - window.addEventListener('resize', this.Resize, false); + window.addEventListener('resize', function(event) { + that.Resize(); + }, false); - var that = this; - this.htmlCanvas_.addEventListener('contextmenu', function(event) { // Prevent right click on the canvas event.preventDefault(); }, false); this.htmlCanvas_.addEventListener('mouseleave', function(event) { - that.ViewportMouseLeave(); + that.ViewportMouseLeave(that.pimpl_); }); this.htmlCanvas_.addEventListener('mouseenter', function(event) { - that.ViewportMouseEnter(); + that.ViewportMouseEnter(that.pimpl_); }); this.htmlCanvas_.addEventListener('mousedown', function(event) { var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; - that.ViewportMouseDown(event.button, x, y, 0 /* TODO */); + that.ViewportMouseDown(that.pimpl_, event.button, x, y, 0 /* TODO */); }); this.htmlCanvas_.addEventListener('mousemove', function(event) { var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; - that.ViewportMouseMove(x, y); + that.ViewportMouseMove(that.pimpl_, x, y); }); this.htmlCanvas_.addEventListener('mouseup', function(event) { - that.ViewportMouseUp(); + that.ViewportMouseUp(that.pimpl_); }); window.addEventListener('keydown', function(event) { - that.ViewportKeyPressed(event.key, event.shiftKey, event.ctrlKey, event.altKey); + that.ViewportKeyPressed(that.pimpl_, event.key, event.shiftKey, event.ctrlKey, event.altKey); }); this.htmlCanvas_.addEventListener('wheel', function(event) { var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; - that.ViewportMouseWheel(event.deltaY, x, y, event.ctrlKey); + that.ViewportMouseWheel(that.pimpl_, event.deltaY, x, y, event.ctrlKey); event.preventDefault(); }); - var that = this; this.htmlCanvas_.addEventListener('touchstart', function(event) { that.ResetTouch(); }); @@ -158,26 +162,28 @@ this.htmlCanvas_.addEventListener('touchmove', function(event) { if (that.touchTranslation_.length == 2) { var t = that.GetTouchTranslation(event); - that.ViewportMouseMove(t[0], t[1]); + that.ViewportMouseMove(that.pimpl_, t[0], t[1]); } else if (that.touchZoom_.length == 3) { var z0 = that.touchZoom_; var z1 = that.GetTouchZoom(event); - that.ViewportMouseMove(z0[0], z0[1] - z0[2] + z1[2]); + that.ViewportMouseMove(that.pimpl_, z0[0], z0[1] - z0[2] + z1[2]); } else { // Realize the gesture event if (event.targetTouches.length == 1) { // Exactly one finger inside the canvas => Setup a translation that.touchTranslation_ = that.GetTouchTranslation(event); - that.ViewportMouseDown(1 /* middle button */, + that.ViewportMouseDown(that.pimpl_, + 1 /* middle button */, that.touchTranslation_[0], that.touchTranslation_[1], 0); } else if (event.targetTouches.length == 2) { // Exactly 2 fingers inside the canvas => Setup a pinch/zoom that.touchZoom_ = that.GetTouchZoom(event); var z0 = that.touchZoom_; - that.ViewportMouseDown(2 /* right button */, + that.ViewportMouseDown(that.pimpl_, + 2 /* right button */, z0[0], z0[1], 0); } @@ -188,7 +194,7 @@ public ResetTouch() { if (this.touchTranslation_ || this.touchZoom_) { - this.ViewportMouseUp(); + this.ViewportMouseUp(this.pimpl_); } this.touchTranslation_ = false;