PageRenderTime 51ms CodeModel.GetById 1ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/org/mt4jx/input/inputProcessors/componentProcessors/depthProcessor/DepthProcessor.java

http://mt4j.googlecode.com/
Java | 392 lines | 261 code | 85 blank | 46 comment | 35 complexity | 47207344fc063f52aee9a9cedb64e2aa MD5 | raw file
  1package org.mt4jx.input.inputProcessors.componentProcessors.depthProcessor;
  2
  3import java.io.File;
  4import java.util.ArrayList;
  5import java.util.Iterator;
  6import java.util.List;
  7
  8
  9import org.mt4j.components.MTCanvas;
 10import org.mt4j.components.MTComponent;
 11import org.mt4j.components.interfaces.IMTComponent3D;
 12import org.mt4j.components.visibleComponents.shapes.MTRectangle;
 13import org.mt4j.input.inputData.AbstractCursorInputEvt;
 14import org.mt4j.input.inputData.InputCursor;
 15import org.mt4j.input.inputProcessors.IInputProcessor;
 16import org.mt4j.input.inputProcessors.MTGestureEvent;
 17import org.mt4j.input.inputProcessors.componentProcessors.AbstractComponentProcessor;
 18import org.mt4j.input.inputProcessors.componentProcessors.AbstractCursorProcessor;
 19
 20import org.mt4j.util.camera.Icamera;
 21
 22import org.mt4j.util.math.Tools3D;
 23import org.mt4j.util.math.Vector3D;
 24import org.mt4jx.util.extension3D.VelocityMotionMapper;
 25
 26import processing.core.PApplet;
 27
 28public class DepthProcessor extends AbstractCursorProcessor {
 29
 30	private PApplet applet;
 31
 32	/** The dc. */
 33	private DepthContext dpc;
 34
 35	private List<InputCursor> depthCursors = new ArrayList<InputCursor>();
 36	/** The un used cursorss. */
 37	private List<InputCursor> unUsedCursors = new ArrayList<InputCursor>();
 38
 39	/** The locked cursorss. */
 40	private List<InputCursor> lockedCursors = new ArrayList<InputCursor>();
 41
 42	private MTCanvas canvas;
 43
 44	private Icamera cam;
 45	
 46	private MTRectangle visualHelper;
 47
 48	private IMTComponent3D targetComponent;
 49	
 50	/** if gesture is paused by collision detection*/
 51	private boolean gesturePaused = false;
 52	
 53	/** if gesture has been resumed after collision*/
 54	private boolean resumed = false;
 55		
 56	public DepthProcessor(PApplet graphicsContext, MTCanvas canvas, Icamera cam,IMTComponent3D targetComponent) {
 57		this.setLockPriority(1);
 58		this.applet = graphicsContext;
 59		this.setDebug(false);
 60		this.canvas = canvas;
 61		this.setTargetComponent(targetComponent);
 62		this.cam = cam;
 63	}
 64
 65	@Override
 66	public void cursorEnded(InputCursor inputCursor,
 67			AbstractCursorInputEvt positionEvent) {
 68
 69		IMTComponent3D comp = positionEvent.getTarget();
 70				
 71		logger.debug(this.getName() + " INPUT_ENDED RECIEVED - MOTION: "
 72				+ inputCursor.getId());
 73		
 74		if (lockedCursors.contains(inputCursor)) { // cursors was a actual
 75													// gesture cursors
 76					
 77			if(dpc!=null) //TODO correct handling of dpc creation 
 78			{
 79				dpc.updateDepthPosition();
 80			}
 81			//targetComponent = null; //set back target Component
 82			lockedCursors.remove(inputCursor);
 83			if (unUsedCursors.size() > 0) { // check if there are other cursorss
 84											// on the component, we could use
 85											// for depth drag
 86				InputCursor otherMotion = unUsedCursors.get(0); // TODO cycle
 87																// through all
 88																// available
 89																// unUsedCursors
 90																// and try to
 91																// claim one,
 92																// maybe the
 93																// first one is
 94																// claimed but
 95																// another isnt!
 96				if (this.canLock(otherMotion)) { // Check if we have the
 97													// priority to use this
 98													// cursors
 99					dpc = new DepthContext(otherMotion, comp);
100					if (!dpc.isGestureAborted()) {
101						this.getLock(otherMotion);
102						unUsedCursors.remove(otherMotion);
103						lockedCursors.add(otherMotion);
104						// TODO fire started? maybe not.. do we have to?
105					} else {
106						this.fireGestureEvent(new DepthGestureEvent(this,
107								MTGestureEvent.GESTURE_ENDED, getTargetComponent(), inputCursor,
108								dpc.getTranslationVect()));
109					}
110				} else {
111					this.fireGestureEvent(new DepthGestureEvent(this,
112							MTGestureEvent.GESTURE_ENDED, getTargetComponent(), inputCursor,
113							dpc.getTranslationVect()));
114				}
115			} else {
116				this.fireGestureEvent(new DepthGestureEvent(this,
117						MTGestureEvent.GESTURE_ENDED, getTargetComponent(), inputCursor,
118						dpc.getTranslationVect()));
119				
120			}
121			this.unLock(inputCursor); // FIXME TEST
122		} else { // cursors was not used for dragging
123			if (unUsedCursors.contains(inputCursor)) {
124				unUsedCursors.remove(inputCursor);
125			}
126		}
127
128	}
129
130	@Override
131	public void cursorLocked(InputCursor cursor,
132			IInputProcessor lockingprocessor) {
133		if (lockingprocessor instanceof AbstractComponentProcessor) {
134			logger.debug(this.getName() + " Recieved MOTION LOCKED by ("
135					+ ((AbstractComponentProcessor) lockingprocessor).getName()
136					+ ") - cursors ID: " + cursor.getId());
137		} else {
138			logger
139					.debug(this.getName()
140							+ " Recieved MOTION LOCKED by higher priority signal - cursors ID: "
141							+ cursor.getId());
142		}
143
144		if (lockedCursors.contains(cursor)) { // cursors was a actual gesture
145												// cursors
146			lockedCursors.remove(cursor);
147			// TODO fire ended evt?
148			unUsedCursors.add(cursor);
149			logger.debug(this.getName() + " cursors:" + cursor.getId()
150					+ " MOTION LOCKED. Was an active cursors in this gesture!");
151		} else { // TODO remove "else", it is pretty useless
152			if (unUsedCursors.contains(cursor)) {
153				logger
154						.debug(this.getName()
155								+ " MOTION LOCKED. But it was NOT an active cursors in this gesture!");
156			}
157		}
158	}
159
160	@Override
161	public void cursorStarted(InputCursor inputCursor,
162			AbstractCursorInputEvt positionEvent) {
163		IMTComponent3D comp = positionEvent.getTarget();
164		
165		if (lockedCursors.size() == 0) { 
166			dpc = new DepthContext(inputCursor, comp);
167		
168			if (this.canLock(inputCursor)) {
169					if (!dpc.isGestureAborted()) {
170						this.getLock(inputCursor);
171						lockedCursors.add(inputCursor);
172						InputCursor otherCursor = lockedCursors.get(0);
173						//dpc = new DepthContext(inputCursor,comp);
174						this.fireGestureEvent(new DepthGestureEvent(this,
175								MTGestureEvent.GESTURE_STARTED, getTargetComponent(), inputCursor,
176								dpc.getTranslationVect()));
177					}
178					depthCursors.add(inputCursor);
179			}			
180		} else if (lockedCursors.size() > 0) {
181			unUsedCursors.add(inputCursor);
182		}
183	}
184
185	public void cursorUnlocked(InputCursor cursor) {
186		logger
187				.debug(this.getName()
188						+ " Recieved UNLOCKED signal for cursors ID: "
189						+ cursor.getId());
190		if (lockedCursors.size() >= 1) { // we dont need the unlocked cursors,
191											// gesture still in progress
192			return;
193		}
194
195		if (unUsedCursors.contains(cursor)) {
196			if (this.canLock(cursor)) {
197				dpc = new DepthContext(cursor, getTargetComponent());
198				if (!dpc.isGestureAborted()) {
199					this.getLock(cursor);
200					unUsedCursors.remove(cursor);
201					lockedCursors.add(cursor);
202					// TODO fire started? maybe not.. do we have to?
203					logger.debug(this.getName()
204							+ " can resume its gesture with cursors: "
205							+ cursor.getId());
206				} else {
207					dpc = null;
208					logger
209							.debug(this.getName()
210									+ " we could NOT start gesture - cursors not on component: "
211									+ cursor.getId());
212				}
213			} else {
214				logger
215						.debug(this.getName()
216								+ " still in progress - we dont need the unlocked cursors");
217			}
218		}
219
220	}
221
222	public void cursorUpdated(InputCursor inputCursor,
223			AbstractCursorInputEvt positionEvent) {
224		IMTComponent3D comp = positionEvent.getTarget();
225		
226		
227		Vector3D vec = positionEvent.getTarget().getIntersectionGlobal(
228				Tools3D.getCameraPickRay(applet, comp, inputCursor.getCurrentEvent().getX(), inputCursor.getCurrentEvent().getY()));
229		
230		if(vec!=null)
231		{
232			if(lockedCursors.size()==0)
233			{
234				return;
235			}
236					
237			if (lockedCursors.size() == 1) {
238				if(lockedCursors.get(0) == inputCursor) {			
239				
240					dpc.updateDepthPosition();						
241					this.fireGestureEvent(new DepthGestureEvent(this,
242							MTGestureEvent.GESTURE_UPDATED, getTargetComponent(), inputCursor,
243							dpc.getTranslationVect()));
244				}
245			} 
246			
247		}else
248		{			
249			this.fireGestureEvent(new DepthGestureEvent(this,
250					MTGestureEvent.GESTURE_ENDED, getTargetComponent(), inputCursor,
251					dpc.getTranslationVect()));
252		}
253		
254
255	}
256
257	@Override
258	public String getName() {
259		// TODO Auto-generated method stub
260		return null;
261	}
262
263	public void deleteDepthHelper(IMTComponent3D comp) {
264
265	}
266
267	private class DepthContext {
268
269		private Vector3D startPosition;
270
271		private Vector3D lastPosition;
272
273		private Vector3D newPosition;
274		
275		private float lastVal;
276		
277		private float newVal;
278
279		private IMTComponent3D dragDepthObject;
280
281		private InputCursor depthCursor;
282
283		private boolean gestureAborted;
284
285		private MTCanvas mtCanvas;
286
287		private MTComponent mtComp;
288
289		private Vector3D translationVect;
290
291		private VelocityMotionMapper velocityMotionMapper;
292		
293		public DepthContext(InputCursor cursor, IMTComponent3D dragObject) {
294			this.dragDepthObject = dragObject;
295			this.depthCursor = cursor;
296			gestureAborted = false;
297			
298			startPosition = new Vector3D(cursor.getCurrentEvtPosX(),cursor.getCurrentEvtPosY());
299
300			this.newPosition = startPosition.getCopy();
301			
302			this.velocityMotionMapper = new VelocityMotionMapper(10);
303
304			// Set the Drags lastPostition (the last one before the new one)
305			this.lastPosition = startPosition.getCopy();
306			this.lastVal = 0.0f;
307			
308			this.updateDepthPosition();
309
310		}
311
312		/**
313		 * Update drag position.
314		 */
315		public void updateDepthPosition() {
316
317			
318			if(!resumed)
319			{
320				Vector3D newPos = new Vector3D(depthCursor.getCurrentEvtPosX(),depthCursor.getCurrentEvent().getPosY(),0.0f);	
321				
322				Vector3D vec = newPos;
323				
324				int sign = -1;
325				
326				if(newPos.y<lastPosition.y)
327				{
328					sign = 1;
329				}
330				velocityMotionMapper.updateCurrentLength(sign*vec.getSubtracted(lastPosition).length());
331			
332				float currentVal = velocityMotionMapper.calcCurrentValue();
333				
334				newVal = currentVal; 
335				lastVal = currentVal;
336				
337				translationVect = new Vector3D(0.0f, 0.0f, -newVal);
338				
339				lastPosition = newPosition;
340				newPosition = newPos;
341			}else
342			{				
343				translationVect = new Vector3D(0.0f,0.0f,0.0f);
344				newPosition = lastPosition;
345				resumed = false;
346			}
347			
348		}
349
350		/**
351		 * Gets the last position.
352		 * 
353		 * @return the last position
354		 */
355		public Vector3D getLastPosition() {
356			return lastPosition;
357		}
358
359		/**
360		 * Gets the new position.
361		 * 
362		 * @return the new position
363		 */
364		public Vector3D getNewPosition() {
365			return newPosition;
366		}
367
368		/**
369		 * Checks if is gesture aborted.
370		 * 
371		 * @return true, if is gesture aborted
372		 */
373		public boolean isGestureAborted() {
374			return gestureAborted;
375		}
376
377		public Vector3D getTranslationVect() {
378			return translationVect;
379		}
380
381	}
382
383	public void setTargetComponent(IMTComponent3D targetComponent) {
384		this.targetComponent = targetComponent;
385	}
386
387	public IMTComponent3D getTargetComponent() {
388		return targetComponent;
389	}
390
391	
392}