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,37 +201,51 @@ 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) {
joystick joy = new joystick(); int id = find_joy_device(deviceId);
joy.device_id = deviceId;
int id = joy_devices.size(); // Check if the device has not been already added
InputDevice device = mInputManager.getInputDevice(deviceId); if (id < 0) {
joy.name = device.getName(); InputDevice device = mInputManager.getInputDevice(deviceId);
joy.axes = new ArrayList<InputDevice.MotionRange>();
joy.hats = new ArrayList<InputDevice.MotionRange>(); id = joy_devices.size();
List<InputDevice.MotionRange> ranges = device.getMotionRanges();
Collections.sort(ranges, new RangeComparator()); joystick joy = new joystick();
for (InputDevice.MotionRange range : ranges) { joy.device_id = deviceId;
if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) { joy.name = device.getName();
joy.hats.add(range); joy.axes = new ArrayList<InputDevice.MotionRange>();
} else { joy.hats = new ArrayList<InputDevice.MotionRange>();
joy.axes.add(range);
List<InputDevice.MotionRange> ranges = device.getMotionRanges();
Collections.sort(ranges, new RangeComparator());
for (InputDevice.MotionRange range : ranges) {
if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) {
joy.hats.add(range);
} else {
joy.axes.add(range);
}
} }
joy_devices.add(joy);
GodotLib.joyconnectionchanged(id, true, joy.name);
} }
joy_devices.add(joy);
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);
joy_devices.remove(id);
GodotLib.joyconnectionchanged(id, false, ""); // Check if the evice has not been already removed
if (id > -1) {
joy_devices.remove(id);
GodotLib.joyconnectionchanged(id, false, "");
}
} }
@Override @Override
@ -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
return true; if (id > -1) {
GodotLib.joybutton(id, button, false);
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);
return true;
// Check if the device exists
if (id > -1) {
GodotLib.joybutton(id, button, 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);
for (int i = 0; i < joy.axes.size(); i++) { // Check if the device exists
InputDevice.MotionRange range = joy.axes.get(i); if (id > -1) {
float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f; joystick joy = joy_devices.get(id);
GodotLib.joyaxis(device_id, i, value);
}
for (int i = 0; i < joy.hats.size(); i += 2) { for (int i = 0; i < joy.axes.size(); i++) {
int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis())); InputDevice.MotionRange range = joy.axes.get(i);
int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis())); float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f;
GodotLib.joyhat(device_id, hatX, hatY); GodotLib.joyaxis(id, i, value);
}
for (int i = 0; i < joy.hats.size(); i += 2) {
int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis()));
int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis()));
GodotLib.joyhat(id, hatX, hatY);
}
return true;
} }
return true;
}; };
return super.onGenericMotionEvent(event); return super.onGenericMotionEvent(event);