124 lines
3.7 KiB
C#
124 lines
3.7 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?.EventItem == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
EventOptionItemContext[] options = new EventOptionItemContext[4];
|
|
for (int i = 0; i < options.Length; i++)
|
|
{
|
|
string optionText = string.Empty;
|
|
if (rawData.EventItem.Options != null && i < rawData.EventItem.Options.Length &&
|
|
rawData.EventItem.Options[i] != null)
|
|
{
|
|
optionText = rawData.EventItem.Options[i].OptionText;
|
|
}
|
|
|
|
options[i] = new EventOptionItemContext
|
|
{
|
|
OptionIndex = i,
|
|
OptionText = optionText
|
|
};
|
|
}
|
|
|
|
return new EventFormContext
|
|
{
|
|
EventId = rawData.EventItem.Id,
|
|
Title = rawData.EventItem.Title,
|
|
Description = rawData.EventItem.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.SelectOption(args.SelectedItemId);
|
|
CloseUI();
|
|
}
|
|
}
|
|
} |