DragBody.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. internal class DragBody : BaseDrag
  7. {
  8. private Vector3 off;
  9. private Vector3 off2;
  10. private Vector3 mousePos2;
  11. private float maidScale;
  12. private Vector3 maidRot;
  13. private bool scaling;
  14. public event EventHandler Select;
  15. public event EventHandler Scale;
  16. protected override void GetDragType()
  17. {
  18. bool holdShift = Utility.GetModKey(Utility.ModKey.Shift);
  19. if (Input.GetKey(KeyCode.A))
  20. {
  21. CurrentDragType = DragType.Select;
  22. }
  23. else if (Input.GetKey(KeyCode.Z))
  24. {
  25. if (Utility.GetModKey(Utility.ModKey.Control)) CurrentDragType = DragType.MoveY;
  26. else CurrentDragType = holdShift ? DragType.RotY : DragType.MoveXZ;
  27. }
  28. else if (Input.GetKey(KeyCode.X))
  29. {
  30. CurrentDragType = holdShift ? DragType.RotLocalY : DragType.RotLocalXZ;
  31. }
  32. else if (Input.GetKey(KeyCode.C))
  33. {
  34. CurrentDragType = DragType.Scale;
  35. }
  36. else
  37. {
  38. CurrentDragType = DragType.None;
  39. }
  40. }
  41. protected override void InitializeDrag()
  42. {
  43. if (CurrentDragType == DragType.Select)
  44. {
  45. Select?.Invoke(this, EventArgs.Empty);
  46. return;
  47. }
  48. base.InitializeDrag();
  49. maidScale = maid.transform.localScale.x;
  50. maidRot = maid.transform.localEulerAngles;
  51. off = transform.position - Camera.main.ScreenToWorldPoint(
  52. new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)
  53. );
  54. off2 = new Vector3(
  55. transform.position.x - maid.transform.position.x,
  56. transform.position.y - maid.transform.position.y,
  57. transform.position.z - maid.transform.position.z
  58. );
  59. }
  60. protected override void DoubleClick()
  61. {
  62. if (CurrentDragType == DragType.Scale)
  63. {
  64. maid.transform.localScale = new Vector3(1f, 1f, 1f);
  65. Scale?.Invoke(this, EventArgs.Empty);
  66. }
  67. if (CurrentDragType == DragType.RotLocalY || CurrentDragType == DragType.RotLocalXZ)
  68. maid.transform.eulerAngles = new Vector3(0f, maid.transform.eulerAngles.y, 0f);
  69. }
  70. protected override void OnMouseUp()
  71. {
  72. base.OnMouseUp();
  73. if (scaling)
  74. {
  75. scaling = false;
  76. Scale?.Invoke(this, EventArgs.Empty);
  77. }
  78. }
  79. protected override void Drag()
  80. {
  81. Vector3 pos = Camera.main.ScreenToWorldPoint(
  82. new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)
  83. ) + off - off2;
  84. if (CurrentDragType == DragType.MoveXZ)
  85. {
  86. maid.transform.position = new Vector3(pos.x, maid.transform.position.y, pos.z);
  87. }
  88. if (CurrentDragType == DragType.MoveY)
  89. {
  90. maid.transform.position = new Vector3(maid.transform.position.x, pos.y, maid.transform.position.z);
  91. }
  92. if (CurrentDragType == DragType.RotY)
  93. {
  94. Vector3 posOther = Input.mousePosition - mousePos;
  95. maid.transform.eulerAngles = new Vector3(
  96. maid.transform.eulerAngles.x, maidRot.y - posOther.x / 3f, maid.transform.eulerAngles.z
  97. );
  98. }
  99. if (CurrentDragType == DragType.RotLocalXZ)
  100. {
  101. Vector3 posOther = Input.mousePosition - mousePos;
  102. Transform transform = Camera.main.transform;
  103. Vector3 vector3_3 = transform.TransformDirection(Vector3.right);
  104. Vector3 vector3_4 = transform.TransformDirection(Vector3.forward);
  105. transform.TransformDirection(Vector3.forward);
  106. if (mousePos2 != Input.mousePosition)
  107. {
  108. maid.transform.localEulerAngles = maidRot;
  109. maid.transform.RotateAround(
  110. maid.transform.position,
  111. new Vector3(vector3_3.x, 0.0f, vector3_3.z),
  112. posOther.y / 4f
  113. );
  114. maid.transform.RotateAround(
  115. maid.transform.position,
  116. new Vector3(vector3_4.x, 0.0f, vector3_4.z),
  117. -posOther.x / 6.0f
  118. );
  119. }
  120. mousePos2 = Input.mousePosition;
  121. }
  122. if (CurrentDragType == DragType.RotLocalY)
  123. {
  124. Vector3 posOther = Input.mousePosition - mousePos;
  125. Transform transform = Camera.main.transform;
  126. transform.TransformDirection(Vector3.forward);
  127. if (mousePos2 != Input.mousePosition)
  128. {
  129. maid.transform.localEulerAngles = maidRot;
  130. maid.body0.transform.localRotation = Quaternion.Euler(maid.transform.localEulerAngles)
  131. * Quaternion.AngleAxis((-posOther.x / 2.2f), Vector3.up);
  132. }
  133. mousePos2 = Input.mousePosition;
  134. }
  135. if (CurrentDragType == DragType.Scale)
  136. {
  137. scaling = true;
  138. Vector3 posOther = Input.mousePosition - mousePos;
  139. float scale = maidScale + posOther.y / 200f;
  140. if (scale < 0.1f) scale = 0.1f;
  141. maid.transform.localScale = new Vector3(scale, scale, scale);
  142. }
  143. }
  144. }
  145. }