DragPointGeneral.cs 6.5 KB

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