DragDogu.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. internal class DragDogu : BaseDrag
  7. {
  8. private GameObject dogu;
  9. private Vector3 off;
  10. private Vector3 off2;
  11. private Vector3 mousePos2;
  12. private float doguScale;
  13. private Vector3 doguRot;
  14. public event EventHandler Delete;
  15. public event EventHandler Rotate;
  16. public event EventHandler Scale;
  17. public event EventHandler Select;
  18. public bool DeleteMe { get; private set; }
  19. public bool keepDogu = false;
  20. public float scaleFactor = 1f;
  21. public void Initialize(GameObject dogu, bool keepDogu = false)
  22. {
  23. Initialize(dogu, keepDogu, GizmoMode.World,
  24. () => Vector3.zero
  25. );
  26. }
  27. public void Initialize(GameObject dogu, bool keepDogu, GizmoMode mode,
  28. Func<Vector3> position, Func<Vector3> rotation
  29. )
  30. {
  31. this.keepDogu = keepDogu;
  32. this.dogu = dogu;
  33. base.InitializeDragPoint(position, rotation);
  34. InitializeGizmo(this.dogu.transform, 1f, mode);
  35. gizmo.GizmoDrag += (s, a) =>
  36. {
  37. if (CurrentDragType == DragType.RotLocalY || CurrentDragType == DragType.RotLocalXZ)
  38. {
  39. OnRotate();
  40. }
  41. };
  42. }
  43. protected override void GetDragType()
  44. {
  45. bool holdShift = Utility.GetModKey(Utility.ModKey.Shift);
  46. if (Input.GetKey(KeyCode.A))
  47. {
  48. CurrentDragType = DragType.Select;
  49. CurrentGizmoType = GizmoType.None;
  50. }
  51. else if (Input.GetKey(KeyCode.D))
  52. {
  53. CurrentDragType = DragType.Delete;
  54. CurrentGizmoType = GizmoType.None;
  55. }
  56. else if (Input.GetKey(KeyCode.Z))
  57. {
  58. if (Utility.GetModKey(Utility.ModKey.Control)) CurrentDragType = DragType.MoveY;
  59. else CurrentDragType = holdShift ? DragType.RotY : DragType.MoveXZ;
  60. CurrentGizmoType = GizmoType.None;
  61. }
  62. else if (Input.GetKey(KeyCode.X))
  63. {
  64. CurrentDragType = holdShift ? DragType.RotLocalY : DragType.RotLocalXZ;
  65. CurrentGizmoType = GizmoType.Rotate;
  66. }
  67. else if (Input.GetKey(KeyCode.C))
  68. {
  69. CurrentDragType = DragType.Scale;
  70. CurrentGizmoType = GizmoType.None;
  71. }
  72. else
  73. {
  74. CurrentDragType = DragType.None;
  75. CurrentGizmoType = GizmoType.None;
  76. }
  77. }
  78. protected override void InitializeDrag()
  79. {
  80. if (CurrentDragType == DragType.Delete)
  81. {
  82. this.DeleteMe = true;
  83. this.Delete?.Invoke(this, EventArgs.Empty);
  84. return;
  85. }
  86. if (CurrentDragType == DragType.Select)
  87. {
  88. this.Select?.Invoke(this, EventArgs.Empty);
  89. return;
  90. }
  91. base.InitializeDrag();
  92. doguScale = dogu.transform.localScale.x;
  93. doguRot = dogu.transform.localEulerAngles;
  94. off = transform.position - Camera.main.ScreenToWorldPoint(
  95. new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)
  96. );
  97. off2 = new Vector3(
  98. transform.position.x - dogu.transform.position.x,
  99. transform.position.y - dogu.transform.position.y,
  100. transform.position.z - dogu.transform.position.z
  101. );
  102. }
  103. protected override void DoubleClick()
  104. {
  105. if (CurrentDragType == DragType.Scale)
  106. {
  107. dogu.transform.localScale = new Vector3(1f, 1f, 1f);
  108. OnScale();
  109. }
  110. if (CurrentDragType == DragType.RotLocalY || CurrentDragType == DragType.RotLocalXZ)
  111. {
  112. dogu.transform.rotation = new Quaternion(0f, 0f, 0f, 1f);
  113. OnRotate();
  114. }
  115. }
  116. protected override void Drag()
  117. {
  118. if (CurrentDragType == DragType.Select || CurrentDragType == DragType.Delete) return;
  119. Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off - off2;
  120. if (CurrentDragType == DragType.MoveXZ)
  121. {
  122. dogu.transform.position = new Vector3(pos.x, dogu.transform.position.y, pos.z);
  123. }
  124. if (CurrentDragType == DragType.MoveY)
  125. {
  126. dogu.transform.position = new Vector3(dogu.transform.position.x, pos.y, dogu.transform.position.z);
  127. }
  128. if (CurrentDragType == DragType.RotY)
  129. {
  130. Vector3 posOther = Input.mousePosition - mousePos;
  131. dogu.transform.eulerAngles =
  132. new Vector3(dogu.transform.eulerAngles.x, doguRot.y - posOther.x / 3f, dogu.transform.eulerAngles.z);
  133. OnRotate();
  134. }
  135. if (CurrentDragType == DragType.RotLocalXZ)
  136. {
  137. Vector3 posOther = Input.mousePosition - mousePos;
  138. Transform transform = Camera.main.transform;
  139. Vector3 vector3_3 = transform.TransformDirection(Vector3.right);
  140. Vector3 vector3_4 = transform.TransformDirection(Vector3.forward);
  141. transform.TransformDirection(Vector3.forward);
  142. if (mousePos2 != Input.mousePosition)
  143. {
  144. dogu.transform.localEulerAngles = doguRot;
  145. dogu.transform.RotateAround(dogu.transform.position, new Vector3(vector3_3.x, 0.0f, vector3_3.z), posOther.y / 4f);
  146. dogu.transform.RotateAround(dogu.transform.position, new Vector3(vector3_4.x, 0.0f, vector3_4.z), (-posOther.x / 6.0f));
  147. }
  148. mousePos2 = Input.mousePosition;
  149. OnRotate();
  150. }
  151. if (CurrentDragType == DragType.RotLocalY)
  152. {
  153. Vector3 posOther = Input.mousePosition - mousePos;
  154. Transform transform = Camera.main.transform;
  155. Vector3 vector3_3 = transform.TransformDirection(Vector3.right);
  156. transform.TransformDirection(Vector3.forward);
  157. dogu.transform.localEulerAngles = doguRot;
  158. dogu.transform.localRotation = Quaternion.Euler(dogu.transform.localEulerAngles)
  159. * Quaternion.AngleAxis((-posOther.x / 2.2f), Vector3.up);
  160. mousePos2 = Input.mousePosition;
  161. OnRotate();
  162. }
  163. if (CurrentDragType == DragType.Scale)
  164. {
  165. Vector3 posOther = Input.mousePosition - mousePos;
  166. float scale = doguScale + (posOther.y / 200f) * scaleFactor;
  167. if (scale < 0.1f) scale = 0.1f;
  168. dogu.transform.localScale = new Vector3(scale, scale, scale);
  169. OnScale();
  170. }
  171. }
  172. private void OnRotate()
  173. {
  174. Rotate?.Invoke(this, EventArgs.Empty);
  175. }
  176. private void OnScale()
  177. {
  178. Scale?.Invoke(this, EventArgs.Empty);
  179. }
  180. private void OnDestroy()
  181. {
  182. if (!keepDogu) GameObject.Destroy(this.dogu);
  183. }
  184. }
  185. }