| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using System;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- /* 主界面 */
- public class HomeView : JCUnityLib.ViewBase
- {
- [SerializeField] Image myAvatarSprite;
- [SerializeField] Text nickNameText;
- [SerializeField] GameObject[] genders;
- [SerializeField] GameObject btnConnectBow;
- [SerializeField] GameObject btnConnectArrow;
- [SerializeField] GameObject friendTip;
- public static HomeView ins;
- void Awake() {
- if (CommonConfig.needToExamine) {
- transform.Find("LeftPanel/Item (1)").gameObject.SetActive(false);
- transform.Find("RightPanel/Item/Text").GetComponent<TextAutoLanguage>().SetText(1234562);
- transform.Find("RightPanel/Item (1)/Text").GetComponent<TextAutoLanguage>().SetText(1234563);
- transform.Find("RightPanel/Item (3)").gameObject.SetActive(false);
- }
- }
- void Start()
- {
- ins = this;
- BluetoothHolder.Init();
- AudioMgr.Init();
- TopBarView.NeedShowIt(this);
- if (ShootCheck.ins) ShootCheck.ins.AdjustNormalOrHightMode();
- if (LoginMgr.myUserInfo.id > 0) {
- RenderNameOrGender();
- RenderMyAvatarSprite();
- RenderDeviceNames();
- }
- StartCoroutine(RefreshFriendBar());
- }
- void OnDestroy()
- {
- if (ins == this) ins = null;
- TopBarView.DontNeedShowIt(this);
- }
- void Update() {
- if (UserPlayer.ins != null) {
- if (UserPlayer.ins.tempData.hasFriendRequest && !friendTip.activeSelf) {
- friendTip.SetActive(true);
- }
- else if (!UserPlayer.ins.tempData.hasFriendRequest && friendTip.activeSelf) {
- friendTip.SetActive(false);
- }
- }
- UpdateBtnForConnect();
- }
- BluetoothStatusEnum bowStatus;
- void UpdateBtnForConnect() {
- if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status) {
- bowStatus = BluetoothAim.ins.status;
- (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
- btnConnectBow.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
- btnConnectBow.GetComponentInChildren<Text>().color = color;
- btnConnectBow.transform.Find("Check").gameObject.SetActive(bowStatus == BluetoothStatusEnum.ConnectSuccess);
- }
- }
- public static bool ShowProminentBeforeConnectBLE(Action onAgree = null)
- {
- if (PlayerPrefs.GetInt("Location-Prominent", 0) == 0) {
- ModalView m = ModalView.Show();
- string pName = Application.productName;
- m.textKey = "Model_Location-Prominent";
- m.textFormatArgs = new object[] {pName};
- m.onAgree = () => {
- PlayerPrefs.SetInt("Location-Prominent", 1);
- try {
- onAgree?.Invoke();
- } catch (System.Exception e) {
- Debug.LogError(e.Message + "\n" + e.StackTrace);
- }
- };
- return true;
- }
- return false;
- }
- public void OnClick_ConnectBLE() {
- if (ShowProminentBeforeConnectBLE()) return;
- BluetoothAim.ins.DoConnect();
- }
- public void OnClick_ShowDeviceView() {
- AudioMgr.ins.PlayBtn();
- ViewMgr.Instance.ShowView<DeviceView>();
- }
- public void RenderNameOrGender() {
- nickNameText.text = LoginMgr.myUserInfo.nickname;
- genders[LoginMgr.myUserInfo.gender == 2 ? 1 : 0].SetActive(true);
- genders[LoginMgr.myUserInfo.gender == 2 ? 0 : 1].SetActive(false);
- LayoutRebuilder.ForceRebuildLayoutImmediate(nickNameText.transform.parent.GetComponent<RectTransform>());
- }
- public void RenderMyAvatarSprite() {
- myAvatarSprite.sprite = RoleMgr.GetAvatar(LoginMgr.myUserInfo.avatarID);
- }
- public void RenderDeviceNames()
- {
- try {
- (DeviceInfo bowInfo, DeviceInfo arrowInfo) = DeviceMgr.ins.GetCurrentBowArrowInfo();
- // this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(bowInfo.config.name);
- this.transform.Find("ShowBow/Text").GetComponent<TextAutoLanguage>().SetText(200000);
- this.transform.Find("ShowArrow/Text").GetComponent<TextAutoLanguage>().SetText(arrowInfo.config.name);
- } catch (System.Exception) {}
- }
- public Action action_OnClickStartGame;
- public void GoTo(string target) {
- AudioMgr.ins.PlayBtn();
- switch (target)
- {
- case "开始游戏":
- if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
- return;
- }
- ViewMgr.Instance.ShowView<GameStartView>();
- action_OnClickStartGame?.Invoke();
- break;
- case "联机游戏":
- if (CommonConfig.isReleaseVersion && !BluetoothStatus.IsAllConnected()) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
- return;
- }
- GlobalDataTemp.pkMatchType = PKMatchType.OnlinePK;
- ViewMgr.Instance.ShowView<PKGameOptionView>();
- break;
- case "我的":
- ViewMgr.Instance.ShowView<MeView>();
- break;
- default:
- break;
- }
- }
- /* FriendBar */
- GameObject friendItemPrefab = null;
- Transform friendBarContent = null;
- IEnumerator RefreshFriendBar() {
- if (friendItemPrefab == null) {
- friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
- Destroy(friendItemPrefab.GetComponent<Image>());
- friendItemPrefab.SetActive(false);
- }
- if (friendBarContent == null) {
- friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
- }
- while (LoginMgr.myUserInfo.id < 1) {
- yield return null;
- }
- Action<JArray> cb = RenderFriendList;
- UserPlayer.ins.call("friendComp.getMyFriends", null, cb);
- }
- public void RenderFriendList(JArray list) {
- if (flag_SwapFriendAndRank != 0) return;
- for (int i = 1; i < friendBarContent.childCount; i++) {
- Destroy(friendBarContent.GetChild(i).gameObject);
- }
- if (list.Count > 0) {
- Color outColor;
- foreach (var itemInfo in list) {
- int friendID = itemInfo.Value<int>("friendID");
- int avatarID = itemInfo.Value<int>("avatarID");
- string nickname = itemInfo.Value<string>("nickname");
- bool online = itemInfo.Value<bool>("online");
- long offlineTime = itemInfo.Value<long>("offlineTime");
- GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
- o.SetActive(true);
- o.name = friendID.ToString();
- o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
- o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
- TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
- TimeUtil.SetOfflineTimeTextKey(offlineTime, online, timeTip);
- if (online) {
- ColorUtility.TryParseHtmlString("#12B525", out outColor);
- } else {
- ColorUtility.TryParseHtmlString("#A8B2BD", out outColor);
- }
- timeTip.GetComponent<Text>().color = outColor;
- }
- }
- }
- IEnumerator RefreshFriendRankBar() {
- if (friendItemPrefab == null) {
- friendItemPrefab = transform.Find("FriendBar/Scroll View/Viewport/Content/Item").gameObject;
- Destroy(friendItemPrefab.GetComponent<Image>());
- friendItemPrefab.SetActive(false);
- }
- if (friendBarContent == null) {
- friendBarContent = transform.Find("FriendBar/Scroll View/Viewport/Content");
- }
- while (LoginMgr.myUserInfo.id < 1) {
- yield return null;
- }
- Action<JArray> cb = RenderFriendRankList;
- UserPlayer.ins.call("rankComp.getFriendRankList", null, cb);
- }
- void RenderFriendRankList(JArray list) {
- if (flag_SwapFriendAndRank != 1) return;
- for (int i = 1; i < friendBarContent.childCount; i++) {
- Destroy(friendBarContent.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 nickname = itemInfo.Value<string>("nickname");
- bool online = itemInfo.Value<bool>("online");
- long offlineTime = itemInfo.Value<long>("offlineTime");
- int rankNum = itemInfo.Value<int>("rankNum");
- GameObject o = GameObject.Instantiate(friendItemPrefab, friendBarContent);
- o.SetActive(true);
- o.name = userID.ToString();
- o.transform.Find("Avatar").GetComponent<Image>().sprite = RoleMgr.GetAvatar(avatarID);
- o.transform.Find("Nickname").GetComponent<Text>().text = nickname;
- TextAutoLanguage2 timeTip = o.transform.Find("Avatar/Line/Text").GetComponent<TextAutoLanguage2>();
- Action<Text> onApplyToNext = (t) => {
- if (TextAutoLanguage2.GetLanguage() == 0) {
- t.text = $"第{rankNum}名";
- } else {
- string str = rankNum.ToString();
- if (str.EndsWith("1")) t.text = $"{rankNum}st";
- else if (str.EndsWith("2")) t.text = $"{rankNum}nd";
- else if (str.EndsWith("3")) t.text = $"{rankNum}rd";
- else t.text = $"{rankNum}th";
- }
- Color outColor;
- if (rankNum == 1) ColorUtility.TryParseHtmlString("#F0E68C", out outColor);
- else if (rankNum == 2) ColorUtility.TryParseHtmlString("#E0FFFF", out outColor);
- else if (rankNum == 3) ColorUtility.TryParseHtmlString("#CD8162", out outColor);
- else ColorUtility.TryParseHtmlString("#EEE5DE", out outColor);
- t.color = outColor;
- };
- timeTip.onApplyToNext += onApplyToNext;
- }
- }
- }
- int flag_SwapFriendAndRank = 0;
- public void OnClick_SwapFriendAndRank() {
- AudioMgr.ins.PlayBtn();
- if (flag_SwapFriendAndRank == 0) {
- flag_SwapFriendAndRank = 1;
- transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("rank_title");
- StartCoroutine(RefreshFriendRankBar());
- } else if (flag_SwapFriendAndRank == 1) {
- flag_SwapFriendAndRank = 0;
- transform.Find("FriendBar/FrameBtnTop").GetComponentInChildren<TextAutoLanguage2>().SetTextKey("friend_title");
- StartCoroutine(RefreshFriendBar());
- }
- }
- public void OnClick_ShowDetail() {
- AudioMgr.ins.PlayBtn();
- if (flag_SwapFriendAndRank == 0) {
- ViewMgr.Instance.ShowView<FriendView>();
- } else if (flag_SwapFriendAndRank == 1) {
- ViewMgr.Instance.ShowView<RankView>();
- }
- }
- }
|