//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Wednesday, May 23, 2018 //-------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using TableConfig; using UnityEngine; using UnityEngine.UI; namespace Snxxz.UI { public class TreasureSoulWin : Window { [SerializeField] FunctionButton m_TreasureSoulBtn; [SerializeField] Button m_CloseBtn; [SerializeField] Image m_SelectSoulImg; [SerializeField] Text m_SoulName; [SerializeField] Text m_SoulDescription; [SerializeField] Image m_SoulTarget; [SerializeField] Button m_Active; [SerializeField] Text m_ActiveText; [SerializeField] Button m_GotoRealm; [SerializeField] List m_TreasureSouls; [SerializeField] ScrollerController m_ScollerControl; [SerializeField] RectTransform m_ContainerPreview; [SerializeField] Text m_PreviewTitle; [SerializeField] List m_PreviewPropretys; [SerializeField] RectTransform m_ContainerCondition; [SerializeField] ScrollerController m_ConditonControl; TreasureSoulModel m_Model; TreasureSoulModel model { get { return m_Model ?? (m_Model = ModelCenter.Instance.GetModel()); } } PlayerSuitModel suitModel { get { return ModelCenter.Instance.GetModel(); } } PlayerPackModel pack { get { return ModelCenter.Instance.GetModel(); } } public PlayerStrengthengDatas strengthengmodel { get { return ModelCenter.Instance.GetModel(); } } AchievementModel achievementModel { get { return ModelCenter.Instance.GetModel(); } } int m_SelectSoul = 0; List sortSouls = new List(); #region Built-in protected override void BindController() { } protected override void AddListeners() { m_Active.onClick.AddListener(Active); m_GotoRealm.onClick.AddListener(GotoRealm); m_CloseBtn.onClick.AddListener(CloseClick); m_ScollerControl.OnRefreshCell += OnRefreshCell; m_ConditonControl.OnRefreshCell += OnRefreshConditionCell; } protected override void OnPreOpen() { model.treasureSelectSoulChangeEvent += TreasureSelectSoulChangeEvent; PlayerDatas.Instance.PlayerDataRefreshInfoEvent += PlayerDataRefreshInfoEvent; model.treasureSoulEvent += TreasureSoulEvent; achievementModel.achievementCompletedEvent += AchievementCompletedEvent; m_TreasureSoulBtn.state = TitleBtnState.Click; Display(); DisplaySelect(); } private void PlayerDataRefreshInfoEvent(PlayerDataRefresh refreshType) { if (refreshType == PlayerDataRefresh.LV) { m_ScollerControl.m_Scorller.RefreshActiveCellViews(); } } protected override void OnAfterOpen() { } protected override void OnPreClose() { model.treasureSelectSoulChangeEvent -= TreasureSelectSoulChangeEvent; PlayerDatas.Instance.PlayerDataRefreshInfoEvent -= PlayerDataRefreshInfoEvent; model.treasureSoulEvent -= TreasureSoulEvent; achievementModel.achievementCompletedEvent -= AchievementCompletedEvent; for (int i = 0; i < m_TreasureSouls.Count; i++) { m_TreasureSouls[i].Dispose(); } m_SelectSoul = model.selectSoul; } protected override void OnAfterClose() { WindowCenter.Instance.Open(); } #endregion private void AchievementCompletedEvent(int _id) { List list; if (model.TryGetAchievements(model.selectSoul, out list) && list.Contains(_id)) { DisplaySelect(); } } private void Display() { sortSouls.Clear(); var _list = model.GetTreasureSouls(); for (int i = 0; i < _list.Count; i++) { if (model.IsOpenTreasureSoul(_list[i])) { sortSouls.Add(_list[i]); } } if (m_ScollerControl.GetNumberOfCells(m_ScollerControl.m_Scorller) != sortSouls.Count) { m_ScollerControl.Refresh(); for (int i = 0; i < sortSouls.Count; i++) { m_ScollerControl.AddCell(ScrollerDataType.Header, i); } m_ScollerControl.Restart(); } sortSouls.Sort(Compare); model.selectSoul = CheckOpenSelect(); var index = sortSouls.FindIndex((x) => { return x == model.selectSoul; }); m_ScollerControl.m_Scorller.RefreshActiveCellViews(); m_ScollerControl.JumpIndex(index); } int CheckOpenSelect() { if (WindowJumpMgr.Instance.IsJumpState && model.gotoSoul != 0) { var _soul = model.gotoSoul; model.gotoSoul = 0; return _soul; } model.gotoSoul = 0; var _select = 0; for (int i = 0; i < sortSouls.Count; i++) { TreasureSpecialData special; if (model.TryGetTreasureSoul(sortSouls[i], out special)) { if (special.redpoint.state == RedPointState.Simple) { return sortSouls[i]; } } if (!model.IsGetTreasureSoul(sortSouls[i]) && _select == 0) { _select = sortSouls[i]; } } _select = _select == 0 ? sortSouls[0] : _select; return _select; } private void OnRefreshCell(ScrollerDataType type, CellView cell) { TreasureSoulCell treasureSoulCell = cell as TreasureSoulCell; if (cell.index < sortSouls.Count) { treasureSoulCell.Display(sortSouls[cell.index]); } } private void TreasureSelectSoulChangeEvent(int _id) { DisplaySelect(); } private void TreasureSoulEvent(int _id) { if (_id == model.selectSoul) { TreasureSpecialData special; if (model.TryGetTreasureSoul(_id, out special)) { DisplaySelect(); } } } private void Active() { model.ActiveTreasureSoul(model.selectSoul); } private void GotoRealm() { WindowJumpMgr.Instance.WindowJumpTo(JumpUIType.RealmFunc1); } private void DisplaySelect() { TreasureSpecialData special; model.TryGetTreasureSoul(model.selectSoul, out special); m_ContainerCondition.gameObject.SetActive(false); if (special != null) { var config = Config.Instance.Get((int)special.type); if (config != null) { m_SoulName.text = config.Name; m_SoulDescription.text = UIHelper.ReplaceNewLine(config.Description); m_SoulTarget.SetSprite(config.targetDescription); m_SoulTarget.SetNativeSize(); DisplayPrivilege(special); m_SelectSoulImg.SetSprite(config.Icon); m_SelectSoulImg.material = special.active ? MaterialUtility.GetUIDefaultGraphicMaterial() : MaterialUtility.GetDefaultSpriteGrayMaterial(); if (special.active) { m_Active.gameObject.SetActive(false); m_GotoRealm.gameObject.SetActive(false); } else { if (!model.IsCompleteRealmAchievement(model.selectSoul)) { m_Active.gameObject.SetActive(false); m_GotoRealm.gameObject.SetActive(true); } else { m_Active.gameObject.SetActive(true); m_GotoRealm.gameObject.SetActive(false); m_Active.SetInteractable(m_ActiveText, model.IsCompleteAllAchievement(model.selectSoul)); m_ContainerCondition.gameObject.SetActive(true); DisplayCondition(); } } m_ContainerPreview.gameObject.SetActive(!special.active); if (!special.active) { DisplayPreview(model.selectSoul); } } } } private void DisplayPrivilege(TreasureSpecialData special) { int _pattern = -1; switch (special.type) { case TreasurePrivilege.Property: case TreasurePrivilege.Suit: case TreasurePrivilege.Wing: _pattern = 3; break; case TreasurePrivilege.DemonJarAtk: case TreasurePrivilege.MountAtk: case TreasurePrivilege.PetHp: case TreasurePrivilege.Sp: _pattern = 0; break; case TreasurePrivilege.StrengthenAdd: _pattern = 1; break; case TreasurePrivilege.Wash: _pattern = 2; break; } for (int i = 0; i < m_TreasureSouls.Count; i++) { m_TreasureSouls[i].Dispose(); m_TreasureSouls[i].gameObject.SetActive(_pattern == i && model.IsGetTreasureSoul(model.selectSoul)); if (m_TreasureSouls[i].bottom != null) { m_TreasureSouls[i].bottom.gameObject.SetActive(_pattern == i && model.IsGetTreasureSoul(model.selectSoul)); } if (_pattern == i) { m_TreasureSouls[i].Display((int)special.type); } } } private void DisplayPreview(int _id) { m_ContainerPreview.gameObject.SetActive(false); TreasureSpecialData special; model.TryGetTreasureSoul(_id, out special); switch (special.type) { case TreasurePrivilege.Wing: case TreasurePrivilege.Suit: case TreasurePrivilege.Property: m_PreviewTitle.text = Language.Get("TreasureSoulAddPropertyTitle"); break; case TreasurePrivilege.DemonJarAtk: case TreasurePrivilege.StrengthenAdd: case TreasurePrivilege.MountAtk: case TreasurePrivilege.PetHp: case TreasurePrivilege.Sp: m_PreviewTitle.text = Language.Get("TreasureSoulGetPropertyTitle"); break; } for (int i = 0; i < m_PreviewPropretys.Count; i++) { m_PreviewPropretys[i].gameObject.SetActive(false); } switch (special.type) { case TreasurePrivilege.Property: var _index = 0; m_ContainerPreview.gameObject.SetActive(true); foreach (var _key in special.propertyDict.Keys) { var _value = special.propertyDict[_key]; m_PreviewPropretys[_index].gameObject.SetActive(true); m_PreviewPropretys[_index].DisplayUpper(_key, _value); _index++; } break; case TreasurePrivilege.Suit: var percentSuit = (float)special.propertyDict[87] / 10000; var dict = suitModel.GetActiveSuitAttr(); var _suitIndex = 0; foreach (var _key in dict.Keys) { var propertyConfig = Config.Instance.Get(_key); if (propertyConfig == null || propertyConfig.type != 1) { continue; } m_PreviewPropretys[_suitIndex].gameObject.SetActive(true); m_PreviewPropretys[_suitIndex].DisplayUpper(_key, (int)(percentSuit * dict[_key])); _suitIndex++; } m_ContainerPreview.gameObject.SetActive(_suitIndex > 0); break; case TreasurePrivilege.Wing: var percentWing = (float)special.propertyDict[86] / 10000; var itemModel = pack.GetItemModelByIndex(PackType.rptEquip, (int)RoleEquipType.retWing); m_ContainerPreview.gameObject.SetActive(itemModel != null); if (itemModel != null) { var itemConfig = Config.Instance.Get(itemModel.itemId); float value = 0; if (ContainsProperty(itemConfig, 6, out value)) { if (itemModel.GetUseDataModel(42) != null) { value += strengthengmodel.BackpackWings(itemModel.itemId, itemModel.GetUseDataModel(42)[0])[1]; } } m_PreviewPropretys[0].gameObject.SetActive(true); m_PreviewPropretys[0].Display(6, (int)(value * percentWing)); } break; case TreasurePrivilege.DemonJarAtk: case TreasurePrivilege.PetHp: case TreasurePrivilege.MountAtk: case TreasurePrivilege.Sp: var index = 0; m_ContainerPreview.gameObject.SetActive(true); foreach (var _key in special.propertyDict.Keys) { var _value = special.propertyDict[_key] * (special.presentGetCount / special.progress); if (_value <= 0) { m_ContainerPreview.gameObject.SetActive(false); break; } m_PreviewPropretys[index].gameObject.SetActive(true); m_PreviewPropretys[index].DisplayUpper(_key, _value); index++; } break; case TreasurePrivilege.StrengthenAdd: var configs = Config.Instance.GetAllValues(); var present = -1; var next = 0; var presentGetCount = 0; foreach (var value in strengthengmodel._EqInfo.Values) { ItemModel equip = pack.GetItemModelByIndex(PackType.rptEquip, value.EquipIndex); if (equip != null) { presentGetCount += value.EquipPartStarLV; } } for (int i = 0; i < configs.Count; i++) { if (presentGetCount >= configs[i].countNeed) { present = i; } } next = present + 1; m_ContainerPreview.gameObject.SetActive(present >= 0); if (present >= 0) { var config = configs[present]; for (int i = 0; i < m_PreviewPropretys.Count; i++) { m_PreviewPropretys[i].gameObject.SetActive(i < config.attType.Length); if (i < config.attType.Length) { m_PreviewPropretys[i].DisplayUpper(config.attType[i], config.attValue[i]); } } } break; } } private void DisplayCondition() { m_ContainerCondition.gameObject.SetActive(true); List list; model.TryGetAchievements(model.selectSoul, out list); m_ConditonControl.Refresh(); if (list != null) { for (int i = 0; i < list.Count; i++) { var config = Config.Instance.Get(list[i]); if (config != null && config.Type == 50) { continue; } m_ConditonControl.AddCell(ScrollerDataType.Header, list[i]); } } m_ConditonControl.Restart(); } private void OnRefreshConditionCell(ScrollerDataType type, CellView cell) { var _cell = cell as TreasureSoulAchievementCell; _cell.Display(cell.index); } int Compare(int x, int y) { TreasureSpecialData special_x; TreasureSpecialData special_y; if (model.TryGetTreasureSoul(x, out special_x) && model.TryGetTreasureSoul(y, out special_y)) { return special_x.treasureId.CompareTo(special_y.treasureId); } return x.CompareTo(y); } bool ContainsProperty(ItemConfig config, int _property, out float value) { value = 0; if (config.Effect1 == _property && config.EffectValueA1 > 0) { value = config.EffectValueA1; return true; } if (config.Effect2 == _property && config.EffectValueA2 > 0) { value = config.EffectValueA2; return true; } if (config.Effect3 == _property && config.EffectValueA3 > 0) { value = config.EffectValueA3; return true; } if (config.Effect4 == _property && config.EffectValueA4 > 0) { value = config.EffectValueA4; return true; } if (config.Effect5 == _property && config.EffectValueA5 > 0) { value = config.EffectValueA5; return true; } return false; } } }