From 0ee034b2dbfcd12f6d06d2055aa69e455bf277c6 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Thu, 17 Oct 2019 17:41:43 +0200 Subject: [PATCH] [libgdx] Store timeline ids inside set in Animation for O(1) lookup, added Animation#setTimelines. See #1462. --- .../com/esotericsoftware/spine/Animation.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Animation.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Animation.java index 2566b4b4f..ddf723c93 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Animation.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/Animation.java @@ -38,34 +38,41 @@ import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.FloatArray; import com.badlogic.gdx.utils.IntSet; + import com.esotericsoftware.spine.attachments.Attachment; import com.esotericsoftware.spine.attachments.VertexAttachment; /** A simple container for a list of timelines and a name. */ public class Animation { final String name; - final Array timelines; - final IntSet timelineIds; + Array timelines; + final IntSet timelineIDs = new IntSet(); float duration; public Animation (String name, Array timelines, float duration) { if (name == null) throw new IllegalArgumentException("name cannot be null."); - if (timelines == null) throw new IllegalArgumentException("timelines cannot be null."); this.name = name; - this.timelines = timelines; - this.timelineIds = new IntSet(); - for (Timeline timeline : timelines) - timelineIds.add(timeline.getPropertyId()); this.duration = duration; + setTimelines(timelines); } + /** If the returned array or the timelines it contains are modified, {@link #setTimelines(Array)} must be called. */ public Array getTimelines () { return timelines; } - /** Whether the timeline with the property id is contained in this animation **/ - boolean hasTimeline (int id) { - return timelineIds.contains(id); + public void setTimelines (Array timelines) { + if (timelines == null) throw new IllegalArgumentException("timelines cannot be null."); + this.timelines = timelines; + + timelineIDs.clear(); + for (Timeline timeline : timelines) + timelineIDs.add(timeline.getPropertyId()); + } + + /** Return true if this animation contains a timeline with the specified property ID. **/ + public boolean hasTimeline (int id) { + return timelineIDs.contains(id); } /** The duration of the animation in seconds, which is the highest time of all keys in the timeline. */