Skip to content

Commit 32b18fd

Browse files
authored
Custom logger support
1 parent b64561e commit 32b18fd

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

Sources/GameControllerKit/GameControllerKit.swift

Lines changed: 35 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,8 +87,21 @@ public class GameControllerKit: ObservableObject {
8587

8688
self?.logger.info("\(String(describing: message))")
8789
}
90+
91+
if let logger = logger {
92+
self.logger = logger
93+
}
8894
}
8995

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
103+
}
104+
90105
/// Set color of the controllers light
91106
///
92107
/// Use the light settings to signal the user or to create a more immersive experience.
@@ -98,12 +113,16 @@ public class GameControllerKit: ObservableObject {
98113
}
99114

100115
/// Set the event handler
116+
///
117+
/// This function allows you to setup a custom event handler, which you need to receive inputs from the controller.
118+
///
101119
/// - Parameter handler: event handler
102120
public func set(handler: @escaping GCKEventHandler) {
103121
self.eventHandler = handler
104122
}
105123

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

115134
/// Play haptics
116135
///
136+
/// This plays haptics (vibrations) on the gamecontroller.
137+
///
117138
/// - Parameter url: Haptics file
118139
public func playHaptics(url: URL) {
119140
guard let haptics = self.controller?.haptics?.createEngine(withLocality: .default) else {
@@ -133,47 +154,11 @@ public class GameControllerKit: ObservableObject {
133154
}
134155
}
135156

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-
176157
// MARK: - Connect/Disconnect functions
158+
/// Controller did connect
159+
///
160+
/// This function handles the connection of a controller.
161+
/// If it is the first controller it will set to the primary controller
177162
@objc private func controllerDidConnect(_ notification: Notification) {
178163
controllers = GCController.controllers()
179164

@@ -220,6 +205,9 @@ public class GameControllerKit: ObservableObject {
220205
}
221206
}
222207

208+
/// Controller did disconnect
209+
///
210+
/// This function handles the disconnection of a controller.
223211
@objc private func controllerDidDisconnect(_ notification: Notification) {
224212
controllers = GCController.controllers()
225213

@@ -243,6 +231,8 @@ public class GameControllerKit: ObservableObject {
243231

244232
/// Set up controller
245233
///
234+
/// This function sets up the controller, it looks which type it is and then map the elements to the corresponding responders.
235+
///
246236
/// - Parameter controller: Controller
247237
func setupController(controller: GCController) {
248238
// swiftlint:disable:previous function_body_length
@@ -339,6 +329,8 @@ public class GameControllerKit: ObservableObject {
339329

340330
extension GCColor {
341331
/// Random color
332+
///
333+
/// - Returns: A random color.
342334
public static var GCKRandom: GCColor {
343335
return GCColor(
344336
red: .random(in: 0...1),

0 commit comments

Comments
 (0)