DragHead.cs 4.3 KB

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