AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
InteractiveSet.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 using UnityEngine.Events;
7 
8 namespace HoloToolkit.Examples.InteractiveElements
9 {
10  public enum SelectionType
11  {
12  single,
13  multiple
14  }
15 
19  public class InteractiveSet : MonoBehaviour
20  {
21  [Tooltip("Allow single or multiple selection")]
22  public SelectionType Type = SelectionType.single;
23 
24  [Tooltip("Interactives that will be managed by this controller")]
25  public List<InteractiveToggle> Interactives;
26 
27  [Tooltip("Currently selected indices or default starting indices")]
28  public List<int> SelectedIndices = new List<int>() { 0 };
29 
30  [Tooltip("exposed selection changed event")]
31  public UnityEvent OnSelectionEvents;
32 
33  // list of calls to remove events on recycled buttons
34  private Dictionary<InteractiveToggle, UnityAction> Calls = new Dictionary<InteractiveToggle, UnityAction>();
35 
36  private bool mHasInit = false;
37 
38  private void Start()
39  {
40  UpdateInteractives();
41  }
42 
43  public void UpdateInteractives()
44  {
45  foreach (InteractiveToggle toggle in Calls.Keys)
46  {
47  toggle.OnSelectEvents.RemoveListener(Calls[toggle]);
48  }
49  Calls.Clear();
50  for (int i = 0; i < Interactives.Count; ++i)
51  {
52  int itemIndex = i;
53  // add selection event handler to each button
54  UnityAction call = () => HandleOnSelection(itemIndex);
55  Calls.Add(Interactives[i], call);
56  Interactives[i].OnSelectEvents.AddListener(call);
57  if (Type == SelectionType.single)
58  {
59  Interactives[i].AllowDeselect = false;
60  }
61  Interactives[i].HasSelection = SelectedIndices.Contains(i);
62  }
63  OnSelectionEvents.Invoke();
64  }
65 
66  public void RemoveInteractive(int itemIndex)
67  {
68  Interactives[itemIndex].OnSelectEvents.RemoveListener(() => HandleOnSelection(itemIndex));
69  Interactives.RemoveAt(itemIndex);
70  }
71 
76  public void SetSelection(int index)
77  {
78  if (!isActiveAndEnabled ||
79  (index < 0 || Interactives.Count <= index))
80  {
81  return;
82  }
83 
84  Interactives[index].OnInputClicked(null);
85  }
86 
91  private void HandleOnSelection(int index)
92  {
93  if (Type == SelectionType.single)
94  {
95  for (int i = 0; i < Interactives.Count; ++i)
96  {
97  if (i != index)
98  {
99  Interactives[i].HasSelection = false;
100  }
101  }
102 
103  if (!mHasInit)
104  {
105  Interactives[index].HasSelection = true;
106  mHasInit = true;
107  }
108 
109  SelectedIndices.Clear();
110  SelectedIndices.Add(index);
111  }
112  else
113  {
114  Interactives[index].HasSelection = !Interactives[index].HasSelection;
115  if (SelectedIndices.Contains(index))
116  {
117  SelectedIndices.Remove(index);
118  }
119  else
120  {
121  SelectedIndices.Add(index);
122  }
123  }
124  OnSelectionEvents.Invoke();
125  }
126 
127  private void OnDestroy()
128  {
129  for (int i = Interactives.Count - 1; i >= 0; i--)
130  {
131  RemoveInteractive(i);
132  }
133  }
134  }
135 }
A controller for managing multiple radial or tab type buttons
void SetSelection(int index)
Sets the selected index and selected Interactive
InteractiveToggle expands Interactive to expose selection or toggle states.
UnityEvent OnSelectEvents
Exposed Unity Events
Definition: Interactive.cs:72