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

http://mt4j.googlecode.com/ · Java · 215 lines · 94 code · 32 blank · 89 comment · 12 complexity · 2dcbbf0218f3bd9b9fdbcf261af49435 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.scaleProcessor.ScaleEvent;
  24. import org.mt4j.util.math.Vector3D;
  25. /**
  26. * The Class DefaultScaleAction.
  27. *
  28. * @author Christopher Ruff
  29. */
  30. public class DefaultScaleAction implements IGestureEventListener,ICollisionAction {
  31. /** The target. */
  32. private IMTComponent3D target;
  33. /** The has scale limit. */
  34. private boolean hasScaleLimit;
  35. /** The min scale. */
  36. private float minScale;
  37. /** The max scale. */
  38. private float maxScale;
  39. /** The last event. */
  40. private MTGestureEvent lastEvent;
  41. /** The gesture aborted. */
  42. private boolean gestureAborted = false;
  43. /**
  44. * Instantiates a new default scale action.
  45. */
  46. public DefaultScaleAction(){
  47. this(null, 0,0, false);
  48. }
  49. /**
  50. * Instantiates a new default scale action.
  51. *
  52. * @param customTarget the custom target
  53. */
  54. public DefaultScaleAction(IMTComponent3D customTarget){
  55. this(customTarget, 0,0, false);
  56. }
  57. /**
  58. * Instantiates a new default scale action.
  59. *
  60. * @param minScaleFactor the min scale factor
  61. * @param maxScaleFactor the max scale factor
  62. */
  63. public DefaultScaleAction(float minScaleFactor, float maxScaleFactor){
  64. this(null, minScaleFactor, maxScaleFactor, true);
  65. }
  66. /**
  67. * Instantiates a new default scale action.
  68. *
  69. * @param customTarget the custom target
  70. * @param minScaleFactor the min scale factor
  71. * @param maxScaleFactor the max scale factor
  72. */
  73. public DefaultScaleAction(IMTComponent3D customTarget, float minScaleFactor, float maxScaleFactor){
  74. this(customTarget, minScaleFactor, maxScaleFactor, true);
  75. }
  76. /**
  77. * Instantiates a new default scale action.
  78. *
  79. * @param customTarget the custom target
  80. * @param minScaleFactor the min scale factor
  81. * @param maxScaleFactor the max scale factor
  82. * @param useScaleLimit use scale limit
  83. */
  84. private DefaultScaleAction(IMTComponent3D customTarget, float minScaleFactor, float maxScaleFactor, boolean useScaleLimit){
  85. this.target = customTarget;
  86. if (minScaleFactor < 0 || maxScaleFactor < 0){
  87. System.err.println("minScaleFactor < 0 || maxScaleFactor < 0 invalid settings!");
  88. this.hasScaleLimit = false;
  89. }else{
  90. this.hasScaleLimit = useScaleLimit;
  91. }
  92. this.minScale = minScaleFactor;
  93. this.maxScale = maxScaleFactor;
  94. }
  95. /* (non-Javadoc)
  96. * @see org.mt4j.input.inputProcessors.IGestureEventListener#processGestureEvent(org.mt4j.input.inputProcessors.MTGestureEvent)
  97. */
  98. public boolean processGestureEvent(MTGestureEvent g) {
  99. if (g instanceof ScaleEvent){
  100. ScaleEvent scaleEvent = (ScaleEvent)g;
  101. this.lastEvent = scaleEvent;
  102. if (target == null)
  103. target = scaleEvent.getTarget();
  104. switch (scaleEvent.getId()) {
  105. case MTGestureEvent.GESTURE_STARTED:
  106. if (target instanceof MTComponent){
  107. ((MTComponent)target).sendToFront();
  108. /*
  109. Animation[] animations = AnimationManager.getInstance().getAnimationsForTarget(target);
  110. for (int i = 0; i < animations.length; i++) {
  111. Animation animation = animations[i];
  112. animation.stop();
  113. }
  114. */
  115. }
  116. break;
  117. case MTGestureEvent.GESTURE_UPDATED:
  118. if(!gestureAborted)
  119. {
  120. if (this.hasScaleLimit){
  121. if (target instanceof MTComponent) {
  122. MTComponent comp = (MTComponent) target;
  123. //FIXME actually we should use globalmatrix but performance is better for localMatrix..
  124. Vector3D currentScale = comp.getLocalMatrix().getScale();
  125. // if (currentScale.x != currentScale.y){
  126. // System.out.println("non uniform scale!");
  127. // }
  128. //We only check X because only uniform scales (x=y factor) should be used!
  129. if (currentScale.x * scaleEvent.getScaleFactorX() > this.maxScale){
  130. // System.out.println("Scale MAX Limit Hit!");
  131. //We should set to min scale, but we choose performance over accuracy
  132. //float factor = (1f/currentScale.x) * maxScale;
  133. //target.scaleGlobal(factor, factor, scaleEvent.getScaleFactorZ(), scaleEvent.getScalingPoint());
  134. }else if (currentScale.x * scaleEvent.getScaleFactorX() < this.minScale){
  135. // System.out.println("Scale MIN Limit Hit!");
  136. //We should set to min scale, but we choose performance over accuracy
  137. //float factor = (1f/currentScale.x) * minScale;
  138. //target.scaleGlobal(factor, factor, scaleEvent.getScaleFactorZ(), scaleEvent.getScalingPoint());
  139. }else{
  140. target.scaleGlobal(
  141. scaleEvent.getScaleFactorX(),
  142. scaleEvent.getScaleFactorY(),
  143. scaleEvent.getScaleFactorZ(),
  144. scaleEvent.getScalingPoint());
  145. }
  146. }
  147. }else{
  148. target.scaleGlobal(
  149. scaleEvent.getScaleFactorX(),
  150. scaleEvent.getScaleFactorY(),
  151. scaleEvent.getScaleFactorZ(),
  152. scaleEvent.getScalingPoint());
  153. }
  154. }
  155. break;
  156. case MTGestureEvent.GESTURE_CANCELED:
  157. case MTGestureEvent.GESTURE_ENDED:
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. return false;
  164. }
  165. /* (non-Javadoc)
  166. * @see org.mt4j.input.inputProcessors.ICollisionAction#gestureAborted()
  167. */
  168. public boolean gestureAborted() {
  169. return this.gestureAborted;
  170. }
  171. /* (non-Javadoc)
  172. * @see org.mt4j.input.inputProcessors.ICollisionAction#getLastEvent()
  173. */
  174. public MTGestureEvent getLastEvent() {
  175. return this.lastEvent;
  176. }
  177. /* (non-Javadoc)
  178. * @see org.mt4j.input.inputProcessors.ICollisionAction#setGestureAborted(boolean)
  179. */
  180. public void setGestureAborted(boolean aborted) {
  181. this.gestureAborted = aborted;
  182. }
  183. }