Mercurial > hg > orthanc-stone
annotate Platforms/Wasm/wasm-viewport.ts @ 894:f557b18d287f
wasm: error log if canvas GL context can't be created
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 16 Jul 2019 10:51:03 +0200 |
parents | d5579bdc59b5 |
children | 67d0a8da4afe |
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'); |
526
548eed46f535
introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents:
506
diff
changeset
|
2 import * as Logger from './logger' |
466
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; | |
526
548eed46f535
introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents:
506
diff
changeset
|
10 Logger.defaultLogger.debug('Scheduling a refresh of the viewport, as its content changed'); |
230 | 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 |
466
5055031f4a06
- Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents:
420
diff
changeset
|
33 export class WasmViewport { |
226 | 34 |
313 | 35 private static viewportsMapByCppHandle_ : Map<number, WasmViewport> = new Map<number, WasmViewport>(); // key = the C++ handle |
36 private static viewportsMapByCanvasId_ : Map<string, WasmViewport> = new Map<string, WasmViewport>(); // key = the canvasId | |
229 | 37 |
226 | 38 private module_ : any; |
39 private canvasId_ : string; | |
40 private htmlCanvas_ : HTMLCanvasElement; | |
41 private context_ : CanvasRenderingContext2D; | |
42 private imageData_ : any = null; | |
43 private renderingBuffer_ : any = null; | |
454
50229f6eb4cd
TS: one touch drag is not equivalent to left mouse drag instead of middle mouse drag
Alain Mazy <alain@mazy.be>
parents:
453
diff
changeset
|
44 |
50229f6eb4cd
TS: one touch drag is not equivalent to left mouse drag instead of middle mouse drag
Alain Mazy <alain@mazy.be>
parents:
453
diff
changeset
|
45 private touchGestureInProgress_: boolean = false; |
50229f6eb4cd
TS: one touch drag is not equivalent to left mouse drag instead of middle mouse drag
Alain Mazy <alain@mazy.be>
parents:
453
diff
changeset
|
46 private touchCount_: number = 0; |
50229f6eb4cd
TS: one touch drag is not equivalent to left mouse drag instead of middle mouse drag
Alain Mazy <alain@mazy.be>
parents:
453
diff
changeset
|
47 private touchGestureLastCoordinates_: [number, number][] = []; // last x,y coordinates of each touch |
50229f6eb4cd
TS: one touch drag is not equivalent to left mouse drag instead of middle mouse drag
Alain Mazy <alain@mazy.be>
parents:
453
diff
changeset
|
48 |
226 | 49 private touchZoom_ : any = false; |
50 private touchTranslation_ : any = false; | |
51 | |
52 private ViewportSetSize : Function; | |
53 private ViewportRender : Function; | |
54 private ViewportMouseDown : Function; | |
55 private ViewportMouseMove : Function; | |
56 private ViewportMouseUp : Function; | |
57 private ViewportMouseEnter : Function; | |
58 private ViewportMouseLeave : Function; | |
59 private ViewportMouseWheel : Function; | |
60 private ViewportKeyPressed : Function; | |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
61 private ViewportTouchStart : Function; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
62 private ViewportTouchMove : Function; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
63 private ViewportTouchEnd : Function; |
226 | 64 |
227 | 65 private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object |
66 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
67 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
|
68 |
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
69 this.pimpl_ = cppViewport; |
313 | 70 WasmViewport.viewportsMapByCppHandle_[this.pimpl_] = this; |
71 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
|
72 |
226 | 73 this.module_ = module; |
74 this.canvasId_ = canvasId; | |
75 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement; | |
244 | 76 if (this.htmlCanvas_ == null) { |
526
548eed46f535
introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents:
506
diff
changeset
|
77 Logger.defaultLogger.error("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'"); |
244 | 78 } |
226 | 79 this.context_ = this.htmlCanvas_.getContext('2d'); |
80 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 this.ViewportMouseWheel = this.module_.cwrap('ViewportMouseWheel', null, [ 'number', 'number', 'number', 'number', 'number' ]); |
327 | 89 this.ViewportKeyPressed = this.module_.cwrap('ViewportKeyPressed', null, [ 'number', 'number', 'string', 'number', 'number' ]); |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
90 this.ViewportTouchStart = this.module_.cwrap('ViewportTouchStart', null, [ 'number', 'number', 'number', 'number', 'number', 'number', 'number' ]); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
91 this.ViewportTouchMove = this.module_.cwrap('ViewportTouchMove', null, [ 'number', 'number', 'number', 'number', 'number', 'number', 'number' ]); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
92 this.ViewportTouchEnd = this.module_.cwrap('ViewportTouchEnd', null, [ 'number', 'number', 'number', 'number', 'number', 'number', 'number' ]); |
228 | 93 } |
94 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
95 public GetCppViewport() : number { |
228 | 96 return this.pimpl_; |
226 | 97 } |
98 | |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
99 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport { |
313 | 100 if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) { |
101 return WasmViewport.viewportsMapByCppHandle_[cppViewportHandle]; | |
102 } | |
526
548eed46f535
introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents:
506
diff
changeset
|
103 Logger.defaultLogger.error("WasmViewport not found !"); |
313 | 104 return undefined; |
105 } | |
106 | |
107 public static GetFromCanvasId(canvasId: string) : WasmViewport { | |
108 if (WasmViewport.viewportsMapByCanvasId_[canvasId] !== undefined) { | |
109 return WasmViewport.viewportsMapByCanvasId_[canvasId]; | |
229 | 110 } |
526
548eed46f535
introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents:
506
diff
changeset
|
111 Logger.defaultLogger.error("WasmViewport not found !"); |
229 | 112 return undefined; |
113 } | |
114 | |
314 | 115 public static ResizeAll() { |
116 for (let canvasId in WasmViewport.viewportsMapByCanvasId_) { | |
117 WasmViewport.viewportsMapByCanvasId_[canvasId].Resize(); | |
118 } | |
119 } | |
120 | |
226 | 121 public Redraw() { |
122 if (this.imageData_ === null || | |
123 this.renderingBuffer_ === null || | |
227 | 124 this.ViewportRender(this.pimpl_, |
125 this.imageData_.width, | |
226 | 126 this.imageData_.height, |
127 this.renderingBuffer_) == 0) { | |
526
548eed46f535
introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents:
506
diff
changeset
|
128 Logger.defaultLogger.error('The rendering has failed'); |
226 | 129 } else { |
130 // Create an accessor to the rendering buffer (i.e. create a | |
131 // "window" above the heap of the WASM module), then copy it to | |
132 // the ImageData object | |
133 this.imageData_.data.set(new Uint8ClampedArray( | |
558
d5579bdc59b5
Fixed wasm viewport Redraw() to use wasmModule.HEAPU8.buffer instead of the deprecated wasmModule.buffer
Benjamin Golinvaux <bgo@osimis.io>
parents:
545
diff
changeset
|
134 this.module_.HEAPU8.buffer, |
226 | 135 this.renderingBuffer_, |
136 this.imageData_.width * this.imageData_.height * 4)); | |
137 | |
138 this.context_.putImageData(this.imageData_, 0, 0); | |
139 } | |
140 } | |
141 | |
142 public Resize() { | |
143 if (this.imageData_ != null && | |
144 (this.imageData_.width != window.innerWidth || | |
145 this.imageData_.height != window.innerHeight)) { | |
146 this.imageData_ = null; | |
147 } | |
148 | |
420
8bf717c4e497
canvas size is now defined by the parent div size
am@osimis.io
parents:
327
diff
changeset
|
149 // 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
|
150 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
|
151 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
|
152 |
526
548eed46f535
introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents:
506
diff
changeset
|
153 Logger.defaultLogger.debug("resizing WasmViewport: ", this.htmlCanvas_.width, "x", this.htmlCanvas_.height); |
234
9afb50d1ac14
configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents:
231
diff
changeset
|
154 |
226 | 155 if (this.imageData_ === null) { |
156 this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height); | |
228 | 157 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); |
226 | 158 |
159 if (this.renderingBuffer_ != null) { | |
160 this.module_._free(this.renderingBuffer_); | |
161 } | |
162 | |
163 this.renderingBuffer_ = this.module_._malloc(this.imageData_.width * this.imageData_.height * 4); | |
313 | 164 } else { |
165 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height); | |
226 | 166 } |
167 | |
168 this.Redraw(); | |
169 } | |
170 | |
231
5027cb2feb51
viewport is now part of the Application itself and not global anymore
am@osimis.io
parents:
230
diff
changeset
|
171 public Initialize() { |
227 | 172 |
173 // Force the rendering of the viewport for the first time | |
174 this.Resize(); | |
226 | 175 |
228 | 176 var that : WasmViewport = this; |
227 | 177 // Register an event listener to call the Resize() function |
178 // each time the window is resized. | |
228 | 179 window.addEventListener('resize', function(event) { |
180 that.Resize(); | |
181 }, false); | |
226 | 182 |
227 | 183 this.htmlCanvas_.addEventListener('contextmenu', function(event) { |
184 // Prevent right click on the canvas | |
185 event.preventDefault(); | |
186 }, false); | |
187 | |
188 this.htmlCanvas_.addEventListener('mouseleave', function(event) { | |
228 | 189 that.ViewportMouseLeave(that.pimpl_); |
227 | 190 }); |
191 | |
192 this.htmlCanvas_.addEventListener('mouseenter', function(event) { | |
228 | 193 that.ViewportMouseEnter(that.pimpl_); |
227 | 194 }); |
195 | |
196 this.htmlCanvas_.addEventListener('mousedown', function(event) { | |
197 var x = event.pageX - this.offsetLeft; | |
198 var y = event.pageY - this.offsetTop; | |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
199 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
200 that.ViewportMouseDown(that.pimpl_, event.button, x, y, 0 /* TODO detect modifier keys*/); |
227 | 201 }); |
202 | |
203 this.htmlCanvas_.addEventListener('mousemove', function(event) { | |
204 var x = event.pageX - this.offsetLeft; | |
205 var y = event.pageY - this.offsetTop; | |
228 | 206 that.ViewportMouseMove(that.pimpl_, x, y); |
227 | 207 }); |
208 | |
209 this.htmlCanvas_.addEventListener('mouseup', function(event) { | |
228 | 210 that.ViewportMouseUp(that.pimpl_); |
227 | 211 }); |
212 | |
213 window.addEventListener('keydown', function(event) { | |
327 | 214 var keyChar = event.key; |
215 var keyCode = event.keyCode | |
216 if (keyChar.length == 1) { | |
217 keyCode = 0; // maps to OrthancStone::KeyboardKeys_Generic | |
218 } else { | |
219 keyChar = null; | |
220 } | |
221 // console.log("key: ", keyCode, keyChar); | |
222 that.ViewportKeyPressed(that.pimpl_, keyCode, keyChar, event.shiftKey, event.ctrlKey, event.altKey); | |
227 | 223 }); |
226 | 224 |
227 | 225 this.htmlCanvas_.addEventListener('wheel', function(event) { |
226 var x = event.pageX - this.offsetLeft; | |
227 var y = event.pageY - this.offsetTop; | |
228 | 228 that.ViewportMouseWheel(that.pimpl_, event.deltaY, x, y, event.ctrlKey); |
227 | 229 event.preventDefault(); |
545 | 230 }, {passive: false}); // must not be passive if calling event.preventDefault, ie to cancel scroll or zoom of the whole interface |
227 | 231 |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
232 this.htmlCanvas_.addEventListener('touchstart', function(event: TouchEvent) { |
453
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
233 // don't propagate events to the whole body (this could zoom the entire page instead of zooming the viewport) |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
234 event.preventDefault(); |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
235 event.stopPropagation(); |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
236 |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
237 // TODO: find a way to pass the coordinates as an array between JS and C++ |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
238 var x0 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
239 var y0 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
240 var x1 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
241 var y1 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
242 var x2 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
243 var y2 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
244 if (event.targetTouches.length > 0) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
245 x0 = event.targetTouches[0].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
246 y0 = event.targetTouches[0].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
247 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
248 if (event.targetTouches.length > 1) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
249 x1 = event.targetTouches[1].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
250 y1 = event.targetTouches[1].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
251 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
252 if (event.targetTouches.length > 2) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
253 x2 = event.targetTouches[2].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
254 y2 = event.targetTouches[2].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
255 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
256 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
257 that.ViewportTouchStart(that.pimpl_, event.targetTouches.length, x0, y0, x1, y1, x2, y2); |
545 | 258 }, {passive: false}); // must not be passive if calling event.preventDefault, ie to cancel scroll or zoom of the whole interface |
227 | 259 |
260 this.htmlCanvas_.addEventListener('touchend', function(event) { | |
453
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
261 // don't propagate events to the whole body (this could zoom the entire page instead of zooming the viewport) |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
262 event.preventDefault(); |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
263 event.stopPropagation(); |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
264 |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
265 // TODO: find a way to pass the coordinates as an array between JS and C++ |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
266 var x0 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
267 var y0 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
268 var x1 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
269 var y1 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
270 var x2 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
271 var y2 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
272 if (event.targetTouches.length > 0) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
273 x0 = event.targetTouches[0].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
274 y0 = event.targetTouches[0].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
275 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
276 if (event.targetTouches.length > 1) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
277 x1 = event.targetTouches[1].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
278 y1 = event.targetTouches[1].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
279 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
280 if (event.targetTouches.length > 2) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
281 x2 = event.targetTouches[2].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
282 y2 = event.targetTouches[2].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
283 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
284 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
285 that.ViewportTouchEnd(that.pimpl_, event.targetTouches.length, x0, y0, x1, y1, x2, y2); |
227 | 286 }); |
287 | |
454
50229f6eb4cd
TS: one touch drag is not equivalent to left mouse drag instead of middle mouse drag
Alain Mazy <alain@mazy.be>
parents:
453
diff
changeset
|
288 this.htmlCanvas_.addEventListener('touchmove', function(event: TouchEvent) { |
453
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
289 |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
290 // don't propagate events to the whole body (this could zoom the entire page instead of zooming the viewport) |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
291 event.preventDefault(); |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
292 event.stopPropagation(); |
59b4afa92dee
TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents:
420
diff
changeset
|
293 |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
294 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
295 // TODO: find a way to pass the coordinates as an array between JS and C++ |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
296 var x0 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
297 var y0 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
298 var x1 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
299 var y1 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
300 var x2 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
301 var y2 = 0; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
302 if (event.targetTouches.length > 0) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
303 x0 = event.targetTouches[0].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
304 y0 = event.targetTouches[0].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
305 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
306 if (event.targetTouches.length > 1) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
307 x1 = event.targetTouches[1].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
308 y1 = event.targetTouches[1].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
309 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
310 if (event.targetTouches.length > 2) { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
311 x2 = event.targetTouches[2].pageX; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
312 y2 = event.targetTouches[2].pageY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
313 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
314 |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
315 that.ViewportTouchMove(that.pimpl_, event.targetTouches.length, x0, y0, x1, y1, x2, y2); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
316 return; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
454
diff
changeset
|
317 |
545 | 318 }, {passive: false}); // must not be passive if calling event.preventDefault, ie to cancel scroll or zoom of the whole interface |
227 | 319 } |
226 | 320 |
321 public ResetTouch() { | |
227 | 322 if (this.touchTranslation_ || |
323 this.touchZoom_) { | |
228 | 324 this.ViewportMouseUp(this.pimpl_); |
226 | 325 } |
227 | 326 |
327 this.touchTranslation_ = false; | |
328 this.touchZoom_ = false; | |
329 } | |
226 | 330 |
331 public GetTouchTranslation(event) { | |
227 | 332 var touch = event.targetTouches[0]; |
333 return [ | |
334 touch.pageX, | |
335 touch.pageY | |
336 ]; | |
337 } | |
226 | 338 |
339 public GetTouchZoom(event) { | |
227 | 340 var touch1 = event.targetTouches[0]; |
341 var touch2 = event.targetTouches[1]; | |
342 var dx = (touch1.pageX - touch2.pageX); | |
343 var dy = (touch1.pageY - touch2.pageY); | |
344 var d = Math.sqrt(dx * dx + dy * dy); | |
345 return [ | |
346 (touch1.pageX + touch2.pageX) / 2.0, | |
347 (touch1.pageY + touch2.pageY) / 2.0, | |
348 d | |
349 ]; | |
350 } | |
545 | 351 |
227 | 352 } |