61 lines
1.5 KiB
Swift
61 lines
1.5 KiB
Swift
//
|
|
// 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()
|
|
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)
|
|
}
|
|
}
|
|
|
|
}
|