BoneTreeViewModel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal class BoneVisibilityToolData : CacheObject
  6. {
  7. [SerializeField]
  8. bool m_AllVisibility = true;
  9. bool m_PreviousVisibility = true;
  10. public bool allVisibility
  11. {
  12. get { return m_AllVisibility; }
  13. set { m_AllVisibility = value; }
  14. }
  15. public bool previousVisiblity
  16. {
  17. get { return m_PreviousVisibility; }
  18. set { m_PreviousVisibility = value; }
  19. }
  20. }
  21. internal class BoneTreeWidgetModel : IBoneTreeViewModel
  22. {
  23. protected SkinningCache m_SkinningCache;
  24. protected IBoneVisibilityToolView m_View;
  25. protected BoneVisibilityToolData m_Data;
  26. public SkinningCache skinningCache
  27. {
  28. get { return m_SkinningCache; }
  29. }
  30. public IBoneVisibilityToolView view
  31. {
  32. get { return m_View; }
  33. }
  34. public virtual bool GetAllVisibility()
  35. {
  36. return m_Data.allVisibility;
  37. }
  38. public SkeletonSelection GetBoneSelection()
  39. {
  40. return skinningCache.skeletonSelection;
  41. }
  42. public BoneCache[] GetExpandedBones()
  43. {
  44. return skinningCache.GetExpandedBones();
  45. }
  46. public int GetDepth(BoneCache bone)
  47. {
  48. return (int)bone.depth;
  49. }
  50. public SkeletonCache GetSelectedSkeleton()
  51. {
  52. return skinningCache.GetEffectiveSkeleton(skinningCache.selectedSprite);
  53. }
  54. public bool GetVisibility(BoneCache bone)
  55. {
  56. return bone.isVisible;
  57. }
  58. public void SelectBones(BoneCache[] bones)
  59. {
  60. skinningCache.skeletonSelection.elements = bones.ToCharacterIfNeeded();
  61. }
  62. public void SetExpandedBones(BoneCache[] bones)
  63. {
  64. skinningCache.BoneExpansionChanged(bones);
  65. }
  66. public virtual void SetAllVisibility(SkeletonCache skeleton, bool visibility)
  67. {
  68. m_Data.allVisibility = visibility;
  69. SetAllBoneVisibility(skeleton, visibility);
  70. UpdateVisibilityFromPersistentState();
  71. }
  72. public static void SetAllBoneVisibility(SkeletonCache skeleton, bool visibility)
  73. {
  74. if (skeleton != null)
  75. {
  76. foreach (var bone in skeleton.bones)
  77. bone.isVisible = visibility;
  78. }
  79. }
  80. public void SetBoneParent(BoneCache newParent, BoneCache bone, int insertAtIndex)
  81. {
  82. TransformCache parent = newParent;
  83. if (newParent == null)
  84. parent = bone.skeleton;
  85. skinningCache.RestoreBindPose();
  86. bone.SetParent(parent, true);
  87. if (insertAtIndex == -1)
  88. insertAtIndex = parent.ChildCount;
  89. bone.siblingIndex = insertAtIndex;
  90. bone.SetDefaultPose();
  91. }
  92. public void SetDepth(BoneCache bone, int depth)
  93. {
  94. var characterBone = bone.ToCharacterIfNeeded();
  95. characterBone.depth = depth;
  96. if (characterBone != bone || skinningCache.mode == SkinningMode.Character)
  97. skinningCache.SyncSpriteSheetSkeletons();
  98. skinningCache.events.boneDepthChanged.Invoke(bone);
  99. }
  100. public void SetName(BoneCache bone, string name)
  101. {
  102. var characterBone = bone.ToCharacterIfNeeded();
  103. characterBone.name = name;
  104. if (characterBone != bone || skinningCache.mode == SkinningMode.Character)
  105. {
  106. skinningCache.SyncSpriteSheetSkeletons();
  107. }
  108. }
  109. public void SetVisibility(BoneCache bone, bool visibility)
  110. {
  111. bone.isVisible = visibility;
  112. UpdateVisibilityFromPersistentState();
  113. }
  114. public UndoScope UndoScope(string value)
  115. {
  116. return skinningCache.UndoScope(value);
  117. }
  118. private void UpdateVisibilityFromPersistentState()
  119. {
  120. skinningCache.BoneVisibilityChanged(GetSelectedSkeleton());
  121. }
  122. public bool hasCharacter {get { return skinningCache.hasCharacter; } }
  123. public SkinningMode mode {get { return skinningCache.mode; } }
  124. }
  125. internal interface IBoneTreeViewModel
  126. {
  127. void SetVisibility(BoneCache bone, bool visibility);
  128. bool GetVisibility(BoneCache bone);
  129. void SetName(BoneCache bone, string name);
  130. void SetBoneParent(BoneCache newParent, BoneCache bone, int insertAtIndex);
  131. int GetDepth(BoneCache bone);
  132. void SetDepth(BoneCache bone, int depth);
  133. void SetAllVisibility(SkeletonCache skeleton, bool visibility);
  134. bool GetAllVisibility();
  135. void SelectBones(BoneCache[] bones);
  136. void SetExpandedBones(BoneCache[] bones);
  137. IBoneVisibilityToolView view { get; }
  138. SkeletonSelection GetBoneSelection();
  139. BoneCache[] GetExpandedBones();
  140. SkeletonCache GetSelectedSkeleton();
  141. bool hasCharacter { get; }
  142. SkinningMode mode { get; }
  143. UndoScope UndoScope(string value);
  144. }
  145. }