From f5ccad12b3c5bae1d94da9ca8dcae9b8f8b04e70 Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 19 Jan 2013 20:18:09 +0000 Subject: [PATCH] preliminary implementation of wheel events (issue #1423) --- core/src/processing/core/PApplet.java | 58 ++++++++++++++++++++++- core/src/processing/event/MouseEvent.java | 21 ++++++-- core/src/processing/opengl/PGL.java | 8 +++- core/todo.txt | 34 +++++++++---- todo.txt | 8 ++-- 5 files changed, 108 insertions(+), 21 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index c6164690a..44a4efc5a 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -39,6 +39,8 @@ import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.*; @@ -153,7 +155,7 @@ import javax.swing.JFileChooser; */ public class PApplet extends Applet implements PConstants, Runnable, - MouseListener, MouseMotionListener, KeyListener, FocusListener + MouseListener, MouseWheelListener, MouseMotionListener, KeyListener, FocusListener { /** * Full name of the Java version (i.e. 1.5.0_11). @@ -2428,6 +2430,7 @@ public class PApplet extends Applet public void addListeners(Component comp) { comp.addMouseListener(this); + comp.addMouseWheelListener(this); comp.addMouseMotionListener(this); comp.addKeyListener(this); comp.addFocusListener(this); @@ -2451,6 +2454,7 @@ public class PApplet extends Applet public void removeListeners(Component comp) { comp.removeMouseListener(this); + comp.removeMouseWheelListener(this); comp.removeMouseMotionListener(this); comp.removeKeyListener(this); comp.removeFocusListener(this); @@ -2685,6 +2689,9 @@ public class PApplet extends Applet case MouseEvent.EXIT: mouseExited(event); break; + case MouseEvent.WHEEL: + mouseWheel(event); + break; } if ((event.getAction() == MouseEvent.DRAG) || @@ -2695,12 +2702,30 @@ public class PApplet extends Applet } + static protected Method preciseWheelMethod; + static { +// Class callbackClass = callbackObject.getClass(); +// Method selectMethod = +// callbackClass.getMethod(callbackMethod, new Class[] { File.class }); +// selectMethod.invoke(callbackObject, new Object[] { selectedFile }); + try { + preciseWheelMethod = MouseWheelEvent.class.getMethod("getPreciseWheelRotation", new Class[] { }); + } catch (Exception e) { + // ignored, the method will just be set to null + } + } + + /** * Figure out how to process a mouse event. When loop() has been * called, the events will be queued up until drawing is complete. * If noLoop() has been called, then events will happen immediately. */ protected void nativeMouseEvent(java.awt.event.MouseEvent nativeEvent) { + // the 'amount' is the number of button clicks for a click event, + // or the number of steps/clicks on the wheel for a mouse wheel event. + float peAmount = nativeEvent.getClickCount(); + int peAction = 0; switch (nativeEvent.getID()) { case java.awt.event.MouseEvent.MOUSE_PRESSED: @@ -2724,6 +2749,19 @@ public class PApplet extends Applet case java.awt.event.MouseEvent.MOUSE_EXITED: peAction = MouseEvent.EXIT; break; + case java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL: + peAction = MouseEvent.WHEEL; + if (preciseWheelMethod != null) { + try { + peAmount = ((Double) preciseWheelMethod.invoke(nativeEvent, (Object[]) null)).floatValue(); + } catch (Exception e) { + preciseWheelMethod = null; + } + } + if (preciseWheelMethod == null) { + peAmount = ((MouseWheelEvent) nativeEvent).getWheelRotation(); + } + break; } //System.out.println(nativeEvent); @@ -2776,7 +2814,7 @@ public class PApplet extends Applet peAction, peModifiers, nativeEvent.getX(), nativeEvent.getY(), peButton, - nativeEvent.getClickCount())); + peAmount)); } @@ -2840,6 +2878,14 @@ public class PApplet extends Applet } + /** + * @nowebref + */ + public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { + nativeMouseEvent(e); + } + + /** * ( begin auto-generated from mousePressed.xml ) * @@ -2987,6 +3033,14 @@ public class PApplet extends Applet } + public void mouseWheel() { } + + + public void mouseWheel(MouseEvent event) { + mouseWheel(); + } + + ////////////////////////////////////////////////////////////// diff --git a/core/src/processing/event/MouseEvent.java b/core/src/processing/event/MouseEvent.java index 408ef2e95..6835f8c37 100644 --- a/core/src/processing/event/MouseEvent.java +++ b/core/src/processing/event/MouseEvent.java @@ -33,10 +33,12 @@ public class MouseEvent extends Event { static public final int MOVE = 5; static public final int ENTER = 6; static public final int EXIT = 7; + static public final int WHEEL = 8; protected int x, y; protected int button; - protected int clickCount; +// protected int clickCount; + protected float amount; // public MouseEvent(int x, int y) { @@ -48,13 +50,14 @@ public class MouseEvent extends Event { public MouseEvent(Object nativeObject, long millis, int action, int modifiers, - int x, int y, int button, int clickCount) { + int x, int y, int button, float amount) { //int clickCount) { super(nativeObject, millis, action, modifiers); this.flavor = MOUSE; this.x = x; this.y = y; this.button = button; - this.clickCount = clickCount; + //this.clickCount = clickCount; + this.amount = amount; } @@ -79,8 +82,18 @@ public class MouseEvent extends Event { // } + @Deprecated public int getClickCount() { - return clickCount; + return (int) amount; //clickCount; + } + + + /** + * Number of clicks for mouse button events, or the number of steps (positive + * or negative depending on direction) for a mouse wheel event. + */ + public float getAmount() { + return amount; } diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 4a01d1999..6756a2c30 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -3244,11 +3244,15 @@ public class PGL { peButton = PConstants.RIGHT; } + float peAmount = peAction == MouseEvent.WHEEL ? + nativeEvent.getWheelRotation() : + nativeEvent.getClickCount(); + MouseEvent me = new MouseEvent(nativeEvent, nativeEvent.getWhen(), peAction, peModifiers, nativeEvent.getX(), nativeEvent.getY(), peButton, - nativeEvent.getClickCount()); + peAmount); pg.parent.postEvent(me); } @@ -3335,7 +3339,7 @@ public class PGL { } @Override public void mouseWheelMoved(com.jogamp.newt.event.MouseEvent e) { - // Not supported in Processing. + nativeMouseEvent(e, MouseEvent.WHEEL); } } diff --git a/core/todo.txt b/core/todo.txt index d546e5629..ed808260f 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -33,6 +33,9 @@ X add functions for mousePressed(event) and keyPressed(event) et al X better to do this instead of bringing back the magic event X or implementing the magic event on Android X also problematic with it not being called now +X loadBytes does not close input stream +X http://code.google.com/p/processing/issues/detail?id=1542 +X add randomGaussian() andres A P3D sketches failing to run @@ -116,13 +119,31 @@ _ JSONObject.has(key) vs XML.hasAttribute(attr) vs HashMap.containsKey() _ and how it should be handled with hash/dict _ right now using hasKey().. in JSONObject -_ add randomGaussian() - -_ noCursor() seems quite/somewhat broken -X started some work, ignores 'invisible' already being set +_ add mouse wheel support to 2.0 event system +X this is fairly messy since desktop and JS behave a little differently +o wheel event should subclass mouse (since position still relevant) +X just another sub-event as part of mouse +o might be more effort than it's worth? +_ http://code.google.com/p/processing/issues/detail?id=1423 +X peasycam uses e.getWheelRotation() +X js has a couple versions +X http://www.javascriptkit.com/javatutors/onmousewheel.shtml +X e.detail is 1 for 1 click up, -2 for 2 clicks down +X e.wheelDelta is 120 for 1 click up, -240 for two clicks down +X using float value (/120.0f) +X high-precision method grabbed if 1.7 is in use +_ test with actual +_ add to the API docs +_ decide on getAmount() and weirdness w/ clickCount +andres +_ P2D/P3D sketches don't get focus until click +_ also problem for Java2D when canvas is used? +_ need to standardize canvas handling _ OpenGL/P3D sketches show graphical corruption _ http://code.google.com/p/processing/issues/detail?id=1452 +_ noCursor() seems quite/somewhat broken +X started some work, ignores 'invisible' already being set _ add 'gz' as one of the loadXxx() options _ helps .svgz case from being weird, also generally dealing w/ compressed data @@ -643,11 +664,6 @@ _ http://dvcs.w3.org/hg/webevents/raw-file/tip/touchevents.html _ http://www.html5rocks.com/en/mobile/touch.html _ decision: go with what looks like javascript/ios _ touchEvent(), gestureEvent()? -_ add mouse wheel support to 2.0 event system -_ this is fairly messy since desktop and JS behave a little differently -_ wheel event should subclass mouse (since position still relevant) -_ might be more effort than it's worth? -_ http://code.google.com/p/processing/issues/detail?id=1423 LIBRARIES / PGraphicsPDF diff --git a/todo.txt b/todo.txt index 404fcacc0..d1cf468dc 100644 --- a/todo.txt +++ b/todo.txt @@ -43,6 +43,7 @@ X http://code.google.com/p/processing/issues/detail?id=1509 X remove separate launch of QT movie creator X Don't open Changes page on the Wiki from command line X http://code.google.com/p/processing/issues/detail?id=1520 +X prevent inertia scrolling on OS X from making editor jumpy manindra M bug that was causing the Debugger to point to wrong break point line numbers @@ -85,10 +86,7 @@ _ http://code.google.com/p/processing/issues/detail?id=1212 _ excessive CPU usage of PDE after using library manager _ http://code.google.com/p/processing/issues/detail?id=1036 _ confirmed to still be a problem with b5/6 -_ remove PdeKeyListener, roll it into the Java InputHandler for JEditTextArea -_ move Java-specific InputHandler to its own subclass -_ problem changing sketchbook to new location (e.g. from ex to regular) -_ new libraries are not picked up +_ new libraries not picked up when changing sketchbook location _ Contributed modes should show up in mode menu after installation _ http://code.google.com/p/processing/issues/detail?id=1466 _ using "Add Library" requires restart of Processing before lib recognized @@ -99,6 +97,8 @@ _ list on the website would be generated using the same web service _ All I would need to do is update web/contrib_generate/sources.conf _ and the rest would happen automatically. _ alternating blue/white backgrounds aren't updated after changing filter +_ remove PdeKeyListener, roll it into the Java InputHandler for JEditTextArea +_ move Java-specific InputHandler to its own subclass 2.0 FINAL / new interface