DragPoint.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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, 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 || (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. BaseScale = transform.localScale;
  103. collider = GetComponent<Collider>();
  104. renderer = GetComponent<Renderer>();
  105. ApplyDragType();
  106. }
  107. private static GameObject DragPointParent()
  108. {
  109. return dragPointParent ? dragPointParent : (dragPointParent = new GameObject("[MPS DragPoint Parent]"));
  110. }
  111. public static T Make<T>(PrimitiveType primitiveType, Vector3 scale) where T : DragPoint
  112. {
  113. GameObject dragPointGo = GameObject.CreatePrimitive(primitiveType);
  114. dragPointGo.transform.SetParent(DragPointParent().transform, false);
  115. dragPointGo.transform.localScale = scale;
  116. dragPointGo.layer = 8;
  117. T dragPoint = dragPointGo.AddComponent<T>();
  118. dragPoint.renderer.material = dragPointMaterial;
  119. dragPoint.renderer.material.color = defaultColour;
  120. return dragPoint;
  121. }
  122. public virtual void Initialize(Func<Vector3> position, Func<Vector3> rotation)
  123. {
  124. this.position = position;
  125. this.rotation = rotation;
  126. }
  127. public virtual void Set(Transform myObject)
  128. {
  129. MyObject = myObject;
  130. gameObject.name = $"[MPS DragPoint: {MyObject.name}]";
  131. }
  132. public virtual void AddGizmo(float scale = 0.25f, GizmoMode mode = GizmoMode.Local)
  133. {
  134. Gizmo = CustomGizmo.Make(MyObject, scale, mode);
  135. GizmoGo = Gizmo.gameObject;
  136. Gizmo.GizmoVisible = false;
  137. ApplyDragType();
  138. }
  139. protected virtual void ApplyDragType() { }
  140. public void ApplyProperties(bool active = false, bool visible = false, bool gizmo = false)
  141. {
  142. collider.enabled = active;
  143. renderer.enabled = visible;
  144. if (Gizmo) Gizmo.GizmoVisible = gizmo;
  145. }
  146. protected void ApplyColour(Color colour) => renderer.material.color = colour;
  147. protected void ApplyColour(float r, float g, float b, float a = defaultAlpha)
  148. {
  149. ApplyColour(new Color(r, g, b, a));
  150. }
  151. protected Vector3 MouseDelta() => Utility.MousePosition - startMousePosition;
  152. protected bool OtherDragType()
  153. {
  154. return InputManager.GetKey(MpsKey.DragSelect) || InputManager.GetKey(MpsKey.DragDelete)
  155. || InputManager.GetKey(MpsKey.DragMove) || InputManager.GetKey(MpsKey.DragRotate)
  156. || InputManager.GetKey(MpsKey.DragScale) || InputManager.GetKey(MpsKey.DragFinger)
  157. || InputManager.Shift;
  158. }
  159. protected Vector3 CursorPosition()
  160. {
  161. Vector3 mousePosition = Utility.MousePosition;
  162. return camera.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, screenPoint.z))
  163. + startOffset - newOffset;
  164. }
  165. protected virtual void Update()
  166. {
  167. transform.position = position();
  168. transform.eulerAngles = rotation();
  169. UpdateDragType();
  170. }
  171. protected virtual void OnMouseDown()
  172. {
  173. screenPoint = camera.WorldToScreenPoint(transform.position);
  174. startMousePosition = Utility.MousePosition;
  175. startOffset = transform.position - camera.ScreenToWorldPoint(
  176. new Vector3(startMousePosition.x, startMousePosition.y, screenPoint.z)
  177. );
  178. newOffset = transform.position - MyObject.position;
  179. }
  180. protected virtual void OnMouseDrag()
  181. {
  182. if (reinitializeDrag)
  183. {
  184. reinitializeDrag = false;
  185. OnMouseDown();
  186. }
  187. if (collider.enabled && startMousePosition != Utility.MousePosition) Drag();
  188. }
  189. protected abstract void UpdateDragType();
  190. protected abstract void Drag();
  191. protected virtual void OnMouseUp()
  192. {
  193. if ((Time.time - startDoubleClick) < doubleClickSensitivity)
  194. {
  195. startDoubleClick = -1f;
  196. OnDoubleClick();
  197. }
  198. else
  199. {
  200. startDoubleClick = Time.time;
  201. }
  202. }
  203. protected virtual void OnDoubleClick() { }
  204. private void OnEnable()
  205. {
  206. if (position != null)
  207. {
  208. transform.position = position();
  209. transform.eulerAngles = rotation();
  210. }
  211. if (GizmoGo) GizmoGo.SetActive(true);
  212. ApplyDragType();
  213. }
  214. private void OnDisable()
  215. {
  216. if (GizmoGo) GizmoGo.SetActive(false);
  217. }
  218. protected virtual void OnDestroy() => Destroy(GizmoGo);
  219. }
  220. }