Skip to content

Commit 77c6e05

Browse files
joliver82Jesus Oliverriccardobl
authored
Feature - Mouse devices on android (jMonkeyEngine#2597)
* Added support for mouse devices on android Added setMouseGrab method to both MouseInput and InputManager. Android cannot change to grab/capture mode while dragging, all events after mouse down are ignored till mouse up is received * Added documentation * More documentation * fix pointer grab by not handling action cancel * Removed log on each mouse event. Too verbose * set consume flag consistently --------- Co-authored-by: Jesus Oliver <joliver@allot.com> Co-authored-by: Riccardo Balbo <os@rblb.it>
1 parent 69c30f2 commit 77c6e05

File tree

8 files changed

+754
-5
lines changed

8 files changed

+754
-5
lines changed

jme3-android/src/main/java/com/jme3/input/android/AndroidInputHandler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import android.view.ScaleGestureDetector;
4141
import android.view.View;
4242
import com.jme3.input.JoyInput;
43+
import com.jme3.input.MouseInput;
4344
import com.jme3.input.TouchInput;
45+
import com.jme3.input.dummy.DummyMouseInput;
4446
import com.jme3.system.AppSettings;
4547
import java.util.logging.Logger;
4648

@@ -60,11 +62,12 @@ public class AndroidInputHandler implements View.OnTouchListener,
6062
protected GLSurfaceView view;
6163
protected AndroidTouchInput touchInput;
6264
protected AndroidJoyInput joyInput;
63-
65+
protected MouseInput mouseInput;
6466

6567
public AndroidInputHandler() {
6668
touchInput = new AndroidTouchInput(this);
6769
joyInput = new AndroidJoyInput(this);
70+
mouseInput = new DummyMouseInput();
6871
}
6972

7073
public void setView(View view) {
@@ -118,6 +121,10 @@ public JoyInput getJoyInput() {
118121
return joyInput;
119122
}
120123

124+
public MouseInput getMouseInput() {
125+
return mouseInput;
126+
}
127+
121128
/*
122129
* Android input events include the source from which the input came from.
123130
* We must look at the source of the input event to determine which type

jme3-android/src/main/java/com/jme3/input/android/AndroidInputHandler14.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
import android.view.KeyEvent;
3838
import android.view.MotionEvent;
3939
import android.view.View;
40+
41+
import com.jme3.system.AppSettings;
42+
4043
import java.util.logging.Logger;
4144

4245
/**
@@ -55,6 +58,7 @@ public class AndroidInputHandler14 extends AndroidInputHandler implements View.O
5558
public AndroidInputHandler14() {
5659
touchInput = new AndroidTouchInput14(this);
5760
joyInput = new AndroidJoyInput14(this);
61+
mouseInput = new AndroidMouseInput14(this);
5862
}
5963

6064
@Override
@@ -71,6 +75,12 @@ protected void addListeners(GLSurfaceView view) {
7175
view.setOnGenericMotionListener(this);
7276
}
7377

78+
@Override
79+
public void loadSettings(AppSettings settings) {
80+
super.loadSettings(settings);
81+
((AndroidMouseInput14)mouseInput).loadSettings(settings);
82+
}
83+
7484
@Override
7585
public boolean onHover(View view, MotionEvent event) {
7686
if (view != getView()) {
@@ -91,6 +101,12 @@ public boolean onHover(View view, MotionEvent event) {
91101
consumed = ((AndroidTouchInput14)touchInput).onHover(event);
92102
}
93103

104+
boolean isMouse = ((source & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE);
105+
if (isMouse && mouseInput != null) {
106+
// send the event to the mouse processor
107+
consumed = consumed || ((AndroidMouseInput14)mouseInput).onHover(event);
108+
}
109+
94110
return consumed;
95111
}
96112

@@ -116,6 +132,28 @@ public boolean onGenericMotion(View view, MotionEvent event) {
116132
consumed = consumed || ((AndroidJoyInput14)joyInput).onGenericMotion(event);
117133
}
118134

135+
if((source & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE) {
136+
consumed = consumed || ((AndroidMouseInput14)mouseInput).onGenericMotion(event);
137+
}
138+
139+
return consumed;
140+
}
141+
142+
@Override
143+
public boolean onTouch(View view, MotionEvent event) {
144+
if (view != getView()) {
145+
return false;
146+
}
147+
148+
boolean consumed = super.onTouch(view, event);
149+
150+
// Mouse movement while button down is received as onTouch event instead
151+
boolean isMouse = ((event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE);
152+
if (isMouse && mouseInput != null) {
153+
// send the event to the mouse processor
154+
consumed = consumed || ((AndroidMouseInput14)mouseInput).onGenericMotion(event);
155+
}
156+
119157
return consumed;
120158
}
121159

@@ -154,7 +192,6 @@ public boolean onKey(View view, int keyCode, KeyEvent event) {
154192
}
155193

156194
return consumed;
157-
158195
}
159196

160197
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
package com.jme3.input.android;
34+
35+
/**
36+
* <code>AndroidInputHandler24</code> extends <code>AndroidInputHandler14</code> to
37+
* use AndroidMouseInput24 which adds usage of newer events and also enables cursor visibility
38+
* and cursor image change.
39+
*
40+
* @author joliver82
41+
*/
42+
public class AndroidInputHandler24 extends AndroidInputHandler14 {
43+
44+
public AndroidInputHandler24() {
45+
super();
46+
mouseInput = new AndroidMouseInput24(this);
47+
}
48+
49+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
package com.jme3.input.android;
34+
35+
import android.opengl.GLSurfaceView;
36+
import android.view.InputDevice;
37+
import android.view.MotionEvent;
38+
import android.view.View;
39+
40+
/**
41+
* <code>AndroidInputHandler26</code> extends <code>AndroidInputHandler24</code> to
42+
* add the onCapturedPointer events that where added in Android rev 26.<br>
43+
* The onCapturedPointer events are received when mouse is grabbed/captured.
44+
*
45+
* @author joliver82
46+
*/
47+
public class AndroidInputHandler26 extends AndroidInputHandler24 implements View.OnCapturedPointerListener {
48+
49+
public AndroidInputHandler26() {
50+
super();
51+
mouseInput = new AndroidMouseInput26(this);
52+
}
53+
54+
protected void removeListeners(GLSurfaceView view) {
55+
super.removeListeners(view);
56+
view.setOnCapturedPointerListener(null);
57+
}
58+
59+
@Override
60+
protected void addListeners(GLSurfaceView view) {
61+
super.addListeners(view);
62+
view.setOnCapturedPointerListener(this);
63+
}
64+
65+
@Override
66+
public boolean onCapturedPointer(View view, MotionEvent event) {
67+
if (view != getView()) {
68+
return false;
69+
}
70+
71+
boolean consumed = false;
72+
boolean isMouse = ((event.getSource() & InputDevice.SOURCE_MOUSE_RELATIVE) == InputDevice.SOURCE_MOUSE_RELATIVE);
73+
if (isMouse && mouseInput != null) {
74+
consumed = ((AndroidMouseInput26)mouseInput).onCapturedPointer(event);
75+
}
76+
77+
return consumed;
78+
}
79+
}

0 commit comments

Comments
 (0)