GiftBoxSelectorView.cs 14 KB

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