GBA001/Delta/Emulation/EmulationViewController.swift
2015-09-26 14:04:00 -07:00

160 lines
4.8 KiB
Swift

//
// EmulationViewController.swift
// Delta
//
// Created by Riley Testut on 5/5/15.
// Copyright (c) 2015 Riley Testut. All rights reserved.
//
import UIKit
import DeltaCore
class EmulationViewController: UIViewController
{
//MARK: - Properties -
/** Properties **/
let game: Game
let emulatorCore: EmulatorCore
//MARK: - Private Properties
@IBOutlet private var controllerView: ControllerView!
@IBOutlet private var gameView: GameView!
@IBOutlet private var controllerViewHeightConstraint: NSLayoutConstraint!
//MARK: - Initializers -
/** Initializers **/
required init(game: Game)
{
self.game = game
self.emulatorCore = EmulatorCore(game: game)
super.init(nibName: "EmulationViewController", bundle: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("updateControllers"), name: ExternalControllerDidConnectNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("updateControllers"), name: ExternalControllerDidDisconnectNotification, object: nil)
}
required init(coder aDecoder: NSCoder) {
fatalError("initWithCoder: not implemented.")
}
//MARK: - Overrides
/** Overrides **/
//MARK: - UIViewController
/// UIViewController
override func viewDidLoad()
{
super.viewDidLoad()
self.gameView.backgroundColor = UIColor.clearColor()
self.emulatorCore.addGameView(self.gameView)
let controllerSkin = ControllerSkin.defaultControllerSkinForGameUTI(self.game.UTI)
self.controllerView.controllerSkin = controllerSkin
self.controllerView.addReceiver(self)
self.emulatorCore.setGameController(self.controllerView, atIndex: 0)
self.updateControllers()
}
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
self.emulatorCore.startEmulation()
}
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
if Settings.localControllerPlayerIndex != nil && self.controllerView.intrinsicContentSize() != CGSize(width: UIViewNoIntrinsicMetric, height: UIViewNoIntrinsicMetric)
{
let scale = self.view.bounds.width / self.controllerView.intrinsicContentSize().width
self.controllerViewHeightConstraint.constant = self.controllerView.intrinsicContentSize().height * scale
}
else
{
self.controllerViewHeightConstraint.constant = 0
}
}
override func prefersStatusBarHidden() -> Bool
{
return true
}
/// <UIContentContainer>
override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.willTransitionToTraitCollection(newCollection, withTransitionCoordinator: coordinator)
self.controllerView.beginAnimatingUpdateControllerSkin()
coordinator.animateAlongsideTransition(nil) { (context) in
self.controllerView.finishAnimatingUpdateControllerSkin()
}
}
//MARK: - Controllers -
/// Controllers
func updateControllers()
{
self.emulatorCore.removeAllGameControllers()
if let index = Settings.localControllerPlayerIndex
{
self.emulatorCore.setGameController(self.controllerView, atIndex: index)
}
for controller in ExternalControllerManager.sharedManager.connectedControllers
{
if let index = controller.playerIndex
{
self.emulatorCore.setGameController(controller, atIndex: index)
}
}
self.view.setNeedsLayout()
}
}
//MARK: - <GameControllerReceiver> -
/// <GameControllerReceiver>
extension EmulationViewController: GameControllerReceiverType
{
func gameController(gameController: GameControllerType, didActivateInput input: InputType)
{
if UIDevice.currentDevice().supportsVibration
{
UIDevice.currentDevice().vibrate()
}
if let input = input as? ControllerInput
{
switch input
{
case .Menu: self.controllerViewHeightConstraint.constant = 0
}
return
}
guard let input = input as? ControllerInput else { return }
print("Activated \(input)")
}
func gameController(gameController: GameControllerType, didDeactivateInput input: InputType)
{
guard let input = input as? ControllerInput else { return }
print("Deactivated \(input)")
}
}