Sequence.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Text;
  31. namespace Spine {
  32. public class Sequence {
  33. static int nextID = 0;
  34. static readonly Object nextIdLock = new Object();
  35. internal readonly int id;
  36. internal readonly TextureRegion[] regions;
  37. internal int start, digits, setupIndex;
  38. public int Start { get { return start; } set { start = value; } }
  39. public int Digits { get { return digits; } set { digits = value; } }
  40. /// <summary>The index of the region to show for the setup pose.</summary>
  41. public int SetupIndex { get { return setupIndex; } set { setupIndex = value; } }
  42. public TextureRegion[] Regions { get { return regions; } }
  43. /// <summary>Returns a unique ID for this attachment.</summary>
  44. public int Id { get { return id; } }
  45. public Sequence (int count) {
  46. lock (Sequence.nextIdLock) {
  47. id = Sequence.nextID++;
  48. }
  49. regions = new TextureRegion[count];
  50. }
  51. /// <summary>Copy constructor.</summary>
  52. public Sequence (Sequence other) {
  53. lock (Sequence.nextIdLock) {
  54. id = Sequence.nextID++;
  55. }
  56. regions = new TextureRegion[other.regions.Length];
  57. Array.Copy(other.regions, 0, regions, 0, regions.Length);
  58. start = other.start;
  59. digits = other.digits;
  60. setupIndex = other.setupIndex;
  61. }
  62. public void Apply (Slot slot, IHasTextureRegion attachment) {
  63. int index = slot.SequenceIndex;
  64. if (index == -1) index = setupIndex;
  65. if (index >= regions.Length) index = regions.Length - 1;
  66. TextureRegion region = regions[index];
  67. if (attachment.Region != region) {
  68. attachment.Region = region;
  69. attachment.UpdateRegion();
  70. }
  71. }
  72. public string GetPath (string basePath, int index) {
  73. StringBuilder buffer = new StringBuilder(basePath.Length + digits);
  74. buffer.Append(basePath);
  75. string frame = (start + index).ToString();
  76. for (int i = digits - frame.Length; i > 0; i--)
  77. buffer.Append('0');
  78. buffer.Append(frame);
  79. return buffer.ToString();
  80. }
  81. }
  82. public enum SequenceMode {
  83. Hold, Once, Loop, Pingpong, OnceReverse, LoopReverse, PingpongReverse
  84. }
  85. }