Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding back in Xbox controller support #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions DOOM3-iOS/ControllerInputSystem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// ControllerInputSystem.swift
// DOOM3-iOS
//
// Created by Joan Westenberg on 8/11/24.
//


import GameController

class ControllerInputSystem {
static let shared = ControllerInputSystem()

var connectedController: GCController?
weak var delegate: JoystickDelegate?

private init() {
setupControllerConnectivityHandler()
}

func setupControllerConnectivityHandler() {
NotificationCenter.default.addObserver(self,
selector: #selector(controllerConnected),
name: .GCControllerDidConnect,
object: nil)

NotificationCenter.default.addObserver(self,
selector: #selector(controllerDisconnected),
name: .GCControllerDidDisconnect,
object: nil)
}

@objc func controllerConnected(_ notification: Notification) {
guard let controller = notification.object as? GCController else { return }
print("Controller connected: \(controller.productCategory)")
connectedController = controller
setupControllerInputHandling(controller)
}

@objc func controllerDisconnected(_ notification: Notification) {
guard let controller = notification.object as? GCController else { return }
print("Controller disconnected: \(controller.productCategory)")
if controller == connectedController {
connectedController = nil
}
}

func setupControllerInputHandling(_ controller: GCController) {
guard let gamepad = controller.extendedGamepad else { return }

gamepad.leftThumbstick.valueChangedHandler = { [weak self] (_, xValue, yValue) in
let angle = atan2(xValue, -yValue) * (180 / .pi)
let displacement = sqrt(xValue * xValue + yValue * yValue)
self?.delegate?.handleJoyStick(angle: CGFloat(angle), displacement: CGFloat(displacement))
self?.delegate?.handleJoyStickPosition(x: CGFloat(xValue), y: CGFloat(yValue))
}
}
}
57 changes: 32 additions & 25 deletions DOOM3-iOS/GameViewController.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
//
// GameViewController.swift
// DOOM3-iOS
//
// Created by Tom Kidd on 1/26/19.
//

import UIKit
import GameController

class GameViewController: UIViewController {
class GameViewController: UIViewController, JoystickDelegate {

var difficulty = -1
var newgame = false
Expand All @@ -16,18 +10,25 @@ class GameViewController: UIViewController {
@IBOutlet weak var loadingLabel: UILabel!

var selectedSavedGame = ""


// New properties for joystick and controller support
var joyStickView: JoyStickView!

override func viewDidLoad() {
super.viewDidLoad()

// Set up touch-based joystick
joyStickView = JoyStickView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
joyStickView.delegate = self
view.addSubview(joyStickView)

// Set up controller input system
ControllerInputSystem.shared.delegate = self

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { // Change `2.0` to the desired number of seconds.

var argv: [String?] = [ Bundle.main.resourcePath! + "/doom3"];

// argv.append("+map")
// argv.append("game/alphalabs2")
// argv.append("\(self.difficulty)")

#if os(tvOS)
let savesPath = try! FileManager().url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true).path
argv.append("+set")
Expand Down Expand Up @@ -78,10 +79,27 @@ class GameViewController: UIViewController {
Sys_Startup(argc, &cargs)

for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) }

}
}

// MARK: - JoystickDelegate methods

func handleJoyStick(angle: CGFloat, displacement: CGFloat) {
// Handle joystick input (works for both touch and controller)
print("Angle: \(angle), Displacement: \(displacement)")
// Add your game logic here to handle joystick input
// You might want to call a method in your game engine to move the player
// For example: movePlayer(angle: angle, displacement: displacement)
}

func handleJoyStickPosition(x: CGFloat, y: CGFloat) {
// Handle joystick position (works for both touch and controller)
print("X: \(x), Y: \(y)")
// Add your game logic here to handle joystick position
// You might want to call a method in your game engine to move the player
// For example: movePlayer(x: x, y: y)
}

#if os(iOS)

override var prefersHomeIndicatorAutoHidden: Bool {
Expand All @@ -92,15 +110,4 @@ class GameViewController: UIViewController {
return .bottom
}
#endif

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}