AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/AppMain/UI/XMBTopGroupUI/ControllerInfo.cs
ALIENJACK\alien 02693523a3 Merge branch 'master' of http://git.axibug.com/sin365/AxibugEmuOnline
# Conflicts:
#	AxibugEmuOnline.Client/Assets/Script/AppMain/UI/OverlayUI/OverlayManager.cs
2024-12-25 18:23:48 +08:00

131 lines
4.3 KiB
C#

using AxibugEmuOnline.Client;
using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.Event;
using System;
using UnityEngine;
using UnityEngine.UI;
using VirtualNes.Core;
public class ControllerInfo : MonoBehaviour
{
[Header("手柄槽位序号[0,3]")]
[SerializeField]
int m_slotIndex;
[SerializeField]
GameObject m_connectInfoNode;
[SerializeField]
Image m_indexIcon;
[SerializeField]
Text m_playerName;
int m_localJoyIndex;
bool m_isLocal;
public int SlotIndex
{
get => m_slotIndex;
set
{
if (m_slotIndex == value) return;
m_slotIndex = value;
UpdateIndexIcon();
}
}
private void Awake()
{
UpdateIndexIcon();
}
private void OnEnable()
{
Eventer.Instance.RegisterEvent(EEvent.OnMineJoinRoom, OnJoinRoom);
Eventer.Instance.RegisterEvent(EEvent.OnMineRoomCreated, OnMineRoomCreated);
Eventer.Instance.RegisterEvent(EEvent.OnMineLeavnRoom, OnLeaveRoom);
Eventer.Instance.RegisterEvent(EEvent.OnLoginSucceed, OnLoginSuccess);
Eventer.Instance.RegisterEvent(EEvent.OnLossLoginState, OnLossLoginState);
Eventer.Instance.RegisterEvent(EEvent.OnControllerConnectChanged, OnControlConnectChanged);
UpdateConnectInfo();
}
private void OnDisable()
{
Eventer.Instance.UnregisterEvent(EEvent.OnMineJoinRoom, OnJoinRoom);
Eventer.Instance.UnregisterEvent(EEvent.OnMineRoomCreated, OnMineRoomCreated);
Eventer.Instance.UnregisterEvent(EEvent.OnMineLeavnRoom, OnLeaveRoom);
Eventer.Instance.UnregisterEvent(EEvent.OnLoginSucceed, OnLoginSuccess);
Eventer.Instance.UnregisterEvent(EEvent.OnLossLoginState, OnLossLoginState);
Eventer.Instance.UnregisterEvent(EEvent.OnControllerConnectChanged, OnControlConnectChanged);
}
private void OnMineRoomCreated() => UpdateConnectInfo();
private void OnJoinRoom() => UpdateConnectInfo();
private void OnLeaveRoom() => UpdateConnectInfo();
private void OnLoginSuccess() => UpdateConnectInfo();
private void OnLossLoginState() => UpdateConnectInfo();
private void OnControlConnectChanged() => UpdateConnectInfo();
private void UpdateConnectInfo()
{
if (App.roomMgr.InRoom)
{
var slotInfo = App.roomMgr.mineRoomMiniInfo.GamePlaySlotList[SlotIndex];
if (slotInfo.PlayerUID <= 0)
SetDisconnect();
else
UpdateStateView(App.user.userdata.UID == slotInfo.PlayerUID, slotInfo.PlayerNickName, slotInfo.PlayerLocalJoyIdx);
}
else
{
var connecter = Supporter.GetControllerSetuper();
if (connecter == null)
{
SetDisconnect();
return;
}
var localControlIndex = connecter.GetSlotConnectingController(SlotIndex);
if (localControlIndex == null)
SetDisconnect();
else
{
if (App.user.IsLoggedIn)
UpdateStateView(true, App.user.userdata.NickName, localControlIndex.Value);
else
UpdateStateView(true, "Player", localControlIndex.Value);
}
}
}
private void UpdateStateView(bool isLocal, string playerName, int slotIndex)
{
m_localJoyIndex = slotIndex;
m_isLocal = isLocal;
m_connectInfoNode.SetActiveEx(true);
m_playerName.text = playerName;
}
private void SetDisconnect()
{
m_localJoyIndex = -1;
m_isLocal = false;
m_connectInfoNode.SetActiveEx(false);
m_playerName.text = null;
}
private void UpdateIndexIcon()
{
switch (SlotIndex)
{
case 0: m_indexIcon.sprite = Resources.Load<Sprite>("UIImage/JoyImg/P1"); break;
case 1: m_indexIcon.sprite = Resources.Load<Sprite>("UIImage/JoyImg/P2"); break;
case 2: m_indexIcon.sprite = Resources.Load<Sprite>("UIImage/JoyImg/P3"); break;
case 3: m_indexIcon.sprite = Resources.Load<Sprite>("UIImage/JoyImg/P4"); break;
}
}
}