DragPointGeneral.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. internal abstract class DragPointGeneral : DragPoint
  7. {
  8. public const float smallCube = 0.5f;
  9. private float currentScale;
  10. private bool scaling;
  11. private Quaternion currentRotation;
  12. public float ScaleFactor { get; set; } = 1f;
  13. public bool ConstantScale { get; set; }
  14. public event EventHandler Rotate;
  15. public event EventHandler Scale;
  16. public event EventHandler EndScale;
  17. public event EventHandler Delete;
  18. public event EventHandler Select;
  19. public override void AddGizmo(float scale = 0.35f, GizmoMode mode = GizmoMode.Local)
  20. {
  21. base.AddGizmo(scale, mode);
  22. Gizmo.GizmoDrag += (s, a) =>
  23. {
  24. if (Gizmo.CurrentGizmoType == GizmoType.Rotate) OnRotate();
  25. };
  26. }
  27. protected override void Update()
  28. {
  29. base.Update();
  30. if (ConstantScale)
  31. {
  32. float distance = Vector3.Distance(camera.transform.position, transform.position);
  33. transform.localScale = Vector3.one * (0.4f * BaseScale.x * DragPointScale * distance);
  34. }
  35. }
  36. protected override void UpdateDragType()
  37. {
  38. bool shift = Utility.GetModKey(Utility.ModKey.Shift);
  39. if (Input.GetKey(KeyCode.A))
  40. {
  41. CurrentDragType = DragType.Select;
  42. }
  43. else if (Input.GetKey(KeyCode.D))
  44. {
  45. CurrentDragType = DragType.Delete;
  46. }
  47. else if (Input.GetKey(KeyCode.Z))
  48. {
  49. if (Utility.GetModKey(Utility.ModKey.Control)) CurrentDragType = DragType.MoveY;
  50. else CurrentDragType = shift ? DragType.RotY : DragType.MoveXZ;
  51. }
  52. else if (Input.GetKey(KeyCode.X))
  53. {
  54. CurrentDragType = shift ? DragType.RotLocalY : DragType.RotLocalXZ;
  55. }
  56. else if (Input.GetKey(KeyCode.C))
  57. {
  58. CurrentDragType = DragType.Scale;
  59. }
  60. else
  61. {
  62. CurrentDragType = DragType.None;
  63. }
  64. }
  65. protected override void OnMouseDown()
  66. {
  67. if (CurrentDragType == DragType.Delete)
  68. {
  69. OnDelete();
  70. return;
  71. }
  72. if (CurrentDragType == DragType.Select)
  73. {
  74. OnSelect();
  75. return;
  76. }
  77. base.OnMouseDown();
  78. currentScale = this.MyObject.localScale.x;
  79. currentRotation = this.MyObject.rotation;
  80. }
  81. protected override void OnDoubleClick()
  82. {
  83. if (CurrentDragType == DragType.Scale)
  84. {
  85. this.MyObject.localScale = Vector3.one;
  86. OnScale();
  87. OnEndScale();
  88. }
  89. if (CurrentDragType == DragType.RotLocalY || CurrentDragType == DragType.RotLocalXZ)
  90. {
  91. this.MyObject.rotation = Quaternion.identity;
  92. OnRotate();
  93. }
  94. }
  95. protected override void OnMouseUp()
  96. {
  97. base.OnMouseUp();
  98. if (scaling)
  99. {
  100. scaling = false;
  101. OnScale();
  102. OnEndScale();
  103. }
  104. }
  105. protected override void Drag()
  106. {
  107. if (CurrentDragType == DragType.Select || CurrentDragType == DragType.Delete) return;
  108. Vector3 cursorPosition = CursorPosition();
  109. Vector3 mouseDelta = MouseDelta();
  110. if (CurrentDragType == DragType.MoveXZ)
  111. {
  112. MyObject.position = new Vector3(cursorPosition.x, MyObject.position.y, cursorPosition.z);
  113. }
  114. if (CurrentDragType == DragType.MoveY)
  115. {
  116. MyObject.position = new Vector3(
  117. MyObject.position.x, cursorPosition.y, MyObject.position.z
  118. );
  119. }
  120. if (CurrentDragType == DragType.RotY)
  121. {
  122. MyObject.rotation = currentRotation;
  123. MyObject.Rotate(Vector3.up, -mouseDelta.x / 3f, Space.World);
  124. OnRotate();
  125. }
  126. if (CurrentDragType == DragType.RotLocalXZ)
  127. {
  128. MyObject.rotation = currentRotation;
  129. MyObject.Rotate(Vector3.forward, mouseDelta.x / 6f, Space.World);
  130. MyObject.Rotate(Vector3.right, -mouseDelta.y / 4f, Space.World);
  131. OnRotate();
  132. }
  133. if (CurrentDragType == DragType.RotLocalY)
  134. {
  135. MyObject.rotation = currentRotation;
  136. MyObject.Rotate(Vector3.up * -mouseDelta.x / 2.2f);
  137. OnRotate();
  138. }
  139. if (CurrentDragType == DragType.Scale)
  140. {
  141. scaling = true;
  142. float scale = currentScale + (mouseDelta.y / 200f) * ScaleFactor;
  143. if (scale < 0.1f) scale = 0.1f;
  144. MyObject.localScale = new Vector3(scale, scale, scale);
  145. OnScale();
  146. }
  147. }
  148. protected virtual void OnEndScale() => OnEvent(EndScale);
  149. protected virtual void OnScale() => OnEvent(Scale);
  150. protected virtual void OnRotate() => OnEvent(Rotate);
  151. protected virtual void OnSelect() => OnEvent(Select);
  152. protected virtual void OnDelete() => OnEvent(Delete);
  153. private void OnEvent(EventHandler handler) => handler?.Invoke(this, EventArgs.Empty);
  154. }
  155. }