diff --git a/examples/editEntities.js b/examples/editEntities.js index 10cc7df894..883362c4a4 100644 --- a/examples/editEntities.js +++ b/examples/editEntities.js @@ -826,6 +826,9 @@ Controller.keyPressEvent.connect(function(event) { }); Controller.keyReleaseEvent.connect(function (event) { + if (isActive) { + cameraManager.keyReleaseEvent(event); + } // since sometimes our menu shortcut keys don't work, trap our menu items here also and fire the appropriate menu items if (event.text == "BACKSPACE" || event.text == "DELETE") { deleteSelectedEntities(); diff --git a/examples/libraries/entityCameraTool.js b/examples/libraries/entityCameraTool.js index f5095bb149..ddbb6fafd5 100644 --- a/examples/libraries/entityCameraTool.js +++ b/examples/libraries/entityCameraTool.js @@ -15,6 +15,9 @@ var MOUSE_SENSITIVITY = 0.9; var SCROLL_SENSITIVITY = 0.05; var PAN_ZOOM_SCALE_RATIO = 0.4; +var KEY_ORBIT_SENSITIVITY = 40; +var KEY_ZOOM_SENSITIVITY = 10; + // Scaling applied based on the size of the object being focused var FOCUS_ZOOM_SCALE = 1.3; @@ -43,6 +46,10 @@ var easeOutCubic = function(t) { EASE_TIME = 0.5; +function clamp(value, minimum, maximum) { + return Math.min(Math.max(value, minimum), maximum); +} + function mergeObjects(obj1, obj2) { var newObj = {}; for (key in obj1) { @@ -60,6 +67,49 @@ CameraManager = function() { that.enabled = false; that.mode = MODE_INACTIVE; + var actions = { + orbitLeft: 0, + orbitRight: 0, + orbitUp: 0, + orbitDown: 0, + orbitForward: 0, + orbitBackward: 0, + } + + var keyToActionMapping = { + "a": "orbitLeft", + "d": "orbitRight", + "w": "orbitForward", + "s": "orbitBackward", + "e": "orbitUp", + "c": "orbitDown", + + "LEFT": "orbitLeft", + "RIGHT": "orbitRight", + "UP": "orbitForward", + "DOWN": "orbitBackward", + } + + var CAPTURED_KEYS = []; + for (key in keyToActionMapping) { + CAPTURED_KEYS.push(key); + } + + function getActionForKeyEvent(event) { + var action = keyToActionMapping[event.text]; + if (action !== undefined) { + if (event.isShifted) { + if (action == "orbitForward") { + action = "orbitUp"; + } else if (action == "orbitBackward") { + action = "orbitDown"; + } + } + return action; + } + return null; + } + that.zoomDistance = INITIAL_ZOOM_DISTANCE; that.targetZoomDistance = INITIAL_ZOOM_DISTANCE; @@ -82,6 +132,11 @@ CameraManager = function() { that.enable = function() { if (Camera.mode == "independent" || that.enabled) return; + for (var i = 0; i < CAPTURED_KEYS.length; i++) { + print("capturing: " + CAPTURED_KEYS[i]); + Controller.captureKeyEvents({ text: CAPTURED_KEYS[i] }); + } + that.enabled = true; that.mode = MODE_INACTIVE; @@ -112,6 +167,11 @@ CameraManager = function() { that.disable = function(ignoreCamera) { if (!that.enabled) return; + + for (var i = 0; i < CAPTURED_KEYS.length; i++) { + Controller.releaseKeyEvents({ text: CAPTURED_KEYS[i] }); + } + that.enabled = false; that.mode = MODE_INACTIVE; @@ -280,6 +340,20 @@ CameraManager = function() { that.mode = MODE_INACTIVE; } + that.keyPressEvent = function(event) { + var action = getActionForKeyEvent(event); + if (action) { + actions[action] = 1; + } + }; + + that.keyReleaseEvent = function(event) { + var action = getActionForKeyEvent(event); + if (action) { + actions[action] = 0; + } + }; + that.wheelEvent = function(event) { if (!that.enabled) return; @@ -333,6 +407,14 @@ CameraManager = function() { return; } + // Update based on current actions + that.targetYaw += (actions.orbitRight - actions.orbitLeft) * dt * KEY_ORBIT_SENSITIVITY; + that.targetPitch += (actions.orbitUp - actions.orbitDown) * dt * KEY_ORBIT_SENSITIVITY; + that.targetPitch = clamp(that.targetPitch, -90, 90); + var addZoom = (actions.orbitBackward - actions.orbitForward) * dt * KEY_ZOOM_SENSITIVITY; + that.targetZoomDistance = clamp(that.targetZoomDistance + addZoom, MIN_ZOOM_DISTANCE, MAX_ZOOM_DISTANCE); + + if (easing) { easingTime = Math.min(EASE_TIME, easingTime + dt); } @@ -384,6 +466,7 @@ CameraManager = function() { }); Script.update.connect(that.update); + Script.scriptEnding.connect(that.disable); Controller.wheelEvent.connect(that.wheelEvent);