AnimationStateEvents.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import SwiftUI
  2. import Spine
  3. import SpineCppLite
  4. struct AnimationStateEvents: View {
  5. @StateObject
  6. var controller = SpineController(
  7. onInitialized: { controller in
  8. controller.skeleton.scaleX = 0.5
  9. controller.skeleton.scaleY = 0.5
  10. controller.skeleton.findSlot(slotName: "gun")?.setColor(r: 1, g: 0, b: 0, a: 1)
  11. controller.animationStateData.defaultMix = 0.2
  12. let walk = controller.animationState.setAnimationByName(trackIndex: 0, animationName: "walk", loop: true)
  13. controller.animationStateWrapper.setTrackEntryListener(entry: walk) { type, entry, event in
  14. print("Walk animation event \(type)");
  15. }
  16. controller.animationState.addAnimationByName(trackIndex: 0, animationName: "jump", loop: false, delay: 2)
  17. let run = controller.animationState.addAnimationByName(trackIndex: 0, animationName: "run", loop: true, delay: 0)
  18. controller.animationStateWrapper.setTrackEntryListener(entry: run) { type, entry, event in
  19. print("Run animation event \(type)");
  20. }
  21. controller.animationStateWrapper.setStateListener { type, entry, event in
  22. if type == SPINE_EVENT_TYPE_EVENT, let event {
  23. print("User event: { name: \(event.data.name ?? "--"), intValue: \(event.intValue), floatValue: \(event.floatValue), stringValue: \(event.stringValue ?? "--") }")
  24. }
  25. }
  26. let current = controller.animationState.getCurrent(trackIndex: 0)?.animation.name ?? "--"
  27. print("Current: \(current)")
  28. }
  29. )
  30. var body: some View {
  31. VStack {
  32. Text("See output in console!")
  33. SpineView(
  34. from: .bundle(atlasFileName: "spineboy-pma.atlas", skeletonFileName: "spineboy-pro.skel"),
  35. controller: controller
  36. )
  37. }
  38. .navigationTitle("Animation State Listener")
  39. .navigationBarTitleDisplayMode(.inline)
  40. }
  41. }
  42. #Preview {
  43. AnimationStateEvents()
  44. }