235 lines
9.8 KiB
C#

using Assets.Scripts;
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;
public class RouteItem : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler
{
RouteResult routeResult;
// Start is called before the first frame update
Transform left,row1,row2,right;
Transform btnReRide, btnContinue, btnDelete;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
string titleColor = "#5c5c6e";
RouteResult data;
//滑动索引 0route 1match
int index;
public void Initial(RouteResult result,int index)
{
routeResult = result;
left = transform.Find("Left");
row1 = left.Find("Main").Find("Row1");
row2 = left.Find("Main").Find("Row2");
Utils.DisplayImage(transform.Find("CoverImage").GetComponent<RawImage>(), result.RouteImage, true);
left.Find("Main").Find("Name").GetComponent<Text>().text = result.RouteName;
left.Find("Main").Find("Time").GetComponent<Text>().text = result.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
row1.Find("Time").GetComponent<Text>().text = $"<color={titleColor}>Riding time:</color>{result.TrainingTime}";
row1.Find("Distance").GetComponent<Text>().text = $"<color={titleColor}>Mileage:</color>{result.EndDistance.ToString("#0.00")}KM";
row1.Find("Times").GetComponent<Text>().text = $"<color={titleColor}>Times:</color>{result.Count}";
if (index == 0)
{
row1.Find("Rank").GetComponent<Text>().text = $"<color={titleColor}>Rank:</color>{result.Ranking}";
}
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");
row2.Find("Device").GetComponent<Text>().text = $"<color={titleColor}>Cycling equipment:</color>{result.ManufacturerName}";
if (index == 0)
{
right = transform.Find("Right");
btnContinue = right.Find("BtnContinue");
if (btnContinue)
{
bool enabled = result.ContinueCyclingParam != null;
//btnContinue.gameObject.SetActive(false);
//btnContinue.GetComponent<Button>().onClick.AddListener(GoContinue);
UIManager.AddEvent(btnContinue.gameObject, EventTriggerType.PointerClick, (b) => GoContinue());
btnContinue.GetComponent<Button>().interactable = enabled;
btnContinue.GetComponent<Button>().enabled = enabled;
}
btnReRide = right.Find("BtnReRide");
if (btnReRide)
{
//btnReRide.gameObject.SetActive(false);
UIManager.AddEvent(btnReRide.gameObject, EventTriggerType.PointerClick, (b) => GoReRide());
//btnReRide.GetComponent<Button>().onClick.AddListener(GoReRide);
}
btnDelete = right.Find("BtnDelete");
if (btnDelete)
{
//btnDelete.gameObject.SetActive(false);
//btnDelete.GetComponent<Button>().onClick.AddListener(Delete);
UIManager.AddEvent(btnDelete.gameObject, EventTriggerType.PointerClick, (b) => Delete());
}
SetButtonColor(false);
transform.Find("RightMatch").gameObject.SetActive(false);
}
else
{
right = transform.Find("RightMatch");
transform.Find("Right").gameObject.SetActive(false);
right.Find("Value").GetComponent<Text>().text = result.Ranking.ToString();
}
right.gameObject.SetActive(true);
gameObject.GetComponent<Button>().onClick.AddListener(() =>
{
Application.OpenURL(result.ViewUrl);
});
this.index = index;
}
private void Delete()
{
UIManager.ShowConfirm("Delete", "Delete this record?", async () =>
{
var r = await ConfigHelper.mapApi.DeleteRecord(routeResult.Id);
UIManager.CloseConfirm();
if (r.result)
{
DestroyImmediate(gameObject);
}
else
{
Utils.showToast(gameObject, r.errMsg);
}
});
}
private void GoReRide()
{
UIManager.ShowConfirm("ReRide", "Ride again?", () =>
{
App.RouteIdParam = routeResult.RouteId;
App.routeResult = routeResult;
App.routeResult.ContinueCyclingParam = null;
RecordFromUrl();
SceneManager.LoadScene("Ride");
});
}
private void RecordFromUrl()
{
if (App.MainSceneParam.ContainsKey("Name"))
{
App.MainSceneParam["Name"] = "UserInfoPanel";
}
else
{
App.MainSceneParam.Add("Name", "UserInfoPanel");
}
}
private void GoContinue()
{
UIManager.ShowConfirm("Continue", "Continue riding?", () =>
{
if (routeResult.ContinueCyclingParam != null)
{
App.RouteIdParam = routeResult.RouteId;
App.routeResult = routeResult;
RecordFromUrl();
SceneManager.LoadScene("Ride");
}
});
}
void SetButtonColor(bool f)
{
if (index == 0 && routeResult!=null)
{
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");
}
}
}
public void OnPointerExit(PointerEventData eventData)
{
transform.GetComponent<Image>().color = Utils.HexToColorHtml("#23232d");
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");
}
titleColor = "#414251";
SetButtonColor(false);
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
public void OnPointerEnter(PointerEventData eventData)
{
transform.GetComponent<Image>().color = Utils.HexToColorHtml("#353543");
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");
}
titleColor = "#5c5c6e";
SetButtonColor(true);
Cursor.SetCursor(Resources.Load<Texture2D>("Images/PointerButtonHover"), Vector2.zero, CursorMode.Auto);
}
}