DragBody.cs 5.4 KB

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