-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MultipleWindows.swift
95 lines (81 loc) · 4.26 KB
/
MultipleWindows.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
/*
* Copyright © 2023-2024 Dustin Collins (Strega's Gate)
* All Rights Reserved.
*
* http://stregasgate.com
*/
import GateEngine
@main
final class MultipleWindowsGameDelegate: GameDelegate {
/// didFinishLaunching() is executed immediatley after the game is ready to start
func didFinishLaunching(game: Game, options: LaunchOptions) async {
game.insertSystem(SomeRenderingSystem.self)
}
/// This GameDelegate func allows you to change the options and style for the main window
func createMainWindow(game: Game, identifier: String) throws -> Window {
return try game.windowManager.createWindow(identifier: identifier, style: .bestForGames, options: [])
}
/// This GameDelegate func allows you to provide a window on platfroms where a user can manually create one.
func createUserRequestedWindow(game: Game) throws -> Window? {
let id = userWindowNumber.generateID()
let window = try game.windowManager.createWindow(identifier: "user\(id)")
window.title = "User Create Window #\(id)"
window.clearColor = .lightBlue
return window
}
let userWindowNumber = IDGenerator<UInt>()
/// This GameDelegate func allows you to provide a window to an attached display,
/// such as an AirPlay screen when using mirroring on an iOS device.
@MainActor func createWindowForExternalscreen(game: Game) throws -> Window? {
let id = screenWindowNumber.generateID()
let window = try game.windowManager.createWindow(identifier: "external\(id)")
window.title = "External Window #\(id)"
window.clearColor = .lightGreen
return window
}
let screenWindowNumber = IDGenerator<UInt>()
}
// RenderingSystem subclasses can draw content
// However, updating the simulation from a RenderingSystem is a programming error
// GateEngine allows for frame drops and headless execution for servers
// In these cases RenderingSystems do not get updated
class SomeRenderingSystem: RenderingSystem {
override func setup(game: Game) {
// Windows can only be create in a `RenderingSystem` or a designated `GameDelegate` func.
let window1 = game.windowManager.mainWindow
window1?.title = "Main Window"
window1?.clearColor = .lightRed
do {
let window2 = try game.windowManager.createWindow(identifier: "window2")
window2.title = "Programatic Window #2"
window2.clearColor = .lightRed
let window3 = try game.windowManager.createWindow(identifier: "window3")
window3.title = "Programatic Window #3"
window3.clearColor = .lightRed
}catch{
print(error)
}
}
// render() is called only when drawing needs to be done
override func render(game: Game, window: Window, withTimePassed deltaTime: Float) {
var canvas = Canvas(window: window, estimatedCommandCount: 2)
// Draw something different in each window
switch window.identifier {
case "window2":
canvas.insert(Rect(0, 0, 100, 100), color: .yellow, at: Position2(window.pointSafeAreaInsets.leading, window.pointSafeAreaInsets.top))
case "window3":
canvas.insert(Rect(0, 0, 100, 100), color: .magenta, at: Position2(window.pointSafeAreaInsets.leading, window.pointSafeAreaInsets.top))
case "external1":
canvas.insert(Rect(0, 0, 100, 100), color: .purple, at: Position2(window.pointSafeAreaInsets.leading, window.pointSafeAreaInsets.top))
case "user1":
canvas.insert(Rect(0, 0, 100, 100), color: .red, at: Position2(window.pointSafeAreaInsets.leading, window.pointSafeAreaInsets.top))
case "user2":
canvas.insert(Rect(0, 0, 100, 100), color: .green, at: Position2(window.pointSafeAreaInsets.leading, window.pointSafeAreaInsets.top))
case "user3":
canvas.insert(Rect(0, 0, 100, 100), color: .blue, at: Position2(window.pointSafeAreaInsets.leading, window.pointSafeAreaInsets.top))
default:// Main Window and unhandled windows
canvas.insert(Rect(0, 0, 100, 100), color: .cyan, at: Position2(window.pointSafeAreaInsets.leading, window.pointSafeAreaInsets.top))
}
window.insert(canvas)
}
}