Files
Ultimate-Fishing-Simulator-…/Assets/Scripts/Assembly-CSharp/UI_GearShopPanel.cs
2026-03-04 09:37:33 +08:00

101 lines
2.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Michsky.UI.Heat;
using Obvious.Soap;
using UFS3;
using UnityEngine;
public class UI_GearShopPanel : MonoBehaviour
{
public ItemType itemType;
[SerializeField]
private UI_ItemDataButton _ButtonTemplate;
[SerializeField]
private PlayerProfile _PlayerProfile;
[SerializeField]
private Transform _ButtonParent;
[SerializeField]
private IntVariable _Shop_CurrentItemIDSelected;
[SerializeField]
private StringVariable _Shop_Search;
[SerializeField]
private IntVariable _Shop_Sort;
private void OnEnable()
{
_Shop_Search.OnValueChanged += _Shop_Search_OnValueChanged;
_Shop_Sort.OnValueChanged += _Shop_Sort_OnValueChanged;
GetComponentInParent<PanelManager>()?.onPanelChanged.AddListener(PanelManagerOnPanelChanged);
Refresh();
}
private void OnDisable()
{
_Shop_Search.OnValueChanged -= _Shop_Search_OnValueChanged;
_Shop_Sort.OnValueChanged -= _Shop_Sort_OnValueChanged;
GetComponentInParent<PanelManager>()?.onPanelChanged.RemoveListener(PanelManagerOnPanelChanged);
CleanUp();
}
public void CleanUp()
{
UI_ItemDataButton[] componentsInChildren = _ButtonParent.GetComponentsInChildren<UI_ItemDataButton>();
for (int i = 0; i < componentsInChildren.Length; i++)
{
Object.Destroy(componentsInChildren[i].gameObject);
}
}
private void _Shop_Search_OnValueChanged(string obj)
{
Refresh();
}
private void _Shop_Sort_OnValueChanged(int obj)
{
Refresh();
}
private void PanelManagerOnPanelChanged(int index)
{
Refresh();
}
public void Refresh()
{
PanelManager componentInParent = GetComponentInParent<PanelManager>();
if (componentInParent.panels[componentInParent.currentPanelIndex].panelObject.gameObject != base.gameObject)
{
return;
}
_Shop_CurrentItemIDSelected.Value = -1;
CleanUp();
IEnumerable<BaseItemData> enumerable = _PlayerProfile.ItemDatabase.Items.Where((BaseItemData item) => ((IItemType)item).ItemType == itemType);
string search = _Shop_Search.Value;
if (!string.IsNullOrEmpty(search))
{
enumerable = enumerable.Where((BaseItemData item) => item.name != null && item.name.ToLowerInvariant().Contains(search.ToLowerInvariant()));
}
foreach (BaseItemData item in (ItemSortMethod)_Shop_Sort.Value switch
{
ItemSortMethod.Name => enumerable.OrderBy((BaseItemData i) => i.name),
ItemSortMethod.PriceAscending => enumerable.OrderBy((BaseItemData i) => i.Price),
ItemSortMethod.PriceDescending => enumerable.OrderByDescending((BaseItemData i) => i.Price),
_ => enumerable,
})
{
if (_Shop_CurrentItemIDSelected.Value < 0)
{
_Shop_CurrentItemIDSelected.Value = item.ID;
}
Object.Instantiate(_ButtonTemplate, _ButtonParent).Initialize(item);
}
}
}