BaseDrag.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public abstract class BaseDrag : MonoBehaviour
  6. {
  7. private const float doubleClickSensitivity = 0.3f;
  8. protected const int upperArm = 0;
  9. protected const int foreArm = 1;
  10. protected const int hand = 2;
  11. protected const int upperArmRot = 0;
  12. protected const int handRot = 1;
  13. protected Maid maid;
  14. protected Meido meido;
  15. protected Func<Vector3> position;
  16. protected Func<Vector3> rotation;
  17. protected Renderer dragPointRenderer;
  18. protected Collider dragPointCollider;
  19. protected Vector3 worldPoint;
  20. protected Vector3 mousePos;
  21. protected DragType dragType = DragType.None;
  22. protected DragType dragTypeOld;
  23. protected float doubleClickStart = 0f;
  24. protected bool reInitDrag = false;
  25. protected bool isPlaying;
  26. protected GizmoRender gizmo;
  27. public bool Visible
  28. {
  29. get => dragPointRenderer.enabled;
  30. set => dragPointRenderer.enabled = value;
  31. }
  32. public float DragPointScale
  33. {
  34. get => transform.localScale.x;
  35. set => transform.localScale = new Vector3(value, value, value);
  36. }
  37. public float GizmoScale
  38. {
  39. set
  40. {
  41. if (gizmo != null) gizmo.offsetScale = value;
  42. }
  43. }
  44. public bool GizmoVisible
  45. {
  46. get => gizmo?.Visible ?? false;
  47. set
  48. {
  49. if (gizmo != null) gizmo.Visible = value;
  50. }
  51. }
  52. private static bool IsGizmoDrag => Utility.GetFieldValue<GizmoRender, bool>(null, "is_drag_");
  53. public event EventHandler DragEvent;
  54. protected enum DragType
  55. {
  56. None, Select,
  57. MoveXZ, MoveY, RotLocalXZ, RotY, RotLocalY,
  58. Scale
  59. }
  60. public virtual void Initialize(Meido meido, Func<Vector3> position, Func<Vector3> rotation)
  61. {
  62. this.meido = meido;
  63. this.maid = meido.Maid;
  64. this.position = position;
  65. this.rotation = rotation;
  66. this.dragPointRenderer = GetComponent<Renderer>();
  67. this.dragPointCollider = GetComponent<Collider>();
  68. this.dragPointRenderer.enabled = true;
  69. isPlaying = !meido.IsStop;
  70. }
  71. protected void InitializeGizmo(GameObject target, float scale = 0.25f)
  72. {
  73. gizmo = target.AddComponent<GizmoRender>();
  74. gizmo.eRotate = true;
  75. gizmo.offsetScale = scale;
  76. gizmo.lineRSelectedThick = 0.25f;
  77. GizmoVisible = false;
  78. }
  79. protected void InitializeGizmo(Transform target, float scale = 0.25f)
  80. {
  81. InitializeGizmo(target.gameObject, scale);
  82. }
  83. protected virtual void InitializeDrag()
  84. {
  85. worldPoint = Camera.main.WorldToScreenPoint(transform.position);
  86. mousePos = Input.mousePosition;
  87. isPlaying = !meido.IsStop;
  88. }
  89. protected virtual void DoubleClick() { }
  90. protected abstract void Drag();
  91. protected abstract void GetDragType();
  92. private void OnMouseUp()
  93. {
  94. if ((Time.time - doubleClickStart) < doubleClickSensitivity)
  95. {
  96. doubleClickStart = -1;
  97. DoubleClick();
  98. }
  99. else
  100. {
  101. doubleClickStart = Time.time;
  102. }
  103. }
  104. private void Update()
  105. {
  106. GetDragType();
  107. reInitDrag = dragType != dragTypeOld;
  108. dragTypeOld = dragType;
  109. transform.position = position();
  110. transform.eulerAngles = rotation();
  111. if (gizmo != null)
  112. {
  113. if (GizmoVisible)
  114. {
  115. if (isPlaying && IsGizmoDrag)
  116. {
  117. meido.IsStop = true;
  118. isPlaying = false;
  119. }
  120. }
  121. }
  122. }
  123. private void OnMouseDown()
  124. {
  125. InitializeDrag();
  126. }
  127. private void OnMouseDrag()
  128. {
  129. if (dragType == DragType.Select) return;
  130. if (reInitDrag)
  131. {
  132. reInitDrag = false;
  133. InitializeDrag();
  134. }
  135. if (mousePos != Input.mousePosition) Drag();
  136. }
  137. private void OnEnable()
  138. {
  139. if (position != null)
  140. {
  141. transform.position = position();
  142. transform.eulerAngles = rotation();
  143. }
  144. }
  145. private void OnDestroy()
  146. {
  147. GameObject.Destroy(gizmo);
  148. }
  149. protected void OnDragEvent()
  150. {
  151. DragEvent?.Invoke(null, EventArgs.Empty);
  152. }
  153. }
  154. }