BaseDrag.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. // TODO: Finalize dragpopint scaling
  7. public 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 event EventHandler DragEvent;
  104. protected enum DragType
  105. {
  106. None, Select,
  107. MoveXZ, MoveY, RotLocalXZ, RotY, RotLocalY,
  108. Scale
  109. }
  110. public static Material LightBlue = new Material(Shader.Find("Transparent/Diffuse"))
  111. {
  112. color = new Color(0.4f, 0.4f, 1f, 0.3f)
  113. };
  114. public static Material Blue = new Material(Shader.Find("Transparent/Diffuse"))
  115. {
  116. color = new Color(0.5f, 0.5f, 1f, 0.8f)
  117. };
  118. public static GameObject MakeDragPoint(PrimitiveType primitiveType, Vector3 scale, Material material)
  119. {
  120. GameObject dragPoint = GameObject.CreatePrimitive(primitiveType);
  121. dragPoint.transform.localScale = scale;
  122. dragPoint.GetComponent<Renderer>().material = material;
  123. dragPoint.layer = 8;
  124. return dragPoint;
  125. }
  126. public BaseDrag Initialize(Meido meido, Func<Vector3> position, Func<Vector3> rotation)
  127. {
  128. this.InitializeDragPoint(position, rotation);
  129. this.meido = meido;
  130. this.maid = meido.Maid;
  131. isPlaying = !meido.IsStop;
  132. return this;
  133. }
  134. protected void InitializeDragPoint(Func<Vector3> position, Func<Vector3> rotation)
  135. {
  136. this.BaseScale = transform.localScale;
  137. this.position = position;
  138. this.rotation = rotation;
  139. this.dragPointRenderer = GetComponent<Renderer>();
  140. this.dragPointCollider = GetComponent<Collider>();
  141. this.DragPointVisible = true;
  142. }
  143. protected void InitializeGizmo(Transform target, float scale = 0.25f, GizmoMode mode = GizmoMode.Local)
  144. {
  145. gizmoGo = CustomGizmo.MakeGizmo(target, scale, mode);
  146. gizmo = gizmoGo.GetComponent<CustomGizmo>();
  147. if (meido != null)
  148. {
  149. gizmo.GizmoDrag += (s, a) =>
  150. {
  151. meido.IsStop = true;
  152. isPlaying = false;
  153. };
  154. }
  155. GizmoActive = false;
  156. GizmoVisible = false;
  157. }
  158. public void SetDragProp(bool gizmoActive, bool dragPointActive, bool dragPointVisible)
  159. {
  160. this.GizmoActive = gizmoActive;
  161. this.DragPointActive = dragPointActive;
  162. this.DragPointVisible = dragPointVisible;
  163. }
  164. protected virtual void InitializeDrag()
  165. {
  166. worldPoint = Camera.main.WorldToScreenPoint(transform.position);
  167. mousePos = Input.mousePosition;
  168. isPlaying = !meido?.IsStop ?? false;
  169. }
  170. protected virtual void DoubleClick() { }
  171. protected abstract void Drag();
  172. protected abstract void GetDragType();
  173. protected virtual void OnMouseUp()
  174. {
  175. if ((Time.time - doubleClickStart) < doubleClickSensitivity)
  176. {
  177. doubleClickStart = -1;
  178. DoubleClick();
  179. }
  180. else
  181. {
  182. doubleClickStart = Time.time;
  183. }
  184. }
  185. protected virtual void Update()
  186. {
  187. GetDragType();
  188. transform.position = position();
  189. transform.eulerAngles = rotation();
  190. }
  191. protected virtual void OnMouseDown()
  192. {
  193. InitializeDrag();
  194. }
  195. protected virtual void OnMouseDrag()
  196. {
  197. if (CurrentDragType == DragType.Select) return;
  198. if (reInitDrag)
  199. {
  200. reInitDrag = false;
  201. InitializeDrag();
  202. }
  203. if (mousePos != Input.mousePosition) Drag();
  204. }
  205. private void OnEnable()
  206. {
  207. if (position != null)
  208. {
  209. transform.position = position();
  210. transform.eulerAngles = rotation();
  211. }
  212. }
  213. private void OnDestroy()
  214. {
  215. GameObject.Destroy(gizmo);
  216. }
  217. protected void OnDragEvent()
  218. {
  219. DragEvent?.Invoke(null, EventArgs.Empty);
  220. }
  221. }
  222. }