/src/org/mt4j/input/gestureAction/DefaultDragAction.java

http://mt4j.googlecode.com/ · Java · 137 lines · 62 code · 21 blank · 54 comment · 6 complexity · 9e2275aa0954ce33aa97c2b3dff51d31 MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2009, C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.input.gestureAction;
  19. import org.mt4j.components.MTComponent;
  20. import org.mt4j.components.interfaces.IMTComponent3D;
  21. import org.mt4j.input.inputProcessors.IGestureEventListener;
  22. import org.mt4j.input.inputProcessors.MTGestureEvent;
  23. import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragEvent;
  24. /**
  25. * The Class DefaultDragAction.
  26. * @author Christopher Ruff
  27. */
  28. public class DefaultDragAction implements IGestureEventListener,ICollisionAction {
  29. /** The drag target. */
  30. private IMTComponent3D dragTarget;
  31. /** The use custom target. */
  32. private boolean useCustomTarget;
  33. /** The gesture aborted. */
  34. private boolean gestureAborted = false;
  35. /** The last event. */
  36. private MTGestureEvent lastEvent;
  37. /**
  38. * Instantiates a new default drag action.
  39. */
  40. public DefaultDragAction(){
  41. this.useCustomTarget = false;
  42. }
  43. /**
  44. * Instantiates a new default drag action.
  45. *
  46. * @param dragTarget the drag target
  47. */
  48. public DefaultDragAction(IMTComponent3D dragTarget){
  49. this.dragTarget = dragTarget;
  50. this.useCustomTarget = true;
  51. }
  52. /* (non-Javadoc)
  53. * @see org.mt4j.input.inputProcessors.IGestureEventListener#processGestureEvent(org.mt4j.input.inputProcessors.MTGestureEvent)
  54. */
  55. public boolean processGestureEvent(MTGestureEvent g) {
  56. if (g instanceof DragEvent){
  57. DragEvent dragEvent = (DragEvent)g;
  58. lastEvent = dragEvent;
  59. if (!useCustomTarget)
  60. dragTarget = dragEvent.getTarget();
  61. switch (dragEvent.getId()) {
  62. case MTGestureEvent.GESTURE_STARTED:
  63. case MTGestureEvent.GESTURE_RESUMED:
  64. //Put target on top -> draw on top of others
  65. if (dragTarget instanceof MTComponent){
  66. MTComponent baseComp = (MTComponent)dragTarget;
  67. baseComp.sendToFront();
  68. /*
  69. //End all animations of the target
  70. Animation[] animations = AnimationManager.getInstance().getAnimationsForTarget(dragTarget);
  71. for (int i = 0; i < animations.length; i++) {
  72. Animation animation = animations[i];
  73. animation.stop();
  74. }
  75. */
  76. }
  77. if(!gestureAborted)
  78. {
  79. dragTarget.translateGlobal(dragEvent.getTranslationVect());
  80. }
  81. break;
  82. case MTGestureEvent.GESTURE_UPDATED:
  83. if(!gestureAborted)
  84. {
  85. dragTarget.translateGlobal(dragEvent.getTranslationVect());
  86. }
  87. break;
  88. case MTGestureEvent.GESTURE_CANCELED:
  89. break;
  90. case MTGestureEvent.GESTURE_ENDED:
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. return false;
  97. }
  98. /* (non-Javadoc)
  99. * @see org.mt4j.input.inputProcessors.ICollisionAction#gestureAborted()
  100. */
  101. public boolean gestureAborted() {
  102. return this.gestureAborted;
  103. }
  104. /* (non-Javadoc)
  105. * @see org.mt4j.input.inputProcessors.ICollisionAction#getLastEvent()
  106. */
  107. public MTGestureEvent getLastEvent() {
  108. return this.lastEvent;
  109. }
  110. /* (non-Javadoc)
  111. * @see org.mt4j.input.inputProcessors.ICollisionAction#setGestureAborted(boolean)
  112. */
  113. public void setGestureAborted(boolean aborted) {
  114. this.gestureAborted = aborted;
  115. }
  116. }