TeleportService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. using Nxr.Internal;
  16. namespace NXR.Samples
  17. {
  18. [RequireComponent(typeof(Collider))]
  19. public class TeleportService : MonoBehaviour, INxrGazeResponder
  20. {
  21. private Vector3 startingPosition;
  22. private bool gazeAt = false;
  23. void Start()
  24. {
  25. startingPosition = transform.localPosition;
  26. SetGazedAt(false);
  27. NibiruService.OnCameraIdle += CameraIdle;
  28. NibiruService.OnCameraBusy += CameraBusy;
  29. }
  30. public void SetGazedAt(bool gazedAt)
  31. {
  32. gazeAt = gazedAt;
  33. GetComponent<Renderer>().material.color = gazedAt ? Color.green : Color.white;
  34. }
  35. public bool isGazedAt()
  36. {
  37. return gazeAt;
  38. }
  39. #region INxrGazeResponder implementation
  40. /// Called when the user is looking on a GameObject with this script,
  41. /// as long as it is set to an appropriate layer (see NxrGaze).
  42. public void OnGazeEnter()
  43. {
  44. SetGazedAt(true);
  45. }
  46. /// Called when the user stops looking on the GameObject, after OnGazeEnter
  47. /// was already called.
  48. public void OnGazeExit()
  49. {
  50. SetGazedAt(false);
  51. if (gameObject.name.Equals("ButtonStartRec"))
  52. {
  53. Transform btnTransform = gameObject.transform.Find("ApiButtonText");
  54. LocalizationText localizationText = btnTransform.gameObject.GetComponent<LocalizationText>();
  55. localizationText.UpdateKey("voice_say_content_2");
  56. }
  57. }
  58. private bool cursorEnabled = false;
  59. private DISPLAY_MODE curMode = DISPLAY_MODE.MODE_3D;
  60. public void OnPointerDown()
  61. {
  62. NibiruService nibiruService = NxrViewer.Instance.GetNibiruService();
  63. Debug.Log("OnPointerDown : "+gameObject.name);
  64. // start
  65. GameObject apiObj = GameObject.Find("apiRoot");
  66. if (nibiruService != null && apiObj != null && apiObj.transform.Find("ButtonStartRec") != null)
  67. {
  68. GameObject startRecObj = apiObj.transform.Find("ButtonStartRec").gameObject;
  69. if (startRecObj == null) return;
  70. bool isActive = startRecObj.activeSelf;
  71. if (isActive && startRecObj.GetComponent<TeleportService>().isGazedAt())
  72. {
  73. nibiruService.StartVoiceRecording();
  74. }
  75. }
  76. }
  77. public void OnPointerUp()
  78. {
  79. NibiruService nibiruService = NxrViewer.Instance.GetNibiruService();
  80. Debug.Log("OnPointerUp : " + gameObject.name);
  81. // stop
  82. GameObject apiObj = GameObject.Find("apiRoot");
  83. if (nibiruService != null && apiObj != null && apiObj.transform.Find("ButtonStartRec") != null)
  84. {
  85. GameObject startRecObj = apiObj.transform.Find("ButtonStartRec").gameObject;
  86. if (startRecObj == null) return;
  87. bool isActive = startRecObj.activeSelf;
  88. if (isActive && startRecObj.GetComponent<TeleportService>().isGazedAt())
  89. {
  90. nibiruService.StopVoiceRecording();
  91. }
  92. }
  93. }
  94. public void OnGazeTrigger()
  95. {
  96. NibiruService nibiruService = NxrViewer.Instance.GetNibiruService();
  97. // if (nibiruService != null && gameObject.name.Equals("CubeTouchCursor"))
  98. // {
  99. // cursorEnabled = !cursorEnabled;
  100. // nibiruService.SetEnableTouchCursor(cursorEnabled);
  101. // }
  102. // else if (nibiruService != null && gameObject.name.Equals("CubeDisplayMode"))
  103. // {
  104. // curMode = curMode == DISPLAY_MODE.MODE_3D ? DISPLAY_MODE.MODE_2D : DISPLAY_MODE.MODE_3D;
  105. // nibiruService.SetDisplayMode(curMode);
  106. // }
  107. if (nibiruService != null && gameObject.name.Equals("CubeBrightnessUp"))
  108. {
  109. nibiruService.SetBrightnessValue(nibiruService.GetBrightnessValue() + 1);
  110. }
  111. else if (nibiruService != null && gameObject.name.Equals("CubeBrightnessDown"))
  112. {
  113. nibiruService.SetBrightnessValue(nibiruService.GetBrightnessValue() - 1);
  114. }
  115. // else if (gameObject.name.Equals("CubeLanguageCN"))
  116. // {
  117. // NxrViewer.Instance.VoiceLanguage = VOICE_LANGUAGE.CHINESE;
  118. //
  119. // gameObject.GetComponent<Renderer>().material.color = Color.green;
  120. //
  121. // GameObject cnObj = GameObject.Find("CubeLanguageEN");
  122. // if (cnObj != null)
  123. // {
  124. // cnObj.GetComponent<Renderer>().material.color = Color.white;
  125. // }
  126. // // resume
  127. // Transform rootTransform = GameObject.Find("UIRoot").transform;
  128. // rootTransform.Find("CubeNextDemo").transform.gameObject.SetActive(true);
  129. //
  130. // // refresh
  131. // UpdateTextContent(LocalizationManager.chinese);
  132. //
  133. // LocalizationManager.GetInstance.ChangeLanguage(LocalizationManager.chinese);
  134. // }
  135. // else if (gameObject.name.Equals("CubeLanguageEN"))
  136. // {
  137. // NxrViewer.Instance.VoiceLanguage = VOICE_LANGUAGE.ENGLISH;
  138. // gameObject.GetComponent<Renderer>().material.color = Color.green;
  139. // GameObject enObj = GameObject.Find("CubeLanguageCN");
  140. // if (enObj != null)
  141. // {
  142. // enObj.GetComponent<Renderer>().material.color = Color.white;
  143. // }
  144. //
  145. // if (GetPageNumber() == 2)
  146. // {
  147. // Transform rootTransform = GameObject.Find("UIRoot").transform;
  148. // // dismiss
  149. // rootTransform.Find("CubeNextDemo").transform.gameObject.SetActive(false);
  150. // }
  151. //
  152. // // refresh
  153. // UpdateTextContent(LocalizationManager.english);
  154. //
  155. // LocalizationManager.GetInstance.ChangeLanguage(LocalizationManager.english);
  156. // }
  157. else if (nibiruService != null && gameObject.name.Equals("CameraStart"))
  158. {
  159. // camera preview
  160. hasTrigger = false;
  161. nibiruService.GetCameraStatus();
  162. }
  163. else if (nibiruService != null && gameObject.name.Equals("CameraPause"))
  164. {
  165. nibiruService.StopCamereaPreView();
  166. }
  167. // else if (gameObject.name.Equals("CubeNextDemo"))
  168. // {
  169. // int page = GetPageNumber();
  170. // if (page == 0 || page == 2)
  171. // {
  172. // UpdateVoiceApiDemoPage(true);
  173. // // dismiss text
  174. // Transform rootTransform = GameObject.Find("UIRoot").transform;
  175. // rootTransform.Find("TextControlTip").gameObject.SetActive(false);
  176. // rootTransform.Find("TextTip").gameObject.SetActive(false);
  177. // // dismiss progressbar
  178. // Transform parentTransform = GameObject.Find("VolumeRoot").transform;
  179. // parentTransform.Find("VolumeIcon").gameObject.SetActive(false);
  180. // parentTransform.Find("VolumeProgressBG").gameObject.SetActive(false);
  181. // parentTransform.Find("VolumeProgressValue").gameObject.SetActive(false);
  182. // // change en/cn
  183. // rootTransform.Find("CubeLanguageEN").transform.gameObject.SetActive(true);
  184. // rootTransform.Find("CubeLanguageCN").transform.gameObject.SetActive(true);
  185. // return;
  186. // }
  187. // else if (page == 1)
  188. // {
  189. // UpdateVoiceApiDemoPage(false);
  190. // // resume text
  191. // Transform rootTransform = GameObject.Find("UIRoot").transform;
  192. // rootTransform.Find("TextControlTip").gameObject.SetActive(true);
  193. // rootTransform.Find("TextTip").gameObject.SetActive(true);
  194. // }
  195. //
  196. // // next page
  197. // LocalizationText localizationText = GameObject.Find("TextControlTip").GetComponent<LocalizationText>();
  198. // string newKey = localizationText.key == "voice_say_content" ? "voice_say_content_volume" : "voice_say_content";
  199. // localizationText.UpdateKey(newKey);
  200. //
  201. //
  202. // //Debug.Log("-------------------:www---------------" + newKey);
  203. //
  204. // // back
  205. // LocalizationText localizationTextNxt = GameObject.Find("TextNextDemo").GetComponent<LocalizationText>();
  206. // newKey = localizationTextNxt.key == "next_demo_text" ? "last_demo_text" : "next_demo_text";
  207. // localizationTextNxt.UpdateKey(newKey);
  208. //
  209. // LocalizationText localizationTextTip = GameObject.Find("TextTip").GetComponent<LocalizationText>();
  210. // newKey = localizationTextTip.key == "voice_tip_content" ? "voice_tip_content_v" : "voice_tip_content";
  211. // localizationTextTip.UpdateKey(newKey);
  212. //
  213. // if (newKey == "voice_tip_content_v")
  214. // {
  215. // Transform rootTransform = GameObject.Find("UIRoot").transform;
  216. //
  217. // // dismiss en button
  218. // rootTransform.Find("CubeLanguageEN").transform.gameObject.SetActive(false);
  219. // rootTransform.Find("CubeLanguageCN").transform.gameObject.SetActive(false);
  220. // //
  221. // // show progressbar
  222. // Transform parentTransform = GameObject.Find("VolumeRoot").transform;
  223. // parentTransform.Find("VolumeIcon").gameObject.SetActive(true);
  224. // parentTransform.Find("VolumeProgressBG").gameObject.SetActive(true);
  225. // parentTransform.Find("VolumeProgressValue").gameObject.SetActive(true);
  226. //
  227. // UpdateVolumeProgress();
  228. // }
  229. // else if (newKey == "voice_tip_content")
  230. // {
  231. // Transform rootTransform = GameObject.Find("UIRoot").transform;
  232. // rootTransform.Find("CubeLanguageEN").transform.gameObject.SetActive(true);
  233. // rootTransform.Find("CubeLanguageCN").transform.gameObject.SetActive(true);
  234. // //
  235. // // dismiss progressbar
  236. // Transform parentTransform = GameObject.Find("VolumeRoot").transform;
  237. // parentTransform.Find("VolumeIcon").gameObject.SetActive(false);
  238. // parentTransform.Find("VolumeProgressBG").gameObject.SetActive(false);
  239. // parentTransform.Find("VolumeProgressValue").gameObject.SetActive(false);
  240. // }
  241. //
  242. // page++;
  243. // }
  244. else if (nibiruService != null && gameObject.name.Equals("CubeStartSensor"))
  245. {
  246. nibiruService.RegisterSensorListener(SENSOR_TYPE.ACCELEROMETER, SENSOR_LOCATION.HMD);
  247. nibiruService.RegisterSensorListener(SENSOR_TYPE.GYROSCOPE, SENSOR_LOCATION.HMD);
  248. nibiruService.RegisterSensorListener(SENSOR_TYPE.MAGNETIC_FIELD, SENSOR_LOCATION.HMD);
  249. }
  250. else if (nibiruService != null && gameObject.name.Equals("CubeStopSensor"))
  251. {
  252. nibiruService.UnRegisterSensorListener();
  253. }
  254. // else if (nibiruService != null && gameObject.name.Equals("ButtonStopRec"))
  255. // {
  256. // nibiruService.StopVoiceRecording();
  257. // }
  258. // else if (gameObject.name.Equals("ButtonStartRec"))
  259. // {
  260. // //nibiruService.StartVoiceRecording();
  261. // Transform btnTransform = gameObject.transform.Find("ApiButtonText");
  262. // LocalizationText localizationText = btnTransform.gameObject.GetComponent<LocalizationText>();
  263. // localizationText.UpdateKey("voice_say_content_3");
  264. // }
  265. }
  266. private void UpdateVoiceApiDemoPage(bool show)
  267. {
  268. Transform parentTransform = GameObject.Find("apiRoot").transform;
  269. parentTransform.Find("ButtonStartRec").gameObject.SetActive(show);
  270. parentTransform.Find("ApiTextDesc").gameObject.SetActive(show);
  271. }
  272. public int GetPageNumber()
  273. {
  274. GameObject textControllObj = GameObject.Find("TextControlTip");
  275. if (textControllObj != null)
  276. {
  277. LocalizationText localizationText = textControllObj.GetComponent<LocalizationText>();
  278. return localizationText.key == "voice_say_content" ? 0 : 2;
  279. }
  280. else
  281. {
  282. return 1;
  283. }
  284. }
  285. private void UpdateVolumeProgress()
  286. {
  287. if (NxrViewer.Instance.GetNibiruService() == null) return;
  288. int cur = NxrViewer.Instance.GetNibiruService().GetVolumeValue();
  289. int max = NxrViewer.Instance.GetNibiruService().GetMaxVolume();
  290. Transform parentTransform = GameObject.Find("VolumeRoot").transform;
  291. Transform progressTransform = parentTransform.Find("VolumeProgressValue");
  292. float progress = cur * 1.0f / max;
  293. // scaleX=1.98,position.x=0
  294. float newPX = -1.98f * (1.0f - progress) / 2;
  295. progressTransform.localScale = new Vector3(1.98f * progress, progressTransform.localScale.y, progressTransform.localScale.z);
  296. progressTransform.localPosition = new Vector3(newPX, progressTransform.localPosition.y, progressTransform.localPosition.z);
  297. }
  298. bool hasTrigger = false;
  299. void CameraIdle()
  300. {
  301. if (hasTrigger)
  302. {
  303. return;
  304. }
  305. Debug.Log(".CameraIdle");
  306. NibiruService nibiruService = NxrViewer.Instance.GetNibiruService();
  307. if (nibiruService != null)
  308. {
  309. nibiruService.StartCameraPreView(true);
  310. }
  311. hasTrigger = true;
  312. }
  313. void CameraBusy()
  314. {
  315. Debug.Log(".CameraBusy");
  316. }
  317. void UpdateTextContent(string language)
  318. {
  319. GameObject textControlTipObj = GameObject.Find("TextControlTip");
  320. if (textControlTipObj != null) textControlTipObj.GetComponent<LocalizationText>().refresh(language);
  321. GameObject textVoiceStausObj = GameObject.Find("TextVoiceStaus");
  322. if (textVoiceStausObj != null) textVoiceStausObj.GetComponent<LocalizationText>().refresh(language);
  323. GameObject textDecibelValueObj = GameObject.Find("TextDecibelValue");
  324. if (textDecibelValueObj != null) textDecibelValueObj.GetComponent<LocalizationText>().refresh(language);
  325. GameObject textTipObj = GameObject.Find("TextTip");
  326. if (textTipObj != null) textTipObj.GetComponent<LocalizationText>().refresh(language);
  327. GameObject apiBtnTextObj = GameObject.Find("ApiButtonText");
  328. if (apiBtnTextObj != null) apiBtnTextObj.GetComponent<LocalizationText>().refresh(language);
  329. GameObject apiTextDescObj = GameObject.Find("ApiTextDesc");
  330. if (apiTextDescObj != null) apiTextDescObj.GetComponent<LocalizationText>().refresh(language);
  331. GameObject nextObject = GameObject.Find("TextNextDemo");
  332. if (nextObject != null) nextObject.GetComponent<LocalizationText>().refresh(language);
  333. }
  334. private void OnDestroy()
  335. {
  336. NibiruService.OnCameraIdle -= CameraIdle;
  337. NibiruService.OnCameraBusy -= CameraBusy;
  338. Debug.Log("TeleportService.OnDestroy");
  339. }
  340. public void OnUpdateIntersectionPosition(Vector3 position)
  341. {
  342. // Debug.Log("TeleportService.OnUpdateIntersectionPosition=" + position.ToString());
  343. }
  344. #endregion
  345. }
  346. }