PageRenderTime 41ms CodeModel.GetById 20ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

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