annotate Platforms/Wasm/wasm-viewport.ts @ 242:092db46c6291 am

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