Game Development Reference
In-Depth Information
public int getTouchY( int pointer) {
synchronized ( this ) {
int index=getIndex(pointer);
if (index<0 || index >= MAX_TOUCHPOINTS )
return 0;
else
return touchY[index];
}
}
public List<TouchEvent>getTouchEvents() {
synchronized ( this ) {
int len=touchEvents.size();
for ( int i=0; i<len; i++)
touchEventPool.free(touchEvents.get(i));
touchEvents.clear();
touchEvents.addAll(touchEventsBuffer);
touchEventsBuffer.clear();
return touchEvents;
}
}
// returns the index for a given pointerId or −1 if no index.
private int getIndex( int pointerId) {
for ( int i=0; i< MAX_TOUCHPOINTS ; i++) {
if (id[i] == pointerId) {
return i;
}
}
return -1;
}
}
We start off with another TargetApi annotation to tell the compiler that we know what we
are doing. In this case, we have set the minimum API level to 3, but the code in the
multitouch-handler requires API level 5. The compiler would complain without this annotation.
The onTouch() method looks as intimidating as our test example in Chapter 4. However, all we
need to do is marry that test code with our event pooling and synchronization, which we've
already talked about in detail. The only real difference from the SingleTouchHandler.onTouch()
method is that we handle multiple pointers and set the TouchEvent.pointer member accordingly
(instead of using a value of zero).
The polling methods, isT ouchDown() , getTouchX() , and getTouchY() , should look familiar as
well. We perform some error checking and then fetch the corresponding pointer state for the
corresponding pointer index from one of the member arrays that we fill in the onTouch() method.
The final public method, getTouchEvents() , is exactly the same as the corresponding method in
SingleTouchHandler.getTouchEvents() . Now that we are equipped with all these handlers, we
can implement the Input interface.
The last method in the class is a helper method that we use to find the index to a pointer ID.
Search WWH ::




Custom Search