GiftBoxSelectorView.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using ET;
  6. using FairyGUI;
  7. using UI.Bag;
  8. using UI.CommonGame;
  9. using UnityEngine;
  10. namespace GFGGame
  11. {
  12. public class GiftBoxSelectorView : BaseWindow
  13. {
  14. private UI_GiftBoxSelectorUI _ui;
  15. private int _itemId;
  16. private int _count; //物品数量
  17. private int _selCount; //选择的数量
  18. private Dictionary<int, int> _selDic; //选择礼包内的物品<物品id,数量>
  19. private List<LongPressGesture> _listLongPress = new List<LongPressGesture>();
  20. private EffectUI _effectUI1;
  21. private EffectUI _effectUI2;
  22. public override void Dispose()
  23. {
  24. if (_selDic != null)
  25. {
  26. _selDic.Clear();
  27. _selDic = null;
  28. }
  29. for (int i = 0; i < _listLongPress.Count; i++)
  30. {
  31. _listLongPress[i].Dispose();
  32. }
  33. _listLongPress.Clear();
  34. EffectUIPool.Recycle(_effectUI1);
  35. _effectUI1 = null;
  36. EffectUIPool.Recycle(_effectUI2);
  37. _effectUI2 = null;
  38. if (_ui != null)
  39. {
  40. _ui.Dispose();
  41. _ui = null;
  42. }
  43. base.Dispose();
  44. }
  45. protected override void OnInit()
  46. {
  47. base.OnInit();
  48. packageName = UI_GiftBoxSelectorUI.PACKAGE_NAME;
  49. _ui = UI_GiftBoxSelectorUI.Create();
  50. this.viewCom = _ui.target;
  51. this.viewCom.Center();
  52. this.modal = true;
  53. _selDic = new Dictionary<int, int>();
  54. _ui.m_btnCancel.onClick.Add(OnBtnCancelClick);
  55. _ui.m_list.itemRenderer = ListItemRender;
  56. _ui.m_btnSub.onClick.Add(OnBtnSubClick);
  57. AddEffect();
  58. }
  59. protected override void AddEventListener()
  60. {
  61. base.AddEventListener();
  62. }
  63. protected override void OnShown()
  64. {
  65. base.OnShown();
  66. _itemId = (int)this.viewData;
  67. _count = (int)ItemDataManager.GetItemNum(_itemId);
  68. _selDic.Clear();
  69. _selCount = 0;
  70. UpdateView();
  71. UpdateList();
  72. SetSelAllNum();
  73. }
  74. protected override void OnHide()
  75. {
  76. if (_ui.m_list.numItems > 0)
  77. {
  78. _ui.m_list.ScrollToView(0);
  79. }
  80. _ui.m_list.numItems = 0;
  81. _selDic.Clear();
  82. _selCount = 0;
  83. base.OnHide();
  84. }
  85. private void AddEffect()
  86. {
  87. //边框左上角特效
  88. _effectUI1 = EffectUIPool.CreateEffectUI(_ui.m_holderLeftTop, "ui_Activity", "Com_window_L_up");
  89. //边框右下角特效
  90. _effectUI2 = EffectUIPool.CreateEffectUI(_ui.m_holderRightDowm, "ui_Activity", "Com_window_R_Down");
  91. }
  92. protected override void RemoveEventListener()
  93. {
  94. base.RemoveEventListener();
  95. }
  96. private void UpdateView()
  97. {
  98. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  99. _ui.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg);
  100. _ui.m_txtName.text = itemCfg.name;
  101. _ui.m_txtTotalNum.text = $"已拥有:{_count}";
  102. _ui.m_txtItemDes.text = string.IsNullOrEmpty(itemCfg.desc) ? "暂无描述" : itemCfg.desc;
  103. }
  104. private void UpdateList()
  105. {
  106. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  107. _ui.m_list.numItems = itemCfg.itemsArr.Length;
  108. _ui.m_list.visible = true;
  109. }
  110. private void ListItemRender(int index, GObject obj)
  111. {
  112. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  113. UI_ListSelectorItem uiItemChild = UI_ListSelectorItem.Proxy(obj);
  114. int[] itemChildArr = itemCfg.itemsArr[index];
  115. ItemCfg itemCfgChild = ItemCfgArray.Instance.GetCfg(itemChildArr[0]);
  116. uiItemChild.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfgChild);
  117. uiItemChild.m_loaIcon.onClick.Add(OnListSelectorItemClick);
  118. uiItemChild.m_txtNum.text = itemChildArr[1].ToString();
  119. uiItemChild.m_txtSelNum.text = "0";
  120. uiItemChild.m_txtSelNum.onFocusOut.Add(OnChangedTxtSelNum);
  121. uiItemChild.m_btnAdd.onClick.Add(OnBtnAddClick);
  122. uiItemChild.m_btnReduce.onClick.Add(OnChildBtnReduceClick);
  123. uiItemChild.m_txtSelNum.onTouchBegin.Add(OnTextInputOpen);
  124. if (uiItemChild.target.data == null)
  125. {
  126. LongPressGesture longPressGesture = new LongPressGesture(uiItemChild.m_btnAdd);
  127. longPressGesture.trigger = 0.3f;
  128. longPressGesture.interval = 0.1f;
  129. longPressGesture.once = false;
  130. longPressGesture.onAction.Add(OnLongPress);
  131. _listLongPress.Add(longPressGesture);
  132. LongPressGesture longPressGesture2 = new LongPressGesture(uiItemChild.m_btnReduce);
  133. longPressGesture2.trigger = 0.3f;
  134. longPressGesture2.interval = 0.1f;
  135. longPressGesture2.once = false;
  136. longPressGesture2.onAction.Add(OnLongPressReduce);
  137. _listLongPress.Add(longPressGesture2);
  138. }
  139. uiItemChild.target.data = itemCfgChild;
  140. UI_ListSelectorItem.ProxyEnd();
  141. }
  142. private void OnBtnAddClick(EventContext context)
  143. {
  144. GObject sender = context.sender as GObject;
  145. GObject obj = sender.parent;
  146. UI_ListSelectorItem listItem = UI_ListSelectorItem.Proxy(obj);
  147. ItemCfg itemCfg = obj.data as ItemCfg;
  148. UpdateSel(listItem, itemCfg);
  149. }
  150. private void OnLongPress(EventContext context)
  151. {
  152. LongPressGesture gesture = (LongPressGesture)context.sender;
  153. var host = gesture.host;
  154. GObject obj = host.parent;
  155. UI_ListSelectorItem listItem = UI_ListSelectorItem.Proxy(obj);
  156. ItemCfg itemCfg = obj.data as ItemCfg;
  157. UpdateSel(listItem, itemCfg);
  158. }
  159. private void UpdateSel(UI_ListSelectorItem listItem, ItemCfg itemCfg)
  160. {
  161. if (_selCount == _count)
  162. {
  163. return;
  164. }
  165. if (listItem.m_txtSelNum.text == _count.ToString())
  166. {
  167. return;
  168. }
  169. if (_selDic.TryGetValue(itemCfg.id, out int num))
  170. {
  171. if (num == _count)
  172. {
  173. return;
  174. }
  175. _selCount += 1;
  176. _selDic[itemCfg.id] = num + 1;
  177. listItem.m_txtSelNum.text = Convert.ToString(Convert.ToInt32(listItem.m_txtSelNum.text) + 1);
  178. }
  179. else
  180. {
  181. _selDic.Add(itemCfg.id, 1);
  182. _selCount += 1;
  183. listItem.m_txtSelNum.text = Convert.ToString(1);
  184. }
  185. SetSelAllNum();
  186. }
  187. private void OnChildBtnReduceClick(EventContext context)
  188. {
  189. GObject sender = context.sender as GObject;
  190. GObject obj = sender.parent;
  191. UI_ListSelectorItem listItem = UI_ListSelectorItem.Proxy(obj);
  192. ItemCfg itemCfg = obj.data as ItemCfg;
  193. UpdateReduceSel(listItem, itemCfg);
  194. }
  195. private void OnTextInputOpen(EventContext context)
  196. {
  197. GObject sender = context.sender as GObject;
  198. GObject obj = sender.parent;
  199. UI_ListSelectorItem listItem = UI_ListSelectorItem.Proxy(obj);
  200. // 当触摸输入框时执行的逻辑
  201. if (listItem.m_txtSelNum.text == "0")
  202. {
  203. // 将文本设置为空字符串
  204. listItem.m_txtSelNum.text = default;
  205. // 延迟 0.2 秒后恢复文本
  206. Timers.inst.Add(0.2f, 1, (obj) => { listItem.m_txtSelNum.text = default; });
  207. }
  208. }
  209. private void OnLongPressReduce(EventContext context)
  210. {
  211. LongPressGesture gesture = (LongPressGesture)context.sender;
  212. var host = gesture.host;
  213. GObject obj = host.parent;
  214. UI_ListSelectorItem listItem = UI_ListSelectorItem.Proxy(obj);
  215. ItemCfg itemCfg = obj.data as ItemCfg;
  216. UpdateReduceSel(listItem, itemCfg);
  217. }
  218. private void UpdateReduceSel(UI_ListSelectorItem listItem, ItemCfg itemCfg)
  219. {
  220. if (_selCount == 0)
  221. {
  222. return;
  223. }
  224. if (listItem.m_txtSelNum.text == "0")
  225. {
  226. return;
  227. }
  228. if (_selDic.TryGetValue(itemCfg.id, out int num))
  229. {
  230. if (num == 0)
  231. {
  232. return;
  233. }
  234. _selCount -= 1;
  235. _selDic[itemCfg.id] = num - 1;
  236. listItem.m_txtSelNum.text = Convert.ToString(Convert.ToInt32(listItem.m_txtSelNum.text) - 1);
  237. }
  238. else
  239. {
  240. _selCount -= 1;
  241. _selDic.Add(itemCfg.id, 0);
  242. listItem.m_txtSelNum.text = Convert.ToString(0);
  243. }
  244. SetSelAllNum();
  245. }
  246. //监控输入
  247. private void OnChangedTxtSelNum(EventContext context)
  248. {
  249. GObject sender = context.sender as GObject;
  250. GObject obj = sender.parent;
  251. UI_ListSelectorItem listItem = UI_ListSelectorItem.Proxy(obj);
  252. ItemCfg itemCfg = obj.data as ItemCfg;
  253. UpdateSelNumSel(listItem, itemCfg);
  254. }
  255. private void UpdateSelNumSel(UI_ListSelectorItem listItem, ItemCfg itemCfg)
  256. {
  257. bool isNumeric = int.TryParse(listItem.m_txtSelNum.text, out var result);
  258. if (!isNumeric)
  259. {
  260. listItem.m_txtSelNum.text = "0";
  261. }
  262. var txtSelNum = Convert.ToInt32(listItem.m_txtSelNum.text);
  263. if (txtSelNum < 0)
  264. {
  265. if (_selDic.TryGetValue(itemCfg.id, out int numx))
  266. {
  267. listItem.m_txtSelNum.text = numx.ToString();
  268. }
  269. return;
  270. }
  271. _selDic.TryGetValue(itemCfg.id, out var snumx);
  272. var snum = _selCount - snumx + txtSelNum;
  273. //输入的数量大于剩余闲置的量,就设置成最大闲置的量
  274. if (snum > _count)
  275. {
  276. //其他选择的量
  277. var otherNum = _selDic.Where(a => a.Key != itemCfg.id).Select(a => a.Value).Sum();
  278. //物品总量-其他物品选择的量=当前物品最大可以选择的量
  279. var curSelTotalCount = _count - otherNum;
  280. listItem.m_txtSelNum.text = curSelTotalCount.ToString();
  281. _selDic[itemCfg.id] = curSelTotalCount;
  282. _selCount = _selDic.Values.Sum();
  283. }
  284. else
  285. {
  286. if (_selDic.TryGetValue(itemCfg.id, out int num))
  287. {
  288. _selDic[itemCfg.id] = txtSelNum;
  289. _selCount = _selDic.Values.Sum();
  290. }
  291. else
  292. {
  293. _selDic[itemCfg.id] = txtSelNum;
  294. _selCount = _selDic.Values.Sum();
  295. }
  296. }
  297. SetSelAllNum();
  298. }
  299. private void SetSelAllNum()
  300. {
  301. _ui.m_txtSelRewardStr.text = $"已选奖励:{_selCount}/{_count}";
  302. }
  303. //弹出物品详细描述框
  304. private void OnListSelectorItemClick(EventContext context)
  305. {
  306. GObject sender = context.sender as GObject;
  307. GObject obj = sender.parent;
  308. ItemCfg itemCfg = obj.data as ItemCfg;
  309. GoodsItemTipsController.ShowItemTips(itemCfg.id);
  310. }
  311. private void OnBtnCancelClick()
  312. {
  313. this.Hide();
  314. }
  315. //确认按钮
  316. private void OnBtnSubClick()
  317. {
  318. List<GiftBoxSelChildItemProto> reqItemList = new List<GiftBoxSelChildItemProto>();
  319. _selDic = _selDic.Where(pair => pair.Value != 0).ToDictionary(pair => pair.Key, pair => pair.Value);
  320. if (_selDic.Keys.Count > 0)
  321. {
  322. foreach (var itemKv in _selDic)
  323. {
  324. if (itemKv.Value != 0)
  325. {
  326. reqItemList.Add(new GiftBoxSelChildItemProto
  327. {
  328. ChildItemId = itemKv.Key,
  329. ChildItemCount = itemKv.Value
  330. });
  331. }
  332. }
  333. ItemProxy.ReqUseGiftBoxSelItem(_itemId, reqItemList).Coroutine();
  334. this.Hide();
  335. }
  336. else
  337. {
  338. PromptController.Instance.ShowFloatTextPrompt("请先选择");
  339. }
  340. }
  341. }
  342. }