DragHead.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. private bool shodaiFlg;
  15. public event EventHandler Select;
  16. public void Initialize(Transform head, Maid maid, Func<Vector3> posFunc, Func<Vector3> rotFunc)
  17. {
  18. base.Initialize(maid, 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. // Check for "Shodai" faces
  24. try
  25. {
  26. shodaiFlg = false;
  27. TMorph morph = maid.body0.Face.morph;
  28. float throwAway = Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues")
  29. [(int)morph.hash["tangopen"]];
  30. }
  31. catch
  32. {
  33. shodaiFlg = true;
  34. }
  35. InitializeGizmo(this.head);
  36. }
  37. protected override void GetDragType()
  38. {
  39. bool shift = Utility.GetModKey(Utility.ModKey.Shift);
  40. if (Utility.GetModKey(Utility.ModKey.Alt) && Utility.GetModKey(Utility.ModKey.Control))
  41. {
  42. // eyes
  43. dragType = DragType.MoveXZ;
  44. }
  45. else if (Input.GetKey(KeyCode.LeftAlt))
  46. {
  47. // head
  48. dragType = shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  49. }
  50. else if (Input.GetKey(KeyCode.A))
  51. {
  52. dragType = DragType.Select;
  53. }
  54. else
  55. {
  56. dragType = DragType.None;
  57. }
  58. }
  59. protected override void DoubleClick()
  60. {
  61. if (dragType == DragType.MoveXZ)
  62. {
  63. maid.body0.quaDefEyeL.eulerAngles = defEyeRotL;
  64. maid.body0.quaDefEyeR.eulerAngles = defEyeRotR;
  65. }
  66. }
  67. protected override void InitializeDrag()
  68. {
  69. if (dragType == DragType.Select)
  70. {
  71. Select?.Invoke(this, EventArgs.Empty);
  72. return;
  73. }
  74. base.InitializeDrag();
  75. rotate = head.localEulerAngles;
  76. eyeRotL = maid.body0.quaDefEyeL.eulerAngles;
  77. eyeRotR = maid.body0.quaDefEyeR.eulerAngles;
  78. mousePosOther = Input.mousePosition - mousePos;
  79. }
  80. protected override void Drag()
  81. {
  82. if (dragType == DragType.None || dragType == DragType.Select) return;
  83. if (dragType != DragType.MoveXZ)
  84. {
  85. if (isPlaying)
  86. {
  87. maid.GetAnimation().Stop();
  88. OnDragEvent();
  89. }
  90. }
  91. Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z);
  92. Vector3 vec31 = Input.mousePosition - mousePos;
  93. Transform t = GameMain.Instance.MainCamera.gameObject.transform;
  94. Vector3 vec32 = t.TransformDirection(Vector3.right);
  95. Vector3 vec33 = t.TransformDirection(Vector3.forward);
  96. if (dragType == DragType.RotLocalXZ)
  97. {
  98. head.localEulerAngles = rotate;
  99. head.RotateAround(head.position, new Vector3(vec32.x, 0.0f, vec32.z), vec31.y / 3f);
  100. head.RotateAround(head.position, new Vector3(vec33.x, 0.0f, vec33.z), (-vec31.x / 4.5f));
  101. }
  102. if (dragType == DragType.RotLocalY)
  103. {
  104. head.localEulerAngles = rotate;
  105. head.localRotation = Quaternion.Euler(head.localEulerAngles) * Quaternion.AngleAxis(vec31.x / 3f, Vector3.right);
  106. }
  107. if (dragType == DragType.MoveXZ)
  108. {
  109. Vector3 vec34 = new Vector3(eyeRotR.x, eyeRotR.y + vec31.x / 10f, eyeRotR.z + vec31.y / 10f);
  110. if (shodaiFlg)
  111. {
  112. if (vec34.z < 345.7f && vec34.z > 335.6f)
  113. mousePosOther.y = vec31.y;
  114. if (vec34.y < 347.6f && vec34.y > 335.6f)
  115. mousePosOther.x = vec31.x;
  116. }
  117. else
  118. {
  119. if (vec34.z < 354.8f && vec34.z > 344.8f)
  120. mousePosOther.y = vec31.y;
  121. if (vec34.y < 354.0f && vec34.y > 342.0f)
  122. mousePosOther.x = vec31.x;
  123. }
  124. maid.body0.quaDefEyeL.eulerAngles = new Vector3(eyeRotL.x, eyeRotL.y - mousePosOther.x / 10f, eyeRotL.z - mousePosOther.y / 10f);
  125. maid.body0.quaDefEyeR.eulerAngles = new Vector3(eyeRotR.x, eyeRotR.y + mousePosOther.x / 10f, eyeRotR.z + mousePosOther.y / 10f);
  126. }
  127. }
  128. }
  129. }