Prevent a device to be added/deleted more than once on Android
cherry-picked from fb5a601217
This commit is contained in:
parent
6cc116d415
commit
7dcf779a8b
1 changed files with 100 additions and 74 deletions
|
@ -204,22 +204,29 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onInputDeviceAdded(device_id);
|
|
||||||
return joy_devices.size() - 1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInputDeviceAdded(int deviceId) {
|
public void onInputDeviceAdded(int deviceId) {
|
||||||
|
int id = find_joy_device(deviceId);
|
||||||
|
|
||||||
|
// Check if the device has not been already added
|
||||||
|
if (id < 0) {
|
||||||
|
InputDevice device = mInputManager.getInputDevice(deviceId);
|
||||||
|
|
||||||
|
id = joy_devices.size();
|
||||||
|
|
||||||
joystick joy = new joystick();
|
joystick joy = new joystick();
|
||||||
joy.device_id = deviceId;
|
joy.device_id = deviceId;
|
||||||
final int id = joy_devices.size();
|
|
||||||
InputDevice device = mInputManager.getInputDevice(deviceId);
|
|
||||||
final String name = device.getName();
|
|
||||||
joy.name = device.getName();
|
joy.name = device.getName();
|
||||||
joy.axes = new ArrayList<InputDevice.MotionRange>();
|
joy.axes = new ArrayList<InputDevice.MotionRange>();
|
||||||
joy.hats = new ArrayList<InputDevice.MotionRange>();
|
joy.hats = new ArrayList<InputDevice.MotionRange>();
|
||||||
|
|
||||||
List<InputDevice.MotionRange> ranges = device.getMotionRanges();
|
List<InputDevice.MotionRange> ranges = device.getMotionRanges();
|
||||||
Collections.sort(ranges, new RangeComparator());
|
Collections.sort(ranges, new RangeComparator());
|
||||||
|
|
||||||
for (InputDevice.MotionRange range : ranges) {
|
for (InputDevice.MotionRange range : ranges) {
|
||||||
if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) {
|
if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) {
|
||||||
joy.hats.add(range);
|
joy.hats.add(range);
|
||||||
|
@ -227,26 +234,36 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
joy.axes.add(range);
|
joy.axes.add(range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
joy_devices.add(joy);
|
joy_devices.add(joy);
|
||||||
|
|
||||||
|
final int device_id = id;
|
||||||
|
final String name = joy.name;
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
GodotLib.joyconnectionchanged(id, true, name);
|
GodotLib.joyconnectionchanged(device_id, true, name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInputDeviceRemoved(int deviceId) {
|
public void onInputDeviceRemoved(int deviceId) {
|
||||||
final int id = find_joy_device(deviceId);
|
final int device_id = find_joy_device(deviceId);
|
||||||
joy_devices.remove(id);
|
|
||||||
|
// Check if the evice has not been already removed
|
||||||
|
if (device_id > -1) {
|
||||||
|
joy_devices.remove(device_id);
|
||||||
|
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
GodotLib.joyconnectionchanged(id, false, "");
|
GodotLib.joyconnectionchanged(device_id, false, "");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInputDeviceChanged(int deviceId) {
|
public void onInputDeviceChanged(int deviceId) {
|
||||||
|
@ -266,15 +283,18 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
if ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK || (source & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD || (source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
|
if ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK || (source & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD || (source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
|
||||||
|
|
||||||
final int button = get_godot_button(keyCode);
|
final int button = get_godot_button(keyCode);
|
||||||
final int device = find_joy_device(event.getDeviceId());
|
final int device_id = find_joy_device(event.getDeviceId());
|
||||||
|
|
||||||
|
// Check if the device exists
|
||||||
|
if (device_id > -1) {
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
GodotLib.joybutton(device, button, false);
|
GodotLib.joybutton(device_id, button, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
final int chr = event.getUnicodeChar(0);
|
final int chr = event.getUnicodeChar(0);
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
|
@ -284,6 +304,7 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return super.onKeyUp(keyCode, event);
|
return super.onKeyUp(keyCode, event);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -308,18 +329,20 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
|
|
||||||
if (event.getRepeatCount() > 0) // ignore key echo
|
if (event.getRepeatCount() > 0) // ignore key echo
|
||||||
return true;
|
return true;
|
||||||
final int button = get_godot_button(keyCode);
|
|
||||||
final int device = find_joy_device(event.getDeviceId());
|
|
||||||
|
|
||||||
//Log.e(TAG, String.format("joy button down! button %x, %d, device %d", keyCode, button, device));
|
final int button = get_godot_button(keyCode);
|
||||||
|
final int device_id = find_joy_device(event.getDeviceId());
|
||||||
|
|
||||||
|
// Check if the device exists
|
||||||
|
if (device_id > -1) {
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
GodotLib.joybutton(device, button, true);
|
GodotLib.joybutton(device_id, button, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
final int chr = event.getUnicodeChar(0);
|
final int chr = event.getUnicodeChar(0);
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
|
@ -329,6 +352,7 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return super.onKeyDown(keyCode, event);
|
return super.onKeyDown(keyCode, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,12 +362,14 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK && event.getAction() == MotionEvent.ACTION_MOVE) {
|
if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK && event.getAction() == MotionEvent.ACTION_MOVE) {
|
||||||
|
|
||||||
final int device_id = find_joy_device(event.getDeviceId());
|
final int device_id = find_joy_device(event.getDeviceId());
|
||||||
|
|
||||||
|
// Check if the device exists
|
||||||
|
if (device_id > -1) {
|
||||||
joystick joy = joy_devices.get(device_id);
|
joystick joy = joy_devices.get(device_id);
|
||||||
|
|
||||||
for (int i = 0; i < joy.axes.size(); i++) {
|
for (int i = 0; i < joy.axes.size(); i++) {
|
||||||
InputDevice.MotionRange range = joy.axes.get(i);
|
InputDevice.MotionRange range = joy.axes.get(i);
|
||||||
final float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f;
|
final float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f;
|
||||||
//Log.e(TAG, String.format("axis event: %d, value %f", i, value));
|
|
||||||
final int idx = i;
|
final int idx = i;
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -356,7 +382,6 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
for (int i = 0; i < joy.hats.size(); i += 2) {
|
for (int i = 0; i < joy.hats.size(); i += 2) {
|
||||||
final int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis()));
|
final int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis()));
|
||||||
final int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis()));
|
final int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis()));
|
||||||
//Log.e(TAG, String.format("HAT EVENT %d, %d", hatX, hatY));
|
|
||||||
queueEvent(new Runnable() {
|
queueEvent(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -365,6 +390,7 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return super.onGenericMotionEvent(event);
|
return super.onGenericMotionEvent(event);
|
||||||
|
|
Loading…
Reference in a new issue