Proto
PROTO ClockMechanism [
eventIn SFInt32 set_hour
eventIn SFInt32 set_minute
eventIn SFInt32 set_second
field SFBool autoClock TRUE
field SFInt32 hour 12
field SFInt32 minute 0
field SFInt32 second 0
field SFVec3f hourHandAxis 0 0 1
field SFVec3f minuteHandAxis 0 0 1
field SFVec3f secondHandAxis 0 0 1
field SFBool twentyFourHours FALSE
eventOut SFInt32 hour_changed
eventOut SFInt32 minute_changed
eventOut SFInt32 second_changed
eventOut SFRotation hourHand_changed
eventOut SFRotation minuteHand_changed
eventOut SFRotation secondHand_changed
]
{
Group {
children [
DEF TS TimeSensor { loop TRUE }
DEF SCRIPT Script {
eventIn SFInt32 set_hour IS set_hour
eventIn SFInt32 set_minute IS set_minute
eventIn SFInt32 set_second IS set_second
eventIn SFTime set_time
field SFInt32 hr IS hour
field SFInt32 min IS minute
field SFInt32 sec IS second
field SFVec3f hrAx IS hourHandAxis
field SFVec3f minAx IS minuteHandAxis
field SFVec3f secAx IS secondHandAxis
field SFBool do24 IS twentyFourHours
field SFBool auto IS autoClock
field SFBool first TRUE
eventOut SFRotation hourHand_changed IS hourHand_changed
eventOut SFRotation minuteHand_changed IS minuteHand_changed
eventOut SFRotation secondHand_changed IS secondHand_changed
url["javascript:
// private:
function autoSetTime() {
currentDate = new Date();
hr = currentDate.getHours();
min = currentDate.getMinutes();
sec = currentDate.getSeconds();
}
function time2Rot() {
// set some constants
PIPI = Math.PI * 2;
numHrs = 12;
if(do24) { numHrs = 24; }
// find radians (what portion of a full circle-- x * 2PI; x: 0 -> 1)
secRad = (sec / 60) * PIPI;
minRad = (min / 60) * PIPI;
hrRad = (hr / numHrs) * PIPI;
// adjust minute and hour hands for smooth interpolation
minRad += secRad / 60;
hrRad += minRad / numHrs;
// create the new rotations
secondHand_changed = new SFRotation(secAx, -secRad);
minuteHand_changed = new SFRotation(minAx, -minRad);
hourHand_changed = new SFRotation(hrAx, -hrRad);
}
function initialize() { if(auto) { autoSetTime(); } }