DragPointFinger.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using UnityEngine;
  2. using Input = MeidoPhotoStudio.Plugin.InputManager;
  3. namespace MeidoPhotoStudio.Plugin;
  4. public class DragPointFinger : DragPointMeido
  5. {
  6. private static readonly Color DragpointColour = new(0.1f, 0.4f, 0.95f, DefaultAlpha);
  7. private readonly TBody.IKCMO ik = new();
  8. private readonly Quaternion[] jointRotation = new Quaternion[2];
  9. // WARN: This does NOT work and is only done so the compiler does not complain
  10. private
  11. #if COM25
  12. AIKCtrl
  13. #else
  14. IKCtrlData
  15. #endif
  16. ikCtrlData;
  17. private Transform[] ikChain;
  18. private bool baseFinger;
  19. public override void Set(Transform myObject)
  20. {
  21. base.Set(myObject);
  22. var parentName = myObject.parent.name.Split(' ')[2];
  23. // Base finger names have the form 'FingerN' or 'ToeN' where N is a natural number
  24. baseFinger = parentName.Length is 7 or 4;
  25. ikChain = new Transform[2] { myObject.parent, myObject };
  26. ikCtrlData = IkCtrlData;
  27. }
  28. protected override void ApplyDragType()
  29. {
  30. if (baseFinger && CurrentDragType is DragType.RotLocalY)
  31. ApplyProperties(true, true, false);
  32. else if (CurrentDragType is DragType.MoveXZ)
  33. ApplyProperties(true, true, false);
  34. else
  35. ApplyProperties(false, false, false);
  36. ApplyColour(DragpointColour);
  37. }
  38. protected override void UpdateDragType() =>
  39. CurrentDragType = !Input.GetKey(MpsKey.DragFinger)
  40. ? DragType.None
  41. : Input.Shift
  42. ? DragType.RotLocalY
  43. : DragType.MoveXZ;
  44. protected override void OnMouseDown()
  45. {
  46. base.OnMouseDown();
  47. jointRotation[JointUpper] = ikChain[JointUpper].localRotation;
  48. jointRotation[JointMiddle] = ikChain[JointMiddle].localRotation;
  49. InitializeIK(ik, ikChain[JointUpper], ikChain[JointUpper], ikChain[JointMiddle]);
  50. }
  51. protected override void Drag()
  52. {
  53. if (isPlaying)
  54. meido.Stop = true;
  55. if (CurrentDragType is DragType.MoveXZ)
  56. {
  57. Porc(ik, ikCtrlData, ikChain[JointUpper], ikChain[JointUpper], ikChain[JointMiddle]);
  58. if (!baseFinger)
  59. {
  60. SetRotation(JointUpper);
  61. SetRotation(JointMiddle);
  62. }
  63. else
  64. {
  65. jointRotation[JointUpper] = ikChain[JointUpper].localRotation;
  66. jointRotation[JointMiddle] = ikChain[JointMiddle].localRotation;
  67. }
  68. }
  69. else if (CurrentDragType is DragType.RotLocalY)
  70. {
  71. var mouseDelta = MouseDelta();
  72. ikChain[JointUpper].localRotation = jointRotation[JointUpper];
  73. ikChain[JointUpper].Rotate(Vector3.right * (mouseDelta.x / 1.5f));
  74. }
  75. }
  76. private void SetRotation(int joint)
  77. {
  78. var rotation = jointRotation[joint].eulerAngles;
  79. rotation.z = ikChain[joint].localEulerAngles.z;
  80. ikChain[joint].localRotation = Quaternion.Euler(rotation);
  81. }
  82. }