Skip to content

Commit 1015837

Browse files
committed
preliminary implementation of wheel events (issue #1423)
1 parent 1aee130 commit 1015837

5 files changed

Lines changed: 108 additions & 21 deletions

File tree

core/src/processing/core/PApplet.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.awt.event.MouseAdapter;
4040
import java.awt.event.MouseListener;
4141
import java.awt.event.MouseMotionListener;
42+
import java.awt.event.MouseWheelEvent;
43+
import java.awt.event.MouseWheelListener;
4244
import java.awt.event.WindowAdapter;
4345
import java.awt.event.WindowEvent;
4446
import java.awt.image.*;
@@ -153,7 +155,7 @@
153155
*/
154156
public class PApplet extends Applet
155157
implements PConstants, Runnable,
156-
MouseListener, MouseMotionListener, KeyListener, FocusListener
158+
MouseListener, MouseWheelListener, MouseMotionListener, KeyListener, FocusListener
157159
{
158160
/**
159161
* Full name of the Java version (i.e. 1.5.0_11).
@@ -2428,6 +2430,7 @@ synchronized public void noLoop() {
24282430

24292431
public void addListeners(Component comp) {
24302432
comp.addMouseListener(this);
2433+
comp.addMouseWheelListener(this);
24312434
comp.addMouseMotionListener(this);
24322435
comp.addKeyListener(this);
24332436
comp.addFocusListener(this);
@@ -2451,6 +2454,7 @@ public void addListeners(Component comp) {
24512454

24522455
public void removeListeners(Component comp) {
24532456
comp.removeMouseListener(this);
2457+
comp.removeMouseWheelListener(this);
24542458
comp.removeMouseMotionListener(this);
24552459
comp.removeKeyListener(this);
24562460
comp.removeFocusListener(this);
@@ -2685,6 +2689,9 @@ protected void handleMouseEvent(MouseEvent event) {
26852689
case MouseEvent.EXIT:
26862690
mouseExited(event);
26872691
break;
2692+
case MouseEvent.WHEEL:
2693+
mouseWheel(event);
2694+
break;
26882695
}
26892696

26902697
if ((event.getAction() == MouseEvent.DRAG) ||
@@ -2695,12 +2702,30 @@ protected void handleMouseEvent(MouseEvent event) {
26952702
}
26962703

26972704

2705+
static protected Method preciseWheelMethod;
2706+
static {
2707+
// Class<?> callbackClass = callbackObject.getClass();
2708+
// Method selectMethod =
2709+
// callbackClass.getMethod(callbackMethod, new Class[] { File.class });
2710+
// selectMethod.invoke(callbackObject, new Object[] { selectedFile });
2711+
try {
2712+
preciseWheelMethod = MouseWheelEvent.class.getMethod("getPreciseWheelRotation", new Class[] { });
2713+
} catch (Exception e) {
2714+
// ignored, the method will just be set to null
2715+
}
2716+
}
2717+
2718+
26982719
/**
26992720
* Figure out how to process a mouse event. When loop() has been
27002721
* called, the events will be queued up until drawing is complete.
27012722
* If noLoop() has been called, then events will happen immediately.
27022723
*/
27032724
protected void nativeMouseEvent(java.awt.event.MouseEvent nativeEvent) {
2725+
// the 'amount' is the number of button clicks for a click event,
2726+
// or the number of steps/clicks on the wheel for a mouse wheel event.
2727+
float peAmount = nativeEvent.getClickCount();
2728+
27042729
int peAction = 0;
27052730
switch (nativeEvent.getID()) {
27062731
case java.awt.event.MouseEvent.MOUSE_PRESSED:
@@ -2724,6 +2749,19 @@ protected void nativeMouseEvent(java.awt.event.MouseEvent nativeEvent) {
27242749
case java.awt.event.MouseEvent.MOUSE_EXITED:
27252750
peAction = MouseEvent.EXIT;
27262751
break;
2752+
case java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL:
2753+
peAction = MouseEvent.WHEEL;
2754+
if (preciseWheelMethod != null) {
2755+
try {
2756+
peAmount = ((Double) preciseWheelMethod.invoke(nativeEvent, (Object[]) null)).floatValue();
2757+
} catch (Exception e) {
2758+
preciseWheelMethod = null;
2759+
}
2760+
}
2761+
if (preciseWheelMethod == null) {
2762+
peAmount = ((MouseWheelEvent) nativeEvent).getWheelRotation();
2763+
}
2764+
break;
27272765
}
27282766

27292767
//System.out.println(nativeEvent);
@@ -2776,7 +2814,7 @@ protected void nativeMouseEvent(java.awt.event.MouseEvent nativeEvent) {
27762814
peAction, peModifiers,
27772815
nativeEvent.getX(), nativeEvent.getY(),
27782816
peButton,
2779-
nativeEvent.getClickCount()));
2817+
peAmount));
27802818
}
27812819

27822820

@@ -2840,6 +2878,14 @@ public void mouseMoved(java.awt.event.MouseEvent e) {
28402878
}
28412879

28422880

2881+
/**
2882+
* @nowebref
2883+
*/
2884+
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) {
2885+
nativeMouseEvent(e);
2886+
}
2887+
2888+
28432889
/**
28442890
* ( begin auto-generated from mousePressed.xml )
28452891
*
@@ -2987,6 +3033,14 @@ public void mouseExited(MouseEvent event) {
29873033
}
29883034

29893035

3036+
public void mouseWheel() { }
3037+
3038+
3039+
public void mouseWheel(MouseEvent event) {
3040+
mouseWheel();
3041+
}
3042+
3043+
29903044

29913045
//////////////////////////////////////////////////////////////
29923046

core/src/processing/event/MouseEvent.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ public class MouseEvent extends Event {
3333
static public final int MOVE = 5;
3434
static public final int ENTER = 6;
3535
static public final int EXIT = 7;
36+
static public final int WHEEL = 8;
3637

3738
protected int x, y;
3839
protected int button;
39-
protected int clickCount;
40+
// protected int clickCount;
41+
protected float amount;
4042

4143

4244
// public MouseEvent(int x, int y) {
@@ -48,13 +50,14 @@ public class MouseEvent extends Event {
4850

4951
public MouseEvent(Object nativeObject,
5052
long millis, int action, int modifiers,
51-
int x, int y, int button, int clickCount) {
53+
int x, int y, int button, float amount) { //int clickCount) {
5254
super(nativeObject, millis, action, modifiers);
5355
this.flavor = MOUSE;
5456
this.x = x;
5557
this.y = y;
5658
this.button = button;
57-
this.clickCount = clickCount;
59+
//this.clickCount = clickCount;
60+
this.amount = amount;
5861
}
5962

6063

@@ -79,8 +82,18 @@ public int getButton() {
7982
// }
8083

8184

85+
@Deprecated
8286
public int getClickCount() {
83-
return clickCount;
87+
return (int) amount; //clickCount;
88+
}
89+
90+
91+
/**
92+
* Number of clicks for mouse button events, or the number of steps (positive
93+
* or negative depending on direction) for a mouse wheel event.
94+
*/
95+
public float getAmount() {
96+
return amount;
8497
}
8598

8699

core/src/processing/opengl/PGL.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,11 +3244,15 @@ protected void nativeMouseEvent(com.jogamp.newt.event.MouseEvent nativeEvent,
32443244
peButton = PConstants.RIGHT;
32453245
}
32463246

3247+
float peAmount = peAction == MouseEvent.WHEEL ?
3248+
nativeEvent.getWheelRotation() :
3249+
nativeEvent.getClickCount();
3250+
32473251
MouseEvent me = new MouseEvent(nativeEvent, nativeEvent.getWhen(),
32483252
peAction, peModifiers,
32493253
nativeEvent.getX(), nativeEvent.getY(),
32503254
peButton,
3251-
nativeEvent.getClickCount());
3255+
peAmount);
32523256

32533257
pg.parent.postEvent(me);
32543258
}
@@ -3335,7 +3339,7 @@ public void mouseExited(com.jogamp.newt.event.MouseEvent e) {
33353339
}
33363340
@Override
33373341
public void mouseWheelMoved(com.jogamp.newt.event.MouseEvent e) {
3338-
// Not supported in Processing.
3342+
nativeMouseEvent(e, MouseEvent.WHEEL);
33393343
}
33403344
}
33413345

core/todo.txt

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ X add functions for mousePressed(event) and keyPressed(event) et al
3333
X better to do this instead of bringing back the magic event
3434
X or implementing the magic event on Android
3535
X also problematic with it not being called now
36+
X loadBytes does not close input stream
37+
X http://code.google.com/p/processing/issues/detail?id=1542
38+
X add randomGaussian()
3639

3740
andres
3841
A P3D sketches failing to run
@@ -116,13 +119,31 @@ _ JSONObject.has(key) vs XML.hasAttribute(attr) vs HashMap.containsKey()
116119
_ and how it should be handled with hash/dict
117120
_ right now using hasKey().. in JSONObject
118121

119-
_ add randomGaussian()
120-
121-
_ noCursor() seems quite/somewhat broken
122-
X started some work, ignores 'invisible' already being set
122+
_ add mouse wheel support to 2.0 event system
123+
X this is fairly messy since desktop and JS behave a little differently
124+
o wheel event should subclass mouse (since position still relevant)
125+
X just another sub-event as part of mouse
126+
o might be more effort than it's worth?
127+
_ http://code.google.com/p/processing/issues/detail?id=1423
128+
X peasycam uses e.getWheelRotation()
129+
X js has a couple versions
130+
X http://www.javascriptkit.com/javatutors/onmousewheel.shtml
131+
X e.detail is 1 for 1 click up, -2 for 2 clicks down
132+
X e.wheelDelta is 120 for 1 click up, -240 for two clicks down
133+
X using float value (/120.0f)
134+
X high-precision method grabbed if 1.7 is in use
135+
_ test with actual
136+
_ add to the API docs
137+
_ decide on getAmount() and weirdness w/ clickCount
123138

139+
andres
140+
_ P2D/P3D sketches don't get focus until click
141+
_ also problem for Java2D when canvas is used?
142+
_ need to standardize canvas handling
124143
_ OpenGL/P3D sketches show graphical corruption
125144
_ http://code.google.com/p/processing/issues/detail?id=1452
145+
_ noCursor() seems quite/somewhat broken
146+
X started some work, ignores 'invisible' already being set
126147

127148
_ add 'gz' as one of the loadXxx() options
128149
_ 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
643664
_ http://www.html5rocks.com/en/mobile/touch.html
644665
_ decision: go with what looks like javascript/ios
645666
_ touchEvent(), gestureEvent()?
646-
_ add mouse wheel support to 2.0 event system
647-
_ this is fairly messy since desktop and JS behave a little differently
648-
_ wheel event should subclass mouse (since position still relevant)
649-
_ might be more effort than it's worth?
650-
_ http://code.google.com/p/processing/issues/detail?id=1423
651667

652668

653669
LIBRARIES / PGraphicsPDF

todo.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ X http://code.google.com/p/processing/issues/detail?id=1509
4343
X remove separate launch of QT movie creator
4444
X Don't open Changes page on the Wiki from command line
4545
X http://code.google.com/p/processing/issues/detail?id=1520
46+
X prevent inertia scrolling on OS X from making editor jumpy
4647

4748
manindra
4849
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
8586
_ excessive CPU usage of PDE after using library manager
8687
_ http://code.google.com/p/processing/issues/detail?id=1036
8788
_ confirmed to still be a problem with b5/6
88-
_ remove PdeKeyListener, roll it into the Java InputHandler for JEditTextArea
89-
_ move Java-specific InputHandler to its own subclass
90-
_ problem changing sketchbook to new location (e.g. from ex to regular)
91-
_ new libraries are not picked up
89+
_ new libraries not picked up when changing sketchbook location
9290
_ Contributed modes should show up in mode menu after installation
9391
_ http://code.google.com/p/processing/issues/detail?id=1466
9492
_ 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
9997
_ All I would need to do is update web/contrib_generate/sources.conf
10098
_ and the rest would happen automatically.
10199
_ alternating blue/white backgrounds aren't updated after changing filter
100+
_ remove PdeKeyListener, roll it into the Java InputHandler for JEditTextArea
101+
_ move Java-specific InputHandler to its own subclass
102102

103103

104104
2.0 FINAL / new interface

0 commit comments

Comments
 (0)