annotate Platforms/Wasm/wasm-viewport.ts @ 1313:f30905f5d246 broker

Added support for OnGenericSdlEvent to GuiAdapter. This makes it possible to piggyback app-specific logic to special events (here, to support the Viewport refresh events)
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 16 Mar 2020 11:12:39 +0100
parents 67d0a8da4afe
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
1 import * as wasmApplicationRunner from './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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
4 var isPendingRedraw = false;
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 228
diff changeset
5
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
6 function ScheduleWebViewportRedraw(cppViewportHandle: any) : void
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
7 {
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
8 if (!isPendingRedraw) {
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
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
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
11 window.requestAnimationFrame(function() {
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
12 isPendingRedraw = false;
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
13 let viewport = WasmViewport.GetFromCppViewport(cppViewportHandle);
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
14 if (viewport) {
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
15 viewport.Redraw();
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
16 }
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
17 });
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 228
diff changeset
18 }
230
7d2631320615 wasm-application.js is now in ts
am@osimis.io
parents: 229
diff changeset
19 }
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
20
466
5055031f4a06 - Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents: 420
diff changeset
21 (<any>window).ScheduleWebViewportRedraw = ScheduleWebViewportRedraw;
5055031f4a06 - Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents: 420
diff changeset
22
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
23 declare function UTF8ToString(v: any): string;
231
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 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
26 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
27 var canvasId = UTF8ToString(htmlCanvasId);
466
5055031f4a06 - Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents: 420
diff changeset
28 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
29 webViewport.Initialize();
314
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
30
231
5027cb2feb51 viewport is now part of the Application itself and not global anymore
am@osimis.io
parents: 230
diff changeset
31 return cppViewportHandle;
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
5055031f4a06 - Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents: 420
diff changeset
34 (<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
35
466
5055031f4a06 - Added browserify to build. This allows using require calls for modules that
bgo-osimis
parents: 420
diff changeset
36 export class WasmViewport {
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
37
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
38 private static viewportsMapByCppHandle_ : Map<number, WasmViewport> = new Map<number, WasmViewport>(); // key = the C++ handle
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
39 private static viewportsMapByCanvasId_ : Map<string, WasmViewport> = new Map<string, WasmViewport>(); // key = the canvasId
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 228
diff changeset
40
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
41 private module_ : any;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
42 private canvasId_ : string;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
43 private htmlCanvas_ : HTMLCanvasElement;
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
44 private context_ : CanvasRenderingContext2D | null;
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
45 private imageData_ : any = null;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
46 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
47
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 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
49 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
50 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
51
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
52 private touchZoom_ : any = false;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
53 private touchTranslation_ : any = false;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
54
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
55 private ViewportSetSize : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
56 private ViewportRender : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
57 private ViewportMouseDown : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
58 private ViewportMouseMove : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
59 private ViewportMouseUp : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
60 private ViewportMouseEnter : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
61 private ViewportMouseLeave : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
62 private ViewportMouseWheel : Function;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
63 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
64 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
65 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
66 private ViewportTouchEnd : Function;
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
67
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
68 private pimpl_ : any; // Private pointer to the underlying WebAssembly C++ object
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
69
231
5027cb2feb51 viewport is now part of the Application itself and not global anymore
am@osimis.io
parents: 230
diff changeset
70 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
71
5027cb2feb51 viewport is now part of the Application itself and not global anymore
am@osimis.io
parents: 230
diff changeset
72 this.pimpl_ = cppViewport;
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
73 WasmViewport.viewportsMapByCppHandle_[this.pimpl_] = this;
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
74 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
75
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
76 this.module_ = module;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
77 this.canvasId_ = canvasId;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
78 this.htmlCanvas_ = document.getElementById(this.canvasId_) as HTMLCanvasElement;
244
313903066093 cleanup
am@osimis.io
parents: 236
diff changeset
79 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
80 Logger.defaultLogger.error("Can not create WasmViewport, did not find the canvas whose id is '", this.canvasId_, "'");
244
313903066093 cleanup
am@osimis.io
parents: 236
diff changeset
81 }
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
82 this.context_ = this.htmlCanvas_.getContext('2d');
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
83
234
9afb50d1ac14 configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents: 231
diff changeset
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 this.ViewportMouseWheel = this.module_.cwrap('ViewportMouseWheel', null, [ 'number', 'number', 'number', 'number', 'number' ]);
327
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
92 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
93 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
94 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
95 this.ViewportTouchEnd = this.module_.cwrap('ViewportTouchEnd', null, [ 'number', 'number', 'number', 'number', 'number', 'number', 'number' ]);
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
96 }
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
97
234
9afb50d1ac14 configure WasmViewport size relative to window size through html attributes
am@osimis.io
parents: 231
diff changeset
98 public GetCppViewport() : number {
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
99 return this.pimpl_;
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
100 }
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
101
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
102 public static GetFromCppViewport(cppViewportHandle: number) : WasmViewport | null {
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
103 if (WasmViewport.viewportsMapByCppHandle_[cppViewportHandle] !== undefined) {
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
104 return WasmViewport.viewportsMapByCppHandle_[cppViewportHandle];
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
105 }
526
548eed46f535 introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents: 506
diff changeset
106 Logger.defaultLogger.error("WasmViewport not found !");
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
107 return null;
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
108 }
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
109
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
110 public static GetFromCanvasId(canvasId: string) : WasmViewport | null {
313
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
111 if (WasmViewport.viewportsMapByCanvasId_[canvasId] !== undefined) {
8bdc6112bc2e initial resize of canvas
am@osimis.io
parents: 244
diff changeset
112 return WasmViewport.viewportsMapByCanvasId_[canvasId];
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 228
diff changeset
113 }
526
548eed46f535 introduced a Logger class that displays timing and source (C++/JS)
Alain Mazy <alain@mazy.be>
parents: 506
diff changeset
114 Logger.defaultLogger.error("WasmViewport not found !");
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
115 return null;
229
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 228
diff changeset
116 }
b0ba3b38a23c ScheduleRedraw can handle multiple viewports
am@osimis.io
parents: 228
diff changeset
117
314
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
118 public static ResizeAll() {
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
119 for (let canvasId in WasmViewport.viewportsMapByCanvasId_) {
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
120 WasmViewport.viewportsMapByCanvasId_[canvasId].Resize();
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
121 }
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
122 }
97f16214dc5e cleanup
am@osimis.io
parents: 313
diff changeset
123
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
124 public Redraw() {
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
125 if (this.imageData_ === null ||
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
126 this.renderingBuffer_ === null ||
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
127 this.ViewportRender(this.pimpl_,
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
128 this.imageData_.width,
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
129 this.imageData_.height,
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
130 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
131 Logger.defaultLogger.error('The rendering has failed');
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
132 } else {
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
133 // Create an accessor to the rendering buffer (i.e. create a
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
134 // "window" above the heap of the WASM module), then copy it to
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
135 // the ImageData object
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
136 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
137 this.module_.HEAPU8.buffer,
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
138 this.renderingBuffer_,
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
139 this.imageData_.width * this.imageData_.height * 4));
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
140
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
141 if (this.context_) {
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
142 this.context_.putImageData(this.imageData_, 0, 0);
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
143 }
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
144 }
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
145 }
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
146
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
147 public Resize() {
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
148 if (this.imageData_ != null &&
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
149 (this.imageData_.width != window.innerWidth ||
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
150 this.imageData_.height != window.innerHeight)) {
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
151 this.imageData_ = null;
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
152 }
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
153
420
8bf717c4e497 canvas size is now defined by the parent div size
am@osimis.io
parents: 327
diff changeset
154 // width/height is defined by the parent width/height
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
155 if (this.htmlCanvas_.parentElement) {
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
156 this.htmlCanvas_.width = this.htmlCanvas_.parentElement.offsetWidth;
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
157 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
158
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
159 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
160
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
161 if (this.imageData_ === null && this.context_) {
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
162 this.imageData_ = this.context_.getImageData(0, 0, this.htmlCanvas_.width, this.htmlCanvas_.height);
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
163 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height);
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
164
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
165 if (this.renderingBuffer_ != null) {
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
166 this.module_._free(this.renderingBuffer_);
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
167 }
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
168
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
169 this.renderingBuffer_ = this.module_._malloc(this.imageData_.width * this.imageData_.height * 4);
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
170 } else {
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
171 this.ViewportSetSize(this.pimpl_, this.htmlCanvas_.width, this.htmlCanvas_.height);
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
172 }
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
173
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
174 this.Redraw();
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
175 }
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
176 }
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
177
231
5027cb2feb51 viewport is now part of the Application itself and not global anymore
am@osimis.io
parents: 230
diff changeset
178 public Initialize() {
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
179
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
180 // Force the rendering of the viewport for the first time
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
181 this.Resize();
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
182
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
183 var that : WasmViewport = this;
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
184 // Register an event listener to call the Resize() function
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
185 // each time the window is resized.
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
186 window.addEventListener('resize', function(event) {
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
187 that.Resize();
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
188 }, false);
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
189
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
190 this.htmlCanvas_.addEventListener('contextmenu', function(event) {
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
191 // Prevent right click on the canvas
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
192 event.preventDefault();
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
193 }, false);
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
194
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
195 this.htmlCanvas_.addEventListener('mouseleave', function(event) {
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
196 that.ViewportMouseLeave(that.pimpl_);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
197 });
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
198
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
199 this.htmlCanvas_.addEventListener('mouseenter', function(event) {
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
200 that.ViewportMouseEnter(that.pimpl_);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
201 });
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
202
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
203 this.htmlCanvas_.addEventListener('mousedown', function(event) {
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
204 var x = event.pageX - this.offsetLeft;
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
205 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
206
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
207 that.ViewportMouseDown(that.pimpl_, event.button, x, y, 0 /* TODO detect modifier keys*/);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
208 });
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
209
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
210 this.htmlCanvas_.addEventListener('mousemove', function(event) {
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
211 var x = event.pageX - this.offsetLeft;
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
212 var y = event.pageY - this.offsetTop;
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
213 that.ViewportMouseMove(that.pimpl_, x, y);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
214 });
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
215
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
216 this.htmlCanvas_.addEventListener('mouseup', function(event) {
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
217 that.ViewportMouseUp(that.pimpl_);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
218 });
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
219
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
220 window.addEventListener('keydown', function(event) {
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
221 var keyChar: string | null = event.key;
327
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
222 var keyCode = event.keyCode
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
223 if (keyChar.length == 1) {
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
224 keyCode = 0; // maps to OrthancStone::KeyboardKeys_Generic
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
225 } else {
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
226 keyChar = null;
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
227 }
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
228 // console.log("key: ", keyCode, keyChar);
8716176ff7f0 added support for arrow keys
am@osimis.io
parents: 314
diff changeset
229 that.ViewportKeyPressed(that.pimpl_, keyCode, keyChar, event.shiftKey, event.ctrlKey, event.altKey);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
230 });
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
231
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
232 this.htmlCanvas_.addEventListener('wheel', function(event) {
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
233 var x = event.pageX - this.offsetLeft;
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
234 var y = event.pageY - this.offsetTop;
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
235 that.ViewportMouseWheel(that.pimpl_, event.deltaY, x, y, event.ctrlKey);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
236 event.preventDefault();
545
e1ba16436d59 set events passive again
Alain Mazy <alain@mazy.be>
parents: 544
diff changeset
237 }, {passive: false}); // must not be passive if calling event.preventDefault, ie to cancel scroll or zoom of the whole interface
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
238
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
239 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
240 // 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
241 event.preventDefault();
59b4afa92dee TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents: 420
diff changeset
242 event.stopPropagation();
59b4afa92dee TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents: 420
diff changeset
243
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
244 // 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
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254 }
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 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
256 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
257 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
258 }
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
259 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
260 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
261 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
262 }
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
263
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
264 that.ViewportTouchStart(that.pimpl_, event.targetTouches.length, x0, y0, x1, y1, x2, y2);
545
e1ba16436d59 set events passive again
Alain Mazy <alain@mazy.be>
parents: 544
diff changeset
265 }, {passive: false}); // must not be passive if calling event.preventDefault, ie to cancel scroll or zoom of the whole interface
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
266
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
267 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
268 // 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
269 event.preventDefault();
59b4afa92dee TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents: 420
diff changeset
270 event.stopPropagation();
59b4afa92dee TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents: 420
diff changeset
271
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
272 // 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
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280 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
281 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
282 }
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 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
284 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
285 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
286 }
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
287 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
288 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
289 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
290 }
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
291
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
292 that.ViewportTouchEnd(that.pimpl_, event.targetTouches.length, x0, y0, x1, y1, x2, y2);
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
293 });
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
294
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
295 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
296
59b4afa92dee TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents: 420
diff changeset
297 // 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
298 event.preventDefault();
59b4afa92dee TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents: 420
diff changeset
299 event.stopPropagation();
59b4afa92dee TS: stop propagating touch events to the whole html doc
Alain Mazy <alain@mazy.be>
parents: 420
diff changeset
300
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
301
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 // 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
303 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
304 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 }
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 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
314 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
315 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
316 }
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 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
318 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
319 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
320 }
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
321
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
322 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
323 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
324
545
e1ba16436d59 set events passive again
Alain Mazy <alain@mazy.be>
parents: 544
diff changeset
325 }, {passive: false}); // must not be passive if calling event.preventDefault, ie to cancel scroll or zoom of the whole interface
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
326 }
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
327
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
328 public ResetTouch() {
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
329 if (this.touchTranslation_ ||
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
330 this.touchZoom_) {
228
210c1ce8e1a6 WasmViewport is no more a singleton
am@osimis.io
parents: 227
diff changeset
331 this.ViewportMouseUp(this.pimpl_);
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
332 }
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
333
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
334 this.touchTranslation_ = false;
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
335 this.touchZoom_ = false;
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
336 }
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
337
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
338 public GetTouchTranslation(event: any) {
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
339 var touch = event.targetTouches[0];
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
340 return [
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
341 touch.pageX,
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
342 touch.pageY
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
343 ];
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
344 }
226
1fa4c65c7e1b WasmViewport in ts
am@osimis.io
parents:
diff changeset
345
785
67d0a8da4afe made typescript more future-proof (not completed, I finally disabled a few warnings in my tsconfig.json)
Alain Mazy <alain@mazy.be>
parents: 558
diff changeset
346 public GetTouchZoom(event: any) {
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
347 var touch1 = event.targetTouches[0];
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
348 var touch2 = event.targetTouches[1];
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
349 var dx = (touch1.pageX - touch2.pageX);
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
350 var dy = (touch1.pageY - touch2.pageY);
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
351 var d = Math.sqrt(dx * dx + dy * dy);
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
352 return [
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
353 (touch1.pageX + touch2.pageX) / 2.0,
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
354 (touch1.pageY + touch2.pageY) / 2.0,
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
355 d
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
356 ];
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
357 }
545
e1ba16436d59 set events passive again
Alain Mazy <alain@mazy.be>
parents: 544
diff changeset
358
227
c8f11437a6fd getting ready for multiple viewports
am@osimis.io
parents: 226
diff changeset
359 }