Added wheel zoom.
1.1 --- a/scripts/machine.js Sat Apr 16 02:40:22 2011 +0200
1.2 +++ b/scripts/machine.js Sat Apr 16 22:04:51 2011 +0200
1.3 @@ -2,13 +2,14 @@
1.4 var renderer;
1.5 var controller;
1.6 var camera;
1.7 -var cameraSpeed = {'x': 0.1, 'y': 0.1, 'pitch': 0.5, 'yaw': 0.5};
1.8 +var cameraSpeed = {"x": 0.1, "y": 0.1, "z": 0.5, "pitch": 0.5, "yaw": 0.5};
1.9 var keyActionMap = {'A': moveCameraLeft,
1.10 'D': moveCameraRight,
1.11 'W': moveCameraUp,
1.12 'S': moveCameraDown};
1.13 -var mouseActionMap = {'pitch': [[true, false, false], pitchCamera],
1.14 - 'yaw': [[true, false, false], yawCamera]};
1.15 +var mouseActionMap = {"pitch": [[true, false, false], pitchCamera],
1.16 + "yaw": [[true, false, false], yawCamera],
1.17 + "wheel": zoomCamera};
1.18
1.19 function main()
1.20 {
1.21 @@ -30,6 +31,7 @@
1.22 canvas.onmousedown = handleMouseDown;
1.23 document.onmouseup = handleMouseUp;
1.24 document.onmousemove = handleMouseMove;
1.25 + document.onmousewheel = handleMouseWheel;
1.26 //read("config/camera", configureCamera);
1.27 }
1.28
1.29 @@ -78,13 +80,9 @@
1.30 {
1.31 this.y -= this.speed.y;
1.32 }
1.33 -Camera.prototype.moveForward = function()
1.34 +Camera.prototype.zoom = function(delta)
1.35 {
1.36 - this.z -= this.speed.z;
1.37 -}
1.38 -Camera.prototype.moveBackward = function()
1.39 -{
1.40 - this.z += this.speed.z;
1.41 + this.z += this.speed.z * delta;
1.42 }
1.43 Camera.prototype.changePitch = function(delta)
1.44 {
1.45 @@ -110,7 +108,8 @@
1.46 function Mouse(actionMap)
1.47 {
1.48 this.actionMap = actionMap;
1.49 - this.pressed = [false, false, false];
1.50 + this.pressed = [false, false, false];
1.51 + this.wheelDelta = 0;
1.52 }
1.53 Mouse.prototype.buttonDown = function(event)
1.54 {
1.55 @@ -122,11 +121,19 @@
1.56 }
1.57 Mouse.prototype.move = function(event)
1.58 {
1.59 - var pos = [event.clientX, event.clientY];
1.60 - if (this.lastPos)
1.61 + this.currentPos = [event.clientX, event.clientY];
1.62 +}
1.63 +Mouse.prototype.moveWheel = function(event)
1.64 +{
1.65 + this.wheelDelta += event.wheelDelta;
1.66 +}
1.67 +Mouse.prototype.handle = function()
1.68 +{
1.69 + var pos = this.currentPos;
1.70 + if (pos && this.lastPos)
1.71 {
1.72 var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1]];
1.73 - delta = normaliseDelta(delta);
1.74 + delta = normaliseMoveDelta(delta);
1.75 var action = this.actionMap["pitch"];
1.76 if (delta[1] != 0 && action)
1.77 {
1.78 @@ -147,20 +154,30 @@
1.79 }
1.80 }
1.81 this.lastPos = pos;
1.82 -}
1.83 -Mouse.prototype.handle = function()
1.84 -{
1.85 - //if (this.pressed[0]) alert(this.pressed[0] + " " + this.pressed[1] + " " + this.pressed[2]);
1.86 - //if (this.pressed[0] && this.pressed[2])
1.87 +
1.88 + if (this.wheelDelta != 0)
1.89 + {
1.90 + var delta = normaliseWheelDelta(this.wheelDelta);
1.91 + var action = this.actionMap["wheel"];
1.92 + if (delta > 0 && action) action(delta);
1.93 + else if (delta < 0 && action) action(delta);
1.94 + this.wheelDelta = 0;
1.95 + }
1.96 +
1.97 }
1.98
1.99 -function normaliseDelta(delta)
1.100 +function normaliseMoveDelta(delta)
1.101 {
1.102 var width = renderer.context.canvas.width;
1.103 var height = renderer.context.canvas.height;
1.104 return [delta[0] / width, delta[1] / height];
1.105 }
1.106
1.107 +function normaliseWheelDelta(delta)
1.108 +{
1.109 + return delta / 60;
1.110 +}
1.111 +
1.112 function Keyboard(actionMap)
1.113 {
1.114 this.actionMap = actionMap;
1.115 @@ -476,6 +493,11 @@
1.116 camera.moveDown();
1.117 }
1.118
1.119 +function zoomCamera(delta)
1.120 +{
1.121 + camera.zoom(delta);
1.122 +}
1.123 +
1.124 function pitchCamera(delta)
1.125 {
1.126 camera.changePitch(delta);
1.127 @@ -510,3 +532,8 @@
1.128 {
1.129 controller.mouse.move(event);
1.130 }
1.131 +
1.132 +function handleMouseWheel(event)
1.133 +{
1.134 + controller.mouse.moveWheel(event);
1.135 +}