Merge branch 'master' of https://github.com/highfidelity/hifi into mouse_handling_in_editModels

This commit is contained in:
Atlante45 2014-05-14 10:41:29 -07:00
commit 4f93858849
27 changed files with 745 additions and 326 deletions

68
examples/Test.js Normal file
View file

@ -0,0 +1,68 @@
//
// Test.js
// examples
//
// Created by Ryan Huffman on 5/4/14
// Copyright 2014 High Fidelity, Inc.
//
// This provides very basic unit testing functionality.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
test = function(name, func) {
print("Running test: " + name);
var unitTest = new UnitTest(name, func);
try {
unitTest.run();
print(" Success: " + unitTest.numAssertions + " assertions passed");
} catch (error) {
print(" Failure: " + error.message);
}
};
AssertionException = function(expected, actual, message) {
print("Creating exception");
this.message = message + "\n: " + actual + " != " + expected;
this.name = 'AssertionException';
};
UnitTest = function(name, func) {
this.numAssertions = 0;
this.func = func;
};
UnitTest.prototype.run = function() {
this.func();
};
UnitTest.prototype.assertNotEquals = function(expected, actual, message) {
this.numAssertions++;
if (expected == actual) {
throw new AssertionException(expected, actual, message);
}
};
UnitTest.prototype.assertEquals = function(expected, actual, message) {
this.numAssertions++;
if (expected != actual) {
throw new AssertionException(expected, actual, message);
}
};
UnitTest.prototype.assertHasProperty = function(property, actual, message) {
this.numAssertions++;
if (actual[property] === undefined) {
throw new AssertionException(property, actual, message);
}
};
UnitTest.prototype.assertNull = function(value, message) {
this.numAssertions++;
if (value !== null) {
throw new AssertionException(value, null, message);
}
};

View file

@ -0,0 +1,51 @@
//
// streetAreaExample.js
// examples
//
// Created by Ryan Huffman on 5/4/14
// Copyright 2014 High Fidelity, Inc.
//
// This is an example script showing how to load JSON data using XMLHttpRequest.
//
// URL Macro created by Thijs Wenker.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
var url = "https://script.google.com/macros/s/AKfycbwIo4lmF-qUwX1Z-9eA_P-g2gse9oFhNcjVyyksGukyDDEFXgU/exec?action=listOwners&domain=alpha.highfidelity.io";
print("Loading street data from " + url);
var req = new XMLHttpRequest();
// Set response type to "json". This will tell XMLHttpRequest to parse the response data as json, so req.response can be used
// as a regular javascript object
req.responseType = 'json';
req.open("GET", url, false);
req.send();
if (req.status == 200) {
for (var domain in req.response) {
print("DOMAIN: " + domain);
var locations = req.response[domain];
var userAreas = [];
for (var i = 0; i < locations.length; i++) {
var loc = locations[i];
var x1 = loc[1],
x2 = loc[2],
y1 = loc[3],
y2 = loc[4];
userAreas.push({
username: loc[0],
area: Math.abs(x2 - x1) * Math.abs(y2 - y1),
});
}
userAreas.sort(function(a, b) { return a.area > b.area ? -1 : (a.area < b.area ? 1 : 0) });
for (var i = 0; i < userAreas.length; i++) {
print(userAreas[i].username + ": " + userAreas[i].area + " sq units");
}
}
} else {
print("Error loading data: " + req.status + " " + req.statusText + ", " + req.errorCode);
}

View file

@ -0,0 +1,147 @@
//
// testXMLHttpRequest.js
// examples
//
// Created by Ryan Huffman on 5/4/14
// Copyright 2014 High Fidelity, Inc.
//
// XMLHttpRequest Tests
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
Script.include("Test.js");
test("Test default request values", function(finished) {
var req = new XMLHttpRequest();
this.assertEquals(req.UNSENT, req.readyState, "readyState should be UNSENT");
this.assertEquals(0, req.status, "status should be `0` by default");
this.assertEquals("", req.statusText, "statusText should be empty string by default");
this.assertEquals("", req.getAllResponseHeaders(), "getAllResponseHeaders() should return empty string by default");
this.assertEquals("", req.response, "response should be empty string by default");
this.assertEquals("", req.responseText, "responseText should be empty string by default");
this.assertEquals("", req.responseType, "responseType should be empty string by default");
this.assertEquals(0, req.timeout, "timeout should be `0` by default");
this.assertEquals(0, req.errorCode, "there should be no error by default");
});
test("Test readyStates", function() {
var req = new XMLHttpRequest();
var state = req.readyState;
var statesVisited = [true, false, false, false, false]
req.onreadystatechange = function() {
statesVisited[req.readyState] = true;
};
req.open("GET", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false);
req.send();
for (var i = 0; i <= req.DONE; i++) {
this.assertEquals(true, statesVisited[i], i + " should be set");
}
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
});
test("Test TEXT request", function() {
var req = new XMLHttpRequest();
var state = req.readyState;
req.open("GET", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false);
req.send();
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
this.assertEquals(200, req.status, "status should be `200`");
this.assertEquals(0, req.errorCode);
this.assertEquals("OK", req.statusText, "statusText should be `OK`");
this.assertNotEquals("", req.getAllResponseHeaders(), "headers should no longer be empty string");
this.assertNull(req.getResponseHeader('invalidheader'), "invalid header should return `null`");
this.assertEquals("GitHub.com", req.getResponseHeader('Server'), "Server header should be GitHub.com");
this.assertEquals('{"id": 1}', req.response);
this.assertEquals('{"id": 1}', req.responseText);
});
test("Test JSON request", function() {
var req = new XMLHttpRequest();
var state = req.readyState;
req.responseType = "json";
req.open("GET", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false);
req.send();
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
this.assertEquals(200, req.status, "status should be `200`");
this.assertEquals(0, req.errorCode);
this.assertEquals("OK", req.statusText, "statusText should be `OK`");
this.assertNotEquals("", req.getAllResponseHeaders(), "headers should no longer be empty string");
this.assertNull(req.getResponseHeader('invalidheader'), "invalid header should return `null`");
this.assertEquals("GitHub.com", req.getResponseHeader('Server'), "Server header should be GitHub.com");
this.assertHasProperty('id', req.response);
this.assertEquals(1, req.response.id);
this.assertEquals('{"id": 1}', req.responseText);
});
test("Test Bad URL", function() {
var req = new XMLHttpRequest();
var state = req.readyState;
req.open("POST", "hifi://domain/path", false);
req.send();
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
this.assertNotEquals(0, req.errorCode);
});
test("Test Bad Method Error", function() {
var req = new XMLHttpRequest();
var state = req.readyState;
req.open("POST", "https://www.google.com", false);
req.send("randomdata");
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
this.assertEquals(405, req.status);
this.assertEquals(202, req.errorCode);
req.open("POST", "https://www.google.com", false)
req.send();
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
this.assertEquals(405, req.status);
this.assertEquals(202, req.errorCode);
});
test("Test abort", function() {
var req = new XMLHttpRequest();
var state = req.readyState;
req.open("POST", "https://www.google.com", true)
req.send();
req.abort();
this.assertEquals(0, req.status);
this.assertEquals(0, req.errorCode);
});
test("Test timeout", function() {
var req = new XMLHttpRequest();
var state = req.readyState;
var timedOut = false;
req.ontimeout = function() {
timedOut = true;
};
req.open("POST", "https://gist.githubusercontent.com/huffman/33cc618fec183d1bccd0/raw/test.json", false)
req.timeout = 1;
req.send();
this.assertEquals(true, timedOut, "request should have timed out");
this.assertEquals(req.DONE, req.readyState, "readyState should be DONE");
this.assertEquals(0, req.status, "status should be `0`");
this.assertEquals(4, req.errorCode, "4 is the timeout error code for QNetworkReply::NetworkError");
});

View file

@ -3405,6 +3405,8 @@ ScriptEngine* Application::loadScript(const QString& scriptName, bool loadScript
scriptEngine->registerGlobalObject("Clipboard", clipboardScriptable);
connect(scriptEngine, SIGNAL(finished(const QString&)), clipboardScriptable, SLOT(deleteLater()));
connect(scriptEngine, SIGNAL(finished(const QString&)), this, SLOT(scriptFinished(const QString&)));
scriptEngine->registerGlobalObject("Overlays", &_overlays);
QScriptValue windowValue = scriptEngine->registerGlobalObject("Window", WindowScriptingInterface::getInstance());
@ -3447,6 +3449,14 @@ ScriptEngine* Application::loadScript(const QString& scriptName, bool loadScript
return scriptEngine;
}
void Application::scriptFinished(const QString& scriptName) {
if (_scriptEnginesHash.remove(scriptName)) {
_runningScriptsWidget->scriptStopped(scriptName);
_runningScriptsWidget->setRunningScripts(getRunningScripts());
bumpSettings();
}
}
void Application::stopAllScripts(bool restart) {
// stops all current running scripts
for (QHash<QString, ScriptEngine*>::const_iterator it = _scriptEnginesHash.constBegin();
@ -3457,18 +3467,12 @@ void Application::stopAllScripts(bool restart) {
it.value()->stop();
qDebug() << "stopping script..." << it.key();
}
_scriptEnginesHash.clear();
_runningScriptsWidget->setRunningScripts(getRunningScripts());
bumpSettings();
}
void Application::stopScript(const QString &scriptName) {
if (_scriptEnginesHash.contains(scriptName)) {
_scriptEnginesHash.value(scriptName)->stop();
qDebug() << "stopping script..." << scriptName;
_scriptEnginesHash.remove(scriptName);
_runningScriptsWidget->setRunningScripts(getRunningScripts());
bumpSettings();
}
}

View file

@ -292,6 +292,7 @@ public slots:
void toggleLogDialog();
void initAvatarAndViewFrustum();
ScriptEngine* loadScript(const QString& fileNameString, bool loadScriptFromEditor = false);
void scriptFinished(const QString& scriptName);
void stopAllScripts(bool restart = false);
void stopScript(const QString& scriptName);
void reloadAllScripts();

View file

@ -60,15 +60,13 @@ BuckyBalls::BuckyBalls() {
void BuckyBalls::grab(PalmData& palm, float deltaTime) {
float penetration;
glm::vec3 diff;
FingerData& finger = palm.getFingers()[0]; // Sixense has only one finger
glm::vec3 fingerTipPosition = finger.getTipPosition();
glm::vec3 fingerTipPosition = palm.getFingerTipPosition();
if (palm.getControllerButtons() & BUTTON_FWD) {
if (!_bballIsGrabbed[palm.getSixenseID()]) {
// Look for a ball to grab
for (int i = 0; i < NUM_BBALLS; i++) {
diff = _bballPosition[i] - fingerTipPosition;
glm::vec3 diff = _bballPosition[i] - fingerTipPosition;
penetration = glm::length(diff) - (_bballRadius[i] + COLLISION_RADIUS);
if (penetration < 0.f) {
_bballIsGrabbed[palm.getSixenseID()] = i;
@ -77,7 +75,7 @@ void BuckyBalls::grab(PalmData& palm, float deltaTime) {
}
if (_bballIsGrabbed[palm.getSixenseID()]) {
// If ball being grabbed, move with finger
diff = _bballPosition[_bballIsGrabbed[palm.getSixenseID()]] - fingerTipPosition;
glm::vec3 diff = _bballPosition[_bballIsGrabbed[palm.getSixenseID()]] - fingerTipPosition;
penetration = glm::length(diff) - (_bballRadius[_bballIsGrabbed[palm.getSixenseID()]] + COLLISION_RADIUS);
_bballPosition[_bballIsGrabbed[palm.getSixenseID()]] -= glm::normalize(diff) * penetration;
glm::vec3 fingerTipVelocity = palm.getTipVelocity();

View file

@ -609,18 +609,6 @@ bool Avatar::findParticleCollisions(const glm::vec3& particleCenter, float parti
const PalmData* palm = handData->getPalm(i);
if (palm && palm->hasPaddle()) {
// create a disk collision proxy where the hand is
glm::vec3 fingerAxis(0.0f);
for (size_t f = 0; f < palm->getNumFingers(); ++f) {
const FingerData& finger = (palm->getFingers())[f];
if (finger.isActive()) {
// compute finger axis
glm::vec3 fingerTip = finger.getTipPosition();
glm::vec3 fingerRoot = finger.getRootPosition();
fingerAxis = glm::normalize(fingerTip - fingerRoot);
break;
}
}
int jointIndex = -1;
glm::vec3 handPosition;
if (i == 0) {
@ -631,6 +619,8 @@ bool Avatar::findParticleCollisions(const glm::vec3& particleCenter, float parti
_skeletonModel.getRightHandPosition(handPosition);
jointIndex = _skeletonModel.getRightHandJointIndex();
}
glm::vec3 fingerAxis = palm->getFingerDirection();
glm::vec3 diskCenter = handPosition + HAND_PADDLE_OFFSET * fingerAxis;
glm::vec3 diskNormal = palm->getNormal();
const float DISK_THICKNESS = 0.08f;

View file

@ -54,10 +54,9 @@ enum ScreenTintLayer {
NUM_SCREEN_TINT_LAYERS
};
// Where one's own Avatar begins in the world (will be overwritten if avatar data file is found)
// this is basically in the center of the ground plane. Slightly adjusted. This was asked for by
// Grayson as he's building a street around here for demo dinner 2
const glm::vec3 START_LOCATION(0.485f * TREE_SCALE, 0.0f, 0.5f * TREE_SCALE);
// Where one's own Avatar begins in the world (will be overwritten if avatar data file is found).
// This is the start location in the Sandbox (xyz: 6270, 211, 6000).
const glm::vec3 START_LOCATION(0.38269043f * TREE_SCALE, 0.01287842f * TREE_SCALE, 0.36621094f * TREE_SCALE);
class Texture;

View file

@ -23,7 +23,6 @@
using namespace std;
const float FINGERTIP_COLLISION_RADIUS = 0.01f;
const float PALM_COLLISION_RADIUS = 0.03f;
@ -176,8 +175,7 @@ void Hand::renderHandTargets(bool isMine) {
if (!palm.isActive()) {
continue;
}
glm::vec3 targetPosition;
palm.getBallHoldPosition(targetPosition);
glm::vec3 targetPosition = palm.getFingerTipPosition();
glPushMatrix();
glTranslatef(targetPosition.x, targetPosition.y, targetPosition.z);
@ -197,59 +195,20 @@ void Hand::renderHandTargets(bool isMine) {
for (size_t i = 0; i < getNumPalms(); ++i) {
PalmData& palm = getPalms()[i];
if (palm.isActive()) {
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
FingerData& finger = palm.getFingers()[f];
if (finger.isActive()) {
glColor4f(handColor.r, handColor.g, handColor.b, alpha);
glm::vec3 tip = finger.getTipPosition();
glm::vec3 root = finger.getRootPosition();
Avatar::renderJointConnectingCone(root, tip, PALM_FINGER_ROD_RADIUS, PALM_FINGER_ROD_RADIUS);
// Render sphere at palm/finger root
glm::vec3 palmNormal = root + palm.getNormal() * PALM_DISK_THICKNESS;
Avatar::renderJointConnectingCone(root, palmNormal, PALM_DISK_RADIUS, 0.0f);
glPushMatrix();
glTranslatef(root.x, root.y, root.z);
glutSolidSphere(PALM_BALL_RADIUS, 20.0f, 20.0f);
glPopMatrix();
}
}
glColor4f(handColor.r, handColor.g, handColor.b, alpha);
glm::vec3 tip = palm.getFingerTipPosition();
glm::vec3 root = palm.getPosition();
Avatar::renderJointConnectingCone(root, tip, PALM_FINGER_ROD_RADIUS, PALM_FINGER_ROD_RADIUS);
// Render sphere at palm/finger root
glm::vec3 offsetFromPalm = root + palm.getNormal() * PALM_DISK_THICKNESS;
Avatar::renderJointConnectingCone(root, offsetFromPalm, PALM_DISK_RADIUS, 0.0f);
glPushMatrix();
glTranslatef(root.x, root.y, root.z);
glutSolidSphere(PALM_BALL_RADIUS, 20.0f, 20.0f);
glPopMatrix();
}
}
/*
// Draw the hand paddles
int MAX_NUM_PADDLES = 2; // one for left and one for right
glColor4f(handColor.r, handColor.g, handColor.b, 0.3f);
for (int i = 0; i < MAX_NUM_PADDLES; i++) {
const PalmData* palm = getPalm(i);
if (palm) {
// compute finger axis
glm::vec3 fingerAxis(0.f);
for (size_t f = 0; f < palm->getNumFingers(); ++f) {
const FingerData& finger = (palm->getFingers())[f];
if (finger.isActive()) {
glm::vec3 fingerTip = finger.getTipPosition();
glm::vec3 fingerRoot = finger.getRootPosition();
fingerAxis = glm::normalize(fingerTip - fingerRoot);
break;
}
}
// compute paddle position
glm::vec3 handPosition;
if (i == SIXENSE_CONTROLLER_ID_LEFT_HAND) {
_owningAvatar->getSkeletonModel().getLeftHandPosition(handPosition);
} else if (i == SIXENSE_CONTROLLER_ID_RIGHT_HAND) {
_owningAvatar->getSkeletonModel().getRightHandPosition(handPosition);
}
glm::vec3 tip = handPosition + HAND_PADDLE_OFFSET * fingerAxis;
glm::vec3 root = tip + palm->getNormal() * HAND_PADDLE_THICKNESS;
// render a very shallow cone as the paddle
Avatar::renderJointConnectingCone(root, tip, HAND_PADDLE_RADIUS, 0.f);
}
}
*/
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);

View file

@ -137,15 +137,8 @@ void MyAvatar::simulate(float deltaTime) {
Application::getInstance()->getCamera()->setScale(scale);
}
// update the movement of the hand and process handshaking with other avatars...
bool pointing = false;
if (_mousePressed) {
_handState = HAND_STATE_GRASPING;
} else if (pointing) {
_handState = HAND_STATE_POINTING;
} else {
_handState = HAND_STATE_NULL;
}
// no extra movement of the hand here any more ...
_handState = HAND_STATE_NULL;
updateOrientation(deltaTime);

View file

@ -33,7 +33,7 @@ void SkeletonModel::simulate(float deltaTime, bool fullUpdate) {
return; // only simulate for own avatar
}
// find the left and rightmost active Leap palms
// find the left and rightmost active palms
int leftPalmIndex, rightPalmIndex;
Hand* hand = _owningAvatar->getHand();
hand->getLeftRightPalmIndices(leftPalmIndex, rightPalmIndex);
@ -42,7 +42,7 @@ void SkeletonModel::simulate(float deltaTime, bool fullUpdate) {
const FBXGeometry& geometry = _geometry->getFBXGeometry();
if (leftPalmIndex == -1) {
// no Leap data; set hands from mouse
// palms are not yet set, use mouse
if (_owningAvatar->getHandState() == HAND_STATE_NULL) {
restoreRightHandPosition(HAND_RESTORATION_RATE);
} else {
@ -52,15 +52,12 @@ void SkeletonModel::simulate(float deltaTime, bool fullUpdate) {
} else if (leftPalmIndex == rightPalmIndex) {
// right hand only
applyPalmData(geometry.rightHandJointIndex, geometry.rightFingerJointIndices, geometry.rightFingertipJointIndices,
hand->getPalms()[leftPalmIndex]);
applyPalmData(geometry.rightHandJointIndex, hand->getPalms()[leftPalmIndex]);
restoreLeftHandPosition(HAND_RESTORATION_RATE);
} else {
applyPalmData(geometry.leftHandJointIndex, geometry.leftFingerJointIndices, geometry.leftFingertipJointIndices,
hand->getPalms()[leftPalmIndex]);
applyPalmData(geometry.rightHandJointIndex, geometry.rightFingerJointIndices, geometry.rightFingertipJointIndices,
hand->getPalms()[rightPalmIndex]);
applyPalmData(geometry.leftHandJointIndex, hand->getPalms()[leftPalmIndex]);
applyPalmData(geometry.rightHandJointIndex, hand->getPalms()[rightPalmIndex]);
}
}
@ -140,8 +137,7 @@ void SkeletonModel::applyHandPosition(int jointIndex, const glm::vec3& position)
applyRotationDelta(jointIndex, rotationBetween(handRotation * glm::vec3(-sign, 0.0f, 0.0f), forearmVector));
}
void SkeletonModel::applyPalmData(int jointIndex, const QVector<int>& fingerJointIndices,
const QVector<int>& fingertipJointIndices, PalmData& palm) {
void SkeletonModel::applyPalmData(int jointIndex, PalmData& palm) {
if (jointIndex == -1) {
return;
}
@ -152,7 +148,7 @@ void SkeletonModel::applyPalmData(int jointIndex, const QVector<int>& fingerJoin
return;
}
// rotate palm to align with palm direction
// rotate palm to align with its normal (normal points out of hand's palm)
glm::quat palmRotation;
if (Menu::getInstance()->isOptionChecked(MenuOption::AlignForearmsWithWrists)) {
getJointRotation(parentJointIndex, palmRotation, true);
@ -161,27 +157,9 @@ void SkeletonModel::applyPalmData(int jointIndex, const QVector<int>& fingerJoin
}
palmRotation = rotationBetween(palmRotation * geometry.palmDirection, palm.getNormal()) * palmRotation;
// sort the finger indices by raw x, get the average direction
QVector<IndexValue> fingerIndices;
glm::vec3 direction;
for (size_t i = 0; i < palm.getNumFingers(); i++) {
glm::vec3 fingerVector = palm.getFingers()[i].getTipPosition() - palm.getPosition();
float length = glm::length(fingerVector);
if (length > EPSILON) {
direction += fingerVector / length;
}
fingerVector = glm::inverse(palmRotation) * fingerVector * -sign;
IndexValue indexValue = { (int)i, atan2f(fingerVector.z, fingerVector.x) };
fingerIndices.append(indexValue);
}
qSort(fingerIndices.begin(), fingerIndices.end());
// rotate forearm according to average finger direction
float directionLength = glm::length(direction);
const unsigned int MIN_ROTATION_FINGERS = 3;
if (directionLength > EPSILON && palm.getNumFingers() >= MIN_ROTATION_FINGERS) {
palmRotation = rotationBetween(palmRotation * glm::vec3(-sign, 0.0f, 0.0f), direction) * palmRotation;
}
// rotate palm to align with finger direction
glm::vec3 direction = palm.getFingerDirection();
palmRotation = rotationBetween(palmRotation * glm::vec3(-sign, 0.0f, 0.0f), direction) * palmRotation;
// set hand position, rotation
if (Menu::getInstance()->isOptionChecked(MenuOption::AlignForearmsWithWrists)) {

View file

@ -39,8 +39,7 @@ protected:
void applyHandPosition(int jointIndex, const glm::vec3& position);
void applyPalmData(int jointIndex, const QVector<int>& fingerJointIndices,
const QVector<int>& fingertipJointIndices, PalmData& palm);
void applyPalmData(int jointIndex, PalmData& palm);
/// Updates the state of the joint at the specified index.
virtual void updateJointState(int index);

View file

@ -22,9 +22,9 @@ const int CALIBRATION_STATE_Z = 3;
const int CALIBRATION_STATE_COMPLETE = 4;
// default (expected) location of neck in sixense space
const float NECK_X = 250.f; // millimeters
const float NECK_Y = 300.f; // millimeters
const float NECK_Z = 300.f; // millimeters
const float NECK_X = 0.25f; // meters
const float NECK_Y = 0.3f; // meters
const float NECK_Z = 0.3f; // meters
#endif
SixenseManager::SixenseManager() {
@ -107,7 +107,10 @@ void SixenseManager::update(float deltaTime) {
palm->setTrigger(data->trigger);
palm->setJoystick(data->joystick_x, data->joystick_y);
// NOTE: Sixense API returns pos data in millimeters but we IMMEDIATELY convert to meters.
glm::vec3 position(data->pos[0], data->pos[1], data->pos[2]);
position *= METERS_PER_MILLIMETER;
// Transform the measured position into body frame.
glm::vec3 neck = _neckBase;
// Zeroing y component of the "neck" effectively raises the measured position a little bit.
@ -117,15 +120,12 @@ void SixenseManager::update(float deltaTime) {
// Rotation of Palm
glm::quat rotation(data->rot_quat[3], -data->rot_quat[0], data->rot_quat[1], -data->rot_quat[2]);
rotation = glm::angleAxis(PI, glm::vec3(0.f, 1.f, 0.f)) * _orbRotation * rotation;
const glm::vec3 PALM_VECTOR(0.0f, -1.0f, 0.0f);
glm::vec3 newNormal = rotation * PALM_VECTOR;
palm->setRawNormal(newNormal);
palm->setRawRotation(rotation);
// Compute current velocity from position change
glm::vec3 rawVelocity;
if (deltaTime > 0.f) {
rawVelocity = (position - palm->getRawPosition()) / deltaTime / 1000.f;
rawVelocity = (position - palm->getRawPosition()) / deltaTime;
} else {
rawVelocity = glm::vec3(0.0f);
}
@ -140,29 +140,17 @@ void SixenseManager::update(float deltaTime) {
_amountMoved = glm::vec3(0.0f);
}
// initialize the "finger" based on the direction
FingerData finger(palm, hand);
finger.setActive(true);
finger.setRawRootPosition(position);
const float FINGER_LENGTH = 300.0f; // Millimeters
// Store the one fingertip in the palm structure so we can track velocity
const float FINGER_LENGTH = 0.3f; // meters
const glm::vec3 FINGER_VECTOR(0.0f, 0.0f, FINGER_LENGTH);
const glm::vec3 newTipPosition = position + rotation * FINGER_VECTOR;
finger.setRawTipPosition(position + rotation * FINGER_VECTOR);
// Store the one fingertip in the palm structure so we can track velocity
glm::vec3 oldTipPosition = palm->getTipRawPosition();
if (deltaTime > 0.f) {
palm->setTipVelocity((newTipPosition - oldTipPosition) / deltaTime / 1000.f);
palm->setTipVelocity((newTipPosition - oldTipPosition) / deltaTime);
} else {
palm->setTipVelocity(glm::vec3(0.f));
}
palm->setTipPosition(newTipPosition);
// three fingers indicates to the skeleton that we have enough data to determine direction
palm->getFingers().clear();
palm->getFingers().push_back(finger);
palm->getFingers().push_back(finger);
palm->getFingers().push_back(finger);
}
if (numActiveControllers == 2) {
@ -171,7 +159,7 @@ void SixenseManager::update(float deltaTime) {
// if the controllers haven't been moved in a while, disable
const unsigned int MOVEMENT_DISABLE_SECONDS = 3;
if (usecTimestampNow() - _lastMovement > (MOVEMENT_DISABLE_SECONDS * 1000 * 1000)) {
if (usecTimestampNow() - _lastMovement > (MOVEMENT_DISABLE_SECONDS * USECS_PER_SECOND)) {
for (std::vector<PalmData>::iterator it = hand->getPalms().begin(); it != hand->getPalms().end(); it++) {
it->setActive(false);
}
@ -188,8 +176,8 @@ void SixenseManager::update(float deltaTime) {
// (4) move arms a bit forward (Z)
// (5) release BUTTON_FWD on both hands
const float MINIMUM_ARM_REACH = 300.f; // millimeters
const float MAXIMUM_NOISE_LEVEL = 50.f; // millimeters
const float MINIMUM_ARM_REACH = 0.3f; // meters
const float MAXIMUM_NOISE_LEVEL = 0.05f; // meters
const quint64 LOCK_DURATION = USECS_PER_SECOND / 4; // time for lock to be acquired
void SixenseManager::updateCalibration(const sixenseControllerData* controllers) {
@ -229,14 +217,17 @@ void SixenseManager::updateCalibration(const sixenseControllerData* controllers)
return;
}
// NOTE: Sixense API returns pos data in millimeters but we IMMEDIATELY convert to meters.
const float* pos = dataLeft->pos;
glm::vec3 positionLeft(pos[0], pos[1], pos[2]);
positionLeft *= METERS_PER_MILLIMETER;
pos = dataRight->pos;
glm::vec3 positionRight(pos[0], pos[1], pos[2]);
positionRight *= METERS_PER_MILLIMETER;
if (_calibrationState == CALIBRATION_STATE_IDLE) {
float reach = glm::distance(positionLeft, positionRight);
if (reach > 2.f * MINIMUM_ARM_REACH) {
if (reach > 2.0f * MINIMUM_ARM_REACH) {
qDebug("started: sixense calibration");
_averageLeft = positionLeft;
_averageRight = positionRight;

View file

@ -41,7 +41,11 @@ Visage::Visage() :
_headOrigin(DEFAULT_HEAD_ORIGIN) {
#ifdef HAVE_VISAGE
#ifdef WIN32
QByteArray licensePath = Application::resourcesPath().toLatin1() + "visage";
#else
QByteArray licensePath = Application::resourcesPath().toLatin1() + "visage/license.vlc";
#endif
initializeLicenseManager(licensePath.data());
_tracker = new VisageTracker2(Application::resourcesPath().toLatin1() + "visage/tracker.cfg");
_data = new FaceData();

View file

@ -128,6 +128,7 @@ QVector<Model::JointState> Model::createJointStates(const FBXGeometry& geometry)
jointIsSet.fill(false, numJoints);
int numJointsSet = 0;
int lastNumJointsSet = -1;
glm::mat4 baseTransform = glm::mat4_cast(_rotation) * glm::scale(_scale) * glm::translate(_offset);
while (numJointsSet < numJoints && numJointsSet != lastNumJointsSet) {
lastNumJointsSet = numJointsSet;
for (int i = 0; i < numJoints; ++i) {
@ -138,7 +139,6 @@ QVector<Model::JointState> Model::createJointStates(const FBXGeometry& geometry)
const FBXJoint& joint = geometry.joints[i];
int parentIndex = joint.parentIndex;
if (parentIndex == -1) {
glm::mat4 baseTransform = glm::mat4_cast(_rotation) * glm::scale(_scale) * glm::translate(_offset);
glm::quat combinedRotation = joint.preRotation * state.rotation * joint.postRotation;
state.transform = baseTransform * geometry.offset * glm::translate(state.translation) * joint.preTransform *
glm::mat4_cast(combinedRotation) * joint.postTransform;

View file

@ -200,7 +200,7 @@ glm::vec3 ControllerScriptingInterface::getSpatialControlNormal(int controlIndex
case PALM_SPATIALCONTROL:
return palmData->getNormal();
case TIP_SPATIALCONTROL:
return palmData->getNormal(); // currently the tip doesn't have a unique normal, use the palm normal
return palmData->getFingerDirection();
}
}
return glm::vec3(0); // bad index

View file

@ -59,7 +59,7 @@ void OAuthWebViewHandler::displayWebviewForAuthorizationURL(const QUrl& authoriz
_activeWebView = new QWebView;
// keep the window on top and delete it when it closes
_activeWebView->setWindowFlags(Qt::WindowStaysOnTopHint);
_activeWebView->setWindowFlags(Qt::Sheet | Qt::WindowStaysOnTopHint);
_activeWebView->setAttribute(Qt::WA_DeleteOnClose);
qDebug() << "Displaying QWebView for OAuth authorization at" << authorizationURL.toString();

View file

@ -157,6 +157,10 @@ void RunningScriptsWidget::paintEvent(QPaintEvent* event) {
painter.end();
}
void RunningScriptsWidget::scriptStopped(const QString& scriptName) {
_recentlyLoadedScripts.prepend(scriptName);
}
void RunningScriptsWidget::stopScript(int row, int column) {
if (column == 1) { // make sure the user has clicked on the close icon
_lastStoppedScript = _runningScriptsTable->item(row, 0)->toolTip();
@ -169,11 +173,6 @@ void RunningScriptsWidget::loadScript(int row, int column) {
}
void RunningScriptsWidget::allScriptsStopped() {
QStringList list = Application::getInstance()->getRunningScripts();
for (int i = 0; i < list.size(); ++i) {
_recentlyLoadedScripts.prepend(list.at(i));
}
Application::getInstance()->stopAllScripts();
}

View file

@ -36,6 +36,7 @@ protected:
virtual void paintEvent(QPaintEvent* event);
public slots:
void scriptStopped(const QString& scriptName);
void setBoundary(const QRect& rect);
private slots:

View file

@ -26,8 +26,8 @@ HandData::HandData(AvatarData* owningAvatar) :
addNewPalm();
}
glm::vec3 HandData::worldVectorToLeapVector(const glm::vec3& worldVector) const {
return glm::inverse(getBaseOrientation()) * worldVector / LEAP_UNIT_SCALE;
glm::vec3 HandData::worldToLocalVector(const glm::vec3& worldVector) const {
return glm::inverse(getBaseOrientation()) * worldVector;
}
PalmData& HandData::addNewPalm() {
@ -66,69 +66,21 @@ void HandData::getLeftRightPalmIndices(int& leftPalmIndex, int& rightPalmIndex)
PalmData::PalmData(HandData* owningHandData) :
_rawRotation(0.f, 0.f, 0.f, 1.f),
_rawPosition(0.f),
_rawNormal(0.f, 1.f, 0.f),
_rawVelocity(0.f),
_rotationalVelocity(0.f),
_totalPenetration(0.f),
_controllerButtons(0),
_isActive(false),
_leapID(LEAPID_INVALID),
_sixenseID(SIXENSEID_INVALID),
_numFramesWithoutData(0),
_owningHandData(owningHandData),
_isCollidingWithVoxel(false),
_isCollidingWithPalm(false),
_collisionlessPaddleExpiry(0)
{
for (int i = 0; i < NUM_FINGERS_PER_HAND; ++i) {
_fingers.push_back(FingerData(this, owningHandData));
}
_collisionlessPaddleExpiry(0) {
}
void PalmData::addToPosition(const glm::vec3& delta) {
// convert to Leap coordinates, then add to palm and finger positions
glm::vec3 leapDelta = _owningHandData->worldVectorToLeapVector(delta);
_rawPosition += leapDelta;
for (size_t i = 0; i < getNumFingers(); i++) {
FingerData& finger = _fingers[i];
if (finger.isActive()) {
finger.setRawTipPosition(finger.getTipRawPosition() + leapDelta);
finger.setRawRootPosition(finger.getRootRawPosition() + leapDelta);
}
}
}
FingerData::FingerData(PalmData* owningPalmData, HandData* owningHandData) :
_tipRawPosition(0, 0, 0),
_rootRawPosition(0, 0, 0),
_isActive(false),
_leapID(LEAPID_INVALID),
_numFramesWithoutData(0),
_owningPalmData(owningPalmData),
_owningHandData(owningHandData)
{
const int standardTrailLength = 10;
setTrailLength(standardTrailLength);
}
void HandData::setFingerTrailLength(unsigned int length) {
for (size_t i = 0; i < getNumPalms(); ++i) {
PalmData& palm = getPalms()[i];
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
FingerData& finger = palm.getFingers()[f];
finger.setTrailLength(length);
}
}
}
void HandData::updateFingerTrails() {
for (size_t i = 0; i < getNumPalms(); ++i) {
PalmData& palm = getPalms()[i];
for (size_t f = 0; f < palm.getNumFingers(); ++f) {
FingerData& finger = palm.getFingers()[f];
finger.updateTrail();
}
}
_rawPosition += _owningHandData->worldToLocalVector(delta);
}
bool HandData::findSpherePenetration(const glm::vec3& penetratorCenter, float penetratorRadius, glm::vec3& penetration,
@ -157,54 +109,20 @@ glm::vec3 HandData::getBasePosition() const {
return _owningAvatarData->getPosition();
}
void FingerData::setTrailLength(unsigned int length) {
_tipTrailPositions.resize(length);
_tipTrailCurrentStartIndex = 0;
_tipTrailCurrentValidLength = 0;
glm::vec3 PalmData::getFingerTipPosition() const {
glm::vec3 fingerOffset(0.0f, 0.0f, 0.3f);
glm::vec3 palmOffset(0.0f, -0.08f, 0.0f);
return getPosition() + _owningHandData->localToWorldDirection(_rawRotation * (fingerOffset + palmOffset));
}
void FingerData::updateTrail() {
if (_tipTrailPositions.size() == 0)
return;
if (_isActive) {
// Add the next point in the trail.
_tipTrailCurrentStartIndex--;
if (_tipTrailCurrentStartIndex < 0)
_tipTrailCurrentStartIndex = _tipTrailPositions.size() - 1;
_tipTrailPositions[_tipTrailCurrentStartIndex] = getTipPosition();
if (_tipTrailCurrentValidLength < (int)_tipTrailPositions.size())
_tipTrailCurrentValidLength++;
}
else {
// It's not active, so just kill the trail.
_tipTrailCurrentValidLength = 0;
}
glm::vec3 PalmData::getFingerDirection() const {
const glm::vec3 LOCAL_FINGER_DIRECTION(0.0f, 0.0f, 1.0f);
return _owningHandData->localToWorldDirection(_rawRotation * LOCAL_FINGER_DIRECTION);
}
int FingerData::getTrailNumPositions() {
return _tipTrailCurrentValidLength;
}
const glm::vec3& FingerData::getTrailPosition(int index) {
if (index >= _tipTrailCurrentValidLength) {
static glm::vec3 zero(0,0,0);
return zero;
}
int posIndex = (index + _tipTrailCurrentStartIndex) % _tipTrailCurrentValidLength;
return _tipTrailPositions[posIndex];
}
void PalmData::getBallHoldPosition(glm::vec3& position) const {
const float BALL_FORWARD_OFFSET = 0.08f; // put the ball a bit forward of fingers
position = BALL_FORWARD_OFFSET * getNormal();
if (_fingers.size() > 0) {
position += _fingers[0].getTipPosition();
} else {
position += getPosition();
}
glm::vec3 PalmData::getNormal() const {
const glm::vec3 LOCAL_PALM_DIRECTION(0.0f, -1.0f, 0.0f);
return _owningHandData->localToWorldDirection(_rawRotation * LOCAL_PALM_DIRECTION);
}

View file

@ -21,7 +21,6 @@
#include "SharedUtil.h"
class AvatarData;
class FingerData;
class PalmData;
const int NUM_HANDS = 2;
@ -31,8 +30,6 @@ const int NUM_FINGERS = NUM_HANDS * NUM_FINGERS_PER_HAND;
const int LEAPID_INVALID = -1;
const int SIXENSEID_INVALID = -1;
const float LEAP_UNIT_SCALE = 0.001f; ///< convert mm to meters
const int SIXENSE_CONTROLLER_ID_LEFT_HAND = 0;
const int SIXENSE_CONTROLLER_ID_RIGHT_HAND = 1;
@ -41,17 +38,16 @@ public:
HandData(AvatarData* owningAvatar);
virtual ~HandData() {}
// These methods return the positions in Leap-relative space.
// To convert to world coordinates, use Hand::leapPositionToWorldPosition.
// position conversion
glm::vec3 leapPositionToWorldPosition(const glm::vec3& leapPosition) {
return getBasePosition() + getBaseOrientation() * (leapPosition * LEAP_UNIT_SCALE);
glm::vec3 localToWorldPosition(const glm::vec3& localPosition) {
return getBasePosition() + getBaseOrientation() * localPosition;
}
glm::vec3 leapDirectionToWorldDirection(const glm::vec3& leapDirection) {
return getBaseOrientation() * leapDirection;
glm::vec3 localToWorldDirection(const glm::vec3& localVector) {
return getBaseOrientation() * localVector;
}
glm::vec3 worldVectorToLeapVector(const glm::vec3& worldVector) const;
glm::vec3 worldToLocalVector(const glm::vec3& worldVector) const;
std::vector<PalmData>& getPalms() { return _palms; }
const std::vector<PalmData>& getPalms() const { return _palms; }
@ -63,9 +59,6 @@ public:
/// both is not found.
void getLeftRightPalmIndices(int& leftPalmIndex, int& rightPalmIndex) const;
void setFingerTrailLength(unsigned int length);
void updateFingerTrails();
/// Checks for penetration between the described sphere and the hand.
/// \param penetratorCenter the center of the penetration test sphere
/// \param penetratorRadius the radius of the penetration test sphere
@ -89,71 +82,23 @@ private:
HandData& operator= (const HandData&);
};
class FingerData {
public:
FingerData(PalmData* owningPalmData, HandData* owningHandData);
glm::vec3 getTipPosition() const { return _owningHandData->leapPositionToWorldPosition(_tipRawPosition); }
glm::vec3 getRootPosition() const { return _owningHandData->leapPositionToWorldPosition(_rootRawPosition); }
const glm::vec3& getTipRawPosition() const { return _tipRawPosition; }
const glm::vec3& getRootRawPosition() const { return _rootRawPosition; }
bool isActive() const { return _isActive; }
int getLeapID() const { return _leapID; }
void setActive(bool active) { _isActive = active; }
void setLeapID(int id) { _leapID = id; }
void setRawTipPosition(const glm::vec3& pos) { _tipRawPosition = pos; }
void setRawRootPosition(const glm::vec3& pos) { _rootRawPosition = pos; }
void setTrailLength(unsigned int length);
void updateTrail();
int getTrailNumPositions();
const glm::vec3& getTrailPosition(int index);
void incrementFramesWithoutData() { _numFramesWithoutData++; }
void resetFramesWithoutData() { _numFramesWithoutData = 0; }
int getFramesWithoutData() const { return _numFramesWithoutData; }
private:
glm::vec3 _tipRawPosition;
glm::vec3 _rootRawPosition;
bool _isActive; // This has current valid data
int _leapID; // the Leap's serial id for this tracked object
int _numFramesWithoutData; // after too many frames without data, this tracked object assumed lost.
std::vector<glm::vec3> _tipTrailPositions;
int _tipTrailCurrentStartIndex;
int _tipTrailCurrentValidLength;
PalmData* _owningPalmData;
HandData* _owningHandData;
};
class PalmData {
public:
PalmData(HandData* owningHandData);
glm::vec3 getPosition() const { return _owningHandData->leapPositionToWorldPosition(_rawPosition); }
glm::vec3 getNormal() const { return _owningHandData->leapDirectionToWorldDirection(_rawNormal); }
glm::vec3 getVelocity() const { return _owningHandData->leapDirectionToWorldDirection(_rawVelocity); }
glm::vec3 getPosition() const { return _owningHandData->localToWorldPosition(_rawPosition); }
glm::vec3 getVelocity() const { return _owningHandData->localToWorldDirection(_rawVelocity); }
const glm::vec3& getRawPosition() const { return _rawPosition; }
const glm::vec3& getRawNormal() const { return _rawNormal; }
bool isActive() const { return _isActive; }
int getLeapID() const { return _leapID; }
int getSixenseID() const { return _sixenseID; }
std::vector<FingerData>& getFingers() { return _fingers; }
const std::vector<FingerData>& getFingers() const { return _fingers; }
size_t getNumFingers() const { return _fingers.size(); }
void setActive(bool active) { _isActive = active; }
void setLeapID(int id) { _leapID = id; }
void setSixenseID(int id) { _sixenseID = id; }
void setRawRotation(const glm::quat rawRotation) { _rawRotation = rawRotation; };
glm::quat getRawRotation() const { return _rawRotation; }
void setRawPosition(const glm::vec3& pos) { _rawPosition = pos; }
void setRawNormal(const glm::vec3& normal) { _rawNormal = normal; }
void setRawVelocity(const glm::vec3& velocity) { _rawVelocity = velocity; }
const glm::vec3& getRawVelocity() const { return _rawVelocity; }
void addToPosition(const glm::vec3& delta);
@ -162,11 +107,11 @@ public:
void resolvePenetrations() { addToPosition(-_totalPenetration); _totalPenetration = glm::vec3(0.f); }
void setTipPosition(const glm::vec3& position) { _tipPosition = position; }
const glm::vec3 getTipPosition() const { return _owningHandData->leapPositionToWorldPosition(_tipPosition); }
const glm::vec3 getTipPosition() const { return _owningHandData->localToWorldPosition(_tipPosition); }
const glm::vec3& getTipRawPosition() const { return _tipPosition; }
void setTipVelocity(const glm::vec3& velocity) { _tipVelocity = velocity; }
const glm::vec3 getTipVelocity() const { return _owningHandData->leapDirectionToWorldDirection(_tipVelocity); }
const glm::vec3 getTipVelocity() const { return _owningHandData->localToWorldDirection(_tipVelocity); }
const glm::vec3& getTipRawVelocity() const { return _tipVelocity; }
void incrementFramesWithoutData() { _numFramesWithoutData++; }
@ -198,11 +143,14 @@ public:
/// Store position where the palm holds the ball.
void getBallHoldPosition(glm::vec3& position) const;
// return world-frame:
glm::vec3 getFingerTipPosition() const;
glm::vec3 getFingerDirection() const;
glm::vec3 getNormal() const;
private:
std::vector<FingerData> _fingers;
glm::quat _rawRotation;
glm::vec3 _rawPosition;
glm::vec3 _rawNormal;
glm::vec3 _rawVelocity;
glm::vec3 _rotationalVelocity;
glm::quat _lastRotation;
@ -216,7 +164,6 @@ private:
float _joystickX, _joystickY;
bool _isActive; // This has current valid data
int _leapID; // the Leap's serial id for this tracked object
int _sixenseID; // Sixense controller ID for this palm
int _numFramesWithoutData; // after too many frames without data, this tracked object assumed lost.
HandData* _owningHandData;

View file

@ -76,11 +76,11 @@ void DataServerAccountInfo::setDiscourseApiKey(const QString& discourseApiKey) {
}
QDataStream& operator<<(QDataStream &out, const DataServerAccountInfo& info) {
out << info._accessToken << info._username << info._xmppPassword;
out << info._accessToken << info._username << info._xmppPassword << info._discourseApiKey;
return out;
}
QDataStream& operator>>(QDataStream &in, DataServerAccountInfo& info) {
in >> info._accessToken >> info._username >> info._xmppPassword;
in >> info._accessToken >> info._username >> info._xmppPassword >> info._discourseApiKey;
return in;
}

View file

@ -21,7 +21,7 @@
#include "DTLSClientSession.h"
#include "HifiSockAddr.h"
const QString DEFAULT_DOMAIN_HOSTNAME = "alpha.highfidelity.io";
const QString DEFAULT_DOMAIN_HOSTNAME = "sandbox.highfidelity.io";
const unsigned short DEFAULT_DOMAIN_SERVER_PORT = 40102;
const unsigned short DEFAULT_DOMAIN_SERVER_DTLS_PORT = 40103;

View file

@ -35,6 +35,7 @@
#include "MenuItemProperties.h"
#include "LocalVoxels.h"
#include "ScriptEngine.h"
#include "XMLHttpRequestClass.h"
VoxelsScriptingInterface ScriptEngine::_voxelsScriptingInterface;
ParticlesScriptingInterface ScriptEngine::_particlesScriptingInterface;
@ -49,7 +50,12 @@ static QScriptValue soundConstructor(QScriptContext* context, QScriptEngine* eng
static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine){
qDebug() << "script:print()<<" << context->argument(0).toString();
engine->evaluate("Script.print('" + context->argument(0).toString() + "')");
QString message = context->argument(0).toString()
.replace("\\", "\\\\")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("'", "\\'");
engine->evaluate("Script.print('" + message + "')");
return QScriptValue();
}
@ -224,6 +230,9 @@ void ScriptEngine::init() {
qScriptRegisterSequenceMetaType<QVector<glm::quat> >(&_engine);
qScriptRegisterSequenceMetaType<QVector<QString> >(&_engine);
QScriptValue xmlHttpRequestConstructorValue = _engine.newFunction(XMLHttpRequestClass::constructor);
_engine.globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue);
QScriptValue printConstructorValue = _engine.newFunction(debugPrint);
_engine.globalObject().setProperty("print", printConstructorValue);

View file

@ -0,0 +1,233 @@
//
// XMLHttpRequestClass.cpp
// libraries/script-engine/src/
//
// Created by Ryan Huffman on 5/2/14.
// Copyright (c) 2014 High Fidelity, Inc. All rights reserved.
//
// This class is an implementation of the XMLHttpRequest object for scripting use. It provides a near-complete implementation
// of the class described in the Mozilla docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QEventLoop>
#include "XMLHttpRequestClass.h"
XMLHttpRequestClass::XMLHttpRequestClass(QScriptEngine* engine) :
_engine(engine),
_async(true),
_url(),
_method(""),
_responseType(""),
_manager(this),
_request(),
_reply(NULL),
_sendData(NULL),
_rawResponseData(),
_responseData(""),
_onTimeout(QScriptValue::NullValue),
_onReadyStateChange(QScriptValue::NullValue),
_readyState(XMLHttpRequestClass::UNSENT),
_errorCode(QNetworkReply::NoError),
_timeout(0),
_timer(this),
_numRedirects(0) {
_timer.setSingleShot(true);
}
XMLHttpRequestClass::~XMLHttpRequestClass() {
if (_reply) { delete _reply; }
if (_sendData) { delete _sendData; }
}
QScriptValue XMLHttpRequestClass::constructor(QScriptContext* context, QScriptEngine* engine) {
return engine->newQObject(new XMLHttpRequestClass(engine));
}
QScriptValue XMLHttpRequestClass::getStatus() const {
if (_reply) {
return QScriptValue(_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
}
return QScriptValue(0);
}
QString XMLHttpRequestClass::getStatusText() const {
if (_reply) {
return _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
}
return "";
}
void XMLHttpRequestClass::abort() {
abortRequest();
}
void XMLHttpRequestClass::setRequestHeader(const QString& name, const QString& value) {
_request.setRawHeader(QByteArray(name.toLatin1()), QByteArray(value.toLatin1()));
}
void XMLHttpRequestClass::requestMetaDataChanged() {
QVariant redirect = _reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// If this is a redirect, abort the current request and start a new one
if (redirect.isValid() && _numRedirects < MAXIMUM_REDIRECTS) {
_numRedirects++;
abortRequest();
QUrl newUrl = _url.resolved(redirect.toUrl().toString());
_request.setUrl(newUrl);
doSend();
}
}
void XMLHttpRequestClass::requestDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
if (_readyState == OPENED && bytesReceived > 0) {
setReadyState(HEADERS_RECEIVED);
setReadyState(LOADING);
}
}
QScriptValue XMLHttpRequestClass::getAllResponseHeaders() const {
if (_reply) {
QList<QNetworkReply::RawHeaderPair> headerList = _reply->rawHeaderPairs();
QByteArray headers;
for (int i = 0; i < headerList.size(); i++) {
headers.append(headerList[i].first);
headers.append(": ");
headers.append(headerList[i].second);
headers.append("\n");
}
return QString(headers.data());
}
return QScriptValue("");
}
QScriptValue XMLHttpRequestClass::getResponseHeader(const QString& name) const {
if (_reply && _reply->hasRawHeader(name.toLatin1())) {
return QScriptValue(QString(_reply->rawHeader(name.toLatin1())));
}
return QScriptValue::NullValue;
}
void XMLHttpRequestClass::setReadyState(ReadyState readyState) {
if (readyState != _readyState) {
_readyState = readyState;
if (_onReadyStateChange.isFunction()) {
_onReadyStateChange.call(QScriptValue::NullValue);
}
}
}
void XMLHttpRequestClass::open(const QString& method, const QString& url, bool async, const QString& username,
const QString& password) {
if (_readyState == UNSENT) {
_async = async;
_url.setUrl(url);
if (!username.isEmpty()) {
_url.setUserName(username);
}
if (!password.isEmpty()) {
_url.setPassword(password);
}
_request.setUrl(_url);
_method = method;
setReadyState(OPENED);
}
}
void XMLHttpRequestClass::send() {
send(QString::Null());
}
void XMLHttpRequestClass::send(const QString& data) {
if (_readyState == OPENED && !_reply) {
if (!data.isNull()) {
_sendData = new QBuffer(this);
_sendData->setData(data.toUtf8());
}
doSend();
if (!_async) {
QEventLoop loop;
connect(this, SIGNAL(requestComplete()), &loop, SLOT(quit()));
loop.exec();
}
}
}
void XMLHttpRequestClass::doSend() {
_reply = _manager.sendCustomRequest(_request, _method.toLatin1(), _sendData);
connectToReply(_reply);
if (_timeout > 0) {
_timer.start(_timeout);
connect(&_timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));
}
}
void XMLHttpRequestClass::requestTimeout() {
if (_onTimeout.isFunction()) {
_onTimeout.call(QScriptValue::NullValue);
}
abortRequest();
_errorCode = QNetworkReply::TimeoutError;
setReadyState(DONE);
emit requestComplete();
}
void XMLHttpRequestClass::requestError(QNetworkReply::NetworkError code) {
}
void XMLHttpRequestClass::requestFinished() {
disconnect(&_timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));
_errorCode = _reply->error();
if (_errorCode == QNetworkReply::NoError) {
_rawResponseData.append(_reply->readAll());
if (_responseType == "json") {
_responseData = _engine->evaluate("(" + QString(_rawResponseData.data()) + ")");
if (_responseData.isError()) {
_engine->clearExceptions();
_responseData = QScriptValue::NullValue;
}
} else if (_responseType == "arraybuffer") {
_responseData = QScriptValue(_rawResponseData.data());
} else {
_responseData = QScriptValue(QString(_rawResponseData.data()));
}
}
setReadyState(DONE);
emit requestComplete();
}
void XMLHttpRequestClass::abortRequest() {
// Disconnect from signals we don't want to receive any longer.
disconnect(&_timer, SIGNAL(timeout()), this, SLOT(requestTimeout()));
if (_reply) {
disconnectFromReply(_reply);
_reply->abort();
delete _reply;
_reply = NULL;
}
}
void XMLHttpRequestClass::connectToReply(QNetworkReply* reply) {
connect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(requestDownloadProgress(qint64, qint64)));
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(requestMetaDataChanged()));
}
void XMLHttpRequestClass::disconnectFromReply(QNetworkReply* reply) {
disconnect(reply, SIGNAL(finished()), this, SLOT(requestFinished()));
disconnect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(requestError(QNetworkReply::NetworkError)));
disconnect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(requestDownloadProgress(qint64, qint64)));
disconnect(reply, SIGNAL(metaDataChanged()), this, SLOT(requestMetaDataChanged()));
}

View file

@ -0,0 +1,129 @@
//
// XMLHttpRequestClass.h
// libraries/script-engine/src/
//
// Created by Ryan Huffman on 5/2/14.
// Copyright (c) 2014 High Fidelity, Inc. All rights reserved.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_XMLHttpRequestClass_h
#define hifi_XMLHttpRequestClass_h
#include <QBuffer>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QObject>
#include <QScriptContext>
#include <QScriptEngine>
#include <QScriptValue>
#include <QTimer>
class XMLHttpRequestClass : public QObject {
Q_OBJECT
Q_PROPERTY(QScriptValue response READ getResponse)
Q_PROPERTY(QScriptValue responseText READ getResponseText)
Q_PROPERTY(QString responseType READ getResponseType WRITE setResponseType)
Q_PROPERTY(QScriptValue status READ getStatus)
Q_PROPERTY(QString statusText READ getStatusText)
Q_PROPERTY(QScriptValue readyState READ getReadyState)
Q_PROPERTY(QScriptValue errorCode READ getError)
Q_PROPERTY(int timeout READ getTimeout WRITE setTimeout)
Q_PROPERTY(int UNSENT READ getUnsent)
Q_PROPERTY(int OPENED READ getOpened)
Q_PROPERTY(int HEADERS_RECEIVED READ getHeadersReceived)
Q_PROPERTY(int LOADING READ getLoading)
Q_PROPERTY(int DONE READ getDone)
// Callbacks
Q_PROPERTY(QScriptValue ontimeout READ getOnTimeout WRITE setOnTimeout)
Q_PROPERTY(QScriptValue onreadystatechange READ getOnReadyStateChange WRITE setOnReadyStateChange)
public:
XMLHttpRequestClass(QScriptEngine* engine);
~XMLHttpRequestClass();
static const int MAXIMUM_REDIRECTS = 5;
enum ReadyState {
UNSENT = 0,
OPENED,
HEADERS_RECEIVED,
LOADING,
DONE
};
int getUnsent() const { return UNSENT; };
int getOpened() const { return OPENED; };
int getHeadersReceived() const { return HEADERS_RECEIVED; };
int getLoading() const { return LOADING; };
int getDone() const { return DONE; };
static QScriptValue constructor(QScriptContext* context, QScriptEngine* engine);
int getTimeout() const { return _timeout; }
void setTimeout(int timeout) { _timeout = timeout; }
QScriptValue getResponse() const { return _responseData; }
QScriptValue getResponseText() const { return QScriptValue(QString(_rawResponseData.data())); }
QString getResponseType() const { return _responseType; }
void setResponseType(const QString& responseType) { _responseType = responseType; }
QScriptValue getReadyState() const { return QScriptValue(_readyState); }
QScriptValue getError() const { return QScriptValue(_errorCode); }
QScriptValue getStatus() const;
QString getStatusText() const;
QScriptValue getOnTimeout() const { return _onTimeout; }
void setOnTimeout(QScriptValue function) { _onTimeout = function; }
QScriptValue getOnReadyStateChange() const { return _onReadyStateChange; }
void setOnReadyStateChange(QScriptValue function) { _onReadyStateChange = function; }
public slots:
void abort();
void setRequestHeader(const QString& name, const QString& value);
void open(const QString& method, const QString& url, bool async = true, const QString& username = "",
const QString& password = "");
void send();
void send(const QString& data);
QScriptValue getAllResponseHeaders() const;
QScriptValue getResponseHeader(const QString& name) const;
signals:
void requestComplete();
private:
void setReadyState(ReadyState readyState);
void doSend();
void connectToReply(QNetworkReply* reply);
void disconnectFromReply(QNetworkReply* reply);
void abortRequest();
QScriptEngine* _engine;
bool _async;
QUrl _url;
QString _method;
QString _responseType;
QNetworkAccessManager _manager;
QNetworkRequest _request;
QNetworkReply* _reply;
QBuffer* _sendData;
QByteArray _rawResponseData;
QScriptValue _responseData;
QScriptValue _onTimeout;
QScriptValue _onReadyStateChange;
ReadyState _readyState;
QNetworkReply::NetworkError _errorCode;
int _timeout;
QTimer _timer;
int _numRedirects;
private slots:
void requestFinished();
void requestError(QNetworkReply::NetworkError code);
void requestMetaDataChanged();
void requestDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void requestTimeout();
};
#endif // hifi_XMLHttpRequestClass_h

View file

@ -54,6 +54,7 @@ static const float SQUARE_ROOT_OF_3 = (float)sqrt(3.f);
static const float METERS_PER_DECIMETER = 0.1f;
static const float METERS_PER_CENTIMETER = 0.01f;
static const float METERS_PER_MILLIMETER = 0.001f;
static const float MILLIMETERS_PER_METER = 1000.0f;
static const quint64 USECS_PER_MSEC = 1000;
static const quint64 MSECS_PER_SECOND = 1000;
static const quint64 USECS_PER_SECOND = USECS_PER_MSEC * MSECS_PER_SECOND;