|
| 1 | +// |
| 2 | +// Solar2DSwiftUIView.swift |
| 3 | +// |
| 4 | + |
| 5 | +import Foundation |
| 6 | +import SwiftUI |
| 7 | + |
| 8 | + |
| 9 | +struct SafeSolar2DSwiftUIView: View { |
| 10 | + let coronaSdkFilePath: String |
| 11 | + init(coronaSdkFilePath: String) { |
| 12 | + self.coronaSdkFilePath = coronaSdkFilePath |
| 13 | + } |
| 14 | + |
| 15 | + var body: some View { |
| 16 | + Solar2DSwiftUIViewBody(delegate: Solar2DSwiftUIViewDelegate( |
| 17 | + coronaSdkFilePath: coronaSdkFilePath |
| 18 | + )) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fileprivate struct Solar2DSwiftUIViewBody: View { |
| 23 | + @ObservedObject var delegate: Solar2DSwiftUIViewDelegate |
| 24 | + |
| 25 | + init(delegate: Solar2DSwiftUIViewDelegate) { |
| 26 | + self._delegate = .init(wrappedValue: delegate) |
| 27 | + } |
| 28 | + |
| 29 | + var body: some View { |
| 30 | + GeometryReader { geo in |
| 31 | + delegate.view.onChange(of: geo.size) { (newSize: CGSize) in |
| 32 | + delegate.width = newSize.width |
| 33 | + delegate.height = newSize.height |
| 34 | + }.onAppear { |
| 35 | + delegate.width = geo.size.width |
| 36 | + delegate.height = geo.size.height |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +fileprivate final class Solar2DSwiftUIViewDelegate: NSObject, ObservableObject, CoronaViewDelegate { |
| 42 | + let id = UUID().uuidString |
| 43 | + func coronaViewDidResume(_ view: CoronaView!) {} |
| 44 | + func coronaViewWillSuspend(_ view: CoronaView!) {} |
| 45 | + func coronaView(_ view: CoronaView!, receiveEvent event: [AnyHashable : Any]!) -> Any! { |
| 46 | + return true |
| 47 | + } |
| 48 | + |
| 49 | + let coronaSdkFilePath: String |
| 50 | + @Published private var isRunning = true |
| 51 | + @Published var width: CGFloat = 200 |
| 52 | + @Published var height: CGFloat = 200 |
| 53 | + |
| 54 | + init(coronaSdkFilePath: String) { |
| 55 | + self.coronaSdkFilePath = coronaSdkFilePath |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + func start() { |
| 60 | + guard !isRunning else { return } |
| 61 | + isRunning = true |
| 62 | + } |
| 63 | + func kill() { |
| 64 | + guard isRunning else { return } |
| 65 | + isRunning = false |
| 66 | + } |
| 67 | + |
| 68 | + var view: LiveSolar2DView { |
| 69 | + .init(delegate: self) |
| 70 | + } |
| 71 | + |
| 72 | + struct LiveSolar2DView: View { |
| 73 | + @ObservedObject fileprivate var delegate: Solar2DSwiftUIViewDelegate |
| 74 | + |
| 75 | + var body: some View { |
| 76 | + if delegate.isRunning { |
| 77 | + Solar2DSwiftUIView(delegate: delegate) |
| 78 | + .frame(width: delegate.width, height: delegate.height) |
| 79 | + .animation(nil, value: delegate.width) |
| 80 | + .animation(nil, value: delegate.height) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +fileprivate struct Solar2DSwiftUIView: UIViewControllerRepresentable { |
| 88 | + let delegate: Solar2DSwiftUIViewDelegate |
| 89 | + func makeUIViewController(context: Context) -> CoronaViewController { |
| 90 | + let coronaController = CoronaViewController() |
| 91 | + let coronaView = (coronaController.view as! CoronaView) |
| 92 | + coronaView.backgroundColor = .clear |
| 93 | + coronaView.isOpaque = false |
| 94 | +// coronaView.frame = .init(x: 0, y: 0, width: sW, height: sH) |
| 95 | + coronaView.coronaViewDelegate = delegate |
| 96 | + coronaView.run(withPath: delegate.coronaSdkFilePath, parameters: [ |
| 97 | + : |
| 98 | +// "contentWidth":sW, |
| 99 | +// "contentHeight":sH |
| 100 | + ]) |
| 101 | + |
| 102 | + return coronaController |
| 103 | + } |
| 104 | + |
| 105 | + func updateUIViewController(_ uiViewController: CoronaViewController, context: Context) {} |
| 106 | + |
| 107 | + typealias UIViewControllerType = CoronaViewController |
| 108 | +} |
0 commit comments