Merge branch 'master' of https://github.com/worklist/hifi into modelserver

This commit is contained in:
ZappoMan 2014-05-14 12:21:44 -07:00
commit 18de6dddec
29 changed files with 1137 additions and 110 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

@ -9,11 +9,16 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
Script.include("toolBars.js");
var windowDimensions = Controller.getViewportDimensions();
var toolIconUrl = "http://highfidelity-public.s3-us-west-1.amazonaws.com/images/tools/";
var toolHeight = 50;
var toolWidth = 50;
var LASER_WIDTH = 4;
var LASER_COLOR = { red: 255, green: 0, blue: 0 };
var LASER_LENGTH_FACTOR = 1.5;
var LASER_LENGTH_FACTOR = 5;
var LEFT = 0;
var RIGHT = 1;
@ -33,24 +38,7 @@ var modelURLs = [
"http://highfidelity-public.s3-us-west-1.amazonaws.com/meshes/slimer.fbx",
];
var toolIconUrl = "http://highfidelity-public.s3-us-west-1.amazonaws.com/images/tools/";
var numberOfTools = 1;
var toolHeight = 50;
var toolWidth = 50;
var toolVerticalSpacing = 4;
var toolsHeight = toolHeight * numberOfTools + toolVerticalSpacing * (numberOfTools - 1);
var toolsX = windowDimensions.x - 8 - toolWidth;
var toolsY = (windowDimensions.y - toolsHeight) / 2;
var firstModel = Overlays.addOverlay("image", {
x: 0, y: 0, width: toolWidth, height: toolHeight,
subImage: { x: 0, y: toolHeight, width: toolWidth, height: toolHeight },
imageURL: toolIconUrl + "voxel-tool.svg",
x: toolsX, y: toolsY + ((toolHeight + toolVerticalSpacing) * 0), width: toolWidth, height: toolHeight,
visible: true,
alpha: 0.9
});
var toolBar;
function controller(wichSide) {
this.side = wichSide;
@ -206,6 +194,13 @@ function controller(wichSide) {
});
}
this.hideLaser = function() {
Overlays.editOverlay(this.laser, { visible: false });
Overlays.editOverlay(this.ball, { visible: false });
Overlays.editOverlay(this.leftRight, { visible: false });
Overlays.editOverlay(this.topDown, { visible: false });
}
this.moveModel = function () {
if (this.grabbing) {
var newPosition = Vec3.sum(this.palmPosition,
@ -345,67 +340,272 @@ function moveModels() {
rightController.moveModel();
}
var hydraConnected = false;
function checkController(deltaTime) {
var numberOfButtons = Controller.getNumberOfButtons();
var numberOfTriggers = Controller.getNumberOfTriggers();
var numberOfSpatialControls = Controller.getNumberOfSpatialControls();
var controllersPerTrigger = numberOfSpatialControls / numberOfTriggers;
moveOverlays();
// this is expected for hydras
if (!(numberOfButtons==12 && numberOfTriggers == 2 && controllersPerTrigger == 2)) {
//print("no hydra connected?");
return; // bail if no hydra
if (numberOfButtons==12 && numberOfTriggers == 2 && controllersPerTrigger == 2) {
if (!hydraConnected) {
hydraConnected = true;
}
leftController.update();
rightController.update();
moveModels();
} else {
if (hydraConnected) {
hydraConnected = false;
leftController.hideLaser();
rightController.hideLaser();
}
}
leftController.update();
rightController.update();
moveModels();
moveOverlays();
}
function initToolBar() {
toolBar = new ToolBar(0, 0, ToolBar.VERTICAL);
// New Model
newModel = toolBar.addTool({
imageURL: toolIconUrl + "voxel-tool.svg",
subImage: { x: 0, y: Tool.IMAGE_WIDTH, width: Tool.IMAGE_WIDTH, height: Tool.IMAGE_HEIGHT },
width: toolWidth, height: toolHeight,
visible: true,
alpha: 0.9
});
}
function moveOverlays() {
windowDimensions = Controller.getViewportDimensions();
toolsX = windowDimensions.x - 8 - toolWidth;
toolsY = (windowDimensions.y - toolsHeight) / 2;
Overlays.editOverlay(firstModel, {
x: toolsX, y: toolsY + ((toolHeight + toolVerticalSpacing) * 0), width: toolWidth, height: toolHeight,
});
}
function mousePressEvent(event) {
var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y});
var url;
if (clickedOverlay == firstModel) {
url = Window.prompt("Model url", modelURLs[Math.floor(Math.random() * modelURLs.length)]);
if (url == null) {
return; }
} else {
print("Didn't click on anything");
if (typeof(toolBar) === 'undefined') {
initToolBar();
} else if (windowDimensions.x == Controller.getViewportDimensions().x &&
windowDimensions.y == Controller.getViewportDimensions().y) {
return;
}
var position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getFront(MyAvatar.orientation), SPAWN_DISTANCE));
Models.addModel({ position: position,
radius: radiusDefault,
modelURL: url
});
windowDimensions = Controller.getViewportDimensions();
var toolsX = windowDimensions.x - 8 - toolBar.width;
var toolsY = (windowDimensions.y - toolBar.height) / 2;
toolBar.move(toolsX, toolsY);
}
var modelSelected = false;
var selectedModelID;
var selectedModelProperties;
var mouseLastPosition;
var orientation;
var intersection;
var SCALE_FACTOR = 200.0;
var TRANSLATION_FACTOR = 100.0;
var ROTATION_FACTOR = 100.0;
function rayPlaneIntersection(pickRay, point, normal) {
var d = -Vec3.dot(point, normal);
var t = -(Vec3.dot(pickRay.origin, normal) + d) / Vec3.dot(pickRay.direction, normal);
return Vec3.sum(pickRay.origin, Vec3.multiply(pickRay.direction, t));
}
function mousePressEvent(event) {
mouseLastPosition = { x: event.x, y: event.y };
modelSelected = false;
var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y});
if (newModel == toolBar.clicked(clickedOverlay)) {
var url = Window.prompt("Model url", modelURLs[Math.floor(Math.random() * modelURLs.length)]);
if (url == null) {
return;
}
var position = Vec3.sum(MyAvatar.position, Vec3.multiply(Quat.getFront(MyAvatar.orientation), SPAWN_DISTANCE));
Models.addModel({ position: position,
radius: radiusDefault,
modelURL: url
});
} else {
var pickRay = Camera.computePickRay(event.x, event.y);
Vec3.print("[Mouse] Looking at: ", pickRay.origin);
var foundModels = Models.findModels(pickRay.origin, LASER_LENGTH_FACTOR);
for (var i = 0; i < foundModels.length; i++) {
if (!foundModels[i].isKnownID) {
var identify = Models.identifyModel(foundModels[i]);
if (!identify.isKnownID) {
print("Unknown ID " + identify.id + "(update loop)");
continue;
}
foundModels[i] = identify;
}
var properties = Models.getModelProperties(foundModels[i]);
print("Checking properties: " + properties.id + " " + properties.isKnownID);
// P P - Model
// /| A - Palm
// / | d B - unit vector toward tip
// / | X - base of the perpendicular line
// A---X----->B d - distance fom axis
// x x - distance from A
//
// |X-A| = (P-A).B
// X == A + ((P-A).B)B
// d = |P-X|
var A = pickRay.origin;
var B = Vec3.normalize(pickRay.direction);
var P = properties.position;
var x = Vec3.dot(Vec3.subtract(P, A), B);
var X = Vec3.sum(A, Vec3.multiply(B, x));
var d = Vec3.length(Vec3.subtract(P, X));
if (d < properties.radius && 0 < x && x < LASER_LENGTH_FACTOR) {
modelSelected = true;
selectedModelID = foundModels[i];
selectedModelProperties = properties;
selectedModelProperties.oldRadius = selectedModelProperties.radius;
selectedModelProperties.oldPosition = {
x: selectedModelProperties.position.x,
y: selectedModelProperties.position.y,
z: selectedModelProperties.position.z,
};
selectedModelProperties.oldRotation = {
x: selectedModelProperties.modelRotation.x,
y: selectedModelProperties.modelRotation.y,
z: selectedModelProperties.modelRotation.z,
w: selectedModelProperties.modelRotation.w,
};
orientation = MyAvatar.orientation;
intersection = rayPlaneIntersection(pickRay, P, Quat.getFront(orientation));
print("Clicked on " + selectedModelID.id + " " + modelSelected);
return;
}
}
}
}
var oldModifier = 0;
var modifier = 0;
var wasShifted = false;
function mouseMoveEvent(event) {
if (!modelSelected) {
return;
}
if (event.isLeftButton) {
if (event.isRightButton) {
modifier = 1; // Scale
} else {
modifier = 2; // Translate
}
} else if (event.isRightButton) {
modifier = 3; // rotate
} else {
modifier = 0;
}
var pickRay = Camera.computePickRay(event.x, event.y);
if (wasShifted != event.isShifted || modifier != oldModifier) {
selectedModelProperties.oldRadius = selectedModelProperties.radius;
selectedModelProperties.oldPosition = {
x: selectedModelProperties.position.x,
y: selectedModelProperties.position.y,
z: selectedModelProperties.position.z,
};
selectedModelProperties.oldRotation = {
x: selectedModelProperties.modelRotation.x,
y: selectedModelProperties.modelRotation.y,
z: selectedModelProperties.modelRotation.z,
w: selectedModelProperties.modelRotation.w,
};
orientation = MyAvatar.orientation;
intersection = rayPlaneIntersection(pickRay,
selectedModelProperties.oldPosition,
Quat.getFront(orientation));
mouseLastPosition = { x: event.x, y: event.y };
wasShifted = event.isShifted;
oldModifier = modifier;
return;
}
switch (modifier) {
case 0:
return;
case 1:
// Let's Scale
selectedModelProperties.radius = (selectedModelProperties.oldRadius *
(1.0 + (mouseLastPosition.y - event.y) / SCALE_FACTOR));
if (selectedModelProperties.radius < 0.01) {
print("Scale too small ... bailling.");
return;
}
break;
case 2:
// Let's translate
var newIntersection = rayPlaneIntersection(pickRay,
selectedModelProperties.oldPosition,
Quat.getFront(orientation));
var vector = Vec3.subtract(newIntersection, intersection)
if (event.isShifted) {
var i = Vec3.dot(vector, Quat.getRight(orientation));
var j = Vec3.dot(vector, Quat.getUp(orientation));
vector = Vec3.sum(Vec3.multiply(Quat.getRight(orientation), i),
Vec3.multiply(Quat.getFront(orientation), j));
}
selectedModelProperties.position = Vec3.sum(selectedModelProperties.oldPosition, vector);
break;
case 3:
// Let's rotate
var rotation = Quat.fromVec3Degrees({ x: event.y - mouseLastPosition.y, y: event.x - mouseLastPosition.x, z: 0 });
if (event.isShifted) {
rotation = Quat.fromVec3Degrees({ x: event.y - mouseLastPosition.y, y: 0, z: mouseLastPosition.x - event.x });
}
var newRotation = Quat.multiply(orientation, rotation);
newRotation = Quat.multiply(newRotation, Quat.inverse(orientation));
selectedModelProperties.modelRotation = Quat.multiply(newRotation, selectedModelProperties.oldRotation);
break;
}
Models.editModel(selectedModelID, selectedModelProperties);
}
function scriptEnding() {
leftController.cleanup();
rightController.cleanup();
Overlays.deleteOverlay(firstModel);
toolBar.cleanup();
}
Script.scriptEnding.connect(scriptEnding);
// register the call back so it fires before each data send
Script.update.connect(checkController);
Controller.mousePressEvent.connect(mousePressEvent);
Controller.mouseMoveEvent.connect(mouseMoveEvent);

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");
});

197
examples/toolBars.js Normal file
View file

@ -0,0 +1,197 @@
//
// toolBars.js
// examples
//
// Created by Clément Brisset on 5/7/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
Overlay2D = function(properties, overlay) { // overlay is an optionnal variable
if (!(typeof(properties) === 'undefined')) {
if(typeof(overlay) === 'undefined') {
overlay = Overlays.addOverlay("image", properties);
} else {
Overlays.editOverlay(overlay, properties);
}
}
this.overlay = function() {
return overlay;
}
this.x = function() {
return properties.x;
}
this.y = function() {
return properties.y;
}
this.width = function() {
return properties.width;
}
this.height = function() {
return properties.height;
}
this.alpha = function() {
return properties.alpha;
}
this.visible = function() {
return properties.visible;
}
this.move = function(x, y) {
properties.x = x;
properties.y = y;
Overlays.editOverlay(overlay, { x: x, y: y });
}
this.resize = function(width, height) {
properties.width = width;
properties.height = height;
Overlays.editOverlay(overlay, { width: width, height: height });
}
this.setAlpha = function(alpha) {
properties.alpha = alpha;
Overlays.editOverlay(overlay, { alpha: alpha });
}
this.show = function(doShow) {
properties.visible = doShow;
Overlays.editOverlay(overlay, { visible: doShow });
}
this.clicked = function(clickedOverlay) {
return (overlay == clickedOverlay ? true : false);
}
this.cleanup = function() {
print("Cleanup");
Overlays.deleteOverlay(overlay);
}
}
Tool = function(properties, selectable, selected) { // selectable and selected are optional variables.
Overlay2D.call(this, properties);
if(typeof(selectable)==='undefined') {
selectable = false;
if(typeof(selected)==='undefined') {
selected = false;
}
}
this.selectable = function() {
return selectable;
}
this.selected = function() {
return selected;
}
this.select = function(doSelect) {
selected = doSelect;
properties.subImage.y = (selected ? 2 : 1) * properties.subImage.height;
Overlays.editOverlay(this.overlay(), { subImage: properties.subImage });
}
this.toggle = function() {
selected = !selected;
properties.subImage.y = (selected ? 2 : 1) * properties.subImage.height;
Overlays.editOverlay(this.overlay(), { subImage: properties.subImage });
return selected;
}
this.select(selected);
this.baseClicked = this.clicked;
this.clicked = function(clickedOverlay) {
if (this.baseClicked(clickedOverlay)) {
if (selectable) {
this.toggle();
}
return true;
}
return false;
}
}
Tool.prototype = new Overlay2D;
Tool.IMAGE_HEIGHT = 50;
Tool.IMAGE_WIDTH = 50;
ToolBar = function(x, y, direction) {
this.tools = [];
this.x = x;
this.y = y;
this.width = 0;
this.height = 0;
this.addTool = function(properties, selectable, selected) {
if (direction == ToolBar.HORIZONTAL) {
properties.x = this.x + this.width;
properties.y = this.y;
this.width += properties.width + ToolBar.SPACING;
this.height += Math.max(properties.height, this.height);
} else {
properties.x = this.x;
properties.y = this.y + this.height;
this.width = Math.max(properties.width, this.width);
this.height += properties.height + ToolBar.SPACING;
}
this.tools[this.tools.length] = new Tool(properties, selectable, selected);
return ((this.tools.length) - 1);
}
this.move = function(x, y) {
var dx = x - this.x;
var dy = y - this.y;
this.x = x;
this.y = y;
for(var tool in this.tools) {
this.tools[tool].move(this.tools[tool].x() + dx, this.tools[tool].y() + dy);
}
}
this.setAlpha = function(alpha) {
for(var tool in this.tools) {
this.tools[tool].setAlpha(alpha);
}
}
this.show = function(doShow) {
for(var tool in this.tools) {
this.tools[tool].show(doShow);
}
}
this.clicked = function(clickedOverlay) {
for(var tool in this.tools) {
if (this.tools[tool].visible() && this.tools[tool].clicked(clickedOverlay)) {
return parseInt(tool);
}
}
return -1;
}
this.numberOfTools = function() {
return this.tools.length;
}
this.cleanup = function() {
for(var tool in this.tools) {
this.tools[tool].cleanup();
delete this.tools[tool];
}
this.tools = [];
this.x = x;
this.y = y;
this.width = 0;
this.height = 0;
}
}
ToolBar.SPACING = 4;
ToolBar.VERTICAL = 0;
ToolBar.HORIZONTAL = 1;

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

@ -622,7 +622,7 @@ bool Avatar::findParticleCollisions(const glm::vec3& particleCenter, float parti
glm::vec3 fingerAxis = palm->getFingerDirection();
glm::vec3 diskCenter = handPosition + HAND_PADDLE_OFFSET * fingerAxis;
glm::vec3 diskNormal = palm->getPalmDirection();
glm::vec3 diskNormal = palm->getNormal();
const float DISK_THICKNESS = 0.08f;
// collide against the disk

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;
@ -201,7 +200,7 @@ void Hand::renderHandTargets(bool isMine) {
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.getPalmDirection() * PALM_DISK_THICKNESS;
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);

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

@ -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,19 +148,17 @@ 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);
} else {
getJointRotation(jointIndex, palmRotation, true);
}
palmRotation = rotationBetween(palmRotation * geometry.palmDirection, palm.getPalmDirection()) * palmRotation;
palmRotation = rotationBetween(palmRotation * geometry.palmDirection, palm.getNormal()) * palmRotation;
// rotate forearm according to average finger direction
// NOTE: we're doing this in the avatar local frame, so we DON'T want to use Palm::getHandDirection()
// which returns the world-frame.
glm::vec3 direction = palm.getRawRotation() * glm::vec3(0.0f, 0.0f, 1.0f);
// 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

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

@ -106,7 +106,7 @@ void SixenseManager::update(float deltaTime) {
palm->setControllerButtons(data->buttons);
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;

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

@ -198,7 +198,7 @@ glm::vec3 ControllerScriptingInterface::getSpatialControlNormal(int controlIndex
if (palmData) {
switch (controlOfPalm) {
case PALM_SPATIALCONTROL:
return palmData->getPalmDirection();
return palmData->getNormal();
case TIP_SPATIALCONTROL:
return palmData->getFingerDirection();
}

View file

@ -78,6 +78,7 @@ QScriptValue WindowScriptingInterface::showPrompt(const QString& message, const
promptDialog.setWindowTitle("");
promptDialog.setLabelText(message);
promptDialog.setTextValue(defaultText);
promptDialog.setFixedSize(600, 200);
if (promptDialog.exec() == QDialog::Accepted) {
return QScriptValue(promptDialog.textValue());

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

@ -114,13 +114,13 @@ glm::vec3 PalmData::getFingerTipPosition() const {
glm::vec3 palmOffset(0.0f, -0.08f, 0.0f);
return getPosition() + _owningHandData->localToWorldDirection(_rawRotation * (fingerOffset + palmOffset));
}
glm::vec3 PalmData::getFingerDirection() const {
const glm::vec3 LOCAL_FINGER_DIRECTION(0.0f, 0.0f, 1.0f);
return _owningHandData->localToWorldDirection(_rawRotation * LOCAL_FINGER_DIRECTION);
}
glm::vec3 PalmData::getPalmDirection() const {
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

@ -45,7 +45,7 @@ public:
glm::vec3 localToWorldDirection(const glm::vec3& localVector) {
return getBaseOrientation() * localVector;
}
}
glm::vec3 worldToLocalVector(const glm::vec3& worldVector) const;
@ -146,7 +146,7 @@ public:
// return world-frame:
glm::vec3 getFingerTipPosition() const;
glm::vec3 getFingerDirection() const;
glm::vec3 getPalmDirection() const;
glm::vec3 getNormal() const;
private:
glm::quat _rawRotation;

View file

@ -105,7 +105,6 @@ ModelItemID ModelsScriptingInterface::editModel(ModelItemID modelID, const Model
_modelTree->updateModel(modelID, properties);
_modelTree->unlock();
}
return modelID;
}

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