Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/wasm-application.ts @ 240:ddbb339ed4cf am
orthancBaseUrl is now configurable
author | am@osimis.io |
---|---|
date | Wed, 20 Jun 2018 09:43:36 +0200 |
parents | 126c9c0c9333 |
children | 8ff70c04c6df |
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; | |
13 var NotifyUpdateContent: Function = null; | |
14 var SetStartupParameter: Function = null; | |
15 var CreateWasmApplication: Function = null; | |
16 var CreateCppViewport: Function = null; | |
17 var ReleaseCppViewport: Function = null; | |
18 var StartWasmApplication: Function = null; | |
19 | |
20 | |
21 function UpdateContentThread() { | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
22 if (NotifyUpdateContent != null) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
23 NotifyUpdateContent(); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
24 } |
230 | 25 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
26 setTimeout(UpdateContentThread, 100); // Update the viewport content every 100ms if need be |
230 | 27 } |
28 | |
29 | |
30 function GetUriParameters() { | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
31 var parameters = window.location.search.substr(1); |
230 | 32 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
33 if (parameters != null && |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
34 parameters != '') { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
35 var result = {}; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
36 var tokens = parameters.split('&'); |
230 | 37 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
38 for (var i = 0; i < tokens.length; i++) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
39 var tmp = tokens[i].split('='); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
40 if (tmp.length == 2) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
41 result[tmp[0]] = decodeURIComponent(tmp[1]); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
42 } |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
43 } |
230 | 44 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
45 return result; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
46 } |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
47 else { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
48 return {}; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
49 } |
230 | 50 } |
51 | |
229 | 52 module Stone { |
230 | 53 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
54 export class WasmApplication { |
230 | 55 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
56 private viewport_: WasmViewport; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
57 private canvasId_: string; |
230 | 58 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
59 private pimpl_: any; // Private pointer to the underlying WebAssembly C++ object |
230 | 60 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
61 public constructor(canvasId: string) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
62 this.canvasId_ = canvasId; |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
63 //this.module_ = module; |
229 | 64 } |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
65 } |
229 | 66 } |
230 | 67 |
68 | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
69 function _InitializeWasmApplication(canvasId: string): void { |
230 | 70 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
71 /************************************** */ |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
72 CreateWasmApplication(); |
226 | 73 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
74 // 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
|
75 var parameters = GetUriParameters(); |
230 | 76 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
77 for (var key in parameters) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
78 if (parameters.hasOwnProperty(key)) { |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
79 SetStartupParameter(key, parameters[key]); |
230 | 80 } |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
81 } |
230 | 82 |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
83 StartWasmApplication(); |
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 UpdateContentThread(); |
230 | 87 } |
226 | 88 |
240 | 89 function InitializeWasmApplication(wasmModuleName: string, orthancBaseUrl: string) { |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
90 |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
91 Stone.Framework.Configure(wasmModuleName); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
92 |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
93 // 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
|
94 // 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
|
95 Stone.Framework.Initialize(true, function () { |
230 | 96 |
97 console.log("Connecting C++ methods to JS methods"); | |
98 | |
99 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
|
100 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
|
101 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
|
102 ReleaseCppViewport = StoneFrameworkModule.cwrap('ReleaseCppViewport', null, ['number']); |
233 | 103 StartWasmApplication = StoneFrameworkModule.cwrap('StartWasmApplication', null, ['number']); |
230 | 104 |
105 WasmWebService_NotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']); | |
106 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']); | |
107 NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []); | |
108 | |
240 | 109 StoneFrameworkModule.ccall('WasmWebService_SetBaseUrl', null, ['string'], [orthancBaseUrl]); |
110 | |
230 | 111 // Prevent scrolling |
112 document.body.addEventListener('touchmove', function (event) { | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
113 event.preventDefault(); |
230 | 114 }, false); |
115 | |
238
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
116 _InitializeWasmApplication("canvas"); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
117 }); |
126c9c0c9333
SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents:
236
diff
changeset
|
118 } |