topical media & game development
graphic-processing-algorithm-Ch04-p107-p107.pde / pde
int numShapes = 12; //num on side of grid of shapes
MyShape[] shape = new MyShape[numShapes*numShapes];
MyGroup group;
MyControl control;
//*****************************************
void setup(){
size(300,400);
for(int y=0; y<numShapes; y++){
for(int x=0; x<numShapes; x++){
shape[y*numShapes+x] = new MyShape(3,9.,30+x*20., 30+y*20.);
}
}
group = new MyGroup(numShapes*numShapes, shape);
group.move(10.,10.);
control = new MyControl();
}
//*****************************************
void draw( ){
background(255);
control.coordsDisplay.setText("x= " + mouseX + " y= " + mouseY);
group.draw();
}
int xfirst, yfirst;
//*****************************************
void mousePressed(){
xfirst = mouseX; // remember this point
yfirst = mouseY;
// Pick a shape
if(control.part.equals("Group"))
if(group.select((float)mouseX, (float)mouseY, 5.) == true){
control.input.setText("Selected = Group");
//break;
}
if(control.part.equals("Shape"))
for(int i=0; i<group.numShapes; i++)
if(group.shapes[i].select((float)mouseX, (float)mouseY, 10.) == true){
control.input.setText("Selected shape = " + i);
//break;
}
if(control.part.equals("Segment"))
for(int ii=0; ii<group.numShapes; ii++)
for(int j=0; j<group.shapes[ii].numSegments; j++){
if(group.shapes[ii].segs[j].start.select((float)mouseX, (float)mouseY, 10.) == true)
control.input.setText("Selected shape = " + ii + "Seg = " + j);
//break;
}
}
//*****************************************
void mouseDragged(){
int xoff = mouseX - xfirst; // get the offset
int yoff = mouseY - yfirst;
MyPoint ref = new MyPoint(0.,0.);
if(control.part.equals("Group")){
ref = group.centroid();
if(control.status.equals("Move"))
group.move((float)xoff, (float)yoff);
else if(control.status.equals("Rotate"))
group.rotate((float)xoff, ref);
else if(control.status.equals("Scale"))
group.scale((float)mouseX/xfirst, (float)mouseY/yfirst, ref);
}
else if(control.part.equals("Shape")){
for(int i=0; i<group.numShapes; i++)
if(group.shapes[i].isSelected){
ref = group.shapes[i].centroid();
if(control.status.equals("Move"))
group.shapes[i].move((float)xoff, (float)yoff);
else if(control.status.equals("Rotate"))
group.shapes[i].rotate((float)xoff, ref);
else if(control.status.equals("Scale"))
group.shapes[i].scale((float)mouseX/xfirst, (float)mouseY/yfirst, ref);
}
}
else if(control.part.equals("Segment")){
for(int ii=0; ii<group.numShapes; ii++)
for(int j=0; j<group.shapes[ii].numSegments; j++){
ref = group.shapes[ii].segs[j].centroid();
if(group.shapes[ii].segs[j].start.isSelected == true)
if(control.status.equals("Move"))
group.shapes[ii].segs[j].move((float)xoff, (float)yoff);
else if(control.status.equals("Rotate"))
group.shapes[ii].segs[j].rotate((float)xoff, ref);
else if(control.status.equals("Scale"))
group.shapes[ii].segs[j].scale((float)mouseX/xfirst, (float)mouseY/yfirst, ref);
}
}
xfirst = mouseX; //remember this point
yfirst = mouseY;
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.