9 |
/** A class to send mouse events to the listeners that want them. |
/** A class to send mouse events to the listeners that want them. |
10 |
*/ |
*/ |
11 |
public class MouseMultiplexer { |
public class MouseMultiplexer { |
12 |
|
/** A constant representing the horizontal direction. |
13 |
|
*/ |
14 |
public static final int HORIZONTAL = 1; |
public static final int HORIZONTAL = 1; |
15 |
|
/** A constant representing the vertical direction. |
16 |
|
*/ |
17 |
public static final int VERTICAL = 2; |
public static final int VERTICAL = 2; |
18 |
|
|
19 |
public final static int MAXBUTTON = 3; |
/** The largest button number accepted plus one. |
20 |
|
*/ |
21 |
|
public final static int MAXBUTTON = 4; |
22 |
|
|
23 |
|
/** The modifier mask for shift. |
24 |
|
*/ |
25 |
public final static int SHIFT_MASK = 1; |
public final static int SHIFT_MASK = 1; |
26 |
|
/** The modifier mask for control. |
27 |
|
*/ |
28 |
public final static int CONTROL_MASK = 2; |
public final static int CONTROL_MASK = 2; |
29 |
|
/** The modifier mask for alt. |
30 |
|
*/ |
31 |
public final static int ALT_MASK = 4; |
public final static int ALT_MASK = 4; |
32 |
|
|
33 |
|
/** The maximum modifier mask plus one. |
34 |
|
*/ |
35 |
public final static int MAXMASK = 8; |
public final static int MAXMASK = 8; |
36 |
|
|
37 |
|
/** A constant that is all modifier masks ored together. |
38 |
|
*/ |
39 |
private final static int ALLMASK = 7; |
private final static int ALLMASK = 7; |
40 |
|
|
41 |
//-- Internal data structures |
//-- Internal data structures |
49 |
private RelativeAxisListener[] wheelListeners = |
private RelativeAxisListener[] wheelListeners = |
50 |
new RelativeAxisListener[ MAXMASK ]; |
new RelativeAxisListener[ MAXMASK ]; |
51 |
|
|
52 |
//-- Helper class which is used to send events to RelativeAxisListeners |
/** Helper class which is used to send events to RelativeAxisListeners. |
53 |
|
*/ |
54 |
private class RelAxisAdapter implements MousePressListener, MouseDragListener { |
private class RelAxisAdapter implements MousePressListener, MouseDragListener { |
55 |
|
/** The constant to scale the motion with. |
56 |
int curx, cury; |
*/ |
57 |
float multx, multy; |
float multx, multy; |
58 |
|
/** The listeners to call when dragged. |
59 |
|
*/ |
60 |
RelativeAxisListener listx, listy; |
RelativeAxisListener listx, listy; |
61 |
|
|
62 |
|
/** The previous mouse position. |
63 |
|
*/ |
64 |
|
int curx, cury; |
65 |
|
|
66 |
public MouseDragListener pressed(int x, int y) { return this; } |
public MouseDragListener pressed(int x, int y) { return this; } |
67 |
|
|
68 |
public void startDrag(int x, int y) { |
public void startDrag(int x, int y) { |
83 |
|
|
84 |
} |
} |
85 |
|
|
86 |
|
/** If dragging, the draglistener for the current drag. |
87 |
|
* Otherwise null. |
88 |
|
*/ |
89 |
MouseDragListener currentDrag; |
MouseDragListener currentDrag; |
90 |
|
/** If dragging, the mouse button number that started the drag. |
91 |
|
* Otherwise null. |
92 |
|
*/ |
93 |
int currentDragButton; |
int currentDragButton; |
94 |
|
|
95 |
//-- Public API |
//-- Public API |
96 |
|
|
97 |
|
/** Set a listener for mouse presses (for starting drags). |
98 |
|
*/ |
99 |
public void setListener(int button, int modifiers, String description, MousePressListener l) { |
public void setListener(int button, int modifiers, String description, MousePressListener l) { |
100 |
mousePressListeners[ modifiers + MAXMASK * button ] = l; |
mousePressListeners[ modifiers + MAXMASK * button ] = l; |
101 |
} |
} |
102 |
|
|
103 |
public void setListener(int button, int modifiers, String description, MouseClickListener l) { |
/** Set a listener for mouse drags, translating one of the axes of the drag |
104 |
mouseClickListeners[ modifiers + MAXMASK * button ] = l; |
* to the RelativeAxisListener. |
105 |
} |
*/ |
106 |
public void setListener(int button, int modifiers, int dir, float multiplier, |
public void setListener(int button, int modifiers, int dir, float multiplier, |
107 |
String description, RelativeAxisListener l) { |
String description, RelativeAxisListener l) { |
108 |
int ind = modifiers + MAXMASK * button; |
int ind = modifiers + MAXMASK * button; |
118 |
} |
} |
119 |
} |
} |
120 |
|
|
121 |
|
/** Set a listener for mouse clicks. |
122 |
|
*/ |
123 |
|
public void setListener(int button, int modifiers, String description, MouseClickListener l) { |
124 |
|
mouseClickListeners[ modifiers + MAXMASK * button ] = l; |
125 |
|
} |
126 |
|
|
127 |
|
/** Set a listener for the mouse wheel. |
128 |
|
*/ |
129 |
public void setWheelListener(int modifiers, String description, RelativeAxisListener l) { |
public void setWheelListener(int modifiers, String description, RelativeAxisListener l) { |
130 |
wheelListeners[modifiers] = l; |
wheelListeners[modifiers] = l; |
131 |
} |
} |
132 |
|
|
133 |
|
/** Send an event to the correct listener. |
134 |
|
* @return true if the event was used by this multiplexer. |
135 |
|
*/ |
136 |
public boolean deliverEvent(VobMouseEvent e) { |
public boolean deliverEvent(VobMouseEvent e) { |
137 |
if(e.getType() == e.MOUSE_PRESSED) { |
if(e.getType() == e.MOUSE_PRESSED) { |
138 |
// Ignore second button while dragging |
// Ignore second button while dragging |