maandag 21 september 2015

gauge clock2

Created another simple gauge class (GaugeII), then used it for a clock



code:

GaugeII second, minute, hour;
int secold, minold, hourold;

void setup(){
  size(800, 270);
  second = new GaugeII(250, 0, 59, 5, "second"); //(width, begin, end, division lines, name gauge)
  minute = new GaugeII(250, 0, 59, 5, "minute");
  hour = new GaugeII(250, 0, 11, 3,  "hour");
  secold = 100;  //val can never be 100, first run will always draw
  minold = 100;
  hourold = 100;
}

void draw(){
  //to save resource, only redraw when time has changed
  if (second() != secold){
    second.update(second());
    pushMatrix();
    translate(530, 10);
    second.display();
    popMatrix();
    secold = second();
  }
  if (minute() != minold){
    minute.update(minute());
    pushMatrix();
    translate(270, 10);
    minute.display();
    popMatrix();
    minold = minute();
  }
  if (hour() != hourold){
    //time received is 24 hour notation, gauge has scale of 12
    if (hour() <= 11){
      hour.update(hour());
    } else {
      hour.update(hour()-12);
    }
    pushMatrix();
    translate(10, 10);
    hour.display();
    popMatrix();
    hourold = hour();
  }
}


class GaugeII{
  PVector gsize;    //gauge(size, rotationpoint needle/scale)
  PVector gneedle;  //needle(size, angle)
  PVector gscale;   //scale(start, stop)
  int gsteps;       //scale, number of steps
  int gline;        //scale, division lines
  String gname;     //name gauge
 
  GaugeII(int temp_size, float temp_start, float temp_stop, int temp_line, String temp_name){
    gsize = new PVector(temp_size, temp_size-(temp_size/10));
    gneedle = new PVector(-7*temp_size/10, 0);
    gscale = new PVector(temp_start, temp_stop);
    gline = temp_line;
    gsteps = int(temp_stop - temp_start)+1;
    gname = temp_name;
  }
 
  void display(){
    //back
    stroke(0);
    strokeWeight(5);
    fill(255);
    rect(0, 0, gsize.x, gsize.x);
    fill(0);
    textAlign(CENTER);
    textSize(14);
    text(gname, gsize.x/4, 20);
   
    //scale
    stroke(0);
    strokeWeight(2);
    for (int i = 0; i < gsteps; i++){
      pushMatrix();
      translate(gsize.y, gsize.y);
      rotate(map(i, gscale.x, gscale.y, 0, HALF_PI));
      if (i%gline == 0){
        line(gneedle.x-5, 0, gneedle.x+5, 0);
        fill(0);
        translate(gneedle.x-10, 0);
        rotate(-HALF_PI);
        textSize(9);
        text(i, 0, 0);
      } else {
        point(gneedle.x, 0);
      } 
      popMatrix();
    }
   
    //needle
    pushMatrix();
    translate(gsize.y, gsize.y);
    rotate(gneedle.y);
    noStroke();
    fill(255, 0, 0);
    ellipseMode(CENTER);
    ellipse(0, 0, 15, 15);
    stroke(255, 0, 0);
    strokeWeight(2);
    line(0, 0, gneedle.x, 0);
    noStroke();
    popMatrix();
  }
 
  void update(float tempval){
    float gvalue = tempval;
    gneedle.y = map(gvalue, gscale.x, gscale.y, 0, HALF_PI);
  }
}

Geen opmerkingen:

Een reactie posten