annotate Platforms/Wasm/wasm-application-runner.ts @ 418:c23df8b3433b

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 15 Nov 2018 18:32:48 +0100
parents e33659decec5
children 26b90b110719
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
1 ///<reference path='stone-framework-loader.ts'/>
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
2 ///<reference path='wasm-viewport.ts'/>
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
3
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
4 if (!('WebAssembly' in window)) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
5 alert('Sorry, your browser does not support WebAssembly :(');
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
6 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
7
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
8 declare var StoneFrameworkModule : Stone.Framework;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
9
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
10 // global functions
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
11 var WasmWebService_NotifyError: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
12 var WasmWebService_NotifySuccess: Function = null;
386
e33659decec5 renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 314
diff changeset
13 var WasmDoAnimation: Function = null;
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
14 var SetStartupParameter: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
15 var CreateWasmApplication: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
16 var CreateCppViewport: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
17 var ReleaseCppViewport: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
18 var StartWasmApplication: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
19 var SendMessageToStoneApplication: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
20
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
21
386
e33659decec5 renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 314
diff changeset
22 function DoAnimationThread() {
e33659decec5 renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 314
diff changeset
23 if (WasmDoAnimation != null) {
e33659decec5 renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 314
diff changeset
24 WasmDoAnimation();
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
25 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
26
386
e33659decec5 renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 314
diff changeset
27 setTimeout(DoAnimationThread, 100); // Update the viewport content every 100ms if need be
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
28 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
29
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
30
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 303
diff changeset
31 function GetUriParameters(): Map<string, string> {
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
32 var parameters = window.location.search.substr(1);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
33
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
34 if (parameters != null &&
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
35 parameters != '') {
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 303
diff changeset
36 var result = new Map<string, string>();
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
37 var tokens = parameters.split('&');
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
38
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
39 for (var i = 0; i < tokens.length; i++) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
40 var tmp = tokens[i].split('=');
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
41 if (tmp.length == 2) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
42 result[tmp[0]] = decodeURIComponent(tmp[1]);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
43 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
44 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
45
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
46 return result;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
47 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
48 else {
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 303
diff changeset
49 return new Map<string, string>();
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
50 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
51 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
52
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
53 // function UpdateWebApplication(statusUpdateMessage: string) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
54 // console.log(statusUpdateMessage);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
55 // }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
56
314
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
57 function _InitializeWasmApplication(orthancBaseUrl: string): void {
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
58
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
59 CreateWasmApplication();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
60
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
61 // parse uri and transmit the parameters to the app before initializing it
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 303
diff changeset
62 let parameters = GetUriParameters();
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
63
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 303
diff changeset
64 for (let key in parameters) {
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
65 if (parameters.hasOwnProperty(key)) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
66 SetStartupParameter(key, parameters[key]);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
67 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
68 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
69
418
c23df8b3433b refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
70 StartWasmApplication(orthancBaseUrl);
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 303
diff changeset
71
418
c23df8b3433b refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
72 // trigger a first resize of the canvas that has just been initialized
314
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
73 Stone.WasmViewport.ResizeAll();
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
74
386
e33659decec5 renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 314
diff changeset
75 DoAnimationThread();
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
76 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
77
314
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
78 function InitializeWasmApplication(wasmModuleName: string, orthancBaseUrl: string) {
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
79
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
80 Stone.Framework.Configure(wasmModuleName);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
81
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
82 // Wait for the Orthanc Framework to be initialized (this initializes
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
83 // the WebAssembly environment) and then, create and initialize the Wasm application
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
84 Stone.Framework.Initialize(true, function () {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
85
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
86 console.log("Connecting C++ methods to JS methods");
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
87
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
88 SetStartupParameter = StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
89 CreateWasmApplication = StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
90 CreateCppViewport = StoneFrameworkModule.cwrap('CreateCppViewport', 'number', []);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
91 ReleaseCppViewport = StoneFrameworkModule.cwrap('ReleaseCppViewport', null, ['number']);
418
c23df8b3433b refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
92 StartWasmApplication = StoneFrameworkModule.cwrap('StartWasmApplication', null, ['string']);
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
93
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
94 WasmWebService_NotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
95 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']);
386
e33659decec5 renamed UpdateContent() as DoAnimation()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 314
diff changeset
96 WasmDoAnimation = StoneFrameworkModule.cwrap('WasmDoAnimation', null, []);
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
97
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
98 SendMessageToStoneApplication = StoneFrameworkModule.cwrap('SendMessageToStoneApplication', 'string', ['string']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
99
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
100 console.log("Connecting C++ methods to JS methods - done");
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
101
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
102 // Prevent scrolling
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
103 document.body.addEventListener('touchmove', function (event) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
104 event.preventDefault();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
105 }, false);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
106
314
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
107 _InitializeWasmApplication(orthancBaseUrl);
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
108 });
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
109 }