Mercurial > hg > orthanc-stone
comparison Samples/WebAssembly/BasicScene.cpp @ 752:6030db24eaea
Fixed wasm sample (BasicScene) according to changes (ViewportController) +
hg ignore update + dev env notes for bgo
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 22 May 2019 16:00:58 +0200 |
parents | 419e5662d7a5 |
children | 92c400a09f1b |
comparison
equal
deleted
inserted
replaced
751:712ff6ff3c19 | 752:6030db24eaea |
---|---|
30 #include "../../Framework/Scene2D/OpenGLCompositor.h" | 30 #include "../../Framework/Scene2D/OpenGLCompositor.h" |
31 #include "../../Framework/Scene2D/PanSceneTracker.h" | 31 #include "../../Framework/Scene2D/PanSceneTracker.h" |
32 #include "../../Framework/Scene2D/RotateSceneTracker.h" | 32 #include "../../Framework/Scene2D/RotateSceneTracker.h" |
33 #include "../../Framework/Scene2D/Scene2D.h" | 33 #include "../../Framework/Scene2D/Scene2D.h" |
34 #include "../../Framework/Scene2D/ZoomSceneTracker.h" | 34 #include "../../Framework/Scene2D/ZoomSceneTracker.h" |
35 #include "../../Framework/Scene2DViewport/ViewportController.h" | |
35 #include "../../Framework/StoneInitialization.h" | 36 #include "../../Framework/StoneInitialization.h" |
36 #include "../../Framework/OpenGL/WebAssemblyOpenGLContext.h" | 37 #include "../../Framework/OpenGL/WebAssemblyOpenGLContext.h" |
37 | 38 |
38 // From Orthanc framework | 39 // From Orthanc framework |
39 #include <Core/Images/Image.h> | 40 #include <Core/Images/Image.h> |
40 #include <Core/Logging.h> | 41 #include <Core/Logging.h> |
41 #include <Core/OrthancException.h> | 42 #include <Core/OrthancException.h> |
42 | 43 |
44 #include <boost/make_shared.hpp> | |
45 | |
43 #include <stdio.h> | 46 #include <stdio.h> |
44 | 47 |
45 static const unsigned int FONT_SIZE = 32; | 48 static const unsigned int FONT_SIZE = 32; |
46 | 49 |
47 | 50 using boost::shared_ptr; |
48 void PrepareScene(OrthancStone::Scene2D& scene) | 51 using boost::make_shared; |
52 | |
53 void PrepareScene(OrthancStone::Scene2DPtr scene) | |
49 { | 54 { |
50 using namespace OrthancStone; | 55 using namespace OrthancStone; |
51 | 56 |
52 // Texture of 2x2 size | 57 // Texture of 2x2 size |
53 if (1) | 58 if (1) |
70 | 75 |
71 p[3] = 255; | 76 p[3] = 255; |
72 p[4] = 0; | 77 p[4] = 0; |
73 p[5] = 0; | 78 p[5] = 0; |
74 | 79 |
75 scene.SetLayer(12, new ColorTextureSceneLayer(i)); | 80 scene->SetLayer(12, new ColorTextureSceneLayer(i)); |
76 | 81 |
77 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | 82 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); |
78 l->SetOrigin(-3, 2); | 83 l->SetOrigin(-3, 2); |
79 l->SetPixelSpacing(1.5, 1); | 84 l->SetPixelSpacing(1.5, 1); |
80 l->SetAngle(20.0 / 180.0 * M_PI); | 85 l->SetAngle(20.0 / 180.0 * M_PI); |
81 scene.SetLayer(14, l.release()); | 86 scene->SetLayer(14, l.release()); |
82 } | 87 } |
83 | 88 |
84 // Texture of 1x1 size | 89 // Texture of 1x1 size |
85 if (1) | 90 if (1) |
86 { | 91 { |
92 p[2] = 0; | 97 p[2] = 0; |
93 | 98 |
94 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); | 99 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i)); |
95 l->SetOrigin(-2, 1); | 100 l->SetOrigin(-2, 1); |
96 l->SetAngle(20.0 / 180.0 * M_PI); | 101 l->SetAngle(20.0 / 180.0 * M_PI); |
97 scene.SetLayer(13, l.release()); | 102 scene->SetLayer(13, l.release()); |
98 } | 103 } |
99 | 104 |
100 // Some lines | 105 // Some lines |
101 { | 106 { |
102 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer); | 107 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer); |
124 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy)); | 129 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy)); |
125 chain.push_back(ScenePoint2D(4, 2)); | 130 chain.push_back(ScenePoint2D(4, 2)); |
126 layer->AddChain(chain, false); | 131 layer->AddChain(chain, false); |
127 | 132 |
128 layer->SetColor(0,255, 255); | 133 layer->SetColor(0,255, 255); |
129 scene.SetLayer(50, layer.release()); | 134 scene->SetLayer(50, layer.release()); |
130 } | 135 } |
131 | 136 |
132 // Some text | 137 // Some text |
133 if (1) | 138 if (1) |
134 { | 139 { |
135 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer); | 140 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer); |
136 layer->SetText("Hello"); | 141 layer->SetText("Hello"); |
137 scene.SetLayer(100, layer.release()); | 142 scene->SetLayer(100, layer.release()); |
138 } | 143 } |
139 } | 144 } |
140 | |
141 | |
142 | |
143 | 145 |
144 namespace OrthancStone | 146 namespace OrthancStone |
145 { | 147 { |
146 class WebAssemblyViewport : public boost::noncopyable | 148 class WebAssemblyViewport : public boost::noncopyable |
147 { | 149 { |
148 private: | 150 private: |
151 // the construction order is important because compositor_ | |
152 // will hold a reference to the scene that belong to the | |
153 // controller_ object | |
149 OpenGL::WebAssemblyOpenGLContext context_; | 154 OpenGL::WebAssemblyOpenGLContext context_; |
150 Scene2D scene_; | 155 ViewportControllerPtr controller_; |
151 OpenGLCompositor compositor_; | 156 OpenGLCompositor compositor_; |
152 | 157 |
153 void SetupEvents(const std::string& canvas); | 158 void SetupEvents(const std::string& canvas); |
154 | 159 |
155 public: | 160 public: |
156 WebAssemblyViewport(MessageBroker& broker, | 161 WebAssemblyViewport(MessageBroker& broker, |
157 const std::string& canvas) : | 162 const std::string& canvas) : |
158 context_(canvas), | 163 context_(canvas), |
159 scene_(broker), | 164 controller_(make_shared<ViewportController>(broker)), |
160 compositor_(context_, scene_) | 165 compositor_(context_, *controller_->GetScene()) |
161 { | 166 { |
162 compositor_.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, | 167 compositor_.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, |
163 FONT_SIZE, Orthanc::Encoding_Latin1); | 168 FONT_SIZE, Orthanc::Encoding_Latin1); |
164 SetupEvents(canvas); | 169 SetupEvents(canvas); |
165 } | 170 } |
166 | 171 |
167 Scene2D& GetScene() | 172 Scene2DPtr GetScene() |
168 { | 173 { |
169 return scene_; | 174 return controller_->GetScene(); |
175 } | |
176 | |
177 ViewportControllerPtr GetController() | |
178 { | |
179 return controller_; | |
170 } | 180 } |
171 | 181 |
172 void UpdateSize() | 182 void UpdateSize() |
173 { | 183 { |
174 context_.UpdateSize(); | 184 context_.UpdateSize(); |
200 { | 210 { |
201 return context_.GetCanvasHeight(); | 211 return context_.GetCanvasHeight(); |
202 } | 212 } |
203 }; | 213 }; |
204 | 214 |
205 | |
206 | |
207 class ActiveTracker : public boost::noncopyable | 215 class ActiveTracker : public boost::noncopyable |
208 { | 216 { |
209 private: | 217 private: |
210 std::auto_ptr<IPointerTracker> tracker_; | 218 OrthancStone::FlexiblePointerTrackerPtr tracker_; |
211 std::string canvasIdentifier_; | 219 std::string canvasIdentifier_; |
212 bool insideCanvas_; | 220 bool insideCanvas_; |
213 | 221 |
214 public: | 222 public: |
215 ActiveTracker(IPointerTracker* tracker, | 223 ActiveTracker(FlexiblePointerTrackerPtr tracker, |
216 const WebAssemblyViewport& viewport) : | 224 const WebAssemblyViewport& viewport) : |
217 tracker_(tracker), | 225 tracker_(tracker), |
218 canvasIdentifier_(viewport.GetCanvasIdentifier()), | 226 canvasIdentifier_(viewport.GetCanvasIdentifier()), |
219 insideCanvas_(true) | 227 insideCanvas_(true) |
220 { | 228 { |
222 { | 230 { |
223 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | 231 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
224 } | 232 } |
225 } | 233 } |
226 | 234 |
227 void Update(const PointerEvent& event) | 235 bool IsAlive() const |
228 { | 236 { |
229 tracker_->Update(event); | 237 return tracker_->IsAlive(); |
230 } | 238 } |
231 | 239 |
232 void Release() | 240 void PointerMove(const PointerEvent& event) |
233 { | 241 { |
234 tracker_->Release(); | 242 tracker_->PointerMove(event); |
243 } | |
244 | |
245 void PointerUp(const PointerEvent& event) | |
246 { | |
247 tracker_->PointerUp(event); | |
235 } | 248 } |
236 }; | 249 }; |
237 } | 250 } |
238 | 251 |
239 | 252 static OrthancStone::PointerEvent* ConvertMouseEvent( |
240 | 253 const EmscriptenMouseEvent& source, |
241 static OrthancStone::PointerEvent* ConvertMouseEvent(const EmscriptenMouseEvent& source, | 254 OrthancStone::WebAssemblyViewport& viewport) |
242 OrthancStone::WebAssemblyViewport& viewport) | 255 { |
243 { | 256 std::auto_ptr<OrthancStone::PointerEvent> target( |
244 std::auto_ptr<OrthancStone::PointerEvent> target(new OrthancStone::PointerEvent); | 257 new OrthancStone::PointerEvent); |
245 | 258 |
246 target->AddPosition(viewport.GetPixelCenterCoordinates(source.targetX, source.targetY)); | 259 target->AddPosition(viewport.GetPixelCenterCoordinates( |
260 source.targetX, source.targetY)); | |
247 target->SetAltModifier(source.altKey); | 261 target->SetAltModifier(source.altKey); |
248 target->SetControlModifier(source.ctrlKey); | 262 target->SetControlModifier(source.ctrlKey); |
249 target->SetShiftModifier(source.shiftKey); | 263 target->SetShiftModifier(source.shiftKey); |
250 | 264 |
251 return target.release(); | 265 return target.release(); |
252 } | 266 } |
253 | 267 |
254 | 268 std::auto_ptr<OrthancStone::ActiveTracker> tracker_; |
255 std::auto_ptr<OrthancStone::ActiveTracker> tracker_; | |
256 | |
257 | 269 |
258 EM_BOOL OnMouseEvent(int eventType, | 270 EM_BOOL OnMouseEvent(int eventType, |
259 const EmscriptenMouseEvent *mouseEvent, | 271 const EmscriptenMouseEvent *mouseEvent, |
260 void *userData) | 272 void *userData) |
261 { | 273 { |
273 char buf[64]; | 285 char buf[64]; |
274 sprintf(buf, "click %d", count++); | 286 sprintf(buf, "click %d", count++); |
275 | 287 |
276 std::auto_ptr<OrthancStone::TextSceneLayer> layer(new OrthancStone::TextSceneLayer); | 288 std::auto_ptr<OrthancStone::TextSceneLayer> layer(new OrthancStone::TextSceneLayer); |
277 layer->SetText(buf); | 289 layer->SetText(buf); |
278 viewport.GetScene().SetLayer(100, layer.release()); | 290 viewport.GetScene()->SetLayer(100, layer.release()); |
279 viewport.Refresh(); | 291 viewport.Refresh(); |
280 break; | 292 break; |
281 } | 293 } |
282 | 294 |
283 case EMSCRIPTEN_EVENT_MOUSEDOWN: | 295 case EMSCRIPTEN_EVENT_MOUSEDOWN: |
284 { | 296 { |
285 std::auto_ptr<OrthancStone::IPointerTracker> t; | 297 OrthancStone::FlexiblePointerTrackerPtr t; |
286 | 298 |
287 { | 299 { |
288 std::auto_ptr<OrthancStone::PointerEvent> event(ConvertMouseEvent(*mouseEvent, viewport)); | 300 std::auto_ptr<OrthancStone::PointerEvent> event( |
301 ConvertMouseEvent(*mouseEvent, viewport)); | |
289 | 302 |
290 switch (mouseEvent->button) | 303 switch (mouseEvent->button) |
291 { | 304 { |
292 case 0: // Left button | 305 case 0: // Left button |
293 t.reset(new OrthancStone::RotateSceneTracker(viewport.GetScene(), *event)); | 306 emscripten_console_log("Creating RotateSceneTracker"); |
307 t.reset(new OrthancStone::RotateSceneTracker( | |
308 viewport.GetController(), *event)); | |
294 break; | 309 break; |
295 | 310 |
296 case 1: // Middle button | 311 case 1: // Middle button |
297 t.reset(new OrthancStone::PanSceneTracker(viewport.GetScene(), *event)); | 312 emscripten_console_log("Creating PanSceneTracker"); |
313 LOG(INFO) << "Creating PanSceneTracker" ; | |
314 t.reset(new OrthancStone::PanSceneTracker( | |
315 viewport.GetController(), *event)); | |
298 break; | 316 break; |
299 | 317 |
300 case 2: // Right button | 318 case 2: // Right button |
301 t.reset(new OrthancStone::ZoomSceneTracker | 319 emscripten_console_log("Creating ZoomSceneTracker"); |
302 (viewport.GetScene(), *event, viewport.GetCanvasWidth())); | 320 t.reset(new OrthancStone::ZoomSceneTracker( |
321 viewport.GetController(), *event, viewport.GetCanvasWidth())); | |
303 break; | 322 break; |
304 | 323 |
305 default: | 324 default: |
306 break; | 325 break; |
307 } | 326 } |
308 } | 327 } |
309 | 328 |
310 if (t.get() != NULL) | 329 if (t.get() != NULL) |
311 { | 330 { |
312 tracker_.reset(new OrthancStone::ActiveTracker(t.release(), viewport)); | 331 tracker_.reset( |
332 new OrthancStone::ActiveTracker(t, viewport)); | |
313 viewport.Refresh(); | 333 viewport.Refresh(); |
314 } | 334 } |
315 | 335 |
316 break; | 336 break; |
317 } | 337 } |
318 | 338 |
319 case EMSCRIPTEN_EVENT_MOUSEMOVE: | 339 case EMSCRIPTEN_EVENT_MOUSEMOVE: |
320 if (tracker_.get() != NULL) | 340 if (tracker_.get() != NULL) |
321 { | 341 { |
322 std::auto_ptr<OrthancStone::PointerEvent> event(ConvertMouseEvent(*mouseEvent, viewport)); | 342 std::auto_ptr<OrthancStone::PointerEvent> event( |
323 tracker_->Update(*event); | 343 ConvertMouseEvent(*mouseEvent, viewport)); |
344 tracker_->PointerMove(*event); | |
324 viewport.Refresh(); | 345 viewport.Refresh(); |
325 } | 346 } |
326 break; | 347 break; |
327 | 348 |
328 case EMSCRIPTEN_EVENT_MOUSEUP: | 349 case EMSCRIPTEN_EVENT_MOUSEUP: |
329 if (tracker_.get() != NULL) | 350 if (tracker_.get() != NULL) |
330 { | 351 { |
331 tracker_->Release(); | 352 std::auto_ptr<OrthancStone::PointerEvent> event( |
353 ConvertMouseEvent(*mouseEvent, viewport)); | |
354 tracker_->PointerUp(*event); | |
332 viewport.Refresh(); | 355 viewport.Refresh(); |
333 tracker_.reset(); | 356 if (!tracker_->IsAlive()) |
357 tracker_.reset(); | |
334 } | 358 } |
335 break; | 359 break; |
336 | 360 |
337 default: | 361 default: |
338 break; | 362 break; |
343 } | 367 } |
344 | 368 |
345 | 369 |
346 void OrthancStone::WebAssemblyViewport::SetupEvents(const std::string& canvas) | 370 void OrthancStone::WebAssemblyViewport::SetupEvents(const std::string& canvas) |
347 { | 371 { |
348 if (0) | 372 #if 0 |
349 { | 373 emscripten_set_click_callback(canvas.c_str(), this, false, OnMouseEvent); |
350 emscripten_set_click_callback(canvas.c_str(), this, false, OnMouseEvent); | 374 #else |
351 } | 375 emscripten_set_mousedown_callback(canvas.c_str(), this, false, OnMouseEvent); |
352 else | 376 emscripten_set_mousemove_callback(canvas.c_str(), this, false, OnMouseEvent); |
353 { | 377 emscripten_set_mouseup_callback(canvas.c_str(), this, false, OnMouseEvent); |
354 emscripten_set_mousedown_callback(canvas.c_str(), this, false, OnMouseEvent); | 378 #endif |
355 emscripten_set_mousemove_callback(canvas.c_str(), this, false, OnMouseEvent); | 379 } |
356 emscripten_set_mouseup_callback(canvas.c_str(), this, false, OnMouseEvent); | |
357 } | |
358 } | |
359 | |
360 | |
361 | |
362 | 380 |
363 std::auto_ptr<OrthancStone::WebAssemblyViewport> viewport1_; | 381 std::auto_ptr<OrthancStone::WebAssemblyViewport> viewport1_; |
364 std::auto_ptr<OrthancStone::WebAssemblyViewport> viewport2_; | 382 std::auto_ptr<OrthancStone::WebAssemblyViewport> viewport2_; |
365 std::auto_ptr<OrthancStone::WebAssemblyViewport> viewport3_; | 383 std::auto_ptr<OrthancStone::WebAssemblyViewport> viewport3_; |
366 OrthancStone::MessageBroker broker_; | 384 OrthancStone::MessageBroker broker_; |
367 | 385 |
368 | 386 EM_BOOL OnWindowResize( |
369 EM_BOOL OnWindowResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData) | 387 int eventType, const EmscriptenUiEvent *uiEvent, void *userData) |
370 { | 388 { |
371 if (viewport1_.get() != NULL) | 389 if (viewport1_.get() != NULL) |
372 { | 390 { |
373 viewport1_->UpdateSize(); | 391 viewport1_->UpdateSize(); |
374 } | 392 } |
384 } | 402 } |
385 | 403 |
386 return true; | 404 return true; |
387 } | 405 } |
388 | 406 |
389 | |
390 | |
391 extern "C" | 407 extern "C" |
392 { | 408 { |
393 int main(int argc, char const *argv[]) | 409 int main(int argc, char const *argv[]) |
394 { | 410 { |
395 OrthancStone::StoneInitialize(); | 411 OrthancStone::StoneInitialize(); |
412 // Orthanc::Logging::EnableInfoLevel(true); | |
413 // Orthanc::Logging::EnableTraceLevel(true); | |
396 EM_ASM(window.dispatchEvent(new CustomEvent("WebAssemblyLoaded"));); | 414 EM_ASM(window.dispatchEvent(new CustomEvent("WebAssemblyLoaded"));); |
397 } | 415 } |
398 | 416 |
399 EMSCRIPTEN_KEEPALIVE | 417 EMSCRIPTEN_KEEPALIVE |
400 void Initialize() | 418 void Initialize() |
401 { | 419 { |
402 viewport1_.reset(new OrthancStone::WebAssemblyViewport(broker_, "mycanvas1")); | 420 viewport1_.reset( |
421 new OrthancStone::WebAssemblyViewport(broker_, "mycanvas1")); | |
403 PrepareScene(viewport1_->GetScene()); | 422 PrepareScene(viewport1_->GetScene()); |
404 viewport1_->UpdateSize(); | 423 viewport1_->UpdateSize(); |
405 | 424 |
406 viewport2_.reset(new OrthancStone::WebAssemblyViewport(broker_, "mycanvas2")); | 425 viewport2_.reset( |
426 new OrthancStone::WebAssemblyViewport(broker_, "mycanvas2")); | |
407 PrepareScene(viewport2_->GetScene()); | 427 PrepareScene(viewport2_->GetScene()); |
408 viewport2_->UpdateSize(); | 428 viewport2_->UpdateSize(); |
409 | 429 |
410 viewport3_.reset(new OrthancStone::WebAssemblyViewport(broker_, "mycanvas3")); | 430 viewport3_.reset( |
431 new OrthancStone::WebAssemblyViewport(broker_, "mycanvas3")); | |
411 PrepareScene(viewport3_->GetScene()); | 432 PrepareScene(viewport3_->GetScene()); |
412 viewport3_->UpdateSize(); | 433 viewport3_->UpdateSize(); |
413 | 434 |
414 emscripten_set_resize_callback("#window", NULL, false, OnWindowResize); | 435 emscripten_set_resize_callback("#window", NULL, false, OnWindowResize); |
415 } | 436 } |