Skip to content

Commit

Permalink
added pen owner and clip to work with NEWT
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Oct 14, 2015
1 parent 3dc3208 commit 2c6df1f
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .classpath
Expand Up @@ -4,7 +4,7 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/processing-core"/>
<classpathentry kind="lib" path="lib/jpen-2.jar"/>
<classpathentry kind="lib" path="/processing-core/library/gluegen-rt.jar"/>
<classpathentry kind="lib" path="/processing-core/library/jogl-all.jar"/>
<classpathentry kind="lib" path="/processing-core/library/gluegen-rt.jar"/>
<classpathentry kind="output" path="resources/code"/>
</classpath>
8 changes: 4 additions & 4 deletions resources/build.properties
Expand Up @@ -35,7 +35,7 @@ classpath.local.location=/Applications/Processing.app/Contents/Java/core/library
# and project classpath. Use a comma as delimiter. These jar files must be
# inside your classpath.local.location folder.

classpath.local.include=core.jar jpen-2.jar
classpath.local.include=core.jar jpen-2.jar jogl-all.jar gluegen-rt.jar


# Add processing's libraries folder to the classpath.
Expand Down Expand Up @@ -131,12 +131,12 @@ source.repository=https://github.com/codeanticode/tablet.git
# This is used to compare different versions of the same library, and check if
# an update is available.

library.version=5
library.version=6


# The version as the user will see it.

library.prettyVersion=2.0-alpha1
library.prettyVersion=2.0-alpha2


# The min and max revision of Processing compatible with your library.
Expand All @@ -154,7 +154,7 @@ compatible.maxRevision=0
# against. This information is only used in the generated webpage.

tested.platform=osx,windows,linux
tested.processingVersion=3.0b5
tested.processingVersion=3.0


# Additional information for the generated webpage.
Expand Down
30 changes: 22 additions & 8 deletions src/codeanticode/tablet/Tablet.java
Expand Up @@ -40,6 +40,10 @@
import java.awt.Canvas;
import java.lang.reflect.*;

import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;

/**
*
* This class extends the PenListener class of the JPen library by adding getter
Expand Down Expand Up @@ -109,18 +113,28 @@ public Tablet(PApplet parent) {
welcome();

PSurface surf = parent.getSurface();
if (parent.g instanceof PGraphicsOpenGL) {
throw new RuntimeException("The OpenGL renderer is not supported by the Tablet library");
if (parent.g instanceof PGraphicsOpenGL) {
// Using parent with an OpenGL renderer results in no tablet events being
// detected, so will try to get the AWT canvas associated to the GL
// surface, if any.
// PSurfaceJOGL surf = (PSurfaceJOGL)parent.getSurface();
// Object obj = surf.getNative();
// if (obj instanceof Canvas) {
// AwtPenToolkit.addPenListener((Canvas)obj, this);
// } else {
// AwtPenToolkit.addPenListener(parent, this);
GLWindow win = (GLWindow)surf.getNative();
win.addWindowListener(new WindowAdapter() {
@Override
public void windowDestroyNotify(WindowEvent ev) {
synchronized(Tablet.class) {
Tablet.class.notify();
}
}
});
pm = new PenManager(new WindowPenOwner(win));
pm.pen.addListener(this);

// try {
// Tablet.class.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }

} else if (parent.g instanceof PGraphicsFX2D) {
throw new RuntimeException("The JavaFX renderer is not supported by the Tablet library");
// AwtPenToolkit.addPenListener(parent, this);
Expand Down
34 changes: 34 additions & 0 deletions src/codeanticode/tablet/WindowPenClip.java
@@ -0,0 +1,34 @@
package codeanticode.tablet;

import com.jogamp.newt.Window;
import java.awt.geom.Point2D;
import java.awt.Point;
import jpen.owner.PenClip;

final class WindowPenClip
implements PenClip {

final WindowPenOwner windowPenOwner;

public WindowPenClip(WindowPenOwner windowPenOwner) {
this.windowPenOwner=windowPenOwner;
}

@Override
public void evalLocationOnScreen(Point pointOnScreen) {
//-> called holding the pen scheduler lock
pointOnScreen.x=pointOnScreen.y=0;
com.jogamp.nativewindow.util.Point joglPoint=windowPenOwner.getWindow().getLocationOnScreen(null);
pointOnScreen.x=joglPoint.getX();
pointOnScreen.y=joglPoint.getY();
}

@Override
public boolean contains(Point2D.Float point) {
//-> called holding the pen scheduler lock
if(point.x<0 || point.y<0)
return false;
Window window=windowPenOwner.getWindow();
return point.x<=window.getWidth() && point.y<=window.getHeight();
}
}
95 changes: 95 additions & 0 deletions src/codeanticode/tablet/WindowPenOwner.java
@@ -0,0 +1,95 @@
package codeanticode.tablet;

import com.jogamp.newt.event.MouseAdapter;
import com.jogamp.newt.event.MouseEvent;
import com.jogamp.newt.Window;
import java.util.Arrays;
import java.util.Collection;
import jpen.owner.PenClip;
import jpen.owner.PenOwner;
import jpen.PenEvent;
import jpen.PenProvider;
import jpen.provider.osx.CocoaProvider;
import jpen.provider.wintab.WintabProvider;
import jpen.provider.xinput.XinputProvider;

public class WindowPenOwner
implements PenOwner {

private PenManagerHandle penManagerHandle;
private final WindowPenClip WindowPenClip=new WindowPenClip(this);
private final Window window;
private volatile boolean mayBeDraggingOut;

public WindowPenOwner(Window window) {
this.window=window;
}

public Window getWindow() {
return window;
}

@Override
public void setPenManagerHandle(final PenManagerHandle penManagerHandle) {
this.penManagerHandle=penManagerHandle;
window.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent ev) {
mayBeDraggingOut=true;
// escape from the EDT to avoid thread-safety issues...
new Thread(new Runnable() {
public void run() {
penManagerHandle.setPenManagerPaused(false);
}
}).start();
}

@Override
public void mouseExited(MouseEvent ev) {
mayBeDraggingOut=false;
// escape from the EDT to avoid thread-safety issues...
// Note: this has a side effect: some level events continue to
// be fired for a "very short time" after the mouse has exited,
// we would have to modify jpen internals to filter them out if necessary...
// let me know if this needs to be done for your use case.
new Thread(new Runnable() {
public void run() {
penManagerHandle.setPenManagerPaused(true);
}
}).start();
}
});
}

@Override
public Collection<PenProvider.Constructor> getPenProviderConstructors() {
return Arrays.asList(
new PenProvider.Constructor[] {
//Note: do you need a newt mouse/keyboard provider (so that regular newt mouse events/keyboard are fired as jpen events too)?
new XinputProvider.Constructor(),
new WintabProvider.Constructor(),
new CocoaProvider.Constructor(),
}
);
}

@Override
public PenClip getPenClip() {
return WindowPenClip;
}

@Override
public boolean isDraggingOut() {
return mayBeDraggingOut;
}

@Override
public Object evalPenEventTag(PenEvent ev) {
return null;
}

@Override
public boolean enforceSinglePenManager() {
return true;
}
}

0 comments on commit 2c6df1f

Please sign in to comment.