- 规范 UI 设计成为:

- UIForm 负责展示
    - Controller 负责控制
    - UseCase 负责业务逻辑
    - RawData 负责提供原始数据
    - Context 负责为 UIForm 提供数据
- 添加和补充一系列 UI 功能
This commit is contained in:
SepComet
2026-02-16 19:03:09 +08:00
parent d650dd63e7
commit 85b0205c73
251 changed files with 12705 additions and 5220 deletions
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: c13ac6879bf568a42b12d798738efd84
guid: e3e1fd66709232c439fe452b81dffab6
folderAsset: yes
DefaultImporter:
externalObjects: {}
@@ -0,0 +1,8 @@
namespace UI
{
public class RoleItemContext : UIContext
{
public int RoleId;
public string IconName;
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 3ec6493d4e6c45d489e1dd3d5c6228a6
guid: 0337fd9faf4a5b44eb95858bff44c015
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -0,0 +1,8 @@
namespace UI
{
public class RolePropertyAreaContext : UIContext
{
public string RoleName;
public string InitialPropertyText;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f37335b27f20c647a0a6175226c0b45
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
namespace UI
{
public class SelectRoleFormContext : UIContext
{
public RoleItemContext[] RoleItemContexts;
public RolePropertyAreaContext RolePropertyAreaContext;
public int[] RoleIds;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6c34d5aa87925ee41b70887958e7611c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,6 @@
namespace UI
{
public class StartMenuFormContext : UIContext
{
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 77a32872255d9f84b9332418d1352514
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 70ae7a701eac84b4eb4ec27bf2ada3c1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,293 @@
using CustomEvent;
using Definition.Enum;
using GameFramework.Event;
using UnityGameFramework.Runtime;
namespace UI
{
public class SelectRoleFormController : UIFormControllerBase<SelectRoleFormContext>
{
private SelectRoleFormUseCase _useCase;
private SelectRoleFormContext _context;
private SelectRoleForm _selectRoleForm;
private int? _selectRoleFormSerialId;
private bool _pendingRefresh;
private bool _isBindEvent;
private void SubscribeEvents()
{
if (_isBindEvent) return;
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
GameEntry.Event.Subscribe(MenuSelectRoleReturnEventArgs.EventId, OnMenuSelectRoleReturn);
GameEntry.Event.Subscribe(MenuSelectRoleSelectedEventArgs.EventId, OnMenuSelectRoleSelected);
GameEntry.Event.Subscribe(MenuSelectRoleConfirmEventArgs.EventId, OnMenuSelectRoleConfirm);
_isBindEvent = true;
}
private void UnsubscribeEvents()
{
if (!_isBindEvent) return;
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
GameEntry.Event.Unsubscribe(MenuSelectRoleReturnEventArgs.EventId, OnMenuSelectRoleReturn);
GameEntry.Event.Unsubscribe(MenuSelectRoleSelectedEventArgs.EventId, OnMenuSelectRoleSelected);
GameEntry.Event.Unsubscribe(MenuSelectRoleConfirmEventArgs.EventId, OnMenuSelectRoleConfirm);
_isBindEvent = false;
}
private static SelectRoleFormContext BuildContext(SelectRoleFormRawData rawData)
{
if (rawData == null)
{
return null;
}
int count = rawData.RoleIds?.Length ?? 0;
RoleItemContext[] roleItems = new RoleItemContext[count];
for (int i = 0; i < count; i++)
{
string iconName = rawData.RoleIconNames != null && i < rawData.RoleIconNames.Length
? rawData.RoleIconNames[i]
: null;
roleItems[i] = new RoleItemContext
{
RoleId = rawData.RoleIds[i],
IconName = iconName
};
}
RolePropertyAreaContext propertyContext = null;
if (rawData.SelectedRoleId >= 0)
{
propertyContext = new RolePropertyAreaContext
{
RoleName = rawData.SelectedRoleName,
InitialPropertyText = rawData.SelectedRoleInitialPropertyText
};
}
return new SelectRoleFormContext
{
RoleItemContexts = roleItems,
RolePropertyAreaContext = propertyContext,
RoleIds = rawData.RoleIds
};
}
#region UI Methods
protected override int? OpenUIInternal(SelectRoleFormContext context)
{
if (context == null)
{
Log.Warning("SelectRoleFormController.OpenUI() context is null.");
return null;
}
_context = context;
if (_selectRoleForm != null && _selectRoleFormSerialId.HasValue &&
GameEntry.UI.HasUIForm(_selectRoleFormSerialId.Value))
{
_selectRoleForm.RefreshUI(_context);
return _selectRoleFormSerialId;
}
CloseUI();
_pendingRefresh = true;
SubscribeEvents();
_selectRoleFormSerialId = GameEntry.UI.OpenUIForm(UIFormType.SelectRoleForm, context);
return _selectRoleFormSerialId;
}
public override int? OpenUI(object userData = null)
{
if (userData is SelectRoleFormContext selectRoleFormContext)
{
return OpenUIInternal(selectRoleFormContext);
}
if (userData != null)
{
Log.Warning("SelectRoleFormController.OpenUI() userData type is invalid.");
return null;
}
SelectRoleFormRawData rawData = _useCase.CreateModel();
SelectRoleFormContext context = BuildContext(rawData);
return OpenUIInternal(context);
}
public override void CloseUI()
{
_pendingRefresh = false;
UnsubscribeEvents();
if (_selectRoleFormSerialId.HasValue)
{
if (GameEntry.UI.HasUIForm(_selectRoleFormSerialId.Value))
{
GameEntry.UI.CloseUIForm(_selectRoleFormSerialId.Value);
}
_selectRoleForm = null;
_selectRoleFormSerialId = null;
return;
}
if (_selectRoleForm != null)
{
_selectRoleForm.Close();
_selectRoleForm = null;
}
}
public override void BindUseCase(IUIUseCase useCase)
{
if (!(useCase is SelectRoleFormUseCase selectRoleUseCase))
{
Log.Error("SelectRoleUseCase.BindUseCase() useCase is invalid.");
return;
}
_useCase = selectRoleUseCase;
}
private void TryRefreshUI()
{
if (_context == null)
{
return;
}
if (_selectRoleForm == null)
{
_pendingRefresh = true;
return;
}
_selectRoleForm.RefreshUI(_context);
_pendingRefresh = false;
}
#endregion
#region Service
public void UpdateShowRole(RolePropertyAreaContext rolePropertyAreaContext)
{
if (_context != null)
{
_context.RolePropertyAreaContext = rolePropertyAreaContext;
}
if (_selectRoleForm != null)
{
_selectRoleForm.UpdateShowRole(rolePropertyAreaContext);
}
}
#endregion
#region Event Handlers
private void OpenUIFormSuccess(object sender, GameEventArgs e)
{
if (!(e is OpenUIFormSuccessEventArgs args))
{
return;
}
if (!_selectRoleFormSerialId.HasValue)
{
return;
}
if (args.UIForm == null || args.UIForm.SerialId != _selectRoleFormSerialId.Value ||
args.UserData != _context)
{
return;
}
_selectRoleForm = args.UIForm.Logic as SelectRoleForm;
if (_selectRoleForm == null)
{
Log.Warning("SelectRoleFormController open success but form logic is invalid.");
return;
}
if (_pendingRefresh)
{
TryRefreshUI();
}
}
private void CloseUIFormComplete(object sender, GameEventArgs e)
{
if (!(e is CloseUIFormCompleteEventArgs args))
{
return;
}
if (args.SerialId != _selectRoleFormSerialId)
{
return;
}
_selectRoleForm = null;
_selectRoleFormSerialId = null;
}
private void OnMenuSelectRoleReturn(object sender, GameEventArgs e)
{
if (!(sender is SelectRoleForm) || !(e is MenuSelectRoleReturnEventArgs))
{
return;
}
CloseUI();
}
private void OnMenuSelectRoleSelected(object sender, GameEventArgs e)
{
if (!(e is MenuSelectRoleSelectedEventArgs args))
{
return;
}
SelectRoleFormRawData rawData = _useCase != null ? _useCase.SelectRole(args.RoleId) : null;
SelectRoleFormContext context = BuildContext(rawData);
if (context == null)
{
return;
}
_context = context;
UpdateShowRole(_context.RolePropertyAreaContext);
}
private void OnMenuSelectRoleConfirm(object sender, GameEventArgs e)
{
if (!(e is MenuSelectRoleConfirmEventArgs))
{
return;
}
_useCase.ConfirmSelectedRole();
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ff46cd6b508b0304a801223cbae79419
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,276 @@
using CustomEvent;
using Definition.Enum;
using GameFramework.Event;
using UnityGameFramework.Runtime;
namespace UI
{
public class StartMenuFormController : UIFormControllerBase<StartMenuFormContext>
{
private StartMenuFormContext _context;
private StartMenuForm _startMenuForm;
private int? _startMenuFormSerialId;
private bool _pendingRefresh;
private bool _isBindEvent;
private void SubscribeEvents()
{
if (_isBindEvent) return;
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
GameEntry.Event.Subscribe(MenuStartGameEventArgs.EventId, OnMenuStartGameButtonClick);
GameEntry.Event.Subscribe(MenuFileButtonClickEventArgs.EventId, OnMenuFileButtonClick);
GameEntry.Event.Subscribe(MenuGuideButtonClickEventArgs.EventId, OnMenuGuideButtonClick);
GameEntry.Event.Subscribe(MenuSettingButtonClickEventArgs.EventId, OnMenuSettingButtonClick);
GameEntry.Event.Subscribe(MenuQuitButtonClickEventArgs.EventId, OnMenuQuitButtonClick);
GameEntry.Event.Subscribe(MenuAboutButtonClickEventArgs.EventId, OnMenuAboutButtonClick);
_isBindEvent = true;
}
private void UnsubscribeEvents()
{
if (!_isBindEvent) return;
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
GameEntry.Event.Unsubscribe(MenuStartGameEventArgs.EventId, OnMenuStartGameButtonClick);
GameEntry.Event.Unsubscribe(MenuFileButtonClickEventArgs.EventId, OnMenuFileButtonClick);
GameEntry.Event.Unsubscribe(MenuGuideButtonClickEventArgs.EventId, OnMenuGuideButtonClick);
GameEntry.Event.Unsubscribe(MenuSettingButtonClickEventArgs.EventId, OnMenuSettingButtonClick);
GameEntry.Event.Unsubscribe(MenuQuitButtonClickEventArgs.EventId, OnMenuQuitButtonClick);
GameEntry.Event.Unsubscribe(MenuAboutButtonClickEventArgs.EventId, OnMenuAboutButtonClick);
_isBindEvent = false;
}
private static StartMenuFormContext BuildStartMenuFormContext()
{
return new StartMenuFormContext();
}
#region UI Methods
protected override int? OpenUIInternal(StartMenuFormContext context)
{
if (context == null)
{
Log.Warning("StartMenuFormController.OpenUI() context is null.");
return null;
}
_context = context;
if (_startMenuForm != null && _startMenuFormSerialId.HasValue &&
GameEntry.UI.HasUIForm(_startMenuFormSerialId.Value))
{
_startMenuForm.RefreshUI(_context);
return _startMenuFormSerialId;
}
CloseUI();
_pendingRefresh = true;
SubscribeEvents();
_startMenuFormSerialId = GameEntry.UI.OpenUIForm(UIFormType.StartMenuForm, context);
return _startMenuFormSerialId;
}
public override int? OpenUI(object userData = null)
{
if (userData is StartMenuFormContext context)
{
return OpenUIInternal(context);
}
if (userData != null)
{
Log.Warning("StartMenuFormController.OpenUI() userData type is invalid.");
return null;
}
return OpenUIInternal(BuildStartMenuFormContext());
}
public int? OpenUI(StartMenuFormContext context)
{
return OpenUIInternal(context);
}
public override void CloseUI()
{
_pendingRefresh = false;
UnsubscribeEvents();
if (_startMenuFormSerialId.HasValue)
{
if (GameEntry.UI.HasUIForm(_startMenuFormSerialId.Value))
{
GameEntry.UI.CloseUIForm(_startMenuFormSerialId.Value);
}
_startMenuForm = null;
_startMenuFormSerialId = null;
return;
}
if (_startMenuForm != null)
{
_startMenuForm.Close();
_startMenuForm = null;
}
}
public override void BindUseCase(IUIUseCase useCase)
{
Log.Info("StartMenuForm doesn't need UseCase");
}
private void TryRefreshUI()
{
if (_context == null)
{
return;
}
if (_startMenuForm == null)
{
_pendingRefresh = true;
return;
}
_startMenuForm.RefreshUI(_context);
_pendingRefresh = false;
}
#endregion
#region Event Handlers
private void OpenUIFormSuccess(object sender, GameEventArgs e)
{
if (!(e is OpenUIFormSuccessEventArgs args))
{
return;
}
if (!_startMenuFormSerialId.HasValue)
{
return;
}
if (args.UIForm == null || args.UIForm.SerialId != _startMenuFormSerialId.Value ||
args.UserData != _context)
{
return;
}
_startMenuForm = args.UIForm.Logic as StartMenuForm;
if (_startMenuForm == null)
{
Log.Warning("StartMenuFormController open success but form logic is invalid.");
return;
}
if (_pendingRefresh)
{
TryRefreshUI();
}
}
private void CloseUIFormComplete(object sender, GameEventArgs e)
{
if (!(e is CloseUIFormCompleteEventArgs args))
{
return;
}
if (args.SerialId != _startMenuFormSerialId)
{
return;
}
_startMenuForm = null;
_startMenuFormSerialId = null;
}
private void OnMenuStartGameButtonClick(object sender, GameEventArgs e)
{
if (!(sender is StartMenuForm) || !(e is MenuStartGameEventArgs))
{
return;
}
GameEntry.UIRouter.OpenUI(UIFormType.SelectRoleForm);
}
private void OnMenuFileButtonClick(object sender, GameEventArgs e)
{
if (!(sender is StartMenuForm) || !(e is MenuFileButtonClickEventArgs))
{
return;
}
Log.Warning("Menu file button click is not implemented.");
}
private void OnMenuGuideButtonClick(object sender, GameEventArgs e)
{
if (!(sender is StartMenuForm) || !(e is MenuGuideButtonClickEventArgs))
{
return;
}
Log.Warning("Menu guide button click is not implemented.");
}
private void OnMenuSettingButtonClick(object sender, GameEventArgs e)
{
if (!(sender is StartMenuForm) || !(e is MenuSettingButtonClickEventArgs))
{
return;
}
GameEntry.UIRouter.OpenUI(UIFormType.SettingForm);
}
private void OnMenuQuitButtonClick(object sender, GameEventArgs e)
{
if (!(sender is StartMenuForm) || !(e is MenuQuitButtonClickEventArgs))
{
return;
}
GameEntry.UIRouter.OpenUI(UIFormType.DialogForm, new DialogFormRawData
{
Mode = 2,
Title = GameEntry.Localization.GetString("AskQuitGame.Title"),
Message = GameEntry.Localization.GetString("AskQuitGame.Message"),
OnClickConfirm = delegate(object userData)
{
UnityGameFramework.Runtime.GameEntry.Shutdown(ShutdownType.Quit);
}
});
}
private void OnMenuAboutButtonClick(object sender, GameEventArgs e)
{
if (!(sender is StartMenuForm) || !(e is MenuAboutButtonClickEventArgs))
{
return;
}
Log.Warning("Menu about button click is not implemented.");
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3feee66cf220f6f47b147bca4f7967f4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 614e7dc72ab04ce8aaa7e750088f12d6
timeCreated: 1771144270
@@ -0,0 +1,11 @@
namespace UI
{
public class SelectRoleFormRawData
{
public int[] RoleIds;
public string[] RoleIconNames;
public int SelectedRoleId;
public string SelectedRoleName;
public string SelectedRoleInitialPropertyText;
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5c23c1db541a47b09b8c475018758f9e
timeCreated: 1771144321
@@ -0,0 +1,7 @@
namespace UI
{
public class StartMenuFormRawData
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7f5cdd950661442fa1760bc2b2db6f2d
timeCreated: 1771144299
@@ -1,50 +0,0 @@
using DataTable;
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class RoleItem : MonoBehaviour
{
[SerializeField] private CommonButton _button = null;
[SerializeField] private Image _roleImage = null;
private SelectRoleForm _selectRoleForm = null;
private DRRole _role = null;
public void OnInit(SelectRoleForm selectRoleForm, DRRole role)
{
this._selectRoleForm = selectRoleForm;
this._role = role;
GameEntry.SpriteCache.GetSprite(role.IconName, sprite => _roleImage.sprite = sprite);
_button.RegisterAction(0, OnPointerEnter);
_button.RegisterAction(1, OnPointerClick);
_button.RegisterAction(2, OnPointerExit);
}
public void OnReset()
{
this._selectRoleForm = null;
this._role = null;
_button.UnRegisterAction(0, OnPointerEnter);
_button.UnRegisterAction(1, OnPointerClick);
_button.UnRegisterAction(2, OnPointerExit);
}
private void OnPointerEnter()
{
_selectRoleForm.UpdateShowRole(_role);
}
private void OnPointerClick()
{
_selectRoleForm.StartGame();
}
private void OnPointerExit()
{
_selectRoleForm.UpdateShowRole(null);
}
}
}
@@ -1,53 +0,0 @@
using System.Text;
using DataTable;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class RolePropertyArea : MonoBehaviour
{
[SerializeField] private TMP_Text _roleNameText;
[SerializeField] private RawImage _roleAvatar;
[SerializeField] private TMP_Text _roleInitialPropertyText;
[SerializeField] private Transform _roleInitialItemParent;
public void OnInit(DRRole role)
{
_roleNameText.text = role.RoleName;
// _roleAvatar.texture = AssetUtility.GetUITextureIcon(role.IconName);
StringBuilder sb = new StringBuilder();
if (role.InitialProperties != null)
{
foreach (var modifier in role.InitialProperties)
{
sb.Append($"{modifier.ToString()}\n");
}
}
_roleInitialPropertyText.text = sb.ToString();
// foreach (var item in role.InitialItemIds)
// {
// var itemGO = Instantiate(_roleInitialItemParent.GetChild(0), _roleInitialItemParent);
// itemGO.GetComponent<RoleItem>().OnInit(item.Key, item.Value);
// }
}
public void OnReset(object userData = null)
{
// _roleNameText.text = string.Empty;
// _roleAvatar.texture = null;
// _roleInitialPropertyText.text = string.Empty;
// foreach (Transform item in _roleInitialItemParent)
// {
// Destroy(item.gameObject);
// }
}
}
}
@@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelectRoleShowArea : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
@@ -1,71 +0,0 @@
using Definition.Enum;
using Procedure;
using UnityGameFramework.Runtime;
namespace UI
{
public class StartMenuForm : UGuiForm
{
private ProcedureStartMenu _procedureStartMenu = null;
public void OnStartButtonClick()
{
_procedureStartMenu.OpenSelectForm();
}
public void OnFileButtonClick()
{
throw new System.NotImplementedException();
}
public void OnGuideButtonClick()
{
throw new System.NotImplementedException();
}
public void OnSettingButtonClick()
{
GameEntry.UI.OpenUIForm(UIFormType.SettingForm);
}
public void OnQuitButtonClick()
{
GameEntry.UI.OpenDialog(new DialogParams()
{
Mode = 2,
Title = GameEntry.Localization.GetString("AskQuitGame.Title"),
Message = GameEntry.Localization.GetString("AskQuitGame.Message"),
OnClickConfirm = delegate(object userData)
{
UnityGameFramework.Runtime.GameEntry.Shutdown(ShutdownType.Quit);
},
});
}
public void OnAboutButtonClick()
{
throw new System.NotImplementedException();
}
#region Lifecycle
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
if (userData is ProcedureStartMenu procedure)
{
_procedureStartMenu = procedure;
}
}
protected override void OnClose(bool isShutdown, object userData)
{
_procedureStartMenu = null;
base.OnClose(isShutdown, userData);
}
#endregion
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ceb1fe7ab4fa37d4ca15992227229df6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,99 @@
using System.Text;
using DataTable;
using GameFramework.DataTable;
using Procedure;
using UnityEngine;
namespace UI
{
public class SelectRoleFormUseCase : IUIUseCase
{
private readonly IDataTable<DRRole> _roleDataTable;
private readonly ProcedureStartMenu _procedureStartMenu;
public int SelectedRoleId { get; private set; }
public SelectRoleFormUseCase(ProcedureStartMenu procedureStartMenu)
{
_procedureStartMenu = procedureStartMenu;
_roleDataTable = GameEntry.DataTable.GetDataTable<DRRole>();
}
public SelectRoleFormRawData CreateModel()
{
return BuildModel(0);
}
public SelectRoleFormRawData SelectRole(int roleId)
{
SelectedRoleId = roleId;
return BuildModel(roleId);
}
public bool ConfirmSelectedRole()
{
DRRole[] roles = _roleDataTable != null ? _roleDataTable.GetAllDataRows() : null;
if (roles == null || roles.Length == 0)
{
return false;
}
if (SelectedRoleId < 0 || _roleDataTable.GetDataRow(SelectedRoleId) == null)
{
int randomIndex = Random.Range(0, roles.Length);
SelectedRoleId = roles[randomIndex].Id;
}
bool result = SelectedRoleId >= 0;
if (result)
{
_procedureStartMenu.StartGame(SelectedRoleId);
}
return result;
}
private SelectRoleFormRawData BuildModel(int selectedRoleId)
{
DRRole[] roles = _roleDataTable.GetAllDataRows();
int count = _roleDataTable.Count;
int[] roleIds = new int[count];
string[] iconNames = new string[count];
for (int i = 0; i < count; i++)
{
roleIds[i] = roles[i].Id;
iconNames[i] = roles[i].IconName;
}
DRRole selectedRole = selectedRoleId > 0 ? _roleDataTable.GetDataRow(selectedRoleId) : null;
return new SelectRoleFormRawData
{
RoleIds = roleIds,
RoleIconNames = iconNames,
SelectedRoleId = selectedRole?.Id ?? -1,
SelectedRoleName = selectedRole?.RoleName,
SelectedRoleInitialPropertyText = selectedRole != null
? BuildRoleInitialPropertyText(selectedRole)
: null
};
}
private static string BuildRoleInitialPropertyText(DRRole role)
{
if (role == null || role.InitialProperties == null || role.InitialProperties.Length == 0)
{
return string.Empty;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < role.InitialProperties.Length; i++)
{
sb.Append(role.InitialProperties[i]);
sb.Append('\n');
}
return sb.ToString();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aec2db2225cb4bb890eb347e73fc5890
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 424c12088ffa5fc43bdf642094e15ae4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,40 @@
using CustomEvent;
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class RoleItem : MonoBehaviour
{
[SerializeField] private Image _roleImage = null;
private RoleItemContext _context = null;
public void OnInit(RoleItemContext context)
{
_context = context;
if (_context == null || string.IsNullOrEmpty(_context.IconName))
{
return;
}
GameEntry.SpriteCache.GetSprite(_context.IconName, sprite => _roleImage.sprite = sprite);
}
public void OnReset()
{
_context = null;
}
public void UpdateShowRole()
{
int roleId = _context?.RoleId ?? -1;
GameEntry.Event.Fire(this, MenuSelectRoleSelectedEventArgs.Create(roleId));
}
public void OnConfirmRoleClick()
{
GameEntry.Event.Fire(this, MenuSelectRoleConfirmEventArgs.Create());
}
}
}
@@ -0,0 +1,37 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class RolePropertyArea : MonoBehaviour
{
[SerializeField] private TMP_Text _roleNameText;
[SerializeField] private RawImage _roleAvatar;
[SerializeField] private TMP_Text _roleInitialPropertyText;
[SerializeField] private Transform _roleInitialItemParent;
private RolePropertyAreaContext _context;
public void OnInit(RolePropertyAreaContext context)
{
_context = context;
if (_context == null)
{
OnReset();
return;
}
_roleNameText.text = _context.RoleName;
_roleInitialPropertyText.text = _context.InitialPropertyText;
}
public void OnReset(object userData = null)
{
_context = null;
}
}
}
@@ -1,7 +1,5 @@
using System.Linq;
using DataTable;
using GameFramework.DataTable;
using Procedure;
using System;
using CustomEvent;
using UnityEngine;
using UnityGameFramework.Runtime;
@@ -15,52 +13,61 @@ namespace UI
[SerializeField] private RolePropertyArea _propertyArea = null;
private DRRole _currentRole = null;
private IDataTable<DRRole> _roleDataTable = null;
private DRRole[] _roleList = null;
private SelectRoleFormContext _context = null;
private RoleItem[] _roleItems = null;
private ProcedureStartMenu _procedure;
public void RefreshUI(SelectRoleFormContext context)
{
_context = context;
int itemIndex = 1, roleIndex = 0;
while (itemIndex < _roleItems.Length)
{
if (roleIndex < _context.RoleItemContexts.Length)
{
_roleItems[itemIndex].OnInit(_context.RoleItemContexts[roleIndex]);
_roleItems[itemIndex].gameObject.SetActive(true);
}
else
{
_roleItems[itemIndex].gameObject.SetActive(false);
_roleItems[itemIndex].OnReset();
}
itemIndex++;
roleIndex++;
}
UpdateShowRole(_context.RolePropertyAreaContext);
}
#region FSM
protected override void OnInit(object userData)
{
base.OnInit(userData);
if (userData is ProcedureStartMenu procedure)
{
_procedure = procedure;
}
_roleDataTable = GameEntry.DataTable.GetDataTable<DRRole>();
_roleItems = this.transform.GetComponentsInChildren<RoleItem>();
_roleList = _roleDataTable.GetAllDataRows();
}
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
Log.Debug($"{_roleList.Length} || {_roleItems.Length}");
for (int i = 0; i < _roleItems.Length; i++)
if (userData is SelectRoleFormContext context)
{
if (i < _roleList.Length)
{
_roleItems[i].gameObject.SetActive(true);
_roleItems[i].OnInit(this, _roleList[i]);
}
else
{
_roleItems[i].gameObject.SetActive(false);
}
RefreshUI(context);
return;
}
Log.Warning("SelectRoleForm requires SelectRoleFormContext as userData.");
}
protected override void OnClose(bool isShutdown, object userData)
{
base.OnClose(isShutdown, userData);
_context = null;
foreach (var item in _roleItems)
{
item.OnReset();
@@ -69,16 +76,9 @@ namespace UI
#endregion
public void OnReturnButtonClick()
public void UpdateShowRole(RolePropertyAreaContext propertyAreaContext)
{
GameEntry.UI.CloseUIForm(this);
}
public void UpdateShowRole(DRRole role)
{
_currentRole = role;
if (role == null)
if (propertyAreaContext == null)
{
_randomRoleCard.SetActive(true);
_roleCard.SetActive(false);
@@ -87,20 +87,14 @@ namespace UI
else
{
_randomRoleCard.SetActive(false);
_propertyArea.OnInit(role);
_propertyArea.OnInit(propertyAreaContext);
_roleCard.SetActive(true);
}
}
public void StartGame()
public void OnReturnButtonClick()
{
if (_currentRole == null)
{
int roleIndex = Random.Range(0, _roleList.Length);
_currentRole = _roleList[roleIndex];
}
_procedure?.StartGame(_currentRole.Id);
GameEntry.Event.Fire(this, MenuSelectRoleReturnEventArgs.Create());
}
}
}
@@ -0,0 +1,64 @@
using CustomEvent;
using UnityGameFramework.Runtime;
namespace UI
{
public class StartMenuForm : UGuiForm
{
public void RefreshUI(StartMenuFormContext context)
{
}
#region Lifecycle
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
if (userData is StartMenuFormContext context)
{
RefreshUI(context);
return;
}
Log.Warning("StartMenuForm requires StartMenuFormContext as userData.");
}
protected override void OnClose(bool isShutdown, object userData)
{
base.OnClose(isShutdown, userData);
}
#endregion
public void OnStartButtonClick()
{
GameEntry.Event.Fire(this, MenuStartGameEventArgs.Create());
}
public void OnFileButtonClick()
{
GameEntry.Event.Fire(this, MenuFileButtonClickEventArgs.Create());
}
public void OnGuideButtonClick()
{
GameEntry.Event.Fire(this, MenuGuideButtonClickEventArgs.Create());
}
public void OnSettingButtonClick()
{
GameEntry.Event.Fire(this, MenuSettingButtonClickEventArgs.Create());
}
public void OnQuitButtonClick()
{
GameEntry.Event.Fire(this, MenuQuitButtonClickEventArgs.Create());
}
public void OnAboutButtonClick()
{
GameEntry.Event.Fire(this, MenuAboutButtonClickEventArgs.Create());
}
}
}