DragPointGeneral.cs 6.7 KB

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