BaseDrag.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. // TODO: Finalize dragpopint scaling
  6. public abstract class BaseDrag : MonoBehaviour
  7. {
  8. private const float doubleClickSensitivity = 0.3f;
  9. protected const int upperArm = 0;
  10. protected const int foreArm = 1;
  11. protected const int hand = 2;
  12. protected const int upperArmRot = 0;
  13. protected const int handRot = 1;
  14. protected Maid maid;
  15. protected Meido meido;
  16. protected Func<Vector3> position;
  17. protected Func<Vector3> rotation;
  18. protected Renderer dragPointRenderer;
  19. protected Collider dragPointCollider;
  20. protected Vector3 worldPoint;
  21. protected Vector3 mousePos;
  22. protected DragType dragType = DragType.None;
  23. protected DragType dragTypeOld;
  24. protected GizmoType gizmoType;
  25. protected GizmoType gizmoTypeOld;
  26. protected float doubleClickStart = 0f;
  27. protected bool reInitDrag = false;
  28. protected bool isPlaying;
  29. protected GizmoRender gizmo;
  30. public Vector3 BaseScale { get; private set; }
  31. public Vector3 DragPointScale
  32. {
  33. get => transform.localScale;
  34. set
  35. {
  36. transform.localScale = value;
  37. }
  38. }
  39. public bool IsBone { get; set; }
  40. public float GizmoScale
  41. {
  42. get => gizmo.offsetScale;
  43. set
  44. {
  45. if (gizmo != null) gizmo.offsetScale = value;
  46. }
  47. }
  48. public bool GizmoVisible
  49. {
  50. get => gizmo?.Visible ?? false;
  51. set
  52. {
  53. if (gizmo != null) gizmo.Visible = value;
  54. }
  55. }
  56. private bool gizmoActive = false;
  57. public bool GizmoActive
  58. {
  59. get => gizmoActive;
  60. set
  61. {
  62. gizmoActive = value;
  63. GizmoVisible = gizmoActive;
  64. }
  65. }
  66. private bool dragPointVisible;
  67. public bool DragPointVisible
  68. {
  69. get => dragPointVisible;
  70. set
  71. {
  72. dragPointVisible = value;
  73. dragPointRenderer.enabled = dragPointVisible;
  74. }
  75. }
  76. private bool dragPointActive;
  77. public bool DragPointActive
  78. {
  79. get => dragPointActive;
  80. set
  81. {
  82. dragPointActive = value;
  83. dragPointCollider.enabled = dragPointActive;
  84. }
  85. }
  86. protected static bool IsGizmoDrag => Utility.GetFieldValue<GizmoRender, bool>(null, "is_drag_");
  87. public event EventHandler DragEvent;
  88. protected enum DragType
  89. {
  90. None, Select,
  91. MoveXZ, MoveY, RotLocalXZ, RotY, RotLocalY,
  92. Scale
  93. }
  94. protected enum GizmoType
  95. {
  96. Rotate, Move, Scale, None
  97. }
  98. public virtual void Initialize(Func<Vector3> position, Func<Vector3> rotation)
  99. {
  100. this.BaseScale = transform.localScale;
  101. this.position = position;
  102. this.rotation = rotation;
  103. this.dragPointRenderer = GetComponent<Renderer>();
  104. this.dragPointCollider = GetComponent<Collider>();
  105. this.DragPointVisible = true;
  106. }
  107. public virtual BaseDrag Initialize(Meido meido, Func<Vector3> position, Func<Vector3> rotation)
  108. {
  109. this.Initialize(position, rotation);
  110. this.meido = meido;
  111. this.maid = meido.Maid;
  112. isPlaying = !meido.IsStop;
  113. return this;
  114. }
  115. protected void InitializeGizmo(GameObject target, float scale = 0.25f)
  116. {
  117. gizmo = target.AddComponent<GizmoRender>();
  118. gizmo.eRotate = true;
  119. gizmo.offsetScale = scale;
  120. gizmo.lineRSelectedThick = 0.25f;
  121. GizmoVisible = false;
  122. }
  123. protected void InitializeGizmo(Transform target, float scale = 0.25f)
  124. {
  125. InitializeGizmo(target.gameObject, scale);
  126. }
  127. protected void SetGizmo(GizmoType type)
  128. {
  129. if (type == GizmoType.Move)
  130. {
  131. gizmo.eAxis = true;
  132. gizmo.eRotate = false;
  133. gizmo.eScal = false;
  134. GizmoVisible = true;
  135. }
  136. else if (type == GizmoType.Rotate)
  137. {
  138. gizmo.eAxis = false;
  139. gizmo.eRotate = true;
  140. gizmo.eScal = false;
  141. GizmoVisible = true;
  142. }
  143. else if (type == GizmoType.Scale)
  144. {
  145. gizmo.eAxis = false;
  146. gizmo.eRotate = false;
  147. gizmo.eScal = true;
  148. GizmoVisible = true;
  149. }
  150. else if (type == GizmoType.None)
  151. {
  152. gizmo.eAxis = false;
  153. gizmo.eRotate = false;
  154. gizmo.eScal = false;
  155. GizmoVisible = false;
  156. }
  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. reInitDrag = dragType != dragTypeOld;
  189. dragTypeOld = dragType;
  190. transform.position = position();
  191. transform.eulerAngles = rotation();
  192. if (GizmoActive)
  193. {
  194. if (meido != null && IsGizmoDrag)
  195. {
  196. meido.IsStop = true;
  197. isPlaying = false;
  198. }
  199. if (gizmoType != gizmoTypeOld) SetGizmo(gizmoType);
  200. gizmoTypeOld = gizmoType;
  201. }
  202. }
  203. private void OnMouseDown()
  204. {
  205. InitializeDrag();
  206. }
  207. private void OnMouseDrag()
  208. {
  209. if (dragType == DragType.Select) return;
  210. if (reInitDrag)
  211. {
  212. reInitDrag = false;
  213. InitializeDrag();
  214. }
  215. if (mousePos != Input.mousePosition) Drag();
  216. }
  217. private void OnEnable()
  218. {
  219. if (position != null)
  220. {
  221. transform.position = position();
  222. transform.eulerAngles = rotation();
  223. }
  224. }
  225. private void OnDestroy()
  226. {
  227. GameObject.Destroy(gizmo);
  228. }
  229. protected void OnDragEvent()
  230. {
  231. DragEvent?.Invoke(null, EventArgs.Empty);
  232. }
  233. }
  234. }