//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Thursday, April 18, 2019 //-------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace vnxbqy.UI { public class AlchemyUseDrugWin : Window { [SerializeField] AlchemyType m_AlchemyType; [SerializeField] protected ScrollerController m_Controller; //[SerializeField] AlchemySortBehaviour m_AlchemyTypeSort; //[SerializeField] AlchemySortBehaviour m_AlchemyQualitySort; List displayItems = new List(); protected AlchemyModel model { get { return ModelCenter.Instance.GetModel(); } } PackModel packModel { get { return ModelCenter.Instance.GetModel(); } } #region Built-in protected override void BindController() { } protected override void AddListeners() { m_Controller.OnRefreshCell += OnRefreshCell; } protected override void OnPreOpen() { //m_AlchemyTypeSort.Display(); //m_AlchemyQualitySort.Display(); AlchemySortBehaviour.onSortRefresh += OnSortRefresh; model.alchemyDrugUseRefresh += AlchemyDrugUseRefresh; packModel.refreshItemCountEvent += RefreshItemCountEvent; } protected override void OnActived() { base.OnActived(); Display(); } protected override void OnAfterOpen() { } protected override void OnPreClose() { AlchemySortBehaviour.onSortRefresh -= OnSortRefresh; model.alchemyDrugUseRefresh -= AlchemyDrugUseRefresh; packModel.refreshItemCountEvent -= RefreshItemCountEvent; } protected override void OnAfterClose() { } #endregion void Display() { DisplayDrugs(); var index = displayItems.IndexOf(model.jumpAlchemy); if (index != -1) { m_Controller.JumpIndex(index); } else { m_Controller.JumpIndex(0); } model.jumpAlchemy = 0; } void DisplayDrugs() { var lastCount = displayItems.Count; displayItems.Clear(); var items = model.GetAlchemyDrugs(); foreach (var id in items) { var config = AttrFruitConfig.Get(id); if (config.drugType != (int)m_AlchemyType) { continue; } //var itemConfig = ItemConfig.Get(id); //if (m_AlchemyQualitySort.IsSortEnabled(itemConfig.LV)) //{ displayItems.Add(id); //} } displayItems.Sort(Compare); if (lastCount != displayItems.Count) { m_Controller.Refresh(); for (int i = 0; i < displayItems.Count; i++) { m_Controller.AddCell(ScrollerDataType.Header, i); } m_Controller.Restart(); } else { m_Controller.m_Scorller.RefreshActiveCellViews(); } } private void OnSortRefresh() { DisplayDrugs(); } private void AlchemyDrugUseRefresh() { m_Controller.m_Scorller.RefreshActiveCellViews(); } private void OnRefreshCell(ScrollerDataType type, CellView cell) { var useDrugCell = cell as AlchemyUseDrugCell; useDrugCell.Display(displayItems[cell.index], cell.index); } private void RefreshItemCountEvent(PackType packType, int arg2, int itemId) { if (packType == PackType.Item && displayItems.Contains(itemId)) { m_Controller.m_Scorller.RefreshActiveCellViews(); } } private int Compare(int lhs, int rhs) { var lhs_state = model.GetAlchemyDrugState(lhs); var rhs_state = model.GetAlchemyDrugState(rhs); if (lhs_state != rhs_state) { return lhs_state.CompareTo(rhs_state); } var lhs_Config = ItemConfig.Get(lhs); var rhs_Config = ItemConfig.Get(rhs); if (lhs_Config.LV != rhs_Config.LV) { return lhs_Config.LV.CompareTo(rhs_Config.LV); } if (m_AlchemyType == AlchemyType.Fairy) { var lhs_alchemyId = GetAlchemyIdByItemId(lhs); var rhs_alchemyId = GetAlchemyIdByItemId(rhs); if (lhs_alchemyId != 0 && rhs_alchemyId != 0) { return lhs_alchemyId.CompareTo(rhs_alchemyId); } } return lhs.CompareTo(rhs); } int GetAlchemyIdByItemId(int itemId) { var configs = AlchemyConfig.GetValues(); foreach (var config in configs) { if (config.AlchemItemID == itemId) { return config.ID; } } return 0; } } }