SkeletonStyles.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using UnityEngine;
  2. using System;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal interface ISkeletonStyle
  6. {
  7. Color GetColor(BoneCache bone);
  8. Color GetPreviewColor(int index);
  9. Color GetParentLinkColor(BoneCache bone);
  10. Color GetParentLinkPreviewColor(int index);
  11. Color GetOutlineColor(BoneCache bone, bool isSelected, bool isHovered);
  12. Color GetPreviewOutlineColor(int index);
  13. float GetOutlineScale(bool isSelected);
  14. }
  15. internal abstract class SkeletonStyleBase : ISkeletonStyle
  16. {
  17. private const float kOutlineScale = 1.35f;
  18. private const float kSelectedOutlineScale = 1.55f;
  19. public Color GetColor(BoneCache bone)
  20. {
  21. return SetAlpha(GetBoneColorRaw(bone), GetAlpha(bone), VisibilityToolSettings.boneOpacity);
  22. }
  23. public Color GetPreviewColor(int index)
  24. {
  25. return GetBoneColorRaw(index);
  26. }
  27. public Color GetParentLinkColor(BoneCache bone)
  28. {
  29. return SetAlpha(GetBoneColorRaw(bone), 0.2f * GetAlpha(bone), VisibilityToolSettings.boneOpacity);
  30. }
  31. public Color GetParentLinkPreviewColor(int index)
  32. {
  33. return SetAlpha(GetBoneColorRaw(index), 0.2f, 1f);
  34. }
  35. public Color GetOutlineColor(BoneCache bone, bool isSelected, bool isHovered)
  36. {
  37. var skinningCache = bone.skinningCache;
  38. if (isSelected)
  39. return SelectionOutlineSettings.outlineColor;
  40. if (isHovered)
  41. return Handles.preselectionColor;
  42. return SetAlpha(CalculateOutlineColor(GetBoneColorRaw(bone), VisibilityToolSettings.boneOpacity), GetAlpha(bone), VisibilityToolSettings.boneOpacity);
  43. }
  44. public Color GetPreviewOutlineColor(int index)
  45. {
  46. return CalculateOutlineColor(GetBoneColorRaw(index), 1f);
  47. }
  48. public float GetOutlineScale(bool isSelected)
  49. {
  50. if (isSelected)
  51. return 1f + (kSelectedOutlineScale - 1f) * SelectionOutlineSettings.selectedBoneOutlineSize;
  52. return kOutlineScale;
  53. }
  54. private Color CalculateOutlineColor(Color color, float opacity)
  55. {
  56. color *= 0.35f;
  57. return SetAlpha(color, 0.75f, opacity);
  58. }
  59. private Color SetAlpha(Color color, float alpha, float opacity)
  60. {
  61. color.a = alpha * opacity;
  62. return color;
  63. }
  64. protected virtual float GetAlpha(BoneCache bone)
  65. {
  66. return 1f;
  67. }
  68. protected abstract Color GetBoneColorRaw(BoneCache bone);
  69. protected abstract Color GetBoneColorRaw(int index);
  70. }
  71. internal class BoneColorSkeletonStyle : SkeletonStyleBase
  72. {
  73. protected override Color GetBoneColorRaw(BoneCache bone)
  74. {
  75. return bone.bindPoseColor;
  76. }
  77. protected override Color GetBoneColorRaw(int index)
  78. {
  79. return ModuleUtility.CalculateNiceColor(index, 6);
  80. }
  81. protected override float GetAlpha(BoneCache bone)
  82. {
  83. return 0.9f;
  84. }
  85. }
  86. internal class WeightmapSkeletonStyle : SkeletonStyleBase
  87. {
  88. protected override Color GetBoneColorRaw(BoneCache bone)
  89. {
  90. return bone.bindPoseColor;
  91. }
  92. protected override Color GetBoneColorRaw(int index)
  93. {
  94. return ModuleUtility.CalculateNiceColor(index, 6);
  95. }
  96. protected override float GetAlpha(BoneCache bone)
  97. {
  98. var skinningCache = bone.skinningCache;
  99. var selectedSprite = skinningCache.selectedSprite;
  100. var alpha = 0.9f;
  101. if (skinningCache.mode == SkinningMode.Character && skinningCache.selectedSprite != null)
  102. {
  103. var characterPart = selectedSprite.GetCharacterPart();
  104. if (characterPart.Contains(bone) == false)
  105. alpha = 0.25f;
  106. }
  107. return alpha;
  108. }
  109. }
  110. internal static class SkeletonStyles
  111. {
  112. public static readonly ISkeletonStyle Default = new BoneColorSkeletonStyle();
  113. public static readonly ISkeletonStyle WeightMap = new WeightmapSkeletonStyle();
  114. }
  115. }