BaseDrag.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. protected enum DragType
  53. {
  54. None, Select,
  55. MoveXZ, MoveY, RotLocalXZ, RotY, RotLocalY,
  56. Scale
  57. }
  58. public virtual void Initialize(Maid maid, Func<Vector3> position, Func<Vector3> rotation)
  59. {
  60. this.maid = maid;
  61. this.position = position;
  62. this.rotation = rotation;
  63. dragPointRenderer = GetComponent<Renderer>();
  64. dragPointCollider = GetComponent<Collider>();
  65. dragPointRenderer.enabled = true;
  66. isPlaying = maid.GetAnimation().isPlaying;
  67. }
  68. protected void InitializeGizmo(GameObject target, float scale = 0.25f)
  69. {
  70. gizmo = target.gameObject.AddComponent<GizmoRender>();
  71. gizmo.eRotate = true;
  72. gizmo.offsetScale = scale;
  73. gizmo.lineRSelectedThick = 0.25f;
  74. gizmo.Visible = false;
  75. }
  76. protected void InitializeGizmo(Transform target, float scale = 0.25f)
  77. {
  78. InitializeGizmo(target.gameObject, scale);
  79. }
  80. protected virtual void InitializeDrag()
  81. {
  82. worldPoint = Camera.main.WorldToScreenPoint(transform.position);
  83. mousePos = Input.mousePosition;
  84. isPlaying = maid.GetAnimation().isPlaying;
  85. }
  86. protected virtual void DoubleClick() { }
  87. protected abstract void Drag();
  88. protected abstract void GetDragType();
  89. private void OnMouseUp()
  90. {
  91. if ((Time.time - doubleClickStart) < doubleClickSensitivity)
  92. {
  93. doubleClickStart = -1;
  94. DoubleClick();
  95. }
  96. else
  97. {
  98. doubleClickStart = Time.time;
  99. }
  100. }
  101. private void Update()
  102. {
  103. GetDragType();
  104. reInitDrag = dragType != dragTypeOld;
  105. dragTypeOld = dragType;
  106. transform.position = position();
  107. transform.eulerAngles = rotation();
  108. if (gizmo != null)
  109. {
  110. if (GizmoVisible)
  111. {
  112. if (isPlaying && IsGizmoDrag)
  113. {
  114. maid.GetAnimation().Stop();
  115. isPlaying = false;
  116. }
  117. }
  118. }
  119. }
  120. private void OnMouseDown()
  121. {
  122. InitializeDrag();
  123. }
  124. private void OnMouseDrag()
  125. {
  126. if (dragType == DragType.Select) return;
  127. if (reInitDrag)
  128. {
  129. reInitDrag = false;
  130. InitializeDrag();
  131. }
  132. if (mousePos != Input.mousePosition) Drag();
  133. }
  134. private void OnEnable()
  135. {
  136. if (position != null)
  137. {
  138. transform.position = position();
  139. transform.eulerAngles = rotation();
  140. }
  141. }
  142. }
  143. }