Denis Andrašec 0d5c3e3b18
Spine iOS (#2504)
* Add `spine-iOS` SPM package & example app (#1)

* Basic Mesh Rendering (#2)

* Spine C++ Swift Wrapper (#3)

* Load `Atlas` & `SkeletonData` (#4)

Load & dispose `Atlas` & `SkeletonData` from bundled files.

* Generate Swift classes from `spine-cpp-lite.h` (#5)

* Draw `SkeletonData` render commands (#6)

- Use `SkeletonData` render commands in the renderer
- Simple loop for animation support

* Add `BoundsProvider` (#7)

- Implement & support `BoundsProvider` classes
- Introduce alignment and content mode
- Update c to swift script to return optional for find prefixed methods

* Support `SpineController` & `Event` callbacks (#8)

- Support SpineController callbacks
- Support Event callbacks
- Apply tint color in renderer

* Support `DressUp` sample (#9)

- Add `DressUp` sample
- Move SpineViewController to SpineUIView
- Implement SpineUIView export to image

* Remove unused file

* Add `Physics` sample (#10)

- Add `Physics` sample
- Fix offsets in `IKFollowing` sample
- Fix `SpineView` background color

* Add `DebugRendering` sample (#11)

- Add `DebugRendering` sample
- Make `SpineUIView` transparent

* Move remaining files to SPM package (#12)

- Move remaining files to SPM package
- Rename `SpineWrapper` to `SpineCppLite`

* Load assets from different sources (#13)

- Load from bundle, file, http & drawable
- Apply correct blend mode & pma in renderer

* Add `Obj-C` + `UIKit` sample (#14)

- Add `Obj-C` + `UIKit` sample
- Update `Spine` to be usable in Obj-C code base

* Support CocoaPods (#15)

* Metal Best Practices (#16)

- Tripple Buffering
- Buffer Bindings
- Shared Objects

* Annotate functions that should return optional (#17)

* Add option to disable drawing when out of viewport (#18)

- Add option to disable drawing when out of viewport
- Move update clock to controller so multiple views can share it

* Add docs for public Spine classes/methods (#19)

* Fix various regressions (#20)

- Fix retain `SpineController` retain cycle
- Fix issue wehre images were not rendered
2024-06-18 10:02:25 +02:00

77 lines
3.6 KiB
Swift

import SwiftUI
/// A `SwiftUI` `View` to display a Spine skeleton. The skeleton can be loaded from a bundle, local files, http, or a pre-loaded ``SkeletonDrawableWrapper``.
///
/// The skeleton displayed by a ``SpineUIView`` can be controlled via a ``SpineController``.
///
/// The size of the widget can be derived from the bounds provided by a ``BoundsProvider``. If the view is not sized by the bounds
/// computed by the ``BoundsProvider``, the widget will use the computed bounds to fit the skeleton inside the view's dimensions.
///
/// This is a ``UIViewRepresentable`` of `SpineUIView`.
public struct SpineView: UIViewRepresentable {
public typealias UIViewType = SpineUIView
private let source: SpineViewSource
private let controller: SpineController
private let mode: Spine.ContentMode
private let alignment: Spine.Alignment
private let boundsProvider: BoundsProvider
private let backgroundColor: UIColor // Not using `SwiftUI.Color`, as briging to `UIColor` prior iOS 14 might not always work.
@Binding
private var isRendering: Bool?
/// An initializer that constructs a new ``SpineView`` from a ``SpineViewSource``.
///
/// After initialization is complete, the provided `controller` is invoked as per the ``SpineController`` semantics, to allow
/// modifying how the skeleton inside the widget is animated and rendered.
///
/// - Parameters:
/// - from: Specifies the ``SpineViewSource`` from which to load `atlas` and `skeleton` data.
/// - controller: The ``SpineController`` used to modify how the skeleton inside the view is animated and rendered.
/// - skeletonFileName: Specifies either a Skeleton `.json` or `.skel` file containing the skeleton data
/// - bundle: Specifies from which bundle to load the files. Per default, it is `Bundle.main`
/// - mode: How the skeleton is fitted inside ``SpineUIView``. Per default, it is `.fit`
/// - alignment: How the skeleton is alignment inside ``SpineUIView``. Per default, it is `.center`
/// - boundsProvider: The skeleton bounds must be computed via a ``BoundsProvider``. Per default, ``SetupPoseBounds`` is used.
/// - backgroundColor: The background color of the view. Per defaut, `UIColor.clear` is used
/// - isRendering: Bindgin to disable or enable rendering. Disable it when the spine view is out of bounds and you want to preserve CPU/GPU resources.
///
/// - Returns: A new instance of ``SpineView``.
public init(
from source: SpineViewSource,
controller: SpineController = SpineController(),
mode: Spine.ContentMode = .fit,
alignment: Spine.Alignment = .center,
boundsProvider: BoundsProvider = SetupPoseBounds(),
backgroundColor: UIColor = .clear,
isRendering: Binding<Bool?> = .constant(nil)
) {
self.source = source
self.controller = controller
self.mode = mode
self.alignment = alignment
self.boundsProvider = boundsProvider
self.backgroundColor = backgroundColor
_isRendering = isRendering
}
public func makeUIView(context: Context) -> SpineUIView {
return SpineUIView(
from: source,
controller: controller,
mode: mode,
alignment: alignment,
boundsProvider: boundsProvider,
backgroundColor: backgroundColor
)
}
public func updateUIView(_ uiView: SpineUIView, context: Context) {
if let isRendering {
uiView.isRendering = isRendering
}
}
}