forked from libgdx/libgdx
/
AndroidMouseHandler.java
91 lines (78 loc) · 3 KB
/
AndroidMouseHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.backends.android;
import android.view.InputDevice;
import android.view.MotionEvent;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.DefaultAndroidInput.TouchEvent;
/** Mouse handler for devices running Android >= 3.1.
*
* @author Richard Martin */
public class AndroidMouseHandler {
private int deltaX = 0;
private int deltaY = 0;
public boolean onGenericMotion (MotionEvent event, DefaultAndroidInput input) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) return false;
final int action = event.getAction() & MotionEvent.ACTION_MASK;
int x = 0, y = 0;
int scrollAmountX = 0;
int scrollAmountY = 0;
long timeStamp = System.nanoTime();
synchronized (input) {
switch (action) {
case MotionEvent.ACTION_HOVER_MOVE:
x = (int)event.getX();
y = (int)event.getY();
if ((x != deltaX) || (y != deltaY)) { // Avoid garbage events
postTouchEvent(input, TouchEvent.TOUCH_MOVED, x, y, 0, 0, timeStamp);
deltaX = x;
deltaY = y;
}
break;
case MotionEvent.ACTION_SCROLL:
scrollAmountY = (int)-Math.signum(event.getAxisValue(MotionEvent.AXIS_VSCROLL));
scrollAmountX = (int)-Math.signum(event.getAxisValue(MotionEvent.AXIS_HSCROLL));
postTouchEvent(input, TouchEvent.TOUCH_SCROLLED, 0, 0, scrollAmountX, scrollAmountY, timeStamp);
}
}
Gdx.app.getGraphics().requestRendering();
return true;
}
private void logAction (int action) {
String actionStr = "";
if (action == MotionEvent.ACTION_HOVER_ENTER)
actionStr = "HOVER_ENTER";
else if (action == MotionEvent.ACTION_HOVER_MOVE)
actionStr = "HOVER_MOVE";
else if (action == MotionEvent.ACTION_HOVER_EXIT)
actionStr = "HOVER_EXIT";
else if (action == MotionEvent.ACTION_SCROLL)
actionStr = "SCROLL";
else
actionStr = "UNKNOWN (" + action + ")";
Gdx.app.log("AndroidMouseHandler", "action " + actionStr);
}
private void postTouchEvent (DefaultAndroidInput input, int type, int x, int y, int scrollAmountX, int scrollAmountY, long timeStamp) {
TouchEvent event = input.usedTouchEvents.obtain();
event.timeStamp = timeStamp;
event.x = x;
event.y = y;
event.type = type;
event.scrollAmountX = scrollAmountX;
event.scrollAmountY = scrollAmountY;
input.touchEvents.add(event);
}
}