/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

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