Files
ft/Client/Assets/Scripts/UI/PartnerGather/ScrollItems/EventPartnerGatherScrollViewAutoMatch.cs
2026-06-29 21:18:33 +08:00

141 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using asap.core;
using EnhancedUI.EnhancedScroller;
using EventPartnerGather;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
namespace UI.PartnerGather.ScrollItems
{
public class EventPartnerGatherScrollViewAutoMatch : EnhancedScrollerCellView
{
public BtnSwitch btnSwitch;
public GameObject bgLight;
public GameObject textTab1;
public GameObject textTab2;
public Button btnOff;
private CompositeDisposable _disposables = new();
private AutoMatchState _state;
private Button _btnSwitchButton;
private bool _hostReady;
/// <summary>
/// 宿主在 GetCellView 绑定后调用:应用存档/业务状态并解锁交互。列表回收复用后每次展示都会 OnEnable 加锁,须再次调用。
/// </summary>
public void ApplyHostBinding(AutoMatchState state)
{
ELog($"ApplyHostBinding state={state}");
_hostReady = true;
_state = state;
UpdateState(_state);
SetInteractionLocked(false);
// BtnSwitch.OnEnable 每轮会 AddListener回收复用后叠多条GetCellView 在 OnEnable 之后调用,此处收口为单条
if (btnSwitch)
btnSwitch.RemoveAndReAddOnListener();
}
private void Awake()
{
if (btnSwitch)
_btnSwitchButton = btnSwitch.GetComponent<Button>();
}
private void OnEnable()
{
_hostReady = false;
SetInteractionLocked(true);
}
private void SetInteractionLocked(bool locked)
{
if (_btnSwitchButton)
_btnSwitchButton.interactable = !locked;
if (btnOff)
btnOff.interactable = !locked;
}
private void Start()
{
btnOff.onClick.AddListener(OnClickOff);
if (btnSwitch)
btnSwitch.onToggle.AddListener(OnSwitch);
GContext.OnEvent<EventTryCloseAutoMatchResult>().Subscribe(OnCloseAutoMatchResult).AddTo(_disposables);
}
private void OnDestroy()
{
_disposables?.Dispose();
_disposables = null;
}
private void OnSwitch(bool isOn)
{
if (!_hostReady)
return;
ChangeStateAndNotify(isOn ? AutoMatchState.AutoMatchOn : AutoMatchState.AutoMatchOff);
}
private void OnClickOff()
{
if (!_hostReady)
return;
GContext.Publish(new EventTryCloseAutoMatch());
}
private void OnCloseAutoMatchResult(EventTryCloseAutoMatchResult e)
{
ELog($"OnCloseAutoMatchResult canClose={e.IsCanClose}");
if (!_hostReady)
return;
if (e.IsCanClose)
{
ChangeStateAndNotify(AutoMatchState.AutoMatchOff);
}
}
private void ChangeStateAndNotify(AutoMatchState state)
{
if (_state != state)
{
_state = state;
UpdateState(_state);
GContext.Publish(new EventForAutoMatchChangeStage { State = _state });
}
}
/// <summary>
/// 仅刷新显示,不发布事件(宿主与全局事件回调使用)。
/// </summary>
public void SetupState(AutoMatchState state)
{
ELog($"SetupState state={state}");
_state = state;
UpdateState(_state);
}
private void UpdateState(AutoMatchState state)
{
ELog($"UpdateState state={state}");
var isOn = state == AutoMatchState.AutoMatchOn;
btnOff.gameObject.SetActive(isOn);
btnSwitch.SetState(isOn);
bgLight.SetActive(isOn);
textTab1.SetActive(isOn);
textTab2?.SetActive(!isOn);
}
#region Log
private void ELog(string message)
{
#if UNITY_EDITOR
Debug.Log($"<color=cyan> EventPartnerGatherScrollViewAutoMatch -> {gameObject.name} {message} </color>");
#endif
}
#endregion
}
}