瀏覽代碼

1.0.8:
- Adding keyboard support to ArcRotateCamera

deltakosh 12 年之前
父節點
當前提交
82a275d134
共有 3 個文件被更改,包括 63 次插入2 次删除
  1. 60 1
      Babylon/Cameras/babylon.arcRotateCamera.js
  2. 1 1
      babylon.1.0.7.js
  3. 2 0
      what's new.txt

+ 60 - 1
Babylon/Cameras/babylon.arcRotateCamera.js

@@ -8,6 +8,12 @@
         this.beta = beta;
         this.radius = radius;
         this.target = target;
+        
+        this._keys = [];
+        this.keysUp = [38];
+        this.keysDown = [40];
+        this.keysLeft = [37];
+        this.keysRight = [39];
 
         this._scene = scene;
 
@@ -81,12 +87,47 @@
             if (event.preventDefault)
                 event.preventDefault();
         };
+        
+        this._onKeyDown = function (evt) {
+            if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
+                that.keysDown.indexOf(evt.keyCode) !== -1 ||
+                that.keysLeft.indexOf(evt.keyCode) !== -1 ||
+                that.keysRight.indexOf(evt.keyCode) !== -1) {
+                var index = that._keys.indexOf(evt.keyCode);
+
+                if (index === -1) {
+                    that._keys.push(evt.keyCode);
+                }
+                evt.preventDefault();
+            }
+        };
+
+        this._onKeyUp = function (evt) {
+            if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
+                that.keysDown.indexOf(evt.keyCode) !== -1 ||
+                that.keysLeft.indexOf(evt.keyCode) !== -1 ||
+                that.keysRight.indexOf(evt.keyCode) !== -1) {
+                var index = that._keys.indexOf(evt.keyCode);
+
+                if (index >= 0) {
+                    that._keys.splice(index, 1);
+                }
+                evt.preventDefault();
+            }
+        };
+        
+        this._onLostFocus = function () {
+            that._keys = [];
+        };
 
         canvas.addEventListener("pointerdown", this._onPointerDown);
         canvas.addEventListener("pointerup", this._onPointerUp);
         canvas.addEventListener("pointerout", this._onPointerUp);
         canvas.addEventListener("pointermove", this._onPointerMove);
+        window.addEventListener("keydown", this._onKeyDown, true);
+        window.addEventListener("keyup", this._onKeyUp, true);
         window.addEventListener('mousewheel', this._wheel);
+        window.addEventListener("blur", this._onLostFocus, true);
     };
     
     BABYLON.ArcRotateCamera.prototype.detachControl = function (canvas) {
@@ -94,10 +135,28 @@
         canvas.removeEventListener("pointerup", this._onPointerUp);
         canvas.removeEventListener("pointerout", this._onPointerUp);
         canvas.removeEventListener("pointermove", this._onPointerMove);
+        window.removeEventListener("keydown", this._onKeyDown);
+        window.removeEventListener("keyup", this._onKeyUp);
         window.removeEventListener('mousewheel', this._wheel);
+        window.removeEventListener("blur", this._onLostFocus);
     };
 
-    BABYLON.ArcRotateCamera.prototype._update = function() {
+    BABYLON.ArcRotateCamera.prototype._update = function () {
+        // Keyboard
+        for (var index = 0; index < this._keys.length; index++) {
+            var keyCode = this._keys[index];
+
+            if (this.keysLeft.indexOf(keyCode) !== -1) {
+                this.inertialAlphaOffset -= 0.01;
+            } else if (this.keysUp.indexOf(keyCode) !== -1) {
+                this.inertialBetaOffset -= 0.01;
+            } else if (this.keysRight.indexOf(keyCode) !== -1) {
+                this.inertialAlphaOffset += 0.01;
+            } else if (this.keysDown.indexOf(keyCode) !== -1) {
+                this.inertialBetaOffset += 0.01;
+            }
+        }
+        
         // Inertia
         if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0) {
 

文件差異過大導致無法顯示
+ 1 - 1
babylon.1.0.7.js


+ 2 - 0
what's new.txt

@@ -1,3 +1,5 @@
+1.0.8:
+ - Adding keyboard support to ArcRotateCamera
 1.0.7:
  - New demo: Worldmonger
  - Improved IE shaders