2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
9 * @extends {WebInspector.Object}
10 * @param {!Element} element
11 * @param {boolean=} disableRotate
13 WebInspector.TransformController = function(element, disableRotate)
16 this.element = element;
17 this._registerShortcuts();
18 element.addEventListener("keydown", this._onKeyDown.bind(this), false);
19 element.addEventListener("mousemove", this._onMouseMove.bind(this), false);
20 element.addEventListener("mousedown", this._onMouseDown.bind(this), false);
21 element.addEventListener("mouseup", this._onMouseUp.bind(this), false);
22 element.addEventListener("mousewheel", this._onMouseWheel.bind(this), false);
23 this._disableRotate = disableRotate;
30 WebInspector.TransformController.Events = {
31 TransformChanged: "TransformChanged"
34 WebInspector.TransformController.prototype = {
35 _onKeyDown: function(event)
37 var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
38 var handler = this._shortcuts[shortcutKey];
39 event.handled = handler && handler(event);
42 _addShortcuts: function(keys, handler)
44 for (var i = 0; i < keys.length; ++i)
45 this._shortcuts[keys[i].key] = handler;
48 _registerShortcuts: function()
50 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.ResetView, this.resetAndNotify.bind(this));
52 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.ZoomIn, this._onKeyboardZoom.bind(this, zoomFactor));
53 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.ZoomOut, this._onKeyboardZoom.bind(this, 1 / zoomFactor));
54 var panDistanceInPixels = 6;
55 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.PanUp, this._onPan.bind(this, 0, -panDistanceInPixels));
56 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.PanDown, this._onPan.bind(this, 0, panDistanceInPixels));
57 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.PanLeft, this._onPan.bind(this, -panDistanceInPixels, 0));
58 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.PanRight, this._onPan.bind(this, panDistanceInPixels, 0));
59 var rotateDegrees = 5;
60 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.RotateCWX, this._onKeyboardRotate.bind(this, rotateDegrees, 0));
61 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.RotateCCWX, this._onKeyboardRotate.bind(this, -rotateDegrees, 0));
62 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.RotateCWY, this._onKeyboardRotate.bind(this, 0, -rotateDegrees));
63 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.RotateCCWY, this._onKeyboardRotate.bind(this, 0, rotateDegrees));
66 _postChangeEvent: function()
68 this.dispatchEventToListeners(WebInspector.TransformController.Events.TransformChanged);
81 * @param {!Event=} event
83 resetAndNotify: function(event)
86 this._postChangeEvent();
88 event.preventDefault();
104 return this._offsetX;
112 return this._offsetY;
120 return this._rotateX;
128 return this._rotateY;
132 * @param {number} scaleFactor
136 _onScale: function(scaleFactor, x, y)
138 this._scale *= scaleFactor;
139 this._offsetX -= (x - this._offsetX) * (scaleFactor - 1);
140 this._offsetY -= (y - this._offsetY) * (scaleFactor - 1);
141 this._postChangeEvent();
145 * @param {number} offsetX
146 * @param {number} offsetY
148 _onPan: function(offsetX, offsetY)
150 this._offsetX += offsetX;
151 this._offsetY += offsetY;
152 this._postChangeEvent();
156 * @param {number} rotateX
157 * @param {number} rotateY
159 _onRotate: function(rotateX, rotateY)
161 this._rotateX = rotateX;
162 this._rotateY = rotateY;
163 this._postChangeEvent();
167 * @param {number} zoomFactor
169 _onKeyboardZoom: function(zoomFactor)
171 this._onScale(zoomFactor, this.element.clientWidth / 2, this.element.clientHeight / 2);
175 * @param {number} rotateX
176 * @param {number} rotateY
178 _onKeyboardRotate: function(rotateX, rotateY)
180 this._onRotate(this._rotateX + rotateX, this._rotateY + rotateY);
184 * @param {!Event} event
186 _onMouseWheel: function(event)
189 var zoomFactor = 1.1;
191 var mouseWheelZoomSpeed = 1 / 120;
192 var scaleFactor = Math.pow(zoomFactor, event.wheelDeltaY * mouseWheelZoomSpeed);
193 this._onScale(scaleFactor, event.clientX - this.element.totalOffsetLeft(), event.clientY - this.element.totalOffsetTop());
197 * @param {!Event} event
199 _onMouseMove: function(event)
201 if (event.which !== 1 || typeof this._originX !== "number")
203 if (event.shiftKey) {
204 this._onRotate(this._oldRotateX + (this._originY - event.clientY) / this.element.clientHeight * 180, this._oldRotateY - (this._originX - event.clientX) / this.element.clientWidth * 180);
206 this._onPan(event.clientX - this._originX, event.clientY - this._originY);
207 this._originX = event.clientX;
208 this._originY = event.clientY;
213 * @param {!Event} event
215 _setReferencePoint: function(event)
217 this._originX = event.clientX;
218 this._originY = event.clientY;
219 this._oldRotateX = this._rotateX;
220 this._oldRotateY = this._rotateY;
223 _resetReferencePoint: function()
225 delete this._originX;
226 delete this._originY;
227 delete this._oldRotateX;
228 delete this._oldRotateY;
232 * @param {!Event} event
234 _onMouseDown: function(event)
236 if (event.which !== 1)
238 this._setReferencePoint(event);
242 * @param {!Event} event
244 _onMouseUp: function(event)
246 if (event.which !== 1)
248 this._resetReferencePoint();
251 __proto__: WebInspector.Object.prototype