DragHead.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. internal class DragHead : BaseDrag
  6. {
  7. private Transform head;
  8. private Vector3 rotate;
  9. private Vector3 eyeRotL;
  10. private Vector3 eyeRotR;
  11. private Vector3 defEyeRotL;
  12. private Vector3 defEyeRotR;
  13. private Vector3 mousePosOther;
  14. public event EventHandler Select;
  15. public bool IsIK { get; set; }
  16. public DragHead Initialize(Transform head, Meido meido, Func<Vector3> posFunc, Func<Vector3> rotFunc)
  17. {
  18. base.Initialize(meido, posFunc, rotFunc);
  19. this.head = head;
  20. // default eye rotations
  21. defEyeRotL = this.maid.body0.quaDefEyeL.eulerAngles;
  22. defEyeRotR = this.maid.body0.quaDefEyeR.eulerAngles;
  23. InitializeGizmo(this.head);
  24. return this;
  25. }
  26. protected override void GetDragType()
  27. {
  28. bool shift = Utility.GetModKey(Utility.ModKey.Shift);
  29. if (Utility.GetModKey(Utility.ModKey.Alt) && Utility.GetModKey(Utility.ModKey.Control))
  30. {
  31. // eyes
  32. CurrentDragType = Utility.GetModKey(Utility.ModKey.Shift) ? DragType.MoveY : DragType.MoveXZ;
  33. }
  34. else if (Utility.GetModKey(Utility.ModKey.Alt))
  35. {
  36. // head
  37. CurrentDragType = shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  38. }
  39. else if (Input.GetKey(KeyCode.A))
  40. {
  41. CurrentDragType = DragType.Select;
  42. }
  43. else
  44. {
  45. CurrentDragType = DragType.None;
  46. }
  47. }
  48. protected override void DoubleClick()
  49. {
  50. if (CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.MoveY)
  51. {
  52. maid.body0.quaDefEyeL.eulerAngles = defEyeRotL;
  53. maid.body0.quaDefEyeR.eulerAngles = defEyeRotR;
  54. }
  55. if (CurrentDragType == DragType.RotLocalY || CurrentDragType == DragType.RotLocalXZ)
  56. {
  57. meido.IsFreeLook = !meido.IsFreeLook;
  58. }
  59. }
  60. protected override void InitializeDrag()
  61. {
  62. if (CurrentDragType == DragType.Select)
  63. {
  64. Select?.Invoke(this, EventArgs.Empty);
  65. return;
  66. }
  67. if (IsBone) return;
  68. base.InitializeDrag();
  69. rotate = head.localEulerAngles;
  70. eyeRotL = maid.body0.quaDefEyeL.eulerAngles;
  71. eyeRotR = maid.body0.quaDefEyeR.eulerAngles;
  72. mousePosOther = Input.mousePosition - mousePos;
  73. }
  74. protected override void Drag()
  75. {
  76. if (!IsIK || (CurrentDragType == DragType.None || CurrentDragType == DragType.Select) || IsBone) return;
  77. if (!(CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.MoveY))
  78. {
  79. if (isPlaying) meido.IsStop = true;
  80. }
  81. Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z);
  82. Vector3 vec31 = Input.mousePosition - mousePos;
  83. Transform t = GameMain.Instance.MainCamera.gameObject.transform;
  84. Vector3 vec32 = t.TransformDirection(Vector3.right);
  85. Vector3 vec33 = t.TransformDirection(Vector3.forward);
  86. if (CurrentDragType == DragType.RotLocalXZ)
  87. {
  88. head.localEulerAngles = rotate;
  89. head.RotateAround(head.position, new Vector3(vec32.x, 0.0f, vec32.z), vec31.y / 3f);
  90. head.RotateAround(head.position, new Vector3(vec33.x, 0.0f, vec33.z), (-vec31.x / 4.5f));
  91. }
  92. if (CurrentDragType == DragType.RotLocalY)
  93. {
  94. head.localEulerAngles = rotate;
  95. head.localRotation = Quaternion.Euler(head.localEulerAngles)
  96. * Quaternion.AngleAxis(vec31.x / 3f, Vector3.right);
  97. }
  98. if (CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.MoveY)
  99. {
  100. int inv = CurrentDragType == DragType.MoveY ? -1 : 1;
  101. Vector3 vec34 = new Vector3(eyeRotR.x, eyeRotR.y + vec31.x / 10f, eyeRotR.z + vec31.y / 10f);
  102. mousePosOther.y = vec31.y;
  103. mousePosOther.x = vec31.x;
  104. maid.body0.quaDefEyeL.eulerAngles = new Vector3(
  105. eyeRotL.x, eyeRotL.y - mousePosOther.x / 10f, eyeRotL.z - mousePosOther.y / 10f
  106. );
  107. maid.body0.quaDefEyeR.eulerAngles = new Vector3(
  108. eyeRotR.x, eyeRotR.y + inv * mousePosOther.x / 10f, eyeRotR.z + mousePosOther.y / 10f
  109. );
  110. }
  111. }
  112. }
  113. }