Swiftpack.co - Package - snowfox124/JLGamePadPackage

JLGamePad

The story

In occasion I have to test on some Unity development and I need a controller to control the "game" on iPad, via bluetooth. So I decided to make a virtual controller on iOS, which is for simplier testing with both controlling the character, and for debugging the bluetooth connection.

This package is mainly for personal use (At least at this moment), so it may not be generic enough. And sorry that I didn't make any Test for the framework.

Supported version

The package is written in SwiftUI. It support iOS 13.0 as I am working on. It should be supported on MacOS, theoretically, sorry I didn't test it.

What is provided in this package

A simple DPad (direction) and BPad (A,B,X,Y) which is simulating a SFC controller (Sorry no start, select, L, R button).

How to use

Example on showing the UI (SwiftUI)

import JLGamePad

struct ContentView: View {
  var body: some View {
    HStack {
      DPad()  // The direction control
      Spacer()
      BPad()
    }
  }
}

Example listing to button event

import JLGamePad

struct ContentView: View {
  let dPadButtonNotification = NotificationCenter.default.publisher(for: DPadNotification.DPadButtonStatusChanged)
    .makeConnectable()
    .autoconnect()
  let bPadButtonNotification = NotificationCenter.default.publisher(for: BPadNotification.BPadButtonStatusChanged)
    .makeConnectable()
    .autoconnect()

  var body: some View {
    HStack {
      DPad()
        .onReceive(dPadButtonNotification) { (output) in
            guard let dPadButtonEvent = output.object as? DPadButtonEvent else { return }

            if dPadButtonEvent.state == .pressed {
              switch dPadButtonEvent.button {
              case .up:
                print("Up button pressed down")
              case .down:
                print("Down button pressed down")
              case .left:
                print("Left button pressed down")
              case .right:
                print("Right button pressed down")
              }
            }
        }
      Spacer()
      BPad().onReceive(bPadButtonNotification) { (output) in
            guard let bPadButtonEvent = output.object as? BPadButtonEvent else { return }

            if bPadButtonEvent.state == .pressed {
              switch bPadButtonEvent.button {
              case .x:
                print("X button pressed down")
              case .b:
                print("B button pressed down")
              case .y:
                print("Y button pressed down")
              case .a:
                print("A button pressed down")
              }
            }
        }
    }
  }
}

Button events

There are 2 button events (.pressed & .released). Check state of BPadButtonEvent to identify the current button state.

Adjusting the UI

To be honest I didn't make a lot of customizations for the framework as it is for personal use only

For DPad & BPad, you may modify the buttonSize: CGSize & panelSize: CGSize when init. Additionally, for BPad, you may also adjust the button offset: CGFloat from center.


DPad(buttonSize: CGSize(width: 60, height: 60), panelSize: CGSize(width: 180, height: 180))
BPad(buttonSize: CGSize(width: 80, height: 80), panelSize: CGSize(width: 180, height: 180), offset: 15)

Debuggin the button event

Both DPad & Bpad has the printButtonEvent: Bool property, set it to true to print the debug message when button event changed.


DPad(printButtonEvent: true)

// Example message printing in console:
// Action Touch Down: ▲
// Action Touch Up: ▲

Github

link
Stars: 0

Dependencies

Used By

Total: 0

Releases

0.0.1 - 2020-09-01 03:04:34

First version. A simple virtual game pad