380 lines
15 KiB
C#
Raw Normal View History

using Assets.Scripts;
2021-09-23 18:14:53 +08:00
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
2021-09-06 16:53:48 +08:00
public class RouteItem : MonoBehaviour
#if !(UNITY_ANDROID || UNITY_IOS)
,IPointerEnterHandler,IPointerExitHandler
#endif
{
RouteResult routeResult;
// Start is called before the first frame update
2021-09-06 16:53:48 +08:00
Transform left,row1,row2,right,rightDot;
Transform btnReRide, btnContinue, btnDelete;
2021-07-29 19:55:29 +08:00
Transform btnDetail;
void Start()
{
}
2021-09-23 18:14:53 +08:00
// Update is called once per frame
void Update()
{
}
string titleColor = "#5c5c6e";
2021-04-19 14:36:08 +08:00
RouteResult data;
2021-04-19 15:44:11 +08:00
//滑动索引 0route 1match
int index;
//交互用
private Transform parent { get; set; }
2021-09-23 18:14:53 +08:00
public void Initial(object result, int index, Transform parent = null,bool top = false)
{
2021-09-23 18:14:53 +08:00
this.index = index;
if (parent)
{
this.parent = parent;
}
2021-09-06 16:53:48 +08:00
left = transform.Find("BigLeft/Left");
row1 = left.Find("Main").Find("Row1");
row2 = left.Find("Main").Find("Row2");
2021-09-23 18:14:53 +08:00
rightDot = transform.Find("RightDot");
if (rightDot != null)
{
UIManager.AddEvent(rightDot.Find("BtnDot").gameObject, EventTriggerType.PointerClick, (b) => OnPointerEnter(null));
UIManager.AddEvent(rightDot.Find("BtnDelete").gameObject, EventTriggerType.PointerClick, (b) => Delete());
}
//right.gameObject.SetActive(true);
#if !(UNITY_ANDROID || UNITY_IOS)
gameObject.GetComponent<Button>().onClick.AddListener(() =>
{
Detail();
});
#else
gameObject.GetComponent<Button>().enabled = false;
#endif
if (result is RouteResult)
{
Initial((RouteResult)result, index, parent);
}
}
2021-05-08 14:38:23 +08:00
2021-09-23 18:14:53 +08:00
public void Initial(RouteResult result,int index,Transform parent = null)
{
routeResult = result;
Utils.DisplayImageTempDict(transform.Find("BigLeft/CoverImage").GetComponent<RawImage>(), result.RouteImage,
parent.GetComponent<ResultListController>().caches);
//Utils.DisplayImage(transform.Find("BigLeft/CoverImage").GetComponent<RawImage>(), result.RouteImage, true);
2021-05-08 14:38:23 +08:00
left.Find("Main").Find("Name").GetComponent<Text>().text = result.RouteName;
2021-12-30 16:51:04 +08:00
left.Find("Main").Find("Time").GetComponent<Text>().text = result.CreateTime.ToLocalString("HH:mm:ss dd-MM-yyyy");
2021-12-03 18:19:24 +08:00
row1.Find("Time").GetComponent<Text>().text = $"<color={titleColor}>{App.GetLocalString("Riding Time")}:</color>{result.TrainingTime}";
row1.Find("Distance").GetComponent<Text>().text = $"<color={titleColor}>{App.GetLocalString("Mileage")}:</color>{result.EndDistance.ToString("#0.00")}KM";
row1.Find("Times").GetComponent<Text>().text = $"<color={titleColor}>{App.GetLocalString("Times")}:</color>{result.Count}";
2021-04-19 15:44:11 +08:00
if (index == 0)
{
2021-12-03 18:19:24 +08:00
row1.Find("Rank").GetComponent<Text>().text = $"<color={titleColor}>{App.GetLocalString("Rank")}:</color>{result.Ranking}";
2021-04-19 15:44:11 +08:00
}
else
{
row1.Find("Rank").GetComponent<Text>().text = $"";
}
left.Find("Progress").Find("Image").GetComponent<Image>().fillAmount = (float)result.Progress;
left.Find("Progress").Find("Value").GetComponent<Text>().text = (result.Progress * 100).ToString("#0");
2021-12-03 18:19:24 +08:00
row2.Find("Device").GetComponent<Text>().text = $"<color={titleColor}>{App.GetLocalString("Cycling Equipment")}:</color>{result.ManufacturerName}";
2021-04-19 15:44:11 +08:00
if (index == 0)
{
2021-04-19 15:44:11 +08:00
right = transform.Find("Right");
2021-07-29 19:55:29 +08:00
right.gameObject.SetActive(true);
2021-04-19 15:44:11 +08:00
btnContinue = right.Find("BtnContinue");
if (btnContinue)
{
2021-04-25 19:41:35 +08:00
bool enabled = result.ContinueCyclingParam != null;
2021-04-19 15:44:11 +08:00
//btnContinue.gameObject.SetActive(false);
2021-04-25 15:33:17 +08:00
//btnContinue.GetComponent<Button>().onClick.AddListener(GoContinue);
UIManager.AddEvent(btnContinue.gameObject, EventTriggerType.PointerClick, (b) => GoContinue());
2021-04-25 19:41:35 +08:00
btnContinue.GetComponent<Button>().interactable = enabled;
btnContinue.GetComponent<Button>().enabled = enabled;
2021-04-19 15:44:11 +08:00
}
btnReRide = right.Find("BtnReRide");
if (btnReRide)
{
//btnReRide.gameObject.SetActive(false);
2021-04-25 15:33:17 +08:00
UIManager.AddEvent(btnReRide.gameObject, EventTriggerType.PointerClick, (b) => GoReRide());
//btnReRide.GetComponent<Button>().onClick.AddListener(GoReRide);
2021-04-19 15:44:11 +08:00
}
btnDelete = right.Find("BtnDelete");
if (btnDelete)
{
//btnDelete.gameObject.SetActive(false);
2021-04-25 15:33:17 +08:00
//btnDelete.GetComponent<Button>().onClick.AddListener(Delete);
2021-09-28 11:30:51 +08:00
#if UNITY_ANDROID || UNITY_IOS
2021-12-17 13:03:19 +08:00
UIManager.AddEvent(left.Find("Main/ShareContainer/Wx").gameObject, EventTriggerType.PointerClick, b =>
{
Debug.Log(App.CurrentUser.WebHost);
2021-12-17 18:28:02 +08:00
if (App.weChatController.IsWeChatAppInstalled())
{
2021-12-29 17:05:58 +08:00
App.weChatController.ShareWebpageToWX(0, $"http://{App.CurrentUser.WebHost}/RoutesRecords/" + result.RankingId, result.RouteName, "By " + App.CurrentUser.Nickname, null);
2021-12-17 18:28:02 +08:00
}
else
{
Utils.showToast(null, "未安装微信");
}
2021-12-17 13:03:19 +08:00
});
2021-09-06 16:53:48 +08:00
UIManager.AddEvent(btnDelete.gameObject, EventTriggerType.PointerClick, (b) => Detail());
2021-09-28 11:30:51 +08:00
#else
UIManager.AddEvent(btnDelete.gameObject, EventTriggerType.PointerClick, (b) => Delete());
#endif
2021-04-19 15:44:11 +08:00
}
SetButtonColor(false);
transform.Find("RightMatch").gameObject.SetActive(false);
}
2021-04-19 15:44:11 +08:00
else
{
2021-09-02 14:18:26 +08:00
//Debug.Log(index);
2021-04-19 15:44:11 +08:00
right = transform.Find("RightMatch");
2021-07-29 19:55:29 +08:00
right.gameObject.SetActive(true);
btnDetail = right.Find("BtnDetail");
UIManager.AddEvent(btnDetail.gameObject, EventTriggerType.PointerClick, (b) => Detail());
2021-07-29 13:55:33 +08:00
UIManager.AddEvent(right.Find("BtnRide").gameObject, EventTriggerType.PointerClick, (b) => Ride());
2021-07-29 19:55:29 +08:00
SetButtonColor(false);
transform.Find("Right").gameObject.SetActive(false);
}
2021-09-23 18:14:53 +08:00
}
2021-07-29 13:55:33 +08:00
void Detail()
{
2021-07-29 19:55:29 +08:00
var url = routeResult.ViewUrl + "?Token=" + App.CurrentUser.cookie;
if (index == 1)
{
url += "&raceId=" + routeResult.MapCompetitionId;
}
Application.OpenURL(url);
2021-07-29 13:55:33 +08:00
}
void Ride()
{
2021-07-29 19:55:29 +08:00
UIManager.ShowConfirm("Ride", "Ride this route?", () =>
{
App.RouteIdParam = routeResult.RouteId;
App.routeResult = routeResult;
App.routeResult.ContinueCyclingParam = null;
RecordFromUrl();
SceneManager.LoadScene("Ride");
});
2021-07-29 13:55:33 +08:00
}
private void Delete()
{
2021-04-28 15:21:06 +08:00
UIManager.ShowConfirm("Delete", "Delete this record?", async () =>
{
2021-09-23 18:14:53 +08:00
JsonResult<object> r = null;
if (index != 2)
{
r = await ConfigHelper.mapApi.DeleteRecord(routeResult.Id);
}
2021-04-28 15:21:06 +08:00
UIManager.CloseConfirm();
if (r.result)
{
DestroyImmediate(gameObject);
}
2021-09-23 18:14:53 +08:00
else
2021-04-28 15:21:06 +08:00
{
Utils.showToast(gameObject, r.errMsg);
}
});
}
private void GoReRide()
{
2021-04-28 15:21:06 +08:00
UIManager.ShowConfirm("ReRide", "Ride again?", () =>
{
App.RouteIdParam = routeResult.RouteId;
2021-11-11 17:45:02 +08:00
App.routeResult = null;//routeResult;
//App.routeResult.ContinueCyclingParam = null;
RecordFromUrl();
2021-04-28 15:21:06 +08:00
SceneManager.LoadScene("Ride");
});
}
private void RecordFromUrl()
{
if (App.MainSceneParam.ContainsKey("Name"))
{
App.MainSceneParam["Name"] = "UserInfoPanel";
}
else
{
App.MainSceneParam.Add("Name", "UserInfoPanel");
}
}
private void GoContinue()
{
2021-04-28 15:21:06 +08:00
UIManager.ShowConfirm("Continue", "Continue riding?", () =>
{
2021-04-28 15:21:06 +08:00
if (routeResult.ContinueCyclingParam != null)
{
App.RouteIdParam = routeResult.RouteId;
App.routeResult = routeResult;
RecordFromUrl();
2022-04-01 18:41:31 +08:00
if (routeResult.LastFrame.HasValue)
{
SceneManager.LoadScene("VideoPlay");
}
else
{
SceneManager.LoadScene("Ride");
}
2021-04-28 15:21:06 +08:00
}
});
}
2021-09-06 16:53:48 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
2021-04-19 15:44:11 +08:00
void SetButtonColor(bool f)
{
2021-07-29 19:55:29 +08:00
if (routeResult!=null)
2021-04-19 15:44:11 +08:00
{
2021-07-29 19:55:29 +08:00
if (index == 0)
{
2021-07-29 19:55:29 +08:00
if (f)
{
bool enabled = routeResult.ContinueCyclingParam == null;
btnContinue.GetComponent<Image>().color =
enabled ? Utils.HexToColorHtml("#75264e") : Utils.HexToColorHtml("#F93086");
btnReRide.GetComponent<Image>().color = Utils.HexToColorHtml("#F93086");
btnDelete.GetComponent<Image>().color = Utils.HexToColorHtml("#353543");
btnContinue.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
btnReRide.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
btnDelete.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#474759");
}
else
{
btnContinue.GetComponent<Image>().color = Utils.HexToColorHtml("#414251");
btnReRide.GetComponent<Image>().color = Utils.HexToColorHtml("#414251");
btnDelete.GetComponent<Image>().color = Utils.HexToColorHtml("#23232D");
btnContinue.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#353543");
btnReRide.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#353543");
btnDelete.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#353543");
}
}
2021-07-29 19:55:29 +08:00
else if (index == 1)
{
2021-07-29 19:55:29 +08:00
if (f)
{
btnDetail.GetComponent<Image>().color = Utils.HexToColorHtml("#F93086");
btnDetail.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
}
else
{
btnDetail.GetComponent<Image>().color = Utils.HexToColorHtml("#414251");
btnDetail.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#353543");
}
}
2021-04-19 15:44:11 +08:00
}
2021-04-19 15:44:11 +08:00
}
2021-08-27 15:06:56 +08:00
#else
void SetButtonColor(bool f)
{
if (!f)
{
transform.Find("Right").gameObject.SetActive(false);
transform.Find("RightMatch").gameObject.SetActive(false);
transform.Find("RightDot").gameObject.SetActive(true);
}
else
{
transform.Find("Right").gameObject.SetActive(index == 0);
if (index == 0)
{
bool disabled = routeResult.ContinueCyclingParam == null;
if (disabled)
{
btnContinue.GetComponent<Image>().color = Utils.HexToColorHtml("#cdcdcd");
}
}
transform.Find("RightMatch").gameObject.SetActive(index == 1);
transform.Find("RightDot").gameObject.SetActive(false);
}
}
#endif
2021-09-06 16:53:48 +08:00
public bool enter { get; private set; }
public void OnPointerExit(PointerEventData eventData)
{
transform.GetComponent<Image>().color = Utils.HexToColorHtml("#23232d");
2021-07-29 13:55:33 +08:00
//if (index == 1)
//{
// right.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#414251");
// right.Find("Value").GetComponent<Text>().color = Utils.HexToColorHtml("#5c5c6e");
//}
if (left != null)
{
left.Find("Main").Find("Time").GetComponent<Text>().color = Utils.HexToColorHtml("#414251");
left.Find("Progress").Find("Image").GetComponent<Image>().color = Utils.HexToColorHtml("#5c5c6e");
left.Find("Progress").Find("Value").GetComponent<Text>().color = Utils.HexToColorHtml("#414251");
left.Find("Progress").Find("bf").GetComponent<Text>().color = Utils.HexToColorHtml("#414251");
}
if (row1 != null)
{
row1.Find("Time").GetComponent<Text>().color = Utils.HexToColorHtml("#9E9EAD");
row1.Find("Distance").GetComponent<Text>().color = Utils.HexToColorHtml("#9E9EAD");
row1.Find("Times").GetComponent<Text>().color = Utils.HexToColorHtml("#9E9EAD");
row1.Find("Rank").GetComponent<Text>().color = Utils.HexToColorHtml("#9E9EAD");
}
2021-09-07 16:12:16 +08:00
if (row2 != null)
{
row2.Find("Device").GetComponent<Text>().color = Utils.HexToColorHtml("#9E9EAD");
}
titleColor = "#414251";
2021-04-19 15:44:11 +08:00
SetButtonColor(false);
2021-04-28 15:21:06 +08:00
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
2021-09-06 16:53:48 +08:00
enter = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
var pController = parent.GetComponent<ResultListController>();
if (pController.currentItem)
{
pController.currentItem.GetComponent<RouteItem>().OnPointerExit(null);
}
pController.currentItem = transform;
transform.GetComponent<Image>().color = Utils.HexToColorHtml("#353543");
2021-07-29 13:55:33 +08:00
//if (index == 1)
//{
// right.Find("Text").GetComponent<Text>().color = Utils.HexToColorHtml("#5c5c6e");
// right.Find("Value").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
//}
if (left != null)
{
left.Find("Main").Find("Time").GetComponent<Text>().color = Utils.HexToColorHtml("#5c5c6e");
left.Find("Progress").Find("Image").GetComponent<Image>().color = Utils.HexToColorHtml("#F93086");
left.Find("Progress").Find("Value").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
left.Find("Progress").Find("bf").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
}
if (row1 != null)
{
row1.Find("Time").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
row1.Find("Distance").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
row1.Find("Times").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
row1.Find("Rank").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
}
2021-09-07 16:12:16 +08:00
if (row2 != null)
{
row2.Find("Device").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
}
titleColor = "#5c5c6e";
2021-04-19 15:44:11 +08:00
SetButtonColor(true);
2021-04-28 15:21:06 +08:00
Cursor.SetCursor(Resources.Load<Texture2D>("Images/PointerButtonHover"), Vector2.zero, CursorMode.Auto);
2021-09-06 16:53:48 +08:00
enter = true;
}
}