DragPoint.cs 8.9 KB

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