BaseDrag.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. // TODO: Finalize dragpopint scaling
  7. internal abstract class BaseDrag : MonoBehaviour
  8. {
  9. private const float doubleClickSensitivity = 0.3f;
  10. protected const int upperArm = 0;
  11. protected const int foreArm = 1;
  12. protected const int hand = 2;
  13. protected const int upperArmRot = 0;
  14. protected const int handRot = 1;
  15. private GameObject gizmoGo;
  16. protected Maid maid;
  17. protected Meido meido;
  18. protected Func<Vector3> position;
  19. protected Func<Vector3> rotation;
  20. protected Renderer dragPointRenderer;
  21. protected Collider dragPointCollider;
  22. protected Vector3 worldPoint;
  23. protected Vector3 mousePos;
  24. private DragType dragType = DragType.None;
  25. protected DragType CurrentDragType
  26. {
  27. get => dragType;
  28. set
  29. {
  30. dragType = value;
  31. reInitDrag = dragType != dragTypeOld;
  32. dragTypeOld = dragType;
  33. }
  34. }
  35. protected DragType dragTypeOld;
  36. protected float doubleClickStart = 0f;
  37. protected bool reInitDrag = false;
  38. protected bool isPlaying;
  39. protected CustomGizmo gizmo;
  40. protected GizmoType CurrentGizmoType
  41. {
  42. get => gizmo?.CurrentGizmoType ?? GizmoType.None;
  43. set
  44. {
  45. if (gizmo != null)
  46. {
  47. if (GizmoActive) gizmo.CurrentGizmoType = value;
  48. }
  49. }
  50. }
  51. public Vector3 InitialScale { get; private set; }
  52. private float dragPointScale = 1f;
  53. public float DragPointScale
  54. {
  55. get => dragPointScale;
  56. set
  57. {
  58. dragPointScale = value;
  59. transform.localScale = InitialScale * dragPointScale;
  60. }
  61. }
  62. public bool IsBone { get; set; }
  63. public bool GizmoVisible
  64. {
  65. get => gizmo?.Visible ?? false;
  66. set
  67. {
  68. if (gizmo != null) gizmo.Visible = value;
  69. }
  70. }
  71. private bool gizmoActive = false;
  72. public bool GizmoActive
  73. {
  74. get => gizmoActive;
  75. set
  76. {
  77. if (gizmoGo != null)
  78. {
  79. gizmoActive = value;
  80. gizmoGo.SetActive(gizmoActive);
  81. GizmoVisible = gizmoActive;
  82. }
  83. }
  84. }
  85. private bool dragPointVisible;
  86. public bool DragPointVisible
  87. {
  88. get => dragPointVisible;
  89. set
  90. {
  91. dragPointVisible = value;
  92. dragPointRenderer.enabled = dragPointVisible;
  93. }
  94. }
  95. private bool dragPointActive;
  96. public bool DragPointActive
  97. {
  98. get => dragPointActive;
  99. set
  100. {
  101. dragPointActive = value;
  102. dragPointCollider.enabled = dragPointActive;
  103. }
  104. }
  105. public GizmoMode CurrentGizmoMode
  106. {
  107. get => gizmo?.gizmoMode ?? GizmoMode.Local;
  108. set
  109. {
  110. if (gizmo != null)
  111. {
  112. if (GizmoActive) gizmo.gizmoMode = value;
  113. }
  114. }
  115. }
  116. public event EventHandler DragEvent;
  117. protected enum DragType
  118. {
  119. None, Select, Delete,
  120. MoveXZ, MoveY, RotLocalXZ, RotY, RotLocalY,
  121. Scale
  122. }
  123. public static Material LightBlue = new Material(Shader.Find("Transparent/Diffuse"))
  124. {
  125. color = new Color(0.4f, 0.4f, 1f, 0.3f)
  126. };
  127. public static Material Blue = new Material(Shader.Find("Transparent/Diffuse"))
  128. {
  129. color = new Color(0.5f, 0.5f, 1f, 0.8f)
  130. };
  131. public static GameObject MakeDragPoint(PrimitiveType primitiveType, Vector3 scale, Material material)
  132. {
  133. GameObject dragPoint = GameObject.CreatePrimitive(primitiveType);
  134. dragPoint.transform.localScale = scale;
  135. dragPoint.GetComponent<Renderer>().material = material;
  136. dragPoint.layer = 8;
  137. return dragPoint;
  138. }
  139. public static T MakeDragPoint<T>(
  140. PrimitiveType primitiveType, Vector3 scale, Material material
  141. ) where T : BaseDrag
  142. {
  143. GameObject dragPoint = MakeDragPoint(primitiveType, scale, material);
  144. return dragPoint.AddComponent<T>();
  145. }
  146. public BaseDrag Initialize(Meido meido, Func<Vector3> position, Func<Vector3> rotation)
  147. {
  148. this.InitializeDragPoint(position, rotation);
  149. this.meido = meido;
  150. this.maid = meido.Maid;
  151. isPlaying = !meido.IsStop;
  152. return this;
  153. }
  154. protected void InitializeDragPoint(Func<Vector3> position, Func<Vector3> rotation)
  155. {
  156. this.InitialScale = transform.localScale;
  157. this.position = position;
  158. this.rotation = rotation;
  159. this.dragPointRenderer = GetComponent<Renderer>();
  160. this.dragPointCollider = GetComponent<Collider>();
  161. this.DragPointVisible = true;
  162. }
  163. protected void InitializeGizmo(Transform target, float scale = 0.25f, GizmoMode mode = GizmoMode.Local)
  164. {
  165. gizmoGo = CustomGizmo.MakeGizmo(target, scale, mode);
  166. gizmo = gizmoGo.GetComponent<CustomGizmo>();
  167. if (meido != null)
  168. {
  169. gizmo.GizmoDrag += (s, a) =>
  170. {
  171. meido.IsStop = true;
  172. isPlaying = false;
  173. };
  174. }
  175. GizmoActive = false;
  176. GizmoVisible = false;
  177. }
  178. public void SetDragProp(bool gizmoActive, bool dragPointActive, bool dragPointVisible)
  179. {
  180. this.GizmoActive = gizmoActive;
  181. this.DragPointActive = dragPointActive;
  182. this.DragPointVisible = dragPointVisible;
  183. }
  184. public void SetDragProp(bool gizmoActive, bool dragPointActive, bool dragPointVisible, GizmoMode mode)
  185. {
  186. SetDragProp(gizmoActive, dragPointActive, dragPointVisible);
  187. this.CurrentGizmoMode = mode;
  188. }
  189. protected virtual void InitializeDrag()
  190. {
  191. worldPoint = Camera.main.WorldToScreenPoint(transform.position);
  192. mousePos = Input.mousePosition;
  193. isPlaying = !meido?.IsStop ?? false;
  194. }
  195. protected virtual void DoubleClick() { }
  196. protected abstract void Drag();
  197. protected abstract void GetDragType();
  198. protected virtual void OnMouseUp()
  199. {
  200. if ((Time.time - doubleClickStart) < doubleClickSensitivity)
  201. {
  202. doubleClickStart = -1;
  203. DoubleClick();
  204. }
  205. else
  206. {
  207. doubleClickStart = Time.time;
  208. }
  209. }
  210. protected virtual void Update()
  211. {
  212. GetDragType();
  213. transform.position = position();
  214. transform.eulerAngles = rotation();
  215. }
  216. protected virtual void OnMouseDown()
  217. {
  218. InitializeDrag();
  219. }
  220. protected virtual void OnMouseDrag()
  221. {
  222. if (CurrentDragType == DragType.Select) return;
  223. if (reInitDrag)
  224. {
  225. reInitDrag = false;
  226. InitializeDrag();
  227. }
  228. if (mousePos != Input.mousePosition) Drag();
  229. }
  230. private void OnEnable()
  231. {
  232. if (position != null)
  233. {
  234. transform.position = position();
  235. transform.eulerAngles = rotation();
  236. }
  237. }
  238. private void OnDestroy()
  239. {
  240. GameObject.Destroy(gizmo);
  241. }
  242. protected void OnDragEvent()
  243. {
  244. DragEvent?.Invoke(null, EventArgs.Empty);
  245. }
  246. }
  247. }