DragPointGeneral.cs 6.8 KB

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