DragPointLimb.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using UnityEngine;
  2. using Input = MeidoPhotoStudio.Plugin.InputManager;
  3. namespace MeidoPhotoStudio.Plugin;
  4. public class DragPointLimb : DragPointChain
  5. {
  6. private int foot = 1;
  7. private bool isLower;
  8. private bool isMiddle;
  9. private bool isUpper;
  10. public override bool IsBone
  11. {
  12. set
  13. {
  14. base.IsBone = value;
  15. BaseScale = isBone ? BoneScale : OriginalScale;
  16. }
  17. }
  18. public override void Set(Transform myObject)
  19. {
  20. base.Set(myObject);
  21. var name = myObject.name;
  22. foot = name.EndsWith("Foot") ? -1 : 1;
  23. isLower = name.EndsWith("Hand") || foot is -1;
  24. isMiddle = name.EndsWith("Calf") || name.EndsWith("Forearm");
  25. isUpper = !isMiddle && !isLower;
  26. if (isLower)
  27. ikChain[0] = ikChain[0].parent;
  28. }
  29. protected override void ApplyDragType()
  30. {
  31. var current = CurrentDragType;
  32. var isBone = IsBone;
  33. if (CurrentDragType is DragType.Ignore)
  34. {
  35. ApplyProperties();
  36. }
  37. else if (current is DragType.RotLocalXZ)
  38. {
  39. if (isLower)
  40. ApplyProperties(!isBone, false, isBone);
  41. else
  42. ApplyProperties();
  43. }
  44. else if (current is DragType.RotLocalY)
  45. {
  46. if (isLower || isMiddle)
  47. ApplyProperties(!isBone, false, false);
  48. else if (isUpper)
  49. ApplyProperties(false, false, isBone);
  50. else
  51. ApplyProperties();
  52. }
  53. else if (current is DragType.RotY)
  54. {
  55. if (isMiddle)
  56. ApplyProperties(false, false, isBone);
  57. else
  58. ApplyProperties();
  59. }
  60. else if (current is DragType.MoveXZ)
  61. {
  62. if (isLower)
  63. ApplyProperties(true, isBone, false);
  64. else
  65. ApplyProperties();
  66. }
  67. else
  68. {
  69. ApplyProperties(true, isBone, false);
  70. }
  71. }
  72. protected override void UpdateDragType()
  73. {
  74. var control = Input.Control;
  75. var alt = Input.Alt;
  76. // Check for DragMove so that hand dragpoint is not in the way
  77. if (OtherDragType())
  78. {
  79. CurrentDragType = DragType.Ignore;
  80. }
  81. else if (control && !Input.GetKey(MpsKey.DragMove))
  82. {
  83. if (alt)
  84. CurrentDragType = DragType.RotY;
  85. else
  86. CurrentDragType = DragType.MoveXZ;
  87. }
  88. else if (alt)
  89. {
  90. // TODO: Rethink this formatting
  91. CurrentDragType = Input.Shift
  92. ? DragType.RotLocalY
  93. : DragType.RotLocalXZ;
  94. }
  95. else
  96. {
  97. CurrentDragType = Input.Shift
  98. ? DragType.Ignore
  99. : DragType.None;
  100. }
  101. }
  102. protected override void Drag()
  103. {
  104. if (isPlaying)
  105. meido.Stop = true;
  106. var altRotation = CurrentDragType is DragType.MoveXZ or DragType.RotY;
  107. if (CurrentDragType is DragType.None || altRotation)
  108. {
  109. var upperJoint = altRotation ? JointMiddle : JointUpper;
  110. Porc(IK, ikCtrlData, ikChain[upperJoint], ikChain[JointMiddle], ikChain[JointLower]);
  111. InitializeRotation();
  112. }
  113. var mouseDelta = MouseDelta();
  114. if (CurrentDragType is DragType.RotLocalY)
  115. {
  116. var joint = isMiddle ? JointUpper : JointLower;
  117. ikChain[joint].localRotation = jointRotation[joint];
  118. ikChain[joint].Rotate(Vector3.right * (-mouseDelta.x / 1.5f));
  119. }
  120. if (CurrentDragType is DragType.RotLocalXZ)
  121. {
  122. ikChain[JointLower].localRotation = jointRotation[JointLower];
  123. ikChain[JointLower].Rotate(Vector3.up * (foot * mouseDelta.x / 1.5f));
  124. ikChain[JointLower].Rotate(Vector3.forward * (foot * mouseDelta.y / 1.5f));
  125. }
  126. }
  127. }