Mercurial > hg > orthanc-stone
comparison Platforms/WebAssembly/wasm-application.ts @ 230:7d2631320615 am
wasm-application.js is now in ts
author | am@osimis.io |
---|---|
date | Thu, 14 Jun 2018 17:14:10 +0200 |
parents | b0ba3b38a23c |
children | 5027cb2feb51 |
comparison
equal
deleted
inserted
replaced
229:b0ba3b38a23c | 230:7d2631320615 |
---|---|
1 ///<reference path='stone-framework-loader.ts'/> | 1 ///<reference path='stone-framework-loader.ts'/> |
2 ///<reference path='wasm-viewport.ts'/> | 2 ///<reference path='wasm-viewport.ts'/> |
3 | 3 |
4 if (!('WebAssembly' in window)) { | |
5 alert('Sorry, your browser does not support WebAssembly :('); | |
6 } | |
7 | |
8 declare var StoneFrameworkModule : Stone.Framework; | |
9 | |
10 var viewport = null; | |
11 | |
12 // global functions | |
13 var WasmWebService_NotifyError: Function = null; | |
14 var WasmWebService_NotifySuccess: Function = null; | |
15 var NotifyUpdateContent: Function = null; | |
16 var SetStartupParameter: Function = null; | |
17 var CreateWasmApplication: Function = null; | |
18 var CreateCppViewport: Function = null; | |
19 var ReleaseCppViewport: Function = null; | |
20 var StartWasmApplication: Function = null; | |
21 | |
22 | |
23 function UpdateContentThread() { | |
24 if (NotifyUpdateContent != null) { | |
25 NotifyUpdateContent(); | |
26 } | |
27 | |
28 setTimeout(UpdateContentThread, 100); // Update the viewport content every 100ms if need be | |
29 } | |
30 | |
31 | |
32 function GetUriParameters() { | |
33 var parameters = window.location.search.substr(1); | |
34 | |
35 if (parameters != null && | |
36 parameters != '') { | |
37 var result = {}; | |
38 var tokens = parameters.split('&'); | |
39 | |
40 for (var i = 0; i < tokens.length; i++) { | |
41 var tmp = tokens[i].split('='); | |
42 if (tmp.length == 2) { | |
43 result[tmp[0]] = decodeURIComponent(tmp[1]); | |
44 } | |
45 } | |
46 | |
47 return result; | |
48 } | |
49 else { | |
50 return {}; | |
51 } | |
52 } | |
53 | |
4 module Stone { | 54 module Stone { |
5 | 55 |
6 // export declare type InitializationCallback = () => void; | 56 // export declare type InitializationCallback = () => void; |
7 | 57 |
8 // export declare var StoneFrameworkModule : any; | 58 // export declare var StoneFrameworkModule : any; |
9 | 59 |
10 //const ASSETS_FOLDER : string = "assets/lib"; | 60 //const ASSETS_FOLDER : string = "assets/lib"; |
11 //const WASM_FILENAME : string = "orthanc-framework"; | 61 //const WASM_FILENAME : string = "orthanc-framework"; |
12 | 62 |
13 export class WasmApplication { | 63 export class WasmApplication { |
14 | 64 |
15 private viewport_ : WasmViewport; | 65 private viewport_: WasmViewport; |
16 private canvasId_: string; | 66 private canvasId_: string; |
17 | 67 |
18 private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object | 68 private pimpl_: any; // Private pointer to the underlying WebAssembly C++ object |
19 | 69 |
20 public constructor(canvasId: string) { | 70 public constructor(canvasId: string) { |
21 this.canvasId_ = canvasId; | 71 this.canvasId_ = canvasId; |
22 //this.module_ = module; | 72 //this.module_ = module; |
23 } | 73 } |
24 } | 74 } |
25 } | 75 } |
26 | |
27 | |
28 declare function InitializeWasmApplication(canvasId: string) :void; // still in a js file | |
29 | 76 |
30 | 77 |
78 function InitializeWasmApplication(canvasId: string): void { | |
31 | 79 |
80 console.log("Creating main viewport"); | |
81 | |
82 viewport = new Stone.WasmViewport(StoneFrameworkModule, canvasId); | |
83 viewport.Initialize(CreateCppViewport()); | |
84 | |
85 document.getElementById(canvasId).onclick = function () { | |
86 viewport.Redraw(); | |
87 }; | |
88 | |
89 | |
90 /************************************** */ | |
91 CreateWasmApplication(); | |
92 | |
93 // parse uri and transmit the parameters to the app before initializing it | |
94 var parameters = GetUriParameters(); | |
95 | |
96 for (var key in parameters) { | |
97 if (parameters.hasOwnProperty(key)) { | |
98 SetStartupParameter(key, parameters[key]); | |
99 } | |
100 } | |
101 | |
102 StartWasmApplication(viewport.GetCppViewport()); | |
103 /************************************** */ | |
104 | |
105 UpdateContentThread(); | |
106 } | |
32 | 107 |
33 // Wait for the Orthanc Framework to be initialized (this initializes | 108 // Wait for the Orthanc Framework to be initialized (this initializes |
34 // the WebAssembly environment) and then, create and initialize the Wasm application | 109 // the WebAssembly environment) and then, create and initialize the Wasm application |
35 Stone.Framework.Initialize(true, function() { | 110 Stone.Framework.Initialize(true, function () { |
111 | |
112 console.log("Connecting C++ methods to JS methods"); | |
113 | |
114 SetStartupParameter = StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']); | |
115 CreateWasmApplication = StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['any']); | |
116 CreateCppViewport = StoneFrameworkModule.cwrap('CreateCppViewport', 'any', []); | |
117 ReleaseCppViewport = StoneFrameworkModule.cwrap('ReleaseCppViewport', null, ['any']); | |
118 StartWasmApplication = StoneFrameworkModule.cwrap('StartWasmApplication', null, []); | |
119 | |
120 WasmWebService_NotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']); | |
121 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']); | |
122 NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []); | |
123 | |
124 // Prevent scrolling | |
125 document.body.addEventListener('touchmove', function (event) { | |
126 event.preventDefault(); | |
127 }, false); | |
128 | |
129 | |
36 InitializeWasmApplication("canvas"); | 130 InitializeWasmApplication("canvas"); |
37 }); | 131 }); |