OLD | NEW |
(Empty) | |
| 1 /* |
| 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. |
| 5 */ |
| 6 |
| 7 /** |
| 8 * @constructor |
| 9 * @extends {WebInspector.Object} |
| 10 * @param {!Element} element |
| 11 */ |
| 12 WebInspector.TransformController = function(element) |
| 13 { |
| 14 this.element = element; |
| 15 element.addEventListener("mousemove", this._onMouseMove.bind(this), false); |
| 16 element.addEventListener("mousedown", this._onMouseDown.bind(this), false); |
| 17 element.addEventListener("mouseup", this._onMouseUp.bind(this), false); |
| 18 element.addEventListener("mousewheel", this._onMouseWheel.bind(this), false)
; |
| 19 this.reset(); |
| 20 } |
| 21 |
| 22 /** |
| 23 * @enum {string} |
| 24 */ |
| 25 WebInspector.TransformController.Events = { |
| 26 TransformChanged: "TransformChanged" |
| 27 } |
| 28 |
| 29 /** |
| 30 * @enum {number} |
| 31 */ |
| 32 WebInspector.TransformController.TransformType = { |
| 33 Offset: 1 << 0, |
| 34 Scale: 1 << 1, |
| 35 Rotation: 1 << 2 |
| 36 } |
| 37 |
| 38 WebInspector.TransformController.prototype = { |
| 39 /** |
| 40 * @param {number} changeType |
| 41 */ |
| 42 _postChangeEvent: function(changeType) |
| 43 { |
| 44 this.dispatchEventToListeners(WebInspector.TransformController.Events.Tr
ansformChanged, changeType); |
| 45 }, |
| 46 |
| 47 /** |
| 48 * @param {?Event} event |
| 49 */ |
| 50 _onMouseMove: function(event) |
| 51 { |
| 52 if (event.which !== 1) |
| 53 return; |
| 54 // Set reference point if we missed mousedown. |
| 55 if (typeof this._originX !== "number") |
| 56 this._setReferencePoint(event); |
| 57 this._rotateX = this._oldRotateX + (this._originY - event.clientY) / 2; |
| 58 this._rotateY = this._oldRotateY - (this._originX - event.clientX) / 4; |
| 59 this._postChangeEvent(WebInspector.TransformController.TransformType.Rot
ation); |
| 60 }, |
| 61 |
| 62 reset: function() |
| 63 { |
| 64 this._scale = 1; |
| 65 this._offsetX = 0; |
| 66 this._offsetY = 0; |
| 67 this._rotateX = 0; |
| 68 this._rotateY = 0; |
| 69 }, |
| 70 |
| 71 /** |
| 72 * @return {number} |
| 73 */ |
| 74 scale: function() |
| 75 { |
| 76 return this._scale; |
| 77 }, |
| 78 |
| 79 /** |
| 80 * @return {number} |
| 81 */ |
| 82 offsetX: function() |
| 83 { |
| 84 return this._offsetX; |
| 85 }, |
| 86 |
| 87 /** |
| 88 * @return {number} |
| 89 */ |
| 90 offsetY: function() |
| 91 { |
| 92 return this._offsetY; |
| 93 }, |
| 94 |
| 95 /** |
| 96 * @return {number} |
| 97 */ |
| 98 rotateX: function() |
| 99 { |
| 100 return this._rotateX; |
| 101 }, |
| 102 |
| 103 /** |
| 104 * @return {number} |
| 105 */ |
| 106 rotateY: function() |
| 107 { |
| 108 return this._rotateY; |
| 109 }, |
| 110 |
| 111 /** |
| 112 * @param {?Event} event |
| 113 */ |
| 114 _onMouseWheel: function(event) |
| 115 { |
| 116 if (event.shiftKey) { |
| 117 const zoomFactor = 1.1; |
| 118 const mouseWheelZoomSpeed = 1 / 120; |
| 119 var scaleFactor = Math.pow(zoomFactor, event.wheelDeltaY * mouseWhee
lZoomSpeed); |
| 120 this._scale *= scaleFactor; |
| 121 this._offsetX -= (event.clientX - this.element.totalOffsetLeft() - t
his._offsetX) * (scaleFactor - 1); |
| 122 this._offsetY -= (event.clientY - this.element.totalOffsetTop() - th
is._offsetY) * (scaleFactor - 1); |
| 123 this._postChangeEvent(WebInspector.TransformController.TransformType
.Scale | WebInspector.TransformController.TransformType.Offset); |
| 124 } else { |
| 125 this._offsetX += event.wheelDeltaX; |
| 126 this._offsetY += event.wheelDeltaY; |
| 127 this._postChangeEvent(WebInspector.TransformController.TransformType
.Offset); |
| 128 } |
| 129 }, |
| 130 |
| 131 /** |
| 132 * @param {?Event} event |
| 133 */ |
| 134 _setReferencePoint: function(event) |
| 135 { |
| 136 this._originX = event.clientX; |
| 137 this._originY = event.clientY; |
| 138 this._oldRotateX = this._rotateX; |
| 139 this._oldRotateY = this._rotateY; |
| 140 }, |
| 141 |
| 142 _resetReferencePoint: function() |
| 143 { |
| 144 delete this._originX; |
| 145 delete this._originY; |
| 146 delete this._oldRotateX; |
| 147 delete this._oldRotateY; |
| 148 }, |
| 149 |
| 150 /** |
| 151 * @param {?Event} event |
| 152 */ |
| 153 _onMouseDown: function(event) |
| 154 { |
| 155 if (event.which !== 1) |
| 156 return; |
| 157 this._setReferencePoint(event); |
| 158 }, |
| 159 |
| 160 /** |
| 161 * @param {?Event} event |
| 162 */ |
| 163 _onMouseUp: function(event) |
| 164 { |
| 165 if (event.which !== 1) |
| 166 return; |
| 167 this._resetReferencePoint(); |
| 168 }, |
| 169 |
| 170 __proto__: WebInspector.Object.prototype |
| 171 } |
OLD | NEW |