DragPoint.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. internal abstract class DragPoint : MonoBehaviour
  7. {
  8. private const float doubleClickSensitivity = 0.3f;
  9. private Func<Vector3> position;
  10. private Func<Vector3> rotation;
  11. private Collider collider;
  12. private Renderer renderer;
  13. private bool reinitializeDrag;
  14. protected bool Transforming => CurrentDragType >= DragType.MoveXZ;
  15. protected bool Moving => CurrentDragType == DragType.MoveXZ || CurrentDragType == DragType.MoveY;
  16. protected bool Rotating => CurrentDragType >= DragType.RotLocalXZ && CurrentDragType <= DragType.RotLocalY;
  17. protected bool Special => CurrentDragType == DragType.Select || CurrentDragType == DragType.Delete;
  18. private Vector3 startMousePosition;
  19. protected static Camera camera = GameMain.Instance.MainCamera.camera;
  20. public enum DragType
  21. {
  22. None, Ignore, Select, Delete,
  23. MoveXZ, MoveY,
  24. RotLocalXZ, RotY, RotLocalY,
  25. Scale
  26. }
  27. public Transform MyObject { get; protected set; }
  28. public GameObject MyGameObject => MyObject.gameObject;
  29. private float startDoubleClick;
  30. private Vector3 screenPoint;
  31. private Vector3 startOffset;
  32. private Vector3 newOffset;
  33. public static Material LightBlue = new Material(Shader.Find("Transparent/Diffuse"))
  34. {
  35. color = new Color(0.4f, 0.4f, 1f, 0.3f)
  36. };
  37. public static Material Blue = new Material(Shader.Find("Transparent/Diffuse"))
  38. {
  39. color = new Color(0.5f, 0.5f, 1f, 0.8f)
  40. };
  41. public Vector3 BaseScale { get; private set; }
  42. private float dragPointScale = 1f;
  43. public float DragPointScale
  44. {
  45. get => dragPointScale;
  46. set
  47. {
  48. dragPointScale = value;
  49. transform.localScale = BaseScale * dragPointScale;
  50. }
  51. }
  52. public GameObject GizmoGo { get; protected set; }
  53. public CustomGizmo Gizmo { get; protected set; }
  54. private DragType oldDragType;
  55. private DragType currentDragType;
  56. protected DragType CurrentDragType
  57. {
  58. get => currentDragType;
  59. set
  60. {
  61. if (value != oldDragType)
  62. {
  63. currentDragType = value;
  64. reinitializeDrag = true;
  65. oldDragType = currentDragType;
  66. ApplyDragType();
  67. }
  68. }
  69. }
  70. private bool dragPointEnabled = true;
  71. public bool DragPointEnabled
  72. {
  73. get => dragPointEnabled;
  74. set
  75. {
  76. if (dragPointEnabled == value) return;
  77. dragPointEnabled = value;
  78. ApplyDragType();
  79. }
  80. }
  81. private bool gizmoEnabled = true;
  82. public bool GizmoEnabled
  83. {
  84. get => GizmoGo != null && gizmoEnabled;
  85. set
  86. {
  87. if (GizmoGo == null || (GizmoGo != null && gizmoEnabled == value)) return;
  88. gizmoEnabled = value;
  89. ApplyDragType();
  90. }
  91. }
  92. private void Awake()
  93. {
  94. this.BaseScale = transform.localScale;
  95. this.collider = GetComponent<Collider>();
  96. this.renderer = GetComponent<Renderer>();
  97. ApplyDragType();
  98. }
  99. public static T Make<T>(PrimitiveType primitiveType, Vector3 scale, Material material) where T : DragPoint
  100. {
  101. GameObject dragPoint = GameObject.CreatePrimitive(primitiveType);
  102. dragPoint.transform.localScale = scale;
  103. dragPoint.GetComponent<Renderer>().material = material;
  104. dragPoint.layer = 8;
  105. return dragPoint.AddComponent<T>();
  106. }
  107. public virtual void Initialize(Func<Vector3> position, Func<Vector3> rotation)
  108. {
  109. this.position = position;
  110. this.rotation = rotation;
  111. }
  112. public virtual void Set(Transform myObject)
  113. {
  114. this.MyObject = myObject;
  115. }
  116. public virtual void AddGizmo(float scale = 0.25f, GizmoMode mode = GizmoMode.Local)
  117. {
  118. Gizmo = CustomGizmo.Make(this.MyObject, scale, mode);
  119. GizmoGo = Gizmo.gameObject;
  120. Gizmo.GizmoVisible = false;
  121. ApplyDragType();
  122. }
  123. protected virtual void ApplyDragType() { }
  124. public void ApplyProperties(bool active = false, bool visible = false, bool gizmo = false)
  125. {
  126. this.collider.enabled = active;
  127. this.renderer.enabled = visible;
  128. if (this.Gizmo != null) this.Gizmo.GizmoVisible = gizmo;
  129. }
  130. protected Vector3 MouseDelta() => Input.mousePosition - startMousePosition;
  131. protected bool OtherDragType()
  132. {
  133. return Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)
  134. || Input.GetKey(KeyCode.Z) || Input.GetKey(KeyCode.X) || Input.GetKey(KeyCode.C);
  135. }
  136. protected Vector3 CursorPosition()
  137. {
  138. Vector3 mousePosition = Input.mousePosition;
  139. return camera.ScreenToWorldPoint(
  140. new Vector3(mousePosition.x, mousePosition.y, screenPoint.z)
  141. ) + startOffset - newOffset;
  142. }
  143. protected virtual void Update()
  144. {
  145. transform.position = position();
  146. transform.eulerAngles = rotation();
  147. UpdateDragType();
  148. }
  149. protected virtual void OnMouseDown()
  150. {
  151. screenPoint = camera.WorldToScreenPoint(transform.position);
  152. startMousePosition = Input.mousePosition;
  153. startOffset = transform.position - camera.ScreenToWorldPoint(
  154. new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)
  155. );
  156. newOffset = transform.position - MyObject.position;
  157. }
  158. protected virtual void OnMouseDrag()
  159. {
  160. if (reinitializeDrag)
  161. {
  162. reinitializeDrag = false;
  163. OnMouseDown();
  164. }
  165. if (collider.enabled && Input.mousePosition != startMousePosition) Drag();
  166. }
  167. protected abstract void UpdateDragType();
  168. protected abstract void Drag();
  169. protected virtual void OnMouseUp()
  170. {
  171. if ((Time.time - startDoubleClick) < doubleClickSensitivity)
  172. {
  173. startDoubleClick = -1f;
  174. OnDoubleClick();
  175. }
  176. else
  177. {
  178. startDoubleClick = Time.time;
  179. }
  180. }
  181. protected virtual void OnDoubleClick() { }
  182. private void OnEnable()
  183. {
  184. if (position != null)
  185. {
  186. transform.position = position();
  187. transform.eulerAngles = rotation();
  188. }
  189. if (GizmoGo != null) GizmoGo.SetActive(true);
  190. ApplyDragType();
  191. }
  192. private void OnDisable()
  193. {
  194. if (GizmoGo != null) GizmoGo.SetActive(false);
  195. }
  196. protected virtual void OnDestroy() => GameObject.Destroy(GizmoGo);
  197. }
  198. }