44import com .badlogic .gdx .Gdx ;
55import com .badlogic .gdx .Input ;
66import com .badlogic .gdx .InputAdapter ;
7+ import com .badlogic .gdx .controllers .Controller ;
8+ import com .badlogic .gdx .controllers .ControllerListener ;
9+ import com .badlogic .gdx .controllers .Controllers ;
10+ import com .badlogic .gdx .controllers .PovDirection ;
11+ import com .badlogic .gdx .math .Vector3 ;
712import com .miloshpetrov .sol2 .GameOptions ;
813import com .miloshpetrov .sol2 .SolApplication ;
914import com .miloshpetrov .sol2 .ui .SolInputManager ;
@@ -21,6 +26,7 @@ public class InputMapControllerScreen implements InputMapOperations {
2126 private boolean isEnterNewKey ;
2227 private List <InputConfigItem > itemsList = new ArrayList <InputConfigItem >();
2328 private int selectedIndex ;
29+ private int controllerItems ;
2430
2531 public InputMapControllerScreen (InputMapScreen inputMapScreen , GameOptions gameOptions ) {
2632 controls = new ArrayList <SolUiControl >();
@@ -49,6 +55,8 @@ private void InitialiseList(GameOptions gameOptions) {
4955 itemsList .add (InitItem (gameOptions .getControllerAxisShoot2 (), gameOptions .getControllerButtonShoot2 (), "Shoot Secondary" ));
5056 itemsList .add (InitItem (gameOptions .getControllerAxisAbility (), gameOptions .getControllerButtonAbility (), "Ability" ));
5157
58+ controllerItems = itemsList .size ();
59+
5260 // Menu and Interface Keys
5361 itemsList .add (new InputConfigItem ("Pause" , gameOptions .getKeyPauseName ()));
5462 itemsList .add (new InputConfigItem ("Map" , gameOptions .getKeyMapName ()));
@@ -166,28 +174,108 @@ public void updateCustom(SolApplication cmp, SolInputManager.Ptr[] ptrs, boolean
166174 if (changeCtrl .isJustOff ()) {
167175 isEnterNewKey = !isEnterNewKey ;
168176
169- // TODO: Capture Controller Inputs
170-
171177 // Can cancel the key entering by clicking this button a second time
172178 if (!isEnterNewKey ) {
173179 Gdx .input .setInputProcessor (null );
180+ Controllers .clearListeners ();
174181 return ;
175182 }
176183
177- Gdx .input .setInputProcessor (new InputAdapter () {
178- @ Override
179- public boolean keyUp (int keycode ) {
180- if (selectedIndex >= 7 ) {
184+ // Keyboard items
185+ if (selectedIndex >= controllerItems ) {
186+ Gdx .input .setInputProcessor (new InputAdapter () {
187+ @ Override
188+ public boolean keyUp (int keycode ) {
181189 InputConfigItem item = itemsList .get (selectedIndex );
182190 item .setInputKey (Input .Keys .toString (keycode ));
183191 itemsList .set (selectedIndex , item );
192+ Gdx .input .setInputProcessor (null );
193+ Controllers .clearListeners ();
194+
195+ isEnterNewKey = false ;
196+ return true ; // return true to indicate the event was handled
197+ }
198+ });
199+ // Controller items
200+ } else {
201+ // setup the listener that prints events to the console
202+ Controllers .addListener (new ControllerListener () {
203+ public int indexOf (Controller controller ) {
204+ return Controllers .getControllers ().indexOf (controller , true );
205+ }
206+
207+ @ Override
208+ public void connected (Controller controller ) {
209+ }
210+
211+ @ Override
212+ public void disconnected (Controller controller ) {
213+ }
214+
215+ @ Override
216+ public boolean buttonDown (Controller controller , int buttonIndex ) {
217+ // Do nothing on button down - register the button up event
218+ return true ;
219+ }
220+
221+ @ Override
222+ public boolean buttonUp (Controller controller , int buttonIndex ) {
223+ System .out .println ("#" + indexOf (controller ) + ", button " + buttonIndex + " up" );
224+
225+ InputConfigItem item = itemsList .get (selectedIndex );
226+ item .setIsAxis (false );
227+ item .setControllerInput (buttonIndex );
228+ item .setInputKey ("Button: " + buttonIndex );
229+ itemsList .set (selectedIndex , item );
230+
231+ Gdx .input .setInputProcessor (null );
232+ Controllers .clearListeners ();
233+
234+ isEnterNewKey = false ;
235+ return true ; // return true to indicate the event was handled
236+ }
237+
238+ @ Override
239+ public boolean axisMoved (Controller controller , int axisIndex , float value ) {
240+ System .out .println ("#" + indexOf (controller ) + ", axis " + axisIndex + ": " + value );
241+
242+ if (value > 0.5f || value < -0.5f ) {
243+ InputConfigItem item = itemsList .get (selectedIndex );
244+ item .setIsAxis (true );
245+ item .setControllerInput (axisIndex );
246+ item .setInputKey ("Axis: " + axisIndex );
247+ itemsList .set (selectedIndex , item );
248+
249+ Gdx .input .setInputProcessor (null );
250+ Controllers .clearListeners ();
251+
252+ isEnterNewKey = false ;
253+
254+ }
255+ return true ;
184256 }
185- Gdx .input .setInputProcessor (null );
186257
187- isEnterNewKey = false ;
188- return true ; // return true to indicate the event was handled
189- }
190- });
258+ @ Override
259+ public boolean povMoved (Controller controller , int povIndex , PovDirection value ) {
260+ return false ;
261+ }
262+
263+ @ Override
264+ public boolean xSliderMoved (Controller controller , int sliderIndex , boolean value ) {
265+ return false ;
266+ }
267+
268+ @ Override
269+ public boolean ySliderMoved (Controller controller , int sliderIndex , boolean value ) {
270+ return false ;
271+ }
272+
273+ @ Override
274+ public boolean accelerometerMoved (Controller controller , int accelerometerIndex , Vector3 value ) {
275+ return false ;
276+ }
277+ });
278+ }
191279 }
192280 }
193281
@@ -218,6 +306,7 @@ public boolean isCursorOnBg(SolInputManager.Ptr ptr) {
218306 @ Override
219307 public void onAdd (SolApplication cmp ) {
220308 InitialiseList (cmp .getOptions ());
309+ Controllers .clearListeners ();
221310 isEnterNewKey = false ;
222311 selectedIndex = 0 ;
223312 }
0 commit comments