PageRenderTime 63ms CodeModel.GetById 44ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mt4j.googlecode.com/
Java | 74 lines | 54 code | 14 blank | 6 comment | 9 complexity | df694291f482aa89a2b7c45afd26de48 MD5 | raw file
 1package org.mt4j.input.inputSources;
 2
 3import java.util.HashMap;
 4import java.util.Map;
 5import java.util.Observable;
 6import java.util.Observer;
 7
 8import org.mt4j.MTApplication;
 9import org.mt4j.input.inputData.ActiveCursorPool;
10import org.mt4j.input.inputData.InputCursor;
11import org.mt4j.input.inputData.MTFingerInputEvt;
12import org.mt4j.util.MT4jSettings;
13
14import com.alderstone.multitouch.mac.touchpad.Finger;
15import com.alderstone.multitouch.mac.touchpad.FingerState;
16import com.alderstone.multitouch.mac.touchpad.TouchpadObservable;
17
18/**
19 * Input source for Mac OS X Trackpads.
20 * Uses the library from http://kenai.com/projects/macmultitouch
21 * 
22 * @author Florian Thalmann
23 */
24public class MacTrackpadSource extends AbstractInputSource implements Observer {
25
26	private TouchpadObservable tpo;
27	private int windowWidth, windowHeight;
28	private Map<Integer, Long> fingerIdToCursorId;
29	
30	public MacTrackpadSource(MTApplication mtApp) {
31		super(mtApp);
32		this.tpo = TouchpadObservable.getInstance();
33		this.tpo.addObserver(this);
34		
35		this.windowWidth = MT4jSettings.getInstance().getWindowWidth();
36		this.windowHeight = MT4jSettings.getInstance().getWindowHeight();
37		
38		this.fingerIdToCursorId = new HashMap<Integer, Long>();
39	}
40
41	public void update(Observable obj, Object arg) {
42		Finger finger = (Finger) arg;
43		int fingerID = finger.getID();
44		
45		ActiveCursorPool cursorPool = ActiveCursorPool.getInstance();
46		int inputID;
47		Long cursorID = fingerIdToCursorId.get(fingerID);
48		InputCursor cursor = (cursorID != null)? cursorPool.getActiveCursorByID(cursorID) : null;
49		
50		if (finger.getState() == FingerState.PRESSED) {
51			if (cursor == null) { //new finger
52				cursor = new InputCursor();
53				fingerIdToCursorId.put(fingerID, cursor.getId());
54				cursorPool.putActiveCursor(cursor.getId(), cursor);
55				inputID = MTFingerInputEvt.INPUT_STARTED;
56			} else { //updated finger
57				inputID = MTFingerInputEvt.INPUT_UPDATED;
58			}
59		} else { //removed finger
60			if (cursorID != null){
61				cursorPool.removeCursor(cursorID);
62			}
63			fingerIdToCursorId.remove(fingerID);
64			inputID = MTFingerInputEvt.INPUT_ENDED;
65		}
66		
67		int absoluteX = Math.round(finger.getX()*this.windowWidth);
68		int absoluteY = Math.round((1-finger.getY())*this.windowHeight);
69		this.enqueueInputEvent(new MTFingerInputEvt(this, absoluteX, absoluteY, inputID, cursor));
70	}
71
72	public void update() {}
73
74}