Sin descripción
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.

PSDLayerMappingStrategy.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using PDNWrapper;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.PSD
  6. {
  7. internal interface IPSDLayerMappingStrategyComparable
  8. {
  9. int layerID
  10. {
  11. get;
  12. }
  13. string name
  14. {
  15. get;
  16. }
  17. bool isGroup
  18. {
  19. get;
  20. }
  21. }
  22. internal interface IPSDLayerMappingStrategy
  23. {
  24. bool Compare(IPSDLayerMappingStrategyComparable a, IPSDLayerMappingStrategyComparable b);
  25. bool Compare(IPSDLayerMappingStrategyComparable a, BitmapLayer b);
  26. string LayersUnique(IEnumerable<IPSDLayerMappingStrategyComparable> layers);
  27. }
  28. internal abstract class LayerMappingStrategy<T> : IPSDLayerMappingStrategy
  29. {
  30. string m_DuplicatedStringError = L10n.Tr("The following layers have duplicated identifier.");
  31. protected abstract T GetID(IPSDLayerMappingStrategyComparable layer);
  32. protected abstract T GetID(BitmapLayer layer);
  33. protected virtual bool IsGroup(IPSDLayerMappingStrategyComparable layer)
  34. {
  35. return layer.isGroup;
  36. }
  37. protected virtual bool IsGroup(BitmapLayer layer)
  38. {
  39. return layer.IsGroup;
  40. }
  41. public bool Compare(IPSDLayerMappingStrategyComparable x, BitmapLayer y)
  42. {
  43. return Comparer<T>.Default.Compare(GetID(x), GetID(y)) == 0 && IsGroup(x) == IsGroup(y);
  44. }
  45. public bool Compare(IPSDLayerMappingStrategyComparable x, IPSDLayerMappingStrategyComparable y)
  46. {
  47. return Comparer<T>.Default.Compare(GetID(x), GetID(y)) == 0 && IsGroup(x) == IsGroup(y);
  48. }
  49. public string LayersUnique(IEnumerable<IPSDLayerMappingStrategyComparable> layers)
  50. {
  51. var layerNameHash = new HashSet<T>();
  52. var layerGroupHash = new HashSet<T>();
  53. return LayersUnique(layers, layerNameHash, layerGroupHash);
  54. }
  55. string LayersUnique(IEnumerable<IPSDLayerMappingStrategyComparable> layers, HashSet<T> layerNameHash, HashSet<T> layerGroupHash)
  56. {
  57. List<string> duplicateLayerName = new List<string>();
  58. string duplicatedStringError = null;
  59. foreach (var layer in layers)
  60. {
  61. var id = GetID(layer);
  62. var hash = layer.isGroup ? layerGroupHash : layerNameHash;
  63. if (hash.Contains(id))
  64. duplicateLayerName.Add(layer.name);
  65. else
  66. hash.Add(id);
  67. }
  68. if (duplicateLayerName.Count > 0)
  69. {
  70. duplicatedStringError = m_DuplicatedStringError + "\n";
  71. duplicatedStringError += string.Join(", ", duplicateLayerName);
  72. }
  73. return duplicatedStringError;
  74. }
  75. }
  76. internal class LayerMappingUseLayerName : LayerMappingStrategy<string>
  77. {
  78. protected override string GetID(IPSDLayerMappingStrategyComparable x)
  79. {
  80. return x.name.ToLower();
  81. }
  82. protected override string GetID(BitmapLayer x)
  83. {
  84. return x.Name.ToLower();
  85. }
  86. }
  87. internal class LayerMappingUseLayerNameCaseSensitive : LayerMappingStrategy<string>
  88. {
  89. protected override string GetID(IPSDLayerMappingStrategyComparable x)
  90. {
  91. return x.name;
  92. }
  93. protected override string GetID(BitmapLayer x)
  94. {
  95. return x.Name;
  96. }
  97. }
  98. internal class LayerMappingUserLayerID : LayerMappingStrategy<int>
  99. {
  100. protected override int GetID(IPSDLayerMappingStrategyComparable x)
  101. {
  102. return x.layerID;
  103. }
  104. protected override int GetID(BitmapLayer x)
  105. {
  106. return x.LayerID;
  107. }
  108. }
  109. }