/** @param last May be null. */
  private TrackEntry trackEntry(
      int trackIndex, Animation animation, boolean loop, TrackEntry last) {
    TrackEntry entry = trackEntryPool.obtain();
    entry.trackIndex = trackIndex;
    entry.animation = animation;
    entry.loop = loop;

    entry.eventThreshold = 0;
    entry.attachmentThreshold = 0;
    entry.drawOrderThreshold = 0;

    entry.animationStart = 0;
    entry.animationEnd = animation.getDuration();
    entry.animationLast = -1;
    entry.nextAnimationLast = -1;

    entry.delay = 0;
    entry.trackTime = 0;
    entry.trackLast = -1;
    entry.nextTrackLast = -1;
    entry.trackEnd = Float.MAX_VALUE;
    entry.timeScale = 1;

    entry.alpha = 1;
    entry.mixAlpha = 1;
    entry.mixTime = 0;
    entry.mixDuration = last == null ? 0 : data.getMix(last.animation, animation);
    return entry;
  }
  /**
   * Poses the skeleton using the track entry animations. There are no side effects other than
   * invoking listeners, so the animation state can be applied to multiple skeletons to pose them
   * identically.
   */
  public void apply(Skeleton skeleton) {
    if (skeleton == null) throw new IllegalArgumentException("skeleton cannot be null.");
    if (animationsChanged) animationsChanged();

    Array<Event> events = this.events;

    for (int i = 0, n = tracks.size; i < n; i++) {
      TrackEntry current = tracks.get(i);
      if (current == null || current.delay > 0) continue;

      // Apply mixing from entries first.
      float mix = current.alpha;
      if (current.mixingFrom != null) mix *= applyMixingFrom(current, skeleton);
      else if (current.trackTime >= current.trackEnd) //
      mix = 0; // Set to setup pose the last time the entry will be applied.

      // Apply current entry.
      float animationLast = current.animationLast, animationTime = current.getAnimationTime();
      int timelineCount = current.animation.timelines.size;
      Object[] timelines = current.animation.timelines.items;
      if (mix == 1) {
        for (int ii = 0; ii < timelineCount; ii++)
          ((Timeline) timelines[ii])
              .apply(skeleton, animationLast, animationTime, events, 1, true, false);
      } else {
        boolean firstFrame = current.timelinesRotation.size == 0;
        if (firstFrame) current.timelinesRotation.setSize(timelineCount << 1);
        float[] timelinesRotation = current.timelinesRotation.items;

        boolean[] timelinesFirst = current.timelinesFirst.items;
        for (int ii = 0; ii < timelineCount; ii++) {
          Timeline timeline = (Timeline) timelines[ii];
          if (timeline instanceof RotateTimeline) {
            applyRotateTimeline(
                timeline,
                skeleton,
                animationTime,
                mix,
                timelinesFirst[ii],
                timelinesRotation,
                ii << 1,
                firstFrame);
          } else
            timeline.apply(
                skeleton, animationLast, animationTime, events, mix, timelinesFirst[ii], false);
        }
      }
      queueEvents(current, animationTime);
      events.clear();
      current.nextAnimationLast = animationTime;
      current.nextTrackLast = current.trackTime;
    }

    queue.drain();
  }
  private float applyMixingFrom(TrackEntry entry, Skeleton skeleton) {
    TrackEntry from = entry.mixingFrom;
    if (from.mixingFrom != null) applyMixingFrom(from, skeleton);

    float mix;
    if (entry.mixDuration == 0) // Single frame mix to undo mixingFrom changes.
    mix = 1;
    else {
      mix = entry.mixTime / entry.mixDuration;
      if (mix > 1) mix = 1;
    }

    Array<Event> events = mix < from.eventThreshold ? this.events : null;
    boolean attachments = mix < from.attachmentThreshold, drawOrder = mix < from.drawOrderThreshold;
    float animationLast = from.animationLast, animationTime = from.getAnimationTime();
    int timelineCount = from.animation.timelines.size;
    Object[] timelines = from.animation.timelines.items;
    boolean[] timelinesFirst = from.timelinesFirst.items;
    float alpha = from.alpha * entry.mixAlpha * (1 - mix);

    boolean firstFrame = from.timelinesRotation.size == 0;
    if (firstFrame) from.timelinesRotation.setSize(timelineCount << 1);
    float[] timelinesRotation = from.timelinesRotation.items;

    for (int i = 0; i < timelineCount; i++) {
      Timeline timeline = (Timeline) timelines[i];
      boolean setupPose = timelinesFirst[i];
      if (timeline instanceof RotateTimeline)
        applyRotateTimeline(
            timeline,
            skeleton,
            animationTime,
            alpha,
            setupPose,
            timelinesRotation,
            i << 1,
            firstFrame);
      else {
        if (!setupPose) {
          if (!attachments && timeline instanceof AttachmentTimeline) continue;
          if (!drawOrder && timeline instanceof DrawOrderTimeline) continue;
        }
        timeline.apply(skeleton, animationLast, animationTime, events, alpha, setupPose, true);
      }
    }

    if (entry.mixDuration > 0) queueEvents(from, animationTime);
    this.events.clear();
    from.nextAnimationLast = animationTime;
    from.nextTrackLast = from.trackTime;

    return mix;
  }