/
ViewController.swift
86 lines (64 loc) · 2.53 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// ViewController.swift
// TouchBreakout
//
// Created by 宋 奎熹 on 2017/11/16.
// Copyright © 2017年 宋 奎熹. All rights reserved.
//
import Cocoa
import SpriteKit
import GameplayKit
class ViewController: NSViewController {
@IBOutlet var skView: SKView!
var gameScene: GameScene?
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.skView {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
gameScene = scene as? GameScene
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
}
let restartButton = NSButton(image: #imageLiteral(resourceName: "restart"), target: self, action: #selector(restartGame))
restartButton.frame = CGRect(x: 15, y: view.frame.height - 60, width: 45, height: 45)
view.addSubview(restartButton)
}
override func viewDidAppear() {
super.viewDidAppear()
view.window!.styleMask.remove(NSWindow.StyleMask.resizable)
}
@objc func restartGame() {
gameScene?.gameState = .new
}
override func makeTouchBar() -> NSTouchBar? {
let touchBar = NSTouchBar()
touchBar.delegate = self
touchBar.customizationIdentifier = .touchBar
touchBar.defaultItemIdentifiers = [.touchEvent]
touchBar.customizationAllowedItemIdentifiers = [.touchEvent]
return touchBar
}
}
extension ViewController: NSTouchBarDelegate {
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
let touchBarView = TouchBarView()
touchBarView.wantsLayer = true
touchBarView.layer?.backgroundColor = NSColor.clear.cgColor
touchBarView.allowedTouchTypes = .direct
touchBarView.delegate = gameScene
let custom = NSCustomTouchBarItem(identifier: identifier)
custom.view = touchBarView
return custom
}
}
fileprivate extension NSTouchBar.CustomizationIdentifier {
static let touchBar = NSTouchBar.CustomizationIdentifier.init(rawValue: "com.krayc.touchBreakoutBar")
}
fileprivate extension NSTouchBarItem.Identifier {
static let touchEvent = NSTouchBarItem.Identifier("com.krayc.touchEvent")
}