BugReportPopoverService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. namespace SRDebugger.Services.Implementation
  2. {
  3. using System;
  4. using System.Collections;
  5. using Internal;
  6. using SRF;
  7. using SRF.Service;
  8. using UI.Other;
  9. using UnityEngine;
  10. [Service(typeof (BugReportPopoverService))]
  11. public class BugReportPopoverService : SRServiceBase<BugReportPopoverService>
  12. {
  13. private BugReportCompleteCallback _callback;
  14. private bool _isVisible;
  15. private BugReportPopoverRoot _popover;
  16. private BugReportSheetController _sheet;
  17. public bool IsShowingPopover
  18. {
  19. get { return _isVisible; }
  20. }
  21. public void ShowBugReporter(BugReportCompleteCallback callback, bool takeScreenshotFirst = true,
  22. string descriptionText = null)
  23. {
  24. if (_isVisible)
  25. {
  26. throw new InvalidOperationException("Bug report popover is already visible.");
  27. }
  28. if (_popover == null)
  29. {
  30. Load();
  31. }
  32. if (_popover == null)
  33. {
  34. Debug.LogWarning("[SRDebugger] Bug report popover failed loading, executing callback with fail result");
  35. callback(false, "Resource load failed");
  36. return;
  37. }
  38. _callback = callback;
  39. _isVisible = true;
  40. SRDebuggerUtil.EnsureEventSystemExists();
  41. StartCoroutine(OpenCo(takeScreenshotFirst, descriptionText));
  42. }
  43. private IEnumerator OpenCo(bool takeScreenshot, string descriptionText)
  44. {
  45. if (takeScreenshot)
  46. {
  47. // Wait for screenshot to be captured
  48. yield return StartCoroutine(BugReportScreenshotUtil.ScreenshotCaptureCo());
  49. }
  50. _popover.CachedGameObject.SetActive(true);
  51. yield return new WaitForEndOfFrame();
  52. if (!string.IsNullOrEmpty(descriptionText))
  53. {
  54. _sheet.DescriptionField.text = descriptionText;
  55. }
  56. }
  57. private void SubmitComplete(bool didSucceed, string errorMessage)
  58. {
  59. OnComplete(didSucceed, errorMessage, false);
  60. }
  61. private void CancelPressed()
  62. {
  63. OnComplete(false, "User Cancelled", true);
  64. }
  65. private void OnComplete(bool success, string errorMessage, bool close)
  66. {
  67. if (!_isVisible)
  68. {
  69. Debug.LogWarning("[SRDebugger] Received callback at unexpected time. ???");
  70. return;
  71. }
  72. if (!success && !close)
  73. {
  74. return;
  75. }
  76. _isVisible = false;
  77. // Destroy it all so it doesn't linger in the scene using memory
  78. _popover.gameObject.SetActive(false);
  79. Destroy(_popover.gameObject);
  80. _popover = null;
  81. _sheet = null;
  82. BugReportScreenshotUtil.ScreenshotData = null;
  83. _callback(success, errorMessage);
  84. }
  85. private void TakingScreenshot()
  86. {
  87. if (!IsShowingPopover)
  88. {
  89. Debug.LogWarning("[SRDebugger] Received callback at unexpected time. ???");
  90. return;
  91. }
  92. _popover.CanvasGroup.alpha = 0f;
  93. }
  94. private void ScreenshotComplete()
  95. {
  96. if (!IsShowingPopover)
  97. {
  98. Debug.LogWarning("[SRDebugger] Received callback at unexpected time. ???");
  99. return;
  100. }
  101. _popover.CanvasGroup.alpha = 1f;
  102. }
  103. protected override void Awake()
  104. {
  105. base.Awake();
  106. CachedTransform.SetParent(Hierarchy.Get("SRDebugger"));
  107. }
  108. private void Load()
  109. {
  110. var popoverPrefab = Resources.Load<BugReportPopoverRoot>(SRDebugPaths.BugReportPopoverPath);
  111. var sheetPrefab = Resources.Load<BugReportSheetController>(SRDebugPaths.BugReportSheetPath);
  112. if (popoverPrefab == null)
  113. {
  114. Debug.LogError("[SRDebugger] Unable to load bug report popover prefab");
  115. return;
  116. }
  117. if (sheetPrefab == null)
  118. {
  119. Debug.LogError("[SRDebugger] Unable to load bug report sheet prefab");
  120. return;
  121. }
  122. _popover = SRInstantiate.Instantiate(popoverPrefab);
  123. _popover.CachedTransform.SetParent(CachedTransform, false);
  124. _sheet = SRInstantiate.Instantiate(sheetPrefab);
  125. _sheet.CachedTransform.SetParent(_popover.Container, false);
  126. _sheet.SubmitComplete = SubmitComplete;
  127. _sheet.CancelPressed = CancelPressed;
  128. _sheet.TakingScreenshot = TakingScreenshot;
  129. _sheet.ScreenshotComplete = ScreenshotComplete;
  130. _popover.CachedGameObject.SetActive(false);
  131. }
  132. }
  133. }