TimerController.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using UnityEngine;
  22. using UnityEngine.UI;
  23. using Meta.WitAi;
  24. namespace Oculus.Voice.Demo.BuiltInDemo
  25. {
  26. /// <summary>
  27. /// Represents a countdown timer.
  28. /// </summary>
  29. public class TimerController : MonoBehaviour
  30. {
  31. private double _time = 0; // [sec] current time of the countdown timer.
  32. private bool _timerExist = false;
  33. private bool _timerRunning = false;
  34. [Tooltip("The UI text element to show app messages.")]
  35. public Text logText;
  36. [Tooltip("The timer ring sound.")] public AudioClip[] timesUpSounds;
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. if (_timerExist && _timerRunning)
  41. {
  42. _time -= Time.deltaTime;
  43. if (_time < 0)
  44. {
  45. // Raise a ring.
  46. OnElapsedTime();
  47. }
  48. }
  49. }
  50. private void Log(string msg)
  51. {
  52. Debug.Log(msg);
  53. logText.text = msg;
  54. }
  55. /// <summary>
  56. /// Buzzes and resets the timer.
  57. /// </summary>
  58. private void OnElapsedTime()
  59. {
  60. _time = 0;
  61. _timerRunning = false;
  62. _timerExist = false;
  63. Log("Your timer is complete.");
  64. AudioClip timesUpSfx = timesUpSounds[UnityEngine.Random.Range(0, timesUpSounds.Length)];
  65. AudioSource.PlayClipAtPoint(timesUpSfx, Vector3.zero);
  66. }
  67. /// <summary>
  68. /// Deletes the timer. It corresponds to the wit intent "wit$delete_timer"
  69. /// </summary>
  70. public void DeleteTimer()
  71. {
  72. if (!_timerExist)
  73. {
  74. Log("Error: There is no timer to delete.");
  75. return;
  76. }
  77. _timerExist = false;
  78. _time = 0;
  79. _timerRunning = false;
  80. Log("Timer deleted.");
  81. }
  82. /// <summary>
  83. /// Creates a timer. It corresponds to the wit intent "wit$create_timer"
  84. /// </summary>
  85. /// <param name="entityValues">countdown in minutes.</param>
  86. public void CreateTimer(string[] entityValues)
  87. {
  88. if (_timerExist)
  89. {
  90. VLog.W("A timer already exist.");
  91. return;
  92. }
  93. if (ParseTime(entityValues, out _time))
  94. {
  95. _timerExist = true;
  96. _timerRunning = true;
  97. Log($"Countdown Timer is set for {entityValues[0]} {entityValues[1]}(s).");
  98. }
  99. }
  100. /// <summary>
  101. /// Displays current timer value. It corresponds to "wit$get_timer".
  102. /// </summary>
  103. public void GetTimerIntent()
  104. {
  105. // Show the remaining time of the countdown timer.
  106. var msg = GetFormattedTimeFromSeconds();
  107. //Log(msg);
  108. }
  109. /// <summary>
  110. /// Pauses the timer. It corresponds to the wit intent "wit$pause_timer"
  111. /// </summary>
  112. public void PauseTimer()
  113. {
  114. _timerRunning = false;
  115. Log("Timer paused.");
  116. }
  117. /// <summary>
  118. /// It corresponds to the wit intent "wit$resume_timer"
  119. /// </summary>
  120. public void ResumeTimer()
  121. {
  122. _timerRunning = true;
  123. Log("Timer resumed.");
  124. }
  125. /// <summary>
  126. /// Subtracts time from the timer. It corresponds to the wit intent "wit$subtract_time_timer".
  127. /// </summary>
  128. /// <param name="entityValues"></param>
  129. public void SubtractTimeTimer(string[] entityValues)
  130. {
  131. if (!_timerExist)
  132. {
  133. Log("Error: No Timer is created.");
  134. return;
  135. }
  136. if (ParseTime(entityValues, out var time))
  137. {
  138. var msg = $"{entityValues[0]} {entityValues[1]}(s) were subtracted from the timer.";
  139. _time -= time;
  140. if (_time < 0)
  141. {
  142. _time = 0;
  143. Log(msg);
  144. return;
  145. }
  146. Log(msg);
  147. }
  148. else
  149. {
  150. Log("Error in Subtract_time_timer(): Could not parse the wit reply.");
  151. }
  152. }
  153. /// <summary>
  154. /// Adds time to the timer. It corresponds to the wit intent "wit$add_time_timer".
  155. /// </summary>
  156. /// <param name="entityValues"></param>
  157. public void AddTimeToTimer(string[] entityValues)
  158. {
  159. if (!_timerExist)
  160. {
  161. Log("Error: No Timer is created.");
  162. return;
  163. }
  164. if (ParseTime(entityValues, out var time))
  165. {
  166. _time += time;
  167. var msg = $"{entityValues[0]} {entityValues[1]}(s) were added to the timer.";
  168. Log(msg);
  169. }
  170. else
  171. {
  172. Log("Error in AddTimeToTimer(): Could not parse with reply.");
  173. }
  174. }
  175. /// <summary>
  176. /// Returns the remaining time (in sec) of the countdown timer.
  177. /// </summary>
  178. /// <returns></returns>
  179. public double GetRemainingTime()
  180. {
  181. return _time;
  182. }
  183. /// <summary>
  184. /// Returns time in the format of min:sec.
  185. /// </summary>
  186. /// <returns></returns>
  187. public string GetFormattedTimeFromSeconds()
  188. {
  189. if (_time >= TimeSpan.MaxValue.TotalSeconds)
  190. {
  191. _time = TimeSpan.MaxValue.TotalSeconds - 1;
  192. Log("Error: Hit max time");
  193. }
  194. TimeSpan span = TimeSpan.FromSeconds(_time);
  195. return $"{Math.Floor(span.TotalHours)}:{span.Minutes:00}:{span.Seconds:00}.{Math.Floor(span.Milliseconds/100f)}";
  196. }
  197. /// <summary>
  198. /// Parses entity values to get a resulting time value in seconds
  199. /// </summary>
  200. /// <param name="entityValues">The entity value results from a Response Handler</param>
  201. /// <param name="time">The parsed time</param>
  202. /// <returns>The parsed time in seconds or the current value of _time</returns>
  203. /// <exception cref="ArgumentException"></exception>
  204. private bool ParseTime(string[] entityValues, out double time)
  205. {
  206. time = _time;
  207. if (entityValues.Length > 0 && double.TryParse(entityValues[0], out time))
  208. {
  209. if (entityValues.Length < 2)
  210. {
  211. throw new ArgumentException("Entities being parsed must include time value and unit.");
  212. }
  213. // If entity was not included in the result it will be empty, but the array will still be size 2
  214. if (!string.IsNullOrEmpty(entityValues[1]))
  215. {
  216. switch (entityValues[1])
  217. {
  218. case "minute":
  219. time *= 60;
  220. break;
  221. case "hour":
  222. time *= 60 * 60;
  223. break;
  224. }
  225. }
  226. return true;
  227. }
  228. return false;
  229. }
  230. }
  231. }