FriendView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using UnityEngine;
  2. using FairyGUI;
  3. using UI.Friend;
  4. using UI.CommonGame;
  5. namespace GFGGame
  6. {
  7. public class FriendView : BaseView
  8. {
  9. private UI_FriendUI _ui;
  10. private GameObject _scenePrefab;
  11. private GameObject _sceneObject;
  12. private DressUpObj _dressUpObj;
  13. private long _friendRoleId;
  14. public override void Dispose()
  15. {
  16. if (_sceneObject != null)
  17. {
  18. GameObject.Destroy(_sceneObject);
  19. _sceneObject = null;
  20. }
  21. _dressUpObj = null;
  22. if (_ui != null)
  23. {
  24. _ui.Dispose();
  25. _ui = null;
  26. }
  27. base.Dispose();
  28. }
  29. protected override void Init()
  30. {
  31. base.Init();
  32. }
  33. protected override void OnInit()
  34. {
  35. base.OnInit();
  36. packageName = UI_FriendUI.PACKAGE_NAME;
  37. _ui = UI_FriendUI.Create();
  38. viewCom = _ui.target;
  39. isfullScreen = true;
  40. isReturnView = true;
  41. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("SceneFriend"));
  42. _dressUpObj = new DressUpObj();
  43. _ui.m_list.SetVirtual();
  44. _ui.m_list.itemRenderer = RenderListItem;
  45. _ui.m_list.onClickItem.Add(OnListItemClick);
  46. _ui.m_btnInfo.onClick.Add(OnBtnInfoClick);
  47. _ui.m_btnDelete.onClick.Add(OnBtnDeleteClick);
  48. _ui.m_btnAdd.onClick.Add(OnBtnAddClick);
  49. _ui.m_btnSendAll.onClick.Add(OnBtnSendAllClick);
  50. _ui.m_btnBack.onClick.Add(GoBackFrom);
  51. }
  52. protected override void AddEventListener()
  53. {
  54. base.AddEventListener();
  55. EventAgent.AddEventListener(ConstMessage.FRIEND_REFRESH, RefreshView);
  56. EventAgent.AddEventListener(ConstMessage.FRIEND_REMOVE, RefreshRemoveFriend);
  57. EventAgent.AddEventListener(ConstMessage.FRIEND_ADD, RefreshAddFriend);
  58. EventAgent.AddEventListener(ConstMessage.RED_CHANGE, UpdateRedDot);
  59. }
  60. protected override void OnShown()
  61. {
  62. base.OnShown();
  63. _friendRoleId = this.viewData == null ? 0 : (long)this.viewData;
  64. if (_sceneObject == null)
  65. {
  66. CustomSuitData customSuit = CustomSuitDataManager.GetSuitList(CustomSuitDataManager.currentIndex);
  67. _sceneObject = GameObject.Instantiate(_scenePrefab);
  68. _dressUpObj.setSceneObj(_sceneObject);
  69. _dressUpObj.AddOrRemove(customSuit.dressUpData.bgId, true);
  70. }
  71. FriendDataManager.Instance.UpdateFriendList(true);
  72. _ui.m_list.numItems = FriendDataManager.Instance.FriendDatas.Count;
  73. if (_ui.m_list.numItems > 0)
  74. {
  75. if (_friendRoleId > 0)
  76. {
  77. for (int i = 0; i < FriendDataManager.Instance.FriendDatas.Count; i++)
  78. {
  79. if (_friendRoleId == FriendDataManager.Instance.FriendDatas[i])
  80. {
  81. _ui.m_list.selectedIndex = i;
  82. _ui.m_list.ScrollToView(i);
  83. ReqFriendDetialInfo(i);
  84. }
  85. }
  86. }
  87. else
  88. {
  89. _ui.m_list.selectedIndex = 0;
  90. ReqFriendDetialInfo(0);
  91. }
  92. }
  93. else
  94. {
  95. ReqFriendDetialInfo(-1);
  96. }
  97. RefreshView();
  98. UpdateRedDot();
  99. }
  100. protected override void OnHide()
  101. {
  102. base.OnHide();
  103. if (_sceneObject != null)
  104. {
  105. GameObject.Destroy(_sceneObject);
  106. _sceneObject = null;
  107. }
  108. _ui.m_list.numItems = 0;
  109. }
  110. private void GoBackFrom()
  111. {
  112. ViewManager.GoBackFrom(typeof(FriendView).FullName);
  113. }
  114. protected override void RemoveEventListener()
  115. {
  116. base.RemoveEventListener();
  117. EventAgent.RemoveEventListener(ConstMessage.FRIEND_REFRESH, RefreshView);
  118. EventAgent.RemoveEventListener(ConstMessage.FRIEND_REMOVE, RefreshRemoveFriend);
  119. EventAgent.RemoveEventListener(ConstMessage.FRIEND_ADD, RefreshAddFriend);
  120. EventAgent.RemoveEventListener(ConstMessage.RED_CHANGE, UpdateRedDot);
  121. }
  122. private void RefreshView()
  123. {
  124. _ui.m_list.RefreshVirtualList();
  125. _ui.m_txtCount.text = string.Format("好友数:{0}/{1}", _ui.m_list.numItems, GlobalCfgArray.globalCfg.maxFriendCount);
  126. _ui.m_btnSendAll.text = RedDotDataManager.Instance.GetFriendGiftRed() ? "一键领取并赠送" : "一键赠送";
  127. _ui.m_txtPowerCount.SetVar("value", FriendDataManager.Instance.Count.ToString()).FlushVars();
  128. _ui.m_txtPowerCount.SetVar("maxValue", FriendDataManager.Instance.maxGetPowerCount.ToString()).FlushVars();
  129. }
  130. private void RenderListItem(int index, GObject obj)
  131. {
  132. long roleId = FriendDataManager.Instance.FriendDatas[index];
  133. FriendInfoData friendInfo = FriendDataManager.Instance.GetFriendDataById(roleId);
  134. UI_ListItem item = UI_ListItem.Proxy(obj);
  135. RoleInfoManager.Instance.UpdateHead(item.m_comHead, friendInfo.roleInfo.headId, friendInfo.roleInfo.headBorderId);
  136. item.m_txtName.text = friendInfo.roleInfo.roleName;
  137. item.m_txtLvl.text = friendInfo.roleInfo.roleLv.ToString();
  138. item.m_c2.selectedIndex = friendInfo.roleInfo.offlineTimeSec == 0 ? 0 : 1;
  139. item.m_c1.selectedIndex = FriendDataManager.Instance.GetGiftState(friendInfo.roleInfo.roleId);
  140. if (item.m_btnSend.data == null)
  141. {
  142. item.m_btnSend.onClick.Add(OnClickBtnSend);
  143. }
  144. item.m_btnSend.data = roleId;
  145. item.target.data = index;
  146. UI_ListItem.ProxyEnd();
  147. }
  148. private void OnClickBtnSend(EventContext context)
  149. {
  150. GObject item = context.sender as GObject;
  151. FriendInfoData friendInfo = FriendDataManager.Instance.GetFriendDataById((long)item.data);
  152. if (friendInfo.takeGiftState == ConstBonusStatus.CAN_GET)
  153. {
  154. // if (FriendDataManager.Instance.Count >= GlobalCfgArray.globalCfg.maxGetPowerCount)
  155. // {
  156. // PromptController.Instance.ShowFloatTextPrompt("今日体力已全部领取");
  157. // return;
  158. // }
  159. FriendSProxy.ReqTakeGiftFromFriend(friendInfo.roleInfo.roleId).Coroutine();
  160. }
  161. else if (friendInfo.giveGiftState == (int)ConstGiveGiftStatus.CanGave)
  162. {
  163. FriendSProxy.ReqGiveGiftToFriend(friendInfo.roleInfo.roleId).Coroutine();
  164. }
  165. }
  166. private void OnListItemClick(EventContext context)
  167. {
  168. GObject item = context.data as GObject;
  169. int index = (int)item.data;
  170. ReqFriendDetialInfo(index);
  171. }
  172. private void OnBtnSendAllClick()
  173. {
  174. if (RedDotDataManager.Instance.GetFriendGiftRed() && FriendDataManager.Instance.Count < FriendDataManager.Instance.maxGetPowerCount)
  175. {
  176. FriendSProxy.ReqTakeGiftFromAllFriend().Coroutine();
  177. }
  178. else
  179. {
  180. FriendSProxy.ReqGiveGiftToAllFriend().Coroutine();
  181. }
  182. }
  183. private void OnBtnDeleteClick()
  184. {
  185. if (_ui.m_list.numItems == 0)
  186. {
  187. PromptController.Instance.ShowFloatTextPrompt("暂无好友可删除");
  188. return;
  189. }
  190. AlertUI.Show("是否删除好友?").SetLeftButton(true).SetRightButton(true, "确定", (object data) =>
  191. {
  192. int index = _ui.m_list.selectedIndex;
  193. long roleId = FriendDataManager.Instance.FriendDatas[index];
  194. FriendSProxy.ReqDeleteFriend(roleId).Coroutine();
  195. });
  196. }
  197. private void RefreshRemoveFriend()
  198. {
  199. int index = _ui.m_list.selectedIndex;
  200. _ui.m_list.numItems = FriendDataManager.Instance.FriendDatas.Count;
  201. if (FriendDataManager.Instance.FriendDatas.Count > 0)
  202. {
  203. if (index >= FriendDataManager.Instance.FriendDatas.Count)
  204. {
  205. index = FriendDataManager.Instance.FriendDatas.Count - 1;//选中最后一个
  206. }
  207. _ui.m_list.selectedIndex = index;
  208. }
  209. else
  210. {
  211. index = -1;//无好友
  212. }
  213. ReqFriendDetialInfo(index);
  214. _ui.m_txtCount.text = string.Format("好友数:{0}/{1}", _ui.m_list.numItems, GlobalCfgArray.globalCfg.maxFriendCount);
  215. }
  216. private void RefreshAddFriend()
  217. {
  218. int selectedIndex = 0;
  219. if (_ui.m_list.numItems > 0)
  220. {
  221. int childIndex = _ui.m_list.ItemIndexToChildIndex(_ui.m_list.selectedIndex);
  222. if (childIndex < 0) return;
  223. GButton item = _ui.m_list.GetChildAt(childIndex).asButton.GetChild("btnSend").asButton;
  224. long roleId = (long)item.data;
  225. selectedIndex = FriendDataManager.Instance.FriendDatas.IndexOf(roleId);
  226. }
  227. _ui.m_list.numItems = FriendDataManager.Instance.FriendDatas.Count;
  228. _ui.m_list.selectedIndex = selectedIndex;
  229. ReqFriendDetialInfo(selectedIndex);
  230. _ui.m_txtCount.text = string.Format("好友数:{0}/{1}", _ui.m_list.numItems, GlobalCfgArray.globalCfg.maxFriendCount);
  231. }
  232. private async void ReqFriendDetialInfo(int index)
  233. {
  234. if (index >= 0)
  235. {
  236. _friendRoleId = FriendDataManager.Instance.FriendDatas[index];
  237. OtherRoleInfoDetailData roleInfoDetail = await RoleInfoSProxy.ReqOtherRoleDetailInfo(_friendRoleId);
  238. if (roleInfoDetail != null)
  239. {
  240. UpdateScene(roleInfoDetail.customSuitData);
  241. }
  242. }
  243. else
  244. {
  245. CustomSuitData customSuit = CustomSuitDataManager.GetSuitList(CustomSuitDataManager.currentIndex);
  246. UpdateScene(customSuit);
  247. }
  248. }
  249. private void UpdateScene(CustomSuitData suitSavedData)
  250. {
  251. if (suitSavedData != null)
  252. {
  253. _dressUpObj.PutOnDressUpData(suitSavedData.dressUpData);
  254. }
  255. else
  256. {
  257. _dressUpObj.PutOnDefaultDressUpData();
  258. }
  259. Transform transform = _sceneObject.transform.Find("Role");
  260. transform.localPosition = new Vector3(-2, transform.localPosition.y, 0);
  261. }
  262. private void OnBtnInfoClick()
  263. {
  264. if (_friendRoleId == 0)
  265. {
  266. ViewManager.Show<RoleInfoView>(null, new object[] { typeof(FriendView).FullName, _friendRoleId });
  267. }
  268. else
  269. {
  270. FriendInfoData friendInfoData = FriendDataManager.Instance.GetFriendDataById(_friendRoleId);
  271. ViewManager.Show<OtherRoleInfoView>(new object[] { friendInfoData.roleInfo, friendInfoData.roleDetailInfo }, new object[] { typeof(FriendView).FullName, _friendRoleId });
  272. }
  273. }
  274. private void OnBtnAddClick()
  275. {
  276. ViewManager.Show<FriendAddView>();
  277. }
  278. private void UpdateRedDot()
  279. {
  280. RedDotController.Instance.SetComRedDot(_ui.m_btnAdd, RedDotDataManager.Instance.GetFriendApplyRed());
  281. RedDotController.Instance.SetComRedDot(_ui.m_btnSendAll, RedDotDataManager.Instance.GetFriendGiftRed());
  282. // RefreshView();
  283. }
  284. }
  285. }