DragDogu.cs 7.4 KB

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