AtlasAttachmentLoader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. namespace Spine {
  31. /// <summary>
  32. /// An AttachmentLoader that configures attachments using texture regions from an Atlas.
  33. /// See <a href='http://esotericsoftware.com/spine-loading-skeleton-data#JSON-and-binary-data'>Loading Skeleton Data</a> in the Spine Runtimes Guide.
  34. /// </summary>
  35. public class AtlasAttachmentLoader : AttachmentLoader {
  36. private Atlas[] atlasArray;
  37. public AtlasAttachmentLoader (params Atlas[] atlasArray) {
  38. if (atlasArray == null) throw new ArgumentNullException("atlas", "atlas array cannot be null.");
  39. this.atlasArray = atlasArray;
  40. }
  41. private void LoadSequence (string name, string basePath, Sequence sequence) {
  42. TextureRegion[] regions = sequence.Regions;
  43. for (int i = 0, n = regions.Length; i < n; i++) {
  44. string path = sequence.GetPath(basePath, i);
  45. regions[i] = FindRegion(path);
  46. if (regions[i] == null) throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  47. }
  48. }
  49. public RegionAttachment NewRegionAttachment (Skin skin, string name, string path, Sequence sequence) {
  50. RegionAttachment attachment = new RegionAttachment(name);
  51. if (sequence != null)
  52. LoadSequence(name, path, sequence);
  53. else {
  54. AtlasRegion region = FindRegion(path);
  55. if (region == null)
  56. throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  57. attachment.Region = region;
  58. }
  59. return attachment;
  60. }
  61. public MeshAttachment NewMeshAttachment (Skin skin, string name, string path, Sequence sequence) {
  62. MeshAttachment attachment = new MeshAttachment(name);
  63. if (sequence != null)
  64. LoadSequence(name, path, sequence);
  65. else {
  66. AtlasRegion region = FindRegion(path);
  67. if (region == null)
  68. throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  69. attachment.Region = region;
  70. }
  71. return attachment;
  72. }
  73. public BoundingBoxAttachment NewBoundingBoxAttachment (Skin skin, string name) {
  74. return new BoundingBoxAttachment(name);
  75. }
  76. public PathAttachment NewPathAttachment (Skin skin, string name) {
  77. return new PathAttachment(name);
  78. }
  79. public PointAttachment NewPointAttachment (Skin skin, string name) {
  80. return new PointAttachment(name);
  81. }
  82. public ClippingAttachment NewClippingAttachment (Skin skin, string name) {
  83. return new ClippingAttachment(name);
  84. }
  85. public AtlasRegion FindRegion (string name) {
  86. AtlasRegion region;
  87. for (int i = 0; i < atlasArray.Length; i++) {
  88. region = atlasArray[i].FindRegion(name);
  89. if (region != null)
  90. return region;
  91. }
  92. return null;
  93. }
  94. }
  95. }