BaseDrag.cs 4.7 KB

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