2016-01-05 04:40:43 +00:00
|
|
|
//
|
|
|
|
// MachineDocument.swift
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 04/01/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-01-05 04:40:43 +00:00
|
|
|
//
|
|
|
|
|
2016-01-15 01:33:22 +00:00
|
|
|
import AudioToolbox
|
2018-07-28 03:37:24 +00:00
|
|
|
import Cocoa
|
2020-03-22 22:45:24 +00:00
|
|
|
import QuartzCore
|
2016-01-05 04:40:43 +00:00
|
|
|
|
2016-06-17 00:51:35 +00:00
|
|
|
class MachineDocument:
|
|
|
|
NSDocument,
|
|
|
|
NSWindowDelegate,
|
2018-03-22 13:48:19 +00:00
|
|
|
CSMachineDelegate,
|
2020-08-04 22:22:14 +00:00
|
|
|
CSScanTargetViewResponderDelegate,
|
2019-07-22 21:18:31 +00:00
|
|
|
CSROMReciverViewDelegate
|
2016-06-17 00:51:35 +00:00
|
|
|
{
|
2019-08-01 18:31:45 +00:00
|
|
|
// MARK: - Mutual Exclusion.
|
|
|
|
|
|
|
|
/// Ensures exclusive access between calls to self.machine.run and close().
|
|
|
|
private let actionLock = NSLock()
|
|
|
|
/// Ensures exclusive access between calls to machine.updateView and machine.drawView, and close().
|
|
|
|
private let drawLock = NSLock()
|
|
|
|
|
|
|
|
// MARK: - Machine details.
|
|
|
|
|
|
|
|
/// A description of the machine this document should represent once fully set up.
|
|
|
|
private var machineDescription: CSStaticAnalyser?
|
|
|
|
|
|
|
|
/// The active machine, following its successful creation.
|
|
|
|
private var machine: CSMachine!
|
2016-06-01 02:32:38 +00:00
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// @returns the appropriate window content aspect ratio for this @c self.machine.
|
|
|
|
private func aspectRatio() -> NSSize {
|
2016-06-01 23:04:07 +00:00
|
|
|
return NSSize(width: 4.0, height: 3.0)
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// The output audio queue, if any.
|
|
|
|
private var audioQueue: CSAudioQueue!
|
|
|
|
|
|
|
|
// MARK: - Main NIB connections.
|
|
|
|
|
|
|
|
/// The OpenGL view to receive this machine's display.
|
2020-08-04 22:22:14 +00:00
|
|
|
@IBOutlet weak var scanTargetView: CSScanTargetView!
|
2019-08-01 18:31:45 +00:00
|
|
|
|
2021-07-13 02:38:08 +00:00
|
|
|
/// The options view, if any.
|
|
|
|
@IBOutlet var optionsView: NSView!
|
2021-07-13 23:04:24 +00:00
|
|
|
@IBOutlet var optionsController: MachineController!
|
2016-04-18 01:43:39 +00:00
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// The activity panel, if one is deemed appropriate.
|
2021-07-14 03:32:00 +00:00
|
|
|
@IBOutlet var activityView: NSView!
|
2018-06-18 02:52:17 +00:00
|
|
|
|
2020-03-22 17:24:23 +00:00
|
|
|
/// The volume view.
|
2021-07-13 01:28:04 +00:00
|
|
|
@IBOutlet var volumeView: NSView!
|
2020-03-23 04:10:19 +00:00
|
|
|
@IBOutlet var volumeSlider: NSSlider!
|
2020-03-22 17:24:23 +00:00
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
// MARK: - NSDocument Overrides and NSWindowDelegate methods.
|
2016-01-15 01:33:22 +00:00
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Links this class to the MachineDocument NIB.
|
2017-09-20 03:06:37 +00:00
|
|
|
override var windowNibName: NSNib.Name? {
|
2019-03-27 02:15:38 +00:00
|
|
|
return "MachineDocument"
|
2016-10-02 20:57:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
override func read(from url: URL, ofType typeName: String) throws {
|
|
|
|
if let analyser = CSStaticAnalyser(fileAt: url) {
|
|
|
|
self.displayName = analyser.displayName
|
|
|
|
self.configureAs(analyser)
|
|
|
|
} else {
|
|
|
|
throw NSError(domain: "MachineDocument", code: -1, userInfo: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func close() {
|
2020-11-14 00:09:30 +00:00
|
|
|
// Close any dangling sheets.
|
|
|
|
//
|
|
|
|
// Be warned: in 11.0 at least, if there are any panels then posting the endSheet request
|
|
|
|
// will defer the close(), and close() will be called again at the end of that animation.
|
|
|
|
//
|
|
|
|
// So: MAKE SURE IT'S SAFE TO ENTER THIS FUNCTION TWICE. Hence the non-assumption here about
|
|
|
|
// any windows still existing.
|
2020-12-31 20:23:46 +00:00
|
|
|
if let window = self.windowControllers.first?.window {
|
2020-11-14 00:09:30 +00:00
|
|
|
for sheet in window.sheets {
|
|
|
|
window.endSheet(sheet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the machine, if any.
|
2020-02-15 04:16:44 +00:00
|
|
|
machine?.stop()
|
2020-02-03 02:39:20 +00:00
|
|
|
|
2020-11-14 00:09:30 +00:00
|
|
|
// End the update cycle.
|
2019-08-01 18:31:45 +00:00
|
|
|
actionLock.lock()
|
|
|
|
drawLock.lock()
|
|
|
|
machine = nil
|
2020-08-04 22:22:14 +00:00
|
|
|
scanTargetView.invalidate()
|
2019-08-01 18:31:45 +00:00
|
|
|
actionLock.unlock()
|
|
|
|
drawLock.unlock()
|
|
|
|
|
2020-11-14 00:09:30 +00:00
|
|
|
// Let the document controller do its thing.
|
2019-08-01 18:31:45 +00:00
|
|
|
super.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func data(ofType typeName: String) throws -> Data {
|
|
|
|
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
|
|
|
|
}
|
|
|
|
|
2016-09-16 02:12:12 +00:00
|
|
|
override func windowControllerDidLoadNib(_ aController: NSWindowController) {
|
2016-01-05 04:40:43 +00:00
|
|
|
super.windowControllerDidLoadNib(aController)
|
2018-04-03 02:42:41 +00:00
|
|
|
aController.window?.contentAspectRatio = self.aspectRatio()
|
2021-07-14 01:45:07 +00:00
|
|
|
volumeSlider.floatValue = pow(2.0, userDefaultsVolume())
|
2021-07-13 01:28:04 +00:00
|
|
|
|
2021-07-13 02:55:53 +00:00
|
|
|
volumeView.layer!.cornerRadius = 5.0
|
2019-08-01 18:31:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 18:24:38 +00:00
|
|
|
private var missingROMs: String = ""
|
2019-08-01 18:31:45 +00:00
|
|
|
func configureAs(_ analysis: CSStaticAnalyser) {
|
|
|
|
self.machineDescription = analysis
|
|
|
|
|
2021-05-01 01:37:41 +00:00
|
|
|
actionLock.lock()
|
|
|
|
drawLock.lock()
|
|
|
|
|
2021-06-06 18:56:43 +00:00
|
|
|
let missingROMs = NSMutableString()
|
2019-08-01 18:31:45 +00:00
|
|
|
if let machine = CSMachine(analyser: analysis, missingROMs: missingROMs) {
|
2021-06-06 18:56:43 +00:00
|
|
|
setRomRequesterIsVisible(false)
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
self.machine = machine
|
2020-03-23 04:10:19 +00:00
|
|
|
machine.setVolume(userDefaultsVolume())
|
2021-03-25 21:48:48 +00:00
|
|
|
setupMachineOutput()
|
2019-07-30 17:07:33 +00:00
|
|
|
} else {
|
2021-06-06 18:56:43 +00:00
|
|
|
self.missingROMs = missingROMs as String
|
2019-08-01 18:31:45 +00:00
|
|
|
requestRoms()
|
2019-07-30 17:07:33 +00:00
|
|
|
}
|
2021-05-01 01:37:41 +00:00
|
|
|
|
|
|
|
actionLock.unlock()
|
|
|
|
drawLock.unlock()
|
2018-04-03 22:47:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
enum InteractionMode {
|
|
|
|
case notStarted, showingMachinePicker, showingROMRequester, showingMachine
|
|
|
|
}
|
|
|
|
private var interactionMode: InteractionMode = .notStarted
|
|
|
|
|
2018-04-03 22:47:07 +00:00
|
|
|
// Attempting to show a sheet before the window is visible (such as when the NIB is loaded) results in
|
2020-11-13 23:03:46 +00:00
|
|
|
// a sheet mysteriously floating on its own. For now, use windowDidUpdate as a proxy to check whether
|
|
|
|
// the window is visible.
|
2018-04-03 22:47:07 +00:00
|
|
|
func windowDidUpdate(_ notification: Notification) {
|
2020-11-14 00:48:45 +00:00
|
|
|
if self.windowControllers.count > 0, let window = self.windowControllers[0].window, window.isVisible {
|
2020-11-13 23:03:46 +00:00
|
|
|
// Grab the regular window title, if it's not already stored.
|
2021-07-15 02:01:42 +00:00
|
|
|
if self.unadornedWindowTitle == "" {
|
2020-11-13 23:03:46 +00:00
|
|
|
self.unadornedWindowTitle = window.title
|
2019-08-01 18:31:45 +00:00
|
|
|
}
|
2021-07-15 02:01:42 +00:00
|
|
|
updateWindowTitle()
|
2019-08-01 18:31:45 +00:00
|
|
|
|
2020-11-13 23:03:46 +00:00
|
|
|
// If an interaction mode is not yet in effect, pick the proper one and display the relevant thing.
|
|
|
|
if self.interactionMode == .notStarted {
|
|
|
|
// If a full machine exists, just continue showing it.
|
|
|
|
if self.machine != nil {
|
|
|
|
self.interactionMode = .showingMachine
|
|
|
|
setupMachineOutput()
|
|
|
|
return
|
|
|
|
}
|
2019-08-01 18:31:45 +00:00
|
|
|
|
2020-11-13 23:03:46 +00:00
|
|
|
// If a machine has been picked but is not showing, there must be ROMs missing.
|
|
|
|
if self.machineDescription != nil {
|
|
|
|
self.interactionMode = .showingROMRequester
|
|
|
|
requestRoms()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a machine hasn't even been picked yet, show the machine picker.
|
|
|
|
self.interactionMode = .showingMachinePicker
|
|
|
|
Bundle.main.loadNibNamed("MachinePicker", owner: self, topLevelObjects: nil)
|
|
|
|
self.machinePicker?.establishStoredOptions()
|
|
|
|
window.beginSheet(self.machinePickerPanel!, completionHandler: nil)
|
2019-08-01 18:31:45 +00:00
|
|
|
}
|
2018-04-03 03:31:36 +00:00
|
|
|
}
|
2018-04-03 02:42:41 +00:00
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
|
2021-07-15 01:43:58 +00:00
|
|
|
func windowDidEnterFullScreen(_ notification: Notification) {
|
|
|
|
updateActivityViewVisibility()
|
|
|
|
}
|
|
|
|
|
2021-07-14 02:26:50 +00:00
|
|
|
// MARK: - Connections Between Machine and the Outside World.
|
2019-08-01 18:31:45 +00:00
|
|
|
|
|
|
|
private func setupMachineOutput() {
|
2020-08-04 22:22:14 +00:00
|
|
|
if let machine = self.machine, let scanTargetView = self.scanTargetView, machine.view != scanTargetView {
|
2019-06-11 22:21:56 +00:00
|
|
|
// Establish the output aspect ratio and audio.
|
2018-04-03 02:42:41 +00:00
|
|
|
let aspectRatio = self.aspectRatio()
|
2020-08-04 22:22:14 +00:00
|
|
|
machine.setView(scanTargetView, aspectRatio: Float(aspectRatio.width / aspectRatio.height))
|
2016-06-01 02:36:53 +00:00
|
|
|
|
2019-06-11 22:21:56 +00:00
|
|
|
// Attach an options panel if one is available.
|
2021-07-13 23:04:24 +00:00
|
|
|
if let optionsNibName = self.machineDescription?.optionsNibName {
|
|
|
|
Bundle.main.loadNibNamed(optionsNibName, owner: self, topLevelObjects: nil)
|
|
|
|
if let optionsController = self.optionsController {
|
|
|
|
optionsController.machine = machine
|
|
|
|
optionsController.establishStoredOptions()
|
2021-07-13 02:38:08 +00:00
|
|
|
}
|
|
|
|
if let optionsView = self.optionsView, let superview = self.volumeView.superview {
|
|
|
|
// Apply rounded edges.
|
2021-07-13 02:55:53 +00:00
|
|
|
optionsView.layer!.cornerRadius = 5.0
|
2021-07-13 02:38:08 +00:00
|
|
|
|
|
|
|
// Add to the superview.
|
|
|
|
superview.addSubview(optionsView)
|
|
|
|
|
|
|
|
// Apply constraints to appear centred and above the volume view.
|
2021-07-13 02:55:53 +00:00
|
|
|
let constraints = [
|
|
|
|
optionsView.centerXAnchor.constraint(equalTo: volumeView.centerXAnchor),
|
|
|
|
optionsView.bottomAnchor.constraint(equalTo: volumeView.topAnchor, constant: -8.0),
|
|
|
|
]
|
|
|
|
superview.addConstraints(constraints)
|
2021-07-13 02:38:08 +00:00
|
|
|
}
|
2018-04-03 03:31:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 23:03:44 +00:00
|
|
|
// Set up a fader for the volume and options.
|
|
|
|
var fadingViews: [NSView] = []
|
|
|
|
if let optionsView = self.optionsView {
|
|
|
|
fadingViews.append(optionsView)
|
|
|
|
}
|
|
|
|
if let volumeView = self.volumeView {
|
|
|
|
fadingViews.append(volumeView)
|
|
|
|
}
|
|
|
|
optionsFader = ViewFader(views: fadingViews)
|
|
|
|
|
2021-05-08 18:35:57 +00:00
|
|
|
// Create and populate an activity display if required.
|
|
|
|
setupActivityDisplay()
|
|
|
|
|
2018-04-03 02:42:41 +00:00
|
|
|
machine.delegate = self
|
2020-08-04 22:22:14 +00:00
|
|
|
scanTargetView.responderDelegate = self
|
2017-02-11 17:58:47 +00:00
|
|
|
|
2019-09-22 17:53:38 +00:00
|
|
|
// If this machine has a mouse, enable mouse capture; also indicate whether usurption
|
|
|
|
// of the command key is desired.
|
2020-08-04 22:22:14 +00:00
|
|
|
scanTargetView.shouldCaptureMouse = machine.hasMouse
|
|
|
|
scanTargetView.shouldUsurpCommand = machine.shouldUsurpCommand
|
2019-06-11 22:21:56 +00:00
|
|
|
|
2018-04-03 02:42:41 +00:00
|
|
|
setupAudioQueueClockRate()
|
2016-10-25 02:08:24 +00:00
|
|
|
|
2019-06-11 22:21:56 +00:00
|
|
|
// Bring OpenGL view-holding window on top of the options panel and show the content.
|
2020-08-04 22:22:14 +00:00
|
|
|
scanTargetView.isHidden = false
|
|
|
|
scanTargetView.window!.makeKeyAndOrderFront(self)
|
|
|
|
scanTargetView.window!.makeFirstResponder(scanTargetView)
|
2017-02-27 02:58:43 +00:00
|
|
|
|
2020-01-20 21:21:53 +00:00
|
|
|
// Start forwarding best-effort updates.
|
2020-02-03 02:39:20 +00:00
|
|
|
machine.start()
|
2018-04-03 02:42:41 +00:00
|
|
|
}
|
2016-06-21 01:47:27 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 22:53:56 +00:00
|
|
|
func machineSpeakerDidChangeInputClock(_ machine: CSMachine) {
|
2020-01-23 03:10:20 +00:00
|
|
|
// setupAudioQueueClockRate not only needs blocking access to the machine,
|
|
|
|
// but may be triggered on an arbitrary thread by a running machine, and that
|
|
|
|
// running machine may not be able to stop running until it has been called
|
|
|
|
// (e.g. if it is currently trying to run_until an audio event). Break the
|
2023-05-12 18:03:38 +00:00
|
|
|
// deadlock with an async dispatch.
|
2020-01-23 03:10:20 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.setupAudioQueueClockRate()
|
|
|
|
}
|
2018-03-22 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
private func setupAudioQueueClockRate() {
|
2020-01-23 03:10:20 +00:00
|
|
|
// Establish and provide the audio queue, taking advice as to an appropriate sampling rate.
|
|
|
|
//
|
|
|
|
// TODO: this needs to be threadsafe. FIX!
|
2016-06-15 12:07:25 +00:00
|
|
|
let maximumSamplingRate = CSAudioQueue.preferredSamplingRate()
|
2020-02-19 03:33:16 +00:00
|
|
|
let selectedSamplingRate = Float64(self.machine.idealSamplingRate(from: NSRange(location: 0, length: NSInteger(maximumSamplingRate))))
|
2020-12-31 20:23:46 +00:00
|
|
|
let isStereo = self.machine.isStereo
|
2016-06-05 15:20:05 +00:00
|
|
|
if selectedSamplingRate > 0 {
|
2020-02-19 03:33:16 +00:00
|
|
|
// [Re]create the audio queue only if necessary.
|
2021-05-09 00:46:10 +00:00
|
|
|
if self.audioQueue == nil || self.audioQueue.samplingRate != selectedSamplingRate || self.audioQueue != self.machine.audioQueue {
|
2020-02-19 03:33:16 +00:00
|
|
|
self.machine.audioQueue = nil
|
|
|
|
self.audioQueue = CSAudioQueue(samplingRate: Float64(selectedSamplingRate), isStereo:isStereo)
|
2021-05-09 00:46:10 +00:00
|
|
|
self.machine.audioQueue = self.audioQueue
|
|
|
|
self.machine.setAudioSamplingRate(Float(selectedSamplingRate), bufferSize:audioQueue.preferredBufferSize, stereo:isStereo)
|
2020-02-19 03:33:16 +00:00
|
|
|
}
|
2016-06-05 15:20:05 +00:00
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
// MARK: - Pasteboard Forwarding.
|
|
|
|
|
|
|
|
/// Forwards any text currently on the pasteboard into the active machine.
|
|
|
|
func paste(_ sender: Any) {
|
|
|
|
let pasteboard = NSPasteboard.general
|
|
|
|
if let string = pasteboard.string(forType: .string), let machine = self.machine {
|
|
|
|
machine.paste(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Runtime Media Insertion.
|
|
|
|
|
|
|
|
/// Delegate message to receive drag and drop files.
|
2020-09-14 02:11:51 +00:00
|
|
|
final func scanTargetView(_ view: CSScanTargetView, didReceiveFileAt URL: URL) {
|
2021-05-01 01:37:41 +00:00
|
|
|
insertFile(URL)
|
2017-08-17 14:48:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Action for the insert menu command; displays an NSOpenPanel and then segues into the same process
|
|
|
|
/// as if a file had been received via drag and drop.
|
2018-08-05 02:21:23 +00:00
|
|
|
@IBAction final func insertMedia(_ sender: AnyObject!) {
|
2018-08-05 17:12:02 +00:00
|
|
|
let openPanel = NSOpenPanel()
|
2018-08-06 22:56:59 +00:00
|
|
|
openPanel.message = "Hint: you can also insert media by dragging and dropping it onto the machine's window."
|
2018-08-05 17:12:02 +00:00
|
|
|
openPanel.beginSheetModal(for: self.windowControllers[0].window!) { (response) in
|
2018-08-05 02:21:23 +00:00
|
|
|
if response == .OK {
|
2018-08-05 17:12:02 +00:00
|
|
|
for url in openPanel.urls {
|
2021-05-01 01:37:41 +00:00
|
|
|
self.insertFile(url)
|
2018-08-05 17:12:02 +00:00
|
|
|
}
|
2018-08-05 02:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 01:37:41 +00:00
|
|
|
private func insertFile(_ URL: URL) {
|
|
|
|
// Try to insert media.
|
|
|
|
let mediaSet = CSMediaSet(fileAt: URL)
|
|
|
|
if !mediaSet.empty {
|
|
|
|
mediaSet.apply(to: self.machine)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failing that see whether a new machine is required.
|
|
|
|
// TODO.
|
|
|
|
if let newMachine = CSStaticAnalyser(fileAt: URL) {
|
|
|
|
machine?.stop()
|
|
|
|
self.interactionMode = .notStarted
|
|
|
|
self.scanTargetView.willChangeScanTargetOwner()
|
|
|
|
configureAs(newMachine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
// MARK: - Input Management.
|
2016-06-05 12:53:05 +00:00
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Upon a resign key, immediately releases all ongoing input mechanisms — any currently pressed keys,
|
|
|
|
/// and joystick and mouse inputs.
|
2016-09-16 02:12:12 +00:00
|
|
|
func windowDidResignKey(_ notification: Notification) {
|
2018-04-03 03:31:36 +00:00
|
|
|
if let machine = self.machine {
|
|
|
|
machine.clearAllKeys()
|
2018-07-22 20:55:47 +00:00
|
|
|
machine.joystickManager = nil
|
|
|
|
}
|
2020-08-04 22:22:14 +00:00
|
|
|
self.scanTargetView.releaseMouse()
|
2018-07-22 20:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Upon becoming key, attaches joystick input to the machine.
|
2018-07-22 20:55:47 +00:00
|
|
|
func windowDidBecomeKey(_ notification: Notification) {
|
|
|
|
if let machine = self.machine {
|
|
|
|
machine.joystickManager = (DocumentController.shared as! DocumentController).joystickManager
|
2018-04-03 03:31:36 +00:00
|
|
|
}
|
2016-06-05 12:53:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Forwards key down events directly to the machine.
|
2016-09-16 02:12:12 +00:00
|
|
|
func keyDown(_ event: NSEvent) {
|
2018-04-03 03:31:36 +00:00
|
|
|
if let machine = self.machine {
|
2023-12-28 20:05:55 +00:00
|
|
|
machine.setKey(event.keyCode, characters: event.characters, isPressed: true, isRepeat: event.isARepeat)
|
2018-04-03 03:31:36 +00:00
|
|
|
}
|
2016-06-05 12:53:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Forwards key up events directly to the machine.
|
2016-09-16 02:12:12 +00:00
|
|
|
func keyUp(_ event: NSEvent) {
|
2018-04-03 03:31:36 +00:00
|
|
|
if let machine = self.machine {
|
2023-12-28 20:05:55 +00:00
|
|
|
machine.setKey(event.keyCode, characters: event.characters, isPressed: false, isRepeat: false)
|
2018-04-03 03:31:36 +00:00
|
|
|
}
|
2016-06-05 12:53:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Synthesies appropriate key up and key down events upon any change in modifiers.
|
2016-09-16 02:12:12 +00:00
|
|
|
func flagsChanged(_ newModifiers: NSEvent) {
|
2018-04-03 03:31:36 +00:00
|
|
|
if let machine = self.machine {
|
2023-12-03 03:25:19 +00:00
|
|
|
if newModifiers.modifierFlags.contains(.shift) != shiftIsDown {
|
|
|
|
shiftIsDown = newModifiers.modifierFlags.contains(.shift)
|
2023-12-28 20:05:55 +00:00
|
|
|
machine.setKey(VK_Shift, characters: nil, isPressed: shiftIsDown, isRepeat: false)
|
2023-12-03 03:25:19 +00:00
|
|
|
}
|
|
|
|
if newModifiers.modifierFlags.contains(.control) != controlIsDown {
|
|
|
|
controlIsDown = newModifiers.modifierFlags.contains(.control)
|
2023-12-28 20:05:55 +00:00
|
|
|
machine.setKey(VK_Control, characters: nil, isPressed: controlIsDown, isRepeat: false)
|
2023-12-03 03:25:19 +00:00
|
|
|
}
|
|
|
|
if newModifiers.modifierFlags.contains(.command) != commandIsDown {
|
|
|
|
commandIsDown = newModifiers.modifierFlags.contains(.command)
|
2023-12-28 20:05:55 +00:00
|
|
|
machine.setKey(VK_Command, characters: nil, isPressed: commandIsDown, isRepeat: false)
|
2023-12-03 03:25:19 +00:00
|
|
|
}
|
|
|
|
if newModifiers.modifierFlags.contains(.option) != optionIsDown {
|
|
|
|
optionIsDown = newModifiers.modifierFlags.contains(.option)
|
2023-12-28 20:05:55 +00:00
|
|
|
machine.setKey(VK_Option, characters: nil, isPressed: optionIsDown, isRepeat: false)
|
2023-12-03 03:25:19 +00:00
|
|
|
}
|
2018-04-03 03:31:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-03 03:25:19 +00:00
|
|
|
private var shiftIsDown = false
|
|
|
|
private var controlIsDown = false
|
|
|
|
private var commandIsDown = false
|
|
|
|
private var optionIsDown = false
|
2018-04-03 03:31:36 +00:00
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Forwards mouse movement events to the mouse.
|
2019-06-11 20:35:04 +00:00
|
|
|
func mouseMoved(_ event: NSEvent) {
|
2019-06-11 22:41:41 +00:00
|
|
|
if let machine = self.machine {
|
|
|
|
machine.addMouseMotionX(event.deltaX, y: event.deltaY)
|
|
|
|
}
|
2019-06-11 20:35:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Forwards mouse button down events to the mouse.
|
2019-06-11 20:35:04 +00:00
|
|
|
func mouseUp(_ event: NSEvent) {
|
2019-06-11 22:41:41 +00:00
|
|
|
if let machine = self.machine {
|
|
|
|
machine.setMouseButton(Int32(event.buttonNumber), isPressed: false)
|
|
|
|
}
|
2019-06-11 20:35:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Forwards mouse button up events to the mouse.
|
2019-06-11 20:35:04 +00:00
|
|
|
func mouseDown(_ event: NSEvent) {
|
2019-06-11 22:41:41 +00:00
|
|
|
if let machine = self.machine {
|
|
|
|
machine.setMouseButton(Int32(event.buttonNumber), isPressed: true)
|
|
|
|
}
|
2019-06-11 20:35:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
// MARK: - MachinePicker Outlets and Actions
|
2021-07-14 02:26:50 +00:00
|
|
|
|
2018-04-03 03:31:36 +00:00
|
|
|
@IBOutlet var machinePicker: MachinePicker?
|
2018-04-03 22:47:07 +00:00
|
|
|
@IBOutlet var machinePickerPanel: NSWindow?
|
2018-04-03 03:31:36 +00:00
|
|
|
@IBAction func createMachine(_ sender: NSButton?) {
|
2019-07-22 17:00:17 +00:00
|
|
|
let selectedMachine = machinePicker!.selectedMachine()
|
2018-08-06 02:47:51 +00:00
|
|
|
self.windowControllers[0].window?.endSheet(self.machinePickerPanel!)
|
2019-08-01 18:31:45 +00:00
|
|
|
self.machinePicker = nil
|
2019-07-22 17:00:17 +00:00
|
|
|
self.configureAs(selectedMachine)
|
2018-04-03 03:31:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 21:53:07 +00:00
|
|
|
@IBAction func tableViewDoubleClick(_ sender: NSTableView?) {
|
|
|
|
createMachine(nil)
|
|
|
|
}
|
|
|
|
|
2018-04-03 03:31:36 +00:00
|
|
|
@IBAction func cancelCreateMachine(_ sender: NSButton?) {
|
|
|
|
close()
|
2016-06-05 12:53:05 +00:00
|
|
|
}
|
2018-06-13 23:22:34 +00:00
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
// MARK: - ROMRequester Outlets and Actions
|
2021-07-14 02:26:50 +00:00
|
|
|
|
2019-07-22 17:00:17 +00:00
|
|
|
@IBOutlet var romRequesterPanel: NSWindow?
|
|
|
|
@IBOutlet var romRequesterText: NSTextField?
|
2019-07-23 20:24:23 +00:00
|
|
|
@IBOutlet var romReceiverErrorField: NSTextField?
|
2019-07-22 21:18:31 +00:00
|
|
|
@IBOutlet var romReceiverView: CSROMReceiverView?
|
2019-07-23 20:24:23 +00:00
|
|
|
private var romRequestBaseText = ""
|
2021-06-06 18:56:43 +00:00
|
|
|
|
|
|
|
private func setRomRequesterIsVisible(_ visible : Bool) {
|
2021-06-07 00:25:26 +00:00
|
|
|
if !visible && self.romRequesterPanel == nil {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-06 18:56:43 +00:00
|
|
|
if self.romRequesterPanel!.isVisible == visible {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if visible {
|
|
|
|
self.windowControllers[0].window?.beginSheet(self.romRequesterPanel!, completionHandler: nil)
|
|
|
|
} else {
|
|
|
|
self.windowControllers[0].window?.endSheet(self.romRequesterPanel!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 22:02:48 +00:00
|
|
|
func requestRoms() {
|
2019-07-30 17:07:33 +00:00
|
|
|
// Don't act yet if there's no window controller yet.
|
|
|
|
if self.windowControllers.count == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 18:56:43 +00:00
|
|
|
// Load the ROM requester dialogue if it's not already loaded.
|
|
|
|
if self.romRequesterPanel == nil {
|
|
|
|
Bundle.main.loadNibNamed("ROMRequester", owner: self, topLevelObjects: nil)
|
|
|
|
self.romReceiverView!.delegate = self
|
|
|
|
self.romRequestBaseText = romRequesterText!.stringValue
|
|
|
|
romReceiverErrorField?.alphaValue = 0.0
|
|
|
|
}
|
2019-07-22 17:00:17 +00:00
|
|
|
|
2019-07-23 01:46:28 +00:00
|
|
|
// Populate the current absentee list.
|
|
|
|
populateMissingRomList()
|
|
|
|
|
|
|
|
// Show the thing.
|
2021-06-06 18:56:43 +00:00
|
|
|
setRomRequesterIsVisible(true)
|
2019-07-23 01:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func cancelRequestROMs(_ sender: NSButton?) {
|
|
|
|
close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func populateMissingRomList() {
|
2021-06-06 18:24:38 +00:00
|
|
|
romRequesterText!.stringValue = self.romRequestBaseText + self.missingROMs
|
2019-07-22 17:00:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:18:31 +00:00
|
|
|
func romReceiverView(_ view: CSROMReceiverView, didReceiveFileAt URL: URL) {
|
2019-07-22 22:02:48 +00:00
|
|
|
// Test whether the file identified matches any of the currently missing ROMs.
|
|
|
|
// If so then remove that ROM from the missing list and update the request screen.
|
|
|
|
// If no ROMs are still missing, start the machine.
|
2021-06-06 18:56:43 +00:00
|
|
|
if CSMachine.attemptInstallROM(URL) {
|
|
|
|
configureAs(self.machineDescription!)
|
|
|
|
} else {
|
|
|
|
showRomReceiverError(error: "Didn't recognise contents of \(URL.lastPathComponent)")
|
2019-07-23 20:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yucky ugliness follows; my experience as an iOS developer intersects poorly with
|
|
|
|
// NSAnimationContext hence the various stateful diplications below. isShowingError
|
|
|
|
// should be essentially a duplicate of the current alphaValue, and animationCount
|
|
|
|
// is to resolve my inability to figure out how to cancel scheduled animations.
|
|
|
|
private var errorText = ""
|
|
|
|
private var isShowingError = false
|
|
|
|
private var animationCount = 0
|
|
|
|
private func showRomReceiverError(error: String) {
|
|
|
|
// Set or append the new error.
|
|
|
|
if self.errorText.count > 0 {
|
|
|
|
self.errorText = self.errorText + "\n" + error
|
|
|
|
} else {
|
|
|
|
self.errorText = error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the new complete text.
|
|
|
|
romReceiverErrorField!.stringValue = self.errorText
|
|
|
|
|
|
|
|
if !isShowingError {
|
|
|
|
// Schedule the box's appearance.
|
|
|
|
NSAnimationContext.beginGrouping()
|
|
|
|
NSAnimationContext.current.duration = 0.1
|
|
|
|
romReceiverErrorField?.animator().alphaValue = 1.0
|
|
|
|
NSAnimationContext.endGrouping()
|
|
|
|
isShowingError = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule the box to disappear.
|
|
|
|
self.animationCount = self.animationCount + 1
|
|
|
|
let capturedAnimationCount = animationCount
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
|
|
|
|
if self.animationCount == capturedAnimationCount {
|
|
|
|
NSAnimationContext.beginGrouping()
|
|
|
|
NSAnimationContext.current.duration = 1.0
|
|
|
|
self.romReceiverErrorField?.animator().alphaValue = 0.0
|
|
|
|
NSAnimationContext.endGrouping()
|
|
|
|
self.isShowingError = false
|
|
|
|
self.errorText = ""
|
|
|
|
}
|
2019-07-22 22:02:48 +00:00
|
|
|
}
|
2019-07-22 21:18:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 02:26:50 +00:00
|
|
|
// MARK: - Joystick-via-the-keyboard selection.
|
|
|
|
|
2020-02-29 23:30:58 +00:00
|
|
|
@IBAction func useKeyboardAsPhysicalKeyboard(_ sender: NSMenuItem?) {
|
2020-02-29 23:17:39 +00:00
|
|
|
machine.inputMode = .keyboardPhysical
|
2018-06-13 23:22:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 23:30:58 +00:00
|
|
|
@IBAction func useKeyboardAsLogicalKeyboard(_ sender: NSMenuItem?) {
|
|
|
|
machine.inputMode = .keyboardLogical
|
2018-06-13 23:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func useKeyboardAsJoystick(_ sender: NSMenuItem?) {
|
|
|
|
machine.inputMode = .joystick
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:31:45 +00:00
|
|
|
/// Determines which of the menu items to enable and disable based on the ability of the
|
|
|
|
/// current machine to handle keyboard and joystick input, accept new media and whether
|
|
|
|
/// it has an associted activity window.
|
2018-06-13 23:22:34 +00:00
|
|
|
override func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
|
|
|
|
if let menuItem = item as? NSMenuItem {
|
|
|
|
switch item.action {
|
2020-02-29 23:30:58 +00:00
|
|
|
case #selector(self.useKeyboardAsPhysicalKeyboard):
|
2018-10-25 01:59:30 +00:00
|
|
|
if machine == nil || !machine.hasExclusiveKeyboard {
|
2018-08-06 22:52:42 +00:00
|
|
|
menuItem.state = .off
|
2018-06-13 23:22:34 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-29 23:17:39 +00:00
|
|
|
menuItem.state = machine.inputMode == .keyboardPhysical ? .on : .off
|
2018-06-13 23:22:34 +00:00
|
|
|
return true
|
|
|
|
|
2020-02-29 23:30:58 +00:00
|
|
|
case #selector(self.useKeyboardAsLogicalKeyboard):
|
|
|
|
if machine == nil || !machine.hasExclusiveKeyboard {
|
|
|
|
menuItem.state = .off
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItem.state = machine.inputMode == .keyboardLogical ? .on : .off
|
2018-06-13 23:22:34 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
case #selector(self.useKeyboardAsJoystick):
|
|
|
|
if machine == nil || !machine.hasJoystick {
|
2018-08-06 22:52:42 +00:00
|
|
|
menuItem.state = .off
|
2018-06-13 23:22:34 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
menuItem.state = machine.inputMode == .joystick ? .on : .off
|
|
|
|
return true
|
|
|
|
|
2018-08-06 22:52:42 +00:00
|
|
|
case #selector(self.insertMedia(_:)):
|
|
|
|
return self.machine != nil && self.machine.canInsertMedia
|
|
|
|
|
2018-06-13 23:22:34 +00:00
|
|
|
default: break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.validateUserInterfaceItem(item)
|
|
|
|
}
|
2018-06-17 22:53:56 +00:00
|
|
|
|
2021-07-14 02:26:50 +00:00
|
|
|
// MARK: - Screenshots.
|
|
|
|
|
2020-01-26 22:36:16 +00:00
|
|
|
/// Saves a screenshot of the machine's current display.
|
2018-07-28 03:37:24 +00:00
|
|
|
@IBAction func saveScreenshot(_ sender: AnyObject!) {
|
|
|
|
// Grab a date formatter and form a file name.
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
dateFormatter.dateStyle = .short
|
|
|
|
dateFormatter.timeStyle = .long
|
|
|
|
|
|
|
|
let filename = ("Clock Signal Screen Shot " + dateFormatter.string(from: Date()) + ".png").replacingOccurrences(of: "/", with: "-")
|
|
|
|
.replacingOccurrences(of: ":", with: ".")
|
2021-03-26 02:44:18 +00:00
|
|
|
let pictursURL = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask)[0]
|
|
|
|
let url = pictursURL.appendingPathComponent(filename)
|
2018-07-28 03:37:24 +00:00
|
|
|
|
|
|
|
// Obtain the machine's current display.
|
2020-08-16 01:24:10 +00:00
|
|
|
let imageRepresentation = self.machine.imageRepresentation
|
2018-07-28 03:37:24 +00:00
|
|
|
|
|
|
|
// Encode as a PNG and save.
|
2020-08-16 01:24:10 +00:00
|
|
|
let pngData = imageRepresentation.representation(using: .png, properties: [:])
|
2018-07-28 03:37:24 +00:00
|
|
|
try! pngData?.write(to: url)
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:07:51 +00:00
|
|
|
// MARK: - Window Title Updates.
|
2021-07-14 02:26:50 +00:00
|
|
|
|
2019-08-02 21:07:51 +00:00
|
|
|
private var unadornedWindowTitle = ""
|
2021-07-15 01:43:58 +00:00
|
|
|
private var mouseIsCaptured = false
|
2021-07-15 23:22:23 +00:00
|
|
|
private var windowTitleSuffix = ""
|
2021-07-15 01:43:58 +00:00
|
|
|
|
|
|
|
private func updateWindowTitle() {
|
|
|
|
var title = self.unadornedWindowTitle
|
2021-07-15 23:22:23 +00:00
|
|
|
if windowTitleSuffix != "" {
|
|
|
|
title += windowTitleSuffix
|
2021-07-15 01:43:58 +00:00
|
|
|
}
|
|
|
|
if mouseIsCaptured {
|
|
|
|
title += " (press ⌘+control to release mouse)"
|
|
|
|
}
|
|
|
|
self.windowControllers[0].window?.title = title
|
|
|
|
}
|
|
|
|
|
2020-09-14 02:11:51 +00:00
|
|
|
internal func scanTargetViewDidCaptureMouse(_ view: CSScanTargetView) {
|
2021-07-15 01:43:58 +00:00
|
|
|
mouseIsCaptured = true
|
|
|
|
updateWindowTitle()
|
2019-08-02 21:07:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 02:11:51 +00:00
|
|
|
internal func scanTargetViewDidReleaseMouse(_ view: CSScanTargetView) {
|
2021-07-15 01:43:58 +00:00
|
|
|
mouseIsCaptured = false
|
|
|
|
updateWindowTitle()
|
2019-08-02 21:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Activity Display.
|
2019-08-01 18:31:45 +00:00
|
|
|
|
|
|
|
private class LED {
|
2018-06-19 01:22:51 +00:00
|
|
|
let levelIndicator: NSLevelIndicator
|
2021-07-16 01:49:11 +00:00
|
|
|
init(levelIndicator: NSLevelIndicator, isPersistent: Bool) {
|
2018-06-19 01:22:51 +00:00
|
|
|
self.levelIndicator = levelIndicator
|
2021-07-16 01:49:11 +00:00
|
|
|
self.isPersistent = isPersistent
|
2018-06-19 01:22:51 +00:00
|
|
|
}
|
|
|
|
var isLit = false
|
|
|
|
var isBlinking = false
|
2021-07-16 01:49:11 +00:00
|
|
|
var isPersistent = false
|
2018-06-19 01:22:51 +00:00
|
|
|
}
|
2019-08-01 18:31:45 +00:00
|
|
|
private var leds: [String: LED] = [:]
|
2021-07-14 23:03:44 +00:00
|
|
|
private var activityFader: ViewFader! = nil
|
2019-08-01 18:31:45 +00:00
|
|
|
|
2018-06-17 22:53:56 +00:00
|
|
|
func setupActivityDisplay() {
|
2018-06-18 02:52:17 +00:00
|
|
|
var leds = machine.leds
|
|
|
|
if leds.count > 0 {
|
2019-03-27 02:15:38 +00:00
|
|
|
Bundle.main.loadNibNamed("Activity", owner: self, topLevelObjects: nil)
|
2018-06-18 02:52:17 +00:00
|
|
|
|
|
|
|
// Inspect the activity panel for indicators.
|
|
|
|
var activityIndicators: [NSLevelIndicator] = []
|
|
|
|
var textFields: [NSTextField] = []
|
2021-07-14 03:32:00 +00:00
|
|
|
if let activityView = self.activityView {
|
|
|
|
for view in activityView.subviews {
|
2018-06-18 02:52:17 +00:00
|
|
|
if let levelIndicator = view as? NSLevelIndicator {
|
|
|
|
activityIndicators.append(levelIndicator)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let textField = view as? NSTextField {
|
|
|
|
textFields.append(textField)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are fewer level indicators than LEDs, trim that list.
|
|
|
|
if activityIndicators.count < leds.count {
|
|
|
|
leds.removeSubrange(activityIndicators.count ..< leds.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove unused views.
|
|
|
|
for c in leds.count ..< activityIndicators.count {
|
|
|
|
textFields[c].removeFromSuperview()
|
|
|
|
activityIndicators[c].removeFromSuperview()
|
|
|
|
}
|
|
|
|
|
2018-06-19 01:22:51 +00:00
|
|
|
// Apply labels and create leds entries.
|
2018-06-18 02:52:17 +00:00
|
|
|
for c in 0 ..< leds.count {
|
2021-07-16 01:34:14 +00:00
|
|
|
textFields[c].stringValue = leds[c].name
|
2021-07-16 01:49:11 +00:00
|
|
|
self.leds[leds[c].name] = LED(levelIndicator: activityIndicators[c], isPersistent: leds[c].isPersisent)
|
2018-06-19 01:22:51 +00:00
|
|
|
}
|
2018-06-23 23:44:35 +00:00
|
|
|
|
2021-07-14 03:32:00 +00:00
|
|
|
// Create a fader.
|
|
|
|
activityFader = ViewFader(views: [self.activityView!])
|
|
|
|
|
|
|
|
// Add view to window, and constrain.
|
|
|
|
if let superview = activityIndicators[leds.count-1].superview {
|
|
|
|
superview.addConstraint(
|
|
|
|
activityIndicators[leds.count-1].bottomAnchor.constraint(equalTo: activityIndicators[leds.count-1].superview!.bottomAnchor, constant: -8.0)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if let windowView = self.volumeView.superview {
|
|
|
|
windowView.addSubview(self.activityView)
|
|
|
|
|
|
|
|
let constraints = [
|
|
|
|
self.activityView.rightAnchor.constraint(equalTo: windowView.rightAnchor),
|
|
|
|
self.activityView.topAnchor.constraint(equalTo: windowView.topAnchor),
|
|
|
|
]
|
|
|
|
windowView.addConstraints(constraints)
|
|
|
|
|
|
|
|
activityView.layer!.cornerRadius = 5.0
|
|
|
|
activityView.layer!.maskedCorners = [.layerMinXMinYCorner]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show or hide activity view as per current state.
|
2021-07-15 23:53:40 +00:00
|
|
|
updateActivityViewVisibility(true)
|
2018-06-19 01:22:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func machine(_ machine: CSMachine, ledShouldBlink ledName: String) {
|
|
|
|
// If there is such an LED, switch it off for 0.03 of a second; if it's meant
|
|
|
|
// to be off at the end of that, leave it off. Don't allow the blinks to
|
|
|
|
// pile up — allow there to be only one in flight at a time.
|
|
|
|
if let led = leds[ledName] {
|
|
|
|
DispatchQueue.main.async {
|
2021-07-15 23:51:23 +00:00
|
|
|
if !led.isBlinking && led.isLit {
|
2018-06-19 01:22:51 +00:00
|
|
|
led.levelIndicator.floatValue = 0.0
|
|
|
|
led.isBlinking = true
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.03) {
|
|
|
|
led.levelIndicator.floatValue = led.isLit ? 1.0 : 0.0
|
|
|
|
led.isBlinking = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func machine(_ machine: CSMachine, led ledName: String, didChangeToLit isLit: Bool) {
|
|
|
|
// If there is such an LED, switch it appropriately.
|
|
|
|
if let led = leds[ledName] {
|
2021-07-14 03:32:00 +00:00
|
|
|
DispatchQueue.main.async { [self] in
|
2021-07-15 23:22:23 +00:00
|
|
|
// Do nothing for no change of state.
|
|
|
|
if led.isLit == isLit {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-19 01:22:51 +00:00
|
|
|
led.levelIndicator.floatValue = isLit ? 1.0 : 0.0
|
|
|
|
led.isLit = isLit
|
2021-07-15 01:43:58 +00:00
|
|
|
|
|
|
|
// Possibly show or hide the activity subview.
|
2021-07-16 01:49:11 +00:00
|
|
|
self.updateActivityViewVisibility(false, changed: ledName)
|
2018-06-17 22:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 17:24:23 +00:00
|
|
|
|
2021-07-16 01:49:11 +00:00
|
|
|
private func updateActivityViewVisibility(_ isAppLaunch : Bool = false, changed: String? = nil) {
|
2021-07-15 23:51:23 +00:00
|
|
|
if let window = self.windowControllers.first?.window, let activityFader = self.activityFader {
|
2021-07-16 01:49:11 +00:00
|
|
|
// Rules applied below:
|
|
|
|
//
|
|
|
|
// Fullscreen:
|
|
|
|
// (i) always show activity view if any persistent LEDs are present;
|
|
|
|
// (ii) otherwise, show activity view only while at least one LED is lit.
|
|
|
|
//
|
|
|
|
// Windowed:
|
|
|
|
// (i) show while any non-persistent LED is lit;
|
|
|
|
// (ii) show transiently to indicate a change of state in any persistent LED.
|
|
|
|
//
|
|
|
|
let hasLitLEDs = !self.leds.filter {
|
|
|
|
$0.value.isLit && (!$0.value.isPersistent || window.styleMask.contains(.fullScreen)) ||
|
|
|
|
($0.value.isPersistent && window.styleMask.contains(.fullScreen))
|
|
|
|
}.isEmpty
|
|
|
|
let shouldShowTransient = !window.styleMask.contains(.fullScreen) && changed != nil && self.leds[changed!]!.isPersistent
|
|
|
|
|
|
|
|
if hasLitLEDs {
|
|
|
|
activityFader.animateIn()
|
|
|
|
} else if shouldShowTransient {
|
2021-07-15 23:22:23 +00:00
|
|
|
activityFader.showTransiently(for: 1.0)
|
2021-07-15 02:01:42 +00:00
|
|
|
} else {
|
2021-07-16 01:49:11 +00:00
|
|
|
activityFader.animateOut(delay: 0.2)
|
2021-07-15 02:01:42 +00:00
|
|
|
}
|
2021-07-15 01:43:58 +00:00
|
|
|
}
|
2020-03-22 22:45:24 +00:00
|
|
|
}
|
2021-07-14 03:32:00 +00:00
|
|
|
|
|
|
|
// MARK: - In-window panels (i.e. options, volume).
|
|
|
|
|
2021-07-14 23:03:44 +00:00
|
|
|
private var optionsFader: ViewFader! = nil
|
2021-07-13 02:38:08 +00:00
|
|
|
|
2020-09-14 02:11:51 +00:00
|
|
|
internal func scanTargetViewDidShowOSMouseCursor(_ view: CSScanTargetView) {
|
2020-03-22 20:25:07 +00:00
|
|
|
// The OS mouse cursor became visible, so show the volume controls.
|
2021-07-14 23:03:44 +00:00
|
|
|
optionsFader.animateIn()
|
2020-03-22 20:25:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 21:21:25 +00:00
|
|
|
internal func scanTargetViewWouldHideOSMouseCursor(_ view: CSScanTargetView) {
|
2020-03-22 20:25:07 +00:00
|
|
|
// The OS mouse cursor will be hidden, so hide the volume controls.
|
2021-07-14 23:03:44 +00:00
|
|
|
optionsFader.animateOut(delay: 0.0)
|
2020-03-22 20:25:07 +00:00
|
|
|
}
|
2020-03-23 04:10:19 +00:00
|
|
|
|
2021-07-14 03:32:00 +00:00
|
|
|
// MARK: - Helpers for fading things in and out.
|
|
|
|
|
2021-07-14 23:03:44 +00:00
|
|
|
/// Maintains a list of views and offers in-and-out animations on those,
|
|
|
|
/// testing current state as necessary and otherwise coordinating with
|
|
|
|
/// CoreAnimation.
|
2021-07-14 03:32:00 +00:00
|
|
|
private class ViewFader: NSObject, CAAnimationDelegate {
|
2021-07-14 23:03:44 +00:00
|
|
|
private var views: [NSView]
|
2021-07-14 03:32:00 +00:00
|
|
|
|
|
|
|
init(views: [NSView]) {
|
|
|
|
self.views = views
|
2021-07-14 23:03:44 +00:00
|
|
|
for view in views {
|
|
|
|
view.isHidden = true
|
|
|
|
}
|
2021-07-14 03:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func animationDidStop(_ animation: CAAnimation, finished: Bool) {
|
2021-07-14 23:03:44 +00:00
|
|
|
if finished {
|
|
|
|
for view in views {
|
|
|
|
view.isHidden = true
|
|
|
|
}
|
2021-07-14 03:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 23:03:44 +00:00
|
|
|
func animateIn() {
|
|
|
|
for view in views {
|
|
|
|
view.layer?.removeAllAnimations()
|
|
|
|
view.isHidden = false
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 03:40:15 +00:00
|
|
|
|
2021-07-14 23:03:44 +00:00
|
|
|
func animateOut(delay : TimeInterval) {
|
2021-07-15 00:50:41 +00:00
|
|
|
// Do nothing if already animating out or invisible.
|
2021-07-14 23:03:44 +00:00
|
|
|
if views[0].isHidden || views[0].layer?.animation(forKey: "opacity") != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-07-14 03:32:00 +00:00
|
|
|
|
2021-07-14 23:03:44 +00:00
|
|
|
for view in views {
|
|
|
|
let fadeAnimation = CABasicAnimation(keyPath: "opacity")
|
|
|
|
fadeAnimation.beginTime = CACurrentMediaTime() + delay
|
|
|
|
fadeAnimation.fromValue = 1.0
|
|
|
|
fadeAnimation.toValue = 0.0
|
|
|
|
fadeAnimation.duration = 0.2
|
|
|
|
fadeAnimation.delegate = self
|
|
|
|
|
2021-07-15 00:50:41 +00:00
|
|
|
fadeAnimation.fillMode = .forwards
|
|
|
|
fadeAnimation.isRemovedOnCompletion = false
|
|
|
|
|
2021-07-14 23:03:44 +00:00
|
|
|
view.layer?.removeAllAnimations()
|
|
|
|
view.layer!.add(fadeAnimation, forKey: "opacity")
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 23:22:23 +00:00
|
|
|
|
|
|
|
func showTransiently(for period: TimeInterval) {
|
|
|
|
animateIn()
|
|
|
|
animateOut(delay: period)
|
|
|
|
}
|
2021-07-14 03:32:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 02:26:50 +00:00
|
|
|
// MARK: - Volume Control.
|
|
|
|
|
|
|
|
@IBAction func setVolume(_ sender: NSSlider!) {
|
|
|
|
if let machine = self.machine {
|
|
|
|
let linearValue = log2(sender.floatValue)
|
|
|
|
machine.setVolume(linearValue)
|
|
|
|
setUserDefaultsVolume(linearValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 04:10:19 +00:00
|
|
|
// The user's selected volume is stored as 1 - volume in the user defaults in order
|
|
|
|
// to take advantage of the default value being 0.
|
|
|
|
private func userDefaultsVolume() -> Float {
|
|
|
|
return 1.0 - UserDefaults.standard.float(forKey: "defaultVolume")
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setUserDefaultsVolume(_ volume: Float) {
|
|
|
|
UserDefaults.standard.set(1.0 - volume, forKey: "defaultVolume")
|
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|