From e99ac63b661ca7f4216e391f3f7a2da0e1f2f188 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 25 Jul 2024 10:31:57 -0400 Subject: [PATCH] Support building for iOS 18 --- Package.swift | 1 + .../Components/AnchoringComponent.swift | 4 ++ .../EntityContentBuilder.swift | 6 +++ .../ARKitSession/ARKitSession+shared.swift | 2 + .../Entities/ARKitSession/HandTracking.swift | 2 + .../ARKitSession/SceneReconstruction.swift | 2 + .../RealityViewCameraControlsModifier.swift | 40 +++++++++++++++++++ .../LiveViewNativeRealityKit/RealityKit.swift | 31 ++++++++++++++ .../RealityView.swift | 10 +++++ ...RealityViewCamera+AttributeDecodable.swift | 31 ++++++++++++++ 10 files changed, 129 insertions(+) create mode 100644 Sources/LiveViewNativeRealityKit/Modifiers/RealityViewCameraControlsModifier.swift create mode 100644 Sources/LiveViewNativeRealityKit/Types/RealityViewCamera+AttributeDecodable.swift diff --git a/Package.swift b/Package.swift index 691fb18..7d5abc2 100644 --- a/Package.swift +++ b/Package.swift @@ -5,6 +5,7 @@ import PackageDescription let package = Package( name: "LiveViewNativeRealityKit", + platforms: [.visionOS(.v1), .iOS("18.0")], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. .library( diff --git a/Sources/LiveViewNativeRealityKit/Components/AnchoringComponent.swift b/Sources/LiveViewNativeRealityKit/Components/AnchoringComponent.swift index bcb8462..b5a8acc 100644 --- a/Sources/LiveViewNativeRealityKit/Components/AnchoringComponent.swift +++ b/Sources/LiveViewNativeRealityKit/Components/AnchoringComponent.swift @@ -40,6 +40,7 @@ extension AnchoringComponent.Target: AttributeDecodable { guard let value = attribute?.value else { throw AttributeDecodingError.missingAttribute(Self.self) } switch value { + #if os(visionOS) case "hand": self = .hand( try element.attributeValue(Chirality.self, for: "chirality"), @@ -47,6 +48,7 @@ extension AnchoringComponent.Target: AttributeDecodable { ) case "head": self = .head + #endif case "image": self = .image( group: try element.attributeValue(String.self, for: "group"), @@ -64,6 +66,7 @@ extension AnchoringComponent.Target: AttributeDecodable { } } +#if os(visionOS) extension AnchoringComponent.Target.Chirality: AttributeDecodable { public init(from attribute: Attribute?, on element: ElementNode) throws { guard let value = attribute?.value @@ -99,6 +102,7 @@ extension AnchoringComponent.Target.HandLocation: AttributeDecodable { } } } +#endif extension AnchoringComponent.Target.Alignment: AttributeDecodable { public init(from attribute: Attribute?, on element: ElementNode) throws { diff --git a/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift b/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift index 4706de4..8ef418a 100644 --- a/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift +++ b/Sources/LiveViewNativeRealityKit/ContentBuilders/EntityContentBuilder.swift @@ -26,8 +26,10 @@ public struct EntityContentBuilder some View { + content.realityViewCameraControls(controls) + } +} + +extension CameraControls: ParseableModifierValue { + public static func parser(in context: ParseableModifierContext) -> some Parser { + ImplicitStaticMember([ + "dolly": .dolly, + "none": .none, + "orbit": .orbit, + "pan": .pan, + "tilt": .tilt, + ]) + } +} +#endif diff --git a/Sources/LiveViewNativeRealityKit/RealityKit.swift b/Sources/LiveViewNativeRealityKit/RealityKit.swift index 97eb896..879f229 100644 --- a/Sources/LiveViewNativeRealityKit/RealityKit.swift +++ b/Sources/LiveViewNativeRealityKit/RealityKit.swift @@ -1,6 +1,7 @@ import LiveViewNative import LiveViewNativeStylesheet import SwiftUI +import RealityKit public struct CustomizableRealityKitRegistry< Entities: EntityRegistry, @@ -17,6 +18,36 @@ public struct CustomizableRealityKitRegistry< _RealityView>() } } + + #if os(iOS) || os(macOS) + public static func parseModifier( + _ input: inout Substring.UTF8View, + in context: ParseableModifierContext + ) throws -> CustomModifier { + try CustomModifier.parser(in: context).parse(&input) + } + + public struct CustomModifier: ViewModifier, ParseableModifierValue { + enum Storage { + case realityViewCameraControls(_RealityViewCameraControlsModifier) + } + + let storage: Storage + + public static func parser(in context: ParseableModifierContext) -> some Parser { + CustomModifierGroupParser(output: Self.self) { + _RealityViewCameraControlsModifier.parser(in: context).map({ Self(storage: .realityViewCameraControls($0)) }) + } + } + + public func body(content: Content) -> some View { + switch storage { + case .realityViewCameraControls(let modifier): + content.modifier(modifier) + } + } + } + #endif } } diff --git a/Sources/LiveViewNativeRealityKit/RealityView.swift b/Sources/LiveViewNativeRealityKit/RealityView.swift index 1f80d51..d4455a6 100644 --- a/Sources/LiveViewNativeRealityKit/RealityView.swift +++ b/Sources/LiveViewNativeRealityKit/RealityView.swift @@ -80,6 +80,8 @@ struct _RealityView.Storage() @@ -106,6 +108,10 @@ struct _RealityView