Repository Public License
1// ORIX Arcade2// File: ArcadeCore/GameChrome.swift3// Description: Shared in-game UI — scene host, pause button and menu with auto-pause, HUD pills, coach marks and the results card.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import SpriteKit910/// Hosts a SpriteKit scene edge to edge and forwards the safe-area insets to it.11struct GameSceneView: View {12 let scene: SKScene13 var isPaused: Bool1415 var body: some View {16 GeometryReader { geo in17 SpriteView(scene: scene, isPaused: isPaused,18 preferredFramesPerSecond: ProcessInfo.processInfo.isLowPowerModeEnabled ? 60 : 120,19 options: [.ignoresSiblingOrder])20 .ignoresSafeArea()21 .onAppear { push(geo.safeAreaInsets) }22 .onChange(of: geo.safeAreaInsets) { _, insets in push(insets) }23 }24 .accessibilityHidden(true)25 }2627 private func push(_ insets: EdgeInsets) {28 (scene as? ArcadeScene)?.safeInsets = UIEdgeInsets(top: insets.top, left: insets.leading, bottom: insets.bottom, right: insets.trailing)29 }30}3132/// Wraps a playing screen: pause button in the top corner, pause menu, and automatic pause when the app leaves the foreground.33///34/// The pause button occupies a 60 pt square in the top-trailing corner (top-leading in left-handed mode) — keep HUD content clear of it.35struct GameChrome<Content: View>: View {36 let theme: GameTheme37 let title: String38 @Binding var isPaused: Bool39 /// False while a result or menu is on screen, so the pause button hides and auto-pause is skipped.40 var canPause = true41 var pauseDetail: String?42 let onRestart: () -> Void43 let onExit: () -> Void44 @ViewBuilder var content: () -> Content4546 @Environment(ArcadeServices.self) private var services47 @Environment(\.scenePhase) private var scenePhase4849 var body: some View {50 ZStack {51 content()52 if canPause && !isPaused {53 VStack {54 HStack {55 if !services.settings.leftHanded { Spacer() }56 OrixIconButton(systemImage: "pause.fill", label: "Pause") { isPaused = true }57 .accessibilityIdentifier("pauseButton")58 if services.settings.leftHanded { Spacer() }59 }60 Spacer()61 }62 .padding(.horizontal, OrixSpace.sm)63 .padding(.top, 6)64 }65 if isPaused {66 PauseMenu(theme: theme, title: title, detail: pauseDetail,67 onResume: { isPaused = false },68 onRestart: { isPaused = false; onRestart() },69 onExit: onExit)70 .transition(.opacity)71 }72 }73 .animation(OrixMotion.decorative(OrixMotion.snappy), value: isPaused)74 .onChange(of: scenePhase) { _, phase in75 if phase != .active, canPause { isPaused = true }76 }77 }78}7980struct PauseMenu: View {81 let theme: GameTheme82 let title: String83 var detail: String?84 let onResume: () -> Void85 let onRestart: () -> Void86 let onExit: () -> Void87 @Environment(ArcadeServices.self) private var services8889 var body: some View {90 ZStack {91 Color.black.opacity(0.62).ignoresSafeArea()92 VStack(spacing: OrixSpace.md) {93 VStack(spacing: 4) {94 Text("PAUSED").font(OrixFont.overline).tracking(3).foregroundStyle(theme.primary)95 Text(title).font(OrixFont.title).foregroundStyle(OrixColor.textPrimary)96 if let detail { Text(detail).font(OrixFont.caption).foregroundStyle(OrixColor.textSecondary) }97 }98 .padding(.bottom, OrixSpace.xs)99 OrixButton(title: "Resume", systemImage: "play.fill", theme: theme, action: onResume)100 .accessibilityIdentifier("resumeButton")101 OrixButton(title: "Restart", systemImage: "arrow.counterclockwise", kind: .secondary, action: onRestart)102 .accessibilityIdentifier("restartButton")103 HStack(spacing: OrixSpace.sm) {104 quickToggle("Music", on: "music.note", off: "music.note.slash", isOn: services.settings.musicOn) { value in105 services.updateSettings { $0.musicOn = value }106 }107 quickToggle("Sound", on: "speaker.wave.2.fill", off: "speaker.slash.fill", isOn: services.settings.sfxOn) { value in108 services.updateSettings { $0.sfxOn = value }109 }110 quickToggle("Haptics", on: "iphone.radiowaves.left.and.right", off: "iphone.slash", isOn: services.settings.hapticsOn) { value in111 services.updateSettings { $0.hapticsOn = value }112 }113 }114 OrixButton(title: "Quit to Arcade", systemImage: "square.grid.2x2.fill", kind: .destructive, action: onExit)115 .accessibilityIdentifier("quitButton")116 }117 .frame(maxWidth: 340)118 .glassPanel(radius: OrixRadius.xl, tint: theme.primary, padding: OrixSpace.lg)119 .padding(OrixSpace.lg)120 }121 .accessibilityAddTraits(.isModal)122 }123124 private func quickToggle(_ label: String, on: String, off: String, isOn: Bool, set: @escaping (Bool) -> Void) -> some View {125 Button {126 set(!isOn)127 services.feedbackTap()128 } label: {129 VStack(spacing: 4) {130 Image(systemName: isOn ? on : off).font(.system(size: 17, weight: .bold))131 Text(label).font(OrixFont.overline)132 }133 .foregroundStyle(isOn ? theme.primary : OrixColor.textTertiary)134 .frame(maxWidth: .infinity, minHeight: 56)135 .background(Color.white.opacity(isOn ? 0.10 : 0.04), in: RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous))136 .overlay(RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous).strokeBorder(OrixColor.stroke, lineWidth: 1))137 }138 .buttonStyle(OrixPressStyle())139 .accessibilityLabel(label)140 .accessibilityValue(isOn ? "On" : "Off")141 }142}143144/// Compact HUD readout, e.g. score, fuel, streak.145struct HUDPill: View {146 var systemImage: String?147 let text: String148 var tint: Color = OrixColor.textPrimary149 var label: String?150151 var body: some View {152 HStack(spacing: 6) {153 if let systemImage { Image(systemName: systemImage).font(.system(size: 13, weight: .black)).foregroundStyle(tint) }154 Text(text).font(OrixFont.hud(17)).foregroundStyle(OrixColor.textPrimary).contentTransition(.numericText())155 }156 .padding(.horizontal, 12)157 .frame(minHeight: 36)158 .background(Color.black.opacity(0.5), in: Capsule())159 .overlay(Capsule().strokeBorder(tint.opacity(0.35), lineWidth: 1))160 .accessibilityElement(children: .ignore)161 .accessibilityLabel(label.map { "\($0): \(text)" } ?? text)162 }163}164165/// Horizontal gauge for fuel, heat, hull and similar resources.166struct HUDGauge: View {167 let systemImage: String168 let fraction: Double169 var tint: Color170 var label: String171 var width: CGFloat = 92172173 var body: some View {174 HStack(spacing: 6) {175 Image(systemName: systemImage).font(.system(size: 12, weight: .black)).foregroundStyle(tint)176 ZStack(alignment: .leading) {177 Capsule().fill(Color.white.opacity(0.14))178 Capsule().fill(tint).frame(width: max(4, width * min(max(fraction, 0), 1)))179 }180 .frame(width: width, height: 8)181 }182 .padding(.horizontal, 10)183 .frame(minHeight: 30)184 .background(Color.black.opacity(0.5), in: Capsule())185 .accessibilityElement(children: .ignore)186 .accessibilityLabel(label)187 .accessibilityValue("\(Int(min(max(fraction, 0), 1) * 100)) percent")188 }189}190191/// Tutorial hint with a gently bobbing icon. Games decide when to show it.192struct CoachMark: View {193 let text: String194 var systemImage = "hand.point.up.left.fill"195 var theme: GameTheme = .orix196 @State private var bob = false197198 var body: some View {199 HStack(spacing: OrixSpace.sm) {200 Image(systemName: systemImage)201 .font(.system(size: 22, weight: .bold))202 .foregroundStyle(theme.primary)203 .offset(y: bob ? -4 : 4)204 Text(text).font(OrixFont.callout).foregroundStyle(OrixColor.textPrimary).multilineTextAlignment(.leading)205 }206 .padding(.horizontal, OrixSpace.md)207 .padding(.vertical, OrixSpace.sm)208 .background(Color.black.opacity(0.72), in: RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous))209 .overlay(RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous).strokeBorder(theme.primary.opacity(0.5), lineWidth: 1))210 .padding(.horizontal, OrixSpace.lg)211 .allowsHitTesting(false)212 .onAppear {213 guard !OrixAppearance.reduceMotion else { return }214 withAnimation(.easeInOut(duration: 0.7).repeatForever(autoreverses: true)) { bob = true }215 }216 }217}218219struct ResultRow: Identifiable {220 let id = UUID()221 let label: String222 let value: String223 var systemImage: String?224 var highlight = false225}226227/// End-of-run recap used by all three games: title, optional stars, counted-up score, stat rows and actions.228struct ResultsCard: View {229 let theme: GameTheme230 let title: String231 var subtitle: String?232 var stars: Int?233 let score: Int234 var scoreLabel = "SCORE"235 var isNewRecord = false236 var success = true237 var rows: [ResultRow] = []238 let primaryTitle: String239 var primaryIcon: String? = "arrow.counterclockwise"240 let onPrimary: () -> Void241 var secondaryTitle: String?242 var secondaryIcon: String?243 var onSecondary: (() -> Void)?244 let onExit: () -> Void245246 @State private var shownScore: Double = 0247 @State private var appeared = false248 @Environment(ArcadeServices.self) private var services249250 var body: some View {251 ZStack {252 Color.black.opacity(0.66).ignoresSafeArea()253 if isNewRecord || (stars ?? 0) == 3 { ConfettiBurst() }254 VStack(spacing: OrixSpace.md) {255 VStack(spacing: 6) {256 if isNewRecord {257 OrixChip(text: "NEW RECORD", systemImage: "trophy.fill", tint: OrixColor.gold, filled: true)258 .accessibilityIdentifier("newRecordChip")259 }260 Text(title).font(OrixFont.display).foregroundStyle(success ? AnyShapeStyle(theme.gradient) : AnyShapeStyle(OrixColor.textPrimary))261 .multilineTextAlignment(.center).minimumScaleFactor(0.6).lineLimit(2)262 .accessibilityIdentifier("resultsTitle")263 if let subtitle { Text(subtitle).font(OrixFont.callout).foregroundStyle(OrixColor.textSecondary).multilineTextAlignment(.center) }264 }265 if let stars { StarRow(earned: stars) }266 VStack(spacing: 0) {267 Text(scoreLabel).font(OrixFont.overline).tracking(2.4).foregroundStyle(OrixColor.textTertiary)268 CountingNumber(value: shownScore)269 .accessibilityLabel("\(scoreLabel.capitalized) \(score)")270 }271 if !rows.isEmpty {272 VStack(spacing: 0) {273 ForEach(Array(rows.enumerated()), id: \.element.id) { index, row in274 HStack(spacing: OrixSpace.xs) {275 if let icon = row.systemImage {276 Image(systemName: icon).font(.system(size: 13, weight: .bold)).foregroundStyle(theme.primary).frame(width: 20)277 }278 Text(row.label).font(OrixFont.callout).foregroundStyle(OrixColor.textSecondary)279 Spacer()280 Text(row.value).font(OrixFont.callout.monospacedDigit().weight(.heavy))281 .foregroundStyle(row.highlight ? OrixColor.gold : OrixColor.textPrimary)282 }283 .padding(.vertical, 9)284 .accessibilityElement(children: .combine)285 if index < rows.count - 1 { Divider().overlay(OrixColor.stroke) }286 }287 }288 .padding(.horizontal, OrixSpace.sm)289 .background(Color.white.opacity(0.05), in: RoundedRectangle(cornerRadius: OrixRadius.md, style: .continuous))290 }291 VStack(spacing: OrixSpace.xs) {292 OrixButton(title: primaryTitle, systemImage: primaryIcon, theme: theme, action: onPrimary)293 .accessibilityIdentifier("resultsPrimary")294 HStack(spacing: OrixSpace.xs) {295 if let secondaryTitle, let onSecondary {296 OrixButton(title: secondaryTitle, systemImage: secondaryIcon, kind: .secondary, action: onSecondary)297 .accessibilityIdentifier("resultsSecondary")298 }299 OrixButton(title: "Arcade", systemImage: "square.grid.2x2.fill", kind: .secondary, action: onExit)300 .accessibilityIdentifier("resultsExit")301 }302 }303 }304 .frame(maxWidth: 360)305 .glassPanel(radius: OrixRadius.xl, tint: theme.primary, padding: OrixSpace.lg)306 .padding(OrixSpace.md)307 .scaleEffect(appeared ? 1 : 0.9)308 .opacity(appeared ? 1 : 0)309 }310 .accessibilityAddTraits(.isModal)311 .onAppear {312 withAnimation(OrixMotion.decorative(OrixMotion.bouncy)) { appeared = true }313 if OrixAppearance.reduceMotion {314 shownScore = Double(score)315 } else {316 withAnimation(.easeOut(duration: 0.9).delay(0.2)) { shownScore = Double(score) }317 }318 if isNewRecord {319 services.audio.play(.record)320 services.haptics.play(.success)321 }322 }323 }324}325