media @ VU
[] readme course preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthoughts appendix references examples resources _

talk show tell print

extern-proto-clock.vr

extern-proto-clock.vr (wrl ) [ flux / bitmanagement / cortona / octaga ]


  #VRMLV2.0 utf8
  

proto-clock

interface


  EXTERNPROTO ClockMechanism [
          eventIn  SFInt32    set_hour
          eventIn  SFInt32    set_minute
          eventIn  SFInt32    set_second
          field    SFBool     autoClock
          field    SFInt32    hour
          field    SFInt32    minute
          field    SFInt32    second
          field    SFVec3f    hourHandAxis
          field    SFVec3f    minuteHandAxis
          field    SFVec3f    secondHandAxis
          field    SFBool     twentyFourHours
          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
                   field    SFInt32    min
                   field    SFInt32    sec
                   field    SFVec3f    hrAx
                   field    SFVec3f    minAx
                   field    SFVec3f    secAx
                   field    SFBool     do24
                   field    SFBool     auto
                   field    SFBool     first
                   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(); } }
  
  

public:


                      function set_time(tickTock) {
                         if(first || TRUE) {
                                 first = FALSE;
                                 // get time from computer, if autoClock is on
                                 if(auto) { autoSetTime(); }
                                 // or keep our own time
                                 else {
                                    sec ++;
                                    if(sec > 59) { sec = 0; min++; }
                                    if(min > 59) { min = 0; hr++;  }
                                    if(do24) { if(hr > 23) { hr = 0; } }
                                    else     { if(hr > 11) { hr = 0; } }
                                 }
                                 time2Rot();
                                 hour_changed = hr;
                                 minute_changed = min;
                                 second_changed = sec;
                         }
                         else { first = TRUE; }
                      }
                      function set_hour(h)   { hr  = h; }
                      function set_minute(m) { min = m; }
                      function set_second(s) { sec = s; }
                      "]
                }
             ]
     }
     ROUTE TS.cycleTime TO SCRIPT.set_time
  }
  
  


(C) A. Eliëns 21/5/2007

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.