DragDogu.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. using static CustomGizmo;
  6. public class DragDogu : BaseDrag
  7. {
  8. private GameObject dogu;
  9. private Vector3 off;
  10. private Vector3 off2;
  11. private Vector3 mousePos2;
  12. private float doguScale;
  13. private Vector3 doguRot;
  14. public event EventHandler Delete;
  15. public bool DeleteMe { get; private set; }
  16. public void Initialize(GameObject dogu)
  17. {
  18. this.dogu = dogu;
  19. base.InitializeDragPoint(() => this.dogu.transform.position, () => Vector3.zero);
  20. InitializeGizmo(this.dogu.transform, 1f, GizmoMode.World);
  21. }
  22. protected override void GetDragType()
  23. {
  24. bool holdShift = Utility.GetModKey(Utility.ModKey.Shift);
  25. if (Input.GetKey(KeyCode.D))
  26. {
  27. // Actually delete
  28. CurrentDragType = DragType.Select;
  29. CurrentGizmoType = GizmoType.None;
  30. }
  31. else if (Input.GetKey(KeyCode.Z))
  32. {
  33. if (Utility.GetModKey(Utility.ModKey.Control)) CurrentDragType = DragType.MoveY;
  34. else CurrentDragType = holdShift ? DragType.RotY : DragType.MoveXZ;
  35. CurrentGizmoType = GizmoType.None;
  36. }
  37. else if (Input.GetKey(KeyCode.X))
  38. {
  39. CurrentDragType = holdShift ? DragType.RotLocalY : DragType.RotLocalXZ;
  40. CurrentGizmoType = GizmoType.Rotate;
  41. }
  42. else if (Input.GetKey(KeyCode.C))
  43. {
  44. CurrentDragType = DragType.Scale;
  45. CurrentGizmoType = GizmoType.None;
  46. }
  47. else
  48. {
  49. CurrentDragType = DragType.None;
  50. CurrentGizmoType = GizmoType.None;
  51. }
  52. }
  53. protected override void InitializeDrag()
  54. {
  55. if (CurrentDragType == DragType.Select)
  56. {
  57. this.DeleteMe = true;
  58. this.Delete?.Invoke(this, EventArgs.Empty);
  59. return;
  60. }
  61. base.InitializeDrag();
  62. doguScale = dogu.transform.localScale.x;
  63. doguRot = dogu.transform.localEulerAngles;
  64. off = transform.position - Camera.main.ScreenToWorldPoint(
  65. new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)
  66. );
  67. off2 = new Vector3(
  68. transform.position.x - dogu.transform.position.x,
  69. transform.position.y - dogu.transform.position.y,
  70. transform.position.z - dogu.transform.position.z
  71. );
  72. }
  73. protected override void DoubleClick()
  74. {
  75. if (CurrentDragType == DragType.Scale) dogu.transform.localScale = new Vector3(1f, 1f, 1f);
  76. if (CurrentDragType == DragType.RotLocalY || CurrentDragType == DragType.RotLocalXZ)
  77. dogu.transform.rotation = new Quaternion(0f, 0f, 0f, 1f);
  78. }
  79. protected override void Drag()
  80. {
  81. Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, worldPoint.z)) + off - off2;
  82. if (CurrentDragType == DragType.MoveXZ)
  83. {
  84. dogu.transform.position = new Vector3(pos.x, dogu.transform.position.y, pos.z);
  85. }
  86. if (CurrentDragType == DragType.MoveY)
  87. {
  88. dogu.transform.position = new Vector3(dogu.transform.position.x, pos.y, dogu.transform.position.z);
  89. }
  90. if (CurrentDragType == DragType.RotY)
  91. {
  92. Vector3 posOther = Input.mousePosition - mousePos;
  93. dogu.transform.eulerAngles =
  94. new Vector3(dogu.transform.eulerAngles.x, doguRot.y - posOther.x / 3f, dogu.transform.eulerAngles.z);
  95. }
  96. if (CurrentDragType == DragType.RotLocalXZ)
  97. {
  98. Vector3 posOther = Input.mousePosition - mousePos;
  99. Transform transform = Camera.main.transform;
  100. Vector3 vector3_3 = transform.TransformDirection(Vector3.right);
  101. Vector3 vector3_4 = transform.TransformDirection(Vector3.forward);
  102. transform.TransformDirection(Vector3.forward);
  103. if (mousePos2 != Input.mousePosition)
  104. {
  105. dogu.transform.localEulerAngles = doguRot;
  106. dogu.transform.RotateAround(dogu.transform.position, new Vector3(vector3_3.x, 0.0f, vector3_3.z), posOther.y / 4f);
  107. dogu.transform.RotateAround(dogu.transform.position, new Vector3(vector3_4.x, 0.0f, vector3_4.z), (-posOther.x / 6.0f));
  108. }
  109. mousePos2 = Input.mousePosition;
  110. }
  111. if (CurrentDragType == DragType.RotLocalY)
  112. {
  113. Vector3 posOther = Input.mousePosition - mousePos;
  114. Transform transform = Camera.main.transform;
  115. Vector3 vector3_3 = transform.TransformDirection(Vector3.right);
  116. transform.TransformDirection(Vector3.forward);
  117. if (mousePos2 != Input.mousePosition)
  118. {
  119. dogu.transform.localEulerAngles = doguRot;
  120. dogu.transform.localRotation = Quaternion.Euler(dogu.transform.localEulerAngles)
  121. * Quaternion.AngleAxis((-posOther.x / 2.2f), Vector3.up);
  122. }
  123. mousePos2 = Input.mousePosition;
  124. }
  125. if (CurrentDragType == DragType.Scale)
  126. {
  127. Vector3 posOther = Input.mousePosition - mousePos;
  128. float scale = doguScale + posOther.y / 200f;
  129. if (scale < 0.1f) scale = 0.1f;
  130. dogu.transform.localScale = new Vector3(scale, scale, scale);
  131. }
  132. }
  133. private void OnDestroy()
  134. {
  135. GameObject.Destroy(this.dogu);
  136. }
  137. }
  138. }