DragBody.cs 5.7 KB

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