DragHead.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, 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. }
  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) meido.IsStop = true;
  73. }
  74. Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z);
  75. Vector3 vec31 = Input.mousePosition - mousePos;
  76. Transform t = GameMain.Instance.MainCamera.gameObject.transform;
  77. Vector3 vec32 = t.TransformDirection(Vector3.right);
  78. Vector3 vec33 = t.TransformDirection(Vector3.forward);
  79. if (dragType == DragType.RotLocalXZ)
  80. {
  81. head.localEulerAngles = rotate;
  82. head.RotateAround(head.position, new Vector3(vec32.x, 0.0f, vec32.z), vec31.y / 3f);
  83. head.RotateAround(head.position, new Vector3(vec33.x, 0.0f, vec33.z), (-vec31.x / 4.5f));
  84. }
  85. if (dragType == DragType.RotLocalY)
  86. {
  87. head.localEulerAngles = rotate;
  88. head.localRotation = Quaternion.Euler(head.localEulerAngles) * Quaternion.AngleAxis(vec31.x / 3f, Vector3.right);
  89. }
  90. if (dragType == DragType.MoveXZ || dragType == DragType.MoveY)
  91. {
  92. int inv = dragType == DragType.MoveY ? -1 : 1;
  93. Vector3 vec34 = new Vector3(eyeRotR.x, eyeRotR.y + vec31.x / 10f, eyeRotR.z + vec31.y / 10f);
  94. mousePosOther.y = vec31.y;
  95. mousePosOther.x = vec31.x;
  96. maid.body0.quaDefEyeL.eulerAngles = new Vector3(eyeRotL.x, eyeRotL.y - mousePosOther.x / 10f, eyeRotL.z - mousePosOther.y / 10f);
  97. maid.body0.quaDefEyeR.eulerAngles = new Vector3(eyeRotR.x, eyeRotR.y + inv * mousePosOther.x / 10f, eyeRotR.z + mousePosOther.y / 10f);
  98. }
  99. }
  100. }
  101. }