DebugLogPopup.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections;
  5. // Manager class for the debug popup
  6. namespace IngameDebugConsole
  7. {
  8. public class DebugLogPopup : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
  9. {
  10. private RectTransform popupTransform;
  11. // Dimensions of the popup divided by 2
  12. private Vector2 halfSize;
  13. // Background image that will change color to indicate an alert
  14. private Image backgroundImage;
  15. // Canvas group to modify visibility of the popup
  16. private CanvasGroup canvasGroup;
  17. #pragma warning disable 0649
  18. [SerializeField]
  19. private DebugLogManager debugManager;
  20. [SerializeField]
  21. private Text newInfoCountText;
  22. [SerializeField]
  23. private Text newWarningCountText;
  24. [SerializeField]
  25. private Text newErrorCountText;
  26. [SerializeField]
  27. private Text newFPSCountText;
  28. [SerializeField]
  29. private Color alertColorInfo;
  30. [SerializeField]
  31. private Color alertColorWarning;
  32. [SerializeField]
  33. private Color alertColorError;
  34. #pragma warning restore 0649
  35. // Number of new debug entries since the log window has been closed
  36. private int newInfoCount = 0, newWarningCount = 0, newErrorCount = 0;
  37. private Color normalColor;
  38. private bool isPopupBeingDragged = false;
  39. // Coroutines for simple code-based animations
  40. private IEnumerator moveToPosCoroutine = null;
  41. void Awake()
  42. {
  43. popupTransform = (RectTransform) transform;
  44. backgroundImage = GetComponent<Image>();
  45. canvasGroup = GetComponent<CanvasGroup>();
  46. normalColor = backgroundImage.color;
  47. }
  48. void Start()
  49. {
  50. halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
  51. }
  52. public void OnViewportDimensionsChanged()
  53. {
  54. if( !gameObject.activeSelf )
  55. return;
  56. halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
  57. OnEndDrag( null );
  58. }
  59. public void NewInfoLogArrived()
  60. {
  61. newInfoCount++;
  62. newInfoCountText.text = newInfoCount.ToString();
  63. if( newWarningCount == 0 && newErrorCount == 0 )
  64. backgroundImage.color = alertColorInfo;
  65. }
  66. public void NewWarningLogArrived()
  67. {
  68. newWarningCount++;
  69. newWarningCountText.text = newWarningCount.ToString();
  70. if( newErrorCount == 0 )
  71. backgroundImage.color = alertColorWarning;
  72. }
  73. public void NewErrorLogArrived()
  74. {
  75. newErrorCount++;
  76. newErrorCountText.text = newErrorCount.ToString();
  77. backgroundImage.color = alertColorError;
  78. }
  79. public void NewFPSCountArrived(float fps)
  80. {
  81. this.newFPSCountText.text = fps.ToString("0.00");
  82. }
  83. private void Reset()
  84. {
  85. newInfoCount = 0;
  86. newWarningCount = 0;
  87. newErrorCount = 0;
  88. newInfoCountText.text = "0";
  89. newWarningCountText.text = "0";
  90. newErrorCountText.text = "0";
  91. backgroundImage.color = normalColor;
  92. }
  93. // A simple smooth movement animation
  94. private IEnumerator MoveToPosAnimation( Vector3 targetPos )
  95. {
  96. float modifier = 0f;
  97. Vector3 initialPos = popupTransform.position;
  98. while( modifier < 1f )
  99. {
  100. modifier += 4f * Time.unscaledDeltaTime;
  101. popupTransform.position = Vector3.Lerp( initialPos, targetPos, modifier );
  102. yield return null;
  103. }
  104. }
  105. // Popup is clicked
  106. public void OnPointerClick( PointerEventData data )
  107. {
  108. // Hide the popup and show the log window
  109. if( !isPopupBeingDragged )
  110. debugManager.ShowLogWindow();
  111. }
  112. // Hides the log window and shows the popup
  113. public void Show()
  114. {
  115. canvasGroup.interactable = true;
  116. canvasGroup.blocksRaycasts = true;
  117. canvasGroup.alpha = 1f;
  118. // Reset the counters
  119. Reset();
  120. // Update position in case resolution changed while hidden
  121. OnViewportDimensionsChanged();
  122. }
  123. // Hide the popup
  124. public void Hide()
  125. {
  126. canvasGroup.interactable = false;
  127. canvasGroup.blocksRaycasts = false;
  128. canvasGroup.alpha = 0f;
  129. isPopupBeingDragged = false;
  130. }
  131. public void OnBeginDrag( PointerEventData data )
  132. {
  133. isPopupBeingDragged = true;
  134. // If a smooth movement animation is in progress, cancel it
  135. if( moveToPosCoroutine != null )
  136. {
  137. StopCoroutine( moveToPosCoroutine );
  138. moveToPosCoroutine = null;
  139. }
  140. }
  141. // Reposition the popup
  142. public void OnDrag( PointerEventData data )
  143. {
  144. popupTransform.position = data.position;
  145. }
  146. // Smoothly translate the popup to the nearest edge
  147. public void OnEndDrag( PointerEventData data )
  148. {
  149. int screenWidth = Screen.width;
  150. int screenHeight = Screen.height;
  151. Vector3 pos = popupTransform.position;
  152. // Find distances to all four edges
  153. float distToLeft = pos.x;
  154. float distToRight = Mathf.Abs( pos.x - screenWidth );
  155. float distToBottom = Mathf.Abs( pos.y );
  156. float distToTop = Mathf.Abs( pos.y - screenHeight );
  157. float horDistance = Mathf.Min( distToLeft, distToRight );
  158. float vertDistance = Mathf.Min( distToBottom, distToTop );
  159. // Find the nearest edge's coordinates
  160. if( horDistance < vertDistance )
  161. {
  162. if( distToLeft < distToRight )
  163. pos = new Vector3( halfSize.x, pos.y, 0f );
  164. else
  165. pos = new Vector3( screenWidth - halfSize.x, pos.y, 0f );
  166. pos.y = Mathf.Clamp( pos.y, halfSize.y, screenHeight - halfSize.y );
  167. }
  168. else
  169. {
  170. if( distToBottom < distToTop )
  171. pos = new Vector3( pos.x, halfSize.y, 0f );
  172. else
  173. pos = new Vector3( pos.x, screenHeight - halfSize.y, 0f );
  174. pos.x = Mathf.Clamp( pos.x, halfSize.x, screenWidth - halfSize.x );
  175. }
  176. // If another smooth movement animation is in progress, cancel it
  177. if( moveToPosCoroutine != null )
  178. StopCoroutine( moveToPosCoroutine );
  179. // Smoothly translate the popup to the specified position
  180. moveToPosCoroutine = MoveToPosAnimation( pos );
  181. StartCoroutine( moveToPosCoroutine );
  182. isPopupBeingDragged = false;
  183. }
  184. }
  185. }