ClothingDecomposeView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FairyGUI;
  5. using UI.ClothingDecompose;
  6. namespace GFGGame
  7. {
  8. public class ClothingDecomposeView : BaseWindow
  9. {
  10. private UI_ClothingDecomposeUI _ui;
  11. private ValueBarController _valueBarController;
  12. private List<int> _clothingDatas = new List<int>();
  13. private Dictionary<int, int> _decomposeData = new Dictionary<int, int>();
  14. private int _decomposeCount = 0;//要分解物品的数量
  15. private int _curRarity = 0;
  16. public override void Dispose()
  17. {
  18. _valueBarController.Dispose();
  19. _valueBarController = null;
  20. base.Dispose();
  21. }
  22. protected override void OnInit()
  23. {
  24. base.OnInit();
  25. packageName = UI_ClothingDecomposeUI.PACKAGE_NAME;
  26. _ui = UI_ClothingDecomposeUI.Create();
  27. this.viewCom = _ui.target;
  28. isfullScreen = true;
  29. this.clickBlankToClose = false;
  30. _valueBarController = new ValueBarController(_ui.m_valueBar);
  31. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  32. _ui.m_listTab.onClickItem.Add((EventContext context) =>
  33. {
  34. int index = _ui.m_listTab.GetChildIndex(context.data as GObject);
  35. OnClickBtnRarity(index + 1);
  36. });
  37. _ui.m_btnSelect.onClick.Add(OnClickBtnSelect);
  38. _ui.m_btnRule.onClick.Add(OnClickBtnRule);
  39. _ui.m_btnDecompose.onClick.Add(OnClickBtnDecompose);
  40. _ui.m_list.itemRenderer = ListItemRander;
  41. _ui.m_list.SetVirtual();
  42. _ui.m_listReward.itemRenderer = ListRewardItemRander;
  43. _ui.m_listReward.onClickItem.Add(OnClickListRewardItem);
  44. }
  45. protected override void OnShown()
  46. {
  47. base.OnShown();
  48. _ui.m_c1.selectedIndex = 0;
  49. _ui.m_listTab.selectedIndex = 0;
  50. _valueBarController.OnShown();
  51. _valueBarController.Controller(6);
  52. OnClickBtnRarity(ConstDressRarity.Rarity_FANPIN);
  53. if (GuideDataManager.currentGuideId == GuideCfgArray.Instance.GetCfg(ConstGuideId.CLOTHING_DECOMPOSE).id)
  54. {
  55. _ui.m_listTab.selectedIndex = 1;
  56. OnClickBtnRarity(ConstDressRarity.Rarity_ZHENXI);
  57. }
  58. }
  59. protected override void OnHide()
  60. {
  61. base.OnHide();
  62. _valueBarController.OnHide();
  63. }
  64. private void OnClickBtnRarity(int rarity)
  65. {
  66. _curRarity = rarity;
  67. _clothingDatas = DecomposeDataManager.Instance.GetDecomposeDataByRarity(rarity);
  68. _ui.m_list.visible = _clothingDatas != null && _clothingDatas.Count > 0;
  69. _ui.m_txtNone.visible = _clothingDatas == null || _clothingDatas.Count == 0;
  70. if (_clothingDatas == null) return;
  71. _ui.m_list.numItems = _clothingDatas.Count;
  72. _ui.m_btnSelect.selected = false;
  73. CancleAll();
  74. UpdateConsume();
  75. }
  76. private void ListItemRander(int index, GObject obj)
  77. {
  78. UI_ListItem item = UI_ListItem.Proxy(obj);
  79. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_clothingDatas[index]);
  80. RarityIconController.UpdateRarityIcon(item.m_loaRarity, itemCfg.id, false);
  81. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  82. item.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg.res, ext);
  83. item.m_txtName.text = itemCfg.name;
  84. item.m_imgSelect.visible = false;
  85. int itemHasCount = DecomposeDataManager.Instance.ItemCanDecomposeCount(itemCfg.id);
  86. item.m_txtHasCount.text = itemHasCount.ToString();
  87. int itemCount = _decomposeData.ContainsKey(_clothingDatas[index]) ? _decomposeData[_clothingDatas[index]] : 0;
  88. item.m_txtCount.text = itemCount.ToString();
  89. item.m_imgSelect.visible = itemCount > 0;
  90. item.m_btnMinus.visible = itemCount > 0;
  91. if (item.m_btnMinus.data == null)
  92. {
  93. item.m_btnMinus.onClick.Add(OnClickBtnMinus);
  94. }
  95. item.m_btnMinus.data = index;
  96. if (item.m_loaItem.data == null)
  97. {
  98. item.m_loaItem.onClick.Add(OnListItemClick);
  99. }
  100. item.m_loaItem.data = index;
  101. item.target.data = itemCfg;
  102. }
  103. private void OnListItemClick(EventContext context)
  104. {
  105. int index = (int)((context.sender as GLoader).data);
  106. int childIndex = _ui.m_list.ItemIndexToChildIndex(index);
  107. GComponent com = _ui.m_list.GetChildAt(childIndex).asCom;
  108. int itemId = (com.data as ItemCfg).id;
  109. if (!_decomposeData.ContainsKey(itemId)) _decomposeData[itemId] = 0;
  110. if (_decomposeData[itemId] == DecomposeDataManager.Instance.ItemCanDecomposeCount(itemId)) return;
  111. if (_decomposeCount == DecomposeDataManager.MaxCount)
  112. {
  113. PromptController.Instance.ShowFloatTextPrompt("已达到单次可分解上限,请分批操作");
  114. return;
  115. }
  116. UI_ListItem item = UI_ListItem.Proxy(com);
  117. item.m_btnMinus.visible = true;
  118. item.m_imgSelect.visible = true;
  119. _decomposeData[itemId] = _decomposeData[itemId] + 1;
  120. _decomposeCount += 1;
  121. item.m_txtCount.text = _decomposeData[itemId].ToString();
  122. UpdateConsume();
  123. }
  124. private void OnClickBtnMinus(EventContext context)
  125. {
  126. int index = (int)((context.sender as GButton).data);
  127. int childIndex = _ui.m_list.ItemIndexToChildIndex(index);
  128. GComponent com = _ui.m_list.GetChildAt(childIndex).asCom;
  129. UI_ListItem item = UI_ListItem.Proxy(com);
  130. int itemId = (com.data as ItemCfg).id;
  131. _decomposeData[itemId] = _decomposeData[itemId] - 1;
  132. _decomposeCount -= 1;
  133. item.m_txtCount.text = _decomposeData[itemId].ToString();
  134. if (_decomposeData[itemId] == 0)
  135. {
  136. _decomposeData.Remove(itemId);
  137. item.m_imgSelect.visible = false;
  138. item.m_btnMinus.visible = false;
  139. }
  140. UpdateConsume();
  141. }
  142. private void OnClickBtnSelect()
  143. {
  144. if (_ui.m_btnSelect.selected)
  145. {
  146. SelectAll();
  147. }
  148. else
  149. {
  150. CancleAll();
  151. }
  152. UpdateConsume();
  153. }
  154. private void SelectAll()
  155. {
  156. int lastCount = DecomposeDataManager.MaxCount - _decomposeCount;
  157. for (int i = 0; i < _clothingDatas.Count; i++)
  158. {
  159. int itemId = _clothingDatas[i];
  160. int itemHasCount = DecomposeDataManager.Instance.ItemCanDecomposeCount(itemId);
  161. int itemLastCount = _decomposeData.ContainsKey(itemId) ? itemHasCount - _decomposeData[itemId] : itemHasCount;
  162. int count = Math.Min(lastCount, itemLastCount);
  163. lastCount -= count;
  164. if (!_decomposeData.ContainsKey(itemId)) _decomposeData[itemId] = 0;
  165. _decomposeData[itemId] = _decomposeData[itemId] + count;
  166. _decomposeCount += count;
  167. if (lastCount == 0) break;
  168. }
  169. _ui.m_list.numItems = _clothingDatas.Count;
  170. }
  171. private void CancleAll()
  172. {
  173. _decomposeData.Clear();
  174. _ui.m_list.numItems = _clothingDatas.Count;
  175. _decomposeCount = 0;
  176. }
  177. private void UpdateConsume()
  178. {
  179. DecomposeCfg cfg = DecomposeCfgArray.Instance.GetCfg(_curRarity);
  180. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(cfg.consumeId);
  181. _ui.m_listReward.numItems = _decomposeCount > 0 ? cfg.itemsArr.Length : 0;
  182. _ui.m_txtShow.text = string.Format("* 分解{0}件{1}可获得 *", StringUtil.GetColorText(_decomposeCount.ToString(), "#DA826E"), ConstDressRarity.DressRarityList()[_curRarity]);
  183. _ui.m_txtConsume.text = string.Format("消耗{0}X{1}", itemCfg.name, (_decomposeCount * cfg.consumeCount));
  184. }
  185. private void ListRewardItemRander(int index, GObject obj)
  186. {
  187. UI_ListRewardItem item = UI_ListRewardItem.Proxy(obj);
  188. DecomposeCfg decomposeCfg = DecomposeCfgArray.Instance.GetCfg(_curRarity);
  189. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(decomposeCfg.itemsArr[index][0]);
  190. item.m_txtCount.text = (decomposeCfg.itemsArr[index][1] * _decomposeCount).ToString();
  191. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  192. item.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg.res, ext);
  193. item.target.data = itemCfg;
  194. }
  195. private void OnClickListRewardItem(EventContext context)
  196. {
  197. UI_ListRewardItem item = UI_ListRewardItem.Proxy((context.data as GObject));
  198. GoodsItemTipsController.ShowItemTips((item.target.data as ItemCfg).id);
  199. }
  200. private void OnClickBtnDecompose()
  201. {
  202. if (_curRarity > ConstDressRarity.Rarity_ZHENXI)
  203. {
  204. Alert.Show(string.Format("当前选择的稀有度为{0},是否确定分解?", ConstDressRarity.DressRarityList()[_curRarity])).SetLeftButton(true).SetRightButton(true, "确定", (object data) => { DecomposeItem(); });
  205. }
  206. else
  207. {
  208. DecomposeItem();
  209. }
  210. }
  211. private async void DecomposeItem()
  212. {
  213. DecomposeCfg cfg = DecomposeCfgArray.Instance.GetCfg(_curRarity);
  214. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(cfg.consumeId);
  215. int consumeCount = cfg.consumeCount * _decomposeCount;
  216. if (consumeCount > ItemDataManager.GetItemNum(cfg.consumeId))
  217. {
  218. PromptController.Instance.ShowFloatTextPrompt(string.Format("{0}不足", itemCfg.name));
  219. return;
  220. }
  221. List<int> itemIds = _decomposeData.Keys.ToList<int>();
  222. List<int> itemNums = _decomposeData.Values.ToList<int>();
  223. bool result = await ClothingDecomposeSProxy.ClothingDecompose(itemIds, itemNums);
  224. if (result)
  225. {
  226. OnClickBtnRarity(_curRarity);
  227. }
  228. }
  229. private void OnClickBtnRule()
  230. {
  231. ViewManager.Show<ClothingDecomposeRuleView>();
  232. }
  233. private void OnClickBtnBack()
  234. {
  235. ViewManager.GoBackFrom(typeof(ClothingDecomposeView).FullName);
  236. }
  237. protected override void UpdateToCheckGuide(object param)
  238. {
  239. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  240. GuideController.TryGuide(_ui.m_list, ConstGuideId.CLOTHING_DECOMPOSE, 4, "选择重复获得的服饰部件", 0);
  241. GuideController.TryGuide(_ui.m_btnDecompose, ConstGuideId.CLOTHING_DECOMPOSE, 5, "分解获得的材料可用于新服饰合成");
  242. GuideController.TryCompleteGuide(ConstGuideId.CLOTHING_DECOMPOSE, 5);
  243. GuideController.TryGuide(_ui.m_btnBack, ConstGuideId.CLOTHING_SYNTHETIC, 1, "回到绣坊界面");
  244. }
  245. }
  246. }