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