From 32b18fdc14df842337f0a07fc9a1ea69b324b26d Mon Sep 17 00:00:00 2001 From: Wesley de Groot Date: Mon, 2 Sep 2024 14:32:04 +0200 Subject: [PATCH 1/3] Custom logger support --- .../GameControllerKit/GameControllerKit.swift | 78 +++++++++---------- 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/Sources/GameControllerKit/GameControllerKit.swift b/Sources/GameControllerKit/GameControllerKit.swift index d60f4e59..f7dc1e67 100644 --- a/Sources/GameControllerKit/GameControllerKit.swift +++ b/Sources/GameControllerKit/GameControllerKit.swift @@ -56,14 +56,16 @@ public class GameControllerKit: ObservableObject { private var eventHandler: GCKEventHandler? /// Game Controller Kit logger. - private let logger = Logger( + private var logger = Logger( subsystem: "nl.wesleydegroot.GameControllerKit", category: "GameControllerKit" ) /// Initializes a new GameControllerKit instance. /// It sets up notification observers for when game controllers connect or disconnect. - public init() { + /// + /// - Parameter logger: Custom ``Logger`` instance. + public init(_ logger: Logger? = nil) { NotificationCenter.default.addObserver( forName: .GCControllerDidConnect, object: nil, @@ -85,8 +87,21 @@ public class GameControllerKit: ObservableObject { self?.logger.info("\(String(describing: message))") } + + if let logger = logger { + self.logger = logger + } } + /// Set the logger + /// + /// Use a custom ``Logger`` instance if you want to have custom logging. + /// + /// - Parameter color: Color + public func set(logger: Logger) { + self.logger = logger + } + /// Set color of the controllers light /// /// Use the light settings to signal the user or to create a more immersive experience. @@ -98,12 +113,16 @@ public class GameControllerKit: ObservableObject { } /// Set the event handler + /// + /// This function allows you to setup a custom event handler, which you need to receive inputs from the controller. + /// /// - Parameter handler: event handler public func set(handler: @escaping GCKEventHandler) { self.eventHandler = handler } - /// Plays random colors on your controller, if supported + /// Plays random colors on your controller (if supported) + /// This is currently only supported on a DualSense and DualShock controller (Playstation) public func rainbow() { for counter in 0...10 { DispatchQueue.main.asyncAfter(deadline: .now() + (Double(counter)/0.99)) { @@ -114,6 +133,8 @@ public class GameControllerKit: ObservableObject { /// Play haptics /// + /// This plays haptics (vibrations) on the gamecontroller. + /// /// - Parameter url: Haptics file public func playHaptics(url: URL) { guard let haptics = self.controller?.haptics?.createEngine(withLocality: .default) else { @@ -133,47 +154,11 @@ public class GameControllerKit: ObservableObject { } } - /// Translate ``GCKAction`` to ``GCKMovePosition`` - /// - /// - Parameter action: ``GCKAction`` - /// - Returns: ``GCKMovePosition`` - @available(*, deprecated, message: "Use .position on the action directly") - public func translate(action: GCKAction) -> GCKMovePosition { - // swiftlint:disable:previous cyclomatic_complexity - var position: GCKMovePosition = .unknown - - switch action { - case .leftThumbstick(let xPos, let yPos), .rightThumbstick(let xPos, let yPos): - if yPos == 1.0 { - position = .up - } else if xPos > 0 && xPos < 1 && yPos > 0 && yPos < 1 { - position = .upRight - } else if xPos == 1.0 { - position = .right - } else if xPos > 0 && xPos < 1 && yPos < 0 && yPos > -1 { - position = .downRight - } else if yPos == -1.0 { - position = .down - } else if xPos < 0 && xPos > -1 && yPos < 0 && yPos > -1 { - position = .downLeft - } else if xPos == -1.0 { - position = .left - } else if xPos < 0 && xPos > -1 && yPos > 0 && yPos < 1 { - position = .upLeft - } else if xPos == 0 && yPos == 0 { - position = .centered - } else { - position = .unknown - } - - default: - position = GCKMovePosition.unknown - } - - return position - } - // MARK: - Connect/Disconnect functions + /// Controller did connect + /// + /// This function handles the connection of a controller. + /// If it is the first controller it will set to the primary controller @objc private func controllerDidConnect(_ notification: Notification) { controllers = GCController.controllers() @@ -220,6 +205,9 @@ public class GameControllerKit: ObservableObject { } } + /// Controller did disconnect + /// + /// This function handles the disconnection of a controller. @objc private func controllerDidDisconnect(_ notification: Notification) { controllers = GCController.controllers() @@ -243,6 +231,8 @@ public class GameControllerKit: ObservableObject { /// Set up controller /// + /// This function sets up the controller, it looks which type it is and then map the elements to the corresponding responders. + /// /// - Parameter controller: Controller func setupController(controller: GCController) { // swiftlint:disable:previous function_body_length @@ -339,6 +329,8 @@ public class GameControllerKit: ObservableObject { extension GCColor { /// Random color + /// + /// - Returns: A random color. public static var GCKRandom: GCColor { return GCColor( red: .random(in: 0...1), From 1d730c7c0fab870c0da753b5f52b85187fbf9ced Mon Sep 17 00:00:00 2001 From: Wesley de Groot Date: Mon, 2 Sep 2024 14:35:44 +0200 Subject: [PATCH 2/3] Fix linting errors --- Sources/GameControllerKit/GameControllerKit.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/GameControllerKit/GameControllerKit.swift b/Sources/GameControllerKit/GameControllerKit.swift index f7dc1e67..284b2ee6 100644 --- a/Sources/GameControllerKit/GameControllerKit.swift +++ b/Sources/GameControllerKit/GameControllerKit.swift @@ -101,7 +101,7 @@ public class GameControllerKit: ObservableObject { public func set(logger: Logger) { self.logger = logger } - + /// Set color of the controllers light /// /// Use the light settings to signal the user or to create a more immersive experience. @@ -114,7 +114,8 @@ public class GameControllerKit: ObservableObject { /// Set the event handler /// - /// This function allows you to setup a custom event handler, which you need to receive inputs from the controller. + /// This function allows you to setup a custom event handler, + /// which you need to receive inputs from the controller. /// /// - Parameter handler: event handler public func set(handler: @escaping GCKEventHandler) { From 915bd24a98c9f27ef487b37456e16df7a60da279 Mon Sep 17 00:00:00 2001 From: Wesley de Groot Date: Mon, 2 Sep 2024 15:08:06 +0200 Subject: [PATCH 3/3] Fix linting errors --- Sources/GameControllerKit/GameControllerKit.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/GameControllerKit/GameControllerKit.swift b/Sources/GameControllerKit/GameControllerKit.swift index 284b2ee6..0572c7b7 100644 --- a/Sources/GameControllerKit/GameControllerKit.swift +++ b/Sources/GameControllerKit/GameControllerKit.swift @@ -232,7 +232,8 @@ public class GameControllerKit: ObservableObject { /// Set up controller /// - /// This function sets up the controller, it looks which type it is and then map the elements to the corresponding responders. + /// This function sets up the controller, + /// it looks which type it is and then map the elements to the corresponding responders. /// /// - Parameter controller: Controller func setupController(controller: GCController) {