This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.swift
105 lines (87 loc) · 2.98 KB
/
Window.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Window.swift
// Of Witches and Mazes
//
// Created by Gustavo C. Viegas on 7/9/19.
// Copyright © 2019 Gustavo C. Viegas. All rights reserved.
//
import AppKit
/// A class responsible for applying changes and responding to events on the game window.
///
class Window {
/// A class defining the delegate associated with the window.
///
private class Delegate: NSObject, NSWindowDelegate {
/// The instance of the class.
///
static let instance = Delegate()
private override init() {
super.init()
}
func windowDidBecomeMain(_ notification: Notification) {
guard Window.fullscreenMode else { return }
NSApp.presentationOptions = [.hideMenuBar, .hideDock]
}
func windowDidResignMain(_ notification: Notification) {
NSApp.presentationOptions = []
}
func windowDidResignKey(_ notification: Notification) {
MouseInputManager.reset()
KeyboardInputManager.reset()
if SceneManager.currentScene is LevelScene {
let _ = SceneManager.switchToScene(ofKind: .pauseMenu)
}
}
func windowShouldClose(_ sender: NSWindow) -> Bool {
defer { NSApp.terminate(self) }
return true
}
func windowWillMiniaturize(_ notification: Notification) {
MouseInputManager.reset()
KeyboardInputManager.reset()
if SceneManager.currentScene is LevelScene {
let _ = SceneManager.switchToScene(ofKind: .pauseMenu)
}
}
func windowWillEnterFullScreen(_ notification: Notification) {
Window.fullscreenMode = true
NSApp.presentationOptions = [.hideMenuBar, .hideDock]
}
func windowWillExitFullScreen(_ notification: Notification) {
Window.fullscreenMode = false
NSApp.presentationOptions = []
}
}
/// The flag stating whether the window is in fullscreen mode.
///
private static var fullscreenMode = false
/// The flag stating whether the window is in windowed mode.
///
static var windowedMode: Bool {
return !fullscreenMode
}
/// The window.
///
/// - Note: The new window must not be in fullscreen mode.
///
static weak var window: NSWindow? {
didSet {
oldValue?.delegate = nil
window?.delegate = Delegate.instance
fullscreenMode = false
}
}
private init() {}
/// Enters fullscreen mode.
///
class func enterFullscreenMode() {
guard windowedMode, let window = window else { return }
window.toggleFullScreen(nil)
}
/// Exits fullscreen mode.
///
class func exitFullscreenMode() {
guard fullscreenMode, let window = window else { return }
window.toggleFullScreen(nil)
}
}