268 lines
7.9 KiB
C#
268 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class PlayersGUI : MonoBehaviour
|
|
{
|
|
public enum State
|
|
{
|
|
FRIENDS = 0,
|
|
PLAYERS = 1,
|
|
STATS = 2
|
|
}
|
|
|
|
public Button friendsBtn;
|
|
|
|
public Button playersBtn;
|
|
|
|
[Header("Stats")]
|
|
public GameObject userStatsParent;
|
|
|
|
public SteamUserWidget steamUserWidget;
|
|
|
|
[Header("Friends")]
|
|
public GameObject friendsParent;
|
|
|
|
public FriendListWidget friendListWidgetPrefab;
|
|
|
|
public Transform friendListWidgetsParent;
|
|
|
|
public ScrollRect scrollRectWindow;
|
|
|
|
public float scrollWidgetsOffset = 100f;
|
|
|
|
private Vector2 scrollParentStartPos = Vector2.zero;
|
|
|
|
[Header("Players")]
|
|
public GameObject playersParent;
|
|
|
|
public PlayerListWidget playerListWidgetPrefab;
|
|
|
|
public Transform playersListWidgetsParent;
|
|
|
|
public ScrollRect playersScrollRectWindow;
|
|
|
|
public float playersScrollWidgetsOffset = 100f;
|
|
|
|
private Vector2 playersScrollParentStartPos = Vector2.zero;
|
|
|
|
[ReadOnly]
|
|
public State currentState;
|
|
|
|
[ReadOnly]
|
|
public State prevState;
|
|
|
|
public List<FriendListWidget> friendsWidgetsList = new List<FriendListWidget>();
|
|
|
|
public List<PlayerListWidget> playersWidgetsList = new List<PlayerListWidget>();
|
|
|
|
private void Start()
|
|
{
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if ((bool)SteamStatsManager.Instance)
|
|
{
|
|
if (PhotonNetwork.room != null)
|
|
{
|
|
ChangeState(State.PLAYERS);
|
|
}
|
|
else
|
|
{
|
|
ChangeState(State.FRIENDS);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ChangeState(State state)
|
|
{
|
|
if (state == State.PLAYERS && PhotonNetwork.room == null)
|
|
{
|
|
state = State.FRIENDS;
|
|
}
|
|
prevState = currentState;
|
|
currentState = state;
|
|
switch (state)
|
|
{
|
|
case State.FRIENDS:
|
|
RefreshFriends();
|
|
break;
|
|
case State.PLAYERS:
|
|
RefreshPlayers();
|
|
break;
|
|
case State.STATS:
|
|
steamUserWidget.showInviteBtn = prevState == State.FRIENDS;
|
|
steamUserWidget.Refresh();
|
|
break;
|
|
}
|
|
userStatsParent.SetActive(state == State.STATS);
|
|
friendsParent.SetActive(state == State.FRIENDS);
|
|
playersParent.SetActive(state == State.PLAYERS);
|
|
friendsBtn.gameObject.SetActive(PhotonNetwork.room != null && state != State.STATS);
|
|
playersBtn.gameObject.SetActive(PhotonNetwork.room != null && state != State.STATS);
|
|
}
|
|
|
|
public void ChangeState(string stateName)
|
|
{
|
|
switch (stateName)
|
|
{
|
|
case "FRIENDS":
|
|
ChangeState(State.FRIENDS);
|
|
break;
|
|
case "STATS":
|
|
ChangeState(State.STATS);
|
|
break;
|
|
case "PLAYERS":
|
|
ChangeState(State.PLAYERS);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void SetPrevState()
|
|
{
|
|
ChangeState(prevState);
|
|
}
|
|
|
|
public void RefreshFriends(bool reload = true)
|
|
{
|
|
if (!SteamStatsManager.Instance)
|
|
{
|
|
return;
|
|
}
|
|
if (reload)
|
|
{
|
|
SteamStatsManager.Instance.UpdateFriendsList();
|
|
}
|
|
scrollParentStartPos = scrollRectWindow.content.localPosition;
|
|
Canvas.ForceUpdateCanvases();
|
|
scrollRectWindow.verticalScrollbar.value = 1f;
|
|
Canvas.ForceUpdateCanvases();
|
|
foreach (FriendListWidget friendsWidgets in friendsWidgetsList)
|
|
{
|
|
UnityEngine.Object.Destroy(friendsWidgets.gameObject);
|
|
}
|
|
friendsWidgetsList.Clear();
|
|
int num = 0;
|
|
for (int i = 0; i < SteamStatsManager.Instance.friendsList.Count; i++)
|
|
{
|
|
FriendListWidget friendListWidget = UnityEngine.Object.Instantiate(friendListWidgetPrefab);
|
|
friendListWidget.transform.SetParent(friendListWidgetsParent);
|
|
friendListWidget.GetComponent<RectTransform>().localPosition = Vector3.zero + new Vector3(0f, (float)num * (0f - scrollWidgetsOffset), 0f);
|
|
friendListWidget.GetComponent<RectTransform>().localRotation = Quaternion.identity;
|
|
friendListWidget.GetComponent<RectTransform>().localScale = Vector3.one;
|
|
friendListWidget.playersGUI = this;
|
|
friendListWidget.steamUser = SteamStatsManager.Instance.friendsList[i];
|
|
friendListWidget.RefreshWidget();
|
|
friendsWidgetsList.Add(friendListWidget);
|
|
num++;
|
|
}
|
|
scrollRectWindow.content.sizeDelta = new Vector2(scrollRectWindow.content.sizeDelta.x, scrollWidgetsOffset * (float)num + scrollWidgetsOffset * 0f);
|
|
scrollRectWindow.content.localPosition = scrollParentStartPos;
|
|
scrollRectWindow.verticalNormalizedPosition = 1f;
|
|
}
|
|
|
|
public void RefreshPlayers()
|
|
{
|
|
if (!SteamStatsManager.Instance)
|
|
{
|
|
return;
|
|
}
|
|
playersScrollParentStartPos = playersScrollRectWindow.content.localPosition;
|
|
Canvas.ForceUpdateCanvases();
|
|
playersScrollRectWindow.verticalScrollbar.value = 1f;
|
|
Canvas.ForceUpdateCanvases();
|
|
foreach (PlayerListWidget playersWidgets in playersWidgetsList)
|
|
{
|
|
UnityEngine.Object.Destroy(playersWidgets.gameObject);
|
|
}
|
|
playersWidgetsList.Clear();
|
|
int num = 0;
|
|
for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
|
|
{
|
|
if (PhotonNetwork.playerList[i].ID != -1 && !GlobalSettings.Instance.CheckCrackPhotonPlayer(PhotonNetwork.playerList[i]))
|
|
{
|
|
PlayerListWidget playerListWidget = UnityEngine.Object.Instantiate(playerListWidgetPrefab);
|
|
playerListWidget.transform.SetParent(playersListWidgetsParent);
|
|
RectTransform component = playerListWidget.GetComponent<RectTransform>();
|
|
component.localPosition = Vector3.zero + new Vector3(0f, (float)num * (0f - playersScrollWidgetsOffset), 0f);
|
|
component.localRotation = Quaternion.identity;
|
|
component.localScale = Vector3.one;
|
|
playerListWidget.playersGUI = this;
|
|
playerListWidget.photonPlayer = PhotonNetwork.playerList[i];
|
|
if (PhotonNetwork.playerList[i].ID == PhotonNetwork.player.ID)
|
|
{
|
|
playerListWidget.steamUser = SteamStatsManager.Instance.mySteamUser;
|
|
}
|
|
else if (PhotonNetwork.playerList[i].CustomProperties.ContainsKey("steamId"))
|
|
{
|
|
playerListWidget.steamUser = SteamStatsManager.Instance.CreatePlatformUser(Convert.ToUInt64(PhotonNetwork.playerList[i].CustomProperties["steamId"]));
|
|
}
|
|
SteamStatsManager.Instance.RequestUserStats(playerListWidget.steamUser);
|
|
SteamStatsManager.Instance.RequestUserInformation(playerListWidget.steamUser, false);
|
|
playerListWidget.RefreshWidget();
|
|
playersWidgetsList.Add(playerListWidget);
|
|
num++;
|
|
}
|
|
}
|
|
playersScrollRectWindow.content.sizeDelta = new Vector2(playersScrollRectWindow.content.sizeDelta.x, playersScrollWidgetsOffset * (float)num + playersScrollWidgetsOffset * 0f);
|
|
playersScrollRectWindow.content.localPosition = playersScrollParentStartPos;
|
|
playersScrollRectWindow.verticalNormalizedPosition = 1f;
|
|
}
|
|
|
|
public void RefreshWidgetInfo(SteamStatsManager.SteamUser steamUser)
|
|
{
|
|
if (steamUser == null)
|
|
{
|
|
Debug.LogError("RefreshWidgetInfo steamUser == null");
|
|
}
|
|
else if (currentState == State.FRIENDS)
|
|
{
|
|
for (int i = 0; i < friendsWidgetsList.Count; i++)
|
|
{
|
|
if (friendsWidgetsList[i].steamUser != null && friendsWidgetsList[i].steamUser.platformProfileID == steamUser.platformProfileID)
|
|
{
|
|
friendsWidgetsList[i].nameText.text = steamUser.friendName;
|
|
if ((bool)steamUser.avatarMedium)
|
|
{
|
|
friendsWidgetsList[i].avatarImage.sprite = Sprite.Create(steamUser.avatarMedium, new Rect(0f, 0f, steamUser.avatarMedium.width, steamUser.avatarMedium.height), new Vector2(0.5f, 0.5f));
|
|
}
|
|
else
|
|
{
|
|
friendsWidgetsList[i].avatarImage.sprite = null;
|
|
}
|
|
friendsWidgetsList[i].avatarImage.gameObject.SetActive(friendsWidgetsList[i].avatarImage.sprite != null);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (currentState != State.PLAYERS)
|
|
{
|
|
return;
|
|
}
|
|
for (int j = 0; j < playersWidgetsList.Count; j++)
|
|
{
|
|
if (playersWidgetsList[j].steamUser != null && playersWidgetsList[j].steamUser.platformProfileID == steamUser.platformProfileID)
|
|
{
|
|
playersWidgetsList[j].steamUser = steamUser;
|
|
playersWidgetsList[j].nameText.text = steamUser.friendName;
|
|
if ((bool)steamUser.avatarMedium)
|
|
{
|
|
playersWidgetsList[j].avatarImage.sprite = Sprite.Create(steamUser.avatarMedium, new Rect(0f, 0f, steamUser.avatarMedium.width, steamUser.avatarMedium.height), new Vector2(0.5f, 0.5f));
|
|
}
|
|
else
|
|
{
|
|
playersWidgetsList[j].avatarImage.sprite = null;
|
|
}
|
|
playersWidgetsList[j].avatarImage.gameObject.SetActive(playersWidgetsList[j].avatarImage.sprite != null);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|