DragHead.cs 4.4 KB

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