DragPointGeneral.cs 7.0 KB

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