annotate Platforms/Wasm/wasm-application.ts @ 238:126c9c0c9333 am

SimpleViewer demo running both with SDL and Wasm
author am@osimis.io
date Wed, 20 Jun 2018 09:25:39 +0200
parents f73d722d98c8
children ddbb339ed4cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
225
am@osimis.io
parents:
diff changeset
1 ///<reference path='stone-framework-loader.ts'/>
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 226
diff changeset
2 ///<reference path='wasm-viewport.ts'/>
225
am@osimis.io
parents:
diff changeset
3
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
6 }
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
7
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
8 declare var StoneFrameworkModule : Stone.Framework;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
9
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
10 // global functions
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
11 var WasmWebService_NotifyError: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
12 var WasmWebService_NotifySuccess: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
13 var NotifyUpdateContent: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
14 var SetStartupParameter: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
15 var CreateWasmApplication: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
16 var CreateCppViewport: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
17 var ReleaseCppViewport: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
18 var StartWasmApplication: Function = null;
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
19
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
20
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
27 }
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
28
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
29
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
50 }
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
51
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 226
diff changeset
52 module Stone {
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
53
238
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
54 export class WasmApplication {
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 226
diff changeset
64 }
238
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
65 }
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 226
diff changeset
66 }
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
67
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents: 225
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
80 }
238
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
81 }
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
85
238
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
86 UpdateContentThread();
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
87 }
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents: 225
diff changeset
88
238
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
89 function InitializeWasmApplication(wasmModuleName: string) {
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
96
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
97 console.log("Connecting C++ methods to JS methods");
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
98
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
68856534f005 added layout to VSOL, SDL & wasm working
am@osimis.io
parents: 231
diff changeset
103 StartWasmApplication = StoneFrameworkModule.cwrap('StartWasmApplication', null, ['number']);
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
104
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
105 WasmWebService_NotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']);
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
106 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']);
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
107 NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []);
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
108
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
109 // Prevent scrolling
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
110 document.body.addEventListener('touchmove', function (event) {
238
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
111 event.preventDefault();
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
112 }, false);
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
113
238
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
114 _InitializeWasmApplication("canvas");
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
115 });
126c9c0c9333 SimpleViewer demo running both with SDL and Wasm
am@osimis.io
parents: 236
diff changeset
116 }