FriendView.cs 12 KB

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