-
-
Save Ichicoro/65eebe3644c327a033091fa1168fc6d5 to your computer and use it in GitHub Desktop.
Controller support for DolphiniOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Source/iOS/DolphiniOS/DolphiniOS/UI/Emulation/EmulationVIewController.swift b/Source/iOS/DolphiniOS/DolphiniOS/UI/Emulation/EmulationVIewController.swift | |
index 4a792da3e8f2201675a35f97e0849b95ec608d84..13a50b29f8360e9ba20e75e03769778635006fec 100644 | |
--- a/Source/iOS/DolphiniOS/DolphiniOS/UI/Emulation/EmulationVIewController.swift | |
+++ b/Source/iOS/DolphiniOS/DolphiniOS/UI/Emulation/EmulationVIewController.swift | |
@@ -5,11 +5,13 @@ | |
import Foundation | |
import MetalKit | |
import UIKit | |
+import GameController | |
class EmulationViewController: UIViewController | |
{ | |
var softwareFile: String = "" | |
var videoBackend: String = "" | |
+ var padView: UIView? | |
@objc init(file: String, backend: String) | |
{ | |
@@ -37,25 +39,107 @@ class EmulationViewController: UIViewController | |
self.view = MTKView(frame: UIScreen.main.bounds) | |
} | |
- let padView = Bundle(for: type(of: self)).loadNibNamed("TCGameCubePad", owner: self, options: nil)![0] as! UIView | |
- padView.frame = UIScreen.main.bounds | |
- self.view.addSubview(padView) | |
+ self.padView = Bundle(for: type(of: self)).loadNibNamed("TCGameCubePad", owner: self, options: nil)![0] as? UIView | |
+ self.padView!.frame = UIScreen.main.bounds | |
+ self.view.addSubview(self.padView!) | |
} | |
override func viewDidLoad() | |
{ | |
- let queue = DispatchQueue(label: "org.dolphin-emu.ios.emulation-queue") | |
+ let queue = DispatchQueue(label: "dev.marte.ios.emulation-queue") | |
let view = self.view | |
queue.async | |
{ | |
MainiOS.startEmulation(withFile: self.softwareFile, view: view) | |
} | |
+ ObserveForGameControllers() | |
+ connectControllers() | |
} | |
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) | |
{ | |
MainiOS.windowResized() | |
} | |
+ | |
+ | |
+ // Function to run intially to lookout for any MFI or Remote Controllers in the area | |
+ func ObserveForGameControllers() { | |
+ NotificationCenter.default.addObserver(self, selector: #selector(connectControllers), name: NSNotification.Name.GCControllerDidConnect, object: nil) | |
+ NotificationCenter.default.addObserver(self, selector: #selector(disconnectControllers), name: NSNotification.Name.GCControllerDidDisconnect, object: nil) | |
+ } | |
+ | |
+ // This Function is called when a controller is connected to the Apple TV | |
+ @objc func connectControllers() { | |
+ //Used to register the Nimbus Controllers to a specific Player Number | |
+ var indexNumber = 0 | |
+ // Run through each controller currently connected to the system | |
+ self.padView?.alpha = 1 | |
+ for controller in GCController.controllers() { | |
+ //Check to see whether it is an extended Game Controller (Such as a Nimbus) | |
+ if controller.extendedGamepad != nil { | |
+ self.padView?.alpha = 0 | |
+ controller.playerIndex = GCControllerPlayerIndex.init(rawValue: indexNumber)! | |
+ indexNumber += 1 | |
+ setupControllerControls(controller: controller) | |
+ } | |
+ } | |
+ } | |
+ | |
+ @objc func disconnectControllers() { | |
+ self.padView?.alpha = 1 | |
+ } | |
+ | |
+ func setupControllerControls(controller: GCController) { | |
+ //Function that check the controller when anything is moved or pressed on it | |
+ controller.extendedGamepad?.valueChangedHandler = { | |
+ (gamepad: GCExtendedGamepad, element: GCControllerElement) in | |
+ // Add movement in here for sprites of the controllers | |
+ self.controllerInputDetected(gamepad: gamepad, element: element, index: controller.playerIndex.rawValue) | |
+ } | |
+ } | |
+ | |
+ func controllerInputDetected(gamepad: GCExtendedGamepad, element: GCControllerElement, index: Int) { | |
+ if #available(iOS 13.0, *) { | |
+ switch element { | |
+ case gamepad.buttonA: | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_A.rawValue), action: Int32(gamepad.buttonA.isPressed ? 1 : 0)) | |
+ case gamepad.buttonB: | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_X.rawValue), action: Int32(gamepad.buttonB.isPressed ? 1 : 0)) | |
+ case gamepad.buttonX: | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_B.rawValue), action: Int32(gamepad.buttonX.isPressed ? 1 : 0)) | |
+ case gamepad.buttonY: | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_Y.rawValue), action: Int32(gamepad.buttonY.isPressed ? 1 : 0)) | |
+ case gamepad.rightShoulder: | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_Z.rawValue), action: Int32(gamepad.rightShoulder.value == 0 ? 0 : 1)) | |
+ case gamepad.buttonMenu: | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_START.rawValue), action: Int32(gamepad.buttonMenu.value == 0 ? 0 : 1)) | |
+ case gamepad.leftThumbstick: | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_MAIN.rawValue+1), value: CGFloat(-gamepad.leftThumbstick.up.value)) | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_MAIN.rawValue+2), value: CGFloat(gamepad.leftThumbstick.down.value)) | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_MAIN.rawValue+3), value: CGFloat(-gamepad.leftThumbstick.left.value)) | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_MAIN.rawValue+4), value: CGFloat(gamepad.leftThumbstick.right.value)) | |
+ case gamepad.rightThumbstick: | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_C.rawValue+1), value: CGFloat(-gamepad.rightThumbstick.up.value)) | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_C.rawValue+2), value: CGFloat(gamepad.rightThumbstick.down.value)) | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_C.rawValue+3), value: CGFloat(-gamepad.rightThumbstick.left.value)) | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.STICK_C.rawValue+4), value: CGFloat(gamepad.rightThumbstick.right.value)) | |
+ case gamepad.dpad: | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_UP.rawValue), action: gamepad.dpad.up.isPressed ? 1 : 0) | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_DOWN.rawValue), action: gamepad.dpad.down.isPressed ? 1 : 0) | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_LEFT.rawValue), action: gamepad.dpad.left.isPressed ? 1 : 0) | |
+ MainiOS.gamepadEvent(forButton: Int32(TCButtonType.BUTTON_RIGHT.rawValue), action: gamepad.dpad.right.isPressed ? 1 : 0) | |
+ case gamepad.leftTrigger: | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.TRIGGER_L.rawValue), value: CGFloat(gamepad.leftTrigger.value)) | |
+ case gamepad.rightTrigger: | |
+ MainiOS.gamepadMoveEvent(forAxis: Int32(TCButtonType.TRIGGER_R.rawValue), value: CGFloat(gamepad.rightTrigger.value)) | |
+ default: | |
+ NSLog("oeuf") | |
+ } | |
+ } else { | |
+ // Fallback on earlier versions | |
+ } | |
+ //MainiOS.gamepadEvent(forButton: Int32(controllerButton), action: 1) | |
+ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment