|
| 1 | +# GameControllerKit |
| 2 | + |
| 3 | +GameControllerKit is a Swift package that makes it easy to work with game controllers on iOS, macOS, and tvOS. It provides a simple API to connect to game controllers, read input from them, and control their lights and haptics. |
| 4 | + |
| 5 | +[](https://swiftpackageindex.com/0xWDG/GameControllerKit) |
| 6 | +[](https://swiftpackageindex.com/0xWDG/GameControllerKit) |
| 7 | +[](https://swift.org/package-manager) |
| 8 | + |
| 9 | + |
| 10 | +## Requirements |
| 11 | + |
| 12 | +- Swift 5.9+ (Xcode 15+) |
| 13 | +- iOS 13+, macOS 10.15+, tvOS 16+ |
| 14 | + |
| 15 | +## Installation (Pakage.swift) |
| 16 | + |
| 17 | +```swift |
| 18 | +dependencies: [ |
| 19 | + .package(url: "https://github.com/0xWDG/GameControllerKit.git", branch: "main"), |
| 20 | +], |
| 21 | +targets: [ |
| 22 | + .target(name: "MyTarget", dependencies: [ |
| 23 | + .product(name: "GameControllerKit", package: "GameControllerKit"), |
| 24 | + ]), |
| 25 | +] |
| 26 | +``` |
| 27 | + |
| 28 | +## Installation (Xcode) |
| 29 | + |
| 30 | +1. In Xcode, open your project and navigate to **File** → **Swift Packages** → **Add Package Dependency...** |
| 31 | +2. Paste the repository URL (`https://github.com/0xWDG/GameControllerKit`) and click **Next**. |
| 32 | +3. Click **Finish**. |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +```swift |
| 37 | +import SwiftUI |
| 38 | +import GameControllerKit |
| 39 | + |
| 40 | + |
| 41 | +struct ContentView: View { |
| 42 | + @State var gameController = GameControllerKit() |
| 43 | + |
| 44 | + @State var log: [String] = [] |
| 45 | + |
| 46 | + func handler(action: GCKAction, pressed: Bool) { |
| 47 | + log.append( |
| 48 | + "\(String(describing: action)), \(pressed ? "Pressed" : "Unpressed")" |
| 49 | + ) |
| 50 | + |
| 51 | + if action == .buttonA && pressed { |
| 52 | + // When pressed on A or X (on PlayStation controllers), set the color to a random color. |
| 53 | + // Colors are only supported on controllers that have a light, like DualShock and Dualsense controllers. |
| 54 | + gameController.set(color: .GCKRandom) |
| 55 | + } |
| 56 | + |
| 57 | + if case .rightThumbstick = action { |
| 58 | + log.append("Right thumbstick: " + |
| 59 | + String( |
| 60 | + describing: gameController.translate( |
| 61 | + action: action |
| 62 | + ) |
| 63 | + ) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + if case .leftThumbstick = action { |
| 68 | + log.append("Left thumbstick: " + |
| 69 | + String( |
| 70 | + describing: gameController.translate( |
| 71 | + action: action |
| 72 | + ) |
| 73 | + ) |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + var body: some View { |
| 79 | + VStack { |
| 80 | + Button { |
| 81 | + gameController.set(color: .GCKRandom) |
| 82 | + } label: { |
| 83 | + Text("Random Color") |
| 84 | + } |
| 85 | + |
| 86 | + Text("Controller: \(gameController.controller?.productCategory ?? "None"), \((gameController.controllerType ?? .generic).description)") |
| 87 | + Text("Left: \(String(describing: gameController.leftThumbstick))") |
| 88 | + Text("Right: \(String(describing: gameController.rightThumbstick)).") |
| 89 | + Text("Last action: \(String(describing: gameController.lastAction)).") |
| 90 | + |
| 91 | + List { |
| 92 | + ForEach(log.reversed(), id: \.self) { text in |
| 93 | + Text(text) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + .padding() |
| 98 | + .onAppear { |
| 99 | + gameController.set(handler: handler) |
| 100 | + UIApplication.shared.isIdleTimerDisabled = true |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Contact |
| 107 | + |
| 108 | +We can get in touch via [Mastodon ](https://mastodon.social/@0xWDG), [Twitter/X ](https://twitter.com/0xWDG), [Discord ](https://discordapp.com/users/918438083861573692), [Email ](mailto:[email protected]), [Website ](https://wesleydegroot.nl). |
| 109 | + |
| 110 | +Interested learning more about Swift? [Check out my blog](https://wesleydegroot.nl/blog/). |
0 commit comments