Skip to content
Permalink
911cc8d56f
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
122 lines (113 sloc) 3.29 KB
class Selector{
int _startX,_startY, _endX, _endY;
float _state;
float _a, _b, _radius;
boolean _selecting, _animating, _searching;
int _select_mode;
Point point;
Selector(){
_selecting = false;
_animating = false;
_searching = false;
_a = 0;
_b = 0;
_select_mode = 0; //default is rectangular select
_endX = -1;
_endY = -1;
}
void setSelectMode(int mode){ _select_mode = mode; }
void mousePressed(){//triggered on mousePressed()
_startX = mouseX;
_startY = mouseY;
boxList.clear();
switch(_select_mode){
case 0: _selecting = true; //rectangular selection
break;
case 1: _selecting = true; //ellipse selection
break;
case 2: _searching = true; //nearest neighbor search
_state = 0.0;
_animating = true;
break;
case 3: Point p = cam.transform(new Point(mouseX, mouseY)); //place point
pointList.add(p);
pot.insert(p);
}
}
void mouseReleased() {
resetSelection();
}
void resetSelection(){//triggered on mouseReleased()
if(_selecting){ //selecting with rect or circle
ArrayList<Point> solution;
switch(_select_mode){
case 0:
Point p1 = cam.transform(new Point(_startX, _startY));
Point p2 = cam.transform(new Point(_endX+_startX, _endY+_startY));
solution = pot.queryBox(new BoundingBox(min(p1._x, p2._x), max(p1._x, p2._x), min(p1._y, p2._y), max(p1._y, p2._y)));
setSelected(solution);
println("size of list: " + solution.size());
break;
case 1:
Point pp1 = cam.transform(new Point(_startX, _startY));
Point pp2 = cam.transform(new Point(_endX+_startX, _endY+_startY));
solution = pot.queryCircle(pp1, dist(pp1._x, pp1._y, pp2._x, pp2._y));
setSelected(solution);
println("size of list: " + solution.size());
break;
}
_selecting = false;
_startX = -1;
_startY = -1;
}
else{
//_tree.neighborQuery(cam.transform(new Point(_a,_b)),
_searching = false;
}
_endX = -1;
_endY = -1;
_a = 0;
_b = 0;
}
void mouseDragged() {
stretchSelection();
}
void stretchSelection(){
if(_selecting){
_endX = mouseX-_startX;
_endY = mouseY-_startY;
}
}
void draw(){
pushStyle();
stroke(color(100,100,100));
fill(c_HILITE);
if(_selecting){
switch(_select_mode){
case 0: rect(_startX,_startY,_endX,_endY);
break;
case 1: _a = mouseX-_startX;
_b = mouseY-_startY;
_radius = sqrt(_a*_a+_b*_b);
ellipseMode(RADIUS);
ellipse(_startX,_startY,_radius,_radius);
line(_startX,_startY,mouseX,mouseY);
ellipseMode(CENTER);
break;
}
}
if(_animating){ neighborAnimation(); }
popStyle();
}
void neighborAnimation(){
float elapsedTime = constrain(1f/frameRate, 16f/1000f, 32f/1000f);
_state += max( 8 * elapsedTime * sin(_state*PI), 0.01);
float r = _state * 100;
ellipseMode(RADIUS);
ellipse(_startX,_startY,r,r);
if(_state > 1.0) {
_animating = false;
_searching = false;
}
}
}