ClothingDecomposeView.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_comBtnTab.m_btnRarity0.onClick.Add(() => OnClickBtnRarity(ConstDressRarity.Rarity_FANPIN));
  33. _ui.m_comBtnTab.m_btnRarity1.onClick.Add(() => OnClickBtnRarity(ConstDressRarity.Rarity_ZHENXI));
  34. _ui.m_comBtnTab.m_btnRarity2.onClick.Add(() => OnClickBtnRarity(ConstDressRarity.Rarity_DIANCANG));
  35. _ui.m_comBtnTab.m_btnRarity3.onClick.Add(() => OnClickBtnRarity(ConstDressRarity.Rarity_TIANYI));
  36. _ui.m_btnSelect.onClick.Add(OnClickBtnSelect);
  37. _ui.m_btnRule.onClick.Add(OnClickBtnRule);
  38. _ui.m_btnDecompose.onClick.Add(OnClickBtnDecompose);
  39. _ui.m_list.itemRenderer = ListItemRander;
  40. _ui.m_list.SetVirtual();
  41. _ui.m_listReward.itemRenderer = ListRewardItemRander;
  42. _ui.m_listReward.onClickItem.Add(OnClickListRewardItem);
  43. }
  44. protected override void OnShown()
  45. {
  46. base.OnShown();
  47. _ui.m_c1.selectedIndex = 0;
  48. _valueBarController.OnShown();
  49. _valueBarController.Controller(6);
  50. OnClickBtnRarity(ConstDressRarity.Rarity_FANPIN);
  51. }
  52. protected override void OnHide()
  53. {
  54. base.OnHide();
  55. _valueBarController.OnHide();
  56. }
  57. private void OnClickBtnRarity(int rarity)
  58. {
  59. _curRarity = rarity;
  60. _clothingDatas = DecomposeDataManager.Instance.GetDecomposeDataByRarity(rarity);
  61. _ui.m_list.visible = _clothingDatas != null && _clothingDatas.Count > 0;
  62. _ui.m_txtNone.visible = _clothingDatas == null || _clothingDatas.Count == 0;
  63. if (_clothingDatas == null) return;
  64. _ui.m_list.numItems = _clothingDatas.Count;
  65. _ui.m_btnSelect.selected = false;
  66. CancleAll();
  67. UpdateConsume();
  68. }
  69. private void ListItemRander(int index, GObject obj)
  70. {
  71. UI_ListItem item = UI_ListItem.Proxy(obj);
  72. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_clothingDatas[index]);
  73. RarityIconController.UpdateRarityIcon(item.m_loaRarity, itemCfg.id, false);
  74. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  75. item.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg.res, ext);
  76. item.m_txtName.text = itemCfg.name;
  77. item.m_imgSelect.visible = false;
  78. int itemHasCount = DecomposeDataManager.Instance.ItemCanDecomposeCount(itemCfg.id);
  79. item.m_txtHasCount.text = itemHasCount.ToString();
  80. int itemCount = _decomposeData.ContainsKey(_clothingDatas[index]) ? _decomposeData[_clothingDatas[index]] : 0;
  81. item.m_txtCount.text = itemCount.ToString();
  82. item.m_imgSelect.visible = itemCount > 0;
  83. item.m_btnMinus.visible = itemCount > 0;
  84. if (item.m_btnMinus.data == null)
  85. {
  86. item.m_btnMinus.onClick.Add(OnClickBtnMinus);
  87. }
  88. item.m_btnMinus.data = index;
  89. if (item.m_loaItem.data == null)
  90. {
  91. item.m_loaItem.onClick.Add(OnListItemClick);
  92. }
  93. item.m_loaItem.data = index;
  94. item.target.data = itemCfg;
  95. }
  96. private void OnListItemClick(EventContext context)
  97. {
  98. int index = (int)((context.sender as GLoader).data);
  99. int childIndex = _ui.m_list.ItemIndexToChildIndex(index);
  100. GComponent com = _ui.m_list.GetChildAt(childIndex).asCom;
  101. int itemId = (com.data as ItemCfg).id;
  102. if (!_decomposeData.ContainsKey(itemId)) _decomposeData[itemId] = 0;
  103. if (_decomposeData[itemId] == DecomposeDataManager.Instance.ItemCanDecomposeCount(itemId)) return;
  104. if (_decomposeCount == DecomposeDataManager.MaxCount)
  105. {
  106. PromptController.Instance.ShowFloatTextPrompt("已达到单次可分解上限,请分批操作");
  107. return;
  108. }
  109. UI_ListItem item = UI_ListItem.Proxy(com);
  110. item.m_btnMinus.visible = true;
  111. item.m_imgSelect.visible = true;
  112. _decomposeData[itemId] = _decomposeData[itemId] + 1;
  113. _decomposeCount += 1;
  114. item.m_txtCount.text = _decomposeData[itemId].ToString();
  115. UpdateConsume();
  116. }
  117. private void OnClickBtnMinus(EventContext context)
  118. {
  119. int index = (int)((context.sender as GButton).data);
  120. int childIndex = _ui.m_list.ItemIndexToChildIndex(index);
  121. GComponent com = _ui.m_list.GetChildAt(childIndex).asCom;
  122. UI_ListItem item = UI_ListItem.Proxy(com);
  123. int itemId = (com.data as ItemCfg).id;
  124. _decomposeData[itemId] = _decomposeData[itemId] - 1;
  125. _decomposeCount -= 1;
  126. item.m_txtCount.text = _decomposeData[itemId].ToString();
  127. if (_decomposeData[itemId] == 0)
  128. {
  129. _decomposeData.Remove(itemId);
  130. item.m_imgSelect.visible = false;
  131. item.m_btnMinus.visible = false;
  132. }
  133. UpdateConsume();
  134. }
  135. private void OnClickBtnSelect()
  136. {
  137. if (_ui.m_btnSelect.selected)
  138. {
  139. SelectAll();
  140. }
  141. else
  142. {
  143. CancleAll();
  144. }
  145. UpdateConsume();
  146. }
  147. private void SelectAll()
  148. {
  149. int lastCount = DecomposeDataManager.MaxCount - _decomposeCount;
  150. for (int i = 0; i < _clothingDatas.Count; i++)
  151. {
  152. int itemId = _clothingDatas[i];
  153. int itemHasCount = DecomposeDataManager.Instance.ItemCanDecomposeCount(itemId);
  154. int itemLastCount = _decomposeData.ContainsKey(itemId) ? itemHasCount - _decomposeData[itemId] : itemHasCount;
  155. int count = Math.Min(lastCount, itemLastCount);
  156. lastCount -= count;
  157. if (!_decomposeData.ContainsKey(itemId)) _decomposeData[itemId] = 0;
  158. _decomposeData[itemId] = _decomposeData[itemId] + count;
  159. _decomposeCount += count;
  160. if (lastCount == 0) break;
  161. }
  162. _ui.m_list.numItems = _clothingDatas.Count;
  163. }
  164. private void CancleAll()
  165. {
  166. _decomposeData.Clear();
  167. _ui.m_list.numItems = _clothingDatas.Count;
  168. _decomposeCount = 0;
  169. }
  170. private void UpdateConsume()
  171. {
  172. DecomposeCfg cfg = DecomposeCfgArray.Instance.GetCfg(_curRarity);
  173. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(cfg.consumeId);
  174. _ui.m_listReward.numItems = _decomposeCount > 0 ? cfg.itemsArr.Length : 0;
  175. _ui.m_txtShow.text = string.Format("* 分解{0}件{1}可获得 *", StringUtil.GetColorText(_decomposeCount.ToString(), "#DA826E"), ConstDressRarity.DressRarityList()[_curRarity]);
  176. _ui.m_txtConsume.text = string.Format("消耗{0}X{1}", itemCfg.name, (_decomposeCount * cfg.consumeCount));
  177. }
  178. private void ListRewardItemRander(int index, GObject obj)
  179. {
  180. UI_ListRewardItem item = UI_ListRewardItem.Proxy(obj);
  181. DecomposeCfg decomposeCfg = DecomposeCfgArray.Instance.GetCfg(_curRarity);
  182. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(decomposeCfg.itemsArr[index][0]);
  183. item.m_txtCount.text = (decomposeCfg.itemsArr[index][1] * _decomposeCount).ToString();
  184. string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
  185. item.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg.res, ext);
  186. item.target.data = itemCfg;
  187. }
  188. private void OnClickListRewardItem(EventContext context)
  189. {
  190. UI_ListRewardItem item = UI_ListRewardItem.Proxy((context.data as GObject));
  191. GoodsItemTipsController.ShowItemTips((item.target.data as ItemCfg).id);
  192. }
  193. private void OnClickBtnDecompose()
  194. {
  195. if (_curRarity > ConstDressRarity.Rarity_ZHENXI)
  196. {
  197. Alert.Show(string.Format("当前选择的稀有度为{0},是否确定分解?", ConstDressRarity.DressRarityList()[_curRarity])).SetLeftButton(true).SetRightButton(true, "确定", (object data) => { DecomposeItem(); });
  198. }
  199. else
  200. {
  201. DecomposeItem();
  202. }
  203. }
  204. private async void DecomposeItem()
  205. {
  206. DecomposeCfg cfg = DecomposeCfgArray.Instance.GetCfg(_curRarity);
  207. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(cfg.consumeId);
  208. int consumeCount = cfg.consumeCount * _decomposeCount;
  209. if (consumeCount > ItemDataManager.GetItemNum(cfg.consumeId))
  210. {
  211. PromptController.Instance.ShowFloatTextPrompt(string.Format("{0}不足", itemCfg.name));
  212. return;
  213. }
  214. List<int> itemIds = _decomposeData.Keys.ToList<int>();
  215. List<int> itemNums = _decomposeData.Values.ToList<int>();
  216. bool result = await ClothingDecomposeSProxy.ClothingDecompose(itemIds, itemNums);
  217. if (result)
  218. {
  219. OnClickBtnRarity(_curRarity);
  220. }
  221. }
  222. private void OnClickBtnRule()
  223. {
  224. ViewManager.Show<ClothingDecomposeRuleView>();
  225. }
  226. private void OnClickBtnBack()
  227. {
  228. ViewManager.GoBackFrom(typeof(ClothingDecomposeView).FullName);
  229. }
  230. }
  231. }