1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
use std::sync::Arc;
use crate::{
animation::Animation,
animation_state_data::AnimationStateData,
c::{
c_void, spAnimation, spAnimationState, spAnimationStateData, spAnimationState_addAnimation,
spAnimationState_addAnimationByName, spAnimationState_addEmptyAnimation,
spAnimationState_apply, spAnimationState_clearListenerNotifications,
spAnimationState_clearNext, spAnimationState_clearTrack, spAnimationState_clearTracks,
spAnimationState_create, spAnimationState_dispose, spAnimationState_disposeStatics,
spAnimationState_getCurrent, spAnimationState_setAnimation,
spAnimationState_setAnimationByName, spAnimationState_setEmptyAnimation,
spAnimationState_setEmptyAnimations, spAnimationState_update, spEvent, spEventType,
spTrackEntry, spTrackEntry_getAnimationTime, spTrackEntry_getTrackComplete,
},
c_interface::{to_c_str, CTmpMut, CTmpRef, NewFromPtr, SyncPtr},
error::SpineError,
event::Event,
skeleton::Skeleton,
AnimationEvent,
};
/// Applies animations over time, queues animations for later playback, mixes (crossfading) between
/// animations, and applies multiple animations on top of each other (layering).
///
/// See [Applying Animations](http://esotericsoftware.com/spine-applying-animations/) in the Spine
/// Runtimes Guide.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#AnimationState)
#[derive(Debug)]
pub struct AnimationState {
c_animation_state: SyncPtr<spAnimationState>,
owns_memory: bool,
_animation_state_data: Option<Arc<AnimationStateData>>,
}
impl NewFromPtr<spAnimationState> for AnimationState {
unsafe fn new_from_ptr(c_animation_state: *mut spAnimationState) -> Self {
Self {
c_animation_state: SyncPtr(c_animation_state),
owns_memory: false,
_animation_state_data: None,
}
}
}
impl AnimationState {
#[must_use]
pub fn new(animation_state_data: Arc<AnimationStateData>) -> Self {
let c_animation_state = unsafe { spAnimationState_create(animation_state_data.c_ptr()) };
unsafe {
(*c_animation_state).userData =
(Box::leak(Box::default()) as *mut AnimationStateUserData).cast::<c_void>();
}
Self {
c_animation_state: SyncPtr(c_animation_state),
owns_memory: true,
_animation_state_data: Some(animation_state_data),
}
}
pub fn update(&mut self, delta: f32) {
unsafe {
spAnimationState_update(self.c_ptr(), delta);
}
}
pub fn apply(&self, skeleton: &mut Skeleton) -> bool {
unsafe { spAnimationState_apply(self.c_animation_state.0, skeleton.c_ptr()) != 0 }
}
/// Clears all animations in all track entries in this animation state.
pub fn clear_tracks(&mut self) {
unsafe {
spAnimationState_clearTracks(self.c_ptr());
}
}
/// Clears animations for the given track entry index in this animation state.
pub fn clear_track(&mut self, track_index: usize) {
unsafe {
spAnimationState_clearTrack(self.c_ptr(), track_index as i32);
}
}
/// Sets the animation for the given track by name, clearing any queued tracks, and returning
/// the track index. If the track index doesn't exist then it will be created.
///
/// # Safety
///
/// This function should only be called with valid animation names. It is faster than the safe
/// alternative, [`AnimationState::set_animation_by_name`], but will likely segfault if the
/// animation does not exist.
pub unsafe fn set_animation_by_name_unchecked(
&mut self,
track_index: usize,
animation_name: &str,
looping: bool,
) -> CTmpMut<Self, TrackEntry> {
let c_animation_name = to_c_str(animation_name);
CTmpMut::new(
self,
TrackEntry::new_from_ptr(spAnimationState_setAnimationByName(
self.c_ptr(),
track_index as i32,
c_animation_name.as_ptr(),
i32::from(looping),
)),
)
}
/// Sets the animation for the given track by name, clearing any queued tracks, and returning
/// the track index. If the track index doesn't exist then it will be created.
///
/// # Errors
///
/// Returns [`SpineError::NotFound`] if an animation doesn't exist with the given name.
pub fn set_animation_by_name(
&mut self,
track_index: usize,
animation_name: &str,
looping: bool,
) -> Result<CTmpMut<Self, TrackEntry>, SpineError> {
if self
.data()
.skeleton_data()
.animations()
.any(|animation| animation.name() == animation_name)
{
Ok(unsafe {
self.set_animation_by_name_unchecked(track_index, animation_name, looping)
})
} else {
Err(SpineError::new_not_found("Animation", animation_name))
}
}
/// Sets the animation for the given track, clearning any queued tracks, and returning the
/// track index. If the track index doesn't exist then it will be created.
pub fn set_animation(
&mut self,
track_index: usize,
animation: &Animation,
looping: bool,
) -> CTmpMut<Self, TrackEntry> {
unsafe {
CTmpMut::new(
self,
TrackEntry::new_from_ptr(spAnimationState_setAnimation(
self.c_ptr(),
track_index as i32,
animation.c_ptr(),
i32::from(looping),
)),
)
}
}
/// Queues the animation in the given track by name, returning the track index. If the track
/// index doesn't exist then it will be created.
///
/// # Safety
///
/// This function should only be called with valid animation names. It is faster than the safe
/// alternative, [`AnimationState::add_animation_by_name`], but will likely segfault if the
/// animation does not exist.
pub unsafe fn add_animation_by_name_unchecked(
&mut self,
track_index: usize,
animation_name: &str,
looping: bool,
delay: f32,
) -> CTmpMut<Self, TrackEntry> {
let c_animation_name = to_c_str(animation_name);
CTmpMut::new(
self,
TrackEntry::new_from_ptr(spAnimationState_addAnimationByName(
self.c_ptr(),
track_index as i32,
c_animation_name.as_ptr(),
i32::from(looping),
delay,
)),
)
}
/// Queues the animation in the given track by name, returning the track index. If the track
/// index doesn't exist then it will be created.
///
/// # Errors
///
/// Returns [`SpineError::NotFound`] if an animation doesn't exist with the given name.
pub fn add_animation_by_name(
&mut self,
track_index: usize,
animation_name: &str,
looping: bool,
delay: f32,
) -> Result<CTmpMut<Self, TrackEntry>, SpineError> {
if self
.data()
.skeleton_data()
.animations()
.any(|animation| animation.name() == animation_name)
{
Ok(unsafe {
self.add_animation_by_name_unchecked(track_index, animation_name, looping, delay)
})
} else {
Err(SpineError::new_not_found("Animation", animation_name))
}
}
/// Queues the animation in the given track, returning the track index. If the track index
/// doesn't exist then it will be created.
pub fn add_animation(
&mut self,
track_index: usize,
animation: &Animation,
looping: bool,
delay: f32,
) -> CTmpMut<Self, TrackEntry> {
unsafe {
CTmpMut::new(
self,
TrackEntry::new_from_ptr(spAnimationState_addAnimation(
self.c_ptr(),
track_index as i32,
animation.c_ptr(),
i32::from(looping),
delay,
)),
)
}
}
pub fn set_empty_animation(
&mut self,
track_index: usize,
mix_duration: f32,
) -> CTmpMut<Self, TrackEntry> {
unsafe {
CTmpMut::new(
self,
TrackEntry::new_from_ptr(spAnimationState_setEmptyAnimation(
self.c_ptr(),
track_index as i32,
mix_duration,
)),
)
}
}
pub fn add_empty_animation(
&mut self,
track_index: usize,
mix_duration: f32,
delay: f32,
) -> CTmpMut<Self, TrackEntry> {
unsafe {
CTmpMut::new(
self,
TrackEntry::new_from_ptr(spAnimationState_addEmptyAnimation(
self.c_ptr(),
track_index as i32,
mix_duration,
delay,
)),
)
}
}
pub fn set_empty_animations(&mut self, mix_duration: f32) {
unsafe {
spAnimationState_setEmptyAnimations(self.c_ptr(), mix_duration);
}
}
#[must_use]
pub fn get_current(&self, track_index: usize) -> Option<CTmpRef<Self, TrackEntry>> {
unsafe {
let ptr = spAnimationState_getCurrent(self.c_ptr(), track_index as i32);
if !ptr.is_null() {
Some(CTmpRef::new(self, TrackEntry::new_from_ptr(ptr)))
} else {
None
}
}
}
/// Set the event listener on this animation state. An animation state can only have one event
/// listener at a time.
///
/// ```
/// # #[path="./test.rs"]
/// # mod test;
/// # use rusty_spine::{AnimationState, AnimationEvent};
/// # let (_, mut animation_state) = test::TestAsset::spineboy().instance();
/// animation_state.set_listener(|_, animation_event| match animation_event {
/// AnimationEvent::Start { track_entry } => {
/// println!("Animation {} started!", track_entry.track_index());
/// }
/// AnimationEvent::Interrupt { track_entry } => {
/// println!("Animation {} interrupted!", track_entry.track_index());
/// }
/// AnimationEvent::End { track_entry } => {
/// println!("Animation {} ended!", track_entry.track_index());
/// }
/// AnimationEvent::Complete { track_entry } => {
/// println!("Animation {} completed!", track_entry.track_index());
/// }
/// AnimationEvent::Dispose { track_entry } => {
/// println!("Animation {} disposed!", track_entry.track_index());
/// }
/// AnimationEvent::Event {
/// track_entry,
/// name,
/// int,
/// float,
/// string,
/// audio_path,
/// volume,
/// balance,
/// ..
/// } => {
/// println!("Animation {} event!", track_entry.track_index());
/// println!(" Name: {name}");
/// println!(" Integer: {int}");
/// println!(" Float: {float}");
/// if !string.is_empty() {
/// println!(" String: \"{string}\"");
/// }
/// if !audio_path.is_empty() {
/// println!(" Audio: \"{audio_path}\"");
/// println!(" Volume: {volume}");
/// println!(" Balance: {balance}");
/// }
/// }
/// });
/// ```
pub fn set_listener<F>(&mut self, listener: F)
where
F: Fn(&AnimationState, AnimationEvent) + 'static,
{
extern "C" fn c_listener(
c_animation_state: *mut spAnimationState,
c_event_type: spEventType,
c_track_entry: *mut spTrackEntry,
c_event: *mut spEvent,
) {
let user_data = unsafe {
&mut *((*c_animation_state)
.userData
.cast::<AnimationStateUserData>())
};
if let Some(listener) = &user_data.listener {
let animation_state = unsafe { AnimationState::new_from_ptr(c_animation_state) };
let track_entry = unsafe { TrackEntry::new_from_ptr(c_track_entry) };
let event_type = EventType::from(c_event_type);
match event_type {
EventType::Start => {
listener(&animation_state, AnimationEvent::Start { track_entry });
}
EventType::Interrupt => {
listener(&animation_state, AnimationEvent::Interrupt { track_entry });
}
EventType::End => {
listener(&animation_state, AnimationEvent::End { track_entry });
}
EventType::Complete => {
listener(&animation_state, AnimationEvent::Complete { track_entry });
}
EventType::Dispose => {
listener(&animation_state, AnimationEvent::Dispose { track_entry });
}
EventType::Event => {
assert!(!c_event.is_null());
let event = unsafe { Event::new_from_ptr(c_event) };
let raw_event = unsafe { Event::new_from_ptr(c_event) };
listener(
&animation_state,
AnimationEvent::Event {
track_entry,
name: event.data().name(),
time: event.time(),
int: event.int_value(),
float: event.float_value(),
string: event.string_value(),
audio_path: event.data().audio_path(),
volume: event.volume(),
balance: event.balance(),
event: raw_event,
},
);
}
EventType::Unknown => {}
};
}
}
let user_data = unsafe {
&mut *((*self.c_animation_state.0)
.userData
.cast::<AnimationStateUserData>())
};
user_data.listener = Some(Box::new(listener));
unsafe {
self.c_ptr_mut().listener = Some(c_listener);
}
}
pub fn clear_listener_notifications(&mut self) {
unsafe {
spAnimationState_clearListenerNotifications(self.c_ptr());
}
}
pub fn clear_next(&mut self, entry: &TrackEntry) {
unsafe {
spAnimationState_clearNext(self.c_ptr(), entry.c_ptr());
}
}
c_accessor_tmp_ptr_mut!(
/// The [`AnimationStateData`] to look up mix durations.
data,
data_mut,
data,
AnimationStateData,
spAnimationStateData
);
c_accessor!(tracks_count, tracksCount, usize);
c_accessor_array_nullable!(
tracks,
tracks_mut,
track_at_index,
track_at_index_mut,
AnimationState,
TrackEntry,
spTrackEntry,
tracks,
tracks_count
);
c_accessor_mut!(
/// Multiplier for the delta time when the animation state is updated, causing time for all
/// animations and mixes to play slower or faster. Defaults to 1.
///
/// See [`TrackEntry::timescale`] for affecting a single animation.
timescale,
/// Set the timescale, see [`timescale`](`Self::timescale`).
set_timescale,
timeScale,
f32
);
c_accessor_renderer_object!();
pub fn dispose_statics() {
unsafe {
spAnimationState_disposeStatics();
}
}
c_ptr!(c_animation_state, spAnimationState);
}
impl Drop for AnimationState {
fn drop(&mut self) {
if self.owns_memory {
unsafe {
(*self.c_animation_state.0).listener = None;
drop(Box::from_raw(
(*self.c_animation_state.0)
.userData
.cast::<AnimationStateUserData>(),
));
}
unsafe {
spAnimationState_dispose(self.c_animation_state.0);
}
}
}
}
type AnimationStateListenerCb = Box<dyn Fn(&AnimationState, AnimationEvent)>;
#[derive(Default)]
struct AnimationStateUserData {
listener: Option<AnimationStateListenerCb>,
}
/// The variants of event types.
///
/// Usually not necessary to check, instead use the variants of [`AnimationEvent`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventType {
Start = 0,
Interrupt = 1,
End = 2,
Complete = 3,
Dispose = 4,
Event = 5,
Unknown = 99,
}
impl From<spEventType> for EventType {
fn from(event_type: spEventType) -> Self {
match event_type {
0 => Self::Start,
1 => Self::Interrupt,
2 => Self::End,
3 => Self::Complete,
4 => Self::Dispose,
5 => Self::Event,
_ => Self::Unknown,
}
}
}
/// Stores settings and other state for the playback of an animation on an [`AnimationState`] track.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#TrackEntry)
#[derive(Debug)]
pub struct TrackEntry {
c_track_entry: SyncPtr<spTrackEntry>,
}
impl NewFromPtr<spTrackEntry> for TrackEntry {
unsafe fn new_from_ptr(c_track_entry: *mut spTrackEntry) -> Self {
TrackEntry {
c_track_entry: SyncPtr(c_track_entry),
}
}
}
impl TrackEntry {
/// Uses [`track_time`](`Self::track_time`) to compute the
/// [`animation_time`](`Self::animation_time`). When the [`track_time`](`Self::track_time`) is
/// 0, the [`animation_time`](`Self::animation_time`) is equal to the
/// [`animation_start`](`Self::animation_start`) time.
///
/// The [`animation_time`](`Self::animation_time`) is between
/// [`animation_start`](`Self::animation_start`) and
/// [`animation_end`](`Self::animation_end`), except if this track entry is non-looping and
/// [`animation_end`](`Self::animation_end`) is >= to the animation duration, then
/// [`animation_time`](`Self::animation_time`) continues to increase past
/// [`animation_end`](`Self::animation_end`).
#[must_use]
pub fn animation_time(&self) -> f32 {
unsafe { spTrackEntry_getAnimationTime(self.c_ptr()) }
}
/// If this track entry is non-looping, the track time in seconds when
/// [`animation_end`](`Self::animation_end`) is reached, or the current
/// [`track_time`](`Self::track_time`) if it has already been reached. If this track entry is
/// looping, the track time when this animation will reach its next
/// [`animation_end`](`Self::animation_end`) (the next loop completion).
#[must_use]
pub fn track_complete(&self) -> f32 {
unsafe { spTrackEntry_getTrackComplete(self.c_ptr()) }
}
fn handle_valid(handle: &TrackEntryHandle) -> bool {
let track_count = unsafe { (*handle.c_parent.0).tracksCount };
if handle.index < track_count {
let track_at_index =
unsafe { *(*handle.c_parent.0).tracks.offset(handle.index as isize) };
track_at_index == handle.c_item.0
} else {
false
}
}
c_accessor_tmp_ptr_mut!(
/// The animation to apply for this track entry.
animation,
/// The mutable animation to apply for this track entry.
animation_mut,
animation,
Animation,
spAnimation
);
c_accessor_tmp_ptr_optional!(
/// The animation queued to play before this animation, or [`None`]. `previous` makes up a
/// doubly linked list.
previous,
previous,
TrackEntry,
spTrackEntry
);
c_accessor_tmp_ptr_optional!(
/// The animation queued to start after this animation, or [`None`] if there is none. `next`
/// makes up a doubly linked list.
///
/// See [`AnimationState::clear_next`] to truncate the list.
next,
next,
TrackEntry,
spTrackEntry
);
c_accessor_tmp_ptr_optional!(
/// The track entry for the previous animation when mixing from the previous animation to this
/// animation, or [`None`] if no mixing is currently occuring. When mixing from multiple
/// animations, `mixing_from` makes up a linked list.
mixing_from,
mixingFrom,
TrackEntry,
spTrackEntry
);
c_accessor_tmp_ptr_optional!(
/// The track entry for the next animation when mixing from this animation to the next
/// animation, or [`None`] if no mixing is currently occuring. When mixing to multiple
/// animations, `mixing_to` makes up a linked list.
mixing_to,
mixingTo,
TrackEntry,
spTrackEntry
);
c_accessor!(track_index, trackIndex, usize);
c_accessor_bool_mut!(
/// If `true`, the animation will repeat. If `false` it will not, instead its last frame is
/// applied if played beyond its duration.
looping,
/// Set looping, see [`looping`](`Self::looping`).
set_looping,
loop_0
);
c_accessor_bool_mut!(
/// If `true`, when mixing from the previous animation to this animation, the previous
/// animation is applied as normal instead of being mixed out.
///
/// When mixing between animations that key the same property, if a lower track also keys
/// that property then the value will briefly dip toward the lower track value during the
/// mix. This happens because the first animation mixes from 100% to 0% while the second
/// animation mixes from 0% to 100%. Setting `hold_previous` to true applies the first
/// animation at 100% during the mix so the lower track value is overwritten. Such dipping
/// does not occur on the lowest track which keys the property, only when a higher track
/// also keys the property.
///
/// Snapping will occur if `hold_previous` is true and this animation does not key all the
/// same properties as the previous animation.
hold_previous,
/// Set hold previous, see [`hold_previous`](`Self::hold_previous`).
set_hold_previous,
holdPrevious
);
c_accessor_bool_mut!(
/// If `true`, the animation will be applied in reverse. Events are not fired when an
/// animation is applied in reverse.
reverse,
/// Set reverse, see [`reverse`](`Self::reverse`).
set_reverse,
reverse
);
c_accessor_bool_mut!(
/// If `true`, mixing rotation between tracks always uses the shortest rotation direction.
/// If the rotation is animated, the shortest rotation direction may change during the mix.
///
/// If `false`, the shortest rotation direction is remembered when the mix starts and the
/// same direction is used for the rest of the mix.
///
/// Defaults to `false`.
shortest_rotation,
/// Set shortest rotation, see [`shortest_rotation`](`Self::shortest_rotation`).
set_shortest_rotation,
shortestRotation
);
c_accessor_mut!(
/// When the mix percentage ([`mix_time`](`Self::mix_time`) /
/// [`mix_duration`](`Self::mix_duration`)) is less than the `event_threshold`, event
/// timelines are applied while this animation is being mixed out. Defaults to 0, so event
/// timelines are not applied while this animation is being mixed out.
event_threshold,
/// Set the event threshold, see [`event_threshold`](`Self::event_threshold`).
set_event_threshold,
eventThreshold,
f32
);
c_accessor_mut!(
/// When the mix percentage ([`mix_time`](`Self::mix_time`) /
/// [`mix_duration`](`Self::mix_duration`)) is less than the `attachment_threshold`,
/// attachment timelines are applied while this animation is being mixed out. Defaults to 0,
/// so attachment timelines are not applied while this animation is being mixed out.
attachment_threshold,
/// Set the attachment threshold, see
/// [`attachment_threshold`](`Self::attachment_threshold`).
set_attachment_threshold,
attachmentThreshold,
f32
);
c_accessor_mut!(
/// When the mix percentage ([`mix_time`](`Self::mix_time`) /
/// [`mix_duration`](`Self::mix_duration`)) is less than the `draw_order_threshold`, draw
/// order timelines are applied while this animation is being mixed out. Defaults to 0, so
/// draw order timelines are not applied while this animation is being mixed out.
draw_order_threshold,
/// Set the draw order threshold, see
/// [`draw_order_threshold`](`Self::draw_order_threshold`).
set_draw_order_threshold,
drawOrderThreshold,
f32
);
c_accessor_mut!(
/// Seconds when this animation starts, both initially and after looping. Defaults to 0.
///
/// When changing the animationStart time, it often makes sense to set
/// [`animation_last`](`Self::animation_last`) to the same value to prevent timeline keys
/// before the start time from triggering.
animation_start,
/// Set the animation start, see [`animation_start`](`Self::animation_start`).
set_animation_start,
animationStart,
f32
);
c_accessor_mut!(
/// Seconds for the last frame of this animation. Non-looping animations won't play past
/// this time. Looping animations will loop back to
/// [`animation_start`](`Self::animation_start`) at this time. Defaults to the animation
/// duration.
animation_end,
/// Set the animation end, see [`animation_end`](`Self::animation_end`).
set_animation_end,
animationEnd,
f32
);
c_accessor_mut!(
/// The time in seconds this animation was last applied. Some timelines use this for
/// one-time triggers. Eg, when this animation is applied, event timelines will fire all
/// events between the animationLast time (exclusive) and animationTime (inclusive).
/// Defaults to -1 to ensure triggers on frame 0 happen the first time this animation is
/// applied.
animation_last,
/// Set the animation last, see [`animation_last`](`Self::animation_last`).
set_animation_last,
animationLast,
f32
);
c_accessor_mut!(
/// Seconds to postpone playing the animation. When this track entry is the current track
/// entry, delay postpones incrementing the [`track_time`](`Self::track_time`). When this
/// track entry is queued, delay is the time from the start of the previous animation to
/// when this track entry will become the current track entry (ie when the previous track
/// entry [`track_time`](`Self::track_time`) >= this track entry's delay).
///
/// [`timescale`](`Self::timescale`) affects the delay.
///
/// When using addAnimation with a delay <= 0, the delay is set using the mix duration from
/// the AnimationStateData. If mixDuration is set afterward, the delay may need to be
/// adjusted.
delay,
/// Set the delay, see [`delay`](`Self::delay`).
set_delay,
delay,
f32
);
c_accessor!(
/// Current time in seconds this track entry has been the current track entry. The track
/// time determines [`animation_time`](`Self::animation_time`). The track time can be set
/// to start the animation at a time other than 0, without affecting looping.
track_time,
trackTime,
f32
);
c_accessor_mut!(
/// The track time in seconds when this animation will be removed from the track. Defaults
/// to the highest possible float value, meaning the animation will be applied until a new
/// animation is set or the track is cleared. If the track end time is reached, no other
/// animations are queued for playback, and mixing from any previous animations is complete,
/// then the properties keyed by the animation are set to the setup pose and the track is
/// cleared.
///
/// It may be desired to use [`AnimationState::add_empty_animation`] rather than have the
/// animation abruptly cease being applied.
track_end,
/// Set the track end, see [`track_end`](`Self::track_end`).
set_track_end,
trackEnd,
f32
);
c_accessor_mut!(
/// Multiplier for the delta time when this track entry is updated, causing time for this
/// animation to pass slower or faster. Defaults to 1.
///
/// Values < 0 are not supported. To play an animation in reverse, use
/// [`set_reverse`](`Self::set_reverse`).
///
/// [`mix_time`](`Self::mix_time`) is not affected by track entry time scale, so
/// [`mix_duration`](`Self::mix_duration`) may need to be adjusted to match the animation
/// speed.
///
/// When using [`AnimationState::add_animation`] with a delay <= 0, the delay is set using
/// the mix duration from the [`AnimationStateData`], assuming time scale to be 1. If the
/// time scale is not 1, the delay may need to be adjusted.
///
/// See [`AnimationState::timescale`] for affecting all animations.
timescale,
/// Set the timescale, see [`timescale`](`Self::timescale`).
set_timescale,
timeScale,
f32
);
c_accessor_mut!(
/// Values < 1 mix this animation with the skeleton's current pose (usually the pose
/// resulting from lower tracks). Defaults to 1, which overwrites the skeleton's current
/// pose with this animation.
///
/// Typically track 0 is used to completely pose the skeleton, then alpha is used on higher
/// tracks. It doesn't make sense to use alpha on track 0 if the skeleton pose is from the
/// last frame render.
alpha,
/// Set the alpha value, see [`alpha`](`Self::alpha`).
set_alpha,
alpha,
f32
);
c_accessor_mut!(
/// Seconds from 0 to the [`mix_duration`](`Self::mix_duration`) when mixing from the
/// previous animation to this animation. May be slightly more than
/// [`mix_duration`](`Self::mix_duration`) when the mix is complete.
mix_time,
/// Set the mix time, see [`mix_time`](`Self::mix_time`).
set_mix_time,
mixTime,
f32
);
c_accessor_mut!(
/// Seconds for mixing from the previous animation to this animation. Defaults to the value
/// provided by [`AnimationStateData::get_mix`] based on the animation before this animation
/// (if any).
///
/// A mix duration of 0 still mixes out over one frame to provide the track entry being
/// mixed out a chance to revert the properties it was animating.
///
/// The mixDuration can be set manually rather than use the value from
/// [`AnimationStateData::get_mix`]. In that case, the
/// [`mix_duration`](`Self::mix_duration`) can be set for a new track entry only before
/// update is first called.
///
/// When using [`AnimationState::add_animation`] with a `delay <= 0`, the
/// [`delay`](`Self::delay`) is set using the mix duration from the [`AnimationStateData`].
/// If `mix_duration` is set afterward, the delay may need to be adjusted.
///
/// For example:
/// ```
/// # fn foo(entry: &mut rusty_spine::TrackEntry) {
/// entry.set_delay(entry.previous().unwrap().track_complete() - entry.mix_duration());
/// # }
/// ```
mix_duration,
/// Set the mix duration, see [`mix_duration`](`Self::mix_duration`).
set_mix_duration,
mixDuration,
f32
);
c_accessor!(total_alpha, totalAlpha, f32);
c_accessor_renderer_object!();
c_ptr!(c_track_entry, spTrackEntry);
}
c_handle_indexed_decl!(
/// A storeable reference to a [`TrackEntry`].
///
/// Can be acquired from a
/// [`CTmpRef<AnimationState, TrackEntry>`] or [`CTmpMut<AnimationState, TrackEntry>`] acquired
/// from an [`AnimationState`] instance.
///
/// ```
/// # #[path="./test.rs"]
/// # mod test;
/// # use rusty_spine::{AnimationState, EventType, TrackEntryHandle};
/// # let (_, animation_state) = test::TestAsset::spineboy().instance();
/// let track_entry_handles: Vec<TrackEntryHandle> = animation_state.tracks().map(|track| track.unwrap().handle()).collect();
/// for track_entry_handle in track_entry_handles.iter() {
/// let track_entry = track_entry_handle.get(&animation_state).unwrap();
/// println!("{}", track_entry.animation().name());
/// }
/// ```
TrackEntryHandle,
TrackEntry,
AnimationState,
spTrackEntry,
spAnimationState
);
impl<'a> CTmpRef<'a, AnimationState, TrackEntry> {
#[must_use]
pub fn handle(&self) -> TrackEntryHandle {
TrackEntryHandle::new(self.track_index() as i32, self.c_ptr(), self.parent.c_ptr())
}
}
impl<'a> CTmpMut<'a, AnimationState, TrackEntry> {
#[must_use]
pub fn handle(&self) -> TrackEntryHandle {
TrackEntryHandle::new(self.track_index() as i32, self.c_ptr(), self.parent.c_ptr())
}
}
#[cfg(test)]
mod tests {
use crate::test::TestAsset;
#[test]
fn track_entry_optional() {
let (_, mut animation_state) = TestAsset::spineboy().instance();
let _ = animation_state.set_animation_by_name(0, "idle", true);
let _ = animation_state.set_animation_by_name(2, "run", true);
let track_0 = animation_state.tracks().next().unwrap();
let track_1 = animation_state.tracks().nth(1).unwrap();
let track_2 = animation_state.tracks().nth(2).unwrap();
assert!(track_0.is_some());
assert!(track_1.is_none());
assert!(track_2.is_some());
assert_eq!(track_0.unwrap().animation().name(), "idle");
assert_eq!(track_2.unwrap().animation().name(), "run");
assert!(animation_state.tracks().nth(3).is_none());
assert!(animation_state.track_at_index(0).is_some());
assert!(animation_state.track_at_index(1).is_none());
assert!(animation_state.track_at_index(2).is_some());
}
#[test]
fn track_entry_invalidate_clear() {
let (_, mut animation_state) = TestAsset::spineboy().instance();
let _ = animation_state.set_animation_by_name(0, "idle", true);
let track_handle = animation_state.track_at_index(0).unwrap().handle();
assert!(track_handle.get(&animation_state).is_some());
animation_state.clear_track(0);
assert!(track_handle.get(&animation_state).is_none());
}
#[test]
fn track_entry_invalidate_change() {
let (_, mut animation_state) = TestAsset::spineboy().instance();
let _ = animation_state.set_animation_by_name(0, "idle", true);
let track_handle = animation_state.track_at_index(0).unwrap().handle();
assert!(track_handle.get(&animation_state).is_some());
let _ = animation_state.set_animation_by_name(0, "run", true);
assert!(track_handle.get(&animation_state).is_none());
}
}