1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-08-16 00:29:01 +00:00
CLK/OSBindings/Mac/Clock Signal/Documents/Atari2600Document.swift

108 lines
2.5 KiB
Swift
Raw Normal View History

//
// Atari2600Document.swift
// Clock Signal
//
// Created by Thomas Harte on 16/07/2015.
// Copyright © 2015 Thomas Harte. All rights reserved.
//
import Cocoa
class Atari2600Document: MachineDocument {
2016-09-16 02:12:12 +00:00
fileprivate var atari2600 = CSAtari2600()
override var machine: CSMachine! {
get {
return atari2600
}
}
override var name: String! {
get {
return "atari2600"
}
}
// MARK: NSDocument overrides
override class func autosavesInPlace() -> Bool {
return true
}
override var windowNibName: String? {
return "Atari2600Document"
}
2016-09-16 02:12:12 +00:00
override func windowControllerDidLoadNib(_ aController: NSWindowController) {
super.windowControllerDidLoadNib(aController)
// push whatever settings the switches have in the NIB into the emulation
pushSwitchValues()
// show the options window but ensure the OpenGL view is key
showOptions(self)
2016-09-16 02:12:12 +00:00
self.openGLView.window?.makeKey()
}
// MARK: CSOpenGLViewResponderDelegate
2016-09-16 02:12:12 +00:00
fileprivate func inputForKey(_ event: NSEvent) -> Atari2600DigitalInput? {
switch event.keyCode {
case 123: return Atari2600DigitalInputJoy1Left
case 126: return Atari2600DigitalInputJoy1Up
case 124: return Atari2600DigitalInputJoy1Right
case 125: return Atari2600DigitalInputJoy1Down
case 0: return Atari2600DigitalInputJoy1Fire
2016-09-16 02:12:12 +00:00
default:
Swift.print("\(event.keyCode)")
return nil
}
}
2016-09-16 02:12:12 +00:00
override func keyDown(_ event: NSEvent) {
super.keyDown(event)
if let input = inputForKey(event) {
2016-09-16 02:12:12 +00:00
atari2600.setState(true, for: input)
}
2015-08-19 00:58:05 +00:00
if event.keyCode == 36 {
atari2600.setResetLineEnabled(true)
2015-08-19 00:58:05 +00:00
}
}
2016-09-16 02:12:12 +00:00
override func keyUp(_ event: NSEvent) {
super.keyUp(event)
if let input = inputForKey(event) {
2016-09-16 02:12:12 +00:00
atari2600.setState(false, for: input)
}
2015-08-19 00:58:05 +00:00
if event.keyCode == 36 {
atari2600.setResetLineEnabled(false)
2015-08-19 00:58:05 +00:00
}
}
// MARK: Options
@IBOutlet var resetButton: NSButton!
@IBOutlet var selectButton: NSButton!
@IBOutlet var colourButton: NSButton!
@IBOutlet var leftPlayerDifficultyButton: NSButton!
@IBOutlet var rightPlayerDifficultyButton: NSButton!
2016-09-16 02:12:12 +00:00
@IBAction func optionDidChange(_ sender: AnyObject!) {
pushSwitchValues()
}
2016-09-16 02:12:12 +00:00
fileprivate func pushSwitchValues() {
atari2600.colourButton = colourButton.state == NSOnState
atari2600.leftPlayerDifficultyButton = leftPlayerDifficultyButton.state == NSOnState
atari2600.rightPlayerDifficultyButton = rightPlayerDifficultyButton.state == NSOnState
}
2016-09-16 02:12:12 +00:00
@IBAction func optionWasPressed(_ sender: NSButton!) {
if sender == resetButton {
atari2600.pressResetButton()
} else {
atari2600.pressSelectButton()
}
}
2016-09-16 02:12:12 +00:00
}