FriendView.cs 16 KB

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