Key and mouse pressed with boolean

i had a day to think about it and wanted to tell you that
i not understand what you want from a mouse:

  • it is just clicked
  • it is still pressed
  • it is not pressed any more

what else could there be?

now even it is again
“NOT WHAT I WANT”

i give you a ( most likely worthless )
DOUBLE CLICK BUTTON
( because there would need add logic memory and millis timer … )

ButtonPlusPlus.pde

/*
 yes , again about BUTTON
 but this time it is about in deep mouse click operation
 start out from a ready toggle button class
 but give it more abilities like:
 
 DOUBLE CLICK
 
 
 RESET by RIGHT MOUSE CLICK ( or presss the shown other small reset button ) 
 */

Button_pp myButton_pp; //________________________________ make a button of type class Button_pp ( test double click logic )
Button myButton; //______________________________________ make a button of type class Button ( just for ref )

void setup() {
  size(768, 500);
  strokeWeight(3);
  textSize(20);
  myButton_pp = new Button_pp(100, 100, 300, 200, false, "double click test", 0);//xi, yi, wi, hi, seli, texti, idi);
  myButton = new Button(600, 400, 100, 40, false, false, "reset", "--", 1);//xi, yi, wi, hi, seli, visible, text0,text1, idi);
}

void draw() {
  header();
  background(200, 200, 0);
  logo(width-120, 20, 50, 0.01);
  myButton_pp.draw();
  myButton.draw();
  test();
}


void test() { //______________________________________________________test environment connects 2 different buttons
  if ( myButton_pp.seld || myButton_pp.sel ) myButton.vis = true;
  if ( myButton.vis && myButton.sel ) {
    myButton_pp.dreset(); //_________________________________________ reset the double click button
    myButton.sel = false;
    myButton.vis = false;
  }
}

void mousePressed() {
  myButton.mymousePressed();
}





/*
 myTools  KLL
 use extra pde file here in root
 name: myTools.pde
 idea is to develop it here ( master ) and download / upload to other projects.
 content: 
 
 ___________________________________ classes declared:
 
 class Button_pp
 declare:
 Button_pp button(x, y, w, h, sel, text, id);
 
 methods:
 button.draw(); //_______________ from draw
 button.mymousePressed(); //_____ local
 boolean button.over(); //_______ local
  button.dreset(); //_____________ local remote reset

 ___________________________________
 
 class Button
 declare:
 Button button(x, y, w, h, sel, text0, text1, id);
 
 methods:
 button.draw(); //_______________ from main draw
 button.mymousePressed(); //_____ from main mousePressed
 boolean button.over(); //_______ local
 
 ___________________________________ functions to call:
 
 logo(x,y,radius,speed); //_________ just some show
 
 header(); //_______________________ a window title with optional FPS info
 
 */

boolean diag = true; //__________________________________ diagnostic prints global switch

//_______________________________________________________ (DOUBLE CLICK) CLASS BUTTON PLUS PLUS

class Button_pp {   //____________________________________ begin class
  int x, y, w, h, id;
  boolean act, sel, seld;
  String atext, ainfo;
  long click1, dclick1=0, dclicks=0, dclick2=0;
  long dclicklim=200, dclickslim=300;
  int dstage=0;

  Button_pp(int x, int y, int w, int h, boolean sel, String atext, int id) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.sel = sel;
    this.seld = sel;
    this.atext = atext;
    this.id = id;
  }

  void  draw() {
    this.mymousePressed();
    strokeWeight(3);
    if (this.seld)         fill(0, 200, 0);
    else if (this.sel)     fill(0, 200, 200);
    else if (this.act)     fill(200, 0, 200);
    else                   fill(0, 0, 200);
    if (this.over())  stroke(200, 0, 200);
    else              stroke(0, 200, 200);
    rect(this.x, this.y, this.w, this.h);
    noStroke();
    fill(200);
    textSize(30);
    textAlign(CENTER);
    text(this.atext, this.x + this.w/2, this.y + this.h / 2 + 10);
    ainfo = "/ "+dclick1+" \\ "+dclicks+" / "+dclick2+" \\ \n lim "+dclicklim+", "+dclickslim+", "+dclicklim;
    textSize(20);
    if (diag ) text(ainfo, this.x + this.w/2, this.y + this.h - 40);
  }

  boolean  over() {
    return (mouseX > this.x & mouseX < this.x + this.w & mouseY > this.y & mouseY < this.y + this.h);
  }

  void  mymousePressed() { //___________________________ called from this draw()
    if ( this.over()  && mousePressed && (mouseButton == LEFT) ) {
      if ( dstage == 0 ) {
        this.act = true;  //__ left keep pressed
        click1 = millis(); //_ remember start time
        dstage = 1;
      }
      if ( dstage == 2 ) dstage = 3;
    }

    if ( this.act && dstage == 1 )    dclick1 = millis() - click1; //____________________ check timer 1
    if ( this.act && dstage == 2 )    dclicks = millis() - click1-dclick1; //____________ check timer 2
    if ( this.act && dstage == 3 )    dclick2 = millis() - click1-dclick1-dclicks; //____ check timer 3

    if ( this.act && dstage == 2 && dclicks > dclickslim ) {
      this.sel = true;
      this.act=false;
      if (diag ) println("timeout to single click");
    }

    if ( this.over() && this.act && !mousePressed ) {
      if ( dstage == 1 )  dstage = 2;
      if ( dstage == 3 ) { //___________________________ end of a possible double click
        if (diag ) println(" see possible double click ", dclick1, dclicks, dclick2, " msec");
        this.act = false;
        if ( dclick1 < dclicklim && dclicks < dclickslim && dclick2 < dclicklim ) {
          this.seld = true;
          if (diag ) println("double click timing ok");
        } else { 
          this.sel = true; //______________________ treat it as single click?
          if (diag ) println("bad double click timing");
        }
      }
    }

    if ( this.over()  && mousePressed && (mouseButton == RIGHT) ) dreset(); //_______ RIGHT CLICK also RESET
  } //__ end mymousePressed

  void dreset() {
    if (diag ) println("reset");
    dstage   = 0; //________ reset
    dclick1  = 0;
    dclicks  = 0;
    dclick2  = 0;
    this.sel = false;
    this.seld = false;
    this.act = false; //__ end
  }
} //___________________________________________________ end class


//________________________________________________________ FUNCTION HEADER
void header() {
  String tit = "";
  if ( diag ) tit ="<|-- Button Plus Plus --|> "+nf(frameRate, 0, 1)+" fps";
  else               tit ="<|-- Button Plus Plus --|>";
  surface.setTitle(tit);
}

//________________________________________________________ FUNCTION LOGO
float ang = 0;

void logo(int x, int y, int r, float dang) {
  float d1 = 2 * r;
  float d2 = 2 * r / sqrt(2);
  ang += dang; //__________________ animation
  push();
  fill(255); //____________________ fill same all 3 shapes
  strokeWeight(4);
  stroke(200, 200, 0); //__________ here same as main background gives a nice effect
  ellipseMode(CENTER);
  rectMode(CENTER);
  translate(x + r, y + r); //______ CENTER thinking
  push();
  rotate(-ang); //__________________ animate first bigger rect
  rect(0, 0, d1, d1);
  pop();
  ellipse(0, 0, d1, d1);
  rotate(ang); //_________________ animate second smaller rect  
  rect(0, 0, d2, d2);
  //textAlign(CENTER, CENTER);
  //textSize(20);
  //fill(200, 200, 0);
  //text("K L L", 0, 0);
  pop();
}

//_______________________________________________________ (OLD) CLASS BUTTON

class Button {   //____________________________________ begin class
  int x, y, w, h, id;
  boolean sel, vis;
  String atext, btext;

  Button(int x, int y, int w, int h, boolean sel, boolean vis, String atext, String btext, int id) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.sel = sel;
    this.vis = vis;
    this.atext = atext;
    this.btext = btext;
    this.id = id;
  }

  void  draw() {
    if ( this.vis ) {
      strokeWeight(3);
      if (this.sel)         fill(0, 200, 0);
      else                  fill(0, 0, 200);
      if (this.over())      stroke(200, 0, 200);
      else                  stroke(0, 200, 200);
      rect(this.x, this.y, this.w, this.h);
      noStroke();
      fill(200);
      textSize(30);
      textAlign(CENTER);
      String stext = "";
      if ( this.sel )       stext = this.btext; 
      else                  stext = this.atext;
      text(stext, this.x + this.w/2, this.y + this.h / 2 + 8);
    }
  }

  boolean  over() {
    return (mouseX > this.x & mouseX < this.x + this.w & mouseY > this.y & mouseY < this.y + this.h);
  }

  void  mymousePressed() {
    if ( this.over() )   this.sel =!this.sel; //_______ called from main mousePressed
  }
} //___________________________________________________ end class


https://en.wikipedia.org/wiki/Double-click see who invented this:

2 Likes