2021-08-25 15:35:41 +08:00

251 lines
10 KiB
C#

using Assets.Scenes.Ride.Scripts;
using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using Assets.Scripts.UI.Control;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class HomeController : PFUIPanel
{
//[SerializeField] Text Ftp;
//[SerializeField] RawImage Avatar;
//[SerializeField] Text Weight;
//[SerializeField] Text WKG;
//[SerializeField] Text Calories;
//[SerializeField] Text KM;
//[SerializeField] Text Climb;
//[SerializeField] Text NickName;
//[SerializeField] Button More;
[SerializeField] Button BtnRide;
[SerializeField] Transform userInfo;
Transform BtnMatch, BtnTraining;
private MainNav mainNav;
private Dictionary<string, Sprite[]> dict;
// Start is called before the first frame update
protected override void Start()
{
base.Start();
mainNav = this.transform.Find("MainNav").GetComponent<MainNav>();
mainNav.ShowExit();
var BtnContainer = transform.Find("MainFuncContainer");
UIManager.AddEvent(BtnRide.gameObject, EventTriggerType.PointerClick, GoRide);
UIManager.AddEvent(BtnRide.gameObject, EventTriggerType.PointerEnter, OnHover);
UIManager.AddEvent(BtnRide.gameObject, EventTriggerType.PointerExit, OnExit);
BtnMatch = BtnContainer.Find("BtnMatch");
//BtnMatch.GetComponent<Button>().onClick.AddListener(() =>
//{
// Debug.Log(45);
//});
UIManager.AddEvent(BtnMatch.gameObject, EventTriggerType.PointerClick, GoMatch);
UIManager.AddEvent(BtnMatch.gameObject, EventTriggerType.PointerEnter, OnHover);
UIManager.AddEvent(BtnMatch.gameObject, EventTriggerType.PointerExit, OnExit);
BtnTraining = BtnContainer.Find("BtnTraining");
UIManager.AddEvent(BtnTraining.gameObject, EventTriggerType.PointerClick, (e) =>
{
Debug.Log(Application.platform);
goTraining(e);
});
UIManager.AddEvent(BtnTraining.gameObject, EventTriggerType.PointerEnter, OnHover);
UIManager.AddEvent(BtnTraining.gameObject, EventTriggerType.PointerExit, OnExit);
var avatar = userInfo.Find("Avatar").GetComponent<RawImage>();
var rect = ((RectTransform)avatar.transform).rect;
SetRounded(avatar.transform, rect.height);
UIManager.AddEvent(avatar.gameObject, EventTriggerType.PointerClick, x =>
{
UIManager.ShowUserInfoPanel();
});
UIManager.AddEvent(userInfo.Find("BtnMore").gameObject, EventTriggerType.PointerClick, x =>
{
UIManager.ShowUserInfoPanel();
//UIManager.ShowConfirm("Quit", "Do you want to quit PowerFun?", null);
});
//GetSummary();
dict = new Dictionary<string, Sprite[]>
{
//{
// "BtnMatch",new Sprite[]
// {
// Resources.Load<Sprite>("Images/home/Entrance - MATCH_soon"),
// Resources.Load<Sprite>("Images/home/Entrance - MATCH_soon_hover"),
// }
//},
//{
// "BtnTraining",new Sprite[]
// {
// Resources.Load<Sprite>("Images/home/Entrance - TRAINING_soon"),
// Resources.Load<Sprite>("Images/home/Entrance - TRAINING_soon_hover"),
// }
//},
};
}
private void GoRide(BaseEventData e)
{
Debug.Log(App.firstEnter.ToString());
//UIManager.ShowEarthPanel(App.latitude, App.longitude);
#if UNITY_ANDROID || UNITY_IOS
UIManager.ShowMapListPanel();
#else
if (App.firstEnter == 0)
{
App.firstEnter = 1;
UIManager.ShowEarthPanel(App.latitude, App.longitude);
}
else
{
UIManager.ShowMapListPanel();
}
#endif
OnExit(e);
}
void goTraining(BaseEventData e)
{
#if !(UNITY_IOS || UNITY_ANDROID)
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\PowerFun\Rhino.PowerFun.exe";
//Debug.Log(System.IO.File.Exists(path + ));
if (!System.IO.File.Exists(path))
{
//TODO: auto update
UIManager.ShowDownloadWorkoutsPanel();
//Application.OpenURL("https://powerfun.oss-cn-shanghai.aliyuncs.com/Releases/PowerFunWorkouts.exe");
return;
}
UIManager.ShowConfirm("Switch to PowerFun Workouts?", "We will close the Powerfun and start PowerFun Workouts.", () =>
{
Utils.ExecFile(path);
Application.Quit();
});
#else
Application.OpenURL("powerfun://app");
#endif
}
//进入比赛
private void GoMatch(BaseEventData e)
{
UIManager.ShowRaceHomePanel();
OnExit(e);
}
private void OnHover(BaseEventData e)
{
var pe = (PointerEventData)e;
var parent = pe.pointerEnter.transform.parent;
if (dict.ContainsKey(parent.name))
{
parent.Find("Image").GetComponent<Image>().sprite = dict[parent.name][1];
}
//parent.Find("Image").GetComponent<Image>().sprite = Resources.Load<Sprite>("Images/home/Entrance - ROUTES_hover");
//parent.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
parent.DOScale(new Vector3(1.05f, 1.05f, 0), 0.3f);
//parent.GetComponent<RectTransform>().localScale = ;
}
private void OnExit(BaseEventData e)
{
var pe = (PointerEventData)e;
var parent = pe.pointerEnter.transform.parent;
//parent.Find("Image").GetComponent<Image>().sprite = Resources.Load<Sprite>("Images/home/Entrance - ROUTES_nomal");
//parent.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#5C5C6E");
if (dict.ContainsKey(parent.name))
{
parent.Find("Image").GetComponent<Image>().sprite = dict[parent.name][0];
}
parent.DOScale(new Vector3(1f, 1f, 0), 0.3f);
//parent.GetComponent<RectTransform>().localScale = new Vector3(1f, 1f, 0);
}
void MoreFunc()
{
//Utils.showToast(gameObject, "更多信息", 1);
//DOTween.ToAlpha(() => )
}
void SetCurrentUser(SummaryResultModel summary)
{
userInfo.Find("NickName").GetComponent<Text>().text = App.CurrentUser.Nickname;
userInfo.Find("GroupTop").Find("FtpContainer").Find("FtpValue").GetComponent<Text>().text = App.CurrentUser.FTP.ToString();
userInfo.Find("GroupTop").Find("WeightContainer").Find("WeightValue").GetComponent<Text>().text = App.CurrentUser.Weight.ToString();
userInfo.Find("GroupTop").Find("WKGContainer").Find("WKGValue").GetComponent<Text>().text = $"{ (App.CurrentUser.FTP/ App.CurrentUser.Weight).ToString("0.0") }";
userInfo.Find("IDText").GetComponent<Text>().text = "ID:" + App.CurrentUser.Id.ToString("000000");
if (!string.IsNullOrWhiteSpace(App.CurrentUser.Unionid))
{
userInfo.Find("Wx").GetComponent<Image>().sprite = Resources.Load<Sprite>("Images/Wechat_person_1");
}
if (App.CurrentUser.Sex == 2)
{
userInfo.Find("SexIcon").GetComponent<Image>().sprite = Resources.Load<Sprite>("Images/woman");
}
else
{
userInfo.Find("SexIcon").GetComponent<Image>().sprite = Resources.Load<Sprite>("Images/man");
}
Utils.DisplayHead(userInfo.Find("Avatar").GetComponent<RawImage>(), App.CurrentUser.WxHeadImg);
userInfo.Find("Country").GetComponent<RawImage>().texture = UIManager.Instance.loginRegOptions.GetCountryImageByName(App.CurrentUser.Country);
userInfo.Find("CaloriesContainer").Find("CaloriesValue").GetComponent<Text>().text = double.Parse(summary.Kcal,CultureInfo.InvariantCulture).ToString("0") +" KCAL";
userInfo.Find("KMContainer").Find("KMValue").GetComponent<Text>().text = summary.TotalDistance.ToString("0") +" KM";
userInfo.Find("ClimbContainer").Find("ClimbValue").GetComponent<Text>().text = double.Parse(summary.TotalClimb,CultureInfo.InvariantCulture).ToString("0") + " M";
userInfo.Find("GroupTop").Find("FtpContainer").Find("FtpTime").GetComponent<Text>().text = App.CurrentUser.LastUpdateFtpTime.ToString("dd.MM.yyyy");
userInfo.Find("GroupTop").Find("WeightContainer").Find("WeightTime").GetComponent<Text>().text = App.CurrentUser.LastUpdateWeightTime.ToString("dd.MM.yyyy");
userInfo.Find("GroupTop").Find("WKGContainer").Find("WKGTime").GetComponent<Text>().text =
DateTime.Compare(App.CurrentUser.LastUpdateFtpTime, App.CurrentUser.LastUpdateWeightTime)>0
? App.CurrentUser.LastUpdateFtpTime.ToString("dd.MM.yyyy")
: App.CurrentUser.LastUpdateWeightTime.ToString("dd.MM.yyyy");
}
async void GetSummary()
{
var res = await ConfigHelper.userApi.GetSummary();
if (res.result)
{
//App.CurrentUser.Distance = res.data.TotalDistance;
//App.CurrentUser.Climb = res.data.TotalClimb;
//App.CurrentUser.Carlories = res.data.Kcal;
SetCurrentUser(res.data);
//Climb.text = $"{res.data.TotalClimb.ToString()}M";
//Calories.text = $"{res.data.Kcal.ToString()}KCAL";
//Calories.text = res.data.
}
else
{
Utils.showToast(gameObject, res.errMsg);
}
//var res = await NoAuthApi.GetCurrentUser();
//if (res.result)
//{
// Global.CurrentUser = res.data;
// SetCurrentUser();
//}
//else
//{
// Utils.showToast(gameObject, res.errMsg);
//}
}
// Update is called once per frame
void Update()
{
}
public override void Show()
{
var cg = this.GetComponent<CanvasGroup>();
cg.alpha = 0;
base.Show();
cg.DOFade(1f, 0.3f);
GetSummary();
}
}