迁移剩下的 UI 到 Serialization 方式
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.AsyncTask;
|
||||
using SepCore.Definition;
|
||||
using SepCore.Entity;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public enum DisplayListAreaType : byte
|
||||
{
|
||||
None = 0,
|
||||
Prop = 1,
|
||||
Weapon = 2
|
||||
}
|
||||
|
||||
public partial class ShopController
|
||||
{
|
||||
private async UniTask<ShopContext> BuildContext(ShopRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
Log.Error("ShopFormController.BuildContext() rawData is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_rawData = rawData;
|
||||
|
||||
List<GoodsItemContext> goodsItems = new List<GoodsItemContext>();
|
||||
foreach (var item in rawData.GoodsItems)
|
||||
{
|
||||
var context = await CreateGoodsItemContextAsync(item);
|
||||
goodsItems.Add(context);
|
||||
}
|
||||
|
||||
return new ShopContext(rawData)
|
||||
{
|
||||
GoodsItems = goodsItems,
|
||||
PropListContext =
|
||||
BuildDisplayListContext(DisplayListAreaType.Prop, rawData.PropItems, rawData.PropMaxCount),
|
||||
WeaponListContext = BuildDisplayListContext(DisplayListAreaType.Weapon, rawData.WeaponItems,
|
||||
rawData.WeaponMaxCount),
|
||||
NeedRefreshGoodsItems = true,
|
||||
NeedRefreshPlayerCoin = true,
|
||||
NeedRefreshPropList = true,
|
||||
NeedRefreshWeaponList = true,
|
||||
NeedRefreshPrice = true
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayListContext BuildDisplayListContext(DisplayListAreaType listType,
|
||||
IReadOnlyList<object> items, int maxCount)
|
||||
{
|
||||
DisplayItemContext[] itemContexts = new DisplayItemContext[items.Count];
|
||||
switch (listType)
|
||||
{
|
||||
case DisplayListAreaType.Weapon:
|
||||
IReadOnlyList<WeaponBase> weapons = (IReadOnlyList<WeaponBase>)items;
|
||||
for (int i = 0; i < weapons.Count; i++)
|
||||
{
|
||||
itemContexts[i] = BuildWeaponItem(weapons[i]);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DisplayListAreaType.Prop:
|
||||
IReadOnlyList<PropItem> propItems = (IReadOnlyList<PropItem>)items;
|
||||
for (int i = 0; i < propItems.Count; i++)
|
||||
{
|
||||
itemContexts[i] = BuildPropItem(propItems[i]);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return new DisplayListContext
|
||||
{
|
||||
CurrentCount = itemContexts.Length,
|
||||
MaxCount = maxCount,
|
||||
ItemContexts = itemContexts
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayItemContext BuildPropItem(PropItem propItem)
|
||||
{
|
||||
return new DisplayItemContext
|
||||
{
|
||||
IconAssetName = propItem.IconAssetName,
|
||||
Rarity = propItem.Rarity,
|
||||
IsWeapon = false
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayItemContext BuildWeaponItem(WeaponBase weaponBase)
|
||||
{
|
||||
return new DisplayItemContext
|
||||
{
|
||||
IconAssetName = weaponBase.WeaponData.IconAssetName,
|
||||
Rarity = weaponBase.WeaponData.Rarity,
|
||||
IsWeapon = true
|
||||
};
|
||||
}
|
||||
|
||||
private static void AppendDisplayItemContext(DisplayListContext listContext, DisplayItemContext newItem)
|
||||
{
|
||||
int oldCount = listContext.ItemContexts.Length;
|
||||
DisplayItemContext[] newContexts = new DisplayItemContext[oldCount + 1];
|
||||
if (oldCount > 0)
|
||||
{
|
||||
System.Array.Copy(listContext.ItemContexts, newContexts, oldCount);
|
||||
}
|
||||
|
||||
newContexts[oldCount] = newItem;
|
||||
listContext.ItemContexts = newContexts;
|
||||
listContext.CurrentCount = oldCount + 1;
|
||||
}
|
||||
|
||||
private async UniTask<GoodsItemContext> CreateGoodsItemContextAsync(GoodsItemRawData rawData,
|
||||
float timeout = 30f)
|
||||
{
|
||||
var context = new GoodsItemContext(rawData);
|
||||
context.Icon = context.ItemType switch
|
||||
{
|
||||
ItemType.Weapon => await SpriteLoader.LoadSpriteByKeyAsync(rawData.DataRow switch
|
||||
{
|
||||
WeaponConfig weaponConfig => weaponConfig.Sprite,
|
||||
WeaponData weaponData => weaponData.IconAssetName,
|
||||
_ => string.Empty
|
||||
}),
|
||||
ItemType.Prop => await SpriteLoader.LoadSpriteByKeyAsync(((PropConfig)rawData.DataRow).Sprite),
|
||||
_ => null
|
||||
};
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user