Prevent a device to be added/deleted more than once on Android

This commit is contained in:
Xavier Sellier 2018-04-05 10:15:57 -04:00
parent f5b95f0f81
commit fb5a601217
1 changed files with 65 additions and 40 deletions

View File

@ -201,21 +201,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;
int id = joy_devices.size();
InputDevice device = mInputManager.getInputDevice(deviceId);
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);
@ -223,16 +231,22 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
joy.axes.add(range); joy.axes.add(range);
} }
} }
joy_devices.add(joy); joy_devices.add(joy);
GodotLib.joyconnectionchanged(id, true, joy.name); GodotLib.joyconnectionchanged(id, true, joy.name);
} }
}
@Override @Override
public void onInputDeviceRemoved(int deviceId) { public void onInputDeviceRemoved(int deviceId) {
int id = find_joy_device(deviceId); int id = find_joy_device(deviceId);
// Check if the evice has not been already removed
if (id > -1) {
joy_devices.remove(id); joy_devices.remove(id);
GodotLib.joyconnectionchanged(id, false, ""); GodotLib.joyconnectionchanged(id, false, "");
} }
}
@Override @Override
public void onInputDeviceChanged(int deviceId) { public void onInputDeviceChanged(int deviceId) {
@ -252,14 +266,17 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
if ((source & InputDevice.SOURCE_JOYSTICK) != 0 || (source & InputDevice.SOURCE_DPAD) != 0 || (source & InputDevice.SOURCE_GAMEPAD) != 0) { if ((source & InputDevice.SOURCE_JOYSTICK) != 0 || (source & InputDevice.SOURCE_DPAD) != 0 || (source & InputDevice.SOURCE_GAMEPAD) != 0) {
int button = get_godot_button(keyCode); int button = get_godot_button(keyCode);
int device = find_joy_device(event.getDeviceId()); int id = find_joy_device(event.getDeviceId());
GodotLib.joybutton(device, button, false); // Check if the device exists
if (id > -1) {
GodotLib.joybutton(id, button, false);
return true; return true;
}
} else { } else {
GodotLib.key(keyCode, event.getUnicodeChar(0), false); GodotLib.key(keyCode, event.getUnicodeChar(0), false);
}; };
return super.onKeyUp(keyCode, event); return super.onKeyUp(keyCode, event);
}; };
@ -283,15 +300,19 @@ 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;
int button = get_godot_button(keyCode); int button = get_godot_button(keyCode);
int device = find_joy_device(event.getDeviceId()); int id = find_joy_device(event.getDeviceId());
GodotLib.joybutton(device, button, true); // Check if the device exists
if (id > -1) {
GodotLib.joybutton(id, button, true);
return true; return true;
}
} else { } else {
GodotLib.key(keyCode, event.getUnicodeChar(0), true); GodotLib.key(keyCode, event.getUnicodeChar(0), true);
}; };
return super.onKeyDown(keyCode, event); return super.onKeyDown(keyCode, event);
} }
@ -300,21 +321,25 @@ 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) {
int device_id = find_joy_device(event.getDeviceId()); int id = find_joy_device(event.getDeviceId());
joystick joy = joy_devices.get(device_id);
// Check if the device exists
if (id > -1) {
joystick joy = joy_devices.get(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);
float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f; float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f;
GodotLib.joyaxis(device_id, i, value); GodotLib.joyaxis(id, i, value);
} }
for (int i = 0; i < joy.hats.size(); i += 2) { for (int i = 0; i < joy.hats.size(); i += 2) {
int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis())); int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis()));
int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis())); int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis()));
GodotLib.joyhat(device_id, hatX, hatY); GodotLib.joyhat(id, hatX, hatY);
} }
return true; return true;
}
}; };
return super.onGenericMotionEvent(event); return super.onGenericMotionEvent(event);