DragDogu.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 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. () => 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. doguRotation = Dogu.transform.rotation;
  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 = Quaternion.identity;
  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 mouseDelta = Input.mousePosition - mousePos;
  132. Dogu.transform.rotation = doguRotation;
  133. Dogu.transform.Rotate(Vector3.up, doguRotation.y - mouseDelta.x / 3f, Space.World);
  134. OnRotate();
  135. }
  136. if (CurrentDragType == DragType.RotLocalXZ)
  137. {
  138. Vector3 mouseDelta = Input.mousePosition - mousePos;
  139. Vector3 cameraDirectionForward = Camera.main.transform.TransformDirection(Vector3.forward);
  140. Vector3 cameraDirectionRight = Camera.main.transform.TransformDirection(Vector3.right);
  141. Dogu.transform.rotation = doguRotation;
  142. Dogu.transform.Rotate(cameraDirectionForward, doguRotation.x - mouseDelta.x / 3f, Space.World);
  143. Dogu.transform.Rotate(cameraDirectionRight, doguRotation.y + mouseDelta.y / 3f, Space.World);
  144. OnRotate();
  145. }
  146. if (CurrentDragType == DragType.RotLocalY)
  147. {
  148. Vector3 mouseDelta = Input.mousePosition - mousePos;
  149. Dogu.transform.rotation = doguRotation;
  150. Dogu.transform.Rotate(Vector3.up * (-mouseDelta.x / 2.2f));
  151. OnRotate();
  152. }
  153. if (CurrentDragType == DragType.Scale)
  154. {
  155. Vector3 posOther = Input.mousePosition - mousePos;
  156. float scale = doguScale + (posOther.y / 200f) * scaleFactor;
  157. if (scale < 0.1f) scale = 0.1f;
  158. Dogu.transform.localScale = new Vector3(scale, scale, scale);
  159. OnScale();
  160. }
  161. }
  162. private void OnRotate()
  163. {
  164. Rotate?.Invoke(this, EventArgs.Empty);
  165. }
  166. private void OnScale()
  167. {
  168. Scale?.Invoke(this, EventArgs.Empty);
  169. }
  170. private void OnDestroy()
  171. {
  172. if (!keepDogu) GameObject.Destroy(this.Dogu);
  173. }
  174. }
  175. }