Skip to content

Commit

Permalink
Added support for using PeasyCam on offscreen PGraphics objects
Browse files Browse the repository at this point in the history
  • Loading branch information
additv committed Dec 4, 2013
1 parent 8d226be commit b0e0d9d
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/peasy/PeasyCam.java
Expand Up @@ -41,8 +41,9 @@ private static enum Constraint {
YAW, PITCH, ROLL, SUPPRESS_ROLL
}

private final PApplet p;

private final PGraphics p;
private final PApplet parent;

private final double startDistance;
private final Vector3D startCenter;

Expand Down Expand Up @@ -100,14 +101,24 @@ public void handleWheel(final int delta) {
private final PMatrix3D originalMatrix; // for HUD restore

public final String VERSION = "200";

public PeasyCam(final PApplet parent, final double distance) {
this(parent, 0, 0, 0, distance);
this(parent, parent.g, 0, 0, 0, distance);
}

public PeasyCam(final PApplet parent, final double lookAtX, final double lookAtY,
final double lookAtZ, final double distance) {
this.p = parent;
this(parent, parent.g, lookAtX, lookAtY, lookAtZ, distance);
}

public PeasyCam(final PApplet parent, final PGraphics pg, final double distance) {
this(parent, pg, 0, 0, 0, distance);
}

public PeasyCam(final PApplet parent, PGraphics pg, final double lookAtX, final double lookAtY,
final double lookAtZ, final double distance) {
this.parent = parent;
this.p = pg;
this.startCenter = this.center = new Vector3D(lookAtX, lookAtY, lookAtZ);
this.startDistance = this.distance = distance;
this.rotation = new Rotation();
Expand Down Expand Up @@ -167,11 +178,11 @@ public void setActive(final boolean active) {
}
isActive = active;
if (isActive) {
p.registerMethod("mouseEvent", peasyEventListener);
p.registerMethod("keyEvent", peasyEventListener);
parent.registerMethod("mouseEvent", peasyEventListener);
parent.registerMethod("keyEvent", peasyEventListener);
} else {
p.unregisterMethod("mouseEvent", peasyEventListener);
p.unregisterMethod("keyEvent", peasyEventListener);
parent.unregisterMethod("mouseEvent", peasyEventListener);
parent.unregisterMethod("keyEvent", peasyEventListener);
}
}

Expand Down Expand Up @@ -279,8 +290,8 @@ public void mouseEvent(final MouseEvent e) {
}
break;
case MouseEvent.DRAG:
final double dx = p.mouseX - p.pmouseX;
final double dy = p.mouseY - p.pmouseY;
final double dx = parent.mouseX - parent.pmouseX;
final double dy = parent.mouseY - parent.pmouseY;

if (e.isShiftDown()) {
if (dragConstraint == null && Math.abs(dx - dy) > 1) {
Expand All @@ -293,7 +304,7 @@ public void mouseEvent(final MouseEvent e) {
dragConstraint = null;
}

final int b = p.mouseButton;
final int b = parent.mouseButton;
if (centerDragHandler != null
&& (b == PConstants.CENTER || (b == PConstants.LEFT && e
.isMetaDown()))) {
Expand Down Expand Up @@ -324,9 +335,9 @@ private void mouseRotate(final double dx, final double dy) {
final int xSign = dx > 0 ? -1 : 1;
final int ySign = dy < 0 ? -1 : 1;

final double eccentricity = Math.abs((p.height / 2d) - p.mouseY)
final double eccentricity = Math.abs((p.height / 2d) - parent.mouseY)
/ (p.height / 2d);
final double rho = Math.abs((p.width / 2d) - p.mouseX) / (p.width / 2d);
final double rho = Math.abs((p.width / 2d) - parent.mouseX) / (p.width / 2d);

if (dragConstraint == null || dragConstraint == Constraint.YAW
|| dragConstraint == Constraint.SUPPRESS_ROLL) {
Expand All @@ -345,13 +356,13 @@ private void mouseRotate(final double dx, final double dy) {
final double adz = Math.abs(dy) * rho;
final Vector3D vz = u.add(new Vector3D(0, adz, 0));
rotateZ.impulse(Vector3D.angle(u, vz) * -ySign
* (p.mouseX < p.width / 2 ? -1 : 1));
* (parent.mouseX < p.width / 2 ? -1 : 1));
}
{
final double adz = Math.abs(dx) * eccentricity;
final Vector3D vz = u.add(new Vector3D(0, adz, 0));
rotateZ.impulse(Vector3D.angle(u, vz) * xSign
* (p.mouseY > p.height / 2 ? -1 : 1));
* (parent.mouseY > p.height / 2 ? -1 : 1));
}
}
}
Expand Down Expand Up @@ -457,7 +468,7 @@ public void rotateZ(final double angle) {
}

PApplet getApplet() {
return p;
return parent;
}

public CameraState getState() {
Expand Down Expand Up @@ -596,16 +607,16 @@ protected AbstractInterp(final long timeInMillis) {
}

void start() {
startTime = p.millis();
p.registerMethod("draw", this);
startTime = parent.millis();
parent.registerMethod("draw", this);
}

void cancel() {
p.unregisterMethod("draw", this);
parent.unregisterMethod("draw", this);
}

public void draw() {
final double t = (p.millis() - startTime) / timeInMillis;
final double t = (parent.millis() - startTime) / timeInMillis;
if (t > .99) {
cancel();
setEndState();
Expand Down

0 comments on commit b0e0d9d

Please sign in to comment.