// processing IDE 3.5.3 // button tutorial // http://kll.engineering-news.org/kllfusion01/articles.php?article_id=166 // here we make a class for ONE Button. // a class has 2 advantages, it has a own set of memory, with mixed variable types... // like a record struct // and it can have methods ( functions ) included. // https://processing.org/reference/class.html Button myButton = new Button(100, 100, 80, 30, false, "text", 0); // make a button of type class Button // handing parameter Button( x , y , w , h , initial ONOFF , description test , serial id ); // in that case not need predefined variables, ( the button ( a class ) remembers its settings ) // but in later cases like making a button row as array of class it might make sense again to define int xi,yi,wi,hi... void setup() { size(300, 300); } void draw() { background(200, 200, 0); myButton.draw(); } class Button { //________________________________________ begin class int x, y, w, h, id; boolean click=false, sel; String atext; Button(int _x, int _y, int _w, int _h, boolean _sel, String _atext, int _id) { x = _x; y = _y; w = _w; h = _h; sel = _sel; atext = _atext; id = _id; } void draw() { mouse(); //__________________________________________ check on operation if (sel) fill(0, 200, 0); //___________________ ON OFF fill color else fill(0, 0, 200); if (over()) stroke(200, 0, 200); //_______________ hover stroke color else stroke(0, 200, 200); strokeWeight(3); rect(x, y, w, h); //_________________________________ button area as rectangle noStroke(); //_______________________________________ button text ( here only one ) fill(200); text(atext, x + 10, y + h - 10); } boolean over() { return (mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h); } void mouse() { if ( over() && mousePressed && !click) { //__________ operation click = true; //___________________________________ just clicked sel = !sel; //_____________________________________ toggle action } if ( over() && !mousePressed ) click = false; //____ reset } } //_____________________________________________________ end class