DragPointGeneral.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. using Input = InputManager;
  7. public abstract class DragPointGeneral : DragPoint
  8. {
  9. public const float smallCube = 0.5f;
  10. private float currentScale;
  11. private bool scaling;
  12. private Quaternion currentRotation;
  13. public Quaternion DefaultRotation { get; set; } = Quaternion.identity;
  14. public Vector3 DefaultPosition { get; set; } = Vector3.zero;
  15. public float ScaleFactor { get; set; } = 1f;
  16. public bool ConstantScale { get; set; }
  17. public static readonly Color moveColour = new Color(0.2f, 0.5f, 0.95f, defaultAlpha);
  18. public static readonly Color rotateColour = new Color(0.2f, 0.75f, 0.3f, defaultAlpha);
  19. public static readonly Color scaleColour = new Color(0.8f, 0.7f, 0.3f, defaultAlpha);
  20. public static readonly Color selectColour = new Color(0.9f, 0.5f, 1f, defaultAlpha);
  21. public static readonly Color deleteColour = new Color(1f, 0.1f, 0.1f, defaultAlpha);
  22. public event EventHandler Move;
  23. public event EventHandler Rotate;
  24. public event EventHandler Scale;
  25. public event EventHandler EndScale;
  26. public event EventHandler Delete;
  27. public event EventHandler Select;
  28. public override void AddGizmo(float scale = 0.35f, GizmoMode mode = GizmoMode.Local)
  29. {
  30. base.AddGizmo(scale, mode);
  31. Gizmo.GizmoDrag += (s, a) =>
  32. {
  33. if (Gizmo.CurrentGizmoType == GizmoType.Rotate) OnRotate();
  34. };
  35. }
  36. protected virtual void ApplyColours()
  37. {
  38. Color colour = moveColour;
  39. if (Rotating) colour = rotateColour;
  40. else if (Scaling) colour = scaleColour;
  41. else if (Selecting) colour = selectColour;
  42. else if (Deleting) colour = deleteColour;
  43. ApplyColour(colour);
  44. }
  45. protected override void Update()
  46. {
  47. base.Update();
  48. if (ConstantScale)
  49. {
  50. float distance = Vector3.Distance(camera.transform.position, transform.position);
  51. transform.localScale = Vector3.one * (0.4f * BaseScale.x * DragPointScale * distance);
  52. }
  53. }
  54. protected override void UpdateDragType()
  55. {
  56. bool shift = Input.Shift;
  57. if (Input.GetKey(MpsKey.DragSelect))
  58. {
  59. CurrentDragType = DragType.Select;
  60. }
  61. else if (Input.GetKey(MpsKey.DragDelete))
  62. {
  63. CurrentDragType = DragType.Delete;
  64. }
  65. else if (Input.GetKey(MpsKey.DragMove))
  66. {
  67. if (Input.Control) CurrentDragType = DragType.MoveY;
  68. else CurrentDragType = shift ? DragType.RotY : DragType.MoveXZ;
  69. }
  70. else if (Input.GetKey(MpsKey.DragRotate))
  71. {
  72. CurrentDragType = shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  73. }
  74. else if (Input.GetKey(MpsKey.DragScale))
  75. {
  76. CurrentDragType = DragType.Scale;
  77. }
  78. else
  79. {
  80. CurrentDragType = DragType.None;
  81. }
  82. }
  83. protected override void OnMouseDown()
  84. {
  85. if (Deleting)
  86. {
  87. OnDelete();
  88. return;
  89. }
  90. if (Selecting)
  91. {
  92. OnSelect();
  93. return;
  94. }
  95. base.OnMouseDown();
  96. currentScale = MyObject.localScale.x;
  97. currentRotation = MyObject.rotation;
  98. }
  99. protected override void OnDoubleClick()
  100. {
  101. if (Scaling)
  102. {
  103. MyObject.localScale = Vector3.one;
  104. OnScale();
  105. OnEndScale();
  106. }
  107. if (Rotating)
  108. {
  109. MyObject.rotation = DefaultRotation;
  110. OnRotate();
  111. }
  112. if (Moving)
  113. {
  114. MyObject.position = DefaultPosition;
  115. OnMove();
  116. }
  117. }
  118. protected override void OnMouseUp()
  119. {
  120. base.OnMouseUp();
  121. if (scaling)
  122. {
  123. scaling = false;
  124. OnScale();
  125. OnEndScale();
  126. }
  127. }
  128. protected override void Drag()
  129. {
  130. if (CurrentDragType == DragType.Select || CurrentDragType == DragType.Delete) return;
  131. Vector3 cursorPosition = CursorPosition();
  132. Vector3 mouseDelta = MouseDelta();
  133. if (CurrentDragType == DragType.MoveXZ)
  134. {
  135. MyObject.position = new Vector3(cursorPosition.x, MyObject.position.y, cursorPosition.z);
  136. OnMove();
  137. }
  138. if (CurrentDragType == DragType.MoveY)
  139. {
  140. MyObject.position = new Vector3(
  141. MyObject.position.x, cursorPosition.y, MyObject.position.z
  142. );
  143. OnMove();
  144. }
  145. if (CurrentDragType == DragType.RotY)
  146. {
  147. MyObject.rotation = currentRotation;
  148. MyObject.Rotate(Vector3.up, -mouseDelta.x / 3f, Space.World);
  149. OnRotate();
  150. }
  151. if (CurrentDragType == DragType.RotLocalXZ)
  152. {
  153. MyObject.rotation = currentRotation;
  154. Vector3 forward = camera.transform.forward;
  155. Vector3 right = camera.transform.right;
  156. forward.y = 0f;
  157. right.y = 0f;
  158. MyObject.Rotate(forward, -mouseDelta.x / 6f, Space.World);
  159. MyObject.Rotate(right, mouseDelta.y / 4f, Space.World);
  160. OnRotate();
  161. }
  162. if (CurrentDragType == DragType.RotLocalY)
  163. {
  164. MyObject.rotation = currentRotation;
  165. MyObject.Rotate(Vector3.up * -mouseDelta.x / 2.2f);
  166. OnRotate();
  167. }
  168. if (CurrentDragType == DragType.Scale)
  169. {
  170. scaling = true;
  171. float scale = currentScale + (mouseDelta.y / 200f * ScaleFactor);
  172. if (scale < 0.1f) scale = 0.1f;
  173. MyObject.localScale = new Vector3(scale, scale, scale);
  174. OnScale();
  175. }
  176. }
  177. protected virtual void OnEndScale() => OnEvent(EndScale);
  178. protected virtual void OnScale() => OnEvent(Scale);
  179. protected virtual void OnMove() => OnEvent(Move);
  180. protected virtual void OnRotate() => OnEvent(Rotate);
  181. protected virtual void OnSelect() => OnEvent(Select);
  182. protected virtual void OnDelete() => OnEvent(Delete);
  183. private void OnEvent(EventHandler handler) => handler?.Invoke(this, EventArgs.Empty);
  184. }
  185. }