MenuController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ARLocation.MapboxRoutes.Examples.Search
  5. {
  6. public class MenuController : MonoBehaviour
  7. {
  8. public enum LineType
  9. {
  10. Route,
  11. NextTarget
  12. }
  13. public string MapboxToken = "pk.eyJ1IjoiZG1iZm0iLCJhIjoiY2tyYW9hdGMwNGt6dTJ2bzhieDg3NGJxNyJ9.qaQsMUbyu4iARFe0XB2SWg";
  14. public GameObject ARSession;
  15. public GameObject ARSessionOrigin;
  16. public GameObject RouteContainer;
  17. public Camera Camera;
  18. public MapboxRoute MapboxRoute;
  19. public AbstractRouteRenderer RoutePathRenderer;
  20. public AbstractRouteRenderer NextTargetPathRenderer;
  21. public bool DebugMode = true;
  22. private AbstractRouteRenderer currentPathRenderer => s.LineType == LineType.Route ? RoutePathRenderer : NextTargetPathRenderer;
  23. public LineType PathRendererType
  24. {
  25. get => s.LineType;
  26. set
  27. {
  28. if (value != s.LineType)
  29. {
  30. currentPathRenderer.enabled = false;
  31. s.LineType = value;
  32. currentPathRenderer.enabled = true;
  33. if (s.View == View.Route)
  34. {
  35. MapboxRoute.RoutePathRenderer = currentPathRenderer;
  36. }
  37. }
  38. }
  39. }
  40. enum View
  41. {
  42. SearchMenu,
  43. Route,
  44. }
  45. [System.Serializable]
  46. private class State
  47. {
  48. public string QueryText = "";
  49. public List<GeocodingFeature> Results = new List<GeocodingFeature>();
  50. public View View = View.SearchMenu;
  51. public Location destination;
  52. public LineType LineType = LineType.NextTarget;
  53. public string ErrorMessage;
  54. }
  55. private State s = new State();
  56. private GUIStyle _textStyle;
  57. GUIStyle textStyle()
  58. {
  59. if (_textStyle == null)
  60. {
  61. _textStyle = new GUIStyle(GUI.skin.label);
  62. _textStyle.fontSize = 48;
  63. _textStyle.fontStyle = FontStyle.Bold;
  64. }
  65. return _textStyle;
  66. }
  67. private GUIStyle _textFieldStyle;
  68. GUIStyle textFieldStyle()
  69. {
  70. if (_textFieldStyle == null)
  71. {
  72. _textFieldStyle = new GUIStyle(GUI.skin.textField);
  73. _textFieldStyle.fontSize = 48;
  74. }
  75. return _textFieldStyle;
  76. }
  77. private GUIStyle _errorLabelStyle;
  78. GUIStyle errorLabelSytle()
  79. {
  80. if (_errorLabelStyle == null)
  81. {
  82. _errorLabelStyle = new GUIStyle(GUI.skin.label);
  83. _errorLabelStyle.fontSize = 24;
  84. _errorLabelStyle.fontStyle = FontStyle.Bold;
  85. _errorLabelStyle.normal.textColor = Color.red;
  86. }
  87. return _errorLabelStyle;
  88. }
  89. private GUIStyle _buttonStyle;
  90. GUIStyle buttonStyle()
  91. {
  92. if (_buttonStyle == null)
  93. {
  94. _buttonStyle = new GUIStyle(GUI.skin.button);
  95. _buttonStyle.fontSize = 48;
  96. }
  97. return _buttonStyle;
  98. }
  99. void OnGUI()
  100. {
  101. if (s.View == View.Route)
  102. {
  103. return;
  104. }
  105. GUILayout.BeginVertical(new GUIStyle() { padding = new RectOffset(20, 20, 20, 20) });
  106. var w = Screen.width;
  107. GUILayout.BeginVertical(GUILayout.MaxHeight(100));
  108. GUILayout.Label("Location Search", textStyle());
  109. GUILayout.BeginHorizontal(GUILayout.MaxHeight(100), GUILayout.MinHeight(100));
  110. s.QueryText = GUILayout.TextField(s.QueryText, textFieldStyle(), GUILayout.MinWidth(0.8f * w), GUILayout.MaxWidth(0.8f * w));
  111. if (GUILayout.Button("OK", buttonStyle(), GUILayout.MinWidth(0.15f * w), GUILayout.MaxWidth(0.15f * w)))
  112. {
  113. s.ErrorMessage = null;
  114. StartCoroutine(search());
  115. }
  116. GUILayout.EndHorizontal();
  117. GUILayout.EndVertical();
  118. GUILayout.BeginVertical();
  119. if (s.ErrorMessage != null)
  120. {
  121. GUILayout.Label(s.ErrorMessage, errorLabelSytle());
  122. }
  123. foreach (var r in s.Results)
  124. {
  125. if (GUILayout.Button(r.place_name, new GUIStyle(buttonStyle()) { alignment = TextAnchor.MiddleLeft, fontSize = 24, fixedHeight = 0.05f * Screen.height }))
  126. {
  127. StartRoute(r.geometry.coordinates[0]);
  128. }
  129. }
  130. GUILayout.EndVertical();
  131. GUILayout.EndVertical();
  132. }
  133. public void StartRoute(Location dest)
  134. {
  135. s.destination = dest;
  136. if (ARLocationProvider.Instance.IsEnabled)
  137. {
  138. loadRoute(ARLocationProvider.Instance.CurrentLocation.ToLocation());
  139. }
  140. else
  141. {
  142. ARLocationProvider.Instance.OnEnabled.AddListener(loadRoute);
  143. }
  144. }
  145. public void EndRoute()
  146. {
  147. ARLocationProvider.Instance.OnEnabled.RemoveListener(loadRoute);
  148. ARSession.SetActive(false);
  149. ARSessionOrigin.SetActive(false);
  150. RouteContainer.SetActive(false);
  151. Camera.gameObject.SetActive(true);
  152. s.View = View.SearchMenu;
  153. }
  154. private void loadRoute(Location _)
  155. {
  156. if (s.destination != null)
  157. {
  158. var api = new MapboxApi(MapboxToken);
  159. var loader = new RouteLoader(api, DebugMode);
  160. StartCoroutine(
  161. loader.LoadRoute(
  162. new RouteWaypoint { Type = RouteWaypointType.UserLocation },
  163. new RouteWaypoint { Type = RouteWaypointType.Location, Location = s.destination },
  164. (err, res) =>
  165. {
  166. if (err != null)
  167. {
  168. s.ErrorMessage = err;
  169. s.Results = new List<GeocodingFeature>();
  170. return;
  171. }
  172. ARSession.SetActive(true);
  173. ARSessionOrigin.SetActive(true);
  174. RouteContainer.SetActive(true);
  175. Camera.gameObject.SetActive(false);
  176. s.View = View.Route;
  177. MapboxRoute.RoutePathRenderer = currentPathRenderer;
  178. MapboxRoute.BuildRoute(res);
  179. }));
  180. }
  181. }
  182. IEnumerator search()
  183. {
  184. var api = new MapboxApi(MapboxToken);
  185. yield return api.QueryLocal(s.QueryText, DebugMode);
  186. if (api.ErrorMessage != null)
  187. {
  188. s.ErrorMessage = api.ErrorMessage;
  189. s.Results = new List<GeocodingFeature>();
  190. }
  191. else
  192. {
  193. s.Results = api.QueryLocalResult.features;
  194. }
  195. }
  196. }
  197. }