Hello all. As an exercise in becoming more proficient at Processing, I’ve been trying to make my own GUI instead of using one of the great already-made libraries like CP5. I’ve been doing well enough, but am getting hung up on calling nested functions which revolve around calling PApplet. What I’m aiming to do is create a button class which has registerMethod mouse events, instead of using the much easier mousePressed() calls in the main sketch. What I’d like is to be able to place the buttons inside another class, and call that class from the main.
I know this makes things hard for me, but this is an exercise to push my boundaries a bit.
Here is some very sanitized code for what I’m trying to get. If it helps, the actual application will be to be able to have a servo controller package class (one slider, 2 buttons) that I can call any number of times and manipulate each.
Sorry about the format, I cannot for the life of me find the code format button
Also: I also posted this on the old forums, hope that’s OK.
PackServ tryMe;
void setup(){
size(200,150);
tryMe = new PackServ();
}
void draw(){
background (250);
fill(25,200,10);
tryMe.Add(this);
}
public class PackServ{
PApplet SlApp;
Butt Butt1, Butt2;
Boolean moveUnit=false;
String theName;
int pkX, pkY, pkW;
void Add(PApplet pappy) {
SlApp = pappy;
theName = "The Slider";
pkX = 20; pkY = 20; pkW = 100;
Butt1 = new Butt (pappy);
Butt2 = new Butt (pappy);
drawIt();
}
void drawIt() {
fill(200); strokeWeight(1);
rect(pkX,pkY,pkW,60,5);
Butt1.Add("but1", pkX+25, pkY+25, 25);
Butt2.Add("but2", pkX+75, pkY+25, 25);
}
} // Pau Class
public class Butt
{
PApplet butApplet;
int theX, theY, theW, theH, bRad;
boolean togStat = false, pressed = false;
boolean onClick = false;
color n;
color h = color(240, 210, 10);
color f = color (40, 90, 190);
color a = color (240, 20, 20);
color c = color (250,150,50);
int oBb, li;
String theLabel;
Butt (PApplet kid) {
butApplet = kid;
butApplet.registerMethod("mouseEvent", this);
n = f;
}
void Add(String _myName, int x, int y, int siz) {
theLabel = _myName;
theX = x; theY = y; bRad = siz;
fill(75); textSize(9);
buttonLook();
}
boolean overB(int mx, int my) {
if (dist (mx, my, theX, theY) <= bRad){
return true;
} else {
return false; }
}
void buttonLook(){
if (overB(mouseX,mouseY) && pressed) {
n = c; oBb = 3; li = 100;
}
else if (overB(mouseX,mouseY)) {
text(theLabel, theX-10, theY+25);
n = h;
}
else {n = f; oBb=1; li=25; }
strokeWeight(oBb); stroke(li); fill(n);
ellipse(theX, theY, bRad, bRad);
}
void mouseEvent(MouseEvent e) {
pressed = false;
int mx = e.getX(), my = e.getY();
switch (e.getAction()) {
case MouseEvent.PRESS:
pressed = overB(mx,my);
break;
}
}
} // Pau class