From 112bca57ce1de6d98d90751e6fc10d1c0d122ea3 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 5 Apr 2020 14:57:06 -0400 Subject: [PATCH] Fix keyboard input on Mac --- Tetris Mac/AppDelegate.swift | 8 +--- Tetris Mac/TetrisViewController.swift | 7 ++-- ...isHostingView.swift => TetrisWindow.swift} | 39 ++++++++----------- Tetris.xcodeproj/project.pbxproj | 8 ++-- 4 files changed, 25 insertions(+), 37 deletions(-) rename Tetris Mac/{TetrisHostingView.swift => TetrisWindow.swift} (57%) diff --git a/Tetris Mac/AppDelegate.swift b/Tetris Mac/AppDelegate.swift index 5914ee0..259d303 100644 --- a/Tetris Mac/AppDelegate.swift +++ b/Tetris Mac/AppDelegate.swift @@ -16,14 +16,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { // Create the window and set the content view. - window = NSWindow( - contentRect: NSRect(x: 0, y: 0, width: 634, height: 804), - styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], - backing: .buffered, defer: false) + window = TetrisWindow() window.center() - window.setFrameAutosaveName("Main Window") - window.contentViewController = TetrisViewController() - window.title = "Tetris" window.makeKeyAndOrderFront(nil) } diff --git a/Tetris Mac/TetrisViewController.swift b/Tetris Mac/TetrisViewController.swift index 83af39f..90e1204 100644 --- a/Tetris Mac/TetrisViewController.swift +++ b/Tetris Mac/TetrisViewController.swift @@ -13,9 +13,10 @@ import TetrisKit class TetrisViewController: NSViewController { - let gameController = GameController() + let gameController: GameController - init() { + init(gameController: GameController) { + self.gameController = gameController super.init(nibName: nil, bundle: nil) preferredContentSize = NSSize(width: 634, height: 804) } @@ -25,7 +26,7 @@ class TetrisViewController: NSViewController { } override func loadView() { - self.view = TetrisHostingView(gameController) + self.view = NSHostingView(rootView: ContentView(controller: gameController)) } override func viewDidAppear() { diff --git a/Tetris Mac/TetrisHostingView.swift b/Tetris Mac/TetrisWindow.swift similarity index 57% rename from Tetris Mac/TetrisHostingView.swift rename to Tetris Mac/TetrisWindow.swift index c2b78a3..d1494dd 100644 --- a/Tetris Mac/TetrisHostingView.swift +++ b/Tetris Mac/TetrisWindow.swift @@ -1,35 +1,28 @@ // -// TetrisHostingView.swift +// TetrisWindow.swift // Tetris Mac // -// Created by Shadowfacts on 10/16/19. -// Copyright © 2019 Shadowfacts. All rights reserved. +// Created by Shadowfacts on 4/5/20. +// Copyright © 2020 Shadowfacts. All rights reserved. // import Cocoa -import SwiftUI -import Carbon import TetrisKit +import Carbon -class TetrisHostingView: NSHostingView { - - let gameController: GameController +class TetrisWindow: NSWindow { - override var acceptsFirstResponder: Bool { - return true - } + let gameController = GameController() - 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") + init() { + super.init(contentRect: NSRect(x: 0, y: 0, width: 634, height: 804), + styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], + backing: .buffered, + defer: false) + + setFrameAutosaveName("Main Window") + contentViewController = TetrisViewController(gameController: gameController) + title = "Tetris" } override func keyDown(with event: NSEvent) { @@ -58,5 +51,5 @@ class TetrisHostingView: NSHostingView { super.flagsChanged(with: event) } } - + } diff --git a/Tetris.xcodeproj/project.pbxproj b/Tetris.xcodeproj/project.pbxproj index 8d61bda..fabb530 100644 --- a/Tetris.xcodeproj/project.pbxproj +++ b/Tetris.xcodeproj/project.pbxproj @@ -60,6 +60,7 @@ D608564723551602005BE4BC /* BoardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D608564523551602005BE4BC /* BoardView.swift */; }; D608564923551BAC005BE4BC /* GridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D608564823551BAC005BE4BC /* GridView.swift */; }; D608564A23551BAC005BE4BC /* GridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D608564823551BAC005BE4BC /* GridView.swift */; }; + D640E3E9243A5F15008F9972 /* TetrisWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = D640E3E8243A5F14008F9972 /* TetrisWindow.swift */; }; D6AA0AE6235D273E00D2FFE8 /* GamePieceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6AA0AE4235D272200D2FFE8 /* GamePieceView.swift */; }; D6AA0AE7235D273F00D2FFE8 /* GamePieceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6AA0AE4235D272200D2FFE8 /* GamePieceView.swift */; }; D6AA0AE8235D273F00D2FFE8 /* GamePieceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6AA0AE4235D272200D2FFE8 /* GamePieceView.swift */; }; @@ -93,7 +94,6 @@ D6E560722357D65700BF9ACF /* NextTetrominoesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DC0E9823579A4D008A0A98 /* NextTetrominoesView.swift */; }; D6E560732357D65700BF9ACF /* Tetromino+Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D84DF323567231002968FB /* Tetromino+Color.swift */; }; D6E560772357DF7D00BF9ACF /* TetrisViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6E560762357DF7D00BF9ACF /* TetrisViewController.swift */; }; - D6E560792357E1EC00BF9ACF /* TetrisHostingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6E560782357E1EC00BF9ACF /* TetrisHostingView.swift */; }; D6E5609C2358C50A00BF9ACF /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D6E5609B2358C50A00BF9ACF /* Media.xcassets */; }; D6E5609D2358C50A00BF9ACF /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D6E5609B2358C50A00BF9ACF /* Media.xcassets */; }; D6E5609E2358C50A00BF9ACF /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D6E5609B2358C50A00BF9ACF /* Media.xcassets */; }; @@ -305,6 +305,7 @@ D608562F23551269005BE4BC /* TilesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TilesView.swift; sourceTree = ""; }; D608564523551602005BE4BC /* BoardView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardView.swift; sourceTree = ""; }; D608564823551BAC005BE4BC /* GridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GridView.swift; sourceTree = ""; }; + D640E3E8243A5F14008F9972 /* TetrisWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TetrisWindow.swift; sourceTree = ""; }; D6AA0AE4235D272200D2FFE8 /* GamePieceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GamePieceView.swift; sourceTree = ""; }; D6D84DF323567231002968FB /* Tetromino+Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Tetromino+Color.swift"; sourceTree = ""; }; D6D84DF72356B8F0002968FB /* TetrominoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TetrominoView.swift; sourceTree = ""; }; @@ -321,7 +322,6 @@ D6E560482357D5D300BF9ACF /* TetrisKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TetrisKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D6E560602357D63E00BF9ACF /* TetrisUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TetrisUI.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D6E560762357DF7D00BF9ACF /* TetrisViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TetrisViewController.swift; sourceTree = ""; }; - D6E560782357E1EC00BF9ACF /* TetrisHostingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TetrisHostingView.swift; sourceTree = ""; }; D6E5609B2358C50A00BF9ACF /* Media.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Media.xcassets; sourceTree = ""; }; D6E5609F2358C68C00BF9ACF /* BundleHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BundleHelper.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -546,8 +546,8 @@ isa = PBXGroup; children = ( D6E560322357C8AF00BF9ACF /* AppDelegate.swift */, + D640E3E8243A5F14008F9972 /* TetrisWindow.swift */, D6E560762357DF7D00BF9ACF /* TetrisViewController.swift */, - D6E560782357E1EC00BF9ACF /* TetrisHostingView.swift */, D6E560342357C8AF00BF9ACF /* ContentView.swift */, D6E560362357C8B100BF9ACF /* Assets.xcassets */, D6E5603B2357C8B100BF9ACF /* Main.storyboard */, @@ -1082,9 +1082,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - D6E560792357E1EC00BF9ACF /* TetrisHostingView.swift in Sources */, D6E560352357C8AF00BF9ACF /* ContentView.swift in Sources */, D6E560332357C8AF00BF9ACF /* AppDelegate.swift in Sources */, + D640E3E9243A5F15008F9972 /* TetrisWindow.swift in Sources */, D6E560772357DF7D00BF9ACF /* TetrisViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0;