Select one of my objects from an object array
in
Programming Questions
•
8 months ago
Hello Processing,
i know this title could seem to be confused but i want to build a simply origami sketch builder.
For this action, i try to use bassicly setVertex powerfull method assigned to a texture with an array of selection objects.
The difficulty will be like this:
I need to assign setVertex method to the selected object from my array by mousePressed.
This explanation coulnd't be sufficient so there is my short sketch:
PShape test;
int Ax, Ay, Aw, Ah;
Control[] Rect;
void setup() {
size(800, 500, P3D);
PImage tex = loadImage("333.jpg");
Rect = new Control[3];
Ax=120-25;
Ay=120-5;
Aw=Ah=25;
test = createShape();
test.noStroke();
test.texture(tex);
test.tint(255);
test.vertex(120, 120, 0, 0, 0);
test.vertex(width-20, 120, 0, tex.width, 0);
test.vertex(width-20, height-20, 0, tex.width, tex.height);
test.vertex(120, height-20, 0, 0, tex.height);
test.end();
for (int i = 0; i<3;i++) {
Rect[i] = new Control(120+int(random(width-240)), 120+int(random(height-240)));
}
}
void draw() {
background(255);
for (int i = 0; i<3;i++) {
Rect[i].Check(test);
Rect[i].display();
}
// for(Control control : Rect){
// control.display();
// }
pushStyle();
noFill();
rect(Ax, Ay, Aw, Ah);
popStyle();
if (keyPressed&&key=='1') {
Rect[1] = new Control(mouseX, mouseY);
}
if (pressA()) {
Ax = mouseX-10;
Ay=mouseY-10;
test.setVertex(0, mouseX, mouseY, 0);
}
shape(test);
}
boolean pressA() {
if (mousePressed&&mouseX>Ax&&mouseY>Ay&&mouseX<Ax+Aw&&mouseY<Ay+Ah) {
return true;
}
else {
return false;
}
}
class Control {
int mx;
int my;
Control(int mx_, int my_) {
mx= mx_;
my = my_;
}
boolean Check(PShape test) {
int Rectselected = 1;
if (mousePressed&&mouseX>this.mx&&this.mx+25>mouseX&&mouseY<this.my+25&&this.my<mouseY) {
this.mx=mouseX-10;
this.my=mouseY-10;
test.setVertex(Rectselected, mouseX-10, mouseY-10, 0);// there is the problem I need to be able at this point to explain to Processing which Rect[] is selected
return true;
}
else {
return false;
}
}
void display() {
noFill();
stroke(255, 0, 0);
rect(mx, my, 25, 25);
}
}
Thanks in advance
1