Skip to content

Commit

Permalink
merging menu and selector with main sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
laa11004 committed Dec 5, 2015
1 parent 25f6e0a commit 17baa76
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
42 changes: 42 additions & 0 deletions kdtest/Menu.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Menu{
Selector _selector;
Menu(){
selector = new Selector();
}

void activate(int button){
selector.setSelectMode(button);
}

int onaButton(int x, int y){
if( 0<x && x<=60 && y>0 && y<=25){ return 1; }
else if( 0<x && x<=60 && y>25 && y<=51){ return 2; }
else if( 0<x && x<=60 && y>51 && y<=77){ return 3; }
else if( 0<x && x<=60 && y>77 && y<=103){ return 4; }
else { return 0; }
}

void draw(){
pushStyle();
rect(0,0,60,25,0,7,7,0);//rect button
rect(15,20,5,10);//rect icon
rect(0,26,60,25,0,7,7,0);//circle button
ellipseMode(CORNER);
ellipse(0.0,26.0,60.0,25.0);//circle icon
rect(0,52,60,25,0,7,7,0);//NN button
ellipse(0.0,52.0,60.0,25.0);//NN icon
rect(0,78,60,25,0,7,7,0);//InserPoint button
popStyle();
selector.draw();
}

void mousePressed(){
int button = onaButton(mouseX,mouseY);
if( button != 0 ){ activate(button); }
else{ selector.mousePressed(); }
}

void mouseDragged(){ selector.stretchSelection(); }

void mouseReleased(){ selector.resetSelection(); }
}
98 changes: 98 additions & 0 deletions kdtest/Selector.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
class Selector{
float _startX,_startY, _endX, _endY, _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 = 1;
_endX = -1;
_endY = -1;
}

void setSelectMode(int mode){ _select_mode = mode; }

void mousePressed(){//triggered on mousePressed()
_startX = mouseX;
_startY = mouseY;
switch(_select_mode){
case 1: _selecting = true; //rectangular selection
break;
case 2: _selecting = true; //ellipse selection
break;
case 3: _searching = true; //nearest neighbor search
_state = 0.0;
_animating = true;
break;
case 4: Point p = cam.transform(new Point(mouseX, mouseY)); //place point
pointList.add(p);
tree.insert(p);
}
}

void resetSelection(){//triggered on mouseReleased()
if(_selecting){ //selecting with rect or circle
switch(_select_mode){
case 1: //_tree.rectQuery(cam.transform(new Point(_startX,_startY)),cam.transform(new Point(_endX,_endY)));
break;
case 2: //_tree.circleQuery(cam.transform(new Point(_a,_b)),
}
_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 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 1: rect(_startX,_startY,_endX,_endY);
break;
case 2: _a = mouseX-_startX;
_b = mouseY-_startY;
_radius = sqrt(_a*_a+_b*_b);
ellipseMode(RADIUS);
ellipse(_startX,_startY,_radius,_radius);
line(_startX,_startY,mouseX,mouseY);
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;
}
}
}

0 comments on commit 17baa76

Please sign in to comment.