2021-09-30 09:06:35 +08:00

425 lines
17 KiB
C#

using Assets.Scripts;
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;
public class RouteItem : MonoBehaviour
#if !(UNITY_ANDROID || UNITY_IOS)
,IPointerEnterHandler,IPointerExitHandler
#endif
{
RouteResult routeResult;
RowerResultModel rowerResult;
// Start is called before the first frame update
Transform left,row1,row2,right,rightDot;
Transform btnReRide, btnContinue, btnDelete;
Transform btnDetail;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
string titleColor = "#5c5c6e";
RouteResult data;
//滑动索引 0route 1match
int index;
//交互用
private Transform parent { get; set; }
public void Initial(object result, int index, Transform parent = null,bool top = false)
{
this.index = index;
if (parent)
{
this.parent = parent;
}
left = transform.Find("BigLeft/Left");
row1 = left.Find("Main").Find("Row1");
row2 = left.Find("Main").Find("Row2");
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);
}
else
{
Initial((RowerResultModel)result, index, parent);
}
}
/// <summary>
/// 划船机初始化
/// </summary>
/// <param name="result"></param>
/// <param name="index"></param>
/// <param name="parent"></param>
public void Initial(RowerResultModel result, int index, Transform parent = null)
{
rowerResult = result;
row1.Find("Rank").gameObject.SetActive(false);
row1.Find("Times").gameObject.SetActive(false);
var raw = transform.Find("BigLeft/CoverImage").GetComponent<RawImage>();
if (string.IsNullOrEmpty(result.Cover))
{
raw.texture = Resources.Load<Texture>("Images/Rower/地图1");
}
else
{
Utils.DisplayImage(raw, result.Cover, true);
}
row1.Find("Time").GetComponent<Text>().text = $"<color=#5c5c6e>Rowing time:</color>{TimeSpan.FromSeconds(result.TotalTime).ToString()}";
row1.Find("Distance").GetComponent<Text>().text = $"<color=#5c5c6e>Distance:</color>{result.TotalDistance}M";
row2.Find("Device").GetComponent<Text>().text = $"<color=#5c5c6e>Rowing equipment:</color>{result.ManufacturerName}";
left.Find("Progress").gameObject.SetActive(false);
left.Find("Main/Name").GetComponent<Text>().text = $"{result.CreateTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")} Free rowing";
left.Find("Main/Time").GetComponent<Text>().text = $"{result.StartTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")} ~ {result.CreateTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")}";
var rect = left.Find("Main").GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(399,rect.sizeDelta.y);
SetButtonColor(false);
right = transform.Find("RightRower");
if (right != null)
{
btnDetail = right.Find("BtnDetail");
if (btnDetail)
{
UIManager.AddEvent(btnDetail.gameObject, EventTriggerType.PointerClick, b =>
{
UIManager.ShowRowerResult(result.Id);
});
}
btnReRide = right.Find("BtnRide");
if (btnReRide)
{
UIManager.AddEvent(btnReRide.gameObject, EventTriggerType.PointerClick, b =>
{
UIManager.ShowConfirm("Re-Rowing", "Rowing again?", () =>
{
UIManager.CloseConfirm();
UIManager.ShowRowerPanel();
});
});
}
}
//UIManager.AddEvent(transform.gameObject, EventTriggerType.PointerClick, b => UIManager.ShowRowerResult(result.Id));
}
public void Initial(RouteResult result,int index,Transform parent = null)
{
routeResult = result;
Utils.DisplayImage(transform.Find("BigLeft/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("HH:mm:ss dd-MM-yyyy");
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");
right.gameObject.SetActive(true);
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);
#if UNITY_ANDROID || UNITY_IOS
UIManager.AddEvent(btnDelete.gameObject, EventTriggerType.PointerClick, (b) => Detail());
#else
UIManager.AddEvent(btnDelete.gameObject, EventTriggerType.PointerClick, (b) => Delete());
#endif
}
SetButtonColor(false);
transform.Find("RightMatch").gameObject.SetActive(false);
}
else
{
//Debug.Log(index);
right = transform.Find("RightMatch");
right.gameObject.SetActive(true);
btnDetail = right.Find("BtnDetail");
UIManager.AddEvent(btnDetail.gameObject, EventTriggerType.PointerClick, (b) => Detail());
UIManager.AddEvent(right.Find("BtnRide").gameObject, EventTriggerType.PointerClick, (b) => Ride());
SetButtonColor(false);
transform.Find("Right").gameObject.SetActive(false);
}
}
void Detail()
{
var url = routeResult.ViewUrl + "?Token=" + App.CurrentUser.cookie;
if (index == 1)
{
url += "&raceId=" + routeResult.MapCompetitionId;
}
Application.OpenURL(url);
}
void Ride()
{
UIManager.ShowConfirm("Ride", "Ride this route?", () =>
{
App.RouteIdParam = routeResult.RouteId;
App.routeResult = routeResult;
App.routeResult.ContinueCyclingParam = null;
RecordFromUrl();
SceneManager.LoadScene("Ride");
});
}
private void Delete()
{
UIManager.ShowConfirm("Delete", "Delete this record?", async () =>
{
JsonResult<object> r = null;
if (index != 2)
{
r = await ConfigHelper.mapApi.DeleteRecord(routeResult.Id);
}
else
{
r = await ConfigHelper.rowerApi.Delete(rowerResult.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");
}
});
}
#if !(UNITY_ANDROID || UNITY_IOS)
void SetButtonColor(bool f)
{
if (routeResult!=null)
{
if (index == 0)
{
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");
}
}
else if (index == 1)
{
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");
}
}
}
}
#else
void SetButtonColor(bool f)
{
if (!f)
{
transform.Find("RightRower").gameObject.SetActive(false);
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("RightRower").gameObject.SetActive(index == 2);
transform.Find("RightDot").gameObject.SetActive(false);
}
}
#endif
public bool enter { get; private set; }
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");
}
if (row2 != null)
{
row2.Find("Device").GetComponent<Text>().color = Utils.HexToColorHtml("#9E9EAD");
}
titleColor = "#414251";
SetButtonColor(false);
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
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");
//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");
}
if (row2 != null)
{
row2.Find("Device").GetComponent<Text>().color = Utils.HexToColorHtml("#ffffff");
}
titleColor = "#5c5c6e";
SetButtonColor(true);
Cursor.SetCursor(Resources.Load<Texture2D>("Images/PointerButtonHover"), Vector2.zero, CursorMode.Auto);
enter = true;
}
}