DragDogu.cs 6.9 KB

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