We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi Everyone, I am on beginner level in processing and have been writing a code using ControlP5, yet having issues how to add a button in a class triggered by mouse click. Found this sourcehttps://forum.processing.org/one/topic/having-a-controlp5-controller-inside-a-class.html, which nearly answers my question, without mouseClick. I don't use processing in another environment (so eliminated static+PApplet parts completely). Was also checking the ControlP5 examples, but none of them use ControlEvent in class (or I couldn't find it with my beginner level). I have a solution, but outside of class. The final aim is to collect values assigned to the same objects coming from different controllers. And later on, want to compare those objects based on values. Right now, I only have one controller (textfield, but will create more) "assigned" to objects. Hope I make sense. Any help/recommendation/advice very much appreciated. Thank you!!! first part
import controlP5.*;
Object obj=new Object();
ControlP5 cp5;
int c=0; //object counter
int a=0; //attribute counter
PImage img;
ArrayList<Object> objSet=new ArrayList();
ArrayList<Textfield> t =new ArrayList();
ArrayList s=new ArrayList();
ArrayList temp=new ArrayList();
void setup() {
size(640, 600);
img= loadImage("room1.jpg");
img.resize(320, 300);
cp5=new ControlP5(this);
}
void draw() {
background(0);
image(img, 0, 0);
//println(objSet.size());
for (int i=0; i<objSet.size(); i++) {
Object obj1=(Object) objSet.get(i);
obj1.display();
}
}
void mousePressed() {
if (mouseX<320 && mouseY<300) {
obj.press();
a=0;
}
}
void mouseReleased() {
if (mouseX<320 && mouseY<300) {
obj.release();
objSet.add(obj);
t.add(cp5.addTextfield("object"+c, 330, 10+c*100, 200, 20)
.setId(c));
c+=1;
}
}
void controlEvent (ControlEvent theEvent) {
println("got an event from controller id:" +theEvent.getId()+":"+ theEvent.getStringValue());
temp.add(theEvent.getId()); //collected Id
s.add(theEvent.getId(), theEvent.getStringValue()); //textField into arrayList
int tempC=theEvent.getId(); //actual Id
checker(s,temp,tempC);
println(s);
}
//you have to fill in with data first, but responds to modification
ArrayList checker(ArrayList toFilter, ArrayList helperA, int helperN) { //removes false values
int counter=0;
for (int i=0; i<helperA.size(); i++) {
if ((int)helperA.get(i)-helperN==0) {
counter+=1;
if (counter%2==0) {
toFilter.remove(helperN+1);
helperA.remove(i);
counter=0;
}
}
}
return toFilter;
}
and the class itself: class Object {
boolean done=false;
//ArrayList<Object> objSet=new ArrayList();
public ArrayList mouseV=new ArrayList();
ArrayList<String> attributeN=new ArrayList();
ArrayList attributeV=new ArrayList();
Object () {
}
void press() { //make first part of rect
mouseV.add(new PVector(mouseX, mouseY));
done=false;
}
void release() { //finish rect
mouseV.add(new PVector(mouseX, mouseY));
done=true;
}
PShape display() {
PVector centroid= new PVector();
PShape rect=new PShape();
if (done==true) {
noFill();
stroke(0, 255, 0);
for (int i=1; i<mouseV.size()+1; i+=2) {
PVector v1= (PVector)mouseV.get(i-1);
PVector v2= (PVector)mouseV.get(i);
rect= createShape(RECT, v1.x, v1.y, v2.x-v1.x, v2.y-v1.y);
shape(rect, 0, 0);
centroid.x=(v1.x+v2.x)/2;
centroid.y=(v1.y+v2.y)/2;
}
}
return rect;
}
}
Answers
You could show and hide your controllers. For example, in setup you declare:
cp5 = new ControlP5(this);
and in draw you could do either:
Any controlP5 widgets associated to this object will be shown or hiden all together. I hope this helps,
Kf
Thank you, but they are unfortunately not associated to class objects. My solution is a little cheat simply because I don't know how to do the right way. I mean in your solution they are associated to cp5, but I want to associate them to my custom class objects as well, when I hit mouse button. Hope I make sense.
Maybe you can post some code?
Sure, sure. Here I added a textfield and 2 numboxes in class, hopefully attached them to object class, but I got stuck when I add mouse interaction (line 38 and line 45). Thank you in advance!!! first part:
class itself, issue is highlighted: