2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.1 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. #if VIU_STEAMVR_2_0_0_OR_NEWER && UNITY_STANDALONE
  6. using Valve.VR;
  7. #endif
  8. [RequireComponent(typeof(InputField))]
  9. public class OverlayKeyboardSample : MonoBehaviour
  10. , ISelectHandler
  11. , IDeselectHandler
  12. {
  13. public bool minimalMode;
  14. public void OnSelect(BaseEventData eventData)
  15. {
  16. ShowKeyboard(this);
  17. }
  18. public void OnDeselect(BaseEventData eventData)
  19. {
  20. HideKeyboard();
  21. }
  22. #if VIU_STEAMVR && UNITY_STANDALONE
  23. private static OverlayKeyboardSample activeKeyboard;
  24. private static System.Text.StringBuilder strBuilder;
  25. private InputField textEntry;
  26. private string text = "";
  27. protected virtual void Start()
  28. {
  29. textEntry = GetComponent<InputField>();
  30. }
  31. protected virtual void OnDisable()
  32. {
  33. if (activeKeyboard == this)
  34. {
  35. HideKeyboard();
  36. }
  37. }
  38. static OverlayKeyboardSample()
  39. {
  40. #if VIU_STEAMVR_1_2_1_OR_NEWER
  41. SteamVR_Events.System(Valve.VR.EVREventType.VREvent_KeyboardCharInput).AddListener(OnKeyboardCharInput);
  42. SteamVR_Events.System(Valve.VR.EVREventType.VREvent_KeyboardClosed).AddListener(OnKeyboardClosed);
  43. #elif VIU_STEAMVR_1_2_0_OR_NEWER
  44. SteamVR_Events.System("KeyboardCharInput").AddListener(OnKeyboardCharInput);
  45. SteamVR_Events.System("KeyboardClosed").AddListener(OnKeyboardClosed);
  46. #elif VIU_STEAMVR_1_1_1
  47. SteamVR_Utils.Event.Listen("KeyboardCharInput", OnKeyboardCharInputArgs);
  48. SteamVR_Utils.Event.Listen("KeyboardClosed", OnKeyboardClosedArgs);
  49. #endif
  50. }
  51. public static void ShowKeyboard(OverlayKeyboardSample caller)
  52. {
  53. if (activeKeyboard != null)
  54. {
  55. HideKeyboard();
  56. }
  57. if (activeKeyboard == null)
  58. {
  59. var vr = SteamVR.instance;
  60. if (vr != null)
  61. {
  62. caller.text = caller.textEntry.text;
  63. #if VIU_STEAMVR_2_6_0_OR_NEWER
  64. uint flag = 0;
  65. if (caller.minimalMode)
  66. {
  67. flag = (uint)EKeyboardFlags.KeyboardFlag_Minimal;
  68. }
  69. vr.overlay.ShowKeyboard(0, 0, flag, "Description", 256, caller.text, 0);
  70. #else
  71. vr.overlay.ShowKeyboard(0, 0, "Description", 256, caller.text, caller.minimalMode, 0);
  72. #endif
  73. }
  74. activeKeyboard = caller;
  75. }
  76. }
  77. public static void HideKeyboard()
  78. {
  79. if (activeKeyboard != null)
  80. {
  81. var vr = SteamVR.instance;
  82. if (vr != null)
  83. {
  84. vr.overlay.HideKeyboard();
  85. }
  86. }
  87. activeKeyboard = null;
  88. }
  89. #if VIU_STEAMVR_1_1_1
  90. private static void OnKeyboardCharInputArgs(params object[] args) { OnKeyboardCharInput((Valve.VR.VREvent_t)args[0]); }
  91. private static void OnKeyboardClosedArgs(params object[] args) { OnKeyboardClosed((Valve.VR.VREvent_t)args[0]); }
  92. #endif
  93. private static void OnKeyboardCharInput(Valve.VR.VREvent_t arg)
  94. {
  95. if (activeKeyboard == null) { return; }
  96. var keyboard = arg.data.keyboard;
  97. var inputBytes = new byte[]
  98. {
  99. keyboard.cNewInput0,
  100. keyboard.cNewInput1,
  101. keyboard.cNewInput2,
  102. keyboard.cNewInput3,
  103. keyboard.cNewInput4,
  104. keyboard.cNewInput5,
  105. keyboard.cNewInput6,
  106. keyboard.cNewInput7
  107. };
  108. var len = 0;
  109. for (; inputBytes[len] != 0 && len < 7; len++) ;
  110. var input = System.Text.Encoding.UTF8.GetString(inputBytes, 0, len);
  111. if (activeKeyboard.minimalMode)
  112. {
  113. if (input == "\b")
  114. {
  115. if (activeKeyboard.text.Length > 0)
  116. {
  117. activeKeyboard.text = activeKeyboard.text.Substring(0, activeKeyboard.text.Length - 1);
  118. }
  119. }
  120. else if (input == "\x1b")
  121. {
  122. // Close the keyboard
  123. HideKeyboard();
  124. }
  125. else
  126. {
  127. activeKeyboard.text += input;
  128. }
  129. activeKeyboard.textEntry.text = activeKeyboard.text;
  130. }
  131. else
  132. {
  133. var vr = SteamVR.instance;
  134. if (vr != null)
  135. {
  136. if (strBuilder == null) { strBuilder = new System.Text.StringBuilder(1024); }
  137. vr.overlay.GetKeyboardText(strBuilder, 1024);
  138. activeKeyboard.text = strBuilder.ToString();
  139. activeKeyboard.textEntry.text = activeKeyboard.text;
  140. strBuilder.Length = 0;
  141. }
  142. }
  143. }
  144. private static void OnKeyboardClosed(Valve.VR.VREvent_t arg)
  145. {
  146. activeKeyboard = null;
  147. }
  148. #else
  149. protected virtual void Start()
  150. {
  151. Debug.LogWarning("SteamVR plugin not found! install it to enable OverlayKeyboard!");
  152. }
  153. protected virtual void OnDisable() { }
  154. public static void ShowKeyboard(OverlayKeyboardSample caller) { }
  155. public static void HideKeyboard() { }
  156. #endif
  157. }