PageRenderTime 88ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Qualcomm Augmented Reality/Scripts/CloudRecoEventHandler.cs

https://gitlab.com/sheetanshusrivastava3/Unity3D-code-repository-design-patterns
C# | 233 lines | 121 code | 39 blank | 73 comment | 10 complexity | 048459f7ddbe1a02a19d741180b5a6f1 MD5 | raw file
  1. /*==============================================================================
  2. * Copyright (c) 2012-2014 Qualcomm Connected Experiences, Inc. All Rights Reserved.
  3. * ==============================================================================*/
  4. using System;
  5. using UnityEngine;
  6. /// <summary>
  7. /// This MonoBehaviour implements the Cloud Reco Event handling for this sample.
  8. /// It registers itself at the CloudRecoBehaviour and is notified of new search results as well as error messages
  9. /// The current state is visualized and new results are enabled using the TargetFinder API.
  10. /// </summary>
  11. public class CloudRecoEventHandler : MonoBehaviour, ICloudRecoEventHandler
  12. {
  13. #region PRIVATE_MEMBER_VARIABLES
  14. // CloudRecoBehaviour reference to avoid lookups
  15. private CloudRecoBehaviour mCloudRecoBehaviour;
  16. // ImageTracker reference to avoid lookups
  17. private ImageTracker mImageTracker;
  18. // reference to the cloud reco scene manager component:
  19. //private ContentManager mContentManager;
  20. // array that all textures are loaded into on startup
  21. private Texture mRequestingTexture;
  22. // the parent gameobject of the referenced ImageTargetTemplate - reused for all target search results
  23. private GameObject mParentOfImageTargetTemplate;
  24. #endregion // PRIVATE_MEMBER_VARIABLES
  25. #region EXPOSED_PUBLIC_VARIABLES
  26. /// <summary>
  27. /// can be set in the Unity inspector to reference a ImageTargetBehaviour that is used for augmentations of new cloud reco results.
  28. /// </summary>
  29. public ImageTargetBehaviour ImageTargetTemplate;
  30. #endregion
  31. #region ICloudRecoEventHandler_IMPLEMENTATION
  32. /// <summary>
  33. /// called when TargetFinder has been initialized successfully
  34. /// </summary>
  35. public void OnInitialized()
  36. {
  37. // get a reference to the Image Tracker, remember it
  38. mImageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
  39. //mContentManager = (ContentManager)FindObjectOfType(typeof(ContentManager));
  40. }
  41. /// <summary>
  42. /// visualize initialization errors
  43. /// </summary>
  44. public void OnInitError(TargetFinder.InitState initError)
  45. {
  46. switch (initError)
  47. {
  48. case TargetFinder.InitState.INIT_ERROR_NO_NETWORK_CONNECTION:
  49. ErrorMsg.New("Network Unavailable", "Please check your internet connection and try again.", RestartApplication);
  50. break;
  51. case TargetFinder.InitState.INIT_ERROR_SERVICE_NOT_AVAILABLE:
  52. ErrorMsg.New("Service Unavailable", "Failed to initialize app because the service is not available.", null);
  53. break;
  54. }
  55. }
  56. /// <summary>
  57. /// visualize update errors
  58. /// </summary>
  59. public void OnUpdateError(TargetFinder.UpdateState updateError)
  60. {
  61. switch (updateError)
  62. {
  63. case TargetFinder.UpdateState.UPDATE_ERROR_AUTHORIZATION_FAILED:
  64. ErrorMsg.New("Authorization Error","The cloud recognition service access keys are incorrect or have expired.");
  65. break;
  66. case TargetFinder.UpdateState.UPDATE_ERROR_NO_NETWORK_CONNECTION:
  67. ErrorMsg.New("Network Unavailable","Please check your internet connection and try again.");
  68. break;
  69. case TargetFinder.UpdateState.UPDATE_ERROR_PROJECT_SUSPENDED:
  70. ErrorMsg.New("Authorization Error","The cloud recognition service has been suspended.");
  71. break;
  72. case TargetFinder.UpdateState.UPDATE_ERROR_REQUEST_TIMEOUT:
  73. ErrorMsg.New("Request Timeout","The network request has timed out, please check your internet connection and try again.");
  74. break;
  75. case TargetFinder.UpdateState.UPDATE_ERROR_SERVICE_NOT_AVAILABLE:
  76. ErrorMsg.New("Service Unavailable","The service is unavailable, please try again later.");
  77. break;
  78. case TargetFinder.UpdateState.UPDATE_ERROR_TIMESTAMP_OUT_OF_RANGE:
  79. ErrorMsg.New("Clock Sync Error","Please update the date and time and try again.");
  80. break;
  81. case TargetFinder.UpdateState.UPDATE_ERROR_UPDATE_SDK:
  82. ErrorMsg.New("Unsupported Version","The application is using an unsupported version of Vuforia.");
  83. break;
  84. }
  85. }
  86. /// <summary>
  87. /// when we start scanning, unregister Trackable from the ImageTargetTemplate, then delete all trackables
  88. /// </summary>
  89. public void OnStateChanged(bool scanning)
  90. {
  91. if (scanning)
  92. {
  93. // clear all known trackables
  94. mImageTracker.TargetFinder.ClearTrackables(false);
  95. //ImageTargetTemplate.transform.GetChild(0).gameObject.SetActive(false);
  96. // hide the ImageTargetTemplate
  97. //mContentManager.HideObject();
  98. }
  99. else
  100. {
  101. //ImageTargetTemplate.transform.GetChild(0).gameObject.SetActive(true);
  102. }
  103. }
  104. /// <summary>
  105. /// Handles new search results
  106. /// </summary>
  107. /// <param name="targetSearchResult"></param>
  108. public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
  109. {
  110. // This code demonstrates how to reuse an ImageTargetBehaviour for new search results and modifying it according to the metadata
  111. // Depending on your application, it can make more sense to duplicate the ImageTargetBehaviour using Instantiate(),
  112. // or to create a new ImageTargetBehaviour for each new result
  113. // Vuforia will return a new object with the right script automatically if you use
  114. // TargetFinder.EnableTracking(TargetSearchResult result, string gameObjectName)
  115. //Check if the metadata isn't null
  116. if(targetSearchResult.MetaData == null)
  117. {
  118. return;
  119. }
  120. // enable the new result with the same ImageTargetBehaviour:
  121. ImageTargetBehaviour imageTargetBehaviour = mImageTracker.TargetFinder.EnableTracking(targetSearchResult, mParentOfImageTargetTemplate) as ImageTargetBehaviour;
  122. if (imageTargetBehaviour != null)
  123. {
  124. //ImageTargetTemplate.transform.GetChild(0).gameObject.SetActive(true);
  125. // stop the target finder
  126. mCloudRecoBehaviour.CloudRecoEnabled = false;
  127. AppManagerMore.instance.RequestPayLoadUrl(targetSearchResult.UniqueTargetId);
  128. // Calls the TargetCreated Method of the SceneManager object to start loading
  129. // the BookData from the JSON
  130. //mContentManager.TargetCreated(targetSearchResult.MetaData);
  131. //mContentManager.AnimationsManager.SetInitialAnimationFlags();
  132. }
  133. }
  134. #endregion // ICloudRecoEventHandler_IMPLEMENTATION
  135. /// <summary>
  136. /// register for events at the CloudRecoBehaviour
  137. /// </summary>
  138. void Start()
  139. {
  140. // look up the gameobject containing the ImageTargetTemplate:
  141. mParentOfImageTargetTemplate = ImageTargetTemplate.gameObject;
  142. // intialize the ErrorMsg class
  143. ErrorMsg.Init();
  144. // register this event handler at the cloud reco behaviour
  145. CloudRecoBehaviour cloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
  146. if (cloudRecoBehaviour)
  147. {
  148. cloudRecoBehaviour.RegisterEventHandler(this);
  149. }
  150. // remember cloudRecoBehaviour for later
  151. mCloudRecoBehaviour = cloudRecoBehaviour;
  152. // load and remember all used textures:
  153. mRequestingTexture = Resources.Load("UserInterface/TextureRequesting") as Texture;
  154. }
  155. /// <summary>
  156. /// draw the sample GUI and error messages
  157. /// </summary>
  158. void OnGUI()
  159. {
  160. if (mCloudRecoBehaviour.CloudRecoInitialized && mImageTracker.TargetFinder.IsRequesting())
  161. {
  162. // draw the requesting texture
  163. DrawTexture(mRequestingTexture);
  164. }
  165. // draw error messages in case there were any
  166. ErrorMsg.Draw();
  167. }
  168. #region PRIVATE_METHODS
  169. /// <summary>
  170. /// draws a textures using UnityGUI
  171. /// </summary>
  172. private void DrawTexture(Texture texture)
  173. {
  174. // scale texture up by device dependent factor
  175. int smallerScreenDimension = Screen.width < Screen.height ? Screen.width : Screen.height;
  176. float deviceDependentScale = smallerScreenDimension / 480f;
  177. // draw texture at center with given verticalPosition and scale:
  178. float width = texture.width * deviceDependentScale;
  179. float height = texture.height * deviceDependentScale;
  180. #if UNITY_IOS
  181. height = iOSGetAnyGUIObjectSize.getHeightValue();
  182. #endif
  183. float left = (Screen.width * 0.5f) - (width * 0.5f);
  184. float top = (Screen.height * 0.9f) - (height * 0.5f);
  185. GUI.DrawTexture(new Rect(left, top, width, height), texture);
  186. }
  187. //Error Handling Callback that gets called when the application is not connected to the internet
  188. private void RestartApplication()
  189. {
  190. //Restarts the app
  191. Application.LoadLevel("Vuforia-1-About");
  192. }
  193. #endregion // PRIVATE_METHODS
  194. }