Files

162 lines
4.7 KiB
C#
Raw Permalink Normal View History

2022-12-04 19:19:07 +02:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2022-12-05 03:15:48 +02:00
using TMPro;
2022-12-04 19:19:07 +02:00
namespace SharedGame
{
public class GameInterface : MonoBehaviour
{
public int maxLogLines = 20;
2022-12-05 03:15:48 +02:00
public TextMeshProUGUI txtGameLog;
public TextMeshProUGUI txtPluginLog;
public TextMeshProUGUI txtIdlePerc;
public TextMeshProUGUI txtUpdatePerc;
public TextMeshProUGUI txtNowChecksum;
public TextMeshProUGUI txtPeriodicChecksum;
2022-12-04 19:19:07 +02:00
public Button btnPlayer1;
public Button btnPlayer2;
public Button btnConnect;
public Toggle tglPluginLog;
public Toggle tglGameLog;
public bool gameLog = true;
public bool pluginLog = true;
private GameManager gameManager => GameManager.Instance;
private readonly List<string> gameLogs = new List<string>();
private readonly List<string> pluginLogs = new List<string>();
private void Awake()
{
gameManager.OnStatus += OnStatus;
gameManager.OnRunningChanged += OnRunningChanged;
GGPORunner.OnPluginLog += OnPluginLog;
GGPORunner.OnGameLog += OnGameLog;
btnConnect.onClick.AddListener(OnConnect);
btnPlayer1.onClick.AddListener(OnPlayer1);
btnPlayer2.onClick.AddListener(OnPlayer2);
tglPluginLog.isOn = false;
tglGameLog.isOn = false;
tglPluginLog.onValueChanged.AddListener(OnTogglePluginLog);
tglGameLog.onValueChanged.AddListener(OnToggleGameLog);
SetConnectText("");
}
private void OnDestroy()
{
2022-12-05 16:04:05 +02:00
if (gameManager != null)
{
gameManager.OnStatus -= OnStatus;
gameManager.OnRunningChanged -= OnRunningChanged;
}
2022-12-04 19:19:07 +02:00
GGPORunner.OnPluginLog -= OnPluginLog;
GGPORunner.OnGameLog -= OnGameLog;
btnConnect.onClick.RemoveListener(OnConnect);
btnPlayer1.onClick.RemoveListener(OnPlayer1);
btnPlayer2.onClick.RemoveListener(OnPlayer2);
tglPluginLog.onValueChanged.RemoveListener(OnTogglePluginLog);
tglGameLog.onValueChanged.RemoveListener(OnToggleGameLog);
}
private void OnRunningChanged(bool obj)
{
SetConnectText(obj ? "Shutdown" : "--");
}
private void OnToggleGameLog(bool value)
{
if (tglGameLog.isOn)
{
txtGameLog.text = string.Join("\n", gameLogs);
}
txtGameLog.gameObject.SetActive(tglGameLog.isOn);
}
private void OnTogglePluginLog(bool value)
{
if (tglPluginLog.isOn)
{
txtPluginLog.text = string.Join("\n", gameLogs);
}
txtPluginLog.gameObject.SetActive(tglPluginLog.isOn);
}
2022-12-11 00:28:21 +02:00
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha5))
{
OnConnect();
}
}
2022-12-04 19:19:07 +02:00
private void SetConnectText(string text)
{
2022-12-05 03:15:48 +02:00
btnConnect.GetComponentInChildren<TextMeshProUGUI>().text = text;
2022-12-04 19:19:07 +02:00
}
private void OnGameLog(string text)
{
if (gameLog)
{
2022-12-05 19:45:10 +02:00
//Debug.Log("[GameLog] " + text);
2022-12-04 19:19:07 +02:00
}
gameLogs.Insert(0, text);
while (gameLogs.Count > maxLogLines)
{
gameLogs.RemoveAt(gameLogs.Count - 1);
}
if (tglGameLog.isOn)
{
txtGameLog.text = string.Join("\n", gameLogs);
}
}
private void OnPluginLog(string text)
{
if (pluginLog)
{
2022-12-05 19:45:10 +02:00
//Debug.Log("[PluginLog] " + text);
2022-12-04 19:19:07 +02:00
}
pluginLogs.Insert(0, text);
while (pluginLogs.Count > maxLogLines)
{
pluginLogs.RemoveAt(gameLogs.Count - 1);
}
if (tglPluginLog.isOn)
{
txtPluginLog.text = string.Join("\n", pluginLogs);
}
}
private void OnPlayer1()
{
gameManager.DisconnectPlayer(0);
}
private void OnPlayer2()
{
gameManager.DisconnectPlayer(1);
}
private void OnConnect()
{
if (gameManager.IsRunning)
{
gameManager.Shutdown();
}
}
private void OnStatus(StatusInfo status)
{
txtIdlePerc.text = status.idlePerc.ToString();
txtUpdatePerc.text = status.updatePerc.ToString();
txtNowChecksum.text = status.now.ToString();
txtPeriodicChecksum.text = status.periodic.ToString();
}
}
}