using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace MoreMountains.Tools { /// /// A class used to handle the display of a debug log tab in a MMDebugMenu /// public class MMDebugMenuDebugTab : MonoBehaviour { /// the scrollrect where the log will be displayed public ScrollRect DebugScrollRect; /// the text container public Text DebugText; /// the prompt input public InputField CommandPrompt; /// a decorative prompt character public Text CommandPromptCharacter; /// whether or not the touch screen is visible public bool TouchScreenVisible = false; protected TouchScreenKeyboard _touchScreenKeyboard; protected RectTransform _rectTransform; protected float _mobileMenuOffset = -1000f; protected bool _touchScreenVisibleLastFrame; /// /// On awake we prepare our prompt listener /// protected virtual void Awake() { MMDebug.MMDebugLogEvent.Register(OnMMDebugLogEvent); DebugText.text = ""; _rectTransform = this.gameObject.GetComponent(); CommandPrompt.onEndEdit.AddListener(val => { CommandPrompt.text = ""; if (val != "") { MMDebug.DebugLogCommand(val); } }); } /// /// if the mobile touchscreen is open, we move away /// protected virtual void Update() { TouchScreenVisible = TouchScreenKeyboard.visible; if (TouchScreenVisible) { _rectTransform.MMSetBottom(650f); } else { _rectTransform.MMSetBottom(0f); } } /// /// on late update we scroll to the bottom if needed /// protected virtual void LateUpdate() { if (_touchScreenVisibleLastFrame != TouchScreenVisible) { StartCoroutine(ScrollToLogBottomCo()); } _touchScreenVisibleLastFrame = TouchScreenVisible; } /// /// Scrolls to the bottom on enable /// protected virtual void OnEnable() { StartCoroutine(ScrollToLogBottomCo()); } /// /// when we get a new log event, we update our text and scroll to the bottom /// /// protected virtual void OnMMDebugLogEvent(MMDebug.DebugLogItem item) { DebugText.text = MMDebug.LogHistoryText; if (this.gameObject.activeInHierarchy) { StartCoroutine(ScrollToLogBottomCo()); } } /// /// A coroutine used to scroll to the bottom /// /// protected virtual IEnumerator ScrollToLogBottomCo() { yield return new WaitForEndOfFrame(); DebugScrollRect.normalizedPosition = Vector2.zero; CommandPrompt.ActivateInputField(); CommandPrompt.Select(); } /// /// Stops listening for events /// public virtual void OnDestroy() { MMDebug.MMDebugLogEvent.Unregister(OnMMDebugLogEvent); } } }