import SwiftUI struct LocationPlanCanvas: View { let width: Int let depth: Int let locations: [LayoutLocation] let selected: UUID? let editing: Bool let open: (UUID) -> Void var change: ((UUID, LocationGeometry) -> Void)? = nil var body: some View { GeometryReader { proxy in let scale = max(0.001, min((proxy.size.width - 20) / CGFloat(width), (proxy.size.height - 20) / CGFloat(depth))) let board = CGSize(width: CGFloat(width) * scale, height: CGFloat(depth) * scale) ZStack { RoundedRectangle(cornerRadius: 8).fill(Color(uiColor: .secondarySystemGroupedBackground)) ZStack(alignment: .topLeading) { PlanGrid() ForEach(locations) { place in if let geometry = place.geometry { PlanTile(place: place, geometry: geometry, scale: scale, selected: selected == place.id, editing: editing, open: { open(place.id) }, change: { change?(place.id, $0) }) .zIndex(geometry.template.kind == .furniture ? (selected == place.id ? 3 : 2) : (selected == place.id ? 1 : 0)) } } } .frame(width: board.width, height: board.height) .overlay { Rectangle().strokeBorder(.secondary.opacity(0.2), lineWidth: 1).allowsHitTesting(false) } if locations.isEmpty { Text("摆放位置后会显示在这里") .font(.footnote).foregroundStyle(.secondary).allowsHitTesting(false) } } .clipped() } .frame(height: editing ? 330 : 275) .accessibilityElement(children: .contain) .accessibilityLabel("位置俯视图") .accessibilityIdentifier("location.floor.canvas") } } private struct PlanGrid: View { var body: some View { Canvas { context, size in var lines = Path() for x in stride(from: CGFloat(0), through: size.width, by: 14) { lines.move(to: CGPoint(x: x, y: 0)) lines.addLine(to: CGPoint(x: x, y: size.height)) } for y in stride(from: CGFloat(0), through: size.height, by: 14) { lines.move(to: CGPoint(x: 0, y: y)) lines.addLine(to: CGPoint(x: size.width, y: y)) } context.stroke(lines, with: .color(.secondary.opacity(0.13)), lineWidth: 1) } .allowsHitTesting(false) .accessibilityHidden(true) } } private struct PlanTile: View { let place: LayoutLocation let geometry: LocationGeometry let scale: CGFloat let selected: Bool let editing: Bool let open: () -> Void let change: (LocationGeometry) -> Void @GestureState private var move = CGSize.zero @GestureState private var resize = CGSize.zero private var width: CGFloat { max(4, CGFloat(geometry.widthMm) * scale + resize.width) } private var depth: CGFloat { max(4, CGFloat(geometry.depthMm) * scale + resize.height) } var body: some View { ZStack(alignment: .topLeading) { let tile = LocationFloorArt(template: geometry.template, rotation: geometry.rotation) .overlay(alignment: geometry.template.kind == .room ? .topLeading : .bottom) { if width > 29 && depth > 17 { Text(place.name) .font(.caption2.weight(.semibold)) .lineLimit(1) .padding(.horizontal, 4).padding(.vertical, 2) .background(.regularMaterial, in: .rect(cornerRadius: 4)) .padding(2) .allowsHitTesting(false) } } .frame(width: width, height: depth) .overlay { if selected { RoundedRectangle(cornerRadius: 4).stroke(Color.atlasBlue, lineWidth: 2) } } .contentShape(Rectangle()) if editing { tile .onTapGesture(perform: open) .highPriorityGesture(DragGesture(minimumDistance: 4, coordinateSpace: .global) .updating($move) { value, state, _ in state = value.translation } .onEnded { value in var edited = geometry edited.xMm += millimeters(value.translation.width) edited.yMm += millimeters(value.translation.height) change(edited) }) .accessibilityAddTraits(.isButton) .accessibilityAction { open() } } else { Button(action: open) { tile }.buttonStyle(.plain) } if selected && editing { Circle() .fill(Color.atlasBlue) .overlay { Circle().strokeBorder(.white, lineWidth: 2) } .frame(width: 15, height: 15) .frame(width: 40, height: 40) .contentShape(Rectangle()) .highPriorityGesture(DragGesture(minimumDistance: 3, coordinateSpace: .global) .updating($resize) { value, state, _ in state = value.translation } .onEnded { value in var edited = geometry edited.widthMm += millimeters(value.translation.width) edited.depthMm += millimeters(value.translation.height) change(edited) }) .position(x: width, y: depth) .accessibilityHidden(true) } } .frame(width: width, height: depth, alignment: .topLeading) .position(x: CGFloat(geometry.xMm) * scale + width / 2 + move.width, y: CGFloat(geometry.yMm) * scale + depth / 2 + move.height) .accessibilityLabel(place.name) } private func millimeters(_ points: CGFloat) -> Int { Int((points / scale / 100).rounded()) * 100 } } struct LocationFloorArt: View { let template: LocationTemplate let rotation: Int @Environment(\.colorScheme) private var scheme private struct Piece { let x: CGFloat let y: CGFloat let w: CGFloat let h: CGFloat let color: Color let radius: CGFloat init(_ x: CGFloat, _ y: CGFloat, _ w: CGFloat, _ h: CGFloat, _ color: Color, _ radius: CGFloat = 5) { self.x = x; self.y = y; self.w = w; self.h = h self.color = color; self.radius = radius } } private let sage = Color(red: 0.55, green: 0.66, blue: 0.54) private let pale = Color(red: 0.77, green: 0.82, blue: 0.64) private let tan = Color(red: 0.85, green: 0.69, blue: 0.53) private let cream = Color(red: 0.97, green: 0.91, blue: 0.78) private let wood = Color(red: 0.65, green: 0.43, blue: 0.34) private let slate = Color(red: 0.33, green: 0.40, blue: 0.47) var body: some View { if template.kind == .furniture { GeometryReader { proxy in let sideways = rotation % 180 != 0 let width = sideways ? proxy.size.height : proxy.size.width let height = sideways ? proxy.size.width : proxy.size.height furnishing.frame(width: width, height: height) .rotationEffect(.degrees(Double(rotation))) .position(x: proxy.size.width / 2, y: proxy.size.height / 2) } } else { room } } private var room: some View { let tint = template == .bathroom || template == .kitchen || template == .balcony ? Color.teal : Color.brown return RoundedRectangle(cornerRadius: 4) .fill(tint.opacity(scheme == .dark ? 0.26 : 0.16)) .overlay { RoundedRectangle(cornerRadius: 4) .strokeBorder(tint.opacity(0.76), style: StrokeStyle(lineWidth: template.kind == .room ? 1.6 : 1, dash: template.kind == .area ? [4, 3] : [])) } } private var furnishing: some View { Canvas { context, size in let shapes = pieces let left = (shapes.map(\.x).min() ?? 0) - 2 let top = (shapes.map(\.y).min() ?? 0) - 2 let right = (shapes.map { $0.x + $0.w }.max() ?? 100) + 2 let bottom = (shapes.map { $0.y + $0.h }.max() ?? 70) + 2 let sx = size.width / (right - left), sy = size.height / (bottom - top) for part in shapes { let rect = CGRect(x: (part.x - left) * sx, y: (part.y - top) * sy, width: part.w * sx, height: part.h * sy) let path = Path(roundedRect: rect, cornerRadius: part.radius * min(sx, sy)) context.fill(path, with: .color(part.color)) context.stroke(path, with: .color(.black.opacity(0.4)), lineWidth: min(1.3, max(0.45, min(sx, sy)))) } } .allowsHitTesting(false) } private var pieces: [Piece] { switch template { case .sofa: [.init(4,6,105,39,sage,10), .init(74,30,35,38,sage,9), .init(15,16,25,20,pale), .init(41,16,25,20,pale), .init(67,16,25,20,pale), .init(80,37,22,23,pale), .init(97,13,12,48,sage)] case .bed: [.init(17,4,74,86,tan,8), .init(21,9,66,12,wood), .init(24,16,28,17,cream), .init(56,16,28,17,cream), .init(22,35,64,49,cream,7), .init(22,65,64,19,sage)] case .tvStand: [.init(5,12,106,47,tan,7), .init(32,22,51,9,slate), .init(11,11,94,14,slate), .init(12,39,28,13,cream), .init(44,39,28,13,cream), .init(76,39,27,13,cream)] case .wardrobe: [.init(5,10,104,48,wood), .init(10,15,22,38,tan), .init(34,15,22,38,tan), .init(58,15,22,38,tan), .init(82,15,22,38,tan), .init(26,29,3,10,slate), .init(39,29,3,10,slate), .init(74,29,3,10,slate), .init(86,29,3,10,slate)] case .bookcase: [.init(6,7,102,49,wood), .init(13,13,88,36,cream), .init(41,13,4,36,wood,2), .init(71,13,4,36,wood,2), .init(19,23,8,23,sage), .init(28,26,9,20,slate), .init(49,17,8,29,wood), .init(58,23,8,23,sage), .init(82,22,13,24,slate)] case .shelf: [.init(5,12,107,46,sage), .init(12,18,21,34,pale), .init(36,18,21,34,pale), .init(60,18,21,34,pale), .init(84,18,21,34,pale)] case .desk: [.init(5,10,106,55,wood,7), .init(10,14,96,44,tan), .init(25,22,22,24,cream), .init(62,22,31,20,slate), .init(72,42,11,7,slate)] case .table: [.init(3,3,58,58,wood,29), .init(6,5,52,52,tan,26)] default: [.init(20,7,66,60,tan,9), .init(22,9,62,20,cream,7), .init(43,45,20,8,wood,4)] } } }