Merge pull request #10555 from ctrlaltdavid/21372

Make stopwatch buttons work properly
This commit is contained in:
Andrew Meadows 2017-06-02 11:27:27 -07:00 committed by GitHub
commit d480123e93
5 changed files with 96 additions and 27 deletions

View file

@ -43,13 +43,47 @@ var minuteHandID = Entities.addEntity({
modelURL: Script.resolvePath("models/Stopwatch-min-hand.fbx"),
});
var startStopButtonID = Entities.addEntity({
type: "Model",
name: "stopwatch/startStop",
parentID: stopwatchID,
dimensions: Vec3.multiply(scale, { x: 0.8, y: 0.8, z: 1.0 }),
localPosition: Vec3.multiply(scale, { x: 0, y: -0.1, z: -2.06 }),
modelURL: Script.resolvePath("models/transparent-box.fbx")
});
var resetButtonID = Entities.addEntity({
type: "Model",
name: "stopwatch/startStop",
parentID: stopwatchID,
dimensions: Vec3.multiply(scale, { x: 0.6, y: 0.6, z: 0.8 }),
localPosition: Vec3.multiply(scale, { x: -1.5, y: -0.1, z: -1.2 }),
localRotation: Quat.fromVec3Degrees({ x: 0, y: 36, z: 0 }),
modelURL: Script.resolvePath("models/transparent-box.fbx")
});
Entities.editEntity(stopwatchID, {
userData: JSON.stringify({
secondHandID: secondHandID,
minuteHandID: minuteHandID,
minuteHandID: minuteHandID
}),
script: Script.resolvePath("stopwatchClient.js"),
serverScripts: Script.resolvePath("stopwatchServer.js")
});
Entities.editEntity(startStopButtonID, {
userData: JSON.stringify({
stopwatchID: stopwatchID,
grabbableKey: { wantsTrigger: true }
}),
script: Script.resolvePath("stopwatchStartStop.js")
});
Entities.editEntity(resetButtonID, {
userData: JSON.stringify({
stopwatchID: stopwatchID,
grabbableKey: { wantsTrigger: true }
}),
script: Script.resolvePath("stopwatchReset.js")
});
Script.stop()

View file

@ -0,0 +1,22 @@
//
// stopwatchReset.js
//
// Created by David Rowe on 26 May 2017.
// Copyright 2017 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
//
(function () {
this.preload = function (entityID) {
var properties = Entities.getEntityProperties(entityID, "userData");
this.messageChannel = "STOPWATCH-" + JSON.parse(properties.userData).stopwatchID;
};
function click() {
Messages.sendMessage(this.messageChannel, "reset");
}
this.startNearTrigger = click;
this.startFarTrigger = click;
this.clickDownOnEntity = click;
});

View file

@ -13,6 +13,7 @@
self.equipped = false;
self.isActive = false;
self.seconds = 0;
self.secondHandID = null;
self.minuteHandID = null;
@ -46,11 +47,19 @@
};
self.messageReceived = function(channel, message, sender) {
print("Message received", channel, sender, message);
if (channel === self.messageChannel && message === 'click') {
if (self.isActive) {
self.resetTimer();
} else {
self.startTimer();
if (channel === self.messageChannel) {
switch (message) {
case "startStop":
if (self.isActive) {
self.stopTimer();
} else {
self.startTimer();
}
break;
case "reset":
self.stopTimer();
self.resetTimer();
break;
}
}
};
@ -58,14 +67,7 @@
return Entities.getEntityProperties(self.entityID, "position").position;
};
self.resetTimer = function() {
print("Stopping stopwatch");
if (self.tickInjector) {
self.tickInjector.stop();
}
if (self.tickIntervalID !== null) {
Script.clearInterval(self.tickIntervalID);
self.tickIntervalID = null;
}
print("Resetting stopwatch");
Entities.editEntity(self.secondHandID, {
localRotation: Quat.fromPitchYawRollDegrees(0, 0, 0),
angularVelocity: { x: 0, y: 0, z: 0 },
@ -74,7 +76,7 @@
localRotation: Quat.fromPitchYawRollDegrees(0, 0, 0),
angularVelocity: { x: 0, y: 0, z: 0 },
});
self.isActive = false;
self.seconds = 0;
};
self.startTimer = function() {
print("Starting stopwatch");
@ -88,7 +90,6 @@
self.tickInjector.restart();
}
var seconds = 0;
self.tickIntervalID = Script.setInterval(function() {
if (self.tickInjector) {
self.tickInjector.setOptions({
@ -97,15 +98,15 @@
loop: true
});
}
seconds++;
self.seconds++;
const degreesPerTick = -360 / 60;
Entities.editEntity(self.secondHandID, {
localRotation: Quat.fromPitchYawRollDegrees(0, seconds * degreesPerTick, 0),
localRotation: Quat.fromPitchYawRollDegrees(0, self.seconds * degreesPerTick, 0),
});
if (seconds % 60 == 0) {
if (self.seconds % 60 == 0) {
Entities.editEntity(self.minuteHandID, {
localRotation: Quat.fromPitchYawRollDegrees(0, (seconds / 60) * degreesPerTick, 0),
localRotation: Quat.fromPitchYawRollDegrees(0, (self.seconds / 60) * degreesPerTick, 0),
});
Audio.playSound(self.chimeSound, {
position: self.getStopwatchPosition(),
@ -117,4 +118,15 @@
self.isActive = true;
};
self.stopTimer = function () {
print("Stopping stopwatch");
if (self.tickInjector) {
self.tickInjector.stop();
}
if (self.tickIntervalID !== null) {
Script.clearInterval(self.tickIntervalID);
self.tickIntervalID = null;
}
self.isActive = false;
};
});

View file

@ -1,20 +1,21 @@
//
// stopwatchServer.js
// stopwatchStartStop.js
//
// Created by Ryan Huffman on 1/20/17.
// Created by David Rowe on 26 May 2017.
// Copyright 2017 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
//
(function() {
(function () {
var messageChannel;
this.preload = function(entityID) {
this.messageChannel = "STOPWATCH-" + entityID;
this.preload = function (entityID) {
var properties = Entities.getEntityProperties(entityID, "userData");
this.messageChannel = "STOPWATCH-" + JSON.parse(properties.userData).stopwatchID;
};
function click() {
Messages.sendMessage(this.messageChannel, 'click');
Messages.sendMessage(this.messageChannel, "startStop");
}
this.startNearTrigger = click;
this.startFarTrigger = click;