com.badlogic.gdx.controllers.android.AndroidController Maven / Gradle / Ivy
/*******************************************************************************
* 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.controllers.android;
import android.view.InputDevice;
import android.view.InputDevice.MotionRange;
import com.badlogic.gdx.controllers.ControlType;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.ControllerListener;
import com.badlogic.gdx.controllers.PovDirection;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.IntFloatMap;
import com.badlogic.gdx.utils.IntIntMap;
public class AndroidController implements Controller {
private final int deviceId;
private boolean attached;
private final String name;
protected final IntIntMap buttons = new IntIntMap();
protected final float[] axes;
protected final int[] axesIds;
private final Array listeners = new Array();
public AndroidController(int deviceId, String name) {
this.deviceId = deviceId;
this.name = name;
InputDevice device = InputDevice.getDevice(deviceId);
int numAxes = 0;
for (MotionRange range : device.getMotionRanges()) {
if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
numAxes += 1;
}
}
axesIds = new int[numAxes];
axes = new float[numAxes];
int i = 0;
for (MotionRange range : device.getMotionRanges()) {
if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
axesIds[i++] = range.getAxis();
}
}
}
public boolean isAttached () {
return attached;
}
public void setAttached (boolean attached) {
this.attached = attached;
}
public int getDeviceId () {
return deviceId;
}
@Override
public boolean getSliderX (int sliderIndex) {
return false;
}
@Override
public boolean getSliderY (int sliderIndex) {
return false;
}
@Override
public Vector3 getAccelerometer (int accelerometerIndex) {
return Vector3.Zero;
}
@Override
public void setAccelerometerSensitivity (float sensitivity) {
}
@Override
public void addListener (ControllerListener listener) {
this.listeners.add(listener);
}
@Override
public void removeListener (ControllerListener listener) {
this.listeners.removeValue(listener, true);
}
public Array getListeners() {
return this.listeners;
}
@Override
public boolean getButton (int buttonIndex) {
return buttons.containsKey(buttonIndex);
}
@Override
public float getAxis (int axisIndex) {
if(axisIndex < 0 || axisIndex >= axes.length) return 0;
return axes[axisIndex];
}
@Override
public PovDirection getPov (int povIndex) {
return PovDirection.center;
}
@Override
public String getName () {
return name;
}
}