import GameController
import Foundation
#if os(tvOS)
public var theGamePad : GCController?
public var theSiriRemote : GCController?
fileprivate var theControllerID : Int = -1 // 0=gamePad 1=Siri -1=none
public func public_connectControllers() {
var indexNumber = 0
for controller in GCController.controllers() {
if controller.extendedGamepad != nil && indexNumber==0 { // only support ONE controller for now
theGamePad=controller
controller.playerIndex = GCControllerPlayerIndex.init(rawValue: indexNumber)!
indexNumber += 1
}
if controller.microGamepad != nil { theSiriRemote=controller }
}
checkForControllerChange()
}
public func public_disconnectControllers() {
// which controllers remain? (if any)
for controller in GCController.controllers() {
if controller.extendedGamepad == nil { theGamePad = nil }
if controller.microGamepad == nil { theSiriRemote = nil }
}
checkForControllerChange()
}
private func checkForControllerChange() {
var newID : Int = -1
if theSiriRemote != nil { newID=1 }
if theGamePad != nil { newID=0 }
//
if newID != theControllerID && newID>=0 { // controller type HAS changed
print("Changed from \(theControllerID) to \(newID)")
}
if newID<0 {
alert_NO_GAMEPAD()
} else {
theControllerID = newID
theMsgBox.dismiss()
}
}