Beispiel #1
0
func processEvent(env *C.JNIEnv, e *C.AInputEvent) {
	switch C.AInputEvent_getType(e) {
	case C.AINPUT_EVENT_TYPE_KEY:
		processKey(env, e)
	case C.AINPUT_EVENT_TYPE_MOTION:
		// At most one of the events in this batch is an up or down event; get its index and change.
		upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT
		upDownType := touch.TypeMove
		switch C.AMotionEvent_getAction(e) & C.AMOTION_EVENT_ACTION_MASK {
		case C.AMOTION_EVENT_ACTION_DOWN, C.AMOTION_EVENT_ACTION_POINTER_DOWN:
			upDownType = touch.TypeBegin
		case C.AMOTION_EVENT_ACTION_UP, C.AMOTION_EVENT_ACTION_POINTER_UP:
			upDownType = touch.TypeEnd
		}

		for i, n := C.size_t(0), C.AMotionEvent_getPointerCount(e); i < n; i++ {
			t := touch.TypeMove
			if i == upDownIndex {
				t = upDownType
			}
			eventsIn <- touch.Event{
				X:        float32(C.AMotionEvent_getX(e, i)),
				Y:        float32(C.AMotionEvent_getY(e, i)),
				Sequence: touch.Sequence(C.AMotionEvent_getPointerId(e, i)),
				Type:     t,
			}
		}
	default:
		log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
	}
}
Beispiel #2
0
func processEvent(e *C.AInputEvent) {
	switch C.AInputEvent_getType(e) {
	case C.AINPUT_EVENT_TYPE_KEY:
		log.Printf("TODO input event: key")
	case C.AINPUT_EVENT_TYPE_MOTION:
		// At most one of the events in this batch is an up or down event; get its index and change.
		upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT
		upDownChange := event.ChangeNone
		switch C.AMotionEvent_getAction(e) & C.AMOTION_EVENT_ACTION_MASK {
		case C.AMOTION_EVENT_ACTION_DOWN, C.AMOTION_EVENT_ACTION_POINTER_DOWN:
			upDownChange = event.ChangeOn
		case C.AMOTION_EVENT_ACTION_UP, C.AMOTION_EVENT_ACTION_POINTER_UP:
			upDownChange = event.ChangeOff
		}

		for i, n := C.size_t(0), C.AMotionEvent_getPointerCount(e); i < n; i++ {
			change := event.ChangeNone
			if i == upDownIndex {
				change = upDownChange
			}
			eventsIn <- event.Touch{
				ID:     event.TouchSequenceID(C.AMotionEvent_getPointerId(e, i)),
				Change: change,
				Loc: geom.Point{
					X: geom.Pt(float32(C.AMotionEvent_getX(e, i)) / pixelsPerPt),
					Y: geom.Pt(float32(C.AMotionEvent_getY(e, i)) / pixelsPerPt),
				},
			}
		}
	default:
		log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
	}
}
Beispiel #3
0
func processEvent(cb Callbacks, e *C.AInputEvent) {
	switch C.AInputEvent_getType(e) {
	case C.AINPUT_EVENT_TYPE_KEY:
		log.Printf("TODO input event: key")
	case C.AINPUT_EVENT_TYPE_MOTION:
		if cb.Touch == nil {
			return
		}
		x := C.AMotionEvent_getX(e, 0)
		y := C.AMotionEvent_getY(e, 0)

		var ty event.TouchType
		switch C.AMotionEvent_getAction(e) {
		case C.AMOTION_EVENT_ACTION_DOWN:
			ty = event.TouchStart
		case C.AMOTION_EVENT_ACTION_MOVE:
			ty = event.TouchMove
		case C.AMOTION_EVENT_ACTION_UP:
			ty = event.TouchEnd
		}
		cb.Touch(event.Touch{
			Type: ty,
			Loc: geom.Point{
				X: geom.Pt(float32(x) / geom.PixelsPerPt),
				Y: geom.Pt(float32(y) / geom.PixelsPerPt),
			},
		})
	default:
		log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
	}
}
Beispiel #4
0
func dispatchEvent(nativeEvent *C.AInputEvent) bool {
	switch C.AInputEvent_getType(nativeEvent) {
	case C.AINPUT_EVENT_TYPE_MOTION:
		action := C.AMotionEvent_getAction(nativeEvent) & C.AMOTION_EVENT_ACTION_MASK
		switch action {
		case C.AMOTION_EVENT_ACTION_UP:
			down := false
			x := float32(C.AMotionEvent_getX(nativeEvent, 0))
			y := float32(C.AMotionEvent_getY(nativeEvent, 0))
			event <- ActionUpDownEvent{Down: down, X: x, Y: y}
		case C.AMOTION_EVENT_ACTION_DOWN:
			down := true
			x := float32(C.AMotionEvent_getX(nativeEvent, 0))
			y := float32(C.AMotionEvent_getY(nativeEvent, 0))
			event <- ActionUpDownEvent{Down: down, X: x, Y: y}
		case C.AMOTION_EVENT_ACTION_MOVE:
			x := float32(C.AMotionEvent_getX(nativeEvent, 0))
			y := float32(C.AMotionEvent_getY(nativeEvent, 0))
			event <- ActionMoveEvent{X: x, Y: y}
		}
	}
	return false
}
Beispiel #5
0
func (game *game) onTouch(event *C.AInputEvent) bool {
	action := C.AMotionEvent_getAction(event) & C.AMOTION_EVENT_ACTION_MASK
	x := float32(C.AMotionEvent_getX(event, 0))
	y := float32(C.AMotionEvent_getY(event, 0))
	switch action {
	case C.AMOTION_EVENT_ACTION_UP:
		game.touching = false
	case C.AMOTION_EVENT_ACTION_DOWN:
		game.touching = true
		game.touchX, game.touchY = x, y
	case C.AMOTION_EVENT_ACTION_MOVE:
		if !game.touching {
			break
		}
		game.offsetX += 2 * (x - game.touchX) / float32(game.width)
		game.offsetY += 2 * -(y - game.touchY) / float32(game.height)
		game.touchX, game.touchY = x, y
	}
	return true
}
Beispiel #6
0
func processEvent(cb Callbacks, e *C.AInputEvent) {
	switch C.AInputEvent_getType(e) {
	case C.AINPUT_EVENT_TYPE_KEY:
		log.Printf("TODO input event: key")
	case C.AINPUT_EVENT_TYPE_MOTION:
		if cb.Touch == nil {
			return
		}

		// At most one of the events in this batch is an up or down event; get its index and type.
		upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT
		upDownTyp := event.TouchMove
		switch C.AMotionEvent_getAction(e) & C.AMOTION_EVENT_ACTION_MASK {
		case C.AMOTION_EVENT_ACTION_DOWN, C.AMOTION_EVENT_ACTION_POINTER_DOWN:
			upDownTyp = event.TouchStart
		case C.AMOTION_EVENT_ACTION_UP, C.AMOTION_EVENT_ACTION_POINTER_UP:
			upDownTyp = event.TouchEnd
		}

		for i, n := C.size_t(0), C.AMotionEvent_getPointerCount(e); i < n; i++ {
			typ := event.TouchMove
			if i == upDownIndex {
				typ = upDownTyp
			}
			x := C.AMotionEvent_getX(e, i)
			y := C.AMotionEvent_getY(e, i)
			cb.Touch(event.Touch{
				Type: typ,
				Loc: geom.Point{
					X: geom.Pt(float32(x) / geom.PixelsPerPt),
					Y: geom.Pt(float32(y) / geom.PixelsPerPt),
				},
			})
		}
	default:
		log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
	}
}