Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Viewport/WebAssemblyViewport.cpp @ 1576:92fca2b3ba3d
sanitizing the handling of canvas size
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 24 Sep 2020 16:40:30 +0200 |
parents | e4a52cbbdd70 |
children | e8a120dd05bd |
comparison
equal
deleted
inserted
replaced
1575:e4a52cbbdd70 | 1576:92fca2b3ba3d |
---|---|
36 | 36 |
37 #include <OrthancException.h> | 37 #include <OrthancException.h> |
38 | 38 |
39 #include <boost/make_shared.hpp> | 39 #include <boost/make_shared.hpp> |
40 #include <boost/enable_shared_from_this.hpp> | 40 #include <boost/enable_shared_from_this.hpp> |
41 #include <boost/math/special_functions/round.hpp> | |
41 | 42 |
42 namespace OrthancStone | 43 namespace OrthancStone |
43 { | 44 { |
44 static void ConvertMouseEvent(PointerEvent& target, | 45 static void ConvertMouseEvent(PointerEvent& target, |
45 const EmscriptenMouseEvent& source, | 46 const EmscriptenMouseEvent& source, |
110 | 111 |
111 virtual void Invalidate() ORTHANC_OVERRIDE | 112 virtual void Invalidate() ORTHANC_OVERRIDE |
112 { | 113 { |
113 that_.Invalidate(); | 114 that_.Invalidate(); |
114 } | 115 } |
116 | |
117 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE | |
118 { | |
119 that_.RefreshCanvasSize(); | |
120 } | |
115 }; | 121 }; |
116 | 122 |
117 | 123 |
118 EM_BOOL WebAssemblyViewport::OnRequestAnimationFrame(double time, void *userData) | 124 EM_BOOL WebAssemblyViewport::OnRequestAnimationFrame(double time, void *userData) |
119 { | 125 { |
135 LOG(TRACE) << __func__; | 141 LOG(TRACE) << __func__; |
136 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData); | 142 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData); |
137 | 143 |
138 if (that->compositor_.get() != NULL) | 144 if (that->compositor_.get() != NULL) |
139 { | 145 { |
140 that->UpdateSize(*that->compositor_); | 146 that->RefreshCanvasSize(); |
141 that->Invalidate(); | 147 that->Invalidate(); |
142 } | 148 } |
143 | 149 |
144 LOG(TRACE) << "Exiting: " << __func__; | 150 LOG(TRACE) << "Exiting: " << __func__; |
145 return true; | 151 return true; |
213 void WebAssemblyViewport::FitForPrint() | 219 void WebAssemblyViewport::FitForPrint() |
214 { | 220 { |
215 if (compositor_.get() != NULL && | 221 if (compositor_.get() != NULL && |
216 controller_ /* should always be true */) | 222 controller_ /* should always be true */) |
217 { | 223 { |
218 UpdateSize(*compositor_); | 224 RefreshCanvasSize(); |
219 compositor_->FitContent(controller_->GetScene()); | 225 compositor_->FitContent(controller_->GetScene()); |
220 OnRequestAnimationFrame(0, reinterpret_cast<void*>(this)); | 226 OnRequestAnimationFrame(0, reinterpret_cast<void*>(this)); |
221 } | 227 } |
222 } | 228 } |
223 | 229 |
246 canvasCssSelector_("#" + canvasId), | 252 canvasCssSelector_("#" + canvasId), |
247 #else | 253 #else |
248 canvasCssSelector_(canvasId), | 254 canvasCssSelector_(canvasId), |
249 #endif | 255 #endif |
250 interactor_(new DefaultViewportInteractor), | 256 interactor_(new DefaultViewportInteractor), |
251 enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents) | 257 enableEmscriptenMouseEvents_(enableEmscriptenMouseEvents), |
258 canvasWidth_(0), | |
259 canvasHeight_(0) | |
252 { | 260 { |
253 } | 261 } |
254 | 262 |
255 void WebAssemblyViewport::PostConstructor() | 263 void WebAssemblyViewport::PostConstructor() |
256 { | 264 { |
310 } | 318 } |
311 } | 319 } |
312 | 320 |
313 void WebAssemblyViewport::UpdateCanvasSize() | 321 void WebAssemblyViewport::UpdateCanvasSize() |
314 { | 322 { |
315 UpdateSize(*compositor_); | 323 RefreshCanvasSize(); |
316 } | 324 } |
317 | 325 |
318 WebAssemblyViewport::~WebAssemblyViewport() | 326 WebAssemblyViewport::~WebAssemblyViewport() |
319 { | 327 { |
320 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, | 328 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, |
356 else | 364 else |
357 { | 365 { |
358 interactor_.reset(interactor); | 366 interactor_.reset(interactor); |
359 } | 367 } |
360 } | 368 } |
369 | |
370 | |
371 void WebAssemblyViewport::RefreshCanvasSize() | |
372 { | |
373 double w, h; | |
374 emscripten_get_element_css_size(GetCanvasCssSelector().c_str(), &w, &h); | |
375 | |
376 /** | |
377 * Emscripten has the function emscripten_get_element_css_size() | |
378 * to query the width and height of a named HTML element. I'm | |
379 * calling this first to get the initial size of the canvas DOM | |
380 * element, and then call emscripten_set_canvas_size() to | |
381 * initialize the framebuffer size of the canvas to the same | |
382 * size as its DOM element. | |
383 * https://floooh.github.io/2017/02/22/emsc-html.html | |
384 **/ | |
385 if (w > 0 && | |
386 h > 0) | |
387 { | |
388 canvasWidth_ = static_cast<unsigned int>(boost::math::iround(w)); | |
389 canvasHeight_ = static_cast<unsigned int>(boost::math::iround(h)); | |
390 } | |
391 else | |
392 { | |
393 canvasWidth_ = 0; | |
394 canvasHeight_ = 0; | |
395 } | |
396 | |
397 emscripten_set_canvas_element_size(GetCanvasCssSelector().c_str(), canvasWidth_, canvasHeight_); | |
398 | |
399 if (compositor_.get() != NULL) | |
400 { | |
401 compositor_->SetCanvasSize(canvasWidth_, canvasHeight_); | |
402 } | |
403 } | |
361 } | 404 } |