源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 line
6.2 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. using System.Collections.Generic;
  32. namespace Spine {
  33. /// <summary>Stores attachments by slot index and attachment name.
  34. /// <para>See SkeletonData <see cref="Spine.SkeletonData.DefaultSkin"/>, Skeleton <see cref="Spine.Skeleton.Skin"/>, and
  35. /// <a href="http://esotericsoftware.com/spine-runtime-skins">Runtime skins</a> in the Spine Runtimes Guide.</para>
  36. /// </summary>
  37. public class Skin {
  38. internal string name;
  39. private Dictionary<AttachmentKeyTuple, Attachment> attachments =
  40. new Dictionary<AttachmentKeyTuple, Attachment>(AttachmentKeyTupleComparer.Instance);
  41. public string Name { get { return name; } }
  42. public Dictionary<AttachmentKeyTuple, Attachment> Attachments { get { return attachments; } }
  43. public Skin (string name) {
  44. if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
  45. this.name = name;
  46. }
  47. /// <summary>Adds an attachment to the skin for the specified slot index and name. If the name already exists for the slot, the previous value is replaced.</summary>
  48. public void AddAttachment (int slotIndex, string name, Attachment attachment) {
  49. if (attachment == null) throw new ArgumentNullException("attachment", "attachment cannot be null.");
  50. attachments[new AttachmentKeyTuple(slotIndex, name)] = attachment;
  51. }
  52. /// <summary>Returns the attachment for the specified slot index and name, or null.</summary>
  53. /// <returns>May be null.</returns>
  54. public Attachment GetAttachment (int slotIndex, string name) {
  55. Attachment attachment;
  56. attachments.TryGetValue(new AttachmentKeyTuple(slotIndex, name), out attachment);
  57. return attachment;
  58. }
  59. /// <summary>Finds the skin keys for a given slot. The results are added to the passed List(names).</summary>
  60. /// <param name="slotIndex">The target slotIndex. To find the slot index, use <see cref="Spine.Skeleton.FindSlotIndex"/> or <see cref="Spine.SkeletonData.FindSlotIndex"/>
  61. /// <param name="names">Found skin key names will be added to this list.</param>
  62. public void FindNamesForSlot (int slotIndex, List<string> names) {
  63. if (names == null) throw new ArgumentNullException("names", "names cannot be null.");
  64. foreach (AttachmentKeyTuple key in attachments.Keys)
  65. if (key.slotIndex == slotIndex) names.Add(key.name);
  66. }
  67. /// <summary>Finds the attachments for a given slot. The results are added to the passed List(Attachment).</summary>
  68. /// <param name="slotIndex">The target slotIndex. To find the slot index, use <see cref="Spine.Skeleton.FindSlotIndex"/> or <see cref="Spine.SkeletonData.FindSlotIndex"/>
  69. /// <param name="attachments">Found Attachments will be added to this list.</param>
  70. public void FindAttachmentsForSlot (int slotIndex, List<Attachment> attachments) {
  71. if (attachments == null) throw new ArgumentNullException("attachments", "attachments cannot be null.");
  72. foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in this.attachments)
  73. if (entry.Key.slotIndex == slotIndex) attachments.Add(entry.Value);
  74. }
  75. override public string ToString () {
  76. return name;
  77. }
  78. /// <summary>Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.</summary>
  79. internal void AttachAll (Skeleton skeleton, Skin oldSkin) {
  80. foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in oldSkin.attachments) {
  81. int slotIndex = entry.Key.slotIndex;
  82. Slot slot = skeleton.slots.Items[slotIndex];
  83. if (slot.Attachment == entry.Value) {
  84. Attachment attachment = GetAttachment(slotIndex, entry.Key.name);
  85. if (attachment != null) slot.Attachment = attachment;
  86. }
  87. }
  88. }
  89. public struct AttachmentKeyTuple {
  90. public readonly int slotIndex;
  91. public readonly string name;
  92. internal readonly int nameHashCode;
  93. public AttachmentKeyTuple (int slotIndex, string name) {
  94. this.slotIndex = slotIndex;
  95. this.name = name;
  96. nameHashCode = this.name.GetHashCode();
  97. }
  98. }
  99. // Avoids boxing in the dictionary.
  100. class AttachmentKeyTupleComparer : IEqualityComparer<AttachmentKeyTuple> {
  101. internal static readonly AttachmentKeyTupleComparer Instance = new AttachmentKeyTupleComparer();
  102. bool IEqualityComparer<AttachmentKeyTuple>.Equals (AttachmentKeyTuple o1, AttachmentKeyTuple o2) {
  103. return o1.slotIndex == o2.slotIndex && o1.nameHashCode == o2.nameHashCode && string.Equals(o1.name, o2.name, StringComparison.Ordinal);
  104. }
  105. int IEqualityComparer<AttachmentKeyTuple>.GetHashCode (AttachmentKeyTuple o) {
  106. return o.slotIndex;
  107. }
  108. }
  109. }
  110. }