Files
2026-05-21 16:04:49 +02:00

116 lines
4.2 KiB
C#

using Cysharp.Threading.Tasks;
using UnityEngine.UIElements;
namespace DanieleMarotta.RiversongCodeShowcase
{
public class HousePanelUIView : UIView<HousePanelModel>
{
private VisualElement _needs;
private Label _notReadyNeedsNotMetLabel;
private Label _notReadyNeedsMetLabel;
private Label _fetchingMaterialsLabel;
private Label _maxedOutLabel;
private VisualElement _upgradeMaterials;
public override UniTask InitializeAsync(UIService uiService, VisualElement rootElement)
{
base.InitializeAsync(uiService, rootElement);
_needs = rootElement.Q<VisualElement>(className: "house-panel__needs");
_notReadyNeedsNotMetLabel = rootElement.Q<Label>(className: "house-panel__not-ready-needs-not-met");
_notReadyNeedsMetLabel = rootElement.Q<Label>(className: "house-panel__not-ready-needs-met");
_fetchingMaterialsLabel = rootElement.Q<Label>(className: "house-panel__fetching-materials");
_maxedOutLabel = rootElement.Q<Label>(className: "house-panel__maxed-out");
_upgradeMaterials = rootElement.Q<VisualElement>(className: "house-panel__upgrade-materials");
_ = AnimateHouseUpgradeStatusAsync();
return UniTask.CompletedTask;
}
protected override void OnModelChanged()
{
base.OnModelChanged();
_ = CreateNeedViewsAsync();
_ = CreateUpgradeMaterialViewsAsync();
UpdateUpgradeState();
}
private async UniTask CreateNeedViewsAsync()
{
var template = UIService.TemplateLibrary.BuildingPanel.Need;
await UIService.CreateViews<HousePanelNeedModel, HousePanelNeedUIView>(Model.Needs, _needs, template);
}
private async UniTask CreateUpgradeMaterialViewsAsync()
{
var template = UIService.TemplateLibrary.BuildingPanel.UpgradeMaterial;
await UIService.CreateViews<HousePanelUpgradeMaterialModel, HousePanelUpgradeMaterialUIView>(Model.UpgradeMaterials, _upgradeMaterials, template);
}
private void UpdateUpgradeState()
{
_notReadyNeedsNotMetLabel.style.display = DisplayStyle.None;
_notReadyNeedsMetLabel.style.display = DisplayStyle.None;
_fetchingMaterialsLabel.style.display = DisplayStyle.None;
_maxedOutLabel.style.display = DisplayStyle.None;
switch (Model.UpgradeState)
{
case TierUpgradeState.NotReady:
_notReadyNeedsNotMetLabel.style.display = Model.AllNeedsMet ? DisplayStyle.None : DisplayStyle.Flex;
_notReadyNeedsMetLabel.style.display = Model.AllNeedsMet ? DisplayStyle.Flex : DisplayStyle.None;
break;
case TierUpgradeState.FetchingMaterials:
case TierUpgradeState.AllMaterialsFetched:
_fetchingMaterialsLabel.style.display = DisplayStyle.Flex;
break;
case TierUpgradeState.MaxedOut:
_maxedOutLabel.style.display = DisplayStyle.Flex;
break;
}
}
protected override void OnModelPropertyChanged(object sender, BindablePropertyChangedEventArgs e)
{
base.OnModelPropertyChanged(sender, e);
switch (e.propertyName)
{
case nameof(HousePanelModel.AllNeedsMet):
case nameof(HousePanelModel.UpgradeState):
UpdateUpgradeState();
break;
}
}
private async UniTask AnimateHouseUpgradeStatusAsync()
{
_fetchingMaterialsLabel.text = _fetchingMaterialsLabel.text.Replace(".", string.Empty);
var i = 0;
while (_fetchingMaterialsLabel.panel != null)
{
await UniTask.WaitForSeconds(1, true);
var text = _fetchingMaterialsLabel.text;
text = text.Substring(0, text.Length - i);
i = (i + 1) % 4;
if (i > 0) text += new string('.', i);
_fetchingMaterialsLabel.text = text;
}
}
}
}