Animation.cs 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. using System.Collections.Generic;
  31. namespace Spine {
  32. /// <summary>
  33. /// Stores a list of timelines to animate a skeleton's pose over time.</summary>
  34. public class Animation {
  35. internal String name;
  36. internal ExposedList<Timeline> timelines;
  37. internal HashSet<string> timelineIds;
  38. internal float duration;
  39. public Animation (string name, ExposedList<Timeline> timelines, float duration) {
  40. if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
  41. this.name = name;
  42. SetTimelines(timelines);
  43. this.duration = duration;
  44. }
  45. public ExposedList<Timeline> Timelines {
  46. get { return timelines; }
  47. set { SetTimelines(value); }
  48. }
  49. public void SetTimelines (ExposedList<Timeline> timelines) {
  50. if (timelines == null) throw new ArgumentNullException("timelines", "timelines cannot be null.");
  51. this.timelines = timelines;
  52. // Note: avoiding reallocations by adding all hash set entries at
  53. // once (EnsureCapacity() is only available in newer .Net versions).
  54. int idCount = 0;
  55. int timelinesCount = timelines.Count;
  56. Timeline[] timelinesItems = timelines.Items;
  57. for (int t = 0; t < timelinesCount; ++t)
  58. idCount += timelinesItems[t].PropertyIds.Length;
  59. string[] propertyIds = new string[idCount];
  60. int currentId = 0;
  61. for (int t = 0; t < timelinesCount; ++t) {
  62. string[] ids = timelinesItems[t].PropertyIds;
  63. for (int i = 0, idsLength = ids.Length; i < idsLength; ++i)
  64. propertyIds[currentId++] = ids[i];
  65. }
  66. this.timelineIds = new HashSet<string>(propertyIds);
  67. }
  68. /// <summary>The duration of the animation in seconds, which is usually the highest time of all frames in the timeline. The duration is
  69. /// used to know when it has completed and when it should loop back to the start.</summary>
  70. public float Duration { get { return duration; } set { duration = value; } }
  71. /// <summary>The animation's name, which is unique across all animations in the skeleton.</summary>
  72. public string Name { get { return name; } }
  73. /// <summary>Returns true if this animation contains a timeline with any of the specified property IDs.</summary>
  74. public bool HasTimeline (string[] propertyIds) {
  75. foreach (string id in propertyIds)
  76. if (timelineIds.Contains(id)) return true;
  77. return false;
  78. }
  79. /// <summary>Applies the animation's timelines to the specified skeleton.</summary>
  80. /// <seealso cref="Timeline.Apply(Skeleton, float, float, ExposedList, float, MixBlend, MixDirection)"/>
  81. /// <param name="skeleton">The skeleton the animation is being applied to. This provides access to the bones, slots, and other skeleton
  82. /// components the timelines may change.</param>
  83. /// <param name="lastTime">The last time in seconds this animation was applied. Some timelines trigger only at specific times rather
  84. /// than every frame. Pass -1 the first time an animation is applied to ensure frame 0 is triggered.</param>
  85. /// <param name="time"> The time in seconds the skeleton is being posed for. Most timelines find the frame before and the frame after
  86. /// this time and interpolate between the frame values. If beyond the <see cref="Duration"/> and <code>loop</code> is
  87. /// true then the animation will repeat, else the last frame will be applied.</param>
  88. /// <param name="loop">If true, the animation repeats after the <see cref="Duration"/>.</param>
  89. /// <param name="events">If any events are fired, they are added to this list. Can be null to ignore fired events or if no timelines
  90. /// fire events.</param>
  91. /// <param name="alpha"> 0 applies the current or setup values (depending on <code>blend</code>). 1 applies the timeline values. Between
  92. /// 0 and 1 applies values between the current or setup values and the timeline values. By adjusting
  93. /// <code>alpha</code> over time, an animation can be mixed in or out. <code>alpha</code> can also be useful to apply
  94. /// animations on top of each other (layering).</param>
  95. /// <param name="blend">Controls how mixing is applied when <code>alpha</code> < 1.</param>
  96. /// <param name="direction">Indicates whether the timelines are mixing in or out. Used by timelines which perform instant transitions,
  97. /// such as <see cref="DrawOrderTimeline"/> or <see cref="AttachmentTimeline"/>.</param>
  98. public void Apply (Skeleton skeleton, float lastTime, float time, bool loop, ExposedList<Event> events, float alpha,
  99. MixBlend blend, MixDirection direction) {
  100. if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null.");
  101. if (loop && duration != 0) {
  102. time %= duration;
  103. if (lastTime > 0) lastTime %= duration;
  104. }
  105. Timeline[] timelines = this.timelines.Items;
  106. for (int i = 0, n = this.timelines.Count; i < n; i++)
  107. timelines[i].Apply(skeleton, lastTime, time, events, alpha, blend, direction);
  108. }
  109. override public string ToString () {
  110. return name;
  111. }
  112. }
  113. /// <summary>
  114. /// Controls how timeline values are mixed with setup pose values or current pose values when a timeline is applied with
  115. /// <code>alpha</code> < 1.</summary>
  116. /// <seealso cref="Timeline.Apply(Skeleton, float, float, ExposedList, float, MixBlend, MixDirection)"/>
  117. public enum MixBlend {
  118. /// <summary> Transitions from the setup value to the timeline value (the current value is not used). Before the first frame, the
  119. /// setup value is set.</summary>
  120. Setup,
  121. /// <summary>
  122. /// <para>
  123. /// Transitions from the current value to the timeline value. Before the first frame, transitions from the current value to
  124. /// the setup value. Timelines which perform instant transitions, such as <see cref="DrawOrderTimeline"/> or
  125. /// <see cref="AttachmentTimeline"/>, use the setup value before the first frame.</para>
  126. /// <para>
  127. /// <code>First</code> is intended for the first animations applied, not for animations layered on top of those.</para>
  128. /// </summary>
  129. First,
  130. /// <summary>
  131. /// <para>
  132. /// Transitions from the current value to the timeline value. No change is made before the first frame (the current value is
  133. /// kept until the first frame).</para>
  134. /// <para>
  135. /// <code>Replace</code> is intended for animations layered on top of others, not for the first animations applied.</para>
  136. /// </summary>
  137. Replace,
  138. /// <summary>
  139. /// <para>
  140. /// Transitions from the current value to the current value plus the timeline value. No change is made before the first frame
  141. /// (the current value is kept until the first frame).</para>
  142. /// <para>
  143. /// <code>Add</code> is intended for animations layered on top of others, not for the first animations applied. Properties
  144. /// set by additive animations must be set manually or by another animation before applying the additive animations, else the
  145. /// property values will increase each time the additive animations are applied.
  146. /// </para>
  147. /// </summary>
  148. Add
  149. }
  150. /// <summary>
  151. /// Indicates whether a timeline's <code>alpha</code> is mixing out over time toward 0 (the setup or current pose value) or
  152. /// mixing in toward 1 (the timeline's value). Some timelines use this to decide how values are applied.</summary>
  153. /// <seealso cref="Timeline.Apply(Skeleton, float, float, ExposedList, float, MixBlend, MixDirection)"/>
  154. public enum MixDirection {
  155. In,
  156. Out
  157. }
  158. public enum Property {
  159. Rotate = 0, X, Y, ScaleX, ScaleY, ShearX, ShearY, Inherit, //
  160. RGB, Alpha, RGB2, //
  161. Attachment, Deform, //
  162. Event, DrawOrder, //
  163. IkConstraint, TransformConstraint, //
  164. PathConstraintPosition, PathConstraintSpacing, PathConstraintMix, //
  165. PhysicsConstraintInertia, PhysicsConstraintStrength, PhysicsConstraintDamping, PhysicsConstraintMass, //
  166. PhysicsConstraintWind, PhysicsConstraintGravity, PhysicsConstraintMix, PhysicsConstraintReset, //
  167. Sequence
  168. }
  169. /// <summary>
  170. /// The base class for all timelines.</summary>
  171. public abstract class Timeline {
  172. private readonly string[] propertyIds;
  173. internal readonly float[] frames;
  174. /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
  175. public Timeline (int frameCount, params string[] propertyIds) {
  176. if (propertyIds == null) throw new System.ArgumentNullException("propertyIds", "propertyIds cannot be null.");
  177. this.propertyIds = propertyIds;
  178. frames = new float[frameCount * FrameEntries];
  179. }
  180. /// <summary>Uniquely encodes both the type of this timeline and the skeleton properties that it affects.</summary>
  181. public string[] PropertyIds {
  182. get { return propertyIds; }
  183. }
  184. /// <summary>The time in seconds and any other values for each frame.</summary>
  185. public float[] Frames {
  186. get { return frames; }
  187. }
  188. /// <summary>The number of entries stored per frame.</summary>
  189. public virtual int FrameEntries {
  190. get { return 1; }
  191. }
  192. /// <summary>The number of frames for this timeline.</summary>
  193. public virtual int FrameCount {
  194. get { return frames.Length / FrameEntries; }
  195. }
  196. public float Duration {
  197. get {
  198. return frames[frames.Length - FrameEntries];
  199. }
  200. }
  201. /// <summary>Applies this timeline to the skeleton.</summary>
  202. /// <param name="skeleton">The skeleton the timeline is being applied to. This provides access to the bones, slots, and other
  203. /// skeleton components the timeline may change.</param>
  204. /// <param name="lastTime">The time this timeline was last applied. Timelines such as <see cref="EventTimeline"/> trigger only
  205. /// at specific times rather than every frame. In that case, the timeline triggers everything between
  206. /// <code>lastTime</code> (exclusive) and <code>time</code> (inclusive). Pass -1 the first time an animation is
  207. /// applied to ensure frame 0 is triggered.</param>
  208. /// <param name="time">The time in seconds that the skeleton is being posed for. Most timelines find the frame before and the frame
  209. /// after this time and interpolate between the frame values.If beyond the last frame, the last frame will be
  210. /// applied.</param>
  211. /// <param name="events">If any events are fired, they are added to this list. Can be null to ignore fired events or if the timeline
  212. /// does not fire events.</param>
  213. /// <param name="alpha">0 applies the current or setup value (depending on <code>blend</code>). 1 applies the timeline value.
  214. /// Between 0 and 1 applies a value between the current or setup value and the timeline value.By adjusting
  215. /// <code>alpha</code> over time, an animation can be mixed in or out. <code>alpha</code> can also be useful to
  216. /// apply animations on top of each other (layering).</param>
  217. /// <param name="blend">Controls how mixing is applied when <code>alpha</code> < 1.</param>
  218. /// <param name="direction">Indicates whether the timeline is mixing in or out. Used by timelines which perform instant transitions,
  219. /// such as <see cref="DrawOrderTimeline"/> or <see cref="AttachmentTimeline"/>, and other such as <see cref="ScaleTimeline"/>.</param>
  220. public abstract void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> events, float alpha,
  221. MixBlend blend, MixDirection direction);
  222. /// <summary>Search using a stride of 1.</summary>
  223. /// <param name="time">Must be >= the first value in <code>frames</code>.</param>
  224. /// <returns>The index of the first value <= <code>time</code>.</returns>
  225. internal static int Search (float[] frames, float time) {
  226. int n = frames.Length;
  227. for (int i = 1; i < n; i++)
  228. if (frames[i] > time) return i - 1;
  229. return n - 1;
  230. }
  231. /// <summary>Search using the specified stride.</summary>
  232. /// <param name="time">Must be >= the first value in <code>frames</code>.</param>
  233. /// <returns>The index of the first value <= <code>time</code>.</returns>
  234. internal static int Search (float[] frames, float time, int step) {
  235. int n = frames.Length;
  236. for (int i = step; i < n; i += step)
  237. if (frames[i] > time) return i - step;
  238. return n - step;
  239. }
  240. }
  241. /// <summary>An interface for timelines which change the property of a bone.</summary>
  242. public interface IBoneTimeline {
  243. /// <summary>The index of the bone in <see cref="Skeleton.Bones"/> that will be changed when this timeline is applied.</summary>
  244. int BoneIndex { get; }
  245. }
  246. /// <summary>An interface for timelines which change the property of a slot.</summary>
  247. public interface ISlotTimeline {
  248. /// <summary>The index of the slot in <see cref="Skeleton.Slots"/> that will be changed when this timeline is applied.</summary>
  249. int SlotIndex { get; }
  250. }
  251. /// <summary>The base class for timelines that interpolate between frame values using stepped, linear, or a Bezier curve.</summary>
  252. public abstract class CurveTimeline : Timeline {
  253. public const int LINEAR = 0, STEPPED = 1, BEZIER = 2, BEZIER_SIZE = 18;
  254. internal float[] curves;
  255. /// <summary>The number of key frames for this timeline.</summary>
  256. /// <param name="bezierCount">The maximum number of Bezier curves. See <see cref="Shrink(int)"/>.</param>
  257. /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
  258. public CurveTimeline (int frameCount, int bezierCount, params string[] propertyIds)
  259. : base(frameCount, propertyIds) {
  260. curves = new float[frameCount + bezierCount * BEZIER_SIZE];
  261. curves[frameCount - 1] = STEPPED;
  262. }
  263. /// <summary>Sets the specified frame to linear interpolation.</summary>
  264. /// <param name="frame">Between 0 and <code>frameCount - 1</code>, inclusive.</param>
  265. public void SetLinear (int frame) {
  266. curves[frame] = LINEAR;
  267. }
  268. /// <summary>Sets the specified frame to stepped interpolation.</summary>
  269. /// <param name="frame">Between 0 and <code>frameCount - 1</code>, inclusive.</param>
  270. public void SetStepped (int frame) {
  271. curves[frame] = STEPPED;
  272. }
  273. /// <summary>Returns the interpolation type for the specified frame.</summary>
  274. /// <param name="frame">Between 0 and <code>frameCount - 1</code>, inclusive.</param>
  275. /// <returns><see cref="LINEAR"/>, <see cref="STEPPED"/> or <see cref="BEZIER"/> + the index of the Bezier segments.</returns>
  276. public float GetCurveType (int frame) {
  277. return (int)curves[frame];
  278. }
  279. /// <summary>Shrinks the storage for Bezier curves, for use when <code>bezierCount</code> (specified in the constructor) was larger
  280. /// than the actual number of Bezier curves.</summary>
  281. public void Shrink (int bezierCount) {
  282. int size = FrameCount + bezierCount * BEZIER_SIZE;
  283. if (curves.Length > size) {
  284. float[] newCurves = new float[size];
  285. Array.Copy(curves, 0, newCurves, 0, size);
  286. curves = newCurves;
  287. }
  288. }
  289. /// <summary>
  290. /// Stores the segments for the specified Bezier curve. For timelines that modify multiple values, there may be more than
  291. /// one curve per frame.</summary>
  292. /// <param name="bezier">The ordinal of this Bezier curve for this timeline, between 0 and <code>bezierCount - 1</code> (specified
  293. /// in the constructor), inclusive.</param>
  294. /// <param name="frame">Between 0 and <code>frameCount - 1</code>, inclusive.</param>
  295. /// <param name="value">The index of the value for the frame this curve is used for.</param>
  296. /// <param name="time1">The time for the first key.</param>
  297. /// <param name="value1">The value for the first key.</param>
  298. /// <param name="cx1">The time for the first Bezier handle.</param>
  299. /// <param name="cy1">The value for the first Bezier handle.</param>
  300. /// <param name="cx2">The time of the second Bezier handle.</param>
  301. /// <param name="cy2">The value for the second Bezier handle.</param>
  302. /// <param name="time2">The time for the second key.</param>
  303. /// <param name="value2">The value for the second key.</param>
  304. public void SetBezier (int bezier, int frame, int value, float time1, float value1, float cx1, float cy1, float cx2,
  305. float cy2, float time2, float value2) {
  306. float[] curves = this.curves;
  307. int i = FrameCount + bezier * BEZIER_SIZE;
  308. if (value == 0) curves[frame] = BEZIER + i;
  309. float tmpx = (time1 - cx1 * 2 + cx2) * 0.03f, tmpy = (value1 - cy1 * 2 + cy2) * 0.03f;
  310. float dddx = ((cx1 - cx2) * 3 - time1 + time2) * 0.006f, dddy = ((cy1 - cy2) * 3 - value1 + value2) * 0.006f;
  311. float ddx = tmpx * 2 + dddx, ddy = tmpy * 2 + dddy;
  312. float dx = (cx1 - time1) * 0.3f + tmpx + dddx * 0.16666667f, dy = (cy1 - value1) * 0.3f + tmpy + dddy * 0.16666667f;
  313. float x = time1 + dx, y = value1 + dy;
  314. for (int n = i + BEZIER_SIZE; i < n; i += 2) {
  315. curves[i] = x;
  316. curves[i + 1] = y;
  317. dx += ddx;
  318. dy += ddy;
  319. ddx += dddx;
  320. ddy += dddy;
  321. x += dx;
  322. y += dy;
  323. }
  324. }
  325. /// <summary>
  326. /// Returns the Bezier interpolated value for the specified time.</summary>
  327. /// <param name="frameIndex">The index into <see cref="Frames"/> for the values of the frame before <code>time</code>.</param>
  328. /// <param name="valueOffset">The offset from <code>frameIndex</code> to the value this curve is used for.</param>
  329. /// <param name="i">The index of the Bezier segments. See <see cref="GetCurveType(int)"/>.</param>
  330. public float GetBezierValue (float time, int frameIndex, int valueOffset, int i) {
  331. float[] curves = this.curves;
  332. if (curves[i] > time) {
  333. float x = frames[frameIndex], y = frames[frameIndex + valueOffset];
  334. return y + (time - x) / (curves[i] - x) * (curves[i + 1] - y);
  335. }
  336. int n = i + BEZIER_SIZE;
  337. for (i += 2; i < n; i += 2) {
  338. if (curves[i] >= time) {
  339. float x = curves[i - 2], y = curves[i - 1];
  340. return y + (time - x) / (curves[i] - x) * (curves[i + 1] - y);
  341. }
  342. }
  343. frameIndex += FrameEntries;
  344. { // scope added to prevent compile error "float x and y declared in enclosing scope"
  345. float x = curves[n - 2], y = curves[n - 1];
  346. return y + (time - x) / (frames[frameIndex] - x) * (frames[frameIndex + valueOffset] - y);
  347. }
  348. }
  349. }
  350. /// <summary>The base class for a <see cref="CurveTimeline"/> that sets one property.</summary>
  351. public abstract class CurveTimeline1 : CurveTimeline {
  352. public const int ENTRIES = 2;
  353. internal const int VALUE = 1;
  354. /// <param name="bezierCount">The maximum number of Bezier curves. See <see cref="Shrink(int)"/>.</param>
  355. /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
  356. public CurveTimeline1 (int frameCount, int bezierCount, string propertyId)
  357. : base(frameCount, bezierCount, propertyId) {
  358. }
  359. public override int FrameEntries {
  360. get { return ENTRIES; }
  361. }
  362. /// <summary>Sets the time and value for the specified frame.</summary>
  363. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  364. /// <param name="time">The frame time in seconds</param>
  365. public void SetFrame (int frame, float time, float value) {
  366. frame <<= 1;
  367. frames[frame] = time;
  368. frames[frame + VALUE] = value;
  369. }
  370. /// <summary>Returns the interpolated value for the specified time.</summary>
  371. public float GetCurveValue (float time) {
  372. float[] frames = this.frames;
  373. int i = frames.Length - 2;
  374. for (int ii = 2; ii <= i; ii += 2) {
  375. if (frames[ii] > time) {
  376. i = ii - 2;
  377. break;
  378. }
  379. }
  380. int curveType = (int)curves[i >> 1];
  381. switch (curveType) {
  382. case LINEAR:
  383. float before = frames[i], value = frames[i + VALUE];
  384. return value + (time - before) / (frames[i + ENTRIES] - before) * (frames[i + ENTRIES + VALUE] - value);
  385. case STEPPED:
  386. return frames[i + VALUE];
  387. }
  388. return GetBezierValue(time, i, VALUE, curveType - BEZIER);
  389. }
  390. public float GetRelativeValue (float time, float alpha, MixBlend blend, float current, float setup) {
  391. if (time < frames[0]) {
  392. switch (blend) {
  393. case MixBlend.Setup:
  394. return setup;
  395. case MixBlend.First:
  396. return current + (setup - current) * alpha;
  397. }
  398. return current;
  399. }
  400. float value = GetCurveValue(time);
  401. switch (blend) {
  402. case MixBlend.Setup:
  403. return setup + value * alpha;
  404. case MixBlend.First:
  405. case MixBlend.Replace:
  406. value += setup - current;
  407. break;
  408. }
  409. return current + value * alpha;
  410. }
  411. public float GetAbsoluteValue (float time, float alpha, MixBlend blend, float current, float setup) {
  412. if (time < frames[0]) {
  413. switch (blend) {
  414. case MixBlend.Setup:
  415. return setup;
  416. case MixBlend.First:
  417. return current + (setup - current) * alpha;
  418. }
  419. return current;
  420. }
  421. float value = GetCurveValue(time);
  422. if (blend == MixBlend.Setup) return setup + (value - setup) * alpha;
  423. return current + (value - current) * alpha;
  424. }
  425. public float GetAbsoluteValue (float time, float alpha, MixBlend blend, float current, float setup, float value) {
  426. if (time < frames[0]) {
  427. switch (blend) {
  428. case MixBlend.Setup:
  429. return setup;
  430. case MixBlend.First:
  431. return current + (setup - current) * alpha;
  432. }
  433. return current;
  434. }
  435. if (blend == MixBlend.Setup) return setup + (value - setup) * alpha;
  436. return current + (value - current) * alpha;
  437. }
  438. public float GetScaleValue (float time, float alpha, MixBlend blend, MixDirection direction, float current, float setup) {
  439. float[] frames = this.frames;
  440. if (time < frames[0]) {
  441. switch (blend) {
  442. case MixBlend.Setup:
  443. return setup;
  444. case MixBlend.First:
  445. return current + (setup - current) * alpha;
  446. }
  447. return current;
  448. }
  449. float value = GetCurveValue(time) * setup;
  450. if (alpha == 1) {
  451. if (blend == MixBlend.Add) return current + value - setup;
  452. return value;
  453. }
  454. // Mixing out uses sign of setup or current pose, else use sign of key.
  455. if (direction == MixDirection.Out) {
  456. switch (blend) {
  457. case MixBlend.Setup:
  458. return setup + (Math.Abs(value) * Math.Sign(setup) - setup) * alpha;
  459. case MixBlend.First:
  460. case MixBlend.Replace:
  461. return current + (Math.Abs(value) * Math.Sign(current) - current) * alpha;
  462. }
  463. } else {
  464. float s;
  465. switch (blend) {
  466. case MixBlend.Setup:
  467. s = Math.Abs(setup) * Math.Sign(value);
  468. return s + (value - s) * alpha;
  469. case MixBlend.First:
  470. case MixBlend.Replace:
  471. s = Math.Abs(current) * Math.Sign(value);
  472. return s + (value - s) * alpha;
  473. }
  474. }
  475. return current + (value - setup) * alpha;
  476. }
  477. }
  478. /// <summary>The base class for a <see cref="CurveTimeline"/> which sets two properties.</summary>
  479. public abstract class CurveTimeline2 : CurveTimeline {
  480. public const int ENTRIES = 3;
  481. internal const int VALUE1 = 1, VALUE2 = 2;
  482. /// <param name="bezierCount">The maximum number of Bezier curves. See <see cref="Shrink(int)"/>.</param>
  483. /// <param name="propertyIds">Unique identifiers for the properties the timeline modifies.</param>
  484. public CurveTimeline2 (int frameCount, int bezierCount, string propertyId1, string propertyId2)
  485. : base(frameCount, bezierCount, propertyId1, propertyId2) {
  486. }
  487. public override int FrameEntries {
  488. get { return ENTRIES; }
  489. }
  490. /// <summary>Sets the time and values for the specified frame.</summary>
  491. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  492. /// <param name="time">The frame time in seconds.</param>
  493. public void SetFrame (int frame, float time, float value1, float value2) {
  494. frame *= ENTRIES;
  495. frames[frame] = time;
  496. frames[frame + VALUE1] = value1;
  497. frames[frame + VALUE2] = value2;
  498. }
  499. }
  500. /// <summary>Changes a bone's local <see cref="Bone.Rotation"/>.</summary>
  501. public class RotateTimeline : CurveTimeline1, IBoneTimeline {
  502. readonly int boneIndex;
  503. public RotateTimeline (int frameCount, int bezierCount, int boneIndex)
  504. : base(frameCount, bezierCount, (int)Property.Rotate + "|" + boneIndex) {
  505. this.boneIndex = boneIndex;
  506. }
  507. public int BoneIndex {
  508. get {
  509. return boneIndex;
  510. }
  511. }
  512. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  513. MixDirection direction) {
  514. Bone bone = skeleton.bones.Items[boneIndex];
  515. if (bone.active) bone.rotation = GetRelativeValue(time, alpha, blend, bone.rotation, bone.data.rotation);
  516. }
  517. }
  518. /// <summary>Changes a bone's local <see cref"Bone.X"/> and <see cref"Bone.Y"/>.</summary>
  519. public class TranslateTimeline : CurveTimeline2, IBoneTimeline {
  520. readonly int boneIndex;
  521. public TranslateTimeline (int frameCount, int bezierCount, int boneIndex)
  522. : base(frameCount, bezierCount, //
  523. (int)Property.X + "|" + boneIndex, //
  524. (int)Property.Y + "|" + boneIndex) {
  525. this.boneIndex = boneIndex;
  526. }
  527. public int BoneIndex {
  528. get {
  529. return boneIndex;
  530. }
  531. }
  532. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  533. MixDirection direction) {
  534. Bone bone = skeleton.bones.Items[boneIndex];
  535. if (!bone.active) return;
  536. float[] frames = this.frames;
  537. if (time < frames[0]) {
  538. switch (blend) {
  539. case MixBlend.Setup:
  540. bone.x = bone.data.x;
  541. bone.y = bone.data.y;
  542. return;
  543. case MixBlend.First:
  544. bone.x += (bone.data.x - bone.x) * alpha;
  545. bone.y += (bone.data.y - bone.y) * alpha;
  546. return;
  547. }
  548. return;
  549. }
  550. float x, y;
  551. GetCurveValue(out x, out y, time); // note: reference implementation has code inlined
  552. switch (blend) {
  553. case MixBlend.Setup:
  554. bone.x = bone.data.x + x * alpha;
  555. bone.y = bone.data.y + y * alpha;
  556. break;
  557. case MixBlend.First:
  558. case MixBlend.Replace:
  559. bone.x += (bone.data.x + x - bone.x) * alpha;
  560. bone.y += (bone.data.y + y - bone.y) * alpha;
  561. break;
  562. case MixBlend.Add:
  563. bone.x += x * alpha;
  564. bone.y += y * alpha;
  565. break;
  566. }
  567. }
  568. public void GetCurveValue (out float x, out float y, float time) {
  569. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
  570. switch (curveType) {
  571. case LINEAR:
  572. float before = frames[i];
  573. x = frames[i + VALUE1];
  574. y = frames[i + VALUE2];
  575. float t = (time - before) / (frames[i + ENTRIES] - before);
  576. x += (frames[i + ENTRIES + VALUE1] - x) * t;
  577. y += (frames[i + ENTRIES + VALUE2] - y) * t;
  578. break;
  579. case STEPPED:
  580. x = frames[i + VALUE1];
  581. y = frames[i + VALUE2];
  582. break;
  583. default:
  584. x = GetBezierValue(time, i, VALUE1, curveType - BEZIER);
  585. y = GetBezierValue(time, i, VALUE2, curveType + BEZIER_SIZE - BEZIER);
  586. break;
  587. }
  588. }
  589. }
  590. /// <summary>Changes a bone's local <see cref"Bone.X"/>.</summary>
  591. public class TranslateXTimeline : CurveTimeline1, IBoneTimeline {
  592. readonly int boneIndex;
  593. public TranslateXTimeline (int frameCount, int bezierCount, int boneIndex)
  594. : base(frameCount, bezierCount, (int)Property.X + "|" + boneIndex) {
  595. this.boneIndex = boneIndex;
  596. }
  597. public int BoneIndex {
  598. get {
  599. return boneIndex;
  600. }
  601. }
  602. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  603. MixDirection direction) {
  604. Bone bone = skeleton.bones.Items[boneIndex];
  605. if (bone.active) bone.x = GetRelativeValue(time, alpha, blend, bone.x, bone.data.x);
  606. }
  607. }
  608. /// <summary>Changes a bone's local <see cref"Bone.Y"/>.</summary>
  609. public class TranslateYTimeline : CurveTimeline1, IBoneTimeline {
  610. readonly int boneIndex;
  611. public TranslateYTimeline (int frameCount, int bezierCount, int boneIndex)
  612. : base(frameCount, bezierCount, (int)Property.Y + "|" + boneIndex) {
  613. this.boneIndex = boneIndex;
  614. }
  615. public int BoneIndex {
  616. get {
  617. return boneIndex;
  618. }
  619. }
  620. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  621. MixDirection direction) {
  622. Bone bone = skeleton.bones.Items[boneIndex];
  623. if (bone.active) bone.y = GetRelativeValue(time, alpha, blend, bone.y, bone.data.y);
  624. }
  625. }
  626. /// <summary>Changes a bone's local <see cref="Bone.ScaleX"> and <see cref="Bone.ScaleY">.</summary>
  627. public class ScaleTimeline : CurveTimeline2, IBoneTimeline {
  628. readonly int boneIndex;
  629. public ScaleTimeline (int frameCount, int bezierCount, int boneIndex)
  630. : base(frameCount, bezierCount, //
  631. (int)Property.ScaleX + "|" + boneIndex, //
  632. (int)Property.ScaleY + "|" + boneIndex) {
  633. this.boneIndex = boneIndex;
  634. }
  635. public int BoneIndex {
  636. get {
  637. return boneIndex;
  638. }
  639. }
  640. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  641. MixDirection direction) {
  642. Bone bone = skeleton.bones.Items[boneIndex];
  643. if (!bone.active) return;
  644. float[] frames = this.frames;
  645. if (time < frames[0]) {
  646. switch (blend) {
  647. case MixBlend.Setup:
  648. bone.scaleX = bone.data.scaleX;
  649. bone.scaleY = bone.data.scaleY;
  650. return;
  651. case MixBlend.First:
  652. bone.scaleX += (bone.data.scaleX - bone.scaleX) * alpha;
  653. bone.scaleY += (bone.data.scaleY - bone.scaleY) * alpha;
  654. return;
  655. }
  656. return;
  657. }
  658. float x, y;
  659. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
  660. switch (curveType) {
  661. case LINEAR:
  662. float before = frames[i];
  663. x = frames[i + VALUE1];
  664. y = frames[i + VALUE2];
  665. float t = (time - before) / (frames[i + ENTRIES] - before);
  666. x += (frames[i + ENTRIES + VALUE1] - x) * t;
  667. y += (frames[i + ENTRIES + VALUE2] - y) * t;
  668. break;
  669. case STEPPED:
  670. x = frames[i + VALUE1];
  671. y = frames[i + VALUE2];
  672. break;
  673. default:
  674. x = GetBezierValue(time, i, VALUE1, curveType - BEZIER);
  675. y = GetBezierValue(time, i, VALUE2, curveType + BEZIER_SIZE - BEZIER);
  676. break;
  677. }
  678. x *= bone.data.scaleX;
  679. y *= bone.data.scaleY;
  680. if (alpha == 1) {
  681. if (blend == MixBlend.Add) {
  682. bone.scaleX += x - bone.data.scaleX;
  683. bone.scaleY += y - bone.data.scaleY;
  684. } else {
  685. bone.scaleX = x;
  686. bone.scaleY = y;
  687. }
  688. } else {
  689. // Mixing out uses sign of setup or current pose, else use sign of key.
  690. float bx, by;
  691. if (direction == MixDirection.Out) {
  692. switch (blend) {
  693. case MixBlend.Setup:
  694. bx = bone.data.scaleX;
  695. by = bone.data.scaleY;
  696. bone.scaleX = bx + (Math.Abs(x) * Math.Sign(bx) - bx) * alpha;
  697. bone.scaleY = by + (Math.Abs(y) * Math.Sign(by) - by) * alpha;
  698. break;
  699. case MixBlend.First:
  700. case MixBlend.Replace:
  701. bx = bone.scaleX;
  702. by = bone.scaleY;
  703. bone.scaleX = bx + (Math.Abs(x) * Math.Sign(bx) - bx) * alpha;
  704. bone.scaleY = by + (Math.Abs(y) * Math.Sign(by) - by) * alpha;
  705. break;
  706. case MixBlend.Add:
  707. bone.scaleX += (x - bone.data.scaleX) * alpha;
  708. bone.scaleY += (y - bone.data.scaleY) * alpha;
  709. break;
  710. }
  711. } else {
  712. switch (blend) {
  713. case MixBlend.Setup:
  714. bx = Math.Abs(bone.data.scaleX) * Math.Sign(x);
  715. by = Math.Abs(bone.data.scaleY) * Math.Sign(y);
  716. bone.scaleX = bx + (x - bx) * alpha;
  717. bone.scaleY = by + (y - by) * alpha;
  718. break;
  719. case MixBlend.First:
  720. case MixBlend.Replace:
  721. bx = Math.Abs(bone.scaleX) * Math.Sign(x);
  722. by = Math.Abs(bone.scaleY) * Math.Sign(y);
  723. bone.scaleX = bx + (x - bx) * alpha;
  724. bone.scaleY = by + (y - by) * alpha;
  725. break;
  726. case MixBlend.Add:
  727. bone.scaleX += (x - bone.data.scaleX) * alpha;
  728. bone.scaleY += (y - bone.data.scaleY) * alpha;
  729. break;
  730. }
  731. }
  732. }
  733. }
  734. }
  735. /// <summary>Changes a bone's local <see cref="Bone.ScaleX">.</summary>
  736. public class ScaleXTimeline : CurveTimeline1, IBoneTimeline {
  737. readonly int boneIndex;
  738. public ScaleXTimeline (int frameCount, int bezierCount, int boneIndex)
  739. : base(frameCount, bezierCount, (int)Property.ScaleX + "|" + boneIndex) {
  740. this.boneIndex = boneIndex;
  741. }
  742. public int BoneIndex {
  743. get {
  744. return boneIndex;
  745. }
  746. }
  747. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  748. MixDirection direction) {
  749. Bone bone = skeleton.bones.Items[boneIndex];
  750. if (bone.active) bone.scaleX = GetScaleValue(time, alpha, blend, direction, bone.scaleX, bone.data.scaleX);
  751. }
  752. }
  753. /// <summary>Changes a bone's local <see cref="Bone.ScaleY">.</summary>
  754. public class ScaleYTimeline : CurveTimeline1, IBoneTimeline {
  755. readonly int boneIndex;
  756. public ScaleYTimeline (int frameCount, int bezierCount, int boneIndex)
  757. : base(frameCount, bezierCount, (int)Property.ScaleY + "|" + boneIndex) {
  758. this.boneIndex = boneIndex;
  759. }
  760. public int BoneIndex {
  761. get {
  762. return boneIndex;
  763. }
  764. }
  765. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  766. MixDirection direction) {
  767. Bone bone = skeleton.bones.Items[boneIndex];
  768. if (bone.active) bone.scaleY = GetScaleValue(time, alpha, blend, direction, bone.scaleY, bone.data.scaleY);
  769. }
  770. }
  771. /// <summary>Changes a bone's local <see cref="Bone.ShearX"/> and <see cref="Bone.ShearY"/>.</summary>
  772. public class ShearTimeline : CurveTimeline2, IBoneTimeline {
  773. readonly int boneIndex;
  774. public ShearTimeline (int frameCount, int bezierCount, int boneIndex)
  775. : base(frameCount, bezierCount, //
  776. (int)Property.ShearX + "|" + boneIndex, //
  777. (int)Property.ShearY + "|" + boneIndex) {
  778. this.boneIndex = boneIndex;
  779. }
  780. public int BoneIndex {
  781. get {
  782. return boneIndex;
  783. }
  784. }
  785. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  786. MixDirection direction) {
  787. Bone bone = skeleton.bones.Items[boneIndex];
  788. if (!bone.active) return;
  789. float[] frames = this.frames;
  790. if (time < frames[0]) { // Time is before first frame.
  791. switch (blend) {
  792. case MixBlend.Setup:
  793. bone.shearX = bone.data.shearX;
  794. bone.shearY = bone.data.shearY;
  795. return;
  796. case MixBlend.First:
  797. bone.shearX += (bone.data.shearX - bone.shearX) * alpha;
  798. bone.shearY += (bone.data.shearY - bone.shearY) * alpha;
  799. return;
  800. }
  801. return;
  802. }
  803. float x, y;
  804. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
  805. switch (curveType) {
  806. case LINEAR:
  807. float before = frames[i];
  808. x = frames[i + VALUE1];
  809. y = frames[i + VALUE2];
  810. float t = (time - before) / (frames[i + ENTRIES] - before);
  811. x += (frames[i + ENTRIES + VALUE1] - x) * t;
  812. y += (frames[i + ENTRIES + VALUE2] - y) * t;
  813. break;
  814. case STEPPED:
  815. x = frames[i + VALUE1];
  816. y = frames[i + VALUE2];
  817. break;
  818. default:
  819. x = GetBezierValue(time, i, VALUE1, curveType - BEZIER);
  820. y = GetBezierValue(time, i, VALUE2, curveType + BEZIER_SIZE - BEZIER);
  821. break;
  822. }
  823. switch (blend) {
  824. case MixBlend.Setup:
  825. bone.shearX = bone.data.shearX + x * alpha;
  826. bone.shearY = bone.data.shearY + y * alpha;
  827. break;
  828. case MixBlend.First:
  829. case MixBlend.Replace:
  830. bone.shearX += (bone.data.shearX + x - bone.shearX) * alpha;
  831. bone.shearY += (bone.data.shearY + y - bone.shearY) * alpha;
  832. break;
  833. case MixBlend.Add:
  834. bone.shearX += x * alpha;
  835. bone.shearY += y * alpha;
  836. break;
  837. }
  838. }
  839. }
  840. /// <summary>Changes a bone's local <see cref="Bone.ShearX"/>.</summary>
  841. public class ShearXTimeline : CurveTimeline1, IBoneTimeline {
  842. readonly int boneIndex;
  843. public ShearXTimeline (int frameCount, int bezierCount, int boneIndex)
  844. : base(frameCount, bezierCount, (int)Property.ShearX + "|" + boneIndex) {
  845. this.boneIndex = boneIndex;
  846. }
  847. public int BoneIndex {
  848. get {
  849. return boneIndex;
  850. }
  851. }
  852. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  853. MixDirection direction) {
  854. Bone bone = skeleton.bones.Items[boneIndex];
  855. if (bone.active) bone.shearX = GetRelativeValue(time, alpha, blend, bone.shearX, bone.data.shearX);
  856. }
  857. }
  858. /// <summary>Changes a bone's local <see cref="Bone.ShearY"/>.</summary>
  859. public class ShearYTimeline : CurveTimeline1, IBoneTimeline {
  860. readonly int boneIndex;
  861. public ShearYTimeline (int frameCount, int bezierCount, int boneIndex)
  862. : base(frameCount, bezierCount, (int)Property.ShearY + "|" + boneIndex) {
  863. this.boneIndex = boneIndex;
  864. }
  865. public int BoneIndex {
  866. get {
  867. return boneIndex;
  868. }
  869. }
  870. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  871. MixDirection direction) {
  872. Bone bone = skeleton.bones.Items[boneIndex];
  873. if (bone.active) bone.shearY = GetRelativeValue(time, alpha, blend, bone.shearY, bone.data.shearY);
  874. }
  875. }
  876. /// <summary>Changes a bone's <see cref="Bone.Inherit"/>.</summary>
  877. public class InheritTimeline : Timeline, IBoneTimeline {
  878. public const int ENTRIES = 2;
  879. public const int INHERIT = 1;
  880. readonly int boneIndex;
  881. public InheritTimeline (int frameCount, int boneIndex)
  882. : base(frameCount, (int)Property.Inherit + "|" + boneIndex) {
  883. this.boneIndex = boneIndex;
  884. }
  885. public int BoneIndex {
  886. get {
  887. return boneIndex;
  888. }
  889. }
  890. public override int FrameEntries {
  891. get { return ENTRIES; }
  892. }
  893. /// <summary>Sets the transform mode for the specified frame.</summary>
  894. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  895. /// <param name="time">The frame time in seconds.</param>
  896. public void SetFrame (int frame, float time, Inherit inherit) {
  897. frame *= ENTRIES;
  898. frames[frame] = time;
  899. frames[frame + INHERIT] = (int)inherit;
  900. }
  901. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  902. MixDirection direction) {
  903. Bone bone = skeleton.bones.Items[boneIndex];
  904. if (!bone.active) return;
  905. if (direction == MixDirection.Out) {
  906. if (blend == MixBlend.Setup) bone.inherit = bone.data.inherit;
  907. return;
  908. }
  909. float[] frames = this.frames;
  910. if (time < frames[0]) {
  911. if (blend == MixBlend.Setup || blend == MixBlend.First) bone.inherit = bone.data.inherit;
  912. return;
  913. }
  914. bone.inherit = InheritEnum.Values[(int)frames[Search(frames, time, ENTRIES) + INHERIT]];
  915. }
  916. }
  917. /// <summary>Changes a slot's <see cref="Slot.Color"/>.</summary>
  918. public class RGBATimeline : CurveTimeline, ISlotTimeline {
  919. public const int ENTRIES = 5;
  920. protected const int R = 1, G = 2, B = 3, A = 4;
  921. readonly int slotIndex;
  922. public RGBATimeline (int frameCount, int bezierCount, int slotIndex)
  923. : base(frameCount, bezierCount, //
  924. (int)Property.RGB + "|" + slotIndex, //
  925. (int)Property.Alpha + "|" + slotIndex) {
  926. this.slotIndex = slotIndex;
  927. }
  928. public override int FrameEntries {
  929. get { return ENTRIES; }
  930. }
  931. public int SlotIndex {
  932. get {
  933. return slotIndex;
  934. }
  935. }
  936. /// <summary>Sets the time and color for the specified frame.</summary>
  937. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  938. /// <param name="time">The frame time in seconds.</param>
  939. public void SetFrame (int frame, float time, float r, float g, float b, float a) {
  940. frame *= ENTRIES;
  941. frames[frame] = time;
  942. frames[frame + R] = r;
  943. frames[frame + G] = g;
  944. frames[frame + B] = b;
  945. frames[frame + A] = a;
  946. }
  947. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  948. MixDirection direction) {
  949. Slot slot = skeleton.slots.Items[slotIndex];
  950. if (!slot.bone.active) return;
  951. float[] frames = this.frames;
  952. if (time < frames[0]) {
  953. SlotData setup = slot.data;
  954. switch (blend) {
  955. case MixBlend.Setup:
  956. slot.r = setup.r;
  957. slot.g = setup.g;
  958. slot.b = setup.b;
  959. slot.a = setup.a;
  960. return;
  961. case MixBlend.First:
  962. slot.r += (setup.r - slot.r) * alpha;
  963. slot.g += (setup.g - slot.g) * alpha;
  964. slot.b += (setup.b - slot.b) * alpha;
  965. slot.a += (setup.a - slot.a) * alpha;
  966. slot.ClampColor();
  967. return;
  968. }
  969. return;
  970. }
  971. float r, g, b, a;
  972. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
  973. switch (curveType) {
  974. case LINEAR:
  975. float before = frames[i];
  976. r = frames[i + R];
  977. g = frames[i + G];
  978. b = frames[i + B];
  979. a = frames[i + A];
  980. float t = (time - before) / (frames[i + ENTRIES] - before);
  981. r += (frames[i + ENTRIES + R] - r) * t;
  982. g += (frames[i + ENTRIES + G] - g) * t;
  983. b += (frames[i + ENTRIES + B] - b) * t;
  984. a += (frames[i + ENTRIES + A] - a) * t;
  985. break;
  986. case STEPPED:
  987. r = frames[i + R];
  988. g = frames[i + G];
  989. b = frames[i + B];
  990. a = frames[i + A];
  991. break;
  992. default:
  993. r = GetBezierValue(time, i, R, curveType - BEZIER);
  994. g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
  995. b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
  996. a = GetBezierValue(time, i, A, curveType + BEZIER_SIZE * 3 - BEZIER);
  997. break;
  998. }
  999. if (alpha == 1) {
  1000. slot.r = r;
  1001. slot.g = g;
  1002. slot.b = b;
  1003. slot.a = a;
  1004. } else {
  1005. float br, bg, bb, ba;
  1006. if (blend == MixBlend.Setup) {
  1007. br = slot.data.r;
  1008. bg = slot.data.g;
  1009. bb = slot.data.b;
  1010. ba = slot.data.a;
  1011. } else {
  1012. br = slot.r;
  1013. bg = slot.g;
  1014. bb = slot.b;
  1015. ba = slot.a;
  1016. }
  1017. slot.r = br + (r - br) * alpha;
  1018. slot.g = bg + (g - bg) * alpha;
  1019. slot.b = bb + (b - bb) * alpha;
  1020. slot.a = ba + (a - ba) * alpha;
  1021. }
  1022. slot.ClampColor();
  1023. }
  1024. }
  1025. /// <summary>Changes the RGB for a slot's <see cref="Slot.Color"/>.</summary>
  1026. public class RGBTimeline : CurveTimeline, ISlotTimeline {
  1027. public const int ENTRIES = 4;
  1028. protected const int R = 1, G = 2, B = 3;
  1029. readonly int slotIndex;
  1030. public RGBTimeline (int frameCount, int bezierCount, int slotIndex)
  1031. : base(frameCount, bezierCount, //
  1032. (int)Property.RGB + "|" + slotIndex) {
  1033. this.slotIndex = slotIndex;
  1034. }
  1035. public override int FrameEntries {
  1036. get { return ENTRIES; }
  1037. }
  1038. public int SlotIndex {
  1039. get {
  1040. return slotIndex;
  1041. }
  1042. }
  1043. /// <summary>Sets the time and color for the specified frame.</summary>
  1044. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1045. /// <param name="time">The frame time in seconds.</param>
  1046. public void SetFrame (int frame, float time, float r, float g, float b) {
  1047. frame <<= 2;
  1048. frames[frame] = time;
  1049. frames[frame + R] = r;
  1050. frames[frame + G] = g;
  1051. frames[frame + B] = b;
  1052. }
  1053. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1054. MixDirection direction) {
  1055. Slot slot = skeleton.slots.Items[slotIndex];
  1056. if (!slot.bone.active) return;
  1057. float[] frames = this.frames;
  1058. if (time < frames[0]) {
  1059. SlotData setup = slot.data;
  1060. switch (blend) {
  1061. case MixBlend.Setup:
  1062. slot.r = setup.r;
  1063. slot.g = setup.g;
  1064. slot.b = setup.b;
  1065. return;
  1066. case MixBlend.First:
  1067. slot.r += (setup.r - slot.r) * alpha;
  1068. slot.g += (setup.g - slot.g) * alpha;
  1069. slot.b += (setup.b - slot.b) * alpha;
  1070. slot.ClampColor();
  1071. return;
  1072. }
  1073. return;
  1074. }
  1075. float r, g, b;
  1076. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i >> 2];
  1077. switch (curveType) {
  1078. case LINEAR:
  1079. float before = frames[i];
  1080. r = frames[i + R];
  1081. g = frames[i + G];
  1082. b = frames[i + B];
  1083. float t = (time - before) / (frames[i + ENTRIES] - before);
  1084. r += (frames[i + ENTRIES + R] - r) * t;
  1085. g += (frames[i + ENTRIES + G] - g) * t;
  1086. b += (frames[i + ENTRIES + B] - b) * t;
  1087. break;
  1088. case STEPPED:
  1089. r = frames[i + R];
  1090. g = frames[i + G];
  1091. b = frames[i + B];
  1092. break;
  1093. default:
  1094. r = GetBezierValue(time, i, R, curveType - BEZIER);
  1095. g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
  1096. b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
  1097. break;
  1098. }
  1099. if (alpha == 1) {
  1100. slot.r = r;
  1101. slot.g = g;
  1102. slot.b = b;
  1103. } else {
  1104. float br, bg, bb;
  1105. if (blend == MixBlend.Setup) {
  1106. SlotData setup = slot.data;
  1107. br = setup.r;
  1108. bg = setup.g;
  1109. bb = setup.b;
  1110. } else {
  1111. br = slot.r;
  1112. bg = slot.g;
  1113. bb = slot.b;
  1114. }
  1115. slot.r = br + (r - br) * alpha;
  1116. slot.g = bg + (g - bg) * alpha;
  1117. slot.b = bb + (b - bb) * alpha;
  1118. }
  1119. slot.ClampColor();
  1120. }
  1121. }
  1122. /// <summary>Changes the alpha for a slot's <see cref="Slot.Color"/>.</summary>
  1123. public class AlphaTimeline : CurveTimeline1, ISlotTimeline {
  1124. readonly int slotIndex;
  1125. public AlphaTimeline (int frameCount, int bezierCount, int slotIndex)
  1126. : base(frameCount, bezierCount, (int)Property.Alpha + "|" + slotIndex) {
  1127. this.slotIndex = slotIndex;
  1128. }
  1129. public int SlotIndex {
  1130. get {
  1131. return slotIndex;
  1132. }
  1133. }
  1134. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1135. MixDirection direction) {
  1136. Slot slot = skeleton.slots.Items[slotIndex];
  1137. if (!slot.bone.active) return;
  1138. float[] frames = this.frames;
  1139. if (time < frames[0]) {
  1140. SlotData setup = slot.data;
  1141. switch (blend) {
  1142. case MixBlend.Setup:
  1143. slot.a = setup.a;
  1144. return;
  1145. case MixBlend.First:
  1146. slot.a += (setup.a - slot.a) * alpha;
  1147. slot.ClampColor();
  1148. return;
  1149. }
  1150. return;
  1151. }
  1152. float a = GetCurveValue(time);
  1153. if (alpha == 1)
  1154. slot.a = a;
  1155. else {
  1156. if (blend == MixBlend.Setup) slot.a = slot.data.a;
  1157. slot.a += (a - slot.a) * alpha;
  1158. }
  1159. slot.ClampColor();
  1160. }
  1161. }
  1162. /// <summary>Changes a slot's <see cref="Slot.Color"/> and <see cref="Slot.DarkColor"/> for two color tinting.</summary>
  1163. public class RGBA2Timeline : CurveTimeline, ISlotTimeline {
  1164. public const int ENTRIES = 8;
  1165. protected const int R = 1, G = 2, B = 3, A = 4, R2 = 5, G2 = 6, B2 = 7;
  1166. readonly int slotIndex;
  1167. public RGBA2Timeline (int frameCount, int bezierCount, int slotIndex)
  1168. : base(frameCount, bezierCount, //
  1169. (int)Property.RGB + "|" + slotIndex, //
  1170. (int)Property.Alpha + "|" + slotIndex, //
  1171. (int)Property.RGB2 + "|" + slotIndex) {
  1172. this.slotIndex = slotIndex;
  1173. }
  1174. public override int FrameEntries {
  1175. get {
  1176. return ENTRIES;
  1177. }
  1178. }
  1179. /// <summary>
  1180. /// The index of the slot in <see cref="Skeleton.Slots"/> that will be changed when this timeline is applied. The
  1181. /// <see cref="Slot"/> must have a dark color available.</summary>
  1182. public int SlotIndex {
  1183. get {
  1184. return slotIndex;
  1185. }
  1186. }
  1187. /// <summary>Sets the time, light color, and dark color for the specified frame.</summary>
  1188. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1189. /// <param name="time">The frame time in seconds.</param>
  1190. public void SetFrame (int frame, float time, float r, float g, float b, float a, float r2, float g2, float b2) {
  1191. frame <<= 3;
  1192. frames[frame] = time;
  1193. frames[frame + R] = r;
  1194. frames[frame + G] = g;
  1195. frames[frame + B] = b;
  1196. frames[frame + A] = a;
  1197. frames[frame + R2] = r2;
  1198. frames[frame + G2] = g2;
  1199. frames[frame + B2] = b2;
  1200. }
  1201. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1202. MixDirection direction) {
  1203. Slot slot = skeleton.slots.Items[slotIndex];
  1204. if (!slot.bone.active) return;
  1205. float[] frames = this.frames;
  1206. if (time < frames[0]) {
  1207. SlotData setup = slot.data;
  1208. switch (blend) {
  1209. case MixBlend.Setup:
  1210. slot.r = setup.r;
  1211. slot.g = setup.g;
  1212. slot.b = setup.b;
  1213. slot.a = setup.a;
  1214. slot.ClampColor();
  1215. slot.r2 = setup.r2;
  1216. slot.g2 = setup.g2;
  1217. slot.b2 = setup.b2;
  1218. slot.ClampSecondColor();
  1219. return;
  1220. case MixBlend.First:
  1221. slot.r += (slot.r - setup.r) * alpha;
  1222. slot.g += (slot.g - setup.g) * alpha;
  1223. slot.b += (slot.b - setup.b) * alpha;
  1224. slot.a += (slot.a - setup.a) * alpha;
  1225. slot.ClampColor();
  1226. slot.r2 += (slot.r2 - setup.r2) * alpha;
  1227. slot.g2 += (slot.g2 - setup.g2) * alpha;
  1228. slot.b2 += (slot.b2 - setup.b2) * alpha;
  1229. slot.ClampSecondColor();
  1230. return;
  1231. }
  1232. return;
  1233. }
  1234. float r, g, b, a, r2, g2, b2;
  1235. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i >> 3];
  1236. switch (curveType) {
  1237. case LINEAR:
  1238. float before = frames[i];
  1239. r = frames[i + R];
  1240. g = frames[i + G];
  1241. b = frames[i + B];
  1242. a = frames[i + A];
  1243. r2 = frames[i + R2];
  1244. g2 = frames[i + G2];
  1245. b2 = frames[i + B2];
  1246. float t = (time - before) / (frames[i + ENTRIES] - before);
  1247. r += (frames[i + ENTRIES + R] - r) * t;
  1248. g += (frames[i + ENTRIES + G] - g) * t;
  1249. b += (frames[i + ENTRIES + B] - b) * t;
  1250. a += (frames[i + ENTRIES + A] - a) * t;
  1251. r2 += (frames[i + ENTRIES + R2] - r2) * t;
  1252. g2 += (frames[i + ENTRIES + G2] - g2) * t;
  1253. b2 += (frames[i + ENTRIES + B2] - b2) * t;
  1254. break;
  1255. case STEPPED:
  1256. r = frames[i + R];
  1257. g = frames[i + G];
  1258. b = frames[i + B];
  1259. a = frames[i + A];
  1260. r2 = frames[i + R2];
  1261. g2 = frames[i + G2];
  1262. b2 = frames[i + B2];
  1263. break;
  1264. default:
  1265. r = GetBezierValue(time, i, R, curveType - BEZIER);
  1266. g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
  1267. b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
  1268. a = GetBezierValue(time, i, A, curveType + BEZIER_SIZE * 3 - BEZIER);
  1269. r2 = GetBezierValue(time, i, R2, curveType + BEZIER_SIZE * 4 - BEZIER);
  1270. g2 = GetBezierValue(time, i, G2, curveType + BEZIER_SIZE * 5 - BEZIER);
  1271. b2 = GetBezierValue(time, i, B2, curveType + BEZIER_SIZE * 6 - BEZIER);
  1272. break;
  1273. }
  1274. if (alpha == 1) {
  1275. slot.r = r;
  1276. slot.g = g;
  1277. slot.b = b;
  1278. slot.a = a;
  1279. slot.r2 = r2;
  1280. slot.g2 = g2;
  1281. slot.b2 = b2;
  1282. } else {
  1283. float br, bg, bb, ba, br2, bg2, bb2;
  1284. if (blend == MixBlend.Setup) {
  1285. br = slot.data.r;
  1286. bg = slot.data.g;
  1287. bb = slot.data.b;
  1288. ba = slot.data.a;
  1289. br2 = slot.data.r2;
  1290. bg2 = slot.data.g2;
  1291. bb2 = slot.data.b2;
  1292. } else {
  1293. br = slot.r;
  1294. bg = slot.g;
  1295. bb = slot.b;
  1296. ba = slot.a;
  1297. br2 = slot.r2;
  1298. bg2 = slot.g2;
  1299. bb2 = slot.b2;
  1300. }
  1301. slot.r = br + (r - br) * alpha;
  1302. slot.g = bg + (g - bg) * alpha;
  1303. slot.b = bb + (b - bb) * alpha;
  1304. slot.a = ba + (a - ba) * alpha;
  1305. slot.r2 = br2 + (r2 - br2) * alpha;
  1306. slot.g2 = bg2 + (g2 - bg2) * alpha;
  1307. slot.b2 = bb2 + (b2 - bb2) * alpha;
  1308. }
  1309. slot.ClampColor();
  1310. slot.ClampSecondColor();
  1311. }
  1312. }
  1313. /// <summary>Changes the RGB for a slot's <see cref="Slot.Color"/> and <see cref="Slot.DarkColor"/> for two color tinting.</summary>
  1314. public class RGB2Timeline : CurveTimeline, ISlotTimeline {
  1315. public const int ENTRIES = 7;
  1316. protected const int R = 1, G = 2, B = 3, R2 = 4, G2 = 5, B2 = 6;
  1317. readonly int slotIndex;
  1318. public RGB2Timeline (int frameCount, int bezierCount, int slotIndex)
  1319. : base(frameCount, bezierCount, //
  1320. (int)Property.RGB + "|" + slotIndex, //
  1321. (int)Property.RGB2 + "|" + slotIndex) {
  1322. this.slotIndex = slotIndex;
  1323. }
  1324. public override int FrameEntries {
  1325. get {
  1326. return ENTRIES;
  1327. }
  1328. }
  1329. /// <summary>
  1330. /// The index of the slot in <see cref="Skeleton.Slots"/> that will be changed when this timeline is applied. The
  1331. /// <see cref="Slot"/> must have a dark color available.</summary>
  1332. public int SlotIndex {
  1333. get {
  1334. return slotIndex;
  1335. }
  1336. }
  1337. /// <summary>Sets the time, light color, and dark color for the specified frame.</summary>
  1338. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1339. /// <param name="time">The frame time in seconds.</param>
  1340. public void SetFrame (int frame, float time, float r, float g, float b, float r2, float g2, float b2) {
  1341. frame *= ENTRIES;
  1342. frames[frame] = time;
  1343. frames[frame + R] = r;
  1344. frames[frame + G] = g;
  1345. frames[frame + B] = b;
  1346. frames[frame + R2] = r2;
  1347. frames[frame + G2] = g2;
  1348. frames[frame + B2] = b2;
  1349. }
  1350. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1351. MixDirection direction) {
  1352. Slot slot = skeleton.slots.Items[slotIndex];
  1353. if (!slot.bone.active) return;
  1354. float[] frames = this.frames;
  1355. if (time < frames[0]) {
  1356. SlotData setup = slot.data;
  1357. switch (blend) {
  1358. case MixBlend.Setup:
  1359. slot.r = setup.r;
  1360. slot.g = setup.g;
  1361. slot.b = setup.b;
  1362. slot.ClampColor();
  1363. slot.r2 = setup.r2;
  1364. slot.g2 = setup.g2;
  1365. slot.b2 = setup.b2;
  1366. slot.ClampSecondColor();
  1367. return;
  1368. case MixBlend.First:
  1369. slot.r += (slot.r - setup.r) * alpha;
  1370. slot.g += (slot.g - setup.g) * alpha;
  1371. slot.b += (slot.b - setup.b) * alpha;
  1372. slot.ClampColor();
  1373. slot.r2 += (slot.r2 - setup.r2) * alpha;
  1374. slot.g2 += (slot.g2 - setup.g2) * alpha;
  1375. slot.b2 += (slot.b2 - setup.b2) * alpha;
  1376. slot.ClampSecondColor();
  1377. return;
  1378. }
  1379. return;
  1380. }
  1381. float r, g, b, r2, g2, b2;
  1382. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
  1383. switch (curveType) {
  1384. case LINEAR:
  1385. float before = frames[i];
  1386. r = frames[i + R];
  1387. g = frames[i + G];
  1388. b = frames[i + B];
  1389. r2 = frames[i + R2];
  1390. g2 = frames[i + G2];
  1391. b2 = frames[i + B2];
  1392. float t = (time - before) / (frames[i + ENTRIES] - before);
  1393. r += (frames[i + ENTRIES + R] - r) * t;
  1394. g += (frames[i + ENTRIES + G] - g) * t;
  1395. b += (frames[i + ENTRIES + B] - b) * t;
  1396. r2 += (frames[i + ENTRIES + R2] - r2) * t;
  1397. g2 += (frames[i + ENTRIES + G2] - g2) * t;
  1398. b2 += (frames[i + ENTRIES + B2] - b2) * t;
  1399. break;
  1400. case STEPPED:
  1401. r = frames[i + R];
  1402. g = frames[i + G];
  1403. b = frames[i + B];
  1404. r2 = frames[i + R2];
  1405. g2 = frames[i + G2];
  1406. b2 = frames[i + B2];
  1407. break;
  1408. default:
  1409. r = GetBezierValue(time, i, R, curveType - BEZIER);
  1410. g = GetBezierValue(time, i, G, curveType + BEZIER_SIZE - BEZIER);
  1411. b = GetBezierValue(time, i, B, curveType + BEZIER_SIZE * 2 - BEZIER);
  1412. r2 = GetBezierValue(time, i, R2, curveType + BEZIER_SIZE * 3 - BEZIER);
  1413. g2 = GetBezierValue(time, i, G2, curveType + BEZIER_SIZE * 4 - BEZIER);
  1414. b2 = GetBezierValue(time, i, B2, curveType + BEZIER_SIZE * 5 - BEZIER);
  1415. break;
  1416. }
  1417. if (alpha == 1) {
  1418. slot.r = r;
  1419. slot.g = g;
  1420. slot.b = b;
  1421. slot.r2 = r2;
  1422. slot.g2 = g2;
  1423. slot.b2 = b2;
  1424. } else {
  1425. float br, bg, bb, br2, bg2, bb2;
  1426. if (blend == MixBlend.Setup) {
  1427. SlotData setup = slot.data;
  1428. br = setup.r;
  1429. bg = setup.g;
  1430. bb = setup.b;
  1431. br2 = setup.r2;
  1432. bg2 = setup.g2;
  1433. bb2 = setup.b2;
  1434. } else {
  1435. br = slot.r;
  1436. bg = slot.g;
  1437. bb = slot.b;
  1438. br2 = slot.r2;
  1439. bg2 = slot.g2;
  1440. bb2 = slot.b2;
  1441. }
  1442. slot.r = br + (r - br) * alpha;
  1443. slot.g = bg + (g - bg) * alpha;
  1444. slot.b = bb + (b - bb) * alpha;
  1445. slot.r2 = br2 + (r2 - br2) * alpha;
  1446. slot.g2 = bg2 + (g2 - bg2) * alpha;
  1447. slot.b2 = bb2 + (b2 - bb2) * alpha;
  1448. }
  1449. slot.ClampColor();
  1450. slot.ClampSecondColor();
  1451. }
  1452. }
  1453. /// <summary>Changes a slot's <see cref="Slot.Attachment"/>.</summary>
  1454. public class AttachmentTimeline : Timeline, ISlotTimeline {
  1455. readonly int slotIndex;
  1456. readonly string[] attachmentNames;
  1457. public AttachmentTimeline (int frameCount, int slotIndex)
  1458. : base(frameCount, (int)Property.Attachment + "|" + slotIndex) {
  1459. this.slotIndex = slotIndex;
  1460. attachmentNames = new String[frameCount];
  1461. }
  1462. public int SlotIndex {
  1463. get {
  1464. return slotIndex;
  1465. }
  1466. }
  1467. /// <summary>The attachment name for each frame. May contain null values to clear the attachment. </summary>
  1468. public string[] AttachmentNames {
  1469. get {
  1470. return attachmentNames;
  1471. }
  1472. }
  1473. /// <summary>Sets the time and attachment name for the specified frame.</summary>
  1474. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1475. /// <param name="time">The frame time in seconds.</param>
  1476. public void SetFrame (int frame, float time, String attachmentName) {
  1477. frames[frame] = time;
  1478. attachmentNames[frame] = attachmentName;
  1479. }
  1480. public override void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1481. MixDirection direction) {
  1482. Slot slot = skeleton.slots.Items[slotIndex];
  1483. if (!slot.bone.active) return;
  1484. if (direction == MixDirection.Out) {
  1485. if (blend == MixBlend.Setup) SetAttachment(skeleton, slot, slot.data.attachmentName);
  1486. return;
  1487. }
  1488. float[] frames = this.frames;
  1489. if (time < frames[0]) {
  1490. if (blend == MixBlend.Setup || blend == MixBlend.First) SetAttachment(skeleton, slot, slot.data.attachmentName);
  1491. return;
  1492. }
  1493. SetAttachment(skeleton, slot, attachmentNames[Search(frames, time)]);
  1494. }
  1495. private void SetAttachment (Skeleton skeleton, Slot slot, string attachmentName) {
  1496. slot.Attachment = attachmentName == null ? null : skeleton.GetAttachment(slotIndex, attachmentName);
  1497. }
  1498. }
  1499. /// <summary>Changes a slot's <see cref="Slot.Deform"/> to deform a <see cref="VertexAttachment"/>.</summary>
  1500. public class DeformTimeline : CurveTimeline, ISlotTimeline {
  1501. readonly int slotIndex;
  1502. readonly VertexAttachment attachment;
  1503. internal float[][] vertices;
  1504. public DeformTimeline (int frameCount, int bezierCount, int slotIndex, VertexAttachment attachment)
  1505. : base(frameCount, bezierCount, (int)Property.Deform + "|" + slotIndex + "|" + attachment.Id) {
  1506. this.slotIndex = slotIndex;
  1507. this.attachment = attachment;
  1508. vertices = new float[frameCount][];
  1509. }
  1510. public int SlotIndex {
  1511. get {
  1512. return slotIndex;
  1513. }
  1514. }
  1515. /// <summary>The attachment that will be deformed.</summary>
  1516. /// <seealso cref="VertexAttachment.TimelineAttachment"/>
  1517. public VertexAttachment Attachment {
  1518. get {
  1519. return attachment;
  1520. }
  1521. }
  1522. /// <summary>The vertices for each frame.</summary>
  1523. public float[][] Vertices {
  1524. get {
  1525. return vertices;
  1526. }
  1527. }
  1528. /// <summary>Sets the time and vertices for the specified frame.</summary>
  1529. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1530. /// <param name="time">The frame time in seconds.</param>
  1531. /// <param name="vertices">Vertex positions for an unweighted VertexAttachment, or deform offsets if it has weights.</param>
  1532. public void SetFrame (int frame, float time, float[] vertices) {
  1533. frames[frame] = time;
  1534. this.vertices[frame] = vertices;
  1535. }
  1536. /// <param name="value1">Ignored (0 is used for a deform timeline).</param>
  1537. /// <param name="value2">Ignored (1 is used for a deform timeline).</param>
  1538. public void setBezier (int bezier, int frame, int value, float time1, float value1, float cx1, float cy1, float cx2,
  1539. float cy2, float time2, float value2) {
  1540. float[] curves = this.curves;
  1541. int i = FrameCount + bezier * BEZIER_SIZE;
  1542. if (value == 0) curves[frame] = BEZIER + i;
  1543. float tmpx = (time1 - cx1 * 2 + cx2) * 0.03f, tmpy = cy2 * 0.03f - cy1 * 0.06f;
  1544. float dddx = ((cx1 - cx2) * 3 - time1 + time2) * 0.006f, dddy = (cy1 - cy2 + 0.33333333f) * 0.018f;
  1545. float ddx = tmpx * 2 + dddx, ddy = tmpy * 2 + dddy;
  1546. float dx = (cx1 - time1) * 0.3f + tmpx + dddx * 0.16666667f, dy = cy1 * 0.3f + tmpy + dddy * 0.16666667f;
  1547. float x = time1 + dx, y = dy;
  1548. for (int n = i + BEZIER_SIZE; i < n; i += 2) {
  1549. curves[i] = x;
  1550. curves[i + 1] = y;
  1551. dx += ddx;
  1552. dy += ddy;
  1553. ddx += dddx;
  1554. ddy += dddy;
  1555. x += dx;
  1556. y += dy;
  1557. }
  1558. }
  1559. /// <summary>Returns the interpolated percentage for the specified time.</summary>
  1560. /// <param name="frame">The frame before <code>time</code>.</param>
  1561. private float GetCurvePercent (float time, int frame) {
  1562. float[] curves = this.curves;
  1563. int i = (int)curves[frame];
  1564. switch (i) {
  1565. case LINEAR:
  1566. float x = frames[frame];
  1567. return (time - x) / (frames[frame + FrameEntries] - x);
  1568. case STEPPED:
  1569. return 0;
  1570. }
  1571. i -= BEZIER;
  1572. if (curves[i] > time) {
  1573. float x = frames[frame];
  1574. return curves[i + 1] * (time - x) / (curves[i] - x);
  1575. }
  1576. int n = i + BEZIER_SIZE;
  1577. for (i += 2; i < n; i += 2) {
  1578. if (curves[i] >= time) {
  1579. float x = curves[i - 2], y = curves[i - 1];
  1580. return y + (time - x) / (curves[i] - x) * (curves[i + 1] - y);
  1581. }
  1582. }
  1583. { // scope added to prevent compile error "float x and y declared in enclosing scope"
  1584. float x = curves[n - 2], y = curves[n - 1];
  1585. return y + (1 - y) * (time - x) / (frames[frame + FrameEntries] - x);
  1586. }
  1587. }
  1588. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1589. MixDirection direction) {
  1590. Slot slot = skeleton.slots.Items[slotIndex];
  1591. if (!slot.bone.active) return;
  1592. VertexAttachment vertexAttachment = slot.attachment as VertexAttachment;
  1593. if (vertexAttachment == null || vertexAttachment.TimelineAttachment != attachment) return;
  1594. ExposedList<float> deformArray = slot.deform;
  1595. if (deformArray.Count == 0) blend = MixBlend.Setup;
  1596. float[][] vertices = this.vertices;
  1597. int vertexCount = vertices[0].Length;
  1598. float[] deform;
  1599. float[] frames = this.frames;
  1600. if (time < frames[0]) {
  1601. switch (blend) {
  1602. case MixBlend.Setup:
  1603. deformArray.Clear();
  1604. return;
  1605. case MixBlend.First:
  1606. if (alpha == 1) {
  1607. deformArray.Clear();
  1608. return;
  1609. }
  1610. // Ensure size and preemptively set count.
  1611. if (deformArray.Capacity < vertexCount) deformArray.Capacity = vertexCount;
  1612. deformArray.Count = vertexCount;
  1613. deform = deformArray.Items;
  1614. if (vertexAttachment.bones == null) {
  1615. // Unweighted vertex positions.
  1616. float[] setupVertices = vertexAttachment.vertices;
  1617. for (int i = 0; i < vertexCount; i++)
  1618. deform[i] += (setupVertices[i] - deform[i]) * alpha;
  1619. } else {
  1620. // Weighted deform offsets.
  1621. alpha = 1 - alpha;
  1622. for (int i = 0; i < vertexCount; i++)
  1623. deform[i] *= alpha;
  1624. }
  1625. return;
  1626. }
  1627. return;
  1628. }
  1629. // Ensure size and preemptively set count.
  1630. if (deformArray.Capacity < vertexCount) deformArray.Capacity = vertexCount;
  1631. deformArray.Count = vertexCount;
  1632. deform = deformArray.Items;
  1633. if (time >= frames[frames.Length - 1]) { // Time is after last frame.
  1634. float[] lastVertices = vertices[frames.Length - 1];
  1635. if (alpha == 1) {
  1636. if (blend == MixBlend.Add) {
  1637. if (vertexAttachment.bones == null) {
  1638. // Unweighted vertex positions, no alpha.
  1639. float[] setupVertices = vertexAttachment.vertices;
  1640. for (int i = 0; i < vertexCount; i++)
  1641. deform[i] += lastVertices[i] - setupVertices[i];
  1642. } else {
  1643. // Weighted deform offsets, no alpha.
  1644. for (int i = 0; i < vertexCount; i++)
  1645. deform[i] += lastVertices[i];
  1646. }
  1647. } else {
  1648. // Vertex positions or deform offsets, no alpha.
  1649. Array.Copy(lastVertices, 0, deform, 0, vertexCount);
  1650. }
  1651. } else {
  1652. switch (blend) {
  1653. case MixBlend.Setup: {
  1654. if (vertexAttachment.bones == null) {
  1655. // Unweighted vertex positions, with alpha.
  1656. float[] setupVertices = vertexAttachment.vertices;
  1657. for (int i = 0; i < vertexCount; i++) {
  1658. float setup = setupVertices[i];
  1659. deform[i] = setup + (lastVertices[i] - setup) * alpha;
  1660. }
  1661. } else {
  1662. // Weighted deform offsets, with alpha.
  1663. for (int i = 0; i < vertexCount; i++)
  1664. deform[i] = lastVertices[i] * alpha;
  1665. }
  1666. break;
  1667. }
  1668. case MixBlend.First:
  1669. case MixBlend.Replace:
  1670. // Vertex positions or deform offsets, with alpha.
  1671. for (int i = 0; i < vertexCount; i++)
  1672. deform[i] += (lastVertices[i] - deform[i]) * alpha;
  1673. break;
  1674. case MixBlend.Add:
  1675. if (vertexAttachment.bones == null) {
  1676. // Unweighted vertex positions, no alpha.
  1677. float[] setupVertices = vertexAttachment.vertices;
  1678. for (int i = 0; i < vertexCount; i++)
  1679. deform[i] += (lastVertices[i] - setupVertices[i]) * alpha;
  1680. } else {
  1681. // Weighted deform offsets, alpha.
  1682. for (int i = 0; i < vertexCount; i++)
  1683. deform[i] += lastVertices[i] * alpha;
  1684. }
  1685. break;
  1686. }
  1687. }
  1688. return;
  1689. }
  1690. int frame = Search(frames, time);
  1691. float percent = GetCurvePercent(time, frame);
  1692. float[] prevVertices = vertices[frame];
  1693. float[] nextVertices = vertices[frame + 1];
  1694. if (alpha == 1) {
  1695. if (blend == MixBlend.Add) {
  1696. if (vertexAttachment.bones == null) {
  1697. // Unweighted vertex positions, no alpha.
  1698. float[] setupVertices = vertexAttachment.vertices;
  1699. for (int i = 0; i < vertexCount; i++) {
  1700. float prev = prevVertices[i];
  1701. deform[i] += prev + (nextVertices[i] - prev) * percent - setupVertices[i];
  1702. }
  1703. } else {
  1704. // Weighted deform offsets, no alpha.
  1705. for (int i = 0; i < vertexCount; i++) {
  1706. float prev = prevVertices[i];
  1707. deform[i] += prev + (nextVertices[i] - prev) * percent;
  1708. }
  1709. }
  1710. } else {
  1711. // Vertex positions or deform offsets, no alpha.
  1712. for (int i = 0; i < vertexCount; i++) {
  1713. float prev = prevVertices[i];
  1714. deform[i] = prev + (nextVertices[i] - prev) * percent;
  1715. }
  1716. }
  1717. } else {
  1718. switch (blend) {
  1719. case MixBlend.Setup: {
  1720. if (vertexAttachment.bones == null) {
  1721. // Unweighted vertex positions, with alpha.
  1722. float[] setupVertices = vertexAttachment.vertices;
  1723. for (int i = 0; i < vertexCount; i++) {
  1724. float prev = prevVertices[i], setup = setupVertices[i];
  1725. deform[i] = setup + (prev + (nextVertices[i] - prev) * percent - setup) * alpha;
  1726. }
  1727. } else {
  1728. // Weighted deform offsets, with alpha.
  1729. for (int i = 0; i < vertexCount; i++) {
  1730. float prev = prevVertices[i];
  1731. deform[i] = (prev + (nextVertices[i] - prev) * percent) * alpha;
  1732. }
  1733. }
  1734. break;
  1735. }
  1736. case MixBlend.First:
  1737. case MixBlend.Replace: {
  1738. // Vertex positions or deform offsets, with alpha.
  1739. for (int i = 0; i < vertexCount; i++) {
  1740. float prev = prevVertices[i];
  1741. deform[i] += (prev + (nextVertices[i] - prev) * percent - deform[i]) * alpha;
  1742. }
  1743. break;
  1744. }
  1745. case MixBlend.Add:
  1746. if (vertexAttachment.bones == null) {
  1747. // Unweighted vertex positions, with alpha.
  1748. float[] setupVertices = vertexAttachment.vertices;
  1749. for (int i = 0; i < vertexCount; i++) {
  1750. float prev = prevVertices[i];
  1751. deform[i] += (prev + (nextVertices[i] - prev) * percent - setupVertices[i]) * alpha;
  1752. }
  1753. } else {
  1754. // Weighted deform offsets, with alpha.
  1755. for (int i = 0; i < vertexCount; i++) {
  1756. float prev = prevVertices[i];
  1757. deform[i] += (prev + (nextVertices[i] - prev) * percent) * alpha;
  1758. }
  1759. }
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. }
  1765. /// <summary>Fires an <see cref="Event"/> when specific animation times are reached.</summary>
  1766. public class EventTimeline : Timeline {
  1767. readonly static string[] propertyIds = { ((int)Property.Event).ToString() };
  1768. readonly Event[] events;
  1769. public EventTimeline (int frameCount)
  1770. : base(frameCount, propertyIds) {
  1771. events = new Event[frameCount];
  1772. }
  1773. /// <summary>The event for each frame.</summary>
  1774. public Event[] Events {
  1775. get {
  1776. return events;
  1777. }
  1778. }
  1779. /// <summary>Sets the time and event for the specified frame.</summary>
  1780. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1781. public void SetFrame (int frame, Event e) {
  1782. frames[frame] = e.time;
  1783. events[frame] = e;
  1784. }
  1785. /// <summary>Fires events for frames &gt; <code>lastTime</code> and &lt;= <code>time</code>.</summary>
  1786. public override void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha,
  1787. MixBlend blend, MixDirection direction) {
  1788. if (firedEvents == null) return;
  1789. float[] frames = this.frames;
  1790. int frameCount = frames.Length;
  1791. if (lastTime > time) { // Apply after lastTime for looped animations.
  1792. Apply(skeleton, lastTime, int.MaxValue, firedEvents, alpha, blend, direction);
  1793. lastTime = -1f;
  1794. } else if (lastTime >= frames[frameCount - 1]) // Last time is after last frame.
  1795. return;
  1796. if (time < frames[0]) return;
  1797. int i;
  1798. if (lastTime < frames[0])
  1799. i = 0;
  1800. else {
  1801. i = Search(frames, lastTime) + 1;
  1802. float frameTime = frames[i];
  1803. while (i > 0) { // Fire multiple events with the same frame.
  1804. if (frames[i - 1] != frameTime) break;
  1805. i--;
  1806. }
  1807. }
  1808. for (; i < frameCount && time >= frames[i]; i++)
  1809. firedEvents.Add(events[i]);
  1810. }
  1811. }
  1812. /// <summary>Changes a skeleton's <see cref="Skeleton.DrawOrder"/>.</summary>
  1813. public class DrawOrderTimeline : Timeline {
  1814. static readonly string[] propertyIds = { ((int)Property.DrawOrder).ToString() };
  1815. readonly int[][] drawOrders;
  1816. public DrawOrderTimeline (int frameCount)
  1817. : base(frameCount, propertyIds) {
  1818. drawOrders = new int[frameCount][];
  1819. }
  1820. /// <summary>The draw order for each frame. </summary>
  1821. /// <seealso cref="Timeline.SetFrame(int, float, int[])"/>.
  1822. public int[][] DrawOrders {
  1823. get {
  1824. return drawOrders;
  1825. }
  1826. }
  1827. /// <summary>Sets the time and draw order for the specified frame.</summary>
  1828. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1829. /// <param name="time">The frame time in seconds.</param>
  1830. /// <param name="drawOrder">For each slot in <see cref="Skeleton.Slots"/>, the index of the slot in the new draw order. May be null to use
  1831. /// setup pose draw order.</param>
  1832. public void SetFrame (int frame, float time, int[] drawOrder) {
  1833. frames[frame] = time;
  1834. drawOrders[frame] = drawOrder;
  1835. }
  1836. public override void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1837. MixDirection direction) {
  1838. if (direction == MixDirection.Out) {
  1839. if (blend == MixBlend.Setup) Array.Copy(skeleton.slots.Items, 0, skeleton.drawOrder.Items, 0, skeleton.slots.Count);
  1840. return;
  1841. }
  1842. float[] frames = this.frames;
  1843. if (time < frames[0]) {
  1844. if (blend == MixBlend.Setup || blend == MixBlend.First) Array.Copy(skeleton.slots.Items, 0, skeleton.drawOrder.Items, 0, skeleton.slots.Count);
  1845. return;
  1846. }
  1847. int[] drawOrderToSetupIndex = drawOrders[Search(frames, time)];
  1848. if (drawOrderToSetupIndex == null)
  1849. Array.Copy(skeleton.slots.Items, 0, skeleton.drawOrder.Items, 0, skeleton.slots.Count);
  1850. else {
  1851. Slot[] slots = skeleton.slots.Items;
  1852. Slot[] drawOrder = skeleton.drawOrder.Items;
  1853. for (int i = 0, n = drawOrderToSetupIndex.Length; i < n; i++)
  1854. drawOrder[i] = slots[drawOrderToSetupIndex[i]];
  1855. }
  1856. }
  1857. }
  1858. /// <summary>Changes an IK constraint's <see cref="IkConstraint.Mix"/>, <see cref="IkConstraint.Softness"/>,
  1859. /// <see cref="IkConstraint.BendDirection"/>, <see cref="IkConstraint.Stretch"/>, and <see cref="IkConstraint.Compress"/>.</summary>
  1860. public class IkConstraintTimeline : CurveTimeline {
  1861. public const int ENTRIES = 6;
  1862. private const int MIX = 1, SOFTNESS = 2, BEND_DIRECTION = 3, COMPRESS = 4, STRETCH = 5;
  1863. readonly int constraintIndex;
  1864. public IkConstraintTimeline (int frameCount, int bezierCount, int ikConstraintIndex)
  1865. : base(frameCount, bezierCount, (int)Property.IkConstraint + "|" + ikConstraintIndex) {
  1866. this.constraintIndex = ikConstraintIndex;
  1867. }
  1868. public override int FrameEntries {
  1869. get {
  1870. return ENTRIES;
  1871. }
  1872. }
  1873. /// <summary>The index of the IK constraint in <see cref="Skeleton.IkConstraints"/> that will be changed when this timeline is
  1874. /// applied.</summary>
  1875. public int IkConstraintIndex {
  1876. get {
  1877. return constraintIndex;
  1878. }
  1879. }
  1880. /// <summary>Sets the time, mix, softness, bend direction, compress, and stretch for the specified frame.</summary>
  1881. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1882. /// <param name="time">The frame time in seconds.</param>
  1883. /// <param name="bendDirection">1 or -1.</param>
  1884. public void SetFrame (int frame, float time, float mix, float softness, int bendDirection, bool compress,
  1885. bool stretch) {
  1886. frame *= ENTRIES;
  1887. frames[frame] = time;
  1888. frames[frame + MIX] = mix;
  1889. frames[frame + SOFTNESS] = softness;
  1890. frames[frame + BEND_DIRECTION] = bendDirection;
  1891. frames[frame + COMPRESS] = compress ? 1 : 0;
  1892. frames[frame + STRETCH] = stretch ? 1 : 0;
  1893. }
  1894. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1895. MixDirection direction) {
  1896. IkConstraint constraint = skeleton.ikConstraints.Items[constraintIndex];
  1897. if (!constraint.active) return;
  1898. float[] frames = this.frames;
  1899. if (time < frames[0]) {
  1900. switch (blend) {
  1901. case MixBlend.Setup:
  1902. constraint.mix = constraint.data.mix;
  1903. constraint.softness = constraint.data.softness;
  1904. constraint.bendDirection = constraint.data.bendDirection;
  1905. constraint.compress = constraint.data.compress;
  1906. constraint.stretch = constraint.data.stretch;
  1907. return;
  1908. case MixBlend.First:
  1909. constraint.mix += (constraint.data.mix - constraint.mix) * alpha;
  1910. constraint.softness += (constraint.data.softness - constraint.softness) * alpha;
  1911. constraint.bendDirection = constraint.data.bendDirection;
  1912. constraint.compress = constraint.data.compress;
  1913. constraint.stretch = constraint.data.stretch;
  1914. return;
  1915. }
  1916. return;
  1917. }
  1918. float mix, softness;
  1919. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
  1920. switch (curveType) {
  1921. case LINEAR:
  1922. float before = frames[i];
  1923. mix = frames[i + MIX];
  1924. softness = frames[i + SOFTNESS];
  1925. float t = (time - before) / (frames[i + ENTRIES] - before);
  1926. mix += (frames[i + ENTRIES + MIX] - mix) * t;
  1927. softness += (frames[i + ENTRIES + SOFTNESS] - softness) * t;
  1928. break;
  1929. case STEPPED:
  1930. mix = frames[i + MIX];
  1931. softness = frames[i + SOFTNESS];
  1932. break;
  1933. default:
  1934. mix = GetBezierValue(time, i, MIX, curveType - BEZIER);
  1935. softness = GetBezierValue(time, i, SOFTNESS, curveType + BEZIER_SIZE - BEZIER);
  1936. break;
  1937. }
  1938. if (blend == MixBlend.Setup) {
  1939. constraint.mix = constraint.data.mix + (mix - constraint.data.mix) * alpha;
  1940. constraint.softness = constraint.data.softness + (softness - constraint.data.softness) * alpha;
  1941. if (direction == MixDirection.Out) {
  1942. constraint.bendDirection = constraint.data.bendDirection;
  1943. constraint.compress = constraint.data.compress;
  1944. constraint.stretch = constraint.data.stretch;
  1945. } else {
  1946. constraint.bendDirection = (int)frames[i + BEND_DIRECTION];
  1947. constraint.compress = frames[i + COMPRESS] != 0;
  1948. constraint.stretch = frames[i + STRETCH] != 0;
  1949. }
  1950. } else {
  1951. constraint.mix += (mix - constraint.mix) * alpha;
  1952. constraint.softness += (softness - constraint.softness) * alpha;
  1953. if (direction == MixDirection.In) {
  1954. constraint.bendDirection = (int)frames[i + BEND_DIRECTION];
  1955. constraint.compress = frames[i + COMPRESS] != 0;
  1956. constraint.stretch = frames[i + STRETCH] != 0;
  1957. }
  1958. }
  1959. }
  1960. }
  1961. /// <summary>Changes a transform constraint's mixes.</summary>
  1962. public class TransformConstraintTimeline : CurveTimeline {
  1963. public const int ENTRIES = 7;
  1964. private const int ROTATE = 1, X = 2, Y = 3, SCALEX = 4, SCALEY = 5, SHEARY = 6;
  1965. readonly int constraintIndex;
  1966. public TransformConstraintTimeline (int frameCount, int bezierCount, int transformConstraintIndex)
  1967. : base(frameCount, bezierCount, (int)Property.TransformConstraint + "|" + transformConstraintIndex) {
  1968. constraintIndex = transformConstraintIndex;
  1969. }
  1970. public override int FrameEntries {
  1971. get {
  1972. return ENTRIES;
  1973. }
  1974. }
  1975. /// <summary>The index of the transform constraint in <see cref="Skeleton.TransformConstraints"/> that will be changed when this
  1976. /// timeline is applied.</summary>
  1977. public int TransformConstraintIndex {
  1978. get {
  1979. return constraintIndex;
  1980. }
  1981. }
  1982. /// <summary>Sets the time, rotate mix, translate mix, scale mix, and shear mix for the specified frame.</summary>
  1983. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  1984. /// <param name="time">The frame time in seconds.</param>
  1985. public void SetFrame (int frame, float time, float mixRotate, float mixX, float mixY, float mixScaleX, float mixScaleY,
  1986. float mixShearY) {
  1987. frame *= ENTRIES;
  1988. frames[frame] = time;
  1989. frames[frame + ROTATE] = mixRotate;
  1990. frames[frame + X] = mixX;
  1991. frames[frame + Y] = mixY;
  1992. frames[frame + SCALEX] = mixScaleX;
  1993. frames[frame + SCALEY] = mixScaleY;
  1994. frames[frame + SHEARY] = mixShearY;
  1995. }
  1996. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  1997. MixDirection direction) {
  1998. TransformConstraint constraint = skeleton.transformConstraints.Items[constraintIndex];
  1999. if (!constraint.active) return;
  2000. float[] frames = this.frames;
  2001. if (time < frames[0]) {
  2002. TransformConstraintData data = constraint.data;
  2003. switch (blend) {
  2004. case MixBlend.Setup:
  2005. constraint.mixRotate = data.mixRotate;
  2006. constraint.mixX = data.mixX;
  2007. constraint.mixY = data.mixY;
  2008. constraint.mixScaleX = data.mixScaleX;
  2009. constraint.mixScaleY = data.mixScaleY;
  2010. constraint.mixShearY = data.mixShearY;
  2011. return;
  2012. case MixBlend.First:
  2013. constraint.mixRotate += (data.mixRotate - constraint.mixRotate) * alpha;
  2014. constraint.mixX += (data.mixX - constraint.mixX) * alpha;
  2015. constraint.mixY += (data.mixY - constraint.mixY) * alpha;
  2016. constraint.mixScaleX += (data.mixScaleX - constraint.mixScaleX) * alpha;
  2017. constraint.mixScaleY += (data.mixScaleY - constraint.mixScaleY) * alpha;
  2018. constraint.mixShearY += (data.mixShearY - constraint.mixShearY) * alpha;
  2019. return;
  2020. }
  2021. return;
  2022. }
  2023. float rotate, x, y, scaleX, scaleY, shearY;
  2024. GetCurveValue(out rotate, out x, out y, out scaleX, out scaleY, out shearY, time);
  2025. if (blend == MixBlend.Setup) {
  2026. TransformConstraintData data = constraint.data;
  2027. constraint.mixRotate = data.mixRotate + (rotate - data.mixRotate) * alpha;
  2028. constraint.mixX = data.mixX + (x - data.mixX) * alpha;
  2029. constraint.mixY = data.mixY + (y - data.mixY) * alpha;
  2030. constraint.mixScaleX = data.mixScaleX + (scaleX - data.mixScaleX) * alpha;
  2031. constraint.mixScaleY = data.mixScaleY + (scaleY - data.mixScaleY) * alpha;
  2032. constraint.mixShearY = data.mixShearY + (shearY - data.mixShearY) * alpha;
  2033. } else {
  2034. constraint.mixRotate += (rotate - constraint.mixRotate) * alpha;
  2035. constraint.mixX += (x - constraint.mixX) * alpha;
  2036. constraint.mixY += (y - constraint.mixY) * alpha;
  2037. constraint.mixScaleX += (scaleX - constraint.mixScaleX) * alpha;
  2038. constraint.mixScaleY += (scaleY - constraint.mixScaleY) * alpha;
  2039. constraint.mixShearY += (shearY - constraint.mixShearY) * alpha;
  2040. }
  2041. }
  2042. public void GetCurveValue (out float rotate, out float x, out float y,
  2043. out float scaleX, out float scaleY, out float shearY, float time) {
  2044. float[] frames = this.frames;
  2045. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i / ENTRIES];
  2046. switch (curveType) {
  2047. case LINEAR:
  2048. float before = frames[i];
  2049. rotate = frames[i + ROTATE];
  2050. x = frames[i + X];
  2051. y = frames[i + Y];
  2052. scaleX = frames[i + SCALEX];
  2053. scaleY = frames[i + SCALEY];
  2054. shearY = frames[i + SHEARY];
  2055. float t = (time - before) / (frames[i + ENTRIES] - before);
  2056. rotate += (frames[i + ENTRIES + ROTATE] - rotate) * t;
  2057. x += (frames[i + ENTRIES + X] - x) * t;
  2058. y += (frames[i + ENTRIES + Y] - y) * t;
  2059. scaleX += (frames[i + ENTRIES + SCALEX] - scaleX) * t;
  2060. scaleY += (frames[i + ENTRIES + SCALEY] - scaleY) * t;
  2061. shearY += (frames[i + ENTRIES + SHEARY] - shearY) * t;
  2062. break;
  2063. case STEPPED:
  2064. rotate = frames[i + ROTATE];
  2065. x = frames[i + X];
  2066. y = frames[i + Y];
  2067. scaleX = frames[i + SCALEX];
  2068. scaleY = frames[i + SCALEY];
  2069. shearY = frames[i + SHEARY];
  2070. break;
  2071. default:
  2072. rotate = GetBezierValue(time, i, ROTATE, curveType - BEZIER);
  2073. x = GetBezierValue(time, i, X, curveType + BEZIER_SIZE - BEZIER);
  2074. y = GetBezierValue(time, i, Y, curveType + BEZIER_SIZE * 2 - BEZIER);
  2075. scaleX = GetBezierValue(time, i, SCALEX, curveType + BEZIER_SIZE * 3 - BEZIER);
  2076. scaleY = GetBezierValue(time, i, SCALEY, curveType + BEZIER_SIZE * 4 - BEZIER);
  2077. shearY = GetBezierValue(time, i, SHEARY, curveType + BEZIER_SIZE * 5 - BEZIER);
  2078. break;
  2079. }
  2080. }
  2081. }
  2082. /// <summary>Changes a path constraint's <see cref="PathConstraint.Position"/>.</summary>
  2083. public class PathConstraintPositionTimeline : CurveTimeline1 {
  2084. readonly int constraintIndex;
  2085. public PathConstraintPositionTimeline (int frameCount, int bezierCount, int pathConstraintIndex)
  2086. : base(frameCount, bezierCount, (int)Property.PathConstraintPosition + "|" + pathConstraintIndex) {
  2087. this.constraintIndex = pathConstraintIndex;
  2088. }
  2089. /// <summary>The index of the path constraint slot in <see cref="Skeleton.PathConstraints"/> that will be changed when this timeline
  2090. /// is applied.</summary>
  2091. public int PathConstraintIndex {
  2092. get {
  2093. return constraintIndex;
  2094. }
  2095. }
  2096. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  2097. MixDirection direction) {
  2098. PathConstraint constraint = skeleton.pathConstraints.Items[constraintIndex];
  2099. if (constraint.active)
  2100. constraint.position = GetAbsoluteValue(time, alpha, blend, constraint.position, constraint.data.position);
  2101. }
  2102. }
  2103. /// <summary>Changes a path constraint's <see cref="PathConstraint.Spacing"/>.</summary>
  2104. public class PathConstraintSpacingTimeline : CurveTimeline1 {
  2105. readonly int constraintIndex;
  2106. public PathConstraintSpacingTimeline (int frameCount, int bezierCount, int pathConstraintIndex)
  2107. : base(frameCount, bezierCount, (int)Property.PathConstraintSpacing + "|" + pathConstraintIndex) {
  2108. constraintIndex = pathConstraintIndex;
  2109. }
  2110. /// <summary>The index of the path constraint in <see cref="Skeleton.PathConstraints"/> that will be changed when this timeline
  2111. /// is applied.</summary>
  2112. public int PathConstraintIndex {
  2113. get {
  2114. return constraintIndex;
  2115. }
  2116. }
  2117. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> events, float alpha, MixBlend blend,
  2118. MixDirection direction) {
  2119. PathConstraint constraint = skeleton.pathConstraints.Items[constraintIndex];
  2120. if (constraint.active)
  2121. constraint.spacing = GetAbsoluteValue(time, alpha, blend, constraint.spacing, constraint.data.spacing);
  2122. }
  2123. }
  2124. /// <summary>Changes a path constraint's <see cref="PathConstraint.MixRotate"/>, <see cref="PathConstraint.MixX"/>, and
  2125. /// <see cref="PathConstraint.MixY"/>.</summary>
  2126. public class PathConstraintMixTimeline : CurveTimeline {
  2127. public const int ENTRIES = 4;
  2128. private const int ROTATE = 1, X = 2, Y = 3;
  2129. readonly int constraintIndex;
  2130. public PathConstraintMixTimeline (int frameCount, int bezierCount, int pathConstraintIndex)
  2131. : base(frameCount, bezierCount, (int)Property.PathConstraintMix + "|" + pathConstraintIndex) {
  2132. constraintIndex = pathConstraintIndex;
  2133. }
  2134. public override int FrameEntries {
  2135. get { return ENTRIES; }
  2136. }
  2137. /// <summary>The index of the path constraint slot in <see cref="Skeleton.PathConstraints"/> that will be changed when this timeline
  2138. /// is applied.</summary>
  2139. public int PathConstraintIndex {
  2140. get {
  2141. return constraintIndex;
  2142. }
  2143. }
  2144. /// <summary>Sets the time and color for the specified frame.</summary>
  2145. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  2146. /// <param name="time">The frame time in seconds.</param>
  2147. public void SetFrame (int frame, float time, float mixRotate, float mixX, float mixY) {
  2148. frame <<= 2;
  2149. frames[frame] = time;
  2150. frames[frame + ROTATE] = mixRotate;
  2151. frames[frame + X] = mixX;
  2152. frames[frame + Y] = mixY;
  2153. }
  2154. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  2155. MixDirection direction) {
  2156. PathConstraint constraint = skeleton.pathConstraints.Items[constraintIndex];
  2157. if (!constraint.active) return;
  2158. float[] frames = this.frames;
  2159. if (time < frames[0]) {
  2160. switch (blend) {
  2161. case MixBlend.Setup:
  2162. constraint.mixRotate = constraint.data.mixRotate;
  2163. constraint.mixX = constraint.data.mixX;
  2164. constraint.mixY = constraint.data.mixY;
  2165. return;
  2166. case MixBlend.First:
  2167. constraint.mixRotate += (constraint.data.mixRotate - constraint.mixRotate) * alpha;
  2168. constraint.mixX += (constraint.data.mixX - constraint.mixX) * alpha;
  2169. constraint.mixY += (constraint.data.mixY - constraint.mixY) * alpha;
  2170. return;
  2171. }
  2172. return;
  2173. }
  2174. float rotate, x, y;
  2175. int i = Search(frames, time, ENTRIES), curveType = (int)curves[i >> 2];
  2176. switch (curveType) {
  2177. case LINEAR:
  2178. float before = frames[i];
  2179. rotate = frames[i + ROTATE];
  2180. x = frames[i + X];
  2181. y = frames[i + Y];
  2182. float t = (time - before) / (frames[i + ENTRIES] - before);
  2183. rotate += (frames[i + ENTRIES + ROTATE] - rotate) * t;
  2184. x += (frames[i + ENTRIES + X] - x) * t;
  2185. y += (frames[i + ENTRIES + Y] - y) * t;
  2186. break;
  2187. case STEPPED:
  2188. rotate = frames[i + ROTATE];
  2189. x = frames[i + X];
  2190. y = frames[i + Y];
  2191. break;
  2192. default:
  2193. rotate = GetBezierValue(time, i, ROTATE, curveType - BEZIER);
  2194. x = GetBezierValue(time, i, X, curveType + BEZIER_SIZE - BEZIER);
  2195. y = GetBezierValue(time, i, Y, curveType + BEZIER_SIZE * 2 - BEZIER);
  2196. break;
  2197. }
  2198. if (blend == MixBlend.Setup) {
  2199. PathConstraintData data = constraint.data;
  2200. constraint.mixRotate = data.mixRotate + (rotate - data.mixRotate) * alpha;
  2201. constraint.mixX = data.mixX + (x - data.mixX) * alpha;
  2202. constraint.mixY = data.mixY + (y - data.mixY) * alpha;
  2203. } else {
  2204. constraint.mixRotate += (rotate - constraint.mixRotate) * alpha;
  2205. constraint.mixX += (x - constraint.mixX) * alpha;
  2206. constraint.mixY += (y - constraint.mixY) * alpha;
  2207. }
  2208. }
  2209. }
  2210. /// <summary>The base class for most <see cref="PhysicsConstraint"/> timelines.</summary>
  2211. public abstract class PhysicsConstraintTimeline : CurveTimeline1 {
  2212. readonly int constraintIndex;
  2213. /// <param name="physicsConstraintIndex">-1 for all physics constraints in the skeleton.</param>
  2214. public PhysicsConstraintTimeline (int frameCount, int bezierCount, int physicsConstraintIndex, Property property)
  2215. : base(frameCount, bezierCount, (int)property + "|" + physicsConstraintIndex) {
  2216. constraintIndex = physicsConstraintIndex;
  2217. }
  2218. /// <summary>The index of the physics constraint in <see cref="Skeleton.PhysicsConstraints"/> that will be changed when this timeline
  2219. /// is applied, or -1 if all physics constraints in the skeleton will be changed.</summary>
  2220. public int PhysicsConstraintIndex {
  2221. get {
  2222. return constraintIndex;
  2223. }
  2224. }
  2225. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  2226. MixDirection direction) {
  2227. PhysicsConstraint constraint;
  2228. if (constraintIndex == -1) {
  2229. float value = time >= frames[0] ? GetCurveValue(time) : 0;
  2230. PhysicsConstraint[] constraints = skeleton.physicsConstraints.Items;
  2231. for (int i = 0, n = skeleton.physicsConstraints.Count; i < n; i++) {
  2232. constraint = (PhysicsConstraint)constraints[i];
  2233. if (constraint.active && Global(constraint.data))
  2234. Set(constraint, GetAbsoluteValue(time, alpha, blend, Get(constraint), Setup(constraint), value));
  2235. }
  2236. } else {
  2237. constraint = skeleton.physicsConstraints.Items[constraintIndex];
  2238. if (constraint.active) Set(constraint, GetAbsoluteValue(time, alpha, blend, Get(constraint), Setup(constraint)));
  2239. }
  2240. }
  2241. abstract protected float Setup (PhysicsConstraint constraint);
  2242. abstract protected float Get (PhysicsConstraint constraint);
  2243. abstract protected void Set (PhysicsConstraint constraint, float value);
  2244. abstract protected bool Global (PhysicsConstraintData constraint);
  2245. }
  2246. /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Inertia"/>.</summary>
  2247. public class PhysicsConstraintInertiaTimeline : PhysicsConstraintTimeline {
  2248. public PhysicsConstraintInertiaTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
  2249. : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintInertia) {
  2250. }
  2251. override protected float Setup (PhysicsConstraint constraint) {
  2252. return constraint.data.inertia;
  2253. }
  2254. override protected float Get (PhysicsConstraint constraint) {
  2255. return constraint.inertia;
  2256. }
  2257. override protected void Set (PhysicsConstraint constraint, float value) {
  2258. constraint.inertia = value;
  2259. }
  2260. override protected bool Global (PhysicsConstraintData constraint) {
  2261. return constraint.inertiaGlobal;
  2262. }
  2263. }
  2264. /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Strength"/>.</summary>
  2265. public class PhysicsConstraintStrengthTimeline : PhysicsConstraintTimeline {
  2266. public PhysicsConstraintStrengthTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
  2267. : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintStrength) {
  2268. }
  2269. override protected float Setup (PhysicsConstraint constraint) {
  2270. return constraint.data.strength;
  2271. }
  2272. override protected float Get (PhysicsConstraint constraint) {
  2273. return constraint.strength;
  2274. }
  2275. override protected void Set (PhysicsConstraint constraint, float value) {
  2276. constraint.strength = value;
  2277. }
  2278. override protected bool Global (PhysicsConstraintData constraint) {
  2279. return constraint.strengthGlobal;
  2280. }
  2281. }
  2282. /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Damping"/>.</summary>
  2283. public class PhysicsConstraintDampingTimeline : PhysicsConstraintTimeline {
  2284. public PhysicsConstraintDampingTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
  2285. : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintDamping) {
  2286. }
  2287. override protected float Setup (PhysicsConstraint constraint) {
  2288. return constraint.data.damping;
  2289. }
  2290. override protected float Get (PhysicsConstraint constraint) {
  2291. return constraint.damping;
  2292. }
  2293. override protected void Set (PhysicsConstraint constraint, float value) {
  2294. constraint.damping = value;
  2295. }
  2296. override protected bool Global (PhysicsConstraintData constraint) {
  2297. return constraint.dampingGlobal;
  2298. }
  2299. }
  2300. /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.MassInverse"/>. The timeline values are not inverted.</summary>
  2301. public class PhysicsConstraintMassTimeline : PhysicsConstraintTimeline {
  2302. public PhysicsConstraintMassTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
  2303. : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintMass) {
  2304. }
  2305. override protected float Setup (PhysicsConstraint constraint) {
  2306. return 1 / constraint.data.massInverse;
  2307. }
  2308. override protected float Get (PhysicsConstraint constraint) {
  2309. return 1 / constraint.massInverse;
  2310. }
  2311. override protected void Set (PhysicsConstraint constraint, float value) {
  2312. constraint.massInverse = 1 / value;
  2313. }
  2314. override protected bool Global (PhysicsConstraintData constraint) {
  2315. return constraint.massGlobal;
  2316. }
  2317. }
  2318. /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Wind"/>.</summary>
  2319. public class PhysicsConstraintWindTimeline : PhysicsConstraintTimeline {
  2320. public PhysicsConstraintWindTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
  2321. : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintWind) {
  2322. }
  2323. override protected float Setup (PhysicsConstraint constraint) {
  2324. return constraint.data.wind;
  2325. }
  2326. override protected float Get (PhysicsConstraint constraint) {
  2327. return constraint.wind;
  2328. }
  2329. override protected void Set (PhysicsConstraint constraint, float value) {
  2330. constraint.wind = value;
  2331. }
  2332. override protected bool Global (PhysicsConstraintData constraint) {
  2333. return constraint.windGlobal;
  2334. }
  2335. }
  2336. /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Gravity"/>.</summary>
  2337. public class PhysicsConstraintGravityTimeline : PhysicsConstraintTimeline {
  2338. public PhysicsConstraintGravityTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
  2339. : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintGravity) {
  2340. }
  2341. override protected float Setup (PhysicsConstraint constraint) {
  2342. return constraint.data.gravity;
  2343. }
  2344. override protected float Get (PhysicsConstraint constraint) {
  2345. return constraint.gravity;
  2346. }
  2347. override protected void Set (PhysicsConstraint constraint, float value) {
  2348. constraint.gravity = value;
  2349. }
  2350. override protected bool Global (PhysicsConstraintData constraint) {
  2351. return constraint.gravityGlobal;
  2352. }
  2353. }
  2354. /// <summary>Changes a physics constraint's <see cref="PhysicsConstraint.Mix"/>.</summary>
  2355. public class PhysicsConstraintMixTimeline : PhysicsConstraintTimeline {
  2356. public PhysicsConstraintMixTimeline (int frameCount, int bezierCount, int physicsConstraintIndex)
  2357. : base(frameCount, bezierCount, physicsConstraintIndex, Property.PhysicsConstraintMix) {
  2358. }
  2359. override protected float Setup (PhysicsConstraint constraint) {
  2360. return constraint.data.mix;
  2361. }
  2362. override protected float Get (PhysicsConstraint constraint) {
  2363. return constraint.mix;
  2364. }
  2365. override protected void Set (PhysicsConstraint constraint, float value) {
  2366. constraint.mix = value;
  2367. }
  2368. override protected bool Global (PhysicsConstraintData constraint) {
  2369. return constraint.mixGlobal;
  2370. }
  2371. }
  2372. /// <summary>Resets a physics constraint when specific animation times are reached.</summary>
  2373. public class PhysicsConstraintResetTimeline : Timeline {
  2374. static readonly string[] propertyIds = { ((int)Property.PhysicsConstraintReset).ToString() };
  2375. readonly int constraintIndex;
  2376. /// <param name="physicsConstraintIndex">-1 for all physics constraints in the skeleton.</param>
  2377. public PhysicsConstraintResetTimeline (int frameCount, int physicsConstraintIndex)
  2378. : base(frameCount, propertyIds) {
  2379. constraintIndex = physicsConstraintIndex;
  2380. }
  2381. /// <summary>The index of the physics constraint in <see cref="Skeleton.PhysicsConstraints"/> that will be reset when this timeline is
  2382. /// applied, or -1 if all physics constraints in the skeleton will be reset.</summary>
  2383. public int PhysicsConstraintIndex {
  2384. get {
  2385. return constraintIndex;
  2386. }
  2387. }
  2388. override public int FrameCount {
  2389. get { return frames.Length; }
  2390. }
  2391. /// <summary>Sets the time for the specified frame.<summary>
  2392. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  2393. public void SetFrame (int frame, float time) {
  2394. frames[frame] = time;
  2395. }
  2396. /// <summary>Resets the physics constraint when frames > <code>lastTime</code> and <= <code>time</code>.</summary>
  2397. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  2398. MixDirection direction) {
  2399. PhysicsConstraint constraint = null;
  2400. if (constraintIndex != -1) {
  2401. constraint = skeleton.physicsConstraints.Items[constraintIndex];
  2402. if (!constraint.active) return;
  2403. }
  2404. float[] frames = this.frames;
  2405. if (lastTime > time) { // Apply after lastTime for looped animations.
  2406. Apply(skeleton, lastTime, int.MaxValue, null, alpha, blend, direction);
  2407. lastTime = -1f;
  2408. } else if (lastTime >= frames[frames.Length - 1]) // Last time is after last frame.
  2409. return;
  2410. if (time < frames[0]) return;
  2411. if (lastTime < frames[0] || time >= frames[Search(frames, lastTime) + 1]) {
  2412. if (constraint != null)
  2413. constraint.Reset();
  2414. else {
  2415. PhysicsConstraint[] constraints = skeleton.physicsConstraints.Items;
  2416. for (int i = 0, n = skeleton.physicsConstraints.Count; i < n; i++) {
  2417. constraint = (PhysicsConstraint)constraints[i];
  2418. if (constraint.active) constraint.Reset();
  2419. }
  2420. }
  2421. }
  2422. }
  2423. }
  2424. /// <summary>Changes a slot's <see cref="Slot.SequenceIndex"/> for an attachment's <see cref="Sequence"/>.</summary>
  2425. public class SequenceTimeline : Timeline, ISlotTimeline {
  2426. public const int ENTRIES = 3;
  2427. private const int MODE = 1, DELAY = 2;
  2428. readonly int slotIndex;
  2429. readonly IHasTextureRegion attachment;
  2430. public SequenceTimeline (int frameCount, int slotIndex, Attachment attachment)
  2431. : base(frameCount, (int)Property.Sequence + "|" + slotIndex + "|" + ((IHasTextureRegion)attachment).Sequence.Id) {
  2432. this.slotIndex = slotIndex;
  2433. this.attachment = (IHasTextureRegion)attachment;
  2434. }
  2435. public override int FrameEntries {
  2436. get { return ENTRIES; }
  2437. }
  2438. public int SlotIndex {
  2439. get {
  2440. return slotIndex;
  2441. }
  2442. }
  2443. public Attachment Attachment {
  2444. get {
  2445. return (Attachment)attachment;
  2446. }
  2447. }
  2448. /// <summary>Sets the time, mode, index, and frame time for the specified frame.</summary>
  2449. /// <param name="frame">Between 0 and <code>frameCount</code>, inclusive.</param>
  2450. /// <param name="time">Seconds between frames.</param>
  2451. public void SetFrame (int frame, float time, SequenceMode mode, int index, float delay) {
  2452. frame *= ENTRIES;
  2453. frames[frame] = time;
  2454. frames[frame + MODE] = (int)mode | (index << 4);
  2455. frames[frame + DELAY] = delay;
  2456. }
  2457. override public void Apply (Skeleton skeleton, float lastTime, float time, ExposedList<Event> firedEvents, float alpha, MixBlend blend,
  2458. MixDirection direction) {
  2459. Slot slot = skeleton.slots.Items[slotIndex];
  2460. if (!slot.bone.active) return;
  2461. Attachment slotAttachment = slot.attachment;
  2462. if (slotAttachment != attachment) {
  2463. VertexAttachment vertexAttachment = slotAttachment as VertexAttachment;
  2464. if ((vertexAttachment == null)
  2465. || vertexAttachment.TimelineAttachment != attachment) return;
  2466. }
  2467. Sequence sequence = ((IHasTextureRegion)slotAttachment).Sequence;
  2468. if (sequence == null) return;
  2469. float[] frames = this.frames;
  2470. if (time < frames[0]) {
  2471. if (blend == MixBlend.Setup || blend == MixBlend.First) slot.SequenceIndex = -1;
  2472. return;
  2473. }
  2474. int i = Search(frames, time, ENTRIES);
  2475. float before = frames[i];
  2476. int modeAndIndex = (int)frames[i + MODE];
  2477. float delay = frames[i + DELAY];
  2478. int index = modeAndIndex >> 4, count = sequence.Regions.Length;
  2479. SequenceMode mode = (SequenceMode)(modeAndIndex & 0xf);
  2480. if (mode != SequenceMode.Hold) {
  2481. index += (int)((time - before) / delay + 0.0001f);
  2482. switch (mode) {
  2483. case SequenceMode.Once:
  2484. index = Math.Min(count - 1, index);
  2485. break;
  2486. case SequenceMode.Loop:
  2487. index %= count;
  2488. break;
  2489. case SequenceMode.Pingpong: {
  2490. int n = (count << 1) - 2;
  2491. index = n == 0 ? 0 : index % n;
  2492. if (index >= count) index = n - index;
  2493. break;
  2494. }
  2495. case SequenceMode.OnceReverse:
  2496. index = Math.Max(count - 1 - index, 0);
  2497. break;
  2498. case SequenceMode.LoopReverse:
  2499. index = count - 1 - (index % count);
  2500. break;
  2501. case SequenceMode.PingpongReverse: {
  2502. int n = (count << 1) - 2;
  2503. index = n == 0 ? 0 : (index + count - 1) % n;
  2504. if (index >= count) index = n - index;
  2505. break;
  2506. } // end case
  2507. }
  2508. }
  2509. slot.SequenceIndex = index;
  2510. }
  2511. }
  2512. }