InputManager.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.ARFoundation;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.XR.ARSubsystems;
  7. using System;
  8. using UnityEngine.XR.Interaction.Toolkit.AR;
  9. using UnityEngine.SceneManagement;
  10. using UnityEngine.Networking;
  11. using System.Linq;
  12. using ARLocation;
  13. public class InputManager : ARBaseGestureInteractable
  14. {
  15. public List<GameObject> arObjects;
  16. public Camera arCamera;
  17. public ARRaycastManager rayManager;
  18. public GameObject crosshair;
  19. //public ObjectPointsHolder points;
  20. private string selected;
  21. private GameObject selectedObject;
  22. List<ARRaycastHit> hits = new List<ARRaycastHit>();
  23. private Pose pose;
  24. // REAL WORLD STUFF
  25. public GameObject serverManager;
  26. private string playerNumber;
  27. public GameObject playerNumberObject;
  28. private string longitude;
  29. private string latitude;
  30. private float elapsed = 2.0f;
  31. private float period = 2.5f;
  32. public GameObject gpsPrefab;
  33. private string gameNumber;
  34. // Start is called before the first frame update
  35. void Start()
  36. {
  37. selected = "empty";
  38. selectedObject = arObjects[0];
  39. this.gameNumber = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  40. this.playerNumber = playerNumberObject.GetComponent<CurrentPlayerPicker>().GetPlayerNumber();
  41. longitude = "0";
  42. latitude = "0";
  43. }
  44. // Update is called once per frame
  45. void Update()
  46. {
  47. if (SceneManager.GetActiveScene().name != "RealWorld")
  48. {
  49. return;
  50. }
  51. elapsed += Time.deltaTime;
  52. if (elapsed >= period) {
  53. elapsed = 0.0f;
  54. StartCoroutine(GetLocation());
  55. }
  56. }
  57. private void FixedUpdate()
  58. {
  59. CrossHairCalculation();
  60. }
  61. public void ChangeSelection(string selection)
  62. {
  63. this.selected = selection;
  64. foreach (GameObject obj in arObjects)
  65. {
  66. if (obj.name == selected)
  67. {
  68. this.selectedObject = obj;
  69. }
  70. }
  71. }
  72. private bool IsPointerOverUI(TapGesture touch)
  73. {
  74. PointerEventData eventData = new PointerEventData(EventSystem.current);
  75. eventData.position = new Vector2(touch.startPosition.x, touch.startPosition.y);
  76. List<RaycastResult> results = new List<RaycastResult>();
  77. EventSystem.current.RaycastAll(eventData, results);
  78. return results.Count > 0;
  79. }
  80. private void CrossHairCalculation()
  81. {
  82. Vector3 origin = arCamera.ViewportToScreenPoint(new Vector3(0.5f,0.5f,0));
  83. if (GestureTransformationUtility.Raycast(origin, hits, TrackableType.PlaneWithinPolygon))
  84. {
  85. pose = hits[0].pose;
  86. crosshair.transform.position = pose.position;
  87. crosshair.transform.eulerAngles = new Vector3(90, 0, 0);
  88. }
  89. }
  90. // OVERRIDES
  91. protected override bool CanStartManipulationForGesture(TapGesture gesture)
  92. {
  93. if(gesture.TargetObject == null)
  94. {
  95. return true;
  96. }
  97. return false;
  98. }
  99. protected override void OnEndManipulation(TapGesture gesture)
  100. {
  101. if (gesture.WasCancelled)
  102. return;
  103. if (gesture.TargetObject != null || IsPointerOverUI(gesture))
  104. {
  105. return;
  106. }
  107. if(isAnotherSelected())
  108. {
  109. return;
  110. }
  111. if(isDeleteVisible())
  112. return;
  113. if (GestureTransformationUtility.Raycast(gesture.startPosition, hits ,TrackableType.PlaneWithinPolygon))
  114. {
  115. // REAL WORLD GAME BLOCK
  116. if (SceneManager.GetActiveScene().name != "RealWorld")
  117. {
  118. GameObject placedObject = Instantiate(selectedObject, pose.position, pose.rotation);
  119. return;
  120. }
  121. else
  122. {
  123. //GameObject placedObject2 = Instantiate(selectedObject, pose.position, pose.rotation);
  124. GameObject placedObject = Instantiate(selectedObject, pose.position, pose.rotation);
  125. // SET COLOR
  126. string pts = placedObject.GetComponent<Z_color_Select>().GetPoints();
  127. // SET GPS COORDINATES
  128. var loc = new Location()
  129. {
  130. Latitude = Convert.ToDouble(this.latitude),
  131. Longitude = Convert.ToDouble(this.longitude),
  132. Altitude = 0.5f,
  133. AltitudeMode = AltitudeMode.GroundRelative
  134. };
  135. var opts = new PlaceAtLocation.PlaceAtOptions()
  136. {
  137. HideObjectUntilItIsPlaced = false,
  138. MaxNumberOfLocationUpdates = 2,
  139. MovementSmoothing = 0.1f,
  140. UseMovingAverage = false
  141. };
  142. PlaceAtLocation.AddPlaceAtComponent(placedObject, loc, opts);
  143. //SEND THE OBJECT TO SERVER
  144. StartCoroutine(AddObjectServer(placedObject, pts));
  145. }
  146. //var anchorObject = new GameObject("PlacementAnchor");
  147. //anchorObject.transform.position = pose.position;
  148. //anchorObject.transform.rotation = pose.rotation;
  149. //placedObject.transform.parent = anchorObject.transform;
  150. //points.publicSendPointsToServer(selectedObject);
  151. }
  152. }
  153. private bool isDeleteVisible()
  154. {
  155. GameObject parent = GameObject.Find("Canvas");
  156. Transform[] children = parent.GetComponentsInChildren<Transform>(true);
  157. foreach(Transform ch in children)
  158. {
  159. if(ch.gameObject.name == "DeleteButton")
  160. {
  161. return ch.gameObject.activeInHierarchy;
  162. }
  163. }
  164. return false;
  165. }
  166. private bool isAnotherSelected()
  167. {
  168. GameObject found = GameObject.Find("AA_Selektah");
  169. if(found != null)
  170. {
  171. return true;
  172. }
  173. return false;
  174. }
  175. IEnumerator GetLocation()
  176. {
  177. Debug.Log("loc");
  178. // Check if the user has location service enabled.
  179. if (!Input.location.isEnabledByUser)
  180. yield break;
  181. Debug.Log("loc2");
  182. // Starts the location service.
  183. Input.location.Start(1);
  184. // Waits until the location service initializes
  185. int maxWait = 20;
  186. while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
  187. {
  188. yield return new WaitForSeconds(1);
  189. maxWait--;
  190. }
  191. Debug.Log("loc3");
  192. // If the service didn't initialize in 20 seconds this cancels location service use.
  193. if (maxWait < 1)
  194. {
  195. print("Timed out");
  196. yield break;
  197. }
  198. Debug.Log("loc4");
  199. // If the connection failed this cancels location service use.
  200. if (Input.location.status == LocationServiceStatus.Failed)
  201. {
  202. Debug.Log("Unable to determine device location");
  203. yield break;
  204. }
  205. else
  206. {
  207. // If the connection succeeded, this retrieves the device's current location and displays it in the Console window.
  208. //print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
  209. Debug.Log(Input.location.lastData.longitude.ToString());
  210. this.longitude = Input.location.lastData.longitude.ToString();
  211. this.latitude = Input.location.lastData.latitude.ToString();
  212. }
  213. // Stops the location service if there is no need to query location updates continuously.
  214. Input.location.Stop();
  215. }
  216. public IEnumerator AddObjectServer(GameObject placedObj, string points)
  217. {
  218. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_object";
  219. WWWForm form = new WWWForm();
  220. if(points == null) {
  221. points = "4";
  222. }
  223. this.gameNumber = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  224. this.playerNumber = playerNumberObject.GetComponent<CurrentPlayerPicker>().GetPlayerNumber();
  225. placedObj.GetComponent<Z_color_Select>().SetObjectColor(playerNumber);
  226. form.AddField("game", this.gameNumber);
  227. form.AddField("player", this.playerNumber);
  228. form.AddField("points", points);
  229. form.AddField("oid", "1");
  230. if(selectedObject.name != null) {
  231. form.AddField("name", selectedObject.name);
  232. }
  233. form.AddField("longitude", this.longitude);
  234. form.AddField("latitude", this.latitude);
  235. using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
  236. {
  237. yield return www.SendWebRequest();
  238. if (www.result != UnityWebRequest.Result.Success)
  239. {
  240. //Debug.Log(www.error);
  241. // PROMPT ERROR
  242. //errorPanel.SetActive(true);
  243. }
  244. else
  245. {
  246. //Debug.Log("Form upload complete!");
  247. }
  248. }
  249. }
  250. }