|
| 1 | +// The Settings window: the launcher's options, on tabs. The main window |
| 2 | +// (Views.swift) stays small and only has what a player needs before Play. |
| 3 | +// |
| 4 | +// Adding an option: |
| 5 | +// 1. Game.swift: a Prefs key, and the value that gives the original game in |
| 6 | +// Prefs.originalGame (Restore Original Game walks that list). |
| 7 | +// 2. Model.swift: a @Published property (read in init, saved in didSet) and its |
| 8 | +// setenv(..., 0) in play(), so a variable set by hand still wins. |
| 9 | +// 3. Here: a SettingRow in its tab's view, inside a Card: |
| 10 | +// SettingRow(title: "Frame-rate counter", |
| 11 | +// description: "Frames per second in a corner. Off: as in the original game.", |
| 12 | +// isOn: $model.fpsCounter) |
| 13 | +// Other controls go in the closure: SettingRow(title: ..., description: ...) { Picker(...) }. |
| 14 | +// A switch's description ends with "Off: as in the original game."; a picker |
| 15 | +// labels the choice that gives the original game "(original)". |
| 16 | +// A Card holds any views, so it can also end with its own buttons: |
| 17 | +// Card("Data mods") { SettingRow(...) ...; HStack { Spacer(); Button("Add File…") { ... } } } |
| 18 | +// A tab with no rows is hidden: list it in SettingsTab.shown and give it a view in |
| 19 | +// SettingsView.page when its first row lands. |
| 20 | +import AppKit |
| 21 | +import SwiftUI |
| 22 | + |
| 23 | +enum SettingsTab: String, CaseIterable { |
| 24 | + case graphics, picture, controls, online, game, mods, more |
| 25 | + |
| 26 | + /// The tabs that have rows, in this order. |
| 27 | + static let shown: [SettingsTab] = [.graphics, .controls, .more] |
| 28 | + |
| 29 | + var title: String { rawValue.capitalized } |
| 30 | + var icon: String { // SF Symbols available on macOS 11 |
| 31 | + switch self { |
| 32 | + case .graphics: return "display" |
| 33 | + case .picture: return "camera.filters" |
| 34 | + case .controls: return "gamecontroller" |
| 35 | + case .online: return "network" |
| 36 | + case .game: return "car" |
| 37 | + case .mods: return "puzzlepiece" |
| 38 | + case .more: return "ellipsis.circle" |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// One option: title, dim description, the control on the right. |
| 44 | +struct SettingRow<Control: View>: View { |
| 45 | + let title: String |
| 46 | + let description: String |
| 47 | + let control: Control |
| 48 | + init(title: String, description: String = "", @ViewBuilder control: () -> Control) { |
| 49 | + self.title = title; self.description = description; self.control = control() |
| 50 | + } |
| 51 | + var body: some View { |
| 52 | + HStack(alignment: .center, spacing: 12) { |
| 53 | + VStack(alignment: .leading, spacing: 1) { |
| 54 | + Text(title).font(.system(size: 12.5)).foregroundColor(Theme.text) |
| 55 | + if !description.isEmpty { |
| 56 | + Text(description).font(.system(size: 11)).foregroundColor(Theme.dim) |
| 57 | + .fixedSize(horizontal: false, vertical: true) |
| 58 | + } |
| 59 | + } |
| 60 | + Spacer(minLength: 8) |
| 61 | + control |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +struct SettingSwitch: View { |
| 67 | + @Binding var isOn: Bool |
| 68 | + var body: some View { Toggle("", isOn: $isOn).labelsHidden().toggleStyle(SwitchToggleStyle()) } |
| 69 | +} |
| 70 | + |
| 71 | +extension SettingRow where Control == SettingSwitch { |
| 72 | + /// A switch. |
| 73 | + init(title: String, description: String = "", isOn: Binding<Bool>) { |
| 74 | + self.init(title: title, description: description) { SettingSwitch(isOn: isOn) } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// MARK: tabs |
| 79 | + |
| 80 | +struct GraphicsSettings: View { |
| 81 | + @ObservedObject var model: LauncherModel |
| 82 | + var body: some View { |
| 83 | + VStack(alignment: .leading, spacing: 12) { |
| 84 | + if model.canChooseEngine { |
| 85 | + Card("Engine") { |
| 86 | + Picker("", selection: $model.engine) { |
| 87 | + Text("Original").tag(LauncherModel.Engine.original) |
| 88 | + Text("HD").tag(LauncherModel.Engine.hd) |
| 89 | + } |
| 90 | + .pickerStyle(SegmentedPickerStyle()) |
| 91 | + .labelsHidden() |
| 92 | + .fixedSize() |
| 93 | + Text(model.engine == .hd |
| 94 | + ? "Anti-aliasing and ray-traced reflections on top of the game's own look. The Mac works harder and gets warmer." |
| 95 | + : "The game exactly as it looked on PC. Uses the least power.") |
| 96 | + .font(.system(size: 11)).foregroundColor(Theme.dim) |
| 97 | + .fixedSize(horizontal: false, vertical: true) |
| 98 | + } |
| 99 | + } |
| 100 | + Card("HUD") { |
| 101 | + SettingRow(title: "HUD at the screen edges", |
| 102 | + description: "On wide screens the minimap moves to the left and the gauges to the right. Off: the HUD stays in the 4:3 middle.", |
| 103 | + isOn: $model.wideHUD) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +struct ControlsSettings: View { |
| 110 | + @ObservedObject var model: LauncherModel |
| 111 | + var body: some View { |
| 112 | + VStack(alignment: .leading, spacing: 12) { |
| 113 | + Card("Controller") { |
| 114 | + SettingRow(title: "Use a game controller", |
| 115 | + description: "Xbox, PlayStation or MFi. Connect it before Play; buttons are set in the game's Controls menu.", |
| 116 | + isOn: $model.controller) |
| 117 | + SettingRow(title: "Stick dead zone", |
| 118 | + description: "How far the stick moves before the game reacts. Raise it for a stick that drifts.") { |
| 119 | + HStack(spacing: 8) { |
| 120 | + Slider(value: $model.stickDeadzone, in: 0...0.5, step: 0.01).frame(width: 140) |
| 121 | + Text("\(Int((model.stickDeadzone * 100).rounded())) %") |
| 122 | + .font(.system(size: 11.5).monospacedDigit()).foregroundColor(Theme.dim) |
| 123 | + .frame(width: 40, alignment: .trailing) |
| 124 | + } |
| 125 | + } |
| 126 | + .disabled(!model.controller).opacity(model.controller ? 1 : 0.5) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +struct MoreSettings: View { |
| 133 | + @ObservedObject var model: LauncherModel |
| 134 | + var body: some View { |
| 135 | + VStack(alignment: .leading, spacing: 12) { |
| 136 | + Card { |
| 137 | + SettingRow(title: "Portable copy", |
| 138 | + description: "One app with the game inside, for another Mac or a USB drive. Movies and language packs can be left out to make it smaller.") { |
| 139 | + Button("Make Copy…") { model.makePortable() } |
| 140 | + .buttonStyle(SecondaryButtonStyle()) |
| 141 | + .disabled(!model.isReady) |
| 142 | + } |
| 143 | + SettingRow(title: "Graphics settings in the game", |
| 144 | + description: "The game picks them for this Mac again on its next start, as on its first one. Career saves are not affected.") { |
| 145 | + Button("Reset…") { model.resetGraphics() }.buttonStyle(SecondaryButtonStyle()) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// MARK: window |
| 153 | + |
| 154 | +/// The selected tab. An ObservableObject rather than @State: with the command line |
| 155 | +/// tools' Swift 6.4 compiler @State is a macro whose plugin isn't there (pack.sh). |
| 156 | +final class SettingsSelection: ObservableObject { |
| 157 | + @Published var tab: SettingsTab |
| 158 | + init(_ tab: SettingsTab) { self.tab = tab } |
| 159 | +} |
| 160 | + |
| 161 | +struct SettingsView: View { |
| 162 | + static let size = CGSize(width: 560, height: 470) |
| 163 | + @ObservedObject var model: LauncherModel |
| 164 | + @ObservedObject private var selection: SettingsSelection |
| 165 | + private var tab: SettingsTab { selection.tab } |
| 166 | + |
| 167 | + init(model: LauncherModel, tab: SettingsTab = SettingsTab.shown[0]) { |
| 168 | + self.model = model |
| 169 | + self.selection = SettingsSelection(tab) |
| 170 | + } |
| 171 | + |
| 172 | + @ViewBuilder private var page: some View { |
| 173 | + switch tab { |
| 174 | + case .graphics: GraphicsSettings(model: model) |
| 175 | + case .controls: ControlsSettings(model: model) |
| 176 | + case .more: MoreSettings(model: model) |
| 177 | + case .picture, .online, .game, .mods: EmptyView() // no rows yet |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private var tabBar: some View { |
| 182 | + HStack(spacing: 4) { |
| 183 | + ForEach(SettingsTab.shown, id: \.self) { t in |
| 184 | + Button(action: { selection.tab = t }) { |
| 185 | + VStack(spacing: 3) { |
| 186 | + Image(systemName: t.icon).font(.system(size: 17)) |
| 187 | + .frame(height: 22) |
| 188 | + Text(t.title).font(.system(size: 11, weight: .medium)) |
| 189 | + } |
| 190 | + .foregroundColor(t == tab ? Theme.accent : Theme.dim) |
| 191 | + .frame(minWidth: 66).padding(.vertical, 6).padding(.horizontal, 4) |
| 192 | + .background(RoundedRectangle(cornerRadius: 7) |
| 193 | + .fill(Color.white.opacity(t == tab ? 0.08 : 0))) |
| 194 | + .contentShape(Rectangle()) |
| 195 | + } |
| 196 | + .buttonStyle(PlainButtonStyle()) |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + var body: some View { |
| 202 | + ZStack { |
| 203 | + Theme.background |
| 204 | + VStack(spacing: 0) { |
| 205 | + tabBar.padding(.top, 2).padding(.bottom, 10) |
| 206 | + Rectangle().fill(Theme.cardBorder).frame(height: 1) |
| 207 | + ScrollView { |
| 208 | + page.padding(.horizontal, 22).padding(.vertical, 14) |
| 209 | + } |
| 210 | + Rectangle().fill(Theme.cardBorder).frame(height: 1) |
| 211 | + HStack(alignment: .center) { |
| 212 | + if let n = model.notice { |
| 213 | + Text(n).font(.system(size: 11)).foregroundColor(Theme.accent) |
| 214 | + } else if model.isOriginalGame { |
| 215 | + Text("Every option is as in the original game.") |
| 216 | + .font(.system(size: 11)).foregroundColor(Theme.dim) |
| 217 | + } |
| 218 | + Spacer(minLength: 8) |
| 219 | + Button("Restore Original Game…") { model.confirmRestoreOriginalGame() } |
| 220 | + .buttonStyle(SecondaryButtonStyle()) |
| 221 | + .disabled(model.isOriginalGame) |
| 222 | + .help("Every option back to the value that gives the original, unmodified game.") |
| 223 | + } |
| 224 | + .padding(.horizontal, 22).padding(.vertical, 12) |
| 225 | + } |
| 226 | + if let j = model.job { JobOverlay(model: model, job: j) } |
| 227 | + } |
| 228 | + .frame(width: SettingsView.size.width, height: SettingsView.size.height) |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +/// The Settings window: same look as the main window, a fixed size; a tab |
| 233 | +/// taller than the window scrolls. |
| 234 | +func makeSettingsWindow(model: LauncherModel, tab: SettingsTab) -> NSWindow { |
| 235 | + let w = NSWindow(contentRect: NSRect(origin: .zero, size: SettingsView.size), |
| 236 | + styleMask: [.titled, .closable, .miniaturizable], |
| 237 | + backing: .buffered, defer: false) |
| 238 | + w.titlebarAppearsTransparent = true |
| 239 | + w.title = "Settings" |
| 240 | + w.isMovableByWindowBackground = true |
| 241 | + w.isReleasedWhenClosed = false |
| 242 | + w.appearance = NSAppearance(named: .darkAqua) |
| 243 | + w.backgroundColor = NSColor(calibratedRed: 0.067, green: 0.071, blue: 0.082, alpha: 1) |
| 244 | + w.contentView = NSHostingView(rootView: SettingsView(model: model, tab: tab)) |
| 245 | + w.center() |
| 246 | + return w |
| 247 | +} |
0 commit comments