Groundroom / app · groundroom.ai / Sources / GroundroomWalkthrough.swift
| 1 | #if os(visionOS) |
| 2 | import SwiftUI |
| 3 | import RealityKit |
| 4 | import Combine |
| 5 | |
| 6 | @MainActor |
| 7 | final class WalkthroughState: ObservableObject { |
| 8 | enum Phase: Equatable { case window, opening, immersive, closing } |
| 9 | @Published var phase = Phase.window |
| 10 | @Published var viewpoint = RoomLayout.Viewpoint.entrance |
| 11 | @Published var openError = false |
| 12 | } |
| 13 | |
| 14 | // Embedded by GroundroomApp on visionOS. |
| 15 | struct WalkthroughWindow: View { |
| 16 | @ObservedObject var state: WalkthroughState |
| 17 | @Environment(\.openImmersiveSpace) private var openSpace |
| 18 | @Environment(\.dismissImmersiveSpace) private var dismissSpace |
| 19 | |
| 20 | var body: some View { |
| 21 | VStack(alignment: .leading, spacing: 16) { |
| 22 | Text("Groundroom Walkthrough").font(.title2.bold()) |
| 23 | Text("Preview a sample room at life size with your surroundings visible. Stay in one comfortable spot and choose a viewpoint.") |
| 24 | .foregroundStyle(.secondary) |
| 25 | if state.phase == .immersive { |
| 26 | Picker("Viewpoint", selection: $state.viewpoint) { |
| 27 | ForEach(RoomLayout.Viewpoint.allCases) { view in Text(view.rawValue).tag(view) } |
| 28 | } |
| 29 | Button("Exit walkthrough", systemImage: "xmark") { |
| 30 | state.phase = .closing |
| 31 | Task { |
| 32 | await dismissSpace() |
| 33 | state.phase = .window |
| 34 | } |
| 35 | } |
| 36 | } else { |
| 37 | Button("Enter walkthrough", systemImage: "visionpro") { |
| 38 | state.openError = false |
| 39 | state.phase = .opening |
| 40 | state.viewpoint = .entrance |
| 41 | Task { |
| 42 | switch await openSpace(id: "groundroom-walkthrough") { |
| 43 | case .opened: state.phase = .immersive |
| 44 | case .userCancelled: state.phase = .window |
| 45 | default: |
| 46 | state.phase = .window |
| 47 | state.openError = true |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | .disabled(state.phase != .window) |
| 52 | } |
| 53 | if state.openError { Text("The walkthrough couldn’t open. Try again.").foregroundStyle(.secondary) } |
| 54 | } |
| 55 | .padding(32) |
| 56 | .frame(maxWidth: 560, alignment: .leading) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | struct WalkthroughRoom: View { |
| 61 | @ObservedObject var state: WalkthroughState |
| 62 | |
| 63 | var body: some View { |
| 64 | RealityView { content in |
| 65 | guard let room = try? RoomLayout.example.checked() else { return } |
| 66 | let root = Entity() |
| 67 | root.name = "groundroom-preview" |
| 68 | root.position = room.translation(for: state.viewpoint) |
| 69 | let width = Float(room.width), depth = Float(room.depth) |
| 70 | let tone = SimpleMaterial(color: .systemTeal, isMetallic: false) |
| 71 | for sign: Float in [-1, 1] { |
| 72 | let side = ModelEntity(mesh: .generateBox(size: [0.025, 0.025, depth]), materials: [tone]) |
| 73 | side.position = [sign * width / 2, 0.013, 0] |
| 74 | root.addChild(side) |
| 75 | let edge = ModelEntity(mesh: .generateBox(size: [width, 0.025, 0.025]), materials: [tone]) |
| 76 | edge.position = [0, 0.013, sign * depth / 2] |
| 77 | root.addChild(edge) |
| 78 | } |
| 79 | for (index, b) in room.blocks.enumerated() { |
| 80 | let material = SimpleMaterial(color: index == 1 ? .systemBrown : .systemGray, isMetallic: false) |
| 81 | let block = ModelEntity(mesh: .generateBox(size: [Float(b.width), Float(b.height), Float(b.depth)]), |
| 82 | materials: [material]) |
| 83 | block.position = [Float(b.x), Float(b.height / 2), Float(b.z)] |
| 84 | root.addChild(block) |
| 85 | } |
| 86 | content.add(root) |
| 87 | } update: { content in |
| 88 | if let root = content.entities.first(where: { $0.name == "groundroom-preview" }) { |
| 89 | root.position = RoomLayout.example.translation(for: state.viewpoint) |
| 90 | } |
| 91 | } |
| 92 | .onDisappear { state.phase = .window } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #endif |