# HG changeset patch # User Eugen Sawin # Date 1302984291 -7200 # Node ID cf2ca81264fb8080181dbd2c76c14dedd4c44e54 # Parent 388543f4ea07214b04030799f1da07684070707b Added wheel zoom. diff -r 388543f4ea07 -r cf2ca81264fb scripts/machine.js --- a/scripts/machine.js Sat Apr 16 02:40:22 2011 +0200 +++ b/scripts/machine.js Sat Apr 16 22:04:51 2011 +0200 @@ -2,13 +2,14 @@ var renderer; var controller; var camera; -var cameraSpeed = {'x': 0.1, 'y': 0.1, 'pitch': 0.5, 'yaw': 0.5}; +var cameraSpeed = {"x": 0.1, "y": 0.1, "z": 0.5, "pitch": 0.5, "yaw": 0.5}; var keyActionMap = {'A': moveCameraLeft, 'D': moveCameraRight, 'W': moveCameraUp, 'S': moveCameraDown}; -var mouseActionMap = {'pitch': [[true, false, false], pitchCamera], - 'yaw': [[true, false, false], yawCamera]}; +var mouseActionMap = {"pitch": [[true, false, false], pitchCamera], + "yaw": [[true, false, false], yawCamera], + "wheel": zoomCamera}; function main() { @@ -30,6 +31,7 @@ canvas.onmousedown = handleMouseDown; document.onmouseup = handleMouseUp; document.onmousemove = handleMouseMove; + document.onmousewheel = handleMouseWheel; //read("config/camera", configureCamera); } @@ -78,13 +80,9 @@ { this.y -= this.speed.y; } -Camera.prototype.moveForward = function() +Camera.prototype.zoom = function(delta) { - this.z -= this.speed.z; -} -Camera.prototype.moveBackward = function() -{ - this.z += this.speed.z; + this.z += this.speed.z * delta; } Camera.prototype.changePitch = function(delta) { @@ -110,7 +108,8 @@ function Mouse(actionMap) { this.actionMap = actionMap; - this.pressed = [false, false, false]; + this.pressed = [false, false, false]; + this.wheelDelta = 0; } Mouse.prototype.buttonDown = function(event) { @@ -122,11 +121,19 @@ } Mouse.prototype.move = function(event) { - var pos = [event.clientX, event.clientY]; - if (this.lastPos) + this.currentPos = [event.clientX, event.clientY]; +} +Mouse.prototype.moveWheel = function(event) +{ + this.wheelDelta += event.wheelDelta; +} +Mouse.prototype.handle = function() +{ + var pos = this.currentPos; + if (pos && this.lastPos) { var delta = [pos[0] - this.lastPos[0], pos[1] - this.lastPos[1]]; - delta = normaliseDelta(delta); + delta = normaliseMoveDelta(delta); var action = this.actionMap["pitch"]; if (delta[1] != 0 && action) { @@ -147,20 +154,30 @@ } } this.lastPos = pos; -} -Mouse.prototype.handle = function() -{ - //if (this.pressed[0]) alert(this.pressed[0] + " " + this.pressed[1] + " " + this.pressed[2]); - //if (this.pressed[0] && this.pressed[2]) + + if (this.wheelDelta != 0) + { + var delta = normaliseWheelDelta(this.wheelDelta); + var action = this.actionMap["wheel"]; + if (delta > 0 && action) action(delta); + else if (delta < 0 && action) action(delta); + this.wheelDelta = 0; + } + } -function normaliseDelta(delta) +function normaliseMoveDelta(delta) { var width = renderer.context.canvas.width; var height = renderer.context.canvas.height; return [delta[0] / width, delta[1] / height]; } +function normaliseWheelDelta(delta) +{ + return delta / 60; +} + function Keyboard(actionMap) { this.actionMap = actionMap; @@ -476,6 +493,11 @@ camera.moveDown(); } +function zoomCamera(delta) +{ + camera.zoom(delta); +} + function pitchCamera(delta) { camera.changePitch(delta); @@ -510,3 +532,8 @@ { controller.mouse.move(event); } + +function handleMouseWheel(event) +{ + controller.mouse.moveWheel(event); +}