> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.AAhJ0YEP$pF/rev.1371
 * 
 * authors: 
 *   Marco Torchiano

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



int n = 9;  // last mark at..
boolean[] hops;
boolean[] marks;
int step;
int x0;
int y0;
int freeX;
int addX;
int topY;
int a,b;

final String STR_FREE = "Libero";
final String STR_ADD = "Addizione";
final String STR_CORRECT = "ESATTO!";

final int EVENT_HOP = 1;
final int EVENT_MARK = 2;
final int INIT = -1;
final int COMPLETE = -2;
final int FREE = -3;

int status = FREE;

void setup() {
  size(600,400);
  frameRate(10);
  strokeWeight(4);
  hops = new boolean[n];
  marks = new boolean[n];
}

void draw() {
  background(255, 204, 0);
  PFont font = loadFont("Helvetica");
  textFont(font);

  // Top MODE buttons
  textSize(20);
  topY=textAscent()+5;
  freeX = 5 + textWidth(STR_FREE);
  addX = width-5-textWidth(STR_ADD);
  textAlign(LEFT);
  if(status==FREE){
      fill(50,255,50);
      text(STR_FREE,5,topY);
      fill(50,50,50);
      text(STR_ADD,addX, topY);
  }else{
      fill(50,50,50);
      text(STR_FREE,5,topY);
      fill(50,255,50);
      text(STR_ADD,addX, topY);
      
      fill(0);
      textAlign(CENTER);
      textSize(30);
      if(status==COMPLETE){
          fill(255,50,50);
          text(a + " + " + b + " = " + (a+b),width/2,height/3);
          text(STR_CORRECT,width/2,height/3+1.5*textAscent());
      }else{
          text(a + " + " + b + " = ?",width/2,height/3);
      }
  }
  
  x0 = width * 0.05;
  y0 = height *2 / 3;
  step = (width * 0.8)/n;
  var v = step / 3;
  textFont(font,16);
  textAlign(CENTER);
  fill(0);
  
  var ascent = textAscent();

  noFill();

  stroke(0,156,255);
  strokeWeight(4);
  line(x0,y0,width-x0,y0);
  line(width-x0,y0,width-2*x0,y0-x0/2);
  line(width-x0,y0,width-2*x0,y0+x0/2);

  for(var i=0; i<=n; i=i+1){
    stroke(0,156,255);
    strokeWeight(4);
    line(x0+i*step,y0,x0+i*step,y0+v);
    text(i,x0+i*step,y0+v+ascent*1.5);
    if(marks[i]){
        stroke(255,50,50);
        strokeWeight(2);
        var tw = textWidth(""&i);
        ellipse(x0+i*step,y0+v+ascent,2*tw,2*ascent);
    }

    if(i<n){
        if(hops[i]){
          stroke(255,50,50);
          strokeWeight(2);
          arc(x0+i*step+step/2,y0,step,step,PI,2*PI);
        }else{
          stroke(0);
          strokeWeight(0.1);
          arc(x0+i*step+step/2,y0,step,step,PI,2*PI);
        }
    }
  }
}

void reset(){
    for(int i=0; i<=n; i++){
        marks[i] = false;
        hops[i] = false;
    }
}

void startQuiz(){
    a = int(random(n-1));
    b = 10;
    while(a+b > n){
        b = int(random(n));
    }
    reset();
    status = INIT;
}    

void processEvent(evtType,par){
    if(status == COMPLETE){
        startQuiz();
    }else
    if(evtType == EVENT_HOP){
        hops[par]=true;
        if(status == par && par < a+b){
            status++;
        }else
        if(status!=FREE){
            reset();
            status=INIT;
        }
    }else
    if(evtType == EVENT_MARK){
        marks[par]=true;
        if(status == INIT && par == a){
            status = a;
        }else
        if(status == par && par == a+b){
            status=COMPLETE;
        }else
        if(status!=FREE){
            reset();
            status=INIT;
        }
    }    

}

void mouseClicked(){

  if(mouseY < topY){
      if(mouseX < freeX){
          reset();
        status = FREE;
      }
      if(mouseX > addX){
        startQuiz();
      }   
  }
//  if(mouseX > 0){
  if(mouseX > x0/2 && mouseX < 1.5*x0+n*step ){
      

    if(mouseY < y0 && mouseY > y0-step){ // hop click
       var hopNo = (int)((mouseX-x0)/step);
       if(hopNo>=0 && hopNo<=n){
          //hops[hopNo] = true;
          processEvent(EVENT_HOP,hopNo);
       }
    }
    if(mouseY > y0 && mouseY < y0+step){
         var markNo=(int)((mouseX-x0+step/2)/step);
         if(markNo<0) markNo = 0;
          //marks[markNo]= true;
          processEvent(EVENT_MARK,markNo);
    }

  }

}