6 using System.Collections.Generic;
9 #if UNITY_WSA || UNITY_STANDALONE_WIN 30 public bool IsEnabled =
true;
35 public bool HasGaze {
get;
protected set; }
40 public bool HasDown {
get;
protected set; }
45 public bool DetectHold =
false;
50 public float HoldTime = 0.5f;
56 public float RollOffTime = 0.02f;
61 public bool IsSelected {
get;
protected set; }
63 [Tooltip(
"Set a keyword to invoke the OnSelect event")]
64 public string Keyword =
"";
66 [Tooltip(
"Gaze is required for the keyword to trigger this Interactive.")]
67 public bool KeywordRequiresGaze =
true;
80 public enum ButtonStateEnum { Default, Focus, Press, Selected, FocusSelected, PressSelected, Disabled, DisabledSelected }
86 protected float mRollOffTimer = 0;
87 protected float mHoldTimer = 0;
88 protected bool mCheckRollOff =
false;
89 protected bool mCheckHold =
false;
91 #if UNITY_WSA || UNITY_STANDALONE_WIN 92 protected KeywordRecognizer mKeywordRecognizer;
100 protected bool mIgnoreSelect =
false;
101 protected bool mCheckEnabled =
false;
102 protected bool mCheckSelected =
false;
103 protected bool UserInitiatedEvent =
false;
104 protected bool mAllowSelection =
false;
106 protected List<InteractiveWidget> mInteractiveWidgets =
new List<InteractiveWidget>();
110 if (ParentObject == null)
112 ParentObject = this.gameObject;
115 CollectWidgets(forceCollection:
true);
125 mKeywordArray =
new string[1] { Keyword };
126 if (Keyword.IndexOf(
',') > -1)
128 mKeywordArray = Keyword.Split(
',');
130 mKeywordDictionary =
new Dictionary<string, int>();
131 for (
int i = 0; i < mKeywordArray.Length; ++i)
133 mKeywordDictionary.Add(mKeywordArray[i], i);
137 #if UNITY_WSA || UNITY_STANDALONE_WIN 138 if (!KeywordRequiresGaze)
140 mKeywordRecognizer =
new KeywordRecognizer(mKeywordArray);
141 mKeywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
142 mKeywordRecognizer.Start();
148 mCheckEnabled = IsEnabled;
149 mCheckSelected = IsSelected;
164 UserInitiatedEvent =
true;
168 mIgnoreSelect =
false;
174 OnSelectEvents.Invoke();
189 SetKeywordListener(
true);
202 mCheckRollOff =
true;
203 SetKeywordListener(
false);
207 private void SetKeywordListener(
bool listen)
209 #if UNITY_WSA || UNITY_STANDALONE_WIN 212 if (KeywordRequiresGaze && mKeywordArray != null)
214 if (mKeywordRecognizer == null)
216 mKeywordRecognizer =
new KeywordRecognizer(mKeywordArray);
217 mKeywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
218 mKeywordRecognizer.Start();
222 if (!mKeywordRecognizer.IsRunning)
224 mKeywordRecognizer.Start();
231 if (mKeywordRecognizer != null && KeywordRequiresGaze)
233 if (mKeywordRecognizer.IsRunning)
235 mKeywordRecognizer.Stop();
236 mKeywordRecognizer.OnPhraseRecognized -= KeywordRecognizer_OnPhraseRecognized;
237 mKeywordRecognizer.Dispose();
238 mKeywordRecognizer = null;
253 if (lblTheme != null)
257 TextMesh textMesh = gameObject.GetComponentInChildren<TextMesh>();
258 if (textMesh != null)
260 textMesh.text = title;
275 mCheckRollOff =
false;
284 OnDownEvent.Invoke();
294 mIgnoreSelect =
false;
296 mCheckRollOff =
false;
306 mIgnoreSelect =
true;
311 OnHoldEvent.Invoke();
320 return mHoldTimer / HoldTime;
329 private void CollectWidgets(
bool forceCollection =
false)
331 if (mInteractiveWidgets.Count == 0 || forceCollection)
333 if (ParentObject != null)
335 ParentObject.GetComponentsInChildren(mInteractiveWidgets);
337 for (
int i = 0; i < mInteractiveWidgets.Count; ++i)
339 if (mInteractiveWidgets[i].InteractiveHost == null)
341 mInteractiveWidgets[i].InteractiveHost =
this;
345 mInteractiveWidgets.RemoveAt(i);
361 int interactiveCount = mInteractiveWidgets.Count;
362 for (
int i = 0; i < interactiveCount; ++i)
367 int currentCount = mInteractiveWidgets.Count;
368 if (currentCount < interactiveCount)
370 Debug.LogWarningFormat(
"Call to {0}'s SetState removed other interactive widgets. GameObject name: {1}.", widget.GetType(), widget.name);
371 interactiveCount = currentCount;
379 if (mInteractiveWidgets.Contains(widget))
384 mInteractiveWidgets.Add(widget);
390 if (mInteractiveWidgets != null)
392 mInteractiveWidgets.Remove(widget);
396 #if UNITY_WSA || UNITY_STANDALONE_WIN 397 protected virtual void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
401 if (args.text == Keyword && (!KeywordRequiresGaze || HasGaze) && IsEnabled)
403 if (mKeywordDictionary == null)
405 OnInputClicked(null);
482 mCheckSelected = IsSelected;
483 mCheckEnabled = IsEnabled;
492 if (mCheckRollOff && HasDown)
494 if (mRollOffTimer < RollOffTime)
496 mRollOffTimer += Time.deltaTime;
500 mCheckRollOff =
false;
506 if (mHoldTimer < HoldTime)
508 mHoldTimer += Time.deltaTime;
517 if (!UserInitiatedEvent && (mCheckEnabled != IsEnabled || mCheckSelected != IsSelected))
522 UserInitiatedEvent =
false;
527 SetKeywordListener(
false);
532 #if UNITY_WSA || UNITY_STANDALONE_WIN 533 if (mKeywordRecognizer != null && !KeywordRequiresGaze)
535 SetKeywordListener(
true);