Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/wasm-viewport.ts @ 252:40b21c1f8b8d am-2
more usage of IObservable/IObserver
author | am@osimis.io |
---|---|
date | Tue, 03 Jul 2018 10:26:56 +0200 |
parents | 313903066093 |
children | 8bdc6112bc2e |
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; | |
244 | 68 if (this.htmlCanvas_ == null) { |
69 console.log("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'"); | |
70 } | |
226 | 71 this.context_ = this.htmlCanvas_.getContext('2d'); |
72 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 this.ViewportKeyPressed = this.module_.cwrap('ViewportKeyPressed', null, [ 'number', 'string', 'number', 'number' ]); |
228 | 82 } |
83 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
84 public GetCppViewport() : number { |
228 | 85 return this.pimpl_; |
226 | 86 } |
87 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
88 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport { |
229 | 89 if (WasmViewport.cppWebViewportsMaps_[cppViewportHandle] !== undefined) { |
90 return WasmViewport.cppWebViewportsMaps_[cppViewportHandle]; | |
91 } | |
92 console.log("WasmViewport not found !"); | |
93 return undefined; | |
94 } | |
95 | |
226 | 96 public Redraw() { |
97 if (this.imageData_ === null || | |
98 this.renderingBuffer_ === null || | |
227 | 99 this.ViewportRender(this.pimpl_, |
100 this.imageData_.width, | |
226 | 101 this.imageData_.height, |
102 this.renderingBuffer_) == 0) { | |
103 console.log('The rendering has failed'); | |
104 } else { | |
105 // Create an accessor to the rendering buffer (i.e. create a | |
106 // "window" above the heap of the WASM module), then copy it to | |
107 // the ImageData object | |
108 this.imageData_.data.set(new Uint8ClampedArray( | |
109 this.module_.buffer, | |
110 this.renderingBuffer_, | |
111 this.imageData_.width * this.imageData_.height * 4)); | |
112 | |
113 this.context_.putImageData(this.imageData_, 0, 0); | |
114 } | |
115 } | |
116 | |
117 public Resize() { | |
118 if (this.imageData_ != null && | |
119 (this.imageData_.width != window.innerWidth || | |
120 this.imageData_.height != window.innerHeight)) { | |
121 this.imageData_ = null; | |
122 } | |
123 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
124 // 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
|
125 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
|
126 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
|
127 |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
128 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
|
129 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
|
130 |
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
131 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
|
132 |
226 | 133 if (this.imageData_ === null) { |
134 this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height); | |
228 | 135 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); |
226 | 136 |
137 if (this.renderingBuffer_ != null) { | |
138 this.module_._free(this.renderingBuffer_); | |
139 } | |
140 | |
141 this.renderingBuffer_ = this.module_._malloc(this.imageData_.width * this.imageData_.height * 4); | |
142 } | |
143 | |
144 this.Redraw(); | |
145 } | |
146 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
147 public Initialize() { |
227 | 148 |
149 // Force the rendering of the viewport for the first time | |
150 this.Resize(); | |
226 | 151 |
228 | 152 var that : WasmViewport = this; |
227 | 153 // Register an event listener to call the Resize() function |
154 // each time the window is resized. | |
228 | 155 window.addEventListener('resize', function(event) { |
156 that.Resize(); | |
157 }, false); | |
226 | 158 |
227 | 159 this.htmlCanvas_.addEventListener('contextmenu', function(event) { |
160 // Prevent right click on the canvas | |
161 event.preventDefault(); | |
162 }, false); | |
163 | |
164 this.htmlCanvas_.addEventListener('mouseleave', function(event) { | |
228 | 165 that.ViewportMouseLeave(that.pimpl_); |
227 | 166 }); |
167 | |
168 this.htmlCanvas_.addEventListener('mouseenter', function(event) { | |
228 | 169 that.ViewportMouseEnter(that.pimpl_); |
227 | 170 }); |
171 | |
172 this.htmlCanvas_.addEventListener('mousedown', function(event) { | |
173 var x = event.pageX - this.offsetLeft; | |
174 var y = event.pageY - this.offsetTop; | |
228 | 175 that.ViewportMouseDown(that.pimpl_, event.button, x, y, 0 /* TODO */); |
227 | 176 }); |
177 | |
178 this.htmlCanvas_.addEventListener('mousemove', function(event) { | |
179 var x = event.pageX - this.offsetLeft; | |
180 var y = event.pageY - this.offsetTop; | |
228 | 181 that.ViewportMouseMove(that.pimpl_, x, y); |
227 | 182 }); |
183 | |
184 this.htmlCanvas_.addEventListener('mouseup', function(event) { | |
228 | 185 that.ViewportMouseUp(that.pimpl_); |
227 | 186 }); |
187 | |
188 window.addEventListener('keydown', function(event) { | |
228 | 189 that.ViewportKeyPressed(that.pimpl_, event.key, event.shiftKey, event.ctrlKey, event.altKey); |
227 | 190 }); |
226 | 191 |
227 | 192 this.htmlCanvas_.addEventListener('wheel', function(event) { |
193 var x = event.pageX - this.offsetLeft; | |
194 var y = event.pageY - this.offsetTop; | |
228 | 195 that.ViewportMouseWheel(that.pimpl_, event.deltaY, x, y, event.ctrlKey); |
227 | 196 event.preventDefault(); |
197 }); | |
198 | |
199 this.htmlCanvas_.addEventListener('touchstart', function(event) { | |
200 that.ResetTouch(); | |
201 }); | |
202 | |
203 this.htmlCanvas_.addEventListener('touchend', function(event) { | |
204 that.ResetTouch(); | |
205 }); | |
206 | |
207 this.htmlCanvas_.addEventListener('touchmove', function(event) { | |
208 if (that.touchTranslation_.length == 2) { | |
209 var t = that.GetTouchTranslation(event); | |
228 | 210 that.ViewportMouseMove(that.pimpl_, t[0], t[1]); |
227 | 211 } |
212 else if (that.touchZoom_.length == 3) { | |
226 | 213 var z0 = that.touchZoom_; |
227 | 214 var z1 = that.GetTouchZoom(event); |
228 | 215 that.ViewportMouseMove(that.pimpl_, z0[0], z0[1] - z0[2] + z1[2]); |
227 | 216 } |
217 else { | |
218 // Realize the gesture event | |
219 if (event.targetTouches.length == 1) { | |
220 // Exactly one finger inside the canvas => Setup a translation | |
221 that.touchTranslation_ = that.GetTouchTranslation(event); | |
228 | 222 that.ViewportMouseDown(that.pimpl_, |
223 1 /* middle button */, | |
227 | 224 that.touchTranslation_[0], |
225 that.touchTranslation_[1], 0); | |
226 } else if (event.targetTouches.length == 2) { | |
227 // Exactly 2 fingers inside the canvas => Setup a pinch/zoom | |
228 that.touchZoom_ = that.GetTouchZoom(event); | |
229 var z0 = that.touchZoom_; | |
228 | 230 that.ViewportMouseDown(that.pimpl_, |
231 2 /* right button */, | |
227 | 232 z0[0], |
233 z0[1], 0); | |
234 } | |
235 } | |
236 }); | |
237 } | |
226 | 238 |
239 public ResetTouch() { | |
227 | 240 if (this.touchTranslation_ || |
241 this.touchZoom_) { | |
228 | 242 this.ViewportMouseUp(this.pimpl_); |
226 | 243 } |
227 | 244 |
245 this.touchTranslation_ = false; | |
246 this.touchZoom_ = false; | |
247 } | |
226 | 248 |
249 public GetTouchTranslation(event) { | |
227 | 250 var touch = event.targetTouches[0]; |
251 return [ | |
252 touch.pageX, | |
253 touch.pageY | |
254 ]; | |
255 } | |
226 | 256 |
257 public GetTouchZoom(event) { | |
227 | 258 var touch1 = event.targetTouches[0]; |
259 var touch2 = event.targetTouches[1]; | |
260 var dx = (touch1.pageX - touch2.pageX); | |
261 var dy = (touch1.pageY - touch2.pageY); | |
262 var d = Math.sqrt(dx * dx + dy * dy); | |
263 return [ | |
264 (touch1.pageX + touch2.pageX) / 2.0, | |
265 (touch1.pageY + touch2.pageY) / 2.0, | |
266 d | |
267 ]; | |
268 } | |
226 | 269 |
227 | 270 } |
226 | 271 } |
272 |