PageRenderTime 70ms CodeModel.GetById 53ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/input/inputSources/KeyboardInputSource.java

http://mt4j.googlecode.com/
Java | 271 lines | 107 code | 51 blank | 113 comment | 24 complexity | a1acb71eeeb88d8fb68385acb3f774cf 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.inputSources;
 19
 20
 21import java.awt.event.KeyEvent;
 22
 23import org.mt4j.MTApplication;
 24import org.mt4j.input.inputData.ActiveCursorPool;
 25import org.mt4j.input.inputData.InputCursor;
 26import org.mt4j.input.inputData.MTFingerInputEvt;
 27
 28import processing.core.PApplet;
 29
 30/**
 31 * The Class KeyboardInputSource.
 32 * @author Christopher Ruff
 33 */
 34public class KeyboardInputSource extends AbstractInputSource {
 35	
 36	/** The last used keyb id. */
 37	private long lastUsedKeybID;
 38	
 39	/** The location x. */
 40	private int locationX;
 41	
 42	/** The location y. */
 43	private int locationY;
 44	
 45	/** The space has been pressed. */
 46	private boolean spaceHasBeenPressed = false;
 47	
 48	/** The applet. */
 49	private PApplet applet;
 50
 51	private int newFingerLocationKCode;
 52
 53	private int moveUpKeyCode;
 54
 55	private int moveLeftKeyCode;
 56
 57	private int moveDownKeyCode;
 58
 59	private int moveRightKeyCode;
 60
 61	private int fingerDownKeyCode;
 62	
 63	/**
 64	 * Instantiates a new keyboard input source.
 65	 * 
 66	 * @param pa the pa
 67	 */
 68	public KeyboardInputSource(MTApplication pa){
 69		super(pa);
 70		this.applet = pa;
 71		applet.registerKeyEvent(this);
 72		
 73//		applet.registerDraw(this);
 74		
 75		this.locationX = 0;
 76		this.locationY = 0;
 77		
 78		
 79		this.moveUpKeyCode 		= KeyEvent.VK_W;
 80		this.moveLeftKeyCode 	= KeyEvent.VK_A;
 81		this.moveDownKeyCode 	= KeyEvent.VK_S;
 82		this.moveRightKeyCode 	= KeyEvent.VK_D;
 83		
 84		this.newFingerLocationKCode = KeyEvent.VK_N;
 85		
 86		this.fingerDownKeyCode 	= KeyEvent.VK_SHIFT;
 87	}
 88
 89	
 90	//401 = pressed //402 = released
 91	/**
 92	 * Key event.
 93	 * 
 94	 * @param e the e
 95	 */
 96	public void keyEvent(KeyEvent e){
 97//		System.out.println(e.getID());
 98//		System.out.println(e.getKeyCode());
 99		
100		int evtID = e.getID();
101		
102		if (evtID == KeyEvent.KEY_PRESSED ){
103			if (e.isControlDown() && e.getKeyCode() == this.newFingerLocationKCode){
104				locationX = applet.mouseX;
105				locationY = applet.mouseY;
106			}else if (e.getKeyCode() == this.moveUpKeyCode){
107				locationY-=5;
108				if (e.isShiftDown()){
109					fingerDown(e);
110				}
111			}else if (e.getKeyCode() == this.moveLeftKeyCode){
112				locationX-=5;
113				if (e.isShiftDown()){
114					fingerDown(e);
115				}
116			}else if (e.getKeyCode() == this.moveDownKeyCode){
117				locationY+=5;
118				if (e.isShiftDown()){
119					fingerDown(e);
120				}
121			}else if (e.getKeyCode() == this.moveRightKeyCode){
122				locationX+=5;
123				if (e.isShiftDown()){
124					fingerDown(e);
125				}
126			}else if (e.getKeyCode() == this.fingerDownKeyCode){
127				fingerDown(e);
128			}
129		}else if (evtID == KeyEvent.KEY_RELEASED){
130			if (e.getKeyCode() == this.fingerDownKeyCode){
131				fingerUp(e);
132			}
133		}
134			
135		
136		/*
137		switch (e.getKeyCode()){
138		case KeyEvent.VK_W:
139			if (evtID == KeyEvent.KEY_PRESSED ){
140				locationY-=5;
141				if (e.isShiftDown()){
142					shiftPressed(e);
143				}
144			}
145			break;
146		case KeyEvent.VK_A:
147			if (evtID == KeyEvent.KEY_PRESSED){
148				locationX-=5;
149				if (e.isShiftDown()){
150					shiftPressed(e);
151				}
152			}
153			break;
154		case KeyEvent.VK_S:
155			if (evtID == KeyEvent.KEY_PRESSED){
156				locationY+=5;
157				if (e.isShiftDown()){
158					shiftPressed(e);
159				}
160			}
161			break;
162		case KeyEvent.VK_D:
163			if (evtID == KeyEvent.KEY_PRESSED){
164				locationX+=5;
165				if (e.isShiftDown()){
166					shiftPressed(e);
167				}
168			}
169			break;
170		case KeyEvent.VK_SHIFT:
171			if (evtID == KeyEvent.KEY_PRESSED){
172				shiftPressed(e);
173			}
174			else if (evtID == KeyEvent.KEY_RELEASED){
175				shiftReleased(e);
176			}
177			break;
178		case KeyEvent.VK_N: //set the location to the mouseposition
179			if (evtID == KeyEvent.KEY_PRESSED){
180				locationX = applet.mouseX;
181				locationY = applet.mouseY;
182			}
183			break;
184		default:
185			break;
186		}
187		*/
188	}
189	
190
191	public void setNewFingerLocationKeyCode(int keyCode){
192		this.newFingerLocationKCode = keyCode;
193	}
194	
195	public void setFingerDownKeyCode(int keyCode){
196		this.fingerDownKeyCode = keyCode;
197	}
198	
199	public void setMoveUpKeyCode(int keyCode){
200		this.moveUpKeyCode = keyCode;
201	}
202	
203	public void setMoveLeftKeyCode(int keyCode){
204		this.moveLeftKeyCode = keyCode;
205	}
206	
207	public void setmoveDownKeyCode(int keyCode){
208		this.moveDownKeyCode = keyCode;
209	}
210	
211	public void setMoveRightKeyCode(int keyCode){
212		this.moveRightKeyCode = keyCode;
213	}
214	
215	
216	/**
217	 * 
218	 * @param e the e
219	 */
220	private void fingerDown(KeyEvent e){
221		if (!spaceHasBeenPressed){
222			InputCursor m = new InputCursor();
223			MTFingerInputEvt touchEvt = new MTFingerInputEvt(this, locationX, locationY, MTFingerInputEvt.INPUT_STARTED, m);
224//			m.addEvent(touchEvt);
225			
226			lastUsedKeybID = m.getId();
227			ActiveCursorPool.getInstance().putActiveCursor(lastUsedKeybID, m);
228			
229			//FIRE
230			this.enqueueInputEvent(touchEvt);
231			
232		spaceHasBeenPressed = true;
233		}else{
234			InputCursor m = ActiveCursorPool.getInstance().getActiveCursorByID(lastUsedKeybID);
235			
236//			if (m.getLastEvent().getPositionX() != locationX || m.getLastEvent().getPositionY() != locationY){
237			MTFingerInputEvt te = new MTFingerInputEvt(this, locationX, locationY, MTFingerInputEvt.INPUT_UPDATED, m);
238//			m.addEvent(new MTFingerInputEvt2(this, e.getX(), e.getY(), MTFingerInputEvt.FINGER_UPDATE, m));
239			
240			//FIRE
241			this.enqueueInputEvent(te);
242//			}
243		}
244	}
245	
246	
247	/**
248	 * 
249	 * @param e the e
250	 */
251	private void fingerUp(KeyEvent e) {
252		InputCursor m = ActiveCursorPool.getInstance().getActiveCursorByID(lastUsedKeybID);
253		MTFingerInputEvt te = new MTFingerInputEvt(this, locationX, locationY, MTFingerInputEvt.INPUT_ENDED, m);
254//		m.addEvent(te);
255		
256		this.enqueueInputEvent(te);
257		
258		ActiveCursorPool.getInstance().removeCursor((lastUsedKeybID));
259		spaceHasBeenPressed = false;
260		
261//		MTFingerInputEvt te = MTFingerInputEvtPool.getInstance().getEventByID(lastUsedKeybID);
262//		//FIXME warum gibts manchmal NUllpointer weil kein TE vorhanden? auch bei mouse..
263	}
264
265//	@Override
266//	public boolean firesEventType(Class<? extends MTInputEvent> evtClass){
267//		return (evtClass == MTFingerInputEvt.class);
268//	}
269	
270	
271}