Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/wasm-viewport.ts @ 496:8b6ceae45ba0 bgo-commands-codegen
Finished (untested) C++, html, typescript, tsc & browserify production.
author | bgo-osimis |
---|---|
date | Sat, 23 Feb 2019 15:04:29 +0100 |
parents | 5055031f4a06 |
children | 7cdb4634846c |
rev | line source |
---|---|
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
1 import wasmApplicationRunner = require('./wasm-application-runner'); |
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
2 //import stoneFrameworkLoader = require('./stone-framework-loader'); |
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
3 |
230 | 4 var isPendingRedraw = false; |
229 | 5 |
230 | 6 function ScheduleWebViewportRedraw(cppViewportHandle: any) : void |
7 { | |
8 if (!isPendingRedraw) { | |
9 isPendingRedraw = true; | |
10 console.log('Scheduling a refresh of the viewport, as its content changed'); | |
11 window.requestAnimationFrame(function() { | |
12 isPendingRedraw = false; | |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
13 WasmViewport.GetFromCppViewport(cppViewportHandle).Redraw(); |
230 | 14 }); |
229 | 15 } |
230 | 16 } |
226 | 17 |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
18 (<any>window).ScheduleWebViewportRedraw = ScheduleWebViewportRedraw; |
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
19 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
20 declare function UTF8ToString(any): string; |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
21 |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
22 function CreateWasmViewport(htmlCanvasId: string) : any { |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
23 var cppViewportHandle = wasmApplicationRunner.CreateCppViewport(); |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
24 var canvasId = UTF8ToString(htmlCanvasId); |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
25 var webViewport = new WasmViewport((<any> window).StoneFrameworkModule, canvasId, cppViewportHandle); // viewports are stored in a static map in WasmViewport -> won't be deleted |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
26 webViewport.Initialize(); |
314 | 27 |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
28 return cppViewportHandle; |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
29 } |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
30 |
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
31 (<any>window).CreateWasmViewport = CreateWasmViewport; |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
32 |
226 | 33 // export declare type InitializationCallback = () => void; |
34 | |
35 // export declare var StoneFrameworkModule : any; | |
36 | |
37 //const ASSETS_FOLDER : string = "assets/lib"; | |
38 //const WASM_FILENAME : string = "orthanc-framework"; | |
229 | 39 |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
40 export class WasmViewport { |
226 | 41 |
313 | 42 private static viewportsMapByCppHandle_ : Map<number, WasmViewport> = new Map<number, WasmViewport>(); // key = the C++ handle |
43 private static viewportsMapByCanvasId_ : Map<string, WasmViewport> = new Map<string, WasmViewport>(); // key = the canvasId | |
229 | 44 |
226 | 45 private module_ : any; |
46 private canvasId_ : string; | |
47 private htmlCanvas_ : HTMLCanvasElement; | |
48 private context_ : CanvasRenderingContext2D; | |
49 private imageData_ : any = null; | |
50 private renderingBuffer_ : any = null; | |
51 private touchZoom_ : any = false; | |
52 private touchTranslation_ : any = false; | |
53 | |
54 private ViewportSetSize : Function; | |
55 private ViewportRender : Function; | |
56 private ViewportMouseDown : Function; | |
57 private ViewportMouseMove : Function; | |
58 private ViewportMouseUp : Function; | |
59 private ViewportMouseEnter : Function; | |
60 private ViewportMouseLeave : Function; | |
61 private ViewportMouseWheel : Function; | |
62 private ViewportKeyPressed : Function; | |
63 | |
227 | 64 private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object |
65 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
66 public constructor(module: any, canvasId: string, cppViewport: any) { |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
67 |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
68 this.pimpl_ = cppViewport; |
313 | 69 WasmViewport.viewportsMapByCppHandle_[this.pimpl_] = this; |
70 WasmViewport.viewportsMapByCanvasId_[canvasId] = this; | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
71 |
226 | 72 this.module_ = module; |
73 this.canvasId_ = canvasId; | |
74 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement; | |
244 | 75 if (this.htmlCanvas_ == null) { |
76 console.log("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'"); | |
77 } | |
226 | 78 this.context_ = this.htmlCanvas_.getContext('2d'); |
79 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
80 this.ViewportSetSize = this.module_.cwrap('ViewportSetSize', null, [ 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
81 this.ViewportRender = this.module_.cwrap('ViewportRender', null, [ 'number', 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
82 this.ViewportMouseDown = this.module_.cwrap('ViewportMouseDown', null, [ 'number', 'number', 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
83 this.ViewportMouseMove = this.module_.cwrap('ViewportMouseMove', null, [ 'number', 'number', 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
84 this.ViewportMouseUp = this.module_.cwrap('ViewportMouseUp', null, [ 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
85 this.ViewportMouseEnter = this.module_.cwrap('ViewportMouseEnter', null, [ 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
86 this.ViewportMouseLeave = this.module_.cwrap('ViewportMouseLeave', null, [ 'number' ]); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
87 this.ViewportMouseWheel = this.module_.cwrap('ViewportMouseWheel', null, [ 'number', 'number', 'number', 'number', 'number' ]); |
327 | 88 this.ViewportKeyPressed = this.module_.cwrap('ViewportKeyPressed', null, [ 'number', 'number', 'string', 'number', 'number' ]); |
228 | 89 } |
90 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
91 public GetCppViewport() : number { |
228 | 92 return this.pimpl_; |
226 | 93 } |
94 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
95 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport { |
313 | 96 if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) { |
97 return WasmViewport.viewportsMapByCppHandle_[cppViewportHandle]; | |
98 } | |
99 console.log("WasmViewport not found !"); | |
100 return undefined; | |
101 } | |
102 | |
103 public static GetFromCanvasId(canvasId: string) : WasmViewport { | |
104 if (WasmViewport.viewportsMapByCanvasId_[canvasId] !== undefined) { | |
105 return WasmViewport.viewportsMapByCanvasId_[canvasId]; | |
229 | 106 } |
107 console.log("WasmViewport not found !"); | |
108 return undefined; | |
109 } | |
110 | |
314 | 111 public static ResizeAll() { |
112 for (let canvasId in WasmViewport.viewportsMapByCanvasId_) { | |
113 WasmViewport.viewportsMapByCanvasId_[canvasId].Resize(); | |
114 } | |
115 } | |
116 | |
226 | 117 public Redraw() { |
118 if (this.imageData_ === null || | |
119 this.renderingBuffer_ === null || | |
227 | 120 this.ViewportRender(this.pimpl_, |
121 this.imageData_.width, | |
226 | 122 this.imageData_.height, |
123 this.renderingBuffer_) == 0) { | |
124 console.log('The rendering has failed'); | |
125 } else { | |
126 // Create an accessor to the rendering buffer (i.e. create a | |
127 // "window" above the heap of the WASM module), then copy it to | |
128 // the ImageData object | |
129 this.imageData_.data.set(new Uint8ClampedArray( | |
130 this.module_.buffer, | |
131 this.renderingBuffer_, | |
132 this.imageData_.width * this.imageData_.height * 4)); | |
133 | |
134 this.context_.putImageData(this.imageData_, 0, 0); | |
135 } | |
136 } | |
137 | |
138 public Resize() { | |
139 if (this.imageData_ != null && | |
140 (this.imageData_.width != window.innerWidth || | |
141 this.imageData_.height != window.innerHeight)) { | |
142 this.imageData_ = null; | |
143 } | |
144 | |
420
8bf717c4e497
canvas size is now defined by the parent div size
am@osimis.io
parents:
327
diff
changeset
|
145 // width/height is defined by the parent width/height |
8bf717c4e497
canvas size is now defined by the parent div size
am@osimis.io
parents:
327
diff
changeset
|
146 this.htmlCanvas_.width = this.htmlCanvas_.parentElement.offsetWidth; |
8bf717c4e497
canvas size is now defined by the parent div size
am@osimis.io
parents:
327
diff
changeset
|
147 this.htmlCanvas_.height = this.htmlCanvas_.parentElement.offsetHeight; |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
148 |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
149 console.log("resizing WasmViewport: ", this.htmlCanvas_.width, "x", this.htmlCanvas_.height); |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
150 |
226 | 151 if (this.imageData_ === null) { |
152 this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height); | |
228 | 153 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); |
226 | 154 |
155 if (this.renderingBuffer_ != null) { | |
156 this.module_._free(this.renderingBuffer_); | |
157 } | |
158 | |
159 this.renderingBuffer_ = this.module_._malloc(this.imageData_.width * this.imageData_.height * 4); | |
313 | 160 } else { |
161 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); | |
226 | 162 } |
163 | |
164 this.Redraw(); | |
165 } | |
166 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
167 public Initialize() { |
227 | 168 |
169 // Force the rendering of the viewport for the first time | |
170 this.Resize(); | |
226 | 171 |
228 | 172 var that : WasmViewport = this; |
227 | 173 // Register an event listener to call the Resize() function |
174 // each time the window is resized. | |
228 | 175 window.addEventListener('resize', function(event) { |
176 that.Resize(); | |
177 }, false); | |
226 | 178 |
227 | 179 this.htmlCanvas_.addEventListener('contextmenu', function(event) { |
180 // Prevent right click on the canvas | |
181 event.preventDefault(); | |
182 }, false); | |
183 | |
184 this.htmlCanvas_.addEventListener('mouseleave', function(event) { | |
228 | 185 that.ViewportMouseLeave(that.pimpl_); |
227 | 186 }); |
187 | |
188 this.htmlCanvas_.addEventListener('mouseenter', function(event) { | |
228 | 189 that.ViewportMouseEnter(that.pimpl_); |
227 | 190 }); |
191 | |
192 this.htmlCanvas_.addEventListener('mousedown', function(event) { | |
193 var x = event.pageX - this.offsetLeft; | |
194 var y = event.pageY - this.offsetTop; | |
228 | 195 that.ViewportMouseDown(that.pimpl_, event.button, x, y, 0 /* TODO */); |
227 | 196 }); |
197 | |
198 this.htmlCanvas_.addEventListener('mousemove', function(event) { | |
199 var x = event.pageX - this.offsetLeft; | |
200 var y = event.pageY - this.offsetTop; | |
228 | 201 that.ViewportMouseMove(that.pimpl_, x, y); |
227 | 202 }); |
203 | |
204 this.htmlCanvas_.addEventListener('mouseup', function(event) { | |
228 | 205 that.ViewportMouseUp(that.pimpl_); |
227 | 206 }); |
207 | |
208 window.addEventListener('keydown', function(event) { | |
327 | 209 var keyChar = event.key; |
210 var keyCode = event.keyCode | |
211 if (keyChar.length == 1) { | |
212 keyCode = 0; // maps to OrthancStone::KeyboardKeys_Generic | |
213 } else { | |
214 keyChar = null; | |
215 } | |
216 // console.log("key: ", keyCode, keyChar); | |
217 that.ViewportKeyPressed(that.pimpl_, keyCode, keyChar, event.shiftKey, event.ctrlKey, event.altKey); | |
227 | 218 }); |
226 | 219 |
227 | 220 this.htmlCanvas_.addEventListener('wheel', function(event) { |
221 var x = event.pageX - this.offsetLeft; | |
222 var y = event.pageY - this.offsetTop; | |
228 | 223 that.ViewportMouseWheel(that.pimpl_, event.deltaY, x, y, event.ctrlKey); |
227 | 224 event.preventDefault(); |
225 }); | |
226 | |
227 this.htmlCanvas_.addEventListener('touchstart', function(event) { | |
228 that.ResetTouch(); | |
229 }); | |
230 | |
231 this.htmlCanvas_.addEventListener('touchend', function(event) { | |
232 that.ResetTouch(); | |
233 }); | |
234 | |
235 this.htmlCanvas_.addEventListener('touchmove', function(event) { | |
236 if (that.touchTranslation_.length == 2) { | |
237 var t = that.GetTouchTranslation(event); | |
228 | 238 that.ViewportMouseMove(that.pimpl_, t[0], t[1]); |
227 | 239 } |
240 else if (that.touchZoom_.length == 3) { | |
226 | 241 var z0 = that.touchZoom_; |
227 | 242 var z1 = that.GetTouchZoom(event); |
228 | 243 that.ViewportMouseMove(that.pimpl_, z0[0], z0[1] - z0[2] + z1[2]); |
227 | 244 } |
245 else { | |
246 // Realize the gesture event | |
247 if (event.targetTouches.length == 1) { | |
248 // Exactly one finger inside the canvas => Setup a translation | |
249 that.touchTranslation_ = that.GetTouchTranslation(event); | |
228 | 250 that.ViewportMouseDown(that.pimpl_, |
251 1 /* middle button */, | |
227 | 252 that.touchTranslation_[0], |
253 that.touchTranslation_[1], 0); | |
254 } else if (event.targetTouches.length == 2) { | |
255 // Exactly 2 fingers inside the canvas => Setup a pinch/zoom | |
256 that.touchZoom_ = that.GetTouchZoom(event); | |
257 var z0 = that.touchZoom_; | |
228 | 258 that.ViewportMouseDown(that.pimpl_, |
259 2 /* right button */, | |
227 | 260 z0[0], |
261 z0[1], 0); | |
262 } | |
263 } | |
264 }); | |
265 } | |
226 | 266 |
267 public ResetTouch() { | |
227 | 268 if (this.touchTranslation_ || |
269 this.touchZoom_) { | |
228 | 270 this.ViewportMouseUp(this.pimpl_); |
226 | 271 } |
227 | 272 |
273 this.touchTranslation_ = false; | |
274 this.touchZoom_ = false; | |
275 } | |
226 | 276 |
277 public GetTouchTranslation(event) { | |
227 | 278 var touch = event.targetTouches[0]; |
279 return [ | |
280 touch.pageX, | |
281 touch.pageY | |
282 ]; | |
283 } | |
226 | 284 |
285 public GetTouchZoom(event) { | |
227 | 286 var touch1 = event.targetTouches[0]; |
287 var touch2 = event.targetTouches[1]; | |
288 var dx = (touch1.pageX - touch2.pageX); | |
289 var dy = (touch1.pageY - touch2.pageY); | |
290 var d = Math.sqrt(dx * dx + dy * dy); | |
291 return [ | |
292 (touch1.pageX + touch2.pageX) / 2.0, | |
293 (touch1.pageY + touch2.pageY) / 2.0, | |
294 d | |
295 ]; | |
296 } | |
226 | 297 |
227 | 298 } |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
299 |
226 | 300 |