FriendView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. /* 界面-好友 */
  9. public class FriendView : MonoBehaviour, MenuBackInterface
  10. {
  11. [SerializeField] GameObject myFriendBox;
  12. [SerializeField] GameObject friendRequestBox;
  13. [SerializeField] GameObject friendRecommendBox;
  14. [SerializeField] GameObject searchPlayerBox;
  15. void Awake()
  16. {
  17. InitBtnTabs();
  18. InitMyFriendBox();
  19. InitFriendRequestBox();
  20. InitFriendRecommendBox();
  21. InitSearchPlayerBox();
  22. }
  23. void Start()
  24. {
  25. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  26. if (UserPlayer.ins != null) {
  27. UserPlayer.ins.tempData.onUpdate += onUserPlayerTempDataUpdate;
  28. onUserPlayerTempDataUpdate();
  29. }
  30. SetBtnTabSelected(btnTabs[0]);
  31. }
  32. void OnDestroy() {
  33. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  34. if (UserPlayer.ins != null) {
  35. UserPlayer.ins.tempData.onUpdate -= onUserPlayerTempDataUpdate;
  36. }
  37. }
  38. public bool OnMenuBack() {
  39. Destroy(gameObject);
  40. return true;
  41. }
  42. void onUserPlayerTempDataUpdate() {
  43. try {
  44. var data = UserPlayer.ins.tempData;
  45. btnTabs[1].transform.Find("FriendTip").gameObject.SetActive(data.hasFriendRequest);
  46. } catch (System.Exception e) {
  47. Debug.LogError(e.Message);
  48. Debug.LogError(e.StackTrace);
  49. }
  50. }
  51. #region myFriend
  52. void InitMyFriendBox() {
  53. myFriendBox.SetActive(false);
  54. GetMyFriendItemPrefab().SetActive(false);
  55. }
  56. Transform GetMyFriendItemParent() {
  57. return myFriendBox.transform.Find("ScrollView/Viewport/Content");
  58. }
  59. GameObject GetMyFriendItemPrefab() {
  60. return GetMyFriendItemParent().Find("Item").gameObject;
  61. }
  62. void EnterMyFriendBox() {
  63. myFriendBox.SetActive(true);
  64. for (int i = 1; i < GetMyFriendItemParent().childCount; i++) {
  65. Destroy(GetMyFriendItemParent().GetChild(i).gameObject);
  66. }
  67. Action<JArray> cb = delegate(JArray list) {
  68. if (list.Count > 0) {
  69. foreach (var itemInfo in list) {
  70. int friendID = itemInfo.Value<int>("friendID");
  71. int avatarID = itemInfo.Value<int>("avatarID");
  72. string nickname = itemInfo.Value<string>("nickname");
  73. bool online = itemInfo.Value<bool>("online");
  74. long offlineTime = itemInfo.Value<long>("offlineTime");
  75. GameObject o = GameObject.Instantiate(GetMyFriendItemPrefab(), GetMyFriendItemParent());
  76. o.SetActive(true);
  77. o.name = friendID.ToString();
  78. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  79. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  80. o.transform.Find("BG0").gameObject.SetActive(!online);
  81. o.transform.Find("BG1").gameObject.SetActive(online);
  82. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  83. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  84. onlineTip.color = online ? Color.green : Color.gray;
  85. Button btnDelete = o.transform.Find("BtnDelete").GetComponent<Button>();
  86. btnDelete.onClick.RemoveAllListeners();
  87. btnDelete.onClick.AddListener(delegate() {
  88. AudioMgr.ins.PlayBtn();
  89. Action<bool> cb = delegate(bool res) {
  90. if (res) {
  91. Destroy(o);
  92. }
  93. };
  94. UserPlayer.ins.call("friendComp.deleteMyFriend", new object[]{friendID}, cb);
  95. });
  96. }
  97. } else {
  98. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友列表为空"));
  99. }
  100. HomeView.ins?.RenderFriendList(list);
  101. };
  102. UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
  103. PopupMgr.ins.ClearAllTip();
  104. }
  105. #endregion
  106. #region friendRequest
  107. void InitFriendRequestBox() {
  108. friendRequestBox.SetActive(false);
  109. GetFriendRequestItemPrefab().SetActive(false);
  110. }
  111. Transform GetFriendRequestItemParent() {
  112. return friendRequestBox.transform.Find("ScrollView/Viewport/Content");
  113. }
  114. GameObject GetFriendRequestItemPrefab() {
  115. return GetFriendRequestItemParent().Find("Item").gameObject;
  116. }
  117. void EnterFriendRequestBox() {
  118. friendRequestBox.SetActive(true);
  119. for (int i = 1; i < GetFriendRequestItemParent().childCount; i++) {
  120. Destroy(GetFriendRequestItemParent().GetChild(i).gameObject);
  121. }
  122. Action<JArray> cb = delegate(JArray list) {
  123. if (list.Count > 0) {
  124. foreach (var itemInfo in list) {
  125. int otherID = itemInfo.Value<int>("otherID");
  126. int avatarID = itemInfo.Value<int>("avatarID");
  127. string nickname = itemInfo.Value<string>("nickname");
  128. bool online = itemInfo.Value<bool>("online");
  129. long offlineTime = itemInfo.Value<long>("offlineTime");
  130. GameObject o = GameObject.Instantiate(GetFriendRequestItemPrefab(), GetFriendRequestItemParent());
  131. o.SetActive(true);
  132. o.name = otherID.ToString();
  133. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  134. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  135. o.transform.Find("BG0").gameObject.SetActive(!online);
  136. o.transform.Find("BG1").gameObject.SetActive(online);
  137. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  138. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  139. onlineTip.color = online ? Color.green : Color.gray;
  140. Button btnNo = o.transform.Find("BtnNo").GetComponent<Button>();
  141. btnNo.onClick.RemoveAllListeners();
  142. btnNo.onClick.AddListener(delegate() {
  143. AudioMgr.ins.PlayBtn();
  144. UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{false, otherID});
  145. Destroy(o);
  146. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  147. });
  148. Button btnYes = o.transform.Find("BtnYes").GetComponent<Button>();
  149. btnYes.onClick.RemoveAllListeners();
  150. btnYes.onClick.AddListener(delegate() {
  151. AudioMgr.ins.PlayBtn();
  152. UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{true, otherID});
  153. Destroy(o);
  154. UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
  155. });
  156. }
  157. } else {
  158. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友请求列表为空"));
  159. }
  160. };
  161. UserPlayer.ins.call("friendComp.getFriendRequestList", null, cb);
  162. PopupMgr.ins.ClearAllTip();
  163. }
  164. #endregion
  165. #region friendRecommend
  166. void InitFriendRecommendBox() {
  167. friendRecommendBox.SetActive(false);
  168. GetFriendRecommendItemPrefab().SetActive(false);
  169. }
  170. Transform GetFriendRecommendItemParent() {
  171. return friendRecommendBox.transform.Find("ScrollView/Viewport/Content");
  172. }
  173. GameObject GetFriendRecommendItemPrefab() {
  174. return GetFriendRecommendItemParent().Find("Item").gameObject;
  175. }
  176. void EnterFriendRecommendBox() {
  177. friendRecommendBox.SetActive(true);
  178. for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
  179. Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
  180. }
  181. Action<JArray> cb = delegate(JArray list) {
  182. for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
  183. Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
  184. }
  185. if (list.Count > 0) {
  186. foreach (var itemInfo in list) {
  187. int userID = itemInfo.Value<int>("id");
  188. int avatarID = itemInfo.Value<int>("avatarID");
  189. string nickname = itemInfo.Value<string>("nickname");
  190. bool online = itemInfo.Value<bool>("online");
  191. long offlineTime = itemInfo.Value<long>("offlineTime");
  192. int playCount = itemInfo.Value<int>("playCount");
  193. GameObject o = GameObject.Instantiate(GetFriendRecommendItemPrefab(), GetFriendRecommendItemParent());
  194. o.SetActive(true);
  195. o.name = userID.ToString();
  196. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  197. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  198. o.transform.Find("PlayCount").GetComponent<Text>().text =
  199. String.Format(
  200. TextAutoLanguage2.GetTextByKey("friend_record_play-again-count"), playCount
  201. );
  202. o.transform.Find("BG0").gameObject.SetActive(!online);
  203. o.transform.Find("BG1").gameObject.SetActive(online);
  204. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  205. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  206. onlineTip.color = online ? Color.green : Color.gray;
  207. Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
  208. btnYes.onClick.RemoveAllListeners();
  209. btnYes.onClick.AddListener(delegate() {
  210. AudioMgr.ins.PlayBtn();
  211. UserPlayer.ins.call("friendComp.requestAddFriend", userID);
  212. btnYes.interactable = false;
  213. });
  214. }
  215. } else {
  216. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有可推荐的好友"));
  217. }
  218. };
  219. UserPlayer.ins.call("friendComp.getFriendRecommendList", null, cb);
  220. PopupMgr.ins.ClearAllTip();
  221. }
  222. #endregion
  223. #region searchPlayer
  224. void InitSearchPlayerBox() {
  225. searchPlayerBox.SetActive(false);
  226. GetSearchPlayerItemPrefab().SetActive(false);
  227. }
  228. Transform GetSearchPlayerItemParent() {
  229. return searchPlayerBox.transform.Find("ScrollView/Viewport/Content");
  230. }
  231. GameObject GetSearchPlayerItemPrefab() {
  232. return GetSearchPlayerItemParent().Find("Item").gameObject;
  233. }
  234. void EnterSearchPlayerBox() {
  235. searchPlayerBox.SetActive(true);
  236. PopupMgr.ins.ClearAllTip();
  237. }
  238. InputField GetSearchInputField() {
  239. return searchPlayerBox.transform.Find("SearchBox/InputField").GetComponent<InputField>();
  240. }
  241. long _lastSearchTime = 0;
  242. public void btnEvent_SearchPlayer() {
  243. AudioMgr.ins.PlayBtn();
  244. if (JCUnityLib.TimeUtils.GetTimestamp() - _lastSearchTime < 3000) {
  245. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  246. return;
  247. } else {
  248. _lastSearchTime = JCUnityLib.TimeUtils.GetTimestamp();
  249. }
  250. string searchText = GetSearchInputField().text;
  251. Action<JArray> cb = delegate(JArray list) {
  252. for (int i = 1; i < GetSearchPlayerItemParent().childCount; i++) {
  253. Destroy(GetSearchPlayerItemParent().GetChild(i).gameObject);
  254. }
  255. if (list.Count > 0) {
  256. foreach (var itemInfo in list) {
  257. int userID = itemInfo.Value<int>("id");
  258. int avatarID = itemInfo.Value<int>("avatarID");
  259. string nickname = itemInfo.Value<string>("nickname");
  260. bool online = itemInfo.Value<bool>("online");
  261. long offlineTime = itemInfo.Value<long>("offlineTime");
  262. GameObject o = GameObject.Instantiate(GetSearchPlayerItemPrefab(), GetSearchPlayerItemParent());
  263. o.SetActive(true);
  264. o.name = userID.ToString();
  265. o.transform.Find("Avatar/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
  266. o.transform.Find("Name").GetComponent<Text>().text = nickname;
  267. o.transform.Find("BG0").gameObject.SetActive(!online);
  268. o.transform.Find("BG1").gameObject.SetActive(online);
  269. Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
  270. onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
  271. onlineTip.color = online ? Color.green : Color.gray;
  272. Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
  273. btnYes.onClick.RemoveAllListeners();
  274. btnYes.onClick.AddListener(delegate() {
  275. AudioMgr.ins.PlayBtn();
  276. UserPlayer.ins.call("friendComp.requestAddFriend", userID);
  277. btnYes.interactable = false;
  278. });
  279. }
  280. } else {
  281. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有搜索到目标玩家"));
  282. }
  283. };
  284. UserPlayer.ins.call("friendComp.searchPlayersByID", new object[]{searchText}, cb);
  285. }
  286. #endregion
  287. void HideAllBox() {
  288. myFriendBox.SetActive(false);
  289. friendRequestBox.SetActive(false);
  290. friendRecommendBox.SetActive(false);
  291. searchPlayerBox.SetActive(false);
  292. }
  293. public void Back() {
  294. AudioMgr.ins.PlayBtn();
  295. Destroy(this.gameObject);
  296. }
  297. #region 左栏条目切换
  298. [SerializeField] Sprite[] btnTabTextures;
  299. Button[] btnTabs = {null, null, null, null};
  300. void InitBtnTabs() {
  301. btnTabs[0] = this.transform.Find("LineV/BtnTab0").GetComponent<Button>();
  302. btnTabs[1] =this.transform.Find("LineV/BtnTab1").GetComponent<Button>();
  303. btnTabs[2] =this.transform.Find("LineV/BtnTab2").GetComponent<Button>();
  304. btnTabs[3] =this.transform.Find("LineV/BtnTab3").GetComponent<Button>();
  305. for (int i = 0; i < btnTabs.Length; i++) {
  306. Button btnTab = btnTabs[i];
  307. int btnTabIndex = i;
  308. btnTab.onClick.AddListener(() => {
  309. AudioMgr.ins.PlayBtn();
  310. if (!IsBtnTabSelected(btnTab)) {
  311. SetBtnTabSelected(btnTab);
  312. }
  313. });
  314. }
  315. }
  316. bool IsBtnTabSelected(Button btn) {
  317. Image img = btn.GetComponent<Image>();
  318. return img.sprite.name.Equals(btnTabTextures[1].name);
  319. }
  320. void SetBtnTabSelected(Button btn) {
  321. foreach (var item in btnTabs) {
  322. Image img = item.GetComponent<Image>();
  323. img.sprite = item == btn ? btnTabTextures[1] : btnTabTextures[0];
  324. }
  325. int index = Array.IndexOf(btnTabs, btn);
  326. HandleBtnTabSelectedLogic(index);
  327. }
  328. void HandleBtnTabSelectedLogic(int btnIndex) {
  329. HideAllBox();
  330. if (btnIndex == 0) {
  331. EnterMyFriendBox();
  332. } else if (btnIndex == 1) {
  333. EnterFriendRequestBox();
  334. } else if (btnIndex == 2) {
  335. EnterFriendRecommendBox();
  336. } else if (btnIndex == 3) {
  337. EnterSearchPlayerBox();
  338. }
  339. }
  340. #endregion
  341. }