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; 18 19 import com.badlogic.gdx.Application; 20 import com.badlogic.gdx.ApplicationListener; 21 import com.badlogic.gdx.Gdx; 22 import com.badlogic.gdx.Graphics.GraphicsType; 23 import com.badlogic.gdx.LifecycleListener; 24 import com.badlogic.gdx.Application.ApplicationType; 25 import com.badlogic.gdx.utils.Array; 26 import com.badlogic.gdx.utils.GdxRuntimeException; 27 import com.badlogic.gdx.utils.ObjectMap; 28 import com.badlogic.gdx.utils.reflect.ClassReflection; 29 30 /** Provides access to connected {@link Controller} instances. Query the available controllers via {@link #getControllers()}, add 31 * and remove global {@link ControllerListener} instances via {@link #addListener(ControllerListener)} and 32 * {@link #removeListener(ControllerListener)}. The listeners will be invoked on the rendering thread. The global listeners will 33 * be invoked for all events generated by all controllers. Polling a Controller can be done by invoking one of its getter methods. 34 * 35 * @author Nathan Sweet */ 36 public class Controllers { 37 private static final String TAG = "Controllers"; 38 static final ObjectMap<Application, ControllerManager> managers = new ObjectMap<Application, ControllerManager>(); 39 40 /** Returns an array of connected {@link Controller} instances. This method should only be called on the rendering thread. 41 * 42 * @return the connected controllers */ getControllers()43 static public Array<Controller> getControllers () { 44 initialize(); 45 return getManager().getControllers(); 46 } 47 48 /** Add a global {@link ControllerListener} that can react to events from all {@link Controller} instances. The listener will be 49 * invoked on the rendering thread. 50 * @param listener */ addListener(ControllerListener listener)51 static public void addListener (ControllerListener listener) { 52 initialize(); 53 getManager().addListener(listener); 54 } 55 56 /** Removes a global {@link ControllerListener}. The method must be called on the rendering thread. 57 * @param listener */ removeListener(ControllerListener listener)58 static public void removeListener (ControllerListener listener) { 59 initialize(); 60 getManager().removeListener(listener); 61 } 62 63 /** Removes every global {@link ControllerListener} previously added. */ clearListeners()64 static public void clearListeners () { 65 initialize(); 66 getManager().clearListeners(); 67 } 68 getManager()69 static private ControllerManager getManager () { 70 return managers.get(Gdx.app); 71 } 72 initialize()73 static private void initialize () { 74 if (managers.containsKey(Gdx.app)) return; 75 76 String className = null; 77 ApplicationType type = Gdx.app.getType(); 78 ControllerManager manager = null; 79 80 if (type == ApplicationType.Android) { 81 if (Gdx.app.getVersion() >= 12) { 82 className = "com.badlogic.gdx.controllers.android.AndroidControllers"; 83 } else { 84 Gdx.app.log(TAG, "No controller manager is available for Android versions < API level 12"); 85 manager = new ControllerManagerStub(); 86 } 87 } else if (type == ApplicationType.Desktop) { 88 if(Gdx.graphics.getType() == GraphicsType.LWJGL3) { 89 className = "com.badlogic.gdx.controllers.lwjgl3.Lwjgl3ControllerManager"; 90 } else { 91 className = "com.badlogic.gdx.controllers.desktop.DesktopControllerManager"; 92 } 93 } else if (type == ApplicationType.WebGL) { 94 className = "com.badlogic.gdx.controllers.gwt.GwtControllers"; 95 } else { 96 Gdx.app.log(TAG, "No controller manager is available for: " + Gdx.app.getType()); 97 manager = new ControllerManagerStub(); 98 } 99 100 if (manager == null) { 101 try { 102 Class controllerManagerClass = ClassReflection.forName(className); 103 manager = (ControllerManager)ClassReflection.newInstance(controllerManagerClass); 104 } catch (Throwable ex) { 105 throw new GdxRuntimeException("Error creating controller manager: " + className, ex); 106 } 107 } 108 109 managers.put(Gdx.app, manager); 110 final Application app = Gdx.app; 111 Gdx.app.addLifecycleListener(new LifecycleListener() { 112 @Override 113 public void resume () { 114 } 115 116 @Override 117 public void pause () { 118 } 119 120 @Override 121 public void dispose () { 122 managers.remove(app); 123 Gdx.app.log(TAG, "removed manager for application, " + managers.size + " managers active"); 124 125 } 126 }); 127 Gdx.app.log(TAG, "added manager for application, " + managers.size + " managers active"); 128 } 129 } 130