Tetris/Tetris Mac/TetrisHostingView.swift

63 lines
1.6 KiB
Swift
Raw Normal View History

2019-10-16 23:57:56 +00:00
//
// TetrisHostingView.swift
// Tetris Mac
//
// Created by Shadowfacts on 10/16/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import Cocoa
import SwiftUI
import Carbon
import TetrisKit
class TetrisHostingView: NSHostingView<ContentView> {
let gameController: GameController
override var acceptsFirstResponder: Bool {
return true
}
init(_ gameController: GameController) {
self.gameController = gameController
super.init(rootView: ContentView(controller: gameController))
}
required init(rootView: ContentView) {
fatalError("init(rootView:) has not been implemented")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func keyDown(with event: NSEvent) {
switch Int(event.keyCode) {
case kVK_LeftArrow:
gameController.left()
case kVK_RightArrow:
gameController.right()
case kVK_UpArrow:
gameController.rotate(direction: .clockwise)
case kVK_DownArrow:
gameController.drop()
2019-10-20 14:23:32 +00:00
case kVK_Space:
gameController.togglePause()
2019-10-16 23:57:56 +00:00
default:
super.keyDown(with: event)
}
}
override func flagsChanged(with event: NSEvent) {
if event.modifierFlags.contains(.control) {
gameController.rotate(direction: .counterClockwise)
} else if event.modifierFlags.contains(.shift) {
gameController.hold()
} else {
super.flagsChanged(with: event)
}
}
}