AtlasAttachmentLoader.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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
  13. * or 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
  27. * THE 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 array cannot be null.");
  39. this.atlasArray = atlasArray;
  40. }
  41. public RegionAttachment NewRegionAttachment (Skin skin, string name, string path) {
  42. AtlasRegion region = FindRegion(path);
  43. if (region == null) throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  44. RegionAttachment attachment = new RegionAttachment(name);
  45. attachment.RendererObject = region;
  46. attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
  47. attachment.regionOffsetX = region.offsetX;
  48. attachment.regionOffsetY = region.offsetY;
  49. attachment.regionWidth = region.width;
  50. attachment.regionHeight = region.height;
  51. attachment.regionOriginalWidth = region.originalWidth;
  52. attachment.regionOriginalHeight = region.originalHeight;
  53. return attachment;
  54. }
  55. public MeshAttachment NewMeshAttachment (Skin skin, string name, string path) {
  56. AtlasRegion region = FindRegion(path);
  57. if (region == null) throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  58. MeshAttachment attachment = new MeshAttachment(name);
  59. attachment.RendererObject = region;
  60. attachment.RegionU = region.u;
  61. attachment.RegionV = region.v;
  62. attachment.RegionU2 = region.u2;
  63. attachment.RegionV2 = region.v2;
  64. attachment.RegionRotate = region.rotate;
  65. attachment.RegionDegrees = region.degrees;
  66. attachment.regionOffsetX = region.offsetX;
  67. attachment.regionOffsetY = region.offsetY;
  68. attachment.regionWidth = region.width;
  69. attachment.regionHeight = region.height;
  70. attachment.regionOriginalWidth = region.originalWidth;
  71. attachment.regionOriginalHeight = region.originalHeight;
  72. return attachment;
  73. }
  74. public BoundingBoxAttachment NewBoundingBoxAttachment (Skin skin, string name) {
  75. return new BoundingBoxAttachment(name);
  76. }
  77. public PathAttachment NewPathAttachment (Skin skin, string name) {
  78. return new PathAttachment(name);
  79. }
  80. public PointAttachment NewPointAttachment (Skin skin, string name) {
  81. return new PointAttachment(name);
  82. }
  83. public ClippingAttachment NewClippingAttachment(Skin skin, string name) {
  84. return new ClippingAttachment(name);
  85. }
  86. public AtlasRegion FindRegion (string name) {
  87. AtlasRegion region;
  88. for (int i = 0; i < atlasArray.Length; i++) {
  89. region = atlasArray[i].FindRegion(name);
  90. if (region != null)
  91. return region;
  92. }
  93. return null;
  94. }
  95. }
  96. }