From 5bab2af47b03d6f069de41fad0cc587a22ea1c19 Mon Sep 17 00:00:00 2001 From: ThermalSpan Date: Thu, 26 Nov 2015 21:56:34 -0500 Subject: [PATCH] Fixed camera, for now. Added help menu, and a properties page. Need to fix it for javascript mode. --- App/App.pde | 21 +++ App/Quaternion.pde | 43 +++++ App/console.pde | 35 ++++ kdtest/.Camera.pde.swp | Bin 12288 -> 0 bytes kdtest/Camera.pde | 19 +- kdtest/Point.pde | 40 ++++ kdtest/Properties.pde | 37 ++++ kdtest/Tests.pde | 10 + kdtest/kdtest.pde | 63 ++++++- kdtest/web-export/kdtest.pde | 349 +++++++++++++++++++++++++++++++---- 10 files changed, 572 insertions(+), 45 deletions(-) create mode 100644 App/App.pde create mode 100644 App/Quaternion.pde create mode 100644 App/console.pde delete mode 100644 kdtest/.Camera.pde.swp create mode 100644 kdtest/Properties.pde create mode 100644 kdtest/Tests.pde diff --git a/App/App.pde b/App/App.pde new file mode 100644 index 0000000..1386841 --- /dev/null +++ b/App/App.pde @@ -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); +} diff --git a/App/Quaternion.pde b/App/Quaternion.pde new file mode 100644 index 0000000..961fc96 --- /dev/null +++ b/App/Quaternion.pde @@ -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); + } +} + diff --git a/App/console.pde b/App/console.pde new file mode 100644 index 0000000..3965d13 --- /dev/null +++ b/App/console.pde @@ -0,0 +1,35 @@ +class Console { + int _textSize = 10; + int _bufferSize = 40; + int _xOffset = 20; + int _yOffset = 20; + ArrayList _stringList; + + Console() { + _stringList = new ArrayList (); + } + + 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(); + } +} diff --git a/kdtest/.Camera.pde.swp b/kdtest/.Camera.pde.swp deleted file mode 100644 index fabd80fc2d734c315062bcae8974d4d94a39a885..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2O^6&t6vt}?5jF7>g5FAV>6v6^b~bB_*(^bggh&>b71vo5I-Tj-*=BpX+wPw1 ztU)~}DCDA7@#satdpw9251xg<9=wQ>lLrX|_Ta()tE&E*Yyvq3U&D`?dj0Cv>;HRI zyUX_F`SlAc^5yQL!1a_6Z~S&~{pO8d<~}?kL^{bb6^3-@p4Xc3%Q`FG@U=MJ?yRP9 zH;jkg%+!xniH0$V#FUV(~dFMU?uq!LT3a|pK04u->umY?AE5Hgo=nCY?ocII}@sNFx zH|^)#%%>gl2P?n|umY?AE5Hh{0;~WlzzVPetN<&(3Os-cc)k#~9ueZ?qeveA|1bXj z|K~{|{sMd8S8yGC2fhZMgB*MSE`hheMQ|4M!BOxu`1c7R_Q7rNC%6v21z&<|;0us| zb#Mkqa0u)l7UFlX2X26$z*j(l_rZDa3V0su9}?n6@ICkjdo+o*(Y)rNf_@38AMQ^=LyQ3z$&ZURV zpw(|VX1mzULcODd)Ex8{Devp#`X;$-C&-6e&Utqn`^xMA3Sj%z_7;g*tyXr6t#MXm1Fsm!+E+PB($=TdJpI%xBu^~;Wj=6V}cn(MM6Bi9}#lrPx^WV44-Vbj`?eWq?j zgD#u?-l#>_txK^?=hK}k<<%6gem7D!n?Z+n>_KL0mBzW3tK!f!E9kQ+)Mh$V?csGC zhsv?$v8R`uBtE&$98Tr2u!zHF^@0g(mPXe7?!2|oK{a2F==`xG^S*i&7C!?>LbGRS za^0Xe0c@rjNN3ZJ4O4m0R9;%rYN*55VOdW*E^A}Qx~}#+>NyO(ER!Z_c~y%40A1WU AcK`qY diff --git a/kdtest/Camera.pde b/kdtest/Camera.pde index 7a0157e..81b159c 100644 --- a/kdtest/Camera.pde +++ b/kdtest/Camera.pde @@ -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; } } @@ -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); } } diff --git a/kdtest/Point.pde b/kdtest/Point.pde index 7d4127d..542edd2 100644 --- a/kdtest/Point.pde +++ b/kdtest/Point.pde @@ -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; } } diff --git a/kdtest/Properties.pde b/kdtest/Properties.pde new file mode 100644 index 0000000..836635a --- /dev/null +++ b/kdtest/Properties.pde @@ -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" +}; diff --git a/kdtest/Tests.pde b/kdtest/Tests.pde new file mode 100644 index 0000000..71e8df2 --- /dev/null +++ b/kdtest/Tests.pde @@ -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; + } +} diff --git a/kdtest/kdtest.pde b/kdtest/kdtest.pde index 9d73a9b..70c4085 100644 --- a/kdtest/kdtest.pde +++ b/kdtest/kdtest.pde @@ -4,18 +4,18 @@ Mode statusLine; ArrayList 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(); @@ -25,48 +25,101 @@ void setup() { pointList = new ArrayList(); 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() { diff --git a/kdtest/web-export/kdtest.pde b/kdtest/web-export/kdtest.pde index 5d394c3..df471bd 100644 --- a/kdtest/web-export/kdtest.pde +++ b/kdtest/web-export/kdtest.pde @@ -1,71 +1,221 @@ -float angle = 0; Console con; +Camera cam; +Mode statusLine; + +ArrayList pointList; + +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(); + cam = new Camera(); + statusLine = new Mode(); + + pointList = new ArrayList(); + + drawCon = false; + drawHelp = false; + G_mode = 1; } void draw() { - //translate(width-mouseX, height-mouseY); - angle += 0.1; - background(120); + background(c_background); pushMatrix(); - translate(400,400); - rotate(angle); + cam.update(); drawGnoman(200); + for(Point p: pointList) { + p.draw(); + } popMatrix(); - con.print("haaaaaaasa"); - textSize(20); - text("asdasd",20,100); - con.draw(); + + if(drawCon) con.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() { - print("mouse pressed"); + if(G_mode == 4) { + pointList.add(cam.transform(new Point(mouseX, mouseY))); + } + cam.mousePressed(); + con.print("Pressed"); } void mouseDragged() { - print("mouse Dragged"); + cam.mouseDragged(); } -class Camera { + +void mouseReleased() { + cam.mouseReleased(); +} +class Camera { float _x; float _y; float _scale; float _angle; + float _oldX; + float _oldY; + float _oldAngle; + float _oldScale; + + int _initMX; + int _initMY; Camera() { - _x = width/2; - _y = height/2; + _x = 0.0; + _y = 0.0; _scale = 1.0; - _angle = 0.0; + _angle = 4 * PI; } void update() { - + translate(_x + width/2,_y + height/2); + rotate(_angle); + scale(_scale); } - void cam_mousePressed() { - + void mousePressed() { + _initMX = mouseX; + _initMY = mouseY; + _oldScale = _scale; + _oldX = _x; + _oldY = _y; + _oldAngle = _angle; } - void cam_mouseDragged() { + 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); + 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 = _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; + } + } + + void mouseReleased() { } + + Point transform(Point p) { + 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); + } } class Console { - int _textSize = 20; - int _bufferSize = 20; - int _xOffest = 20; + int _textSize = 10; + int _bufferSize = 40; + int _xOffset = 20; int _yOffset = 20; ArrayList _stringList; @@ -78,16 +228,153 @@ class Console { } void draw() { - rect(40,40, 100,100); + pushStyle(); + noStroke(); + fill(0,0,0,150); + rect(0,0,400,_yOffset + (_bufferSize + 1) * _textSize); + fill(30,250,30); textSize(_textSize); int index = 0; - //if (_stringList.size - _bufferSize > 0) { - //index = (_stringList.size - _bufferSize) - 1; - //} - while(index < _stringList.size) { - text("Asdasfa", _xOffset, _yOffset + i * _textSize); + 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(); + } +} +class Mode { + int _h = 30; + int _w = 60; + color c = color(256,256,256); + + Mode () { + + } + + void draw() { + pushStyle(); + fill(c); + rect(0,height,_w,height-_h); + fill(0); + textSize(30); + String s = new String(); + switch(G_mode) { + case 1: + s = "SCALE"; + break; + case 2: + s = "PAN"; + break; + case 3: + s = "ROTATE"; + break; + case 4: + s = "PLACE"; + break; + } + text(s,10, height-_h); + popStyle(); + } +} +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; + } +} + + +// 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" +}; +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; } }