PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/input/inputProcessors/globalProcessors/InputRetargeter.java

http://mt4j.googlecode.com/
Java | 114 lines | 60 code | 12 blank | 42 comment | 8 complexity | d4898557f880d33980c2143784c7db47 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.inputProcessors.globalProcessors;
 19
 20import java.util.HashMap;
 21import java.util.Map;
 22
 23import org.mt4j.components.interfaces.IMTComponent3D;
 24import org.mt4j.input.IHitTestInfoProvider;
 25import org.mt4j.input.inputData.AbstractCursorInputEvt;
 26import org.mt4j.input.inputData.InputCursor;
 27import org.mt4j.input.inputData.MTInputEvent;
 28
 29/**
 30 * The Class InputRetargeter. This global input analyzer is automatically created with each new scene and listens
 31 * to all InputSources for AbstractCursorInputEvt events (Input events with a discrete position attached).
 32 * <br>This global input analyzer uses MTPositionEvents to check which object in the current scene was hit.
 33 * Then the targetComponent is added to the event and the event is delivered to the scenes canvas where the 
 34 * targeted event is delivered to the targetComponent. No new event is created for performance reasons.
 35 * So the event is merely retargeted and redirected.
 36 * 
 37 * @author Christopher Ruff
 38 */
 39public class InputRetargeter extends AbstractGlobalInputProcessor {
 40	private Map<InputCursor, IMTComponent3D> cursorToObjectMap;
 41	
 42	/** The app info provider. */
 43	private IHitTestInfoProvider appInfoProvider;
 44
 45	public InputRetargeter(IHitTestInfoProvider appInfoProvider) {
 46		super();
 47		this.appInfoProvider = appInfoProvider;
 48		this.cursorToObjectMap = new HashMap<InputCursor, IMTComponent3D>();
 49	}
 50
 51
 52	public void processInputEvtImpl(MTInputEvent inputEvent) {
 53		if (inputEvent instanceof AbstractCursorInputEvt) {
 54			AbstractCursorInputEvt posEvt = (AbstractCursorInputEvt) inputEvent;
 55			InputCursor m = posEvt.getCursor();
 56			
 57			switch (posEvt.getId()) {
 58			case AbstractCursorInputEvt.INPUT_STARTED:{
 59//				logger.debug("Finger DOWN-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPosX() + " Y:" + posEvt.getPosY() + "; Source: " + posEvt.getSource());
 60//				System.out.println("Finger DOWN-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPosX() + " Y:" + posEvt.getPosY() + "; Source: " + posEvt.getSource()+  " CursorID: " + m.getId() + " appInfoProv: " + appInfoProvider);
 61				//Check if there is an object under the cursor and save it to a hashtable with the event if so
 62				IMTComponent3D obj = appInfoProvider.getComponentAt(posEvt.getX(), posEvt.getY());
 63				if (obj != null){
 64					cursorToObjectMap.put(m, obj);
 65					posEvt.setTarget(obj);
 66//					posEvt.setCurrentTarget(obj.getRoot()); //Enable this if using event CAPTURING PHASE
 67					posEvt.setCurrentTarget(obj);
 68					posEvt.setEventPhase(MTInputEvent.CAPTURING_PHASE);
 69					this.fireInputEvent(posEvt);
 70				}
 71			}
 72			break;
 73			case AbstractCursorInputEvt.INPUT_UPDATED:{
 74//				logger.debug("Finger UPDATE-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPositionX() + " Y:" + posEvt.getPositionY() + "; Source: " + posEvt.getSource());
 75				IMTComponent3D associatedObj = cursorToObjectMap.get(m);
 76				if (associatedObj != null){
 77					posEvt.setTarget(associatedObj);
 78//					posEvt.setCurrentTarget(associatedObj.getRoot());//Enable this if using event CAPTURING PHASE
 79					posEvt.setCurrentTarget(associatedObj);
 80					posEvt.setEventPhase(MTInputEvent.CAPTURING_PHASE);
 81					this.fireInputEvent(posEvt);
 82				}
 83			}
 84			break;
 85			case AbstractCursorInputEvt.INPUT_ENDED:{
 86//				logger.debug("Finger UP-> " + " ID:" + posEvt.getId() + "; X:" + posEvt.getPositionX() + " Y:" + posEvt.getPositionY() + "; Source: " + posEvt.getSource());
 87//				IMTComponent3D associatedObj = motionToObjectMap.get(m);
 88				IMTComponent3D associatedObj = cursorToObjectMap.remove(m);
 89				if (associatedObj != null){
 90					posEvt.setTarget(associatedObj);
 91//					posEvt.setCurrentTarget(associatedObj.getRoot());//Enable this if using event CAPTURING PHASE
 92					posEvt.setCurrentTarget(associatedObj);
 93					posEvt.setEventPhase(MTInputEvent.CAPTURING_PHASE);
 94					this.fireInputEvent(posEvt);
 95//					motionToObjectMap.remove(m);
 96				}
 97			}
 98			break;
 99			default:
100				break;
101			}
102		}else{
103			//Other event type, evtl ohne absolute x,y coordianten (z.b. joystick)
104			//einfach an mtcanvas weiterleiten?
105//			logger.error("Warning in " + this  + " Dont know how to handle evt: " + inputEvent );
106			
107			//Just fire other input events to the current canvas by default
108			this.fireInputEvent(inputEvent);
109		}
110	}
111
112
113
114}