AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
RoomTest.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 UnityEngine;
5 using Random = System.Random;
6 
7 namespace HoloToolkit.Sharing.Tests
8 {
12  public class RoomTest : MonoBehaviour
13  {
14  private RoomManagerAdapter listener;
15  private RoomManager roomMgr;
16 
17  private string roomName = "New Room";
18  private Vector2 scrollViewVector = Vector2.zero;
19  private int padding = 4;
20  private int areaWidth = 400;
21  private int areaHeight = 300;
22  private int buttonWidth = 80;
23  private int lineHeight = 20;
24 
25  private Vector2 anchorScrollVector = Vector2.zero;
26  private string anchorName = "New Anchor";
27  private readonly byte[] anchorTestData = new byte[5 * 1024 * 1024]; // 5 meg test buffer
28 
29  private void Start()
30  {
31  for (int i = 0; i < anchorTestData.Length; ++i)
32  {
33  anchorTestData[i] = (byte)(i % 256);
34  }
35 
37  if (stage != null)
38  {
39  SharingManager sharingMgr = stage.Manager;
40  if (sharingMgr != null)
41  {
42  roomMgr = sharingMgr.GetRoomManager();
43 
44  listener = new RoomManagerAdapter();
45  listener.RoomAddedEvent += OnRoomAdded;
46  listener.RoomClosedEvent += OnRoomClosed;
47  listener.UserJoinedRoomEvent += OnUserJoinedRoom;
48  listener.UserLeftRoomEvent += OnUserLeftRoom;
49  listener.AnchorsChangedEvent += OnAnchorsChanged;
50  listener.AnchorsDownloadedEvent += OnAnchorsDownloaded;
51  listener.AnchorUploadedEvent += OnAnchorUploadComplete;
52 
53  roomMgr.AddListener(listener);
54  }
55  }
56  }
57 
58  private void OnDestroy()
59  {
60  roomMgr.RemoveListener(listener);
61  listener.Dispose();
62  }
63 
64  private void OnGUI()
65  {
66  // Make a background box
67  scrollViewVector = GUI.BeginScrollView(
68  new Rect(25, 25, areaWidth, areaHeight),
69  scrollViewVector,
70  new Rect(0, 0, areaWidth, areaHeight));
71 
72  if (roomMgr != null)
73  {
74  SessionManager sessionMgr = SharingStage.Instance.Manager.GetSessionManager();
75  if (sessionMgr != null)
76  {
77  roomName = GUI.TextField(
78  new Rect(buttonWidth + padding, 0, areaWidth - (buttonWidth + padding), lineHeight),
79  roomName);
80 
81  if (GUI.Button(new Rect(0, 0, buttonWidth, lineHeight), "Create"))
82  {
83  Random rnd = new Random();
84 
85  Room newRoom = roomMgr.CreateRoom(roomName, rnd.Next(), false);
86  if (newRoom == null)
87  {
88  Debug.LogWarning("Cannot create room");
89  }
90  }
91 
92  Room currentRoom = roomMgr.GetCurrentRoom();
93 
94  for (int i = 0; i < roomMgr.GetRoomCount(); ++i)
95  {
96  Room room = roomMgr.GetRoom(i);
97 
98  int vOffset = (padding + lineHeight) * (i + 1);
99  int hOffset = 0;
100 
101  bool keepOpen = GUI.Toggle(new Rect(hOffset, vOffset, lineHeight, lineHeight), room.GetKeepOpen(), "");
102  room.SetKeepOpen(keepOpen);
103 
104  hOffset += lineHeight + padding;
105 
106  if (currentRoom != null && room.GetID() == currentRoom.GetID())
107  {
108  if (GUI.Button(new Rect(hOffset, vOffset, buttonWidth, lineHeight), "Leave"))
109  {
110  roomMgr.LeaveRoom();
111  }
112  }
113  else
114  {
115  if (GUI.Button(new Rect(hOffset, vOffset, buttonWidth, lineHeight), "Join"))
116  {
117  if (!roomMgr.JoinRoom(room))
118  {
119  Debug.LogWarning("Cannot join room");
120  }
121  }
122  }
123 
124  hOffset += buttonWidth + padding;
125 
126  GUI.Label(new Rect(hOffset, vOffset, areaWidth - (buttonWidth + padding), lineHeight),
127  room.GetName().GetString());
128  }
129  }
130  }
131 
132  // End the ScrollView
133  GUI.EndScrollView();
134 
135  if (roomMgr != null)
136  {
137  Room currentRoom = roomMgr.GetCurrentRoom();
138 
139  if (currentRoom != null)
140  {
141  // Display option to upload anchor
142  anchorScrollVector = GUI.BeginScrollView(
143  new Rect(areaWidth + 50, 25, areaWidth, areaHeight),
144  anchorScrollVector,
145  new Rect(0, 0, areaWidth, areaHeight));
146 
147  anchorName =
148  GUI.TextField(
149  new Rect(
150  buttonWidth + padding,
151  0,
152  areaWidth - (buttonWidth + padding),
153  lineHeight),
154  anchorName);
155 
156  if (GUI.Button(new Rect(0, 0, buttonWidth, lineHeight), "Create"))
157  {
158  if (!roomMgr.UploadAnchor(currentRoom, anchorName, anchorTestData, anchorTestData.Length))
159  {
160  Debug.LogError("Failed to start anchor upload");
161  }
162  }
163 
164  for (int i = 0; i < currentRoom.GetAnchorCount(); ++i)
165  {
166  int vOffset = (padding + lineHeight) * (i + 1);
167 
168  XString currentRoomAnchor = currentRoom.GetAnchorName(i);
169 
170  GUI.Label(
171  new Rect(
172  buttonWidth + padding,
173  vOffset,
174  areaWidth - (buttonWidth + padding),
175  lineHeight),
176  currentRoomAnchor);
177 
178  if (GUI.Button(new Rect(0, vOffset, buttonWidth, lineHeight), "Download"))
179  {
180  if (!roomMgr.DownloadAnchor(currentRoom, currentRoomAnchor))
181  {
182  Debug.LogWarning("Failed to start anchor download");
183  }
184  }
185  }
186 
187  GUI.EndScrollView();
188  }
189  }
190  }
191 
192  private void OnRoomAdded(Room newRoom)
193  {
194  Debug.LogFormat("Room {0} added", newRoom.GetName().GetString());
195  }
196 
197  private void OnRoomClosed(Room room)
198  {
199  Debug.LogFormat("Room {0} closed", room.GetName().GetString());
200  }
201 
202  private void OnUserJoinedRoom(Room room, int user)
203  {
204  User joinedUser = SharingStage.Instance.SessionUsersTracker.GetUserById(user);
205  Debug.LogFormat("User {0} joined Room {1}", joinedUser.GetName(), room.GetName().GetString());
206  }
207 
208  private void OnUserLeftRoom(Room room, int user)
209  {
210  User leftUser = SharingStage.Instance.SessionUsersTracker.GetUserById(user);
211  Debug.LogFormat("User {0} left Room {1}", leftUser.GetName(), room.GetName().GetString());
212  }
213 
214  private void OnAnchorsChanged(Room room)
215  {
216  Debug.LogFormat("Anchors changed for Room {0}", room.GetName().GetString());
217  }
218 
219  private void OnAnchorsDownloaded(bool successful, AnchorDownloadRequest request, XString failureReason)
220  {
221  if (successful)
222  {
223  Debug.LogFormat("Anchors download succeeded for Room {0}", request.GetRoom().GetName().GetString());
224  }
225  else
226  {
227  Debug.LogFormat("Anchors download failed: {0}", failureReason.GetString());
228  }
229  }
230 
231  private void OnAnchorUploadComplete(bool successful, XString failureReason)
232  {
233  if (successful)
234  {
235  Debug.Log("Anchors upload succeeded");
236  }
237  else
238  {
239  Debug.LogFormat("Anchors upload failed: {0}", failureReason.GetString());
240  }
241  }
242  }
243 }
virtual Room CreateRoom(XString roomName, long roomID, bool keepOpenWhenEmpty)
Definition: RoomManager.cs:68
Test class for demonstrating creating rooms and anchors.
Definition: RoomTest.cs:12
System.Action< bool, AnchorDownloadRequest, XString > AnchorsDownloadedEvent
virtual bool UploadAnchor(Room room, XString anchorName, byte[] data, int dataSize)
Definition: RoomManager.cs:89
System.Action< bool, XString > AnchorUploadedEvent
virtual void SetKeepOpen(bool keepOpen)
Definition: Room.cs:69
virtual RoomManager GetRoomManager()
virtual void AddListener(RoomManagerListener newListener)
Definition: RoomManager.cs:43
System.Action< Room, int > UserJoinedRoomEvent
virtual XString GetAnchorName(int index)
Definition: Room.cs:78
virtual bool GetKeepOpen()
Definition: Room.cs:64
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
virtual bool JoinRoom(Room room)
Definition: RoomManager.cs:74
virtual bool DownloadAnchor(Room room, XString anchorName)
Definition: RoomManager.cs:84
System.Action< Room, int > UserLeftRoomEvent
virtual Room GetRoom(int index)
Definition: RoomManager.cs:56
virtual XString GetName()
Definition: Room.cs:43
virtual long GetID()
Definition: Room.cs:49
Allows users of the RoomManager to register to receive event callbacks without having their classes i...
SharingManager Manager
Sharing manager used by the application.
Definition: SharingStage.cs:97
virtual int GetAnchorCount()
Definition: Room.cs:73
The SharingStage is in charge of managing the core networking layer for the application.
Definition: SharingStage.cs:14
virtual void RemoveListener(RoomManagerListener oldListener)
Definition: RoomManager.cs:47
virtual XString GetName()
Definition: User.cs:43