BaseDrag.cs 7.9 KB

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