| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Controllers |
|
| 2.8;2.8 |
| 1 | /* |
|
| 2 | * Controllers.java |
|
| 3 | * |
|
| 4 | * This file is part of JavaGear. |
|
| 5 | * |
|
| 6 | * JavaGear is free software; you can redistribute it and/or modify |
|
| 7 | * it under the terms of the GNU General Public License as published by |
|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|
| 9 | * (at your option) any later version. |
|
| 10 | * |
|
| 11 | * JavaGear is distributed in the hope that it will be useful, |
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 14 | * GNU General Public License for more details. |
|
| 15 | * |
|
| 16 | * You should have received a copy of the GNU General Public License |
|
| 17 | * along with JavaGear; if not, write to the Free Software |
|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 19 | */ |
|
| 20 | ||
| 21 | package uk.co.javagear; |
|
| 22 | ||
| 23 | import java.awt.event.KeyEvent; |
|
| 24 | import java.awt.event.KeyListener; |
|
| 25 | ||
| 26 | ||
| 27 | /** |
|
| 28 | * Emulates SMS/GG controllers. |
|
| 29 | * |
|
| 30 | * @author Copyright (C) 2002-2003 Chris White |
|
| 31 | * @version 18th January 2003 |
|
| 32 | * @see "JavaGear Final Project Report" |
|
| 33 | */ |
|
| 34 | public final class Controllers implements KeyListener { |
|
| 35 | ||
| 36 | private Setup setup; |
|
| 37 | ||
| 38 | /** |
|
| 39 | * Controller 1. |
|
| 40 | */ |
|
| 41 | private int controller1; |
|
| 42 | ||
| 43 | /** |
|
| 44 | * Controller 2. |
|
| 45 | */ |
|
| 46 | private int controller2; |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Game Gear Start Button. |
|
| 50 | */ |
|
| 51 | private int ggstart; |
|
| 52 | ||
| 53 | /** |
|
| 54 | * SMS Reset Button. |
|
| 55 | */ |
|
| 56 | private boolean resetButton; |
|
| 57 | ||
| 58 | // Default Key Mappings |
|
| 59 | 0 | private int up = KeyEvent.VK_UP; |
| 60 | 0 | private int down = KeyEvent.VK_DOWN; |
| 61 | 0 | private int left = KeyEvent.VK_LEFT; |
| 62 | 0 | private int right = KeyEvent.VK_RIGHT; |
| 63 | 0 | private int fire1 = KeyEvent.VK_Z; |
| 64 | 0 | private int fire2 = KeyEvent.VK_X; |
| 65 | 0 | private int start = KeyEvent.VK_ENTER; |
| 66 | ||
| 67 | /** |
|
| 68 | * Controllers Constructor. |
|
| 69 | * |
|
| 70 | * @param setup the system's settings. |
|
| 71 | */ |
|
| 72 | 0 | public Controllers(Setup setup) { |
| 73 | 0 | this.setup = setup; |
| 74 | // Default 0xFF = No Keys Pressed |
|
| 75 | 0 | controller1 = 0xFF; |
| 76 | 0 | controller2 = 0xFF; |
| 77 | 0 | ggstart = 0xFF; |
| 78 | 0 | resetButton = false; |
| 79 | 0 | this.setup.setPauseButton(false); |
| 80 | 0 | } |
| 81 | ||
| 82 | /** |
|
| 83 | * Key pressed. |
|
| 84 | * |
|
| 85 | * @param evt the <code>KeyEvent</code>. |
|
| 86 | */ |
|
| 87 | public void keyPressed(KeyEvent evt) { |
|
| 88 | 0 | int keyCode = evt.getKeyCode(); |
| 89 | ||
| 90 | 0 | if (keyCode == up) { |
| 91 | 0 | controller1 &= ~0x01; // Up |
| 92 | 0 | } else if (keyCode == down) { |
| 93 | 0 | controller1 &= ~0x02; // Down |
| 94 | 0 | } else if (keyCode == left) { |
| 95 | 0 | controller1 &= ~0x04; // Left |
| 96 | 0 | } else if (keyCode == right) { |
| 97 | 0 | controller1 &= ~0x08; // Right |
| 98 | 0 | } else if (keyCode == fire1) { |
| 99 | 0 | controller1 &= ~0x10; // Fire 1 |
| 100 | 0 | } else if (keyCode == fire2) { |
| 101 | 0 | controller1 &= ~0x20; // Fire 2 |
| 102 | 0 | } else if (keyCode == start) { |
| 103 | 0 | if (Setup.System.SMS == setup.getSystem()) { |
| 104 | 0 | setup.setPauseButton(true); // Pause |
| 105 | } else { |
|
| 106 | 0 | ggstart &= ~0x80; // Start |
| 107 | } |
|
| 108 | } |
|
| 109 | 0 | } |
| 110 | ||
| 111 | /** |
|
| 112 | * Key released. |
|
| 113 | * |
|
| 114 | * @param evt the <code>KeyEvent</code>. |
|
| 115 | */ |
|
| 116 | public void keyReleased(KeyEvent evt) { |
|
| 117 | 0 | int keyCode = evt.getKeyCode(); |
| 118 | ||
| 119 | 0 | if (keyCode == up) { |
| 120 | 0 | controller1 |= 0x01; // Up |
| 121 | 0 | } else if (keyCode == down) { |
| 122 | 0 | controller1 |= 0x02; // Down |
| 123 | 0 | } else if (keyCode == left) { |
| 124 | 0 | controller1 |= 0x04; // Left |
| 125 | 0 | } else if (keyCode == right) { |
| 126 | 0 | controller1 |= 0x08; // Right |
| 127 | 0 | } else if (keyCode == fire1) { |
| 128 | 0 | controller1 |= 0x10; // Fire 1 |
| 129 | 0 | } else if (keyCode == fire2) { |
| 130 | 0 | controller1 |= 0x20; // Fire 2 |
| 131 | 0 | } else if (keyCode == start) { |
| 132 | 0 | if (Setup.System.SMS == setup.getSystem()) { |
| 133 | 0 | setup.setPauseButton(false); // Pause |
| 134 | } else { |
|
| 135 | 0 | ggstart |= 0x80; // Start |
| 136 | } |
|
| 137 | } |
|
| 138 | 0 | } |
| 139 | ||
| 140 | /** |
|
| 141 | * Does nothing. |
|
| 142 | * |
|
| 143 | * @param evt and instance of <code>KeyEvent</code>. |
|
| 144 | */ |
|
| 145 | public void keyTyped(KeyEvent evt) { |
|
| 146 | 0 | } |
| 147 | ||
| 148 | /** |
|
| 149 | * Get status of control pad 1. |
|
| 150 | * |
|
| 151 | * @return An integer representing the state the control pad. |
|
| 152 | */ |
|
| 153 | public int getController1() { |
|
| 154 | 0 | return controller1; |
| 155 | } |
|
| 156 | ||
| 157 | /** |
|
| 158 | * Get status of control pad 2. |
|
| 159 | * |
|
| 160 | * @return an integer representing the state the control pad. |
|
| 161 | */ |
|
| 162 | public int getController2() { |
|
| 163 | 0 | if (!resetButton) { |
| 164 | 0 | return controller2; |
| 165 | } else { |
|
| 166 | 0 | resetButton = false; |
| 167 | 0 | return controller2 & ~0x10; |
| 168 | } |
|
| 169 | } |
|
| 170 | ||
| 171 | /** |
|
| 172 | * Get status of Game Gear start button. |
|
| 173 | * |
|
| 174 | * @return An integer representing the state of the button. |
|
| 175 | */ |
|
| 176 | public int getGameGearStart() { |
|
| 177 | 0 | return ggstart; |
| 178 | } |
|
| 179 | ||
| 180 | /** |
|
| 181 | * Set SMS reset button. |
|
| 182 | */ |
|
| 183 | public void setReset() { |
|
| 184 | 0 | resetButton = true; |
| 185 | 0 | } |
| 186 | ||
| 187 | /** |
|
| 188 | * Redefine the key mapping. |
|
| 189 | * |
|
| 190 | * @param newKeys an array of key codes. |
|
| 191 | */ |
|
| 192 | public void setKeys(int [] newKeys) { |
|
| 193 | 0 | up = newKeys[0]; |
| 194 | 0 | down = newKeys[1]; |
| 195 | 0 | left = newKeys[2]; |
| 196 | 0 | right = newKeys[3]; |
| 197 | 0 | fire1 = newKeys[4]; |
| 198 | 0 | fire2 = newKeys[5]; |
| 199 | 0 | start = newKeys[6]; |
| 200 | 0 | } |
| 201 | ||
| 202 | /** |
|
| 203 | * Get the current key mapping. |
|
| 204 | * |
|
| 205 | * @return an array of key codes. |
|
| 206 | */ |
|
| 207 | public int[] getKeys() { |
|
| 208 | 0 | int[] keys = {up, down, left, right, fire1, fire2, start}; |
| 209 | 0 | return keys; |
| 210 | } |
|
| 211 | ||
| 212 | } |