DragHead.cs 4.5 KB

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