vampire-like/Assets/GameMain/Scripts/Presentation/Main/DisplayItemInfo/DisplayItemInfoController.cs

151 lines
4.6 KiB
C#

using Cysharp.Threading.Tasks;
using SepCore.Event;
using SepCore.Definition;
using GameFramework.Event;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace SepCore.UI
{
public class DisplayItemInfoController : UIControllerBase<DisplayItemInfoContext, DisplayItemInfoForm>
{
protected override UIFormType UIFormType => UIFormType.DisplayItemInfoForm;
private bool _locked = false;
protected override void SubscribeCustomEvents()
{
GameEntry.Event.Subscribe(DisplayItemInfoLockEventArgs.EventId, DisplayItemInfoLock);
GameEntry.Event.Subscribe(DisplayItemInfoHideEventArgs.EventId, DisplayItemInfoHide);
}
protected override void UnsubscribeCustomEvents()
{
GameEntry.Event.Unsubscribe(DisplayItemInfoLockEventArgs.EventId, DisplayItemInfoLock);
GameEntry.Event.Unsubscribe(DisplayItemInfoHideEventArgs.EventId, DisplayItemInfoHide);
}
protected override void RefreshUI(DisplayItemInfoForm form, DisplayItemInfoContext context)
{
form.RefreshUI(context);
}
protected override void CloseLoadedFormDirect(DisplayItemInfoForm form)
{
GameEntry.UI.CloseUIForm(form);
}
private static DisplayItemInfoContext BuildContext(DisplayItemInfoRawData rawData)
{
if (rawData == null)
{
Log.Error("DisplayItemInfoFormController.BuildContext() rawData is null.");
return null;
}
return new DisplayItemInfoContext
{
Index = rawData.Index,
IconAssetName = rawData.IconAssetName,
Title = rawData.Title,
Rarity = rawData.Rarity,
TypeText = rawData.TypeText,
Description = rawData.Description,
Price = rawData.Price,
IsWeapon = rawData.IsWeapon,
TargetPos = rawData.TargetPos
};
}
public override async UniTask<int?> OpenUIAsync(object userData = null, float timeout = 30f)
{
if (userData is not DisplayItemInfoRawData rawData)
{
if (userData != null)
{
Log.Warning("DisplayItemInfoController.OpenUIAsync() userData type is invalid.");
}
else
{
Log.Warning("DisplayItemInfoController.OpenUIAsync() rawData is required.");
}
return null;
}
DisplayItemInfoContext context = BuildContext(rawData);
if (context == null)
{
Log.Warning("DisplayItemInfoController.OpenUIAsync() rawData is invalid.");
return null;
}
_locked = false;
return await OpenFormAsync(context, timeout);
}
public override async UniTask CloseUIAsync(object userData = null, float timeout = 30f)
{
_locked = false;
await base.CloseUIAsync(userData, timeout);
}
public override void BindUseCase(IUIUseCase useCase)
{
if (useCase is not DisplayItemInfoUseCase)
{
Log.Error("DisplayItemInfoForm.BindUseCase() useCase is invalid.");
}
}
private bool IsCurrentFormSender(object sender)
{
if (sender is DisplayItemInfoForm displayItemInfoForm)
{
return displayItemInfoForm == Form;
}
if (sender is Component component && Form != null)
{
return component.transform.IsChildOf(Form.transform);
}
return false;
}
#region Event Handlers
private void DisplayItemInfoLock(object sender, GameEventArgs e)
{
if (e is not DisplayItemInfoLockEventArgs)
{
return;
}
_locked = true;
}
private void DisplayItemInfoHide(object sender, GameEventArgs e)
{
if (e is not DisplayItemInfoHideEventArgs args)
{
return;
}
if (args.Force)
{
GameEntry.UIRouter.CloseUIAsync(UIFormType.DisplayItemInfoForm).Forget();
_locked = false;
return;
}
if (_locked && !args.Force) return;
GameEntry.UIRouter.CloseUIAsync(UIFormType.DisplayItemInfoForm).Forget();
_locked = false;
}
#endregion
}
}