Skin.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Spine
  4. {
  5. public class Skin
  6. {
  7. public Skin(string name)
  8. {
  9. if (name == null)
  10. {
  11. throw new ArgumentNullException("name cannot be null.");
  12. }
  13. this.name = name;
  14. }
  15. public string Name
  16. {
  17. get
  18. {
  19. return this.name;
  20. }
  21. }
  22. public void AddAttachment(int slotIndex, string name, Attachment attachment)
  23. {
  24. if (attachment == null)
  25. {
  26. throw new ArgumentNullException("attachment cannot be null.");
  27. }
  28. this.attachments[new KeyValuePair<int, string>(slotIndex, name)] = attachment;
  29. }
  30. public Attachment GetAttachment(int slotIndex, string name)
  31. {
  32. Attachment result;
  33. this.attachments.TryGetValue(new KeyValuePair<int, string>(slotIndex, name), out result);
  34. return result;
  35. }
  36. public void FindNamesForSlot(int slotIndex, List<string> names)
  37. {
  38. if (names == null)
  39. {
  40. throw new ArgumentNullException("names cannot be null.");
  41. }
  42. foreach (KeyValuePair<int, string> keyValuePair in this.attachments.Keys)
  43. {
  44. if (keyValuePair.Key == slotIndex)
  45. {
  46. names.Add(keyValuePair.Value);
  47. }
  48. }
  49. }
  50. public void FindAttachmentsForSlot(int slotIndex, List<Attachment> attachments)
  51. {
  52. if (attachments == null)
  53. {
  54. throw new ArgumentNullException("attachments cannot be null.");
  55. }
  56. foreach (KeyValuePair<KeyValuePair<int, string>, Attachment> keyValuePair in this.attachments)
  57. {
  58. if (keyValuePair.Key.Key == slotIndex)
  59. {
  60. attachments.Add(keyValuePair.Value);
  61. }
  62. }
  63. }
  64. public override string ToString()
  65. {
  66. return this.name;
  67. }
  68. internal void AttachAll(Skeleton skeleton, Skin oldSkin)
  69. {
  70. foreach (KeyValuePair<KeyValuePair<int, string>, Attachment> keyValuePair in oldSkin.attachments)
  71. {
  72. int key = keyValuePair.Key.Key;
  73. Slot slot = skeleton.slots[key];
  74. if (slot.attachment == keyValuePair.Value)
  75. {
  76. Attachment attachment = this.GetAttachment(key, keyValuePair.Key.Value);
  77. if (attachment != null)
  78. {
  79. slot.Attachment = attachment;
  80. }
  81. }
  82. }
  83. }
  84. internal string name;
  85. private Dictionary<KeyValuePair<int, string>, Attachment> attachments = new Dictionary<KeyValuePair<int, string>, Attachment>(Skin.AttachmentComparer.Instance);
  86. private class AttachmentComparer : IEqualityComparer<KeyValuePair<int, string>>
  87. {
  88. bool IEqualityComparer<KeyValuePair<int, string>>.Equals(KeyValuePair<int, string> o1, KeyValuePair<int, string> o2)
  89. {
  90. return o1.Key == o2.Key && o1.Value == o2.Value;
  91. }
  92. int IEqualityComparer<KeyValuePair<int, string>>.GetHashCode(KeyValuePair<int, string> o)
  93. {
  94. return o.Key;
  95. }
  96. internal static readonly Skin.AttachmentComparer Instance = new Skin.AttachmentComparer();
  97. }
  98. }
  99. }