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