Skip to content

Commit

Permalink
Fixed camera, for now. Added help menu, and a properties page. Need t…
Browse files Browse the repository at this point in the history
…o fix it for javascript mode.
  • Loading branch information
rwb11001 committed Nov 27, 2015
1 parent 7b80f08 commit 5bab2af
Show file tree
Hide file tree
Showing 10 changed files with 572 additions and 45 deletions.
21 changes: 21 additions & 0 deletions App/App.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

void setup () {
size(640, 640, P3D);
}


void draw () {
lights ();
background (0);

camera (30.0, mouseY, 220.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);

noStroke();
box(90);
stroke (255);
line(-100, 0, 0, 100, 0, 0);
line(0, -100, 0,0,100,0);
line(0, 0, -100, 0,0,0);
}
43 changes: 43 additions & 0 deletions App/Quaternion.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class Quat {
float w, x, y, z;

//Default Constructor
public Quat () {
w = 1.0;
x = 0.0;
y = 0.0;
z = 0.0;
}

public Quat (float w0, x0, y0, z0) {
w = w0;
x = x0;
y = y0;
z = z0;
}

public Quat add (Quat q) {
float nw = w + q.w;
float nx = x + q.x;
float ny = y + q.y;
float nz = z + q.z;
return new Quat (nw, nx, ny, nz);
}

public Quat mult (Quat q) {
float nw = w * q.w - (x * q.x + y * q.y + z * q.z);
float nx = w * q.w + x * q.x + y * q.y - z * q.z;
float ny = w * q.w - x * q.x + y * q.y + z * q.z;
float nz = q * q.w + x * q.x - y * q.y + z * q.z;
return new Qua (nw, nx, ny, nz);
}

public Quat scale (float t) {
float nw = t * q;
float nx = t * x;
float ny = t * y;
float nz = t * z;
return new Quat (nw, ny, nz);
}
}

35 changes: 35 additions & 0 deletions App/console.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Console {
int _textSize = 10;
int _bufferSize = 40;
int _xOffset = 20;
int _yOffset = 20;
ArrayList<String> _stringList;

Console() {
_stringList = new ArrayList<String> ();
}

void print(String s) {
_stringList.add(s);
}

void draw() {
pushStyle();
noStroke();
fill(0,0,0,150);
rect(0,0,400,_yOffset + (_bufferSize + 1) * _textSize);
fill(30,250,30);
textSize(_textSize);
int index = 0;
int c = 0;
if (_stringList.size() - _bufferSize > 0) {
index = (_stringList.size() - _bufferSize) - 1;
}
while(index < _stringList.size()) {
text(_stringList.get(index), _xOffset, _yOffset + c * _textSize);
index++;
c++;
}
popStyle();
}
}
Binary file removed kdtest/.Camera.pde.swp
Binary file not shown.
19 changes: 10 additions & 9 deletions kdtest/Camera.pde
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ class Camera {
case 1:
difX /= (float)(width);
difY /= (float)(height);
_scale = epsilon + (1 - 4*(difX + difY)) * _oldScale;
_scale = epsilon + abs((1 - 4*(difX + difY)) * _oldScale);
con.print(" " + _scale);
break;
case 2:
difX *= _scale;
difY *= _scale;
_x = cos(_angle)*difX + sin(_angle)*difY + _oldX;
_y = -sin(_angle)*difX + cos(_angle)*difY + _oldY;
// _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;
_x = cos(delta) * _oldX - sin(delta)*_oldY;
_y = sin(delta) * _oldX + sin(delta)*_oldY;
break;
}
}
Expand All @@ -65,10 +66,10 @@ class Camera {
}

Point transform(Point p) {
float px = (1/_scale)*(p._x - width/2);
float py = (1/_scale)*(p._y - height/2);
float x = cos(_angle)*px - sin(_angle)*py - _y;
float y = sin(_angle)*px + cos(_angle)*py - _x;
float px = (1.0/_scale)*((p._x - width/2) - _x);
float py = (1.0/_scale)*((p._y - height/2) - _y);
float x = (cos(_angle)*px + sin(_angle)*py);
float y = (-sin(_angle)*px + cos(_angle)*py);
return new Point(x,y);
}
}
40 changes: 40 additions & 0 deletions kdtest/Point.pde
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
class Point {
float _r = 30;
float _x;
float _y;
boolean _isSelected;

Point(float x0, float y0) {
_x = x0;
_y = y0;
_isSelected = false;
}

void draw() {
pushStyle();
stroke(c_pointStroke);
fill((_isSelected) ? c_pointSelect : c_pointDefault);
ellipse(_x, _y, _r, _r);
popStyle();
}

boolean isSelectedPoint(float x, float y) {
return (x - _x)*(x - _x) + (y - _y) * (y - _y) < _r * _r;
}

boolean isSelectedCir(float x, float y, float r) {
return (x - _x)*(x - _x) + (y - _y)*(y - _y) < r * r;
}

boolean isSelectedRect(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) {
int l0 = lineSideTest(x1, y1, x2, y2, _x, _y);
int l1 = lineSideTest(x2, y2, x3, y3, _x, _y);
int l2 = lineSideTest(x3, y3, x4, y4, _x, _y);
int l3 = lineSideTest(x4, y4, x1, y1, _x, _y);

return (l0 == l1) && (l1 == l2) && (l2 == l3);
}

void setSelected() {
_isSelected = true;
}

void setUnSelected() {
_isSelected = false;
}

void toggleSelected() {
_isSelected = !_isSelected;
}
}
37 changes: 37 additions & 0 deletions kdtest/Properties.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


// Pallete
color c_WHITE = color (255, 255, 255);
color c_BLACK = color (0, 0, 0);

// Screen Color
color c_background = color (50, 50, 50);
color c_gnoman = color (255, 130, 130);

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

// Help Colors
color c_helpBackground = color (5,13,22);
color c_helpStroke = c_BLACK;
color c_helpText = c_WHITE;
int i_helpStrokeWidth = 2;

// Point
color c_pointStroke = c_BLACK;
color c_pointDefault = c_WHITE;
color c_pointSelect = color (100, 255, 100);

// Help Strings
int i_helpStrings = 8;
String[] s_helpStrings = {
"h: Toggle Help",
"t: Toggle Console",
"f: Scale",
"d: Pan",
"s: Rotate",
"a: Place Points",
"g: Select Point",
"c: Clear Points"
};
10 changes: 10 additions & 0 deletions kdtest/Tests.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
int lineSideTest (float ax0, float ay0, float ax1, float ay1, float px, float py) {
float val = (ax1 - ax0) * (py - ay0) - (ay1 * ay0) * (px - ax0);
if (val == 0) {
return 0;
} else if (val > 0) {
return 1;
} else {
return -1;
}
}
63 changes: 58 additions & 5 deletions kdtest/kdtest.pde
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ Mode statusLine;

ArrayList<Point> pointList;

boolean drawCon;
boolean drawCon, drawHelp;
float epsilon = 0.000001;
int G_mode;
// 1 = SCALE
// 2 = PAN
// 3 = ROTATE
// 4 = PLACE
// 5 = SELECT

void setup() {
size(800,800);
background(120);
stroke(256,256,256);
ellipseMode(CENTER);

con = new Console();
Expand All @@ -25,48 +25,101 @@ void setup() {
pointList = new ArrayList<Point>();

drawCon = false;
drawHelp = true;
G_mode = 1;
}

void draw() {
background(120);
background(c_background);

pushMatrix();
cam.update();
drawGnoman(200);
for(Point p: pointList) {
ellipse(p._x, p._y, 40,40);
p.draw();
}
popMatrix();

if(drawCon) con.draw();
statusLine.draw();

if (drawHelp) {
drawHelp();
} else {
statusLine.draw();
}

}

void keyPressed() {
switch(key) {
case 't':
case 'T':
con.print("t");
drawCon = !drawCon;
break;
case 'f':
case 'F':
G_mode = 1;
break;
case 'd':
case 'D':
G_mode = 2;
break;
case 's':
case 'S':
G_mode = 3;
break;
case 'a':
case 'A':
G_mode = 4;
break;
case 'g':
case 'G':
G_mode = 5;
break;
case 'c':
case 'C':
pointList.clear();
break;
case 'h':
case 'H':
drawHelp = !drawHelp;
break;
}
}

void drawHelp() {
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();
}

void checkSelect() {

}

void drawGnoman(int r) {
pushStyle();
stroke(c_gnoman);
line(-r,0,r,0);
line(0,-r,0,r);
popStyle();
}

void mousePressed() {
Expand Down
Loading

0 comments on commit 5bab2af

Please sign in to comment.