DragPointGeneral.cs 6.5 KB

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