Skip to content

Instantly share code, notes, and snippets.

@renebigot
Created June 29, 2016 21:23
Show Gist options
  • Save renebigot/1d64c043bf11b2077899b8b3f61b40f2 to your computer and use it in GitHub Desktop.
Save renebigot/1d64c043bf11b2077899b8b3f61b40f2 to your computer and use it in GitHub Desktop.
// Found at : https://forums.developer.apple.com/thread/25440
//
// GameViewController.swift
// testgc
//
// Created by John McManus on 06/11/2015.
// Copyright (c) 2015 AppyAppster Limited. All rights reserved.
//
import UIKit
import SpriteKit
import GameController
class GameViewController: UIViewController {
var controllerList : [GCController]!
var gameController : GCController!
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed: "GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/ Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/ Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
// Go and Get Contoller -- this assume only Siri Remote is connected
getControllers()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
func getControllers(){
controllerList = GCController.controllers()
if controllerList.count < 1 {
print("no controller try and attach to them")
attachControllers()
}
}
func attachControllers(){
print("\(__FUNCTION__)")
registerForGameControllerNotifications() // Setup notification when Controller is found
GCController.startWirelessControllerDiscoveryWithCompletionHandler({}) //Basic call to get game controllers
}
func registerForGameControllerNotifications() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleControllerDidConnectNotification:", name: GCControllerDidConnectNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleControllerDidDisconnectNotification:", name: GCControllerDidDisconnectNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: GCControllerDidConnectNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: GCControllerDidDisconnectNotification, object: nil)
}
@objc func handleControllerDidConnectNotification(notification: NSNotification) {
print("\(__FUNCTION__)")
// assign the gameController which is found - will break if more than 1
gameController = notification.object as! GCController
// we have a controller so go and setup handlers
self.setupGCEvents()
}
@objc func handleControllerDidDisconnectNotification(notification: NSNotification) {
// if a controller disconnects we should see it
print("\(__FUNCTION__)")
}
func setupGCEvents(){
//if it is a siri remote
if let microGamepad = self.gameController.microGamepad {
print("microGamepad found")
registermicroGamepadEvents(microGamepad)
}
}
func registermicroGamepadEvents(microGamepad :GCMicroGamepad){
print("\(__FUNCTION__)")
//setup the handlers
gameController.controllerPausedHandler = { [unowned self] _ in
self.pauseGame()
}
let buttonHandler: GCControllerButtonValueChangedHandler = { button, _, pressed in
print("buttonHandler")
}
let movementHandler: GCControllerDirectionPadValueChangedHandler = { _, xValue, yValue in
let displacement = float2(x: xValue, y: yValue)
print("displacement:\(displacement)")
}
let motionHandler :GCMotionValueChangedHandler = {(motion: GCMotion)->() in
print("acc:\(motion.userAcceleration)")
print("grav:\(motion.gravity)")
print("att:\(motion.attitude)") // not currently support on tvOS
print("rot:\(motion.rotationRate)") // not currently support on tvOS
}
microGamepad.buttonA.pressedChangedHandler = buttonHandler
microGamepad.buttonX.pressedChangedHandler = buttonHandler
microGamepad.allowsRotation = true
microGamepad.dpad.valueChangedHandler = movementHandler
// ignored error checking, but for example
gameController.motion?.valueChangedHandler = motionHandler
}
func pauseGame(){
print("\(__FUNCTION__)")
//Assigned to menu button
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment