|
14 | 14 | this.ellipsoid = new BABYLON.Vector3(0.5, 1, 0.5); |
15 | 15 |
|
16 | 16 | this._keys = []; |
17 | | - this.keysUp = [38, 87]; |
| 17 | + this.keysUp = [38]; |
18 | 18 | this.keysDown = [40]; |
19 | | - this.keysLeft = [37, 81]; |
20 | | - this.keysRight = [39, 68]; |
| 19 | + this.keysLeft = [37]; |
| 20 | + this.keysRight = [39]; |
21 | 21 |
|
22 | 22 | if (!scene.activeCamera) { |
23 | 23 | scene.activeCamera = this; |
|
88 | 88 | var that = this; |
89 | 89 | var engine = this._scene.getEngine(); |
90 | 90 |
|
91 | | - this._onMouseDown = function (evt) { |
92 | | - previousPosition = { |
93 | | - x: evt.clientX, |
94 | | - y: evt.clientY |
| 91 | + if (this._onMouseDown === undefined) { |
| 92 | + this._onMouseDown = function (evt) { |
| 93 | + previousPosition = { |
| 94 | + x: evt.clientX, |
| 95 | + y: evt.clientY |
| 96 | + }; |
| 97 | + |
| 98 | + evt.preventDefault(); |
95 | 99 | }; |
96 | 100 |
|
97 | | - evt.preventDefault(); |
98 | | - }; |
| 101 | + this._onMouseUp = function (evt) { |
| 102 | + previousPosition = null; |
| 103 | + evt.preventDefault(); |
| 104 | + }; |
99 | 105 |
|
100 | | - this._onMouseUp = function (evt) { |
101 | | - previousPosition = null; |
102 | | - evt.preventDefault(); |
103 | | - }; |
| 106 | + this._onMouseOut = function (evt) { |
| 107 | + previousPosition = null; |
| 108 | + that._keys = []; |
| 109 | + evt.preventDefault(); |
| 110 | + }; |
104 | 111 |
|
105 | | - this._onMouseOut = function (evt) { |
106 | | - previousPosition = null; |
107 | | - that._keys = []; |
108 | | - evt.preventDefault(); |
109 | | - }; |
| 112 | + this._onMouseMove = function (evt) { |
| 113 | + if (!previousPosition && !engine.isPointerLock) { |
| 114 | + return; |
| 115 | + } |
110 | 116 |
|
111 | | - this._onMouseMove = function (evt) { |
112 | | - if (!previousPosition && !engine.isPointerLock) { |
113 | | - return; |
114 | | - } |
| 117 | + var offsetX; |
| 118 | + var offsetY; |
115 | 119 |
|
116 | | - var offsetX; |
117 | | - var offsetY; |
| 120 | + if (!engine.isPointerLock) { |
| 121 | + offsetX = evt.clientX - previousPosition.x; |
| 122 | + offsetY = evt.clientY - previousPosition.y; |
| 123 | + } else { |
| 124 | + offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0; |
| 125 | + offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0; |
| 126 | + } |
118 | 127 |
|
119 | | - if (!engine.isPointerLock) { |
120 | | - offsetX = evt.clientX - previousPosition.x; |
121 | | - offsetY = evt.clientY - previousPosition.y; |
122 | | - } else { |
123 | | - offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0; |
124 | | - offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0; |
125 | | - } |
| 128 | + that.cameraRotation.y += offsetX / 2000.0; |
| 129 | + that.cameraRotation.x += offsetY / 2000.0; |
126 | 130 |
|
127 | | - that.cameraRotation.y += offsetX / 2000.0; |
128 | | - that.cameraRotation.x += offsetY / 2000.0; |
| 131 | + previousPosition = { |
| 132 | + x: evt.clientX, |
| 133 | + y: evt.clientY |
| 134 | + }; |
| 135 | + evt.preventDefault(); |
| 136 | + }; |
129 | 137 |
|
130 | | - previousPosition = { |
131 | | - x: evt.clientX, |
132 | | - y: evt.clientY |
| 138 | + this._onKeyDown = function (evt) { |
| 139 | + if (that.keysUp.indexOf(evt.keyCode) !== -1 || |
| 140 | + that.keysDown.indexOf(evt.keyCode) !== -1 || |
| 141 | + that.keysLeft.indexOf(evt.keyCode) !== -1 || |
| 142 | + that.keysRight.indexOf(evt.keyCode) !== -1) { |
| 143 | + var index = that._keys.indexOf(evt.keyCode); |
| 144 | + |
| 145 | + if (index === -1) { |
| 146 | + that._keys.push(evt.keyCode); |
| 147 | + } |
| 148 | + evt.preventDefault(); |
| 149 | + } |
133 | 150 | }; |
134 | | - evt.preventDefault(); |
135 | | - }; |
136 | | - |
137 | | - this._onKeyDown = function (evt) { |
138 | | - if (that.keysUp.indexOf(evt.keyCode) !== -1 || |
139 | | - that.keysDown.indexOf(evt.keyCode) !== -1 || |
140 | | - that.keysLeft.indexOf(evt.keyCode) !== -1 || |
141 | | - that.keysRight.indexOf(evt.keyCode) !== -1) { |
142 | | - var index = that._keys.indexOf(evt.keyCode); |
143 | | - |
144 | | - if (index === -1) { |
145 | | - that._keys.push(evt.keyCode); |
| 151 | + |
| 152 | + this._onKeyUp = function (evt) { |
| 153 | + if (that.keysUp.indexOf(evt.keyCode) !== -1 || |
| 154 | + that.keysDown.indexOf(evt.keyCode) !== -1 || |
| 155 | + that.keysLeft.indexOf(evt.keyCode) !== -1 || |
| 156 | + that.keysRight.indexOf(evt.keyCode) !== -1) { |
| 157 | + var index = that._keys.indexOf(evt.keyCode); |
| 158 | + |
| 159 | + if (index >= 0) { |
| 160 | + that._keys.splice(index, 1); |
| 161 | + } |
| 162 | + evt.preventDefault(); |
146 | 163 | } |
147 | | - evt.preventDefault(); |
148 | | - } |
149 | | - }; |
| 164 | + }; |
150 | 165 |
|
151 | | - this._onKeyUp = function (evt) { |
152 | | - if (that.keysUp.indexOf(evt.keyCode) !== -1 || |
153 | | - that.keysDown.indexOf(evt.keyCode) !== -1 || |
154 | | - that.keysLeft.indexOf(evt.keyCode) !== -1 || |
155 | | - that.keysRight.indexOf(evt.keyCode) !== -1) { |
156 | | - var index = that._keys.indexOf(evt.keyCode); |
| 166 | + this._onLostFocus = function () { |
| 167 | + that._keys = []; |
| 168 | + }; |
| 169 | + } |
157 | 170 |
|
158 | | - if (index >= 0) { |
159 | | - that._keys.splice(index, 1); |
160 | | - } |
161 | | - evt.preventDefault(); |
162 | | - } |
163 | | - }; |
164 | | - |
165 | | - this._onLostFocus = function () { |
166 | | - that._keys = []; |
167 | | - }; |
168 | | - |
169 | | - canvas.addEventListener("mousedown", this._onMouseDown, true); |
170 | | - canvas.addEventListener("mouseup", this._onMouseUp, true); |
171 | | - canvas.addEventListener("mouseout", this._onMouseOut, true); |
172 | | - canvas.addEventListener("mousemove", this._onMouseMove, true); |
173 | | - window.addEventListener("keydown", this._onKeyDown, true); |
174 | | - window.addEventListener("keyup", this._onKeyUp, true); |
175 | | - window.addEventListener("blur", this._onLostFocus, true); |
| 171 | + canvas.addEventListener("mousedown", this._onMouseDown, false); |
| 172 | + canvas.addEventListener("mouseup", this._onMouseUp, false); |
| 173 | + canvas.addEventListener("mouseout", this._onMouseOut, false); |
| 174 | + canvas.addEventListener("mousemove", this._onMouseMove, false); |
| 175 | + window.addEventListener("keydown", this._onKeyDown, false); |
| 176 | + window.addEventListener("keyup", this._onKeyUp, false); |
| 177 | + window.addEventListener("blur", this._onLostFocus, false); |
176 | 178 | }; |
177 | 179 |
|
178 | 180 | BABYLON.FreeCamera.prototype.detachControl = function (canvas) { |
|
0 commit comments