1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.controllers.android; 18 19 import android.view.InputDevice; 20 import android.view.InputDevice.MotionRange; 21 22 import com.badlogic.gdx.controllers.ControlType; 23 import com.badlogic.gdx.controllers.Controller; 24 import com.badlogic.gdx.controllers.ControllerListener; 25 import com.badlogic.gdx.controllers.PovDirection; 26 import com.badlogic.gdx.math.Vector3; 27 import com.badlogic.gdx.utils.Array; 28 import com.badlogic.gdx.utils.GdxRuntimeException; 29 import com.badlogic.gdx.utils.IntFloatMap; 30 import com.badlogic.gdx.utils.IntIntMap; 31 32 public class AndroidController implements Controller { 33 private final int deviceId; 34 private boolean attached; 35 private final String name; 36 protected final IntIntMap buttons = new IntIntMap(); 37 protected final float[] axes; 38 protected final int[] axesIds; 39 private final Array<ControllerListener> listeners = new Array<ControllerListener>(); 40 41 public AndroidController(int deviceId, String name) { 42 this.deviceId = deviceId; 43 this.name = name; 44 45 InputDevice device = InputDevice.getDevice(deviceId); 46 int numAxes = 0; 47 for (MotionRange range : device.getMotionRanges()) { 48 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { 49 numAxes += 1; 50 } 51 } 52 53 axesIds = new int[numAxes]; 54 axes = new float[numAxes]; 55 int i = 0; 56 for (MotionRange range : device.getMotionRanges()) { 57 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { 58 axesIds[i++] = range.getAxis(); 59 } 60 } 61 } 62 63 public boolean isAttached () { 64 return attached; 65 } 66 67 public void setAttached (boolean attached) { 68 this.attached = attached; 69 } 70 71 public int getDeviceId () { 72 return deviceId; 73 } 74 75 @Override 76 public boolean getSliderX (int sliderIndex) { 77 return false; 78 } 79 80 @Override 81 public boolean getSliderY (int sliderIndex) { 82 return false; 83 } 84 85 @Override 86 public Vector3 getAccelerometer (int accelerometerIndex) { 87 return Vector3.Zero; 88 } 89 90 @Override 91 public void setAccelerometerSensitivity (float sensitivity) { 92 } 93 94 @Override 95 public void addListener (ControllerListener listener) { 96 this.listeners.add(listener); 97 } 98 99 @Override 100 public void removeListener (ControllerListener listener) { 101 this.listeners.removeValue(listener, true); 102 } 103 104 public Array<ControllerListener> getListeners() { 105 return this.listeners; 106 } 107 108 @Override 109 public boolean getButton (int buttonIndex) { 110 return buttons.containsKey(buttonIndex); 111 } 112 113 @Override 114 public float getAxis (int axisIndex) { 115 if(axisIndex < 0 || axisIndex >= axes.length) return 0; 116 return axes[axisIndex]; 117 } 118 119 @Override 120 public PovDirection getPov (int povIndex) { 121 return PovDirection.center; 122 } 123 124 @Override 125 public String getName () { 126 return name; 127 } 128 }