130 lines
4.0 KiB
C#
130 lines
4.0 KiB
C#
using GeometryTD.Definition;
|
|
using GeometryTD.CustomEvent;
|
|
using GameFramework.Event;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class EventFormController : UIFormControllerCommonBase<EventFormContext, EventForm>
|
|
{
|
|
private EventFormUseCase m_UseCase;
|
|
|
|
protected override UIFormType UIFormTypeId => UIFormType.EventForm;
|
|
|
|
protected override void RefreshUI(EventForm form, EventFormContext context)
|
|
{
|
|
form.RefreshUI(context);
|
|
}
|
|
|
|
protected override void SubscribeCustomEvents()
|
|
{
|
|
GameEntry.Event.Subscribe(EventOptionItemSelectedEventArgs.EventId, OnEventOptionItemSelected);
|
|
}
|
|
|
|
protected override void UnsubscribeCustomEvents()
|
|
{
|
|
GameEntry.Event.Unsubscribe(EventOptionItemSelectedEventArgs.EventId, OnEventOptionItemSelected);
|
|
}
|
|
|
|
public override int? OpenUI(object userData = null)
|
|
{
|
|
if (userData is EventFormContext eventFormContext)
|
|
{
|
|
return OpenUIInternal(eventFormContext);
|
|
}
|
|
|
|
if (userData is EventFormRawData rawDataFromUserData)
|
|
{
|
|
return OpenUI(rawDataFromUserData);
|
|
}
|
|
|
|
if (userData != null)
|
|
{
|
|
Log.Warning("EventFormController.OpenUI() userData type is invalid.");
|
|
return null;
|
|
}
|
|
|
|
if (m_UseCase == null)
|
|
{
|
|
Log.Error("EventFormController.OpenUI() useCase is null.");
|
|
return null;
|
|
}
|
|
|
|
EventFormRawData rawData = m_UseCase.CreateInitialModel();
|
|
return OpenUI(rawData);
|
|
}
|
|
|
|
public int? OpenUI(EventFormRawData rawData)
|
|
{
|
|
EventFormContext context = BuildContext(rawData);
|
|
return OpenUIInternal(context);
|
|
}
|
|
|
|
public override void BindUseCase(IUIUseCase useCase)
|
|
{
|
|
if (!(useCase is EventFormUseCase eventFormUseCase))
|
|
{
|
|
Log.Error("EventFormController.BindUseCase() useCase is invalid.");
|
|
return;
|
|
}
|
|
|
|
m_UseCase = eventFormUseCase;
|
|
}
|
|
|
|
private static EventFormContext BuildContext(EventFormRawData rawData)
|
|
{
|
|
if (rawData == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
EventOptionItemContext[] options = new EventOptionItemContext[4];
|
|
for (int i = 0; i < options.Length; i++)
|
|
{
|
|
string optionText = string.Empty;
|
|
bool isSelectable = false;
|
|
string blockedReason = string.Empty;
|
|
if (rawData.OptionItems != null && i < rawData.OptionItems.Length &&
|
|
rawData.OptionItems[i] != null)
|
|
{
|
|
optionText = rawData.OptionItems[i].OptionText;
|
|
isSelectable = rawData.OptionItems[i].IsSelectable;
|
|
blockedReason = rawData.OptionItems[i].BlockedReason;
|
|
}
|
|
|
|
options[i] = new EventOptionItemContext
|
|
{
|
|
OptionIndex = i,
|
|
OptionText = optionText,
|
|
IsSelectable = isSelectable,
|
|
BlockedReason = blockedReason
|
|
};
|
|
}
|
|
|
|
return new EventFormContext
|
|
{
|
|
EventId = rawData.EventId,
|
|
Title = rawData.Title,
|
|
Description = rawData.Description,
|
|
OptionItems = options
|
|
};
|
|
}
|
|
|
|
private void OnEventOptionItemSelected(object sender, GameEventArgs e)
|
|
{
|
|
if (!(e is EventOptionItemSelectedEventArgs args))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (m_UseCase == null)
|
|
{
|
|
Log.Warning("EventFormController.OnOptionSelected() useCase is null.");
|
|
return;
|
|
}
|
|
|
|
m_UseCase.TrySelectOption(args.SelectedItemId);
|
|
}
|
|
}
|
|
}
|