DragPoint.cs 8.7 KB

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