DebugRendering.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import SwiftUI
  2. import Spine
  3. struct DebugRendering: View {
  4. @StateObject
  5. var model = DebugRenderingModel()
  6. var body: some View {
  7. ZStack {
  8. Color.red.ignoresSafeArea()
  9. SpineView(
  10. from: .bundle(atlasFileName: "spineboy-pma.atlas", skeletonFileName: "spineboy-pro.skel"),
  11. controller: model.controller,
  12. mode: .fit,
  13. alignment: .center
  14. )
  15. ForEach(model.boneRects, id: \.id) { boneLocation in
  16. Rectangle()
  17. .fill(.blue)
  18. .offset(x: boneLocation.x, y: boneLocation.y)
  19. .frame(width: boneLocation.width, height: boneLocation.height)
  20. }
  21. }
  22. .navigationTitle("Debug Rendering")
  23. .navigationBarTitleDisplayMode(.inline)
  24. }
  25. }
  26. #Preview {
  27. DebugRendering()
  28. }
  29. final class DebugRenderingModel: ObservableObject {
  30. @Published
  31. var controller: SpineController!
  32. @Published
  33. var boneRects = [BoneRect]()
  34. init() {
  35. controller = SpineController(
  36. onInitialized: { controller in
  37. controller.animationState.setAnimationByName(
  38. trackIndex: 0,
  39. animationName: "walk",
  40. loop: true
  41. )
  42. },
  43. onAfterPaint: {
  44. [weak self] controller in guard let self else { return }
  45. boneRects = controller.drawable.skeleton.bones.map { bone in
  46. let position = controller.fromSkeletonCoordinates(
  47. position: CGPointMake(CGFloat(bone.worldX), CGFloat(bone.worldY))
  48. )
  49. return BoneRect(
  50. id: UUID(),
  51. x: position.x,
  52. y: position.y,
  53. width: 5,
  54. height: 5
  55. )
  56. }
  57. }
  58. )
  59. }
  60. }
  61. struct BoneRect: Hashable {
  62. let id: UUID
  63. let x: CGFloat
  64. let y: CGFloat
  65. let width: CGFloat
  66. let height: CGFloat
  67. }