-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragProcessor.java
268 lines (226 loc) · 9.43 KB
/
DragProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/***********************************************************************
* mt4j Copyright (c) 2008 - 2009 C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************************/
package org.mt4j.input.inputProcessors.componentProcessors.dragProcessor;
import org.mt4j.components.interfaces.IMTComponent3D;
import org.mt4j.input.inputData.InputCursor;
import org.mt4j.input.inputData.MTFingerInputEvt;
import org.mt4j.input.inputProcessors.IInputProcessor;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.AbstractComponentProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor;
import org.mt4j.util.math.Tools3D;
import org.mt4j.util.math.ToolsGeometry;
import org.mt4j.util.math.Vector3D;
import processing.core.PApplet;
/**
* The Class DragProcessor. For multi touch drag behaviour on components.
* Fires DragEvent gesture events.
* @author Christopher Ruff
*/
public class DragProcessor extends AbstractCursorProcessor {
/** The applet. */
private PApplet applet;
/** The dc. */
private DragContext dc;
/**
* Instantiates a new drag processor.
*
* @param graphicsContext the graphics context
*/
public DragProcessor(PApplet graphicsContext){
this.applet = graphicsContext;
this.setLockPriority(1);
this.setDebug(false);
// logger.setLevel(Level.DEBUG);
}
/* (non-Javadoc)
* @see org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor#cursorStarted(org.mt4j.input.inputData.InputCursor, org.mt4j.input.inputData.MTFingerInputEvt)
*/
@Override
public void cursorStarted(InputCursor cursor, MTFingerInputEvt fe) {
IMTComponent3D comp = fe.getTargetComponent();
InputCursor[] theLockedCursors = getLockedCursorsArray();
//if gesture isnt started and no other cursor on comp is locked by higher priority gesture -> start
if (theLockedCursors.length == 0 && this.canLock(getCurrentComponentCursorsArray())){
dc = new DragContext(cursor, comp);
if (!dc.isGestureAborted()){ //See if the drag couldnt start (i.e. cursor doesent hit component anymore etc)
//Lock this cursor with our priority
this.getLock(cursor);
logger.debug(this.getName() + " successfully locked cursor (id:" + cursor.getId() + ")");
this.fireGestureEvent(new DragEvent(this,MTGestureEvent.GESTURE_DETECTED, comp, cursor, dc.getLastPosition(), dc.getNewPosition()));
}else{
logger.debug(this.getName() + " gesture aborted, probably finger not on component!");
dc = null;
}
}
}
/* (non-Javadoc)
* @see org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor#cursorUpdated(org.mt4j.input.inputData.InputCursor, org.mt4j.input.inputData.MTFingerInputEvt)
*/
@Override
public void cursorUpdated(InputCursor c, MTFingerInputEvt fe) {
IMTComponent3D comp = fe.getTargetComponent();
if (getLockedCursors().contains(c)){
dc.updateDragPosition();
this.fireGestureEvent(new DragEvent(this, MTGestureEvent.GESTURE_UPDATED, comp, c, dc.getLastPosition(), dc.getNewPosition()));
}
}
/* (non-Javadoc)
* @see org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor#cursorEnded(org.mt4j.input.inputData.InputCursor, org.mt4j.input.inputData.MTFingerInputEvt)
*/
@Override
public void cursorEnded(InputCursor c, MTFingerInputEvt fe) {
IMTComponent3D comp = fe.getTargetComponent();
logger.debug(this.getName() + " INPUT_ENDED RECIEVED - MOTION: " + c.getId());
if (getLockedCursors().contains(c)){ //cursors was a actual gesture cursors
dc.updateDragPosition();
//Check if we can resume the gesture with another cursor
InputCursor[] availableCursors = getFreeComponentCursorsArray();
if (availableCursors.length > 0 && this.canLock(getCurrentComponentCursorsArray())){
InputCursor otherCursor = availableCursors[0];
DragContext newContext = new DragContext(otherCursor, comp);
if (!newContext.isGestureAborted()){
dc = newContext;
this.getLock(otherCursor);
}else{
this.fireGestureEvent(new DragEvent(this, MTGestureEvent.GESTURE_ENDED, comp, c, dc.getLastPosition(), dc.getNewPosition()));
}
}else{
this.fireGestureEvent(new DragEvent(this, MTGestureEvent.GESTURE_ENDED, comp, c, dc.getLastPosition(), dc.getNewPosition()));
}
}
}
/* (non-Javadoc)
* @see org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor#cursorLocked(org.mt4j.input.inputData.InputCursor, org.mt4j.input.inputProcessors.IInputProcessor)
*/
@Override
public void cursorLocked(InputCursor c, IInputProcessor p) {
if (p instanceof AbstractComponentProcessor){
logger.debug(this.getName() + " Recieved MOTION LOCKED by (" + ((AbstractComponentProcessor)p).getName() + ") - cursors ID: " + c.getId());
}else{
logger.debug(this.getName() + " Recieved MOTION LOCKED by higher priority signal - cursors ID: " + c.getId());
}
if (dc != null && dc.getCursor().equals(c)){
dc = null;
logger.debug(this.getName() + " cursors:" + c.getId() + " CURSOR LOCKED. Was an locked cursor in this gesture!");
}
}
/* (non-Javadoc)
* @see org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor#cursorUnlocked(org.mt4j.input.inputData.InputCursor)
*/
@Override
public void cursorUnlocked(InputCursor c) {
logger.debug(this.getName() + " Recieved UNLOCKED signal for cursors ID: " + c.getId());
if (getLockedCursors().size() >= 1){ //we dont need the unlocked cursors, gesture still in progress
return;
}
if (getFreeComponentCursors().size() > 0 && this.canLock(getCurrentComponentCursorsArray())){
DragContext newContext = new DragContext(c, c.getTarget());
if (!newContext.isGestureAborted()){
dc = newContext;
this.getLock(c);
logger.debug(this.getName() + " can resume its gesture with cursors: " + c.getId());
}else{
dc = null;
logger.debug(this.getName() + " we could NOT start gesture - cursors not on component: " + c.getId());
}
}else{
logger.debug(this.getName() + " still in progress - we dont need the unlocked cursors" );
}
}
/**
* The Class DragContext.
* @author Christopher Ruff
*/
private class DragContext {
/** The start position. */
private Vector3D startPosition;
/** The last position. */
private Vector3D lastPosition;
/** The new position. */
private Vector3D newPosition;
/** The drag object. */
private IMTComponent3D dragObject;
/** The m. */
private InputCursor m;
/** The gesture aborted. */
private boolean gestureAborted;
/** The drag plane normal. */
private Vector3D dragPlaneNormal;
/**
* Instantiates a new drag context.
*
* @param m the m
* @param dragObject the drag object
*/
public DragContext(InputCursor m, IMTComponent3D dragObject){
this.dragObject = dragObject;
this.m = m;
gestureAborted = false;
//Calculate the normal of the plane we will be dragging at (useful if camera isnt default)
this.dragPlaneNormal = dragObject.getViewingCamera().getPosition().getSubtracted(dragObject.getViewingCamera().getViewCenterPos()).normalizeLocal();
//Set the Drag Startposition
Vector3D interSectP = getIntersection(applet, dragObject, m);
if (interSectP != null)
this.startPosition = interSectP;
else{
logger.warn(getName() + " Drag StartPoint Null -> aborting drag");
gestureAborted = true;
this.startPosition = new Vector3D(0,0,0); //TODO ABORT GESTURE!
//abortGesture(m); //TODO in anderen analyzern auch machen
}
this.newPosition = startPosition.getCopy();
this.updateDragPosition();
//Set the Drags lastPostition (the last one before the new one)
this.lastPosition = startPosition.getCopy();
}
public void updateDragPosition(){
if (dragObject == null || dragObject.getViewingCamera() == null){ //IF component was destroyed while gesture still active
this.gestureAborted = true;
return ;
}
Vector3D newPos = ToolsGeometry.getRayPlaneIntersection(
Tools3D.getCameraPickRay(applet, dragObject, m.getCurrentEvtPosX(), this.startPosition.getY()),
dragPlaneNormal,
startPosition);
if (newPos != null){
lastPosition = newPosition;
newPosition = newPos;
}
}
public Vector3D getLastPosition() {
return lastPosition;
}
public Vector3D getNewPosition() {
return newPosition;
}
public boolean isGestureAborted() {
return gestureAborted;
}
public InputCursor getCursor(){
return this.m;
}
}
/* (non-Javadoc)
* @see org.mt4j.input.inputProcessors.componentProcessors.AbstractComponentProcessor#getName()
*/
@Override
public String getName() {
return "Drag Processor";
}
}