1   /*
2    * Copyright 2008 motej
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  package motej.demos.classic;
17  
18  import java.awt.GridLayout;
19  
20  import javax.swing.JPanel;
21  import javax.swing.JTextField;
22  import javax.swing.SwingUtilities;
23  
24  import motejx.extensions.classic.ClassicController;
25  import motejx.extensions.classic.ClassicControllerButtonEvent;
26  import motejx.extensions.classic.ClassicControllerButtonListener;
27  
28  /**
29   * 
30   * <p>
31   * @author <a href="mailto:vfritzsch@users.sourceforge.net">Volker Fritzsch</a>
32   */
33  public class DigitalButtonPanel implements ClassicControllerButtonListener {
34  
35  	private JPanel panel;
36  	
37  	private JTextField noButton = new JTextField("No Button");
38  	
39  	private JTextField buttonX = new JTextField("X");
40  	
41  	private JTextField buttonY = new JTextField("Y");
42  	
43  	private JTextField buttonA = new JTextField("A");
44  	
45  	private JTextField buttonB = new JTextField("B");
46  	
47  	private JTextField buttonZL = new JTextField("ZL");
48  	
49  	private JTextField buttonZR = new JTextField("ZR");
50  	
51  	private JTextField buttonMinus = new JTextField("- / Select");
52  	
53  	private JTextField buttonPlus = new JTextField("+ / Start");
54  	
55  	private JTextField buttonHome = new JTextField("Home");
56  	
57  	private JTextField triggerLeft = new JTextField("Trigger-L");
58  	
59  	private JTextField triggerRight = new JTextField("Trigger-R");
60  	
61  	private JTextField dPadLeft = new JTextField("DPad Left");
62  	
63  	private JTextField dPadRight = new JTextField("DPad Right");
64  	
65  	private JTextField dPadUp = new JTextField("DPad Up");
66  	
67  	private JTextField dPadDown = new JTextField("DPad Down");
68  	
69  	public DigitalButtonPanel(ClassicController cc) {
70  		cc.addClassicControllerButtonListener(this);
71  		
72  		panel = new JPanel(new GridLayout(8,2));
73  		panel.add(noButton);
74  		panel.add(buttonX);
75  		panel.add(buttonY);
76  		panel.add(buttonA);
77  		panel.add(buttonB);
78  		panel.add(buttonPlus);
79  		panel.add(buttonMinus);
80  		panel.add(buttonHome);
81  		panel.add(buttonZL);
82  		panel.add(buttonZR);
83  		panel.add(triggerLeft);
84  		panel.add(triggerRight);
85  		panel.add(dPadLeft);
86  		panel.add(dPadRight);
87  		panel.add(dPadUp);
88  		panel.add(dPadDown);
89  	}
90  
91  	public void buttonPressed(final ClassicControllerButtonEvent evt) {
92  		SwingUtilities.invokeLater(new Runnable() {
93  		
94  			public void run() {
95  				buttonX.setEnabled(evt.isButtonXPressed());
96  				buttonY.setEnabled(evt.isButtonYPressed());
97  				buttonA.setEnabled(evt.isButtonAPressed());
98  				buttonB.setEnabled(evt.isButtonBPressed());
99  				buttonZL.setEnabled(evt.isButtonZLPressed());
100 				buttonZR.setEnabled(evt.isButtonZRPressed());
101 				buttonPlus.setEnabled(evt.isButtonPlusPressed());
102 				buttonMinus.setEnabled(evt.isButtonMinusPressed());
103 				buttonHome.setEnabled(evt.isButtonHomePressed());
104 				triggerLeft.setEnabled(evt.isButtonLeftTriggerPressed());
105 				triggerRight.setEnabled(evt.isButtonRightTriggerPressed());
106 				dPadLeft.setEnabled(evt.isDPadLeftPressed());
107 				dPadRight.setEnabled(evt.isDPadRightPressed());
108 				dPadUp.setEnabled(evt.isDPadUpPressed());
109 				dPadDown.setEnabled(evt.isDPadDownPressed());
110 				noButton.setEnabled(evt.isNoButtonPressed());
111 			}
112 		});
113 	}
114 	
115 	public JPanel getPanel() {
116 		return panel;
117 	}
118 }