spine-runtimes/spine-ios/Sources/Spine/BoundsProvider.swift
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

158 lines
5.4 KiB
Swift

import Foundation
import CoreGraphics
/// Base class for bounds providers. A bounds provider calculates the axis aligned bounding box
/// used to scale and fit a skeleton inside the bounds of a ``SpineUIView``.
@objc(SpineBoundsProvider)
public protocol BoundsProvider {
func computeBounds(for drawable: SkeletonDrawableWrapper) -> CGRect
}
/// A ``BoundsProvider`` that calculates the bounding box of the skeleton based on the visible
/// attachments in the setup pose.
@objc(SpineSetupPoseBounds)
@objcMembers
public final class SetupPoseBounds: NSObject, BoundsProvider {
public override init() {
super.init()
}
public func computeBounds(for drawable: SkeletonDrawableWrapper) -> CGRect {
return CGRect(bounds: drawable.skeleton.bounds)
}
}
/// A ``BoundsProvider`` that returns fixed bounds.
@objc(SpineRawBounds)
@objcMembers
public final class RawBounds: NSObject, BoundsProvider {
public let x: Double
public let y: Double
public let width: Double
public let height: Double
public init(x: Double, y: Double, width: Double, height: Double) {
self.x = x
self.y = y
self.width = width
self.height = height
super.init()
}
public func computeBounds(for drawable: SkeletonDrawableWrapper) -> CGRect {
return CGRectMake(CGFloat(x), CGFloat(y), CGFloat(width), CGFloat(height))
}
}
/// A ``BoundsProvider`` that calculates the bounding box needed for a combination of skins
/// and an animation.
@objc(SpineSkinAndAnimationBounds)
@objcMembers
public final class SkinAndAnimationBounds: NSObject, BoundsProvider {
private let animation: String?
private let skins: [String]
private let stepTime: TimeInterval;
/// Constructs a new provider that will use the given `skins` and `animation` to calculate
/// the bounding box of the skeleton. If no skins are given, the default skin is used.
/// The `stepTime`, given in seconds, defines at what interval the bounds should be sampled
/// across the entire animation.
public init(animation: String? = nil, skins: [String]? = nil, let stepTime: TimeInterval = 0.1) {
self.animation = animation
if let skins, !skins.isEmpty {
self.skins = skins
} else {
self.skins = ["default"]
}
self.stepTime = stepTime
super.init()
}
public func computeBounds(for drawable: SkeletonDrawableWrapper) -> CGRect {
let data = drawable.skeletonData
let oldSkin: Skin? = drawable.skeleton.skin
let customSkin = Skin.create(name: "custom-skin")
for skinName in skins {
let skin = data.findSkin(name: skinName)
if let skin = data.findSkin(name: skinName) {
customSkin.addSkin(other: skin)
}
}
drawable.skeleton.skin = customSkin
drawable.skeleton.setToSetupPose();
let animation = animation.flatMap { data.findAnimation(name: $0) }
var minX = Float.Magnitude.greatestFiniteMagnitude
var minY = Float.Magnitude.greatestFiniteMagnitude
var maxX = -Float.Magnitude.greatestFiniteMagnitude
var maxY = -Float.Magnitude.greatestFiniteMagnitude
if let animation {
drawable.animationState.setAnimation(trackIndex: 0, animation: animation, loop: false)
let steps = Int(max(Double(animation.duration) / stepTime, 1.0))
for i in 0..<steps {
drawable.update(delta: i > 0 ? Float(stepTime) : 0.0)
let bounds = drawable.skeleton.bounds;
minX = min(minX, bounds.x)
minY = min(minY, bounds.y)
maxX = max(maxX, minX + bounds.width)
maxY = max(maxY, minY + bounds.height)
}
} else {
let bounds = drawable.skeleton.bounds;
minX = bounds.x
minY = bounds.y
maxX = minX + bounds.width
maxY = minY + bounds.height
}
drawable.skeleton.setSkinByName(skinName: "default")
drawable.animationState.clearTracks()
if let oldSkin {
drawable.skeleton.skin = oldSkin
}
drawable.skeleton.setToSetupPose()
drawable.update(delta: 0)
customSkin.dispose()
return CGRectMake(CGFloat(minX), CGFloat(minY), CGFloat(maxX - minX), CGFloat(maxY - minY))
}
}
/// How a view should be inscribed into another view.
@objc
public enum ContentMode: Int {
case fit /// As large as possible while still containing the source view entirely within the target view.
case fill /// Fill the target view by distorting the source's aspect ratio.
}
/// How a view should aligned withing another view.
@objc
public enum Alignment: Int {
case topLeft
case topCenter
case topRight
case centerLeft
case center
case centerRight
case bottomLeft
case bottomCenter
case bottomRight
internal var x: CGFloat {
switch self {
case .topLeft, .centerLeft, .bottomLeft: return -1.0
case .topCenter, .center, .bottomCenter: return 0.0
case .topRight, .centerRight, .bottomRight: return 1.0
}
}
internal var y: CGFloat {
switch self {
case .topLeft, .topCenter, .topRight: return -1.0
case .centerLeft, .center, .centerRight: return 0.0
case .bottomLeft, .bottomCenter, .bottomRight: return -1.0
}
}
}