Module_TooltipMenu.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SC.XR.Unity.Module_Tooltip;
  5. using SC.XR.Unity.Module_InputSystem;
  6. using static API_GSXR_Module_InputSystem_Head;
  7. using static API_GSXR_Module_InputSystem_BT3Dof;
  8. using static API_GSXR_Module_InputSystem_GGT26Dof;
  9. using static API_GSXR_Module_InputSystem_KS;
  10. using TMPro;
  11. using UnityEngine.EventSystems;
  12. public class Module_TooltipMenu : MonoBehaviour
  13. {
  14. [SerializeField]
  15. private GameObject _tooltip;
  16. [SerializeField]
  17. private GameObject buttons;
  18. [SerializeField]
  19. private GameObject button_AB;
  20. [SerializeField]
  21. private GameObject textBoard;
  22. [SerializeField]
  23. private TextMeshPro text_ButtonA;
  24. [SerializeField]
  25. private TextMeshPro text_ButtonB;
  26. [Header("Exception Objects")]
  27. [Tooltip("A list of gameobjects can't be selected when locating the point of tooltip.")]
  28. [SerializeField]
  29. private GameObject[] exceptionObjects = new GameObject[] { };
  30. [Header("BoxCollider of ButtonA&B")]
  31. [Tooltip("Don't change these.")]
  32. [SerializeField]
  33. private BoxCollider[] boxColliders = new BoxCollider[] { };
  34. [Header("Text of Tooltip")]
  35. [Tooltip("The texts of each tooltip.")]
  36. [SerializeField]
  37. private string[] tooltipText = new string[]
  38. {
  39. "TooltipTest",
  40. "Hello",
  41. "OK"
  42. };
  43. private GameObject tooltip;
  44. private Modules_TooltipLineRenderer tooltipLineRenderer;
  45. private InputDevicePartType inputDevicePartType;
  46. private bool onLong = false;
  47. private bool selectState = false;
  48. private bool nextState = false;
  49. private void Start()
  50. {
  51. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateRegister(EnterDown);
  52. API_GSXR_Module_InputSystem.GSXR_KeyUpDelegateRegister(EnterUp);
  53. }
  54. private void Update()
  55. {
  56. if(onLong)
  57. {
  58. OnLong();
  59. }
  60. }
  61. private void EnterDown(InputKeyCode keyCode, InputDevicePartBase part)
  62. {
  63. if (selectState&&keyCode == InputKeyCode.Enter && !onLong)
  64. {
  65. inputDevicePartType = part.PartType;
  66. onLong = true;
  67. }
  68. }
  69. private void EnterUp(InputKeyCode keyCode, InputDevicePartBase part)
  70. {
  71. if (selectState&&keyCode == InputKeyCode.Enter && part.PartType == inputDevicePartType)
  72. {
  73. onLong = false;
  74. }
  75. }
  76. private void OnLong()
  77. {
  78. if (selectState)
  79. {
  80. DefaultCursor cursor;
  81. RaycastResult raycastResult;
  82. switch (inputDevicePartType)
  83. {
  84. //忽略GC类型
  85. case InputDevicePartType.Head:
  86. cursor = GSXR_Get_HeadCursor;
  87. raycastResult = GSXR_HeadHitInfo;
  88. break;
  89. //case InputDevicePartType.GCOne:
  90. // break;
  91. //case InputDevicePartType.GCTwo:
  92. // break;
  93. case InputDevicePartType.KSLeft:
  94. cursor = GSXR_Get_KSCursor(GCType.Left);
  95. raycastResult = GSXR_KSHitInfo(GCType.Left);
  96. break;
  97. case InputDevicePartType.KSRight:
  98. cursor = GSXR_Get_KSCursor(GCType.Right);
  99. raycastResult = GSXR_KSHitInfo(GCType.Right);
  100. break;
  101. case InputDevicePartType.HandLeft:
  102. cursor = GSXR_Get_GGTCursor(GGestureType.Left);
  103. raycastResult = GSXR_GGTHitInfo(GGestureType.Left);
  104. break;
  105. case InputDevicePartType.HandRight:
  106. cursor = GSXR_Get_GGTCursor(GGestureType.Right);
  107. raycastResult = GSXR_GGTHitInfo(GGestureType.Right);
  108. break;
  109. default:
  110. cursor = null;
  111. raycastResult = new RaycastResult();
  112. break;
  113. }
  114. try
  115. {
  116. if (raycastResult.gameObject != exceptionObjects[0] && raycastResult.gameObject != exceptionObjects[1])
  117. {
  118. //Debug.Log(cursor.transform.position);
  119. //Debug.Log(raycastResult.gameObject.name);
  120. tooltip.transform.SetParent(raycastResult.gameObject.transform);
  121. Vector3 location = cursor.transform.position;
  122. tooltipLineRenderer.API_SetTargetPosition(location, true);
  123. location.y += 0.1f;
  124. tooltipLineRenderer.API_SetLablePosition(location, true);
  125. }
  126. }
  127. catch// (Exception e)
  128. {
  129. //Debug.Log(e.Message);
  130. }
  131. }
  132. }
  133. private void OnDestroy()
  134. {
  135. if (selectState || nextState)
  136. {
  137. Destroy(tooltip);
  138. }
  139. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateUnRegister(EnterDown);
  140. API_GSXR_Module_InputSystem.GSXR_KeyUpDelegateUnRegister(EnterUp);
  141. }
  142. public void AddTooltip1()
  143. {
  144. AddTooltip(1);
  145. }
  146. public void AddTooltip2()
  147. {
  148. AddTooltip(2);
  149. }
  150. public void AddTooltip3()
  151. {
  152. AddTooltip(3);
  153. }
  154. private void AddTooltip(int number)
  155. {
  156. Vector3 locate = transform.position;
  157. locate.y += 0.2f;
  158. Quaternion rotation = transform.rotation;
  159. tooltip = Instantiate(_tooltip, locate, rotation);
  160. Modules_TooltipUI modules_TooltipUI;
  161. modules_TooltipUI = tooltip.GetComponent<Modules_TooltipUI>();
  162. tooltipLineRenderer = tooltip.GetComponent<Modules_TooltipLineRenderer>();
  163. modules_TooltipUI.API_SetLableText(tooltipText[number - 1]);
  164. buttons.SetActive(false);
  165. button_AB.SetActive(true);
  166. textBoard.SetActive(true);
  167. text_ButtonA.text = "Cancel";
  168. selectState = true;
  169. }
  170. public void DoCancel()
  171. {
  172. if (selectState)
  173. {
  174. Destroy(tooltip);
  175. }
  176. StartCoroutine(ToDestory());
  177. }
  178. public void DoNext()
  179. {
  180. if (nextState)
  181. {
  182. tooltip.GetComponent<Module_TooltipMove>().MoveLableDisable();
  183. nextState = false;
  184. StartCoroutine(ToDestory());
  185. }
  186. else
  187. {
  188. text_ButtonB.text = "OK";
  189. textBoard.GetComponentInChildren<TextMeshPro>().text = "Please move the lable to an ideal location.";
  190. selectState = false;
  191. tooltip.GetComponent<Module_TooltipMove>().MoveLableEnable();
  192. nextState = true;
  193. }
  194. }
  195. private IEnumerator ToDestory()
  196. {
  197. boxColliders[0].enabled = false;
  198. boxColliders[1].enabled = false;
  199. yield return new WaitForSeconds(0.1f);
  200. Destroy(gameObject);
  201. }
  202. }