DebugLogPopup.cs 5.5 KB

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