|
| 1 | +extends RefCounted |
| 2 | +class_name ControllerMapper |
| 3 | + |
| 4 | +func _convert_joypad_path(path: String, device: int, fallback: ControllerSettings.Devices, force_controller_icon_style = ControllerSettings.Devices.NONE) -> String: |
| 5 | + match _get_joypad_type(device, fallback, force_controller_icon_style): |
| 6 | + ControllerSettings.Devices.LUNA: |
| 7 | + return _convert_joypad_to_luna(path) |
| 8 | + ControllerSettings.Devices.PS3: |
| 9 | + return _convert_joypad_to_ps3(path) |
| 10 | + ControllerSettings.Devices.PS4: |
| 11 | + return _convert_joypad_to_ps4(path) |
| 12 | + ControllerSettings.Devices.PS5: |
| 13 | + return _convert_joypad_to_ps5(path) |
| 14 | + ControllerSettings.Devices.STADIA: |
| 15 | + return _convert_joypad_to_stadia(path) |
| 16 | + ControllerSettings.Devices.STEAM: |
| 17 | + return _convert_joypad_to_steam(path) |
| 18 | + ControllerSettings.Devices.SWITCH: |
| 19 | + return _convert_joypad_to_switch(path) |
| 20 | + ControllerSettings.Devices.JOYCON: |
| 21 | + return _convert_joypad_to_joycon(path) |
| 22 | + ControllerSettings.Devices.XBOX360: |
| 23 | + return _convert_joypad_to_xbox360(path) |
| 24 | + ControllerSettings.Devices.XBOXONE: |
| 25 | + return _convert_joypad_to_xboxone(path) |
| 26 | + ControllerSettings.Devices.XBOXSERIES: |
| 27 | + return _convert_joypad_to_xboxseries(path) |
| 28 | + ControllerSettings.Devices.STEAM_DECK: |
| 29 | + return _convert_joypad_to_steamdeck(path) |
| 30 | + ControllerSettings.Devices.OUYA: |
| 31 | + return _convert_joypad_to_ouya(path) |
| 32 | + _: |
| 33 | + return "" |
| 34 | + |
| 35 | +func _get_joypad_type(device, fallback, force_controller_icon_style): |
| 36 | + if force_controller_icon_style != ControllerSettings.Devices.NONE: |
| 37 | + return force_controller_icon_style |
| 38 | + var available = Input.get_connected_joypads() |
| 39 | + if available.is_empty(): |
| 40 | + return fallback |
| 41 | + # If the requested joypad is not on the connected joypad list, try using the last known connected joypad |
| 42 | + if not device in available: |
| 43 | + device = ControllerIcons._last_controller |
| 44 | + # If that fails too, then use whatever joypad we have connected right now |
| 45 | + if not device in available: |
| 46 | + device = available.front() |
| 47 | + |
| 48 | + var controller_name = Input.get_joy_name(device) |
| 49 | + if "Luna Controller" in controller_name: |
| 50 | + return ControllerSettings.Devices.LUNA |
| 51 | + elif "PS3 Controller" in controller_name: |
| 52 | + return ControllerSettings.Devices.PS3 |
| 53 | + elif "PS4 Controller" in controller_name or \ |
| 54 | + "DUALSHOCK 4" in controller_name: |
| 55 | + return ControllerSettings.Devices.PS4 |
| 56 | + elif "PS5 Controller" in controller_name or \ |
| 57 | + "DualSense" in controller_name: |
| 58 | + return ControllerSettings.Devices.PS5 |
| 59 | + elif "Stadia Controller" in controller_name: |
| 60 | + return ControllerSettings.Devices.STADIA |
| 61 | + elif "Steam Controller" in controller_name: |
| 62 | + return ControllerSettings.Devices.STEAM |
| 63 | + elif "Switch Controller" in controller_name or \ |
| 64 | + "Switch Pro Controller" in controller_name: |
| 65 | + return ControllerSettings.Devices.SWITCH |
| 66 | + elif "Joy-Con" in controller_name: |
| 67 | + return ControllerSettings.Devices.JOYCON |
| 68 | + elif "Xbox 360 Controller" in controller_name: |
| 69 | + return ControllerSettings.Devices.XBOX360 |
| 70 | + elif "Xbox One" in controller_name or \ |
| 71 | + "X-Box One" in controller_name or \ |
| 72 | + "Xbox Wireless Controller" in controller_name: |
| 73 | + return ControllerSettings.Devices.XBOXONE |
| 74 | + elif "Xbox Series" in controller_name: |
| 75 | + return ControllerSettings.Devices.XBOXSERIES |
| 76 | + elif "Steam Deck" in controller_name or \ |
| 77 | + "Steam Virtual Gamepad" in controller_name: |
| 78 | + return ControllerSettings.Devices.STEAM_DECK |
| 79 | + elif "OUYA Controller" in controller_name: |
| 80 | + return ControllerSettings.Devices.OUYA |
| 81 | + else: |
| 82 | + return fallback |
| 83 | + |
| 84 | +func _convert_joypad_to_luna(path: String): |
| 85 | + path = path.replace("joypad", "luna") |
| 86 | + match path.substr(path.find("/") + 1): |
| 87 | + "select": |
| 88 | + return path.replace("/select", "/circle") |
| 89 | + "start": |
| 90 | + return path.replace("/start", "/menu") |
| 91 | + "share": |
| 92 | + return path.replace("/share", "/microphone") |
| 93 | + _: |
| 94 | + return path |
| 95 | + |
| 96 | +func _convert_joypad_to_playstation(path: String): |
| 97 | + match path.substr(path.find("/") + 1): |
| 98 | + "a": |
| 99 | + return path.replace("/a", "/cross") |
| 100 | + "b": |
| 101 | + return path.replace("/b", "/circle") |
| 102 | + "x": |
| 103 | + return path.replace("/x", "/square") |
| 104 | + "y": |
| 105 | + return path.replace("/y", "/triangle") |
| 106 | + "lb": |
| 107 | + return path.replace("/lb", "/l1") |
| 108 | + "rb": |
| 109 | + return path.replace("/rb", "/r1") |
| 110 | + "lt": |
| 111 | + return path.replace("/lt", "/l2") |
| 112 | + "rt": |
| 113 | + return path.replace("/rt", "/r2") |
| 114 | + _: |
| 115 | + return path |
| 116 | + |
| 117 | +func _convert_joypad_to_ps3(path: String): |
| 118 | + return _convert_joypad_to_playstation(path.replace("joypad", "ps3")) |
| 119 | + |
| 120 | +func _convert_joypad_to_ps4(path: String): |
| 121 | + path = _convert_joypad_to_playstation(path.replace("joypad", "ps4")) |
| 122 | + match path.substr(path.find("/") + 1): |
| 123 | + "select": |
| 124 | + return path.replace("/select", "/share") |
| 125 | + "start": |
| 126 | + return path.replace("/start", "/options") |
| 127 | + "share": |
| 128 | + return path.replace("/share", "/") |
| 129 | + _: |
| 130 | + return path |
| 131 | + |
| 132 | +func _convert_joypad_to_ps5(path: String): |
| 133 | + path = _convert_joypad_to_playstation(path.replace("joypad", "ps5")) |
| 134 | + match path.substr(path.find("/") + 1): |
| 135 | + "select": |
| 136 | + return path.replace("/select", "/share") |
| 137 | + "start": |
| 138 | + return path.replace("/start", "/options") |
| 139 | + "home": |
| 140 | + return path.replace("/home", "/assistant") |
| 141 | + "share": |
| 142 | + return path.replace("/share", "/microphone") |
| 143 | + _: |
| 144 | + return path |
| 145 | + |
| 146 | +func _convert_joypad_to_stadia(path: String): |
| 147 | + path = path.replace("joypad", "stadia") |
| 148 | + match path.substr(path.find("/") + 1): |
| 149 | + "lb": |
| 150 | + return path.replace("/lb", "/l1") |
| 151 | + "rb": |
| 152 | + return path.replace("/rb", "/r1") |
| 153 | + "lt": |
| 154 | + return path.replace("/lt", "/l2") |
| 155 | + "rt": |
| 156 | + return path.replace("/rt", "/r2") |
| 157 | + "select": |
| 158 | + return path.replace("/select", "/dots") |
| 159 | + "start": |
| 160 | + return path.replace("/start", "/menu") |
| 161 | + "share": |
| 162 | + return path.replace("/share", "/select") |
| 163 | + _: |
| 164 | + return path |
| 165 | + |
| 166 | +func _convert_joypad_to_steam(path: String): |
| 167 | + path = path.replace("joypad", "steam") |
| 168 | + match path.substr(path.find("/") + 1): |
| 169 | + "r_stick_click": |
| 170 | + return path.replace("/r_stick_click", "/right_track_center") |
| 171 | + "select": |
| 172 | + return path.replace("/select", "/back") |
| 173 | + "home": |
| 174 | + return path.replace("/home", "/system") |
| 175 | + "dpad": |
| 176 | + return path.replace("/dpad", "/left_track") |
| 177 | + "dpad_up": |
| 178 | + return path.replace("/dpad_up", "/left_track_up") |
| 179 | + "dpad_down": |
| 180 | + return path.replace("/dpad_down", "/left_track_down") |
| 181 | + "dpad_left": |
| 182 | + return path.replace("/dpad_left", "/left_track_left") |
| 183 | + "dpad_right": |
| 184 | + return path.replace("/dpad_right", "/left_track_right") |
| 185 | + "l_stick": |
| 186 | + return path.replace("/l_stick", "/stick") |
| 187 | + "r_stick": |
| 188 | + return path.replace("/r_stick", "/right_track") |
| 189 | + _: |
| 190 | + return path |
| 191 | + |
| 192 | +func _convert_joypad_to_switch(path: String): |
| 193 | + path = path.replace("joypad", "switch") |
| 194 | + match path.substr(path.find("/") + 1): |
| 195 | + "a": |
| 196 | + return path.replace("/a", "/b") |
| 197 | + "b": |
| 198 | + return path.replace("/b", "/a") |
| 199 | + "x": |
| 200 | + return path.replace("/x", "/y") |
| 201 | + "y": |
| 202 | + return path.replace("/y", "/x") |
| 203 | + "lb": |
| 204 | + return path.replace("/lb", "/l") |
| 205 | + "rb": |
| 206 | + return path.replace("/rb", "/r") |
| 207 | + "lt": |
| 208 | + return path.replace("/lt", "/zl") |
| 209 | + "rt": |
| 210 | + return path.replace("/rt", "/zr") |
| 211 | + "select": |
| 212 | + return path.replace("/select", "/minus") |
| 213 | + "start": |
| 214 | + return path.replace("/start", "/plus") |
| 215 | + "share": |
| 216 | + return path.replace("/share", "/square") |
| 217 | + _: |
| 218 | + return path |
| 219 | + |
| 220 | +func _convert_joypad_to_joycon(path: String): |
| 221 | + path = _convert_joypad_to_switch(path) |
| 222 | + match path.substr(path.find("/") + 1): |
| 223 | + "dpad_up": |
| 224 | + return path.replace("/dpad_up", "/up") |
| 225 | + "dpad_down": |
| 226 | + return path.replace("/dpad_down", "/down") |
| 227 | + "dpad_left": |
| 228 | + return path.replace("/dpad_left", "/left") |
| 229 | + "dpad_right": |
| 230 | + return path.replace("/dpad_right", "/right") |
| 231 | + _: |
| 232 | + return path |
| 233 | + |
| 234 | +func _convert_joypad_to_xbox360(path: String): |
| 235 | + path = path.replace("joypad", "xbox360") |
| 236 | + match path.substr(path.find("/") + 1): |
| 237 | + "select": |
| 238 | + return path.replace("/select", "/back") |
| 239 | + _: |
| 240 | + return path |
| 241 | + |
| 242 | +func _convert_joypad_to_xbox_modern(path: String): |
| 243 | + match path.substr(path.find("/") + 1): |
| 244 | + "select": |
| 245 | + return path.replace("/select", "/view") |
| 246 | + "start": |
| 247 | + return path.replace("/start", "/menu") |
| 248 | + _: |
| 249 | + return path |
| 250 | + |
| 251 | +func _convert_joypad_to_xboxone(path: String): |
| 252 | + return _convert_joypad_to_xbox_modern(path.replace("joypad", "xboxone")) |
| 253 | + |
| 254 | +func _convert_joypad_to_xboxseries(path: String): |
| 255 | + return _convert_joypad_to_xbox_modern(path.replace("joypad", "xboxseries")) |
| 256 | + |
| 257 | +func _convert_joypad_to_steamdeck(path: String): |
| 258 | + path = path.replace("joypad", "steamdeck") |
| 259 | + match path.substr(path.find("/") + 1): |
| 260 | + "lb": |
| 261 | + return path.replace("/lb", "/l1") |
| 262 | + "rb": |
| 263 | + return path.replace("/rb", "/r1") |
| 264 | + "lt": |
| 265 | + return path.replace("/lt", "/l2") |
| 266 | + "rt": |
| 267 | + return path.replace("/rt", "/r2") |
| 268 | + "select": |
| 269 | + return path.replace("/select", "/inventory") |
| 270 | + "start": |
| 271 | + return path.replace("/start", "/menu") |
| 272 | + "home": |
| 273 | + return path.replace("/home", "/steam") |
| 274 | + "share": |
| 275 | + return path.replace("/share", "/dots") |
| 276 | + _: |
| 277 | + return path |
| 278 | + |
| 279 | +func _convert_joypad_to_ouya(path: String): |
| 280 | + path = path.replace("joypad", "ouya") |
| 281 | + match path.substr(path.find("/") + 1): |
| 282 | + "a": |
| 283 | + return path.replace("/a", "/o") |
| 284 | + "x": |
| 285 | + return path.replace("/x", "/u") |
| 286 | + "b": |
| 287 | + return path.replace("/b", "/a") |
| 288 | + "lb": |
| 289 | + return path.replace("/lb", "/l1") |
| 290 | + "rb": |
| 291 | + return path.replace("/rb", "/r1") |
| 292 | + "lt": |
| 293 | + return path.replace("/lt", "/l2") |
| 294 | + "rt": |
| 295 | + return path.replace("/rt", "/r2") |
| 296 | + "start": |
| 297 | + return path.replace("/start", "/menu") |
| 298 | + "share": |
| 299 | + return path.replace("/share", "/microphone") |
| 300 | + _: |
| 301 | + return path |
0 commit comments