BugReportPopoverService.cs 4.4 KB

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