From 6c978c0ff3a77da165203e2b58330b2260c493c9 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 11 Mar 2015 10:27:38 -0700 Subject: [PATCH] Add look.js Merges both lookWithMouse and lookWithTouch, and updates mouse look to only work with right-click --- examples/look.js | 168 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 examples/look.js diff --git a/examples/look.js b/examples/look.js new file mode 100644 index 0000000000..bcdfcf9e44 --- /dev/null +++ b/examples/look.js @@ -0,0 +1,168 @@ +// +// look.js +// examples +// +// Copyright 2015 High Fidelity, Inc. +// +// This is an example script that demonstrates use of the Controller class +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var wantDebugging = false; + + +// Configuration +var TOUCH_YAW_SCALE = -0.25; +var TOUCH_PITCH_SCALE = -12.5; +var FIXED_TOUCH_TIMESTEP = 0.016; + +var MOUSE_YAW_SCALE = -0.25; +var MOUSE_PITCH_SCALE = -12.5; +var FIXED_MOUSE_TIMESTEP = 0.016; + +// Mouse Data +var alwaysLook = false; // if you want the mouse look to happen only when you click, change this to false +var isMouseDown = false; +var lastTouchX = 0; +var lastTouchY = 0; +var yawFromTouch = 0; +var pitchFromTouch = 0; + +// Touch Data +var startedTouching = false; +var lastMouseX = 0; +var lastMouseY = 0; +var yawFromMouse = 0; +var pitchFromMouse = 0; + + +// Mouse Events +function mousePressEvent(event) { + if (wantDebugging) { + print("mousePressEvent event.x,y=" + event.x + ", " + event.y); + } + if (event.isRightButton) { + isMouseDown = true; + lastMouseX = event.x; + lastMouseY = event.y; + } +} + +function mouseReleaseEvent(event) { + if (wantDebugging) { + print("mouseReleaseEvent event.x,y=" + event.x + ", " + event.y); + } + isMouseDown = false; +} + +function mouseMoveEvent(event) { + if (wantDebugging) { + print("mouseMoveEvent event.x,y=" + event.x + ", " + event.y); + } + + if (alwaysLook || isMouseDown) { + yawFromMouse += ((event.x - lastMouseX) * MOUSE_YAW_SCALE * FIXED_MOUSE_TIMESTEP); + pitchFromMouse += ((event.y - lastMouseY) * MOUSE_PITCH_SCALE * FIXED_MOUSE_TIMESTEP); + lastMouseX = event.x; + lastMouseY = event.y; + } +} + +// Touch Events +function touchBeginEvent(event) { + if (wantDebugging) { + print("touchBeginEvent event.x,y=" + event.x + ", " + event.y); + } + lastTouchX = event.x; + lastTouchY = event.y; + yawFromTouch = 0; + pitchFromTouch = 0; + startedTouching = true; + print("TOUCH BEGIN"); +} + +function touchEndEvent(event) { + if (wantDebugging) { + print("touchEndEvent event.x,y=" + event.x + ", " + event.y); + } + startedTouching = false; + print("TOUCH END"); +} + +function touchUpdateEvent(event) { + // print("TOUCH UPDATE"); + if (wantDebugging) { + print("touchUpdateEvent event.x,y=" + event.x + ", " + event.y); + } + + if (!startedTouching) { + // handle Qt 5.4.x bug where we get touch update without a touch begin event + startedTouching = true; + lastTouchX = event.x; + lastTouchY = event.y; + } + + yawFromTouch += ((event.x - lastTouchX) * TOUCH_YAW_SCALE * FIXED_TOUCH_TIMESTEP); + pitchFromTouch += ((event.y - lastTouchY) * TOUCH_PITCH_SCALE * FIXED_TOUCH_TIMESTEP); + lastTouchX = event.x; + lastTouchY = event.y; +} + + +function update(deltaTime) { + if (wantDebugging) { + print("update()..."); + } + + if (yawFromTouch != 0 || yawFromMouse != 0) { + var newOrientation = Quat.multiply(MyAvatar.orientation, Quat.fromPitchYawRollRadians(0, yawFromTouch + yawFromMouse, 0)); + + if (wantDebugging) { + print("changing orientation" + + " [old]MyAvatar.orientation="+MyAvatar.orientation.x + "," + MyAvatar.orientation.y + "," + + MyAvatar.orientation.z + "," + MyAvatar.orientation.w + + " newOrientation="+newOrientation.x + "," + newOrientation.y + "," + newOrientation.z + "," + newOrientation.w); + } + + MyAvatar.orientation = newOrientation; + yawFromTouch = 0; + yawFromMouse = 0; + } + + if (pitchFromTouch != 0 || pitchFromMouse != 0) { + var newPitch = MyAvatar.headPitch + pitchFromTouch + pitchFromMouse; + + if (wantDebugging) { + print("changing pitch [old]MyAvatar.headPitch="+MyAvatar.headPitch+ " newPitch="+newPitch); + } + + MyAvatar.headPitch = newPitch; + pitchFromTouch = 0; + pitchFromMouse = 0; + } +} + + +// Map the mouse events to our functions +Controller.mousePressEvent.connect(mousePressEvent); +Controller.mouseMoveEvent.connect(mouseMoveEvent); +Controller.mouseReleaseEvent.connect(mouseReleaseEvent); + +// Map the mouse events to our functions +Controller.touchBeginEvent.connect(touchBeginEvent); +Controller.touchUpdateEvent.connect(touchUpdateEvent); +Controller.touchEndEvent.connect(touchEndEvent); + +// disable the standard application for mouse events +Controller.captureTouchEvents(); + +function scriptEnding() { + // re-enabled the standard application for mouse events + Controller.releaseTouchEvents(); +} + +// would be nice to change to update +Script.update.connect(update); +Script.scriptEnding.connect(scriptEnding);