Skip to content

Commit

Permalink
Rebuilt Menu and Buttons so that Menu and Selector are no seperate, a…
Browse files Browse the repository at this point in the history
…nd buttons work as a toggle group, and they're prettier and more flexable.
  • Loading branch information
rwb11001 committed Dec 8, 2015
1 parent e80136c commit 1105790
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 188 deletions.
97 changes: 97 additions & 0 deletions kdtest/Button.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
class CirSHandler implements ButtonHandler{
void Selected() {
selector.setSelectMode(1);
useSelector = true;
}
}

class RectSHandler implements ButtonHandler{
void Selected() {
selector.setSelectMode(0);
useSelector = true;
}
}
class PanHandler implements ButtonHandler{
void Selected() {
cam.setMode(0);
useSelector = false;
}
}

class RotHandler implements ButtonHandler{
void Selected() {
cam.setMode(1);
useSelector = false;
}
}

class PlaceHandler implements ButtonHandler{
void Selected() {
selector.setSelectMode(3);
useSelector = true;
}
}

interface ButtonHandler {
void Selected ();
}

class Button {
int _x;
int _y;
int _w;
int _h;
int _r;
boolean _isSel;
String _label;
ArrayList<ButtonHandler> _handlerList;

Button (int x, int y, String label) {
_x = x;
_y = y;
_w = i_buttonWidth;
_h = i_buttonHeight;
_r = i_buttonRad;
_handlerList = new ArrayList<ButtonHandler> ();
_label = label;
_isSel = false;
}

boolean isMouseOver() {
return mouseX >= _x && mouseX <= _x + _w && mouseY >= _y && mouseY <= _y + _h;
}

void Select() {
_isSel = true;
for(ButtonHandler h: _handlerList) {
h.Selected();
}
}

void unSelect() {
_isSel = false;
}

void draw () {
pushStyle();
stroke(c_buttonStr);
if(_isSel) {
fill(c_buttonSel);
} else {
fill(c_buttonDef);
}
rect(_x, _y, _w, _h, 0, _r, _r, 0);
if(_isSel) {
fill(c_WHITE);
} else {
fill(c_BLACK);
}
textSize(20);
text(_label, _x + 3, _y + _h - 10);
popStyle();
}

void addHandler (ButtonHandler h) {
_handlerList.add(h);
}
}
51 changes: 28 additions & 23 deletions kdtest/Camera.pde
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ class Camera {

int _initMX;
int _initMY;

int _mode;

Camera() {
_x = 0.0;
_y = 0.0;
_scale = 1.0;
_angle = 4 * PI;
}

void setMode(int mode) {
_mode = mode;
}

void update() {
translate(_x + width/2,_y + height/2);
Expand All @@ -36,30 +42,29 @@ class Camera {
void mouseDragged() {
float difX = (float)(mouseX - _initMX);
float difY = (float)(mouseY - _initMY);

switch(G_mode) {
case 1:
difX /= (float)(width);
difY /= (float)(height);
_scale = epsilon + abs((1 - 4*(difX + difY)) * _oldScale);
_x = _oldX * _scale;
_y = _oldY * _scale;
break;
case 2:
difX *= 1.0/_scale;
difY *= 1.0/_scale;
// _x = cos(_angle)*difX - sin(_angle)*difY + _oldX;
// _y = sin(_angle)*difX + cos(_angle)*difY + _oldY;
_x = _oldX + difX;
_y = _oldY + difY;
break;
case 3:
float initAngle = atan((_initMY - height/2)/(epsilon + _initMX - width/2));
float newAngle = atan((mouseY - height/2)/(epsilon + mouseX - width/2));
float delta = newAngle - initAngle;
_angle = _oldAngle + delta;
break;

if(mouseButton == RIGHT) {
difX /= (float)(width);
difY /= (float)(height);
_scale = epsilon + abs((1 - 4*(difX + difY)) * _oldScale);
_x = _oldX * _scale;
_y = _oldY * _scale;
} else {
switch(_mode) {
case 0:
difX *= 1.0/_scale;
difY *= 1.0/_scale;
_x = _oldX + difX;
_y = _oldY + difY;
break;
case 1:
float initAngle = atan((_initMY - height/2)/(epsilon + _initMX - width/2));
float newAngle = atan((mouseY - height/2)/(epsilon + mouseX - width/2));
float delta = newAngle - initAngle;
_angle = _oldAngle + delta;
break;
}
}
}

void mouseReleased() {
Expand Down
129 changes: 74 additions & 55 deletions kdtest/Menu.pde
Original file line number Diff line number Diff line change
@@ -1,72 +1,91 @@
class Menu{
ArrayList<Button> _buttonList;
Selector _selector;

Menu(){
_selector = new Selector();
_buttonList = new ArrayList<Button>();
makeButtons();
}

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

void makeButtons(){
_buttonList.add(new Button(0,0,0)); //adds the first button
for( int i = 1; i < 4; i++){ _buttonList.add(new Button(0,i*25+1,i)); }
void draw(){
for(Button b: _buttonList) {
b.draw();
}
}

int onaButton(int x, int y){
int i;
for( i = 0; i < 4; i++){ //loop checks if any button was clicked
if(_buttonList.get(i).isInside(x,y)) { return i; } //and returns the _id if one was in fact clicked
boolean mousePressed(){
boolean result = false;
for(Button b: _buttonList) {
boolean check = b.isMouseOver();
if(check) {
result = true;
for(Button b0: _buttonList) {
b0.unSelect();
}
b.Select();
break;
}
}
return 99;
return result;
}

void draw(){
pushStyle();
for(int i=0; i<4; i++){ _buttonList.get(i).draw(); }
popStyle();
_selector.draw();
void mouseDragged() {
selector.stretchSelection();
}

void mousePressed(){
int button = onaButton(mouseX,mouseY); //which button is selected
if( button != 99 ){ activate(button); } //99 is an arbitrary invalid button _id
else{ _selector.mousePressed(); }

void mouseReleased() {
selector.resetSelection();
}

void mouseDragged(){ _selector.stretchSelection(); }
void mouseReleased(){ _selector.resetSelection(); }
}

class Button{
int _x1, _y1, _x2, _y2, _id;
Button(int x,int y, int id){
_x1 = x;
_y1 = y;
_x2 = 60;
_y2 = 24;
_id = id;
}

boolean isInside(int x, int y){
if( x>=_x1 && x<=_x2 && y>_y1 && y<=_y1+_y2){ return true; }
else{ return false; }
void makeButtons() {
int y = 50;
Button cirS = new Button (0, y, "Circ Sel");
y += i_buttonHeight + 10;
_buttonList.add(cirS);
cirS.addHandler(new CirSHandler());

Button rectS = new Button (0, y, "Rect Sel");
y += i_buttonHeight + 10;
_buttonList.add(rectS);
rectS.addHandler(new RectSHandler());

Button pan = new Button (0, y, "Pan");
y += i_buttonHeight + 10;
_buttonList.add(pan);
pan.addHandler(new PanHandler());

Button rot = new Button (0, y, "Rotate");
y += i_buttonHeight + 10;
_buttonList.add(rot);
rot.addHandler(new RotHandler());

Button place = new Button (0, y, "Place");
y += i_buttonHeight + 10;
_buttonList.add(place);
place.addHandler(new PlaceHandler());
place.Select();
}

void draw(){
rect(_x1,_y1,_x2,_y2,0,7,7,0);//basic button shape
switch(_id){
case 0: rect(_x1+_x2/2,_y1+5,10,10);//rect icon
break;
case 1: ellipseMode(CORNER);
//ellipse(0.0,26.0,60.0,25.0);//circle icon
break;
case 2: //ellipse(0.0,52.0,60.0,25.0);//NN icon
break;
case 3:
break;
default:
}
}
}

void drawHelpMenu() {
float hwidth = 400;
float hheight = 600;

pushStyle();
strokeWeight (i_helpStrokeWidth);
stroke(c_helpStroke);
fill(c_helpBackground);
rect(width/2 - hwidth/2, height/2 - hheight/2, hwidth, hheight, 50, 50, 50, 50);
textSize(40);
fill(c_helpText);
text("Help:", width/2 - 50, height/2 - hheight/2 + 50);
textSize (30);
float x = width/2 - 150;
float y = height/2 - hheight/2 + 100;
for (int i = 0; i < i_helpStrings; i++) {
text(s_helpStrings[i], x, y);
y += 35;
}
popStyle();
}


41 changes: 0 additions & 41 deletions kdtest/Mode.pde

This file was deleted.

8 changes: 8 additions & 0 deletions kdtest/Properties.pde
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ color c_HILITE = color (251, 255, 20, 100);//selector filling
color c_background = color (26, 26, 26);
color c_gnoman = c_LIGHTBLUE;

// Buttons
color c_buttonStr = c_BLACK;
color c_buttonDef = c_WHITE;
color c_buttonSel = color (170, 50, 50);
int i_buttonWidth = 80;
int i_buttonHeight = 40;
int i_buttonRad = 7;

// Menu Colors
color c_modeText = c_BLACK;
color c_modeBackground = color (200,200,200,200);
Expand Down
Loading

0 comments on commit 1105790

Please sign in to comment.