Fix cleanup of controller in tutorial

This commit is contained in:
Ryan Huffman 2016-09-21 16:47:25 -07:00
parent 1e0772d613
commit 2ea71f5e0b
4 changed files with 134 additions and 102 deletions

View file

@ -175,6 +175,7 @@ ControllerDisplay = function() {
};
deleteControllerDisplay = function(controllerDisplay) {
print("Deleting controller display");
for (var i = 0; i < controllerDisplay.overlays.length; ++i) {
Overlays.deleteOverlay(controllerDisplay.overlays[i]);
}

View file

@ -44,14 +44,14 @@ if (!Function.prototype.bind) {
var DEBUG = false;
function debug() {
if (DEBUG) {
print.apply(self, arguments);
print.apply(this, arguments);
}
}
var INFO = true;
function info() {
if (INFO) {
print.apply(self, arguments);
print.apply(this, arguments);
}
}
@ -247,7 +247,8 @@ var stepDisableControllers = function(name) {
}
stepDisableControllers.prototype = {
start: function(onFinish) {
editEntitiesWithTag('door', { visible: true });
controllerDisplayManager = new ControllerDisplayManager();
editEntitiesWithTag('door', { visible: true, collisionless: false });
Menu.setIsOptionChecked("Overlays", false);
Controller.disableMapping('handControllerPointer-click');
Messages.sendLocalMessage('Hifi-Advanced-Movement-Disabler', 'disable');
@ -268,24 +269,39 @@ stepDisableControllers.prototype = {
}
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// //
// STEP: ENABLE CONTROLLERS //
// //
///////////////////////////////////////////////////////////////////////////////
function reenableEverything() {
editEntitiesWithTag('door', { visible: false, collisionless: true });
Menu.setIsOptionChecked("Overlays", true);
Controller.enableMapping('handControllerPointer-click');
Messages.sendLocalMessage('Hifi-Advanced-Movement-Disabler', 'enable');
Messages.sendLocalMessage('Hifi-Teleport-Disabler', 'none');
Messages.sendLocalMessage('Hifi-Grab-Disable', JSON.stringify({
nearGrabEnabled: true,
holdEnabled: true,
farGrabEnabled: true,
}));
setControllerPartLayer('touchpad', 'blank');
setControllerPartLayer('tips', 'blank');
MyAvatar.shouldRenderLocally = true;
if (controllerDisplayManager) {
controllerDisplayManager.destroy();
controllerDisplayManager = null;
}
}
var stepEnableControllers = function(name) {
this.tag = name;
this.shouldLog = false;
}
stepEnableControllers.prototype = {
start: function(onFinish) {
editEntitiesWithTag('door', { visible: false });
Menu.setIsOptionChecked("Overlays", true);
Controller.enableMapping('handControllerPointer-click');
Messages.sendLocalMessage('Hifi-Advanced-Movement-Disabler', 'enable');
Messages.sendLocalMessage('Hifi-Teleport-Disabler', 'none');
Messages.sendLocalMessage('Hifi-Grab-Disable', JSON.stringify({
nearGrabEnabled: true,
holdEnabled: true,
farGrabEnabled: true,
}));
setControllerPartLayer('touchpad', 'blank');
setControllerPartLayer('tips', 'blank');
reenableEverything();
onFinish();
},
cleanup: function() {
@ -433,6 +449,11 @@ stepRaiseAboveHead.prototype = {
start: function(onFinish) {
var tag = this.tag;
var STATE_START = 0;
var STATE_HANDS_DOWN = 1;
var STATE_HANDS_UP = 2;
this.state = STATE_START;
var defaultTransform = {
position: {
x: 0.2459,
@ -451,19 +472,24 @@ stepRaiseAboveHead.prototype = {
editEntitiesWithTag(this.tag, { visible: true });
// Wait 2 seconds before starting to check for hands
this.waitTimeoutID = Script.setTimeout(function() {
this.checkIntervalID = null;
function checkForHandsAboveHead() {
debug("Raise above head: Checking for hands above head...");
this.checkIntervalID = null;
function checkForHandsAboveHead() {
debug("Raise above head: Checking hands...");
if (this.state == STATE_START) {
if (MyAvatar.getLeftPalmPosition().y < (MyAvatar.getHeadPosition().y - 0.1)) {
this.state = STATE_HANDS_DOWN;
}
} else if (this.state == STATE_HANDS_DOWN) {
if (MyAvatar.getLeftPalmPosition().y > (MyAvatar.getHeadPosition().y + 0.1)) {
this.state = STATE_HANDS_UP;
Script.clearInterval(this.checkIntervalID);
this.checkIntervalID = null;
playSuccessSound();
onFinish();
}
}
this.checkIntervalID = Script.setInterval(checkForHandsAboveHead.bind(this), 500);
}.bind(this), 2000);
}
this.checkIntervalID = Script.setInterval(checkForHandsAboveHead.bind(this), 500);
},
cleanup: function() {
if (this.checkIntervalID) {
@ -675,7 +701,8 @@ var stepEquip = function(name) {
this.tempTag = name + "-temporary";
this.PART1 = 0;
this.PART2 = 1;
this.COMPLETE = 2;
this.PART3 = 2;
this.COMPLETE = 3;
Messages.subscribe('Tutorial-Spinner');
Messages.messageReceived.connect(this.onMessage.bind(this));
@ -745,6 +772,7 @@ stepEquip.prototype = {
if (this.currentPart == this.PART1 && message == "wasLit") {
this.currentPart = this.PART2;
Script.setTimeout(function() {
this.currentPart = this.PART3;
hideEntitiesWithTag(this.tagPart1);
showEntitiesWithTag(this.tagPart2);
setControllerPartLayer('tips', 'grip');
@ -752,7 +780,7 @@ stepEquip.prototype = {
}.bind(this), 9000);
}
} else if (channel == "Hifi-Object-Manipulation") {
if (this.currentPart == this.PART2) {
if (this.currentPart == this.PART3) {
var data = parseJSON(message);
if (data.action == 'release' && data.grabbedEntity == this.gunID) {
info("got release");
@ -930,7 +958,7 @@ var stepFinish = function(name) {
}
stepFinish.prototype = {
start: function(onFinish) {
editEntitiesWithTag('door', { visible: false });
editEntitiesWithTag('door', { visible: false, collisonless: true });
showEntitiesWithTag(this.tag);
Settings.setValue("tutorialComplete", true);
onFinish();
@ -956,9 +984,6 @@ stepCleanupFinish.prototype = {
function showEntitiesWithTag(tag) {
editEntitiesWithTag(tag, function(entityID) {
var userData = Entities.getEntityProperties(entityID, "userData").userData;
@ -975,7 +1000,6 @@ function showEntitiesWithTag(tag) {
visible: data.visible == false ? false : true,
collisionless: collisionless,
userData: JSON.stringify(data),
//collisionless: data.collisionless == true ? true : false,
};
Entities.editEntity(entityID, newProperties);
});
@ -1029,8 +1053,6 @@ TutorialManager = function() {
for (var i = 0; i < STEPS.length; ++i) {
STEPS[i].cleanup();
}
//location = "/tutorial_begin";
//location = "/tutorial";
MyAvatar.shouldRenderLocally = false;
this.startNextStep();
}
@ -1078,7 +1100,12 @@ TutorialManager = function() {
if (currentStep) {
currentStep.cleanup();
}
reenableEverything();
currentStepNum = -1;
currentStep = null;
}
}
var tutorialManager = new TutorialManager();
tutorialManager.startTutorial();

View file

@ -48,8 +48,8 @@ if (!Function.prototype.bind) {
}
(function() {
Script.include("file:///C:/Users/Ryan/dev/hifi/tutorial/ownershipToken.js");
Script.include("file:///C:/Users/Ryan/dev/hifi/tutorial/tutorial.js");
Script.include("ownershipToken.js");
Script.include("tutorial.js");
var TutorialZone = function() {
this.token = null;

View file

@ -14,91 +14,101 @@ var zeroRotation = { x: 0, y: 0, z: 0, w: 1 };
///////////////////////////////////////////////////////////////////////////////
// Management of controller display //
///////////////////////////////////////////////////////////////////////////////
ControllerDisplayManager = function() {
var controllerDisplay = null;
var activeController = null;
var controllerCheckerIntervalID = null;
var controllerDisplay = null;
var activeController = null;
var controllerCheckerIntervalID = null;
function updateControllers() {
if (HMD.active) {
if ("Vive" in Controller.Hardware) {
if (!activeController) {
debug("Found vive!");
activeController = createControllerDisplay(VIVE_CONTROLLER_CONFIGURATION);
function updateControllers() {
if (HMD.active) {
if ("Vive" in Controller.Hardware) {
if (!activeController) {
debug("Found vive!");
activeController = createControllerDisplay(VIVE_CONTROLLER_CONFIGURATION);
}
// We've found the controllers, we no longer need to look for active controllers
if (controllerCheckerIntervalID) {
Script.clearInterval(controllerCheckerIntervalID);
controllerCheckerIntervalID = null;
}
} else {
debug("HMD active, but no controllers found");
if (activeController) {
deleteControllerDisplay(activeController);
activeController = null;
}
if (controllerCheckerIntervalID == null) {
controllerCheckerIntervalID = Script.setInterval(updateControllers, 1000);
}
}
// We've found the controllers, we no longer need to look for active controllers
} else {
debug("HMD inactive");
// We aren't in HMD mode, we no longer need to look for active controllers
if (controllerCheckerIntervalID) {
debug("Clearing controller checker interval");
Script.clearInterval(controllerCheckerIntervalID);
controllerCheckerIntervalID = null;
}
} else {
debug("HMD active, but no controllers found");
if (activeController) {
debug("Deleting controller");
deleteControllerDisplay(activeController);
activeController = null;
}
if (controllerCheckerIntervalID == null) {
controllerCheckerIntervalID = Script.setInterval(updateControllers, 1000);
}
}
} else {
debug("HMD inactive");
// We aren't in HMD mode, we no longer need to look for active controllers
if (controllerCheckerIntervalID) {
debug("Clearing controller checker interval");
Script.clearInterval(controllerCheckerIntervalID);
controllerCheckerIntervalID = null;
}
if (activeController) {
debug("Deleting controller");
deleteControllerDisplay(activeController);
activeController = null;
}
}
}
HMD.displayModeChanged.connect(updateControllers);
HMD.displayModeChanged.connect(updateControllers);
updateControllers();
updateControllers();
Messages.subscribe('Controller-Display');
var handleMessages = function(channel, message, sender) {
if (!activeController) {
return;
}
Messages.subscribe('Controller-Display');
var handleMessages = function(channel, message, sender) {
if (!activeController) {
return;
}
if (sender === MyAvatar.sessionUUID) {
if (channel === 'Controller-Display') {
debug('here');
var data = JSON.parse(message);
var name = data.name;
var visible = data.visible;
//c.setDisplayAnnotation(name, visible);
if (name in activeController.annotations) {
debug("hiding");
for (var i = 0; i < activeController.annotations[name].length; ++i) {
debug("hiding", i);
Overlays.editOverlay(activeController.annotations[name][i], { visible: visible });
if (sender === MyAvatar.sessionUUID) {
if (channel === 'Controller-Display') {
debug('here');
var data = JSON.parse(message);
var name = data.name;
var visible = data.visible;
//c.setDisplayAnnotation(name, visible);
if (name in activeController.annotations) {
debug("hiding");
for (var i = 0; i < activeController.annotations[name].length; ++i) {
debug("hiding", i);
Overlays.editOverlay(activeController.annotations[name][i], { visible: visible });
}
}
} else if (channel === 'Controller-Display-Parts') {
debug('here part');
var data = JSON.parse(message);
for (var name in data) {
var visible = data[name];
activeController.setPartVisible(name, visible);
}
} else if (channel === 'Controller-Set-Part-Layer') {
var data = JSON.parse(message);
for (var name in data) {
var layer = data[name];
activeController.setLayerForPart(name, layer);
}
}
} else if (channel === 'Controller-Display-Parts') {
debug('here part');
var data = JSON.parse(message);
for (var name in data) {
var visible = data[name];
activeController.setPartVisible(name, visible);
}
} else if (channel === 'Controller-Set-Part-Layer') {
var data = JSON.parse(message);
for (var name in data) {
var layer = data[name];
activeController.setLayerForPart(name, layer);
}
}
}
}
Messages.messageReceived.connect(handleMessages);
Messages.messageReceived.connect(handleMessages);
this.destroy = function() {
print("Destroying controller display");
Messages.messageReceived.disconnect(handleMessages);
if (activeController) {
deleteControllerDisplay(activeController);
}
};
}
//var c = setupController(TOUCH_CONTROLLER_CONFIGURATION);
//var c = createControllerDisplay(VIVE_CONTROLLER_CONFIGURATION);
@ -112,9 +122,3 @@ Messages.messageReceived.connect(handleMessages);
// c.setLayerForPart("touchpad", layers[num]);
//}, 2000);
//
Script.scriptEnding.connect(function() {
if (activeController) {
deleteControllerDisplay(activeController);
}
//MyAvatar.shouldRenderLocally = true;
});