138 lines
4.1 KiB
C#
138 lines
4.1 KiB
C#
using GeometryTD.CustomEvent;
|
|
using GeometryTD.Definition;
|
|
using GameFramework.Event;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public sealed partial class ShopFormController : UIFormControllerCommonBase<ShopFormContext, ShopForm>
|
|
{
|
|
private ShopFormUseCase _useCase;
|
|
|
|
protected override UIFormType UIFormTypeId => UIFormType.ShopForm;
|
|
|
|
protected override void RefreshUI(ShopForm form, ShopFormContext context)
|
|
{
|
|
form.RefreshUI(context);
|
|
}
|
|
|
|
protected override void SubscribeCustomEvents()
|
|
{
|
|
GameEntry.Event.Subscribe(ShopPurchaseRequestedEventArgs.EventId, OnPurchaseRequested);
|
|
GameEntry.Event.Subscribe(ShopExitRequestedEventArgs.EventId, OnExitRequested);
|
|
GameEntry.Event.Subscribe(ShopInventoryRequestedEventArgs.EventId, OnInventoryRequested);
|
|
}
|
|
|
|
protected override void UnsubscribeCustomEvents()
|
|
{
|
|
GameEntry.Event.Unsubscribe(ShopPurchaseRequestedEventArgs.EventId, OnPurchaseRequested);
|
|
GameEntry.Event.Unsubscribe(ShopExitRequestedEventArgs.EventId, OnExitRequested);
|
|
GameEntry.Event.Unsubscribe(ShopInventoryRequestedEventArgs.EventId, OnInventoryRequested);
|
|
}
|
|
|
|
public override int? OpenUI(object userData = null)
|
|
{
|
|
if (userData is ShopFormContext context)
|
|
{
|
|
return OpenUIInternal(context);
|
|
}
|
|
|
|
if (userData is ShopFormRawData rawDataFromUserData)
|
|
{
|
|
return OpenUI(rawDataFromUserData);
|
|
}
|
|
|
|
if (userData != null)
|
|
{
|
|
Log.Warning("ShopFormController.OpenUI() userData type is invalid.");
|
|
return null;
|
|
}
|
|
|
|
if (_useCase == null)
|
|
{
|
|
Log.Error("ShopFormController.OpenUI() useCase is null.");
|
|
return null;
|
|
}
|
|
|
|
ShopFormRawData rawData = _useCase.CreateInitialModel();
|
|
return OpenUI(rawData);
|
|
}
|
|
|
|
public int? OpenUI(ShopFormRawData rawData)
|
|
{
|
|
ShopFormContext context = BuildContext(rawData);
|
|
return OpenUIInternal(context);
|
|
}
|
|
|
|
public override void BindUseCase(IUIUseCase useCase)
|
|
{
|
|
if (!(useCase is ShopFormUseCase shopFormUseCase))
|
|
{
|
|
Log.Error("ShopFormController.BindUseCase() useCase is invalid.");
|
|
return;
|
|
}
|
|
|
|
_useCase = shopFormUseCase;
|
|
}
|
|
|
|
private void OnPurchaseRequested(object sender, GameEventArgs e)
|
|
{
|
|
if (!IsEventFromCurrentForm(sender) || !(e is ShopPurchaseRequestedEventArgs args) || _useCase == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!_useCase.TryPurchase(args.GoodsIndex, out ShopFormRawData rawData))
|
|
{
|
|
return;
|
|
}
|
|
|
|
SetContext(BuildContext(rawData));
|
|
RefreshCurrentUI();
|
|
}
|
|
|
|
private void OnExitRequested(object sender, GameEventArgs e)
|
|
{
|
|
if (!IsEventFromCurrentForm(sender) || !(e is ShopExitRequestedEventArgs))
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.ShopNode.EndShop();
|
|
}
|
|
|
|
private void OnInventoryRequested(object sender, GameEventArgs e)
|
|
{
|
|
if (!IsEventFromCurrentForm(sender) || !(e is ShopInventoryRequestedEventArgs))
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.UIRouter.OpenUI(UIFormType.RepoForm);
|
|
}
|
|
|
|
private bool IsEventFromCurrentForm(object sender)
|
|
{
|
|
if (Form == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (ReferenceEquals(sender, Form))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (sender is Component component)
|
|
{
|
|
ShopForm ownerForm = component.GetComponentInParent<ShopForm>();
|
|
return ownerForm == Form;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|