Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/wasm-application.ts @ 266:c9cf95b49a86 am-2
removed OrthancSlicesLoader::ISliceLoaderObserver; now using standard messages instead
author | am@osimis.io |
---|---|
date | Tue, 21 Aug 2018 18:14:22 +0200 |
parents | 65562a28fe05 |
children |
rev | line source |
---|---|
225 | 1 ///<reference path='stone-framework-loader.ts'/> |
229 | 2 ///<reference path='wasm-viewport.ts'/> |
225 | 3 |
230 | 4 if (!('WebAssembly' in window)) { |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
5 alert('Sorry, your browser does not support WebAssembly :('); |
230 | 6 } |
7 | |
8 declare var StoneFrameworkModule : Stone.Framework; | |
9 | |
10 // global functions | |
11 var WasmWebService_NotifyError: Function = null; | |
12 var WasmWebService_NotifySuccess: Function = null; | |
255 | 13 var WasmWebService_SetBaseUri: Function = null; |
230 | 14 var NotifyUpdateContent: Function = null; |
15 var SetStartupParameter: Function = null; | |
16 var CreateWasmApplication: Function = null; | |
17 var CreateCppViewport: Function = null; | |
18 var ReleaseCppViewport: Function = null; | |
19 var StartWasmApplication: Function = null; | |
20 | |
21 | |
22 function UpdateContentThread() { | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
23 if (NotifyUpdateContent != null) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
24 NotifyUpdateContent(); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
25 } |
230 | 26 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
27 setTimeout(UpdateContentThread, 100); // Update the viewport content every 100ms if need be |
230 | 28 } |
29 | |
30 | |
31 function GetUriParameters() { | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
32 var parameters = window.location.search.substr(1); |
230 | 33 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
34 if (parameters != null && |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
35 parameters != '') { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
36 var result = {}; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
37 var tokens = parameters.split('&'); |
230 | 38 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
39 for (var i = 0; i < tokens.length; i++) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
40 var tmp = tokens[i].split('='); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
41 if (tmp.length == 2) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
42 result[tmp[0]] = decodeURIComponent(tmp[1]); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
43 } |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
44 } |
230 | 45 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
46 return result; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
47 } |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
48 else { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
49 return {}; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
50 } |
230 | 51 } |
52 | |
229 | 53 module Stone { |
230 | 54 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
55 export class WasmApplication { |
230 | 56 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
57 private viewport_: WasmViewport; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
58 private canvasId_: string; |
230 | 59 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
60 private pimpl_: any; // Private pointer to the underlying WebAssembly C++ object |
230 | 61 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
62 public constructor(canvasId: string) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
63 this.canvasId_ = canvasId; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
64 //this.module_ = module; |
229 | 65 } |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
66 } |
229 | 67 } |
230 | 68 |
69 | |
255 | 70 function _InitializeWasmApplication(canvasId: string, orthancBaseUrl: string): void { |
230 | 71 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
72 /************************************** */ |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
73 CreateWasmApplication(); |
255 | 74 WasmWebService_SetBaseUri(orthancBaseUrl); |
75 | |
226 | 76 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
77 // parse uri and transmit the parameters to the app before initializing it |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
78 var parameters = GetUriParameters(); |
230 | 79 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
80 for (var key in parameters) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
81 if (parameters.hasOwnProperty(key)) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
82 SetStartupParameter(key, parameters[key]); |
230 | 83 } |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
84 } |
230 | 85 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
86 StartWasmApplication(); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
87 /************************************** */ |
230 | 88 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
89 UpdateContentThread(); |
230 | 90 } |
226 | 91 |
240 | 92 function InitializeWasmApplication(wasmModuleName: string, orthancBaseUrl: string) { |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
93 |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
94 Stone.Framework.Configure(wasmModuleName); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
95 |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
96 // Wait for the Orthanc Framework to be initialized (this initializes |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
97 // the WebAssembly environment) and then, create and initialize the Wasm application |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
98 Stone.Framework.Initialize(true, function () { |
230 | 99 |
100 console.log("Connecting C++ methods to JS methods"); | |
101 | |
102 SetStartupParameter = StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']); | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
103 CreateWasmApplication = StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']); |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
104 CreateCppViewport = StoneFrameworkModule.cwrap('CreateCppViewport', 'number', []); |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
105 ReleaseCppViewport = StoneFrameworkModule.cwrap('ReleaseCppViewport', null, ['number']); |
233 | 106 StartWasmApplication = StoneFrameworkModule.cwrap('StartWasmApplication', null, ['number']); |
230 | 107 |
108 WasmWebService_NotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']); | |
109 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']); | |
255 | 110 WasmWebService_SetBaseUri = StoneFrameworkModule.cwrap('WasmWebService_SetBaseUri', null, ['string']); |
230 | 111 NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []); |
112 | |
255 | 113 console.log("Connecting C++ methods to JS methods - done - 2"); |
240 | 114 |
230 | 115 // Prevent scrolling |
116 document.body.addEventListener('touchmove', function (event) { | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
117 event.preventDefault(); |
230 | 118 }, false); |
119 | |
255 | 120 _InitializeWasmApplication("canvas", orthancBaseUrl); |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
121 }); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
122 } |