| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- /* 界面-好友 */
- public class FriendView : JCUnityLib.ViewBase, MenuBackInterface
- {
- [SerializeField] GameObject myFriendBox;
- [SerializeField] GameObject friendRequestBox;
- [SerializeField] GameObject friendRecommendBox;
- [SerializeField] GameObject searchPlayerBox;
- void Awake()
- {
- InitBtnTabs();
- InitMyFriendBox();
- InitFriendRequestBox();
- InitFriendRecommendBox();
- InitSearchPlayerBox();
- }
- [NonSerialized] public int firstBtnTabsIndex = 0;
- public override void OnShowView(object[] args)
- {
- if (args.Length == 2 && (string)args[0] == "setFirstBtnTabsIndex")
- firstBtnTabsIndex = (int)args[1];
- }
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- if (UserPlayer.ins != null) {
- UserPlayer.ins.tempData.onUpdate += onUserPlayerTempDataUpdate;
- onUserPlayerTempDataUpdate();
- }
- SetBtnTabSelected(btnTabs[firstBtnTabsIndex]);
- }
- void OnDestroy() {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- if (UserPlayer.ins != null) {
- UserPlayer.ins.tempData.onUpdate -= onUserPlayerTempDataUpdate;
- }
- if (_hasDeleteAnyFriends || _hasAcceptAnyFriends) {
- HomeView.ins?.RefreshFriendBarAccordingCurrentBar();
- }
- }
- public bool OnMenuBack() {
- ViewMgr.Instance.DestroyView<FriendView>();
- return true;
- }
- void onUserPlayerTempDataUpdate() {
- try {
- var data = UserPlayer.ins.tempData;
- btnTabs[1].transform.Find("FriendTip").gameObject.SetActive(data.hasFriendRequest);
- } catch (System.Exception e) {
- Debug.LogError(e.Message);
- Debug.LogError(e.StackTrace);
- }
- }
- #region myFriend
- void InitMyFriendBox() {
- myFriendBox.SetActive(false);
- GetMyFriendItemPrefab().SetActive(false);
- }
- Transform GetMyFriendItemParent() {
- return myFriendBox.transform.Find("ScrollView/Viewport/Content");
- }
- GameObject GetMyFriendItemPrefab() {
- return GetMyFriendItemParent().Find("Item").gameObject;
- }
- //是否删除过好友,该记录可帮助提醒homeView的好友栏刷新
- bool _hasDeleteAnyFriends = false;
- void EnterMyFriendBox() {
- myFriendBox.SetActive(true);
- for (int i = 1; i < GetMyFriendItemParent().childCount; i++) {
- Destroy(GetMyFriendItemParent().GetChild(i).gameObject);
- }
- Action<JArray> cb = delegate(JArray list) {
- if (list.Count > 0) {
- foreach (var itemInfo in list) {
- int friendID = itemInfo.Value<int>("friendID");
- int avatarID = itemInfo.Value<int>("avatarID");
- string avatarUrl = itemInfo.Value<string>("avatarUrl");
- string nickname = itemInfo.Value<string>("nickname");
- bool online = itemInfo.Value<bool>("online");
- long offlineTime = itemInfo.Value<long>("offlineTime");
- GameObject o = GameObject.Instantiate(GetMyFriendItemPrefab(), GetMyFriendItemParent());
- o.SetActive(true);
- o.name = friendID.ToString();
- RoleMgr.SetAvatarToImage(
- o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
- avatarID, avatarUrl);
- o.transform.Find("Name").GetComponent<Text>().text = nickname;
- o.transform.Find("BG0").gameObject.SetActive(!online);
- o.transform.Find("BG1").gameObject.SetActive(online);
- Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
- onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
- onlineTip.color = online ? Color.green : Color.gray;
- Button btnDelete = o.transform.Find("BtnDelete").GetComponent<Button>();
- btnDelete.onClick.RemoveAllListeners();
- btnDelete.onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- Action<bool> cb = delegate(bool res) {
- if (res) {
- _hasDeleteAnyFriends = true;
- Destroy(o);
- }
- };
- UserPlayer.ins.call("friendComp.deleteMyFriend", new object[]{friendID}, cb);
- });
- }
- } else {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友列表为空"));
- }
- HomeView.ins?.RenderFriendList(list);
- };
- UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
- PopupMgr.ins.ClearAllTip();
- }
- #endregion
- #region friendRequest
- void InitFriendRequestBox() {
- friendRequestBox.SetActive(false);
- GetFriendRequestItemPrefab().SetActive(false);
- }
- Transform GetFriendRequestItemParent() {
- return friendRequestBox.transform.Find("ScrollView/Viewport/Content");
- }
- GameObject GetFriendRequestItemPrefab() {
- return GetFriendRequestItemParent().Find("Item").gameObject;
- }
- //是否同意过好友添加请求,该记录可帮助提醒homeView的好友栏刷新
- bool _hasAcceptAnyFriends = false;
- void EnterFriendRequestBox() {
- friendRequestBox.SetActive(true);
- for (int i = 1; i < GetFriendRequestItemParent().childCount; i++) {
- Destroy(GetFriendRequestItemParent().GetChild(i).gameObject);
- }
- Action<JArray> cb = delegate(JArray list) {
- if (list.Count > 0) {
- foreach (var itemInfo in list) {
- int otherID = itemInfo.Value<int>("otherID");
- int avatarID = itemInfo.Value<int>("avatarID");
- string avatarUrl = itemInfo.Value<string>("avatarUrl");
- string nickname = itemInfo.Value<string>("nickname");
- bool online = itemInfo.Value<bool>("online");
- long offlineTime = itemInfo.Value<long>("offlineTime");
- GameObject o = GameObject.Instantiate(GetFriendRequestItemPrefab(), GetFriendRequestItemParent());
- o.SetActive(true);
- o.name = otherID.ToString();
- RoleMgr.SetAvatarToImage(
- o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
- avatarID, avatarUrl);
- o.transform.Find("Name").GetComponent<Text>().text = nickname;
- o.transform.Find("BG0").gameObject.SetActive(!online);
- o.transform.Find("BG1").gameObject.SetActive(online);
- Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
- onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
- onlineTip.color = online ? Color.green : Color.gray;
- Button btnNo = o.transform.Find("BtnNo").GetComponent<Button>();
- btnNo.onClick.RemoveAllListeners();
- btnNo.onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{false, otherID});
- Destroy(o);
- UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
- });
- Button btnYes = o.transform.Find("BtnYes").GetComponent<Button>();
- btnYes.onClick.RemoveAllListeners();
- btnYes.onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- UserPlayer.ins.call("friendComp.decideAddFriend", new object[]{true, otherID});
- Destroy(o);
- _hasAcceptAnyFriends = true;
- UserPlayer.ins.tempData.hasFriendRequest = GetFriendRequestItemParent().childCount - 1 > 1;
- });
- }
- } else {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("好友请求列表为空"));
- }
- };
- UserPlayer.ins.call("friendComp.getFriendRequestList", null, cb);
- PopupMgr.ins.ClearAllTip();
- }
- #endregion
- #region friendRecommend
- void InitFriendRecommendBox() {
- friendRecommendBox.SetActive(false);
- GetFriendRecommendItemPrefab().SetActive(false);
- }
- Transform GetFriendRecommendItemParent() {
- return friendRecommendBox.transform.Find("ScrollView/Viewport/Content");
- }
- GameObject GetFriendRecommendItemPrefab() {
- return GetFriendRecommendItemParent().Find("Item").gameObject;
- }
- void EnterFriendRecommendBox() {
- friendRecommendBox.SetActive(true);
- for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
- Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
- }
- Action<JArray> cb = delegate(JArray list) {
- for (int i = 1; i < GetFriendRecommendItemParent().childCount; i++) {
- Destroy(GetFriendRecommendItemParent().GetChild(i).gameObject);
- }
- if (list.Count > 0) {
- foreach (var itemInfo in list) {
- int userID = itemInfo.Value<int>("id");
- int avatarID = itemInfo.Value<int>("avatarID");
- string avatarUrl = itemInfo.Value<string>("avatarUrl");
- string nickname = itemInfo.Value<string>("nickname");
- bool online = itemInfo.Value<bool>("online");
- long offlineTime = itemInfo.Value<long>("offlineTime");
- int playCount = itemInfo.Value<int>("playCount");
- GameObject o = GameObject.Instantiate(GetFriendRecommendItemPrefab(), GetFriendRecommendItemParent());
- o.SetActive(true);
- o.name = userID.ToString();
- RoleMgr.SetAvatarToImage(
- o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
- avatarID, avatarUrl
- );
- o.transform.Find("Name").GetComponent<Text>().text = nickname;
- o.transform.Find("PlayCount").GetComponent<Text>().text =
- String.Format(
- TextAutoLanguage2.GetTextByKey("friend_record_play-again-count"), playCount
- );
- o.transform.Find("BG0").gameObject.SetActive(!online);
- o.transform.Find("BG1").gameObject.SetActive(online);
- Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
- onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
- onlineTip.color = online ? Color.green : Color.gray;
- Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
- btnYes.onClick.RemoveAllListeners();
- btnYes.onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- UserPlayer.ins.call("friendComp.requestAddFriend", userID);
- btnYes.interactable = false;
- });
- }
- } else {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有可推荐的好友"));
- }
- };
- UserPlayer.ins.call("friendComp.getFriendRecommendList", null, cb);
- PopupMgr.ins.ClearAllTip();
- }
- #endregion
- #region searchPlayer
- void InitSearchPlayerBox() {
- searchPlayerBox.SetActive(false);
- GetSearchPlayerItemPrefab().SetActive(false);
- }
- Transform GetSearchPlayerItemParent() {
- return searchPlayerBox.transform.Find("ScrollView/Viewport/Content");
- }
- GameObject GetSearchPlayerItemPrefab() {
- return GetSearchPlayerItemParent().Find("Item").gameObject;
- }
- void EnterSearchPlayerBox() {
- searchPlayerBox.SetActive(true);
- PopupMgr.ins.ClearAllTip();
- }
- InputField GetSearchInputField() {
- return searchPlayerBox.transform.Find("SearchBox/InputField").GetComponent<InputField>();
- }
- long _lastSearchTime = 0;
- public void btnEvent_SearchPlayer() {
- AudioMgr.ins.PlayBtn();
- if (JCUnityLib.TimeUtils.GetTimestamp() - _lastSearchTime < 3000) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
- return;
- } else {
- _lastSearchTime = JCUnityLib.TimeUtils.GetTimestamp();
- }
- string searchText = GetSearchInputField().text;
- Action<JArray> cb = delegate(JArray list) {
- for (int i = 1; i < GetSearchPlayerItemParent().childCount; i++) {
- Destroy(GetSearchPlayerItemParent().GetChild(i).gameObject);
- }
- if (list.Count > 0) {
- foreach (var itemInfo in list) {
- int userID = itemInfo.Value<int>("id");
- int avatarID = itemInfo.Value<int>("avatarID");
- string avatarUrl = itemInfo.Value<string>("avatarUrl");
- string nickname = itemInfo.Value<string>("nickname");
- bool online = itemInfo.Value<bool>("online");
- long offlineTime = itemInfo.Value<long>("offlineTime");
- GameObject o = GameObject.Instantiate(GetSearchPlayerItemPrefab(), GetSearchPlayerItemParent());
- o.SetActive(true);
- o.name = userID.ToString();
- RoleMgr.SetAvatarToImage(
- o.transform.Find("Avatar/Sprite").GetComponent<Image>(),
- avatarID, avatarUrl
- );
- o.transform.Find("Name").GetComponent<Text>().text = nickname;
- o.transform.Find("BG0").gameObject.SetActive(!online);
- o.transform.Find("BG1").gameObject.SetActive(online);
- Text onlineTip = o.transform.Find("OnlineTip").GetComponent<Text>();
- onlineTip.text = TimeUtil.GetOfflineTimeStr(offlineTime, online);
- onlineTip.color = online ? Color.green : Color.gray;
- Button btnYes = o.transform.Find("BtnAdd").GetComponent<Button>();
- btnYes.onClick.RemoveAllListeners();
- btnYes.onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- UserPlayer.ins.call("friendComp.requestAddFriend", userID);
- btnYes.interactable = false;
- });
- }
- } else {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("没有搜索到目标玩家"));
- }
- };
- UserPlayer.ins.call("friendComp.searchPlayersByID", new object[]{searchText}, cb);
- }
- #endregion
- void HideAllBox() {
- myFriendBox.SetActive(false);
- friendRequestBox.SetActive(false);
- friendRecommendBox.SetActive(false);
- searchPlayerBox.SetActive(false);
- }
- public void Back() {
- AudioMgr.ins.PlayBtn();
- ViewMgr.Instance.DestroyView<FriendView>();
- }
- #region 左栏条目切换
- [SerializeField] Sprite[] btnTabTextures;
- Button[] btnTabs = {null, null, null, null};
- void InitBtnTabs() {
- btnTabs[0] = this.transform.Find("LineV/BtnTab0").GetComponent<Button>();
- btnTabs[1] =this.transform.Find("LineV/BtnTab1").GetComponent<Button>();
- btnTabs[2] =this.transform.Find("LineV/BtnTab2").GetComponent<Button>();
- btnTabs[3] =this.transform.Find("LineV/BtnTab3").GetComponent<Button>();
- for (int i = 0; i < btnTabs.Length; i++) {
- Button btnTab = btnTabs[i];
- int btnTabIndex = i;
- btnTab.onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- if (!IsBtnTabSelected(btnTab)) {
- SetBtnTabSelected(btnTab);
- }
- });
- }
- }
- bool IsBtnTabSelected(Button btn) {
- Image img = btn.GetComponent<Image>();
- return img.sprite.name.Equals(btnTabTextures[1].name);
- }
- void SetBtnTabSelected(Button btn) {
- foreach (var item in btnTabs) {
- Image img = item.GetComponent<Image>();
- img.sprite = item == btn ? btnTabTextures[1] : btnTabTextures[0];
- }
- int index = Array.IndexOf(btnTabs, btn);
- HandleBtnTabSelectedLogic(index);
- }
- void HandleBtnTabSelectedLogic(int btnIndex) {
- HideAllBox();
- if (btnIndex == 0) {
- EnterMyFriendBox();
- } else if (btnIndex == 1) {
- EnterFriendRequestBox();
- } else if (btnIndex == 2) {
- EnterFriendRecommendBox();
- } else if (btnIndex == 3) {
- EnterSearchPlayerBox();
- }
- }
- #endregion
- }
|