Skip to content

Commit

Permalink
Custom logger support
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWDG authored Sep 2, 2024
1 parent b64561e commit 32b18fd
Showing 1 changed file with 35 additions and 43 deletions.
78 changes: 35 additions & 43 deletions Sources/GameControllerKit/GameControllerKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Check failure on line 104 in Sources/GameControllerKit/GameControllerKit.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 104 in Sources/GameControllerKit/GameControllerKit.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Lines should not have trailing whitespace (trailing_whitespace)
/// Set color of the controllers light
///
/// Use the light settings to signal the user or to create a more immersive experience.
Expand All @@ -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)) {
Expand All @@ -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 {
Expand All @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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.

Check failure on line 234 in Sources/GameControllerKit/GameControllerKit.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Line should be 120 characters or less; currently it has 130 characters (line_length)

Check failure on line 234 in Sources/GameControllerKit/GameControllerKit.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Line should be 120 characters or less; currently it has 130 characters (line_length)
///
/// - Parameter controller: Controller
func setupController(controller: GCController) {
// swiftlint:disable:previous function_body_length
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 32b18fd

Please sign in to comment.