Skip to content

Commit de8a38f

Browse files
authored
Merge pull request #2 from 0xWDG/custom-logger
Custom logger support
2 parents b64561e + 915bd24 commit de8a38f

File tree

1 file changed

+37
-43
lines changed

1 file changed

+37
-43
lines changed

Sources/GameControllerKit/GameControllerKit.swift

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ public class GameControllerKit: ObservableObject {
5656
private var eventHandler: GCKEventHandler?
5757

5858
/// Game Controller Kit logger.
59-
private let logger = Logger(
59+
private var logger = Logger(
6060
subsystem: "nl.wesleydegroot.GameControllerKit",
6161
category: "GameControllerKit"
6262
)
6363

6464
/// Initializes a new GameControllerKit instance.
6565
/// It sets up notification observers for when game controllers connect or disconnect.
66-
public init() {
66+
///
67+
/// - Parameter logger: Custom ``Logger`` instance.
68+
public init(_ logger: Logger? = nil) {
6769
NotificationCenter.default.addObserver(
6870
forName: .GCControllerDidConnect,
6971
object: nil,
@@ -85,6 +87,19 @@ public class GameControllerKit: ObservableObject {
8587

8688
self?.logger.info("\(String(describing: message))")
8789
}
90+
91+
if let logger = logger {
92+
self.logger = logger
93+
}
94+
}
95+
96+
/// Set the logger
97+
///
98+
/// Use a custom ``Logger`` instance if you want to have custom logging.
99+
///
100+
/// - Parameter color: Color
101+
public func set(logger: Logger) {
102+
self.logger = logger
88103
}
89104

90105
/// Set color of the controllers light
@@ -98,12 +113,17 @@ public class GameControllerKit: ObservableObject {
98113
}
99114

100115
/// Set the event handler
116+
///
117+
/// This function allows you to setup a custom event handler,
118+
/// which you need to receive inputs from the controller.
119+
///
101120
/// - Parameter handler: event handler
102121
public func set(handler: @escaping GCKEventHandler) {
103122
self.eventHandler = handler
104123
}
105124

106-
/// Plays random colors on your controller, if supported
125+
/// Plays random colors on your controller (if supported)
126+
/// This is currently only supported on a DualSense and DualShock controller (Playstation)
107127
public func rainbow() {
108128
for counter in 0...10 {
109129
DispatchQueue.main.asyncAfter(deadline: .now() + (Double(counter)/0.99)) {
@@ -114,6 +134,8 @@ public class GameControllerKit: ObservableObject {
114134

115135
/// Play haptics
116136
///
137+
/// This plays haptics (vibrations) on the gamecontroller.
138+
///
117139
/// - Parameter url: Haptics file
118140
public func playHaptics(url: URL) {
119141
guard let haptics = self.controller?.haptics?.createEngine(withLocality: .default) else {
@@ -133,47 +155,11 @@ public class GameControllerKit: ObservableObject {
133155
}
134156
}
135157

136-
/// Translate ``GCKAction`` to ``GCKMovePosition``
137-
///
138-
/// - Parameter action: ``GCKAction``
139-
/// - Returns: ``GCKMovePosition``
140-
@available(*, deprecated, message: "Use .position on the action directly")
141-
public func translate(action: GCKAction) -> GCKMovePosition {
142-
// swiftlint:disable:previous cyclomatic_complexity
143-
var position: GCKMovePosition = .unknown
144-
145-
switch action {
146-
case .leftThumbstick(let xPos, let yPos), .rightThumbstick(let xPos, let yPos):
147-
if yPos == 1.0 {
148-
position = .up
149-
} else if xPos > 0 && xPos < 1 && yPos > 0 && yPos < 1 {
150-
position = .upRight
151-
} else if xPos == 1.0 {
152-
position = .right
153-
} else if xPos > 0 && xPos < 1 && yPos < 0 && yPos > -1 {
154-
position = .downRight
155-
} else if yPos == -1.0 {
156-
position = .down
157-
} else if xPos < 0 && xPos > -1 && yPos < 0 && yPos > -1 {
158-
position = .downLeft
159-
} else if xPos == -1.0 {
160-
position = .left
161-
} else if xPos < 0 && xPos > -1 && yPos > 0 && yPos < 1 {
162-
position = .upLeft
163-
} else if xPos == 0 && yPos == 0 {
164-
position = .centered
165-
} else {
166-
position = .unknown
167-
}
168-
169-
default:
170-
position = GCKMovePosition.unknown
171-
}
172-
173-
return position
174-
}
175-
176158
// MARK: - Connect/Disconnect functions
159+
/// Controller did connect
160+
///
161+
/// This function handles the connection of a controller.
162+
/// If it is the first controller it will set to the primary controller
177163
@objc private func controllerDidConnect(_ notification: Notification) {
178164
controllers = GCController.controllers()
179165

@@ -220,6 +206,9 @@ public class GameControllerKit: ObservableObject {
220206
}
221207
}
222208

209+
/// Controller did disconnect
210+
///
211+
/// This function handles the disconnection of a controller.
223212
@objc private func controllerDidDisconnect(_ notification: Notification) {
224213
controllers = GCController.controllers()
225214

@@ -243,6 +232,9 @@ public class GameControllerKit: ObservableObject {
243232

244233
/// Set up controller
245234
///
235+
/// This function sets up the controller,
236+
/// it looks which type it is and then map the elements to the corresponding responders.
237+
///
246238
/// - Parameter controller: Controller
247239
func setupController(controller: GCController) {
248240
// swiftlint:disable:previous function_body_length
@@ -339,6 +331,8 @@ public class GameControllerKit: ObservableObject {
339331

340332
extension GCColor {
341333
/// Random color
334+
///
335+
/// - Returns: A random color.
342336
public static var GCKRandom: GCColor {
343337
return GCColor(
344338
red: .random(in: 0...1),

0 commit comments

Comments
 (0)