float oldX, oldY;

HashSet streaks = new HashSet();

clearbutton cb;
bloodbutton bb;
inkbutton ib;
void setup() {
  size(500,500);
  stroke(128,0,0);

  frameRate(30);
  smooth();


  cb = new clearbutton(10,10);
  bb = new bloodbutton(25,10);
  ib = new inkbutton(40,10);

}

int BLOOD = 1;
int INK = 2;
int mode = BLOOD;



void drawbuttons(){
cb.draw();
ib.draw();
bb.draw();
}


streak s;
void draw() {
  drawbuttons();
  Iterator i = streaks.iterator();
  while(i.hasNext()){
    streak s = (streak)i.next();
    s.move();
    //s.draw();
  }
}

void setPencolor(){
  if(mode == INK){
    stroke(0,0,0);
  } 
  else {
    stroke(128,0,0); 
  }
  
  
}

void mousePressed(){

  if(cb.isIn()){
     background(204); 
     streaks = new HashSet();
     return;
  }
  if(bb.isIn()){
    mode=BLOOD;
    return;
  }
  if(ib.isIn()){
    mode=INK;
    return;
  }

  setPencolor();
  oldX = mouseX;
  oldY = mouseY;

  strokeWeight(2);
  line(oldX,oldY,oldX,oldY);
}

//float 
float usethick;
void mouseDragged() {
  setPencolor();


  if(cb.isIn()||ib.isIn()||bb.isIn()){
    return;
  }

  float rawthick = 
    sqrt(  pow(oldX-mouseX,2)+  pow(oldY-mouseY,2) );

  float oldthick = usethick;
  usethick = 1.0+(rawthick/7.0);

  while(usethick - oldthick >1.0){
    usethick -= 0.5; 
  }
  float chance = 10 - (1.5 *usethick);
  if(chance < 1.1){
   chance = 1.0; 
  }
//println(chance);
  if(mode == BLOOD && random(chance) < 1.0){
    streaks.add(new streak(mouseX,mouseY,usethick));
  }
  if(mode == BLOOD){
    strokeWeight  (usethick);
  } else {
     strokeWeight(2); 
  }
  line(oldX,oldY, mouseX,mouseY);

  oldX = mouseX;
  oldY = mouseY;
}


class streak{
  float x,y,sz;
  streak(float px, float py, float psz){
    x = px; 
    y = py; 
    sz=psz;
  }
  void move(){
    if(random(20)<1.0){
      y += 1;

      fill(128,0,0);
      noStroke();
      ellipse(x,y,sz,2); 
    }
  }
}

class button{
  float sz = 10;
  float x,y;
  button(float px, float py){
    x = px;
    y = py; 
  }
  void draw(){
    rect(x,y,sz,sz); 
  }
  boolean isIn(){
    return (mouseX >= x && mouseX <= x + sz &&mouseY >= y && mouseY <= y+sz);
  }

}

class clearbutton extends button{
  clearbutton(float px, float py){
     super(px,py);
  }
  void draw(){
    stroke(0);
    strokeWeight(1);

    if(isIn()){
      fill(255);    
    } 
    else {
      fill(204);

    }
    super.draw();
      line(13,13,17,17);
  line(17,13,13,17);

  } 
}




class bloodbutton extends button{
  bloodbutton(float px, float py){
     super(px,py);

  }
  void draw(){
    strokeWeight(1);
    stroke(0);
    if(isIn()){
      stroke(255);    
    } 
    else {
      if(isIn() || mode==BLOOD){
          stroke(255);
      } else {
          stroke(204);
      }


    }
    fill(128,0,0);
    super.draw();
  } 
}


class inkbutton extends button{
  inkbutton(float px, float py){
     super(px,py);
  }
  void draw(){
    strokeWeight(1);
    stroke(0);
      if(isIn() || mode==INK){
          stroke(255);
      } else {
          stroke(204);
      }

    fill(0);
    super.draw();
  } 
}