Files
2026-02-21 16:45:37 +08:00

207 lines
4.7 KiB
C#

using System.Collections;
using BitStrap;
using UnityEngine;
using UnityEngine.UI;
public class HUDMultiplayer : MonoBehaviour
{
public enum MessageType
{
MY_TEXT = 0,
OTHERS_TEXT = 1,
MY_FISH = 2,
OTHERS_FISH = 3,
SYSTEM_MESSAGE = 4,
COUNT = 5
}
public Text connectionState;
public Text messageText;
public Text statusText;
public InputField chatInputField;
public int chatInputCharLimit = 120;
public Text chatText;
public Text chatUseText;
public Text chatShowHideText;
public int chatLinesLimit = 20;
public float chatScrollSpeed = 1000f;
[ReadOnly]
public bool isInInputMode;
public GameObject chatParent;
public GameObject chatToggleParent;
public Color[] messageColors = new Color[5];
public SteamUserWidget steamUserWidget;
private void Awake()
{
connectionState.text = string.Empty;
messageText.text = string.Empty;
chatInputField.characterLimit = chatInputCharLimit;
EnterInputMode(false);
ShowChat(false);
ShowRemotePlayerInfo(false, null);
}
private void OnEnable()
{
string[] args = new string[1] { UtilitiesInput.GetActionKeyName("CHAT_USE") };
chatUseText.text = string.Format(Utilities.GetTranslation("MULTIPLAYER/CHAT_INFO"), args);
chatShowHideText.text = UtilitiesInput.GetActionKeyName("CHAT_SHOW_HIDE") + " - " + Utilities.GetTranslation("HUD_CONTROLS/CHAT_SHOW_HIDE");
}
public void Reset()
{
chatInputField.text = string.Empty;
chatText.text = string.Empty;
}
private void Update()
{
if ((bool)BugReporter.Instance && BugReporter.Instance.isVisible)
{
return;
}
if (isInInputMode)
{
if (Input.GetKeyDown(KeyCode.Return))
{
SendInputFieldMessage();
}
else if (!chatInputField.isFocused && !VRManager.IsVROn())
{
EnterInputMode(false);
}
}
if (UtilitiesInput.GetButtonDown("CHAT_USE"))
{
GameController.Instance.fishingPlayer.EnterChat(!isInInputMode);
EnterInputMode(!isInInputMode);
}
if (UtilitiesInput.GetButtonDown("CHAT_SHOW_HIDE"))
{
chatToggleParent.SetActive(!chatToggleParent.activeSelf);
}
if (isInInputMode && Input.GetAxis("Mouse ScrollWheel") != 0f)
{
float y = chatText.rectTransform.anchoredPosition.y;
y += chatScrollSpeed * ((!(Input.GetAxis("Mouse ScrollWheel") > 0f)) ? 1f : (-1f)) * Time.deltaTime;
y = Mathf.Clamp(y, 0f - chatText.preferredHeight, 0f);
chatText.rectTransform.anchoredPosition = new Vector3(0f, y, 0f);
}
}
public void ShowChat(bool show)
{
chatParent.SetActive(show);
}
public void SetConnectionState(string state)
{
connectionState.text = "Network: " + state;
}
public void SetStatusText(string status)
{
if ((bool)statusText)
{
statusText.text = status;
}
}
public void ShowMessage(string message, float time)
{
StartCoroutine(ShowMessageCoroutine(message, time));
}
private IEnumerator ShowMessageCoroutine(string message, float time)
{
messageText.text = message;
yield return new WaitForSeconds(time);
messageText.text = string.Empty;
}
public void EnterInputMode(bool enter)
{
if (enter)
{
chatInputField.gameObject.SetActive(true);
chatInputField.interactable = true;
chatInputField.ActivateInputField();
}
else
{
chatInputField.DeactivateInputField();
chatInputField.interactable = false;
chatInputField.gameObject.SetActive(false);
}
isInInputMode = enter;
}
public void SendInputFieldMessage()
{
if (chatInputField.text != string.Empty)
{
MultiplayerManager.Instance.multiplayerChat.SendText(chatInputField.text);
chatInputField.text = string.Empty;
chatInputField.ActivateInputField();
chatText.rectTransform.anchoredPosition = Vector3.zero;
}
}
public void ReceiveChatMessage(string text, MessageType messageType)
{
string[] array = chatText.text.Split('\n');
if (array.Length > chatLinesLimit)
{
chatText.text = chatText.text.Remove(0, array[0].Length + 1);
}
string text2 = ColorUtility.ToHtmlStringRGB(messageColors[(int)messageType]);
text = "<color=#" + text2 + ">" + text + "</color>";
switch (messageType)
{
}
if (chatText.text.Length == 0)
{
chatText.text += text;
return;
}
Text text3 = chatText;
text3.text = text3.text + "\n" + text;
}
public void ShowRemotePlayerInfo(bool show, FishingPlayerRemote fishingPlayerRemote)
{
if (show && fishingPlayerRemote == null)
{
Debug.LogError("ShowRemotePlayerInfo fishingPlayerRemote == null");
return;
}
if (show)
{
steamUserWidget.steamUser = fishingPlayerRemote.steamUser;
steamUserWidget.photonPlayer = fishingPlayerRemote.photonView.owner;
steamUserWidget.fishingPlayerRemote = fishingPlayerRemote;
steamUserWidget.Refresh();
}
else
{
fishingPlayerRemote = null;
}
steamUserWidget.gameObject.SetActive(show);
}
}