69 lines
1.5 KiB
C#
69 lines
1.5 KiB
C#
using System.Text;
|
|
using SzafaKit.Foundation;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace SzafaKit.Console
|
|
{
|
|
public class ConsoleUI : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private RectTransform _root;
|
|
|
|
[SerializeField]
|
|
private ScrollRect _scrollRect;
|
|
|
|
[SerializeField]
|
|
private TMP_InputField _input;
|
|
|
|
[SerializeField]
|
|
private TextMeshProUGUI _messages;
|
|
|
|
private StringBuilder _content = new();
|
|
|
|
public bool IsVisible => _root.gameObject.activeSelf;
|
|
|
|
private void Awake()
|
|
{
|
|
_input.onSubmit.AddListener(OnSubmit);
|
|
}
|
|
|
|
private void OnSubmit(string input)
|
|
{
|
|
_input.text = "";
|
|
SzafaEngine.Shared.GetService<ConsoleService>().Execute(input);
|
|
|
|
_input.Select();
|
|
}
|
|
|
|
public void SetVisibility(bool visible)
|
|
{
|
|
_root.gameObject.SetActive(visible);
|
|
|
|
if (visible)
|
|
{
|
|
_input.Select();
|
|
}
|
|
}
|
|
|
|
public void Write(string message)
|
|
{
|
|
bool scrollDown = _scrollRect.verticalNormalizedPosition >= 0.01f;
|
|
|
|
_content.Append(message);
|
|
_messages.text = _content.ToString();
|
|
|
|
if (scrollDown)
|
|
{
|
|
_scrollRect.verticalNormalizedPosition = 0.0f;
|
|
}
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_content.Clear();
|
|
_messages.text = "";
|
|
}
|
|
}
|
|
} |