347 lines
15 KiB
C#
Raw Normal View History

2021-07-20 09:58:01 +08:00
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
2021-08-09 16:32:47 +08:00
using UnityEngine.SceneManagement;
2021-07-20 09:58:01 +08:00
using UnityEngine.UI;
public class RaceItemScript : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IPointerUpHandler
2021-07-20 09:58:01 +08:00
{
// Start is called before the first frame update
public enum ItemType { big, small }
private Transform parent;
[SerializeField] ItemType myType;
2021-09-02 14:18:26 +08:00
private string _from = null;
internal void Refresh()
{
if (mapCompetition == null) return;
var res1 = ConfigHelper.mapCompetitionApi.GetById(mapCompetition.Id);
if (res1.result)
{
Initial(res1.data, null);
}
}
2021-07-20 09:58:01 +08:00
public MapCompetition mapCompetition { get; set; }
void Start()
{
}
// Update is called once per frame
void Update()
{
}
2021-08-27 15:06:56 +08:00
public void Initial(MapCompetition mapCompetition,Transform parent,string from = "list")
2021-07-20 09:58:01 +08:00
{
2021-08-27 15:06:56 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
2021-08-09 16:32:47 +08:00
transform.GetComponent<Button>().onClick.AddListener(() =>
{
App.RouteIdParam = mapCompetition.RouteId;
App.CompetionId = mapCompetition.Id;
SceneManager.LoadScene("Ride");
});
2021-07-20 09:58:01 +08:00
Utils.DisplayImage(transform.GetComponent<RawImage>(), mapCompetition.RouteCover, true);
2021-08-27 15:06:56 +08:00
#else
2021-09-02 14:18:26 +08:00
if (string.IsNullOrEmpty(_from))
{
_from = from;
}
if (_from == "list")
2021-08-27 15:06:56 +08:00
{
Utils.DisplayImage(transform.GetComponent<RawImage>(), mapCompetition.RouteCover, true);
transform.GetComponent<Button>().onClick.AddListener(() =>
{
2021-09-02 14:18:26 +08:00
UIManager.ShowRaceDetailModal(transform,mapCompetition);
2021-08-27 15:06:56 +08:00
});
}
#endif
2021-07-27 10:32:39 +08:00
Utils.DisplayImage(transform.Find("AltitudeCurve").GetComponent<RawImage>(), mapCompetition.AltitudeGraph, true);
transform.Find("InfoContainer/Text").GetComponent<Text>().SetTextWithEllipsis(mapCompetition.Title);
2021-07-29 13:55:33 +08:00
transform.Find("GetReadyContainer").gameObject.SetActive(mapCompetition.Status != 4);
2021-07-27 10:32:39 +08:00
transform.Find("GetReadyContainer-2").gameObject.SetActive(mapCompetition.Status == 1);
2021-07-20 09:58:01 +08:00
//transform.Find("Text").GetComponent<Text>().text = mapCompetition.Title;
//props.Find("DistanceText").GetComponent<Text>().text = $"{myMap.Distance.ToString(myMap.Distance > 1000 ? "#0" : "#0.00")}KM";
//props.Find("EleText").GetComponent<Text>().text = $"{(myMap.TotalClimb ?? 0.0).ToString(myMap.TotalClimb > 1000 ? "#0" : "#0.00")}M";
//props.Find("SlopeText").GetComponent<Text>().text = $"{myMap.AverageGrade.ToString("#0.00")}%";
2021-07-27 10:32:39 +08:00
transform.Find("DataContainer/Distance/Text").GetComponent<Text>().text
2021-07-20 09:58:01 +08:00
= $"{mapCompetition.Distance.ToString(mapCompetition.Distance > 1000 ? "#0" : "#0.00")}KM";
2021-07-27 10:32:39 +08:00
transform.Find("DataContainer/Altitude/Text").GetComponent<Text>().text
2021-07-20 09:58:01 +08:00
= $"{(mapCompetition.TotalClimb ?? 0.0).ToString(mapCompetition.TotalClimb > 1000 ? "#0" : "#0.00")}M";
2021-07-27 10:32:39 +08:00
transform.Find("DataContainer/Slope/Text").GetComponent<Text>().text
2021-07-20 09:58:01 +08:00
= $"{mapCompetition.AverageGrade.ToString("#0.00")}%";
2021-07-29 15:45:44 +08:00
transform.Find("InfoContainer/StartTime/Text").GetComponent<Text>().text = mapCompetition.StartTime.ToLocalTime().ToString();
2021-07-23 15:04:29 +08:00
transform.Find("CountContainer/Text").GetComponent<Text>().text = mapCompetition.ApplyCount.ToString();
2021-07-20 09:58:01 +08:00
SetStatus(mapCompetition);
2021-08-27 15:06:56 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
2021-07-20 09:58:01 +08:00
SetButtonGroup(mapCompetition);
2021-08-27 15:06:56 +08:00
#else
2021-09-02 14:18:26 +08:00
if (_from != "list")
2021-08-27 15:06:56 +08:00
{
SetButtonGroup(mapCompetition);
}
#endif
2021-07-27 18:36:49 +08:00
if (parent != null)
{
this.parent = parent;
}
2021-07-20 09:58:01 +08:00
this.mapCompetition = mapCompetition;
}
private void SetButtonGroup(MapCompetition mapCompetition)
{
2021-07-28 14:59:21 +08:00
var container = transform.Find("BtnContainer");
container.GetComponent<RaceButtonGroupScript>().map = mapCompetition;
container.GetComponent<RaceButtonGroupScript>().parent = transform;
container.Find("BtnCancel").gameObject.SetActive(mapCompetition.CanCancelJoin);
container.Find("BtnJoin").gameObject.SetActive(mapCompetition.CanJoin);
2021-07-29 20:58:48 +08:00
container.Find("BtnEnter").gameObject.SetActive(true);
container.Find("BtnWatch").gameObject.SetActive(false);
2021-07-28 14:59:21 +08:00
//if (transform.Find("BtnContainer") != null)
//{
// transform.Find("BtnContainer").gameObject.Destroy();
//}
//if (mapCompetition.Status == 1)
//{
// var game = Resources.Load<GameObject>("UI/Prefab/Race/ButtonGroup/BtnContainer-Enter");
// var igame = Instantiate(game);
// igame.name = "BtnContainer";
// igame.GetComponent<RaceButtonGroupScript>().map = mapCompetition;
// igame.GetComponent<RaceButtonGroupScript>().parent = transform;
// igame.transform.SetParent(transform);
// igame.transform.localScale = Vector3.one;
// var loc = Vector3.zero;
// loc.y = myType == ItemType.big ? -252 : -257;
// igame.transform.localPosition = loc;
//}
//else
//if (mapCompetition.Status == 4)
//{
// var game = Resources.Load<GameObject>("UI/Prefab/Race/ButtonGroup/BtnContainer-Watch");
// var igame = Instantiate(game);
// igame.name = "BtnContainer";
// igame.GetComponent<RaceButtonGroupScript>().map = mapCompetition;
// igame.GetComponent<RaceButtonGroupScript>().parent = transform;
// igame.transform.SetParent(transform);
// igame.transform.localScale = Vector3.one;
// var loc = Vector3.zero;
// loc.y = myType == ItemType.big ? -252 : -257;
// igame.transform.localPosition = loc;
//}
//else
//{
// if (myType == ItemType.big)
// {
// var game = Resources.Load<GameObject>("UI/Prefab/Race/ButtonGroup/BtnContainer-Double-H");
// var igame = Instantiate(game);
// igame.name = "BtnContainer";
// igame.GetComponent<RaceButtonGroupScript>().map = mapCompetition;
// igame.GetComponent<RaceButtonGroupScript>().parent = transform;
// igame.transform.Find("BtnJoin").gameObject.SetActive(mapCompetition.CanJoin);
// igame.transform.Find("BtnCancel").gameObject.SetActive(mapCompetition.CanCancelJoin);
// igame.transform.SetParent(transform);
// igame.transform.localScale = Vector3.one;
// igame.transform.GetComponent<RectTransform>().anchorMax = new Vector2(0, 0);
// igame.transform.GetComponent<RectTransform>().anchorMin = new Vector2(1, 0);
// igame.transform.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0);
// var loc = Vector3.zero;
// loc.y = 20;
// loc.y = 20;
// igame.transform.GetComponent<RectTransform>().anchoredPosition = loc;
// }
// else
// {
// var game = Resources.Load<GameObject>("UI/Prefab/Race/ButtonGroup/BtnContainer-Double-V");
// var igame = Instantiate(game);
// igame.name = "BtnContainer";
// igame.GetComponent<RaceButtonGroupScript>().map = mapCompetition;
// igame.GetComponent<RaceButtonGroupScript>().parent = transform;
// igame.transform.Find("BtnJoin").gameObject.SetActive(mapCompetition.CanJoin);
// igame.transform.Find("BtnCancel").gameObject.SetActive(mapCompetition.CanCancelJoin);
// igame.transform.SetParent(transform);
// igame.transform.localScale = Vector3.one;
// igame.transform.GetComponent<RectTransform>().anchorMax = new Vector2(0, 0);
// igame.transform.GetComponent<RectTransform>().anchorMin = new Vector2(1, 0);
// igame.transform.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0);
// var loc = Vector3.zero;
// loc.y = 20;
// igame.transform.GetComponent<RectTransform>().anchoredPosition = loc;
// }
//}
//if (transform.Find("Masking").GetComponent<CanvasGroup>().alpha == 1)
//{
// transform.Find("BtnContainer").GetComponent<CanvasGroup>().alpha = 1;
//}
2021-07-20 09:58:01 +08:00
}
private Dictionary<int, string> statusColorDict = new Dictionary<int, string>
{
{2,"#F93086" },{3,"#41A6FE" },{4,"#6E6E7D" },
};
private void SetStatus(MapCompetition mapCompetition)
{
var status = transform.Find("Status");
2021-07-29 21:15:26 +08:00
if (mapCompetition.Status <= 1)
2021-07-20 09:58:01 +08:00
{
status.GetComponent<CanvasGroup>().alpha = 0;
2021-07-20 09:58:01 +08:00
}
2021-07-23 15:04:29 +08:00
else
2021-07-20 09:58:01 +08:00
{
status.GetComponent<RawImage>().color = Utils.HexToColorHtml(statusColorDict[mapCompetition.Status]);
2021-07-28 14:59:21 +08:00
if (mapCompetition.Status == 2 && !mapCompetition.HasJoin)
2021-07-23 15:04:29 +08:00
{
status.GetComponent<CanvasGroup>().alpha = 0;
2021-07-23 15:04:29 +08:00
}
else
{
status.GetComponent<CanvasGroup>().alpha = 1;
2021-07-28 18:33:52 +08:00
status.Find("Text").GetComponent<Text>().text =
mapCompetition.Status == 2?"Registered":mapCompetition.StatusVlaue;
2021-07-23 15:04:29 +08:00
}
2021-07-20 09:58:01 +08:00
}
if (status.GetComponent<ContentFitterController>())
{
Debug.Log("cf刷新一次1");
status.GetComponent<ContentFitterController>().Refresh();
}
2021-07-20 09:58:01 +08:00
}
#if !(UNITY_ANDROID || UNITY_IOS)
2021-08-09 16:32:47 +08:00
float? localY = null;
2021-07-20 09:58:01 +08:00
public void OnPointerExit(PointerEventData eventData)
{
2021-08-09 16:32:47 +08:00
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
transform.DOLocalMoveY(localY.Value, 0.3f);
2021-07-20 09:58:01 +08:00
transform.Find("Masking").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
2021-07-27 10:32:39 +08:00
transform.Find("AltitudeCurve").GetComponent<RawImage>().DOFade(0, 0.5f);
transform.Find("InfoContainer/Text").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
2021-07-20 09:58:01 +08:00
transform.Find("BtnContainer").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
transform.Find("CountContainer").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
transform.Find("GetReadyContainer").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
2021-07-27 10:32:39 +08:00
transform.Find("GetReadyContainer-2").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
transform.Find("InfoContainer").GetComponent<CanvasGroup>().DOFade(1, 0.2f);
2021-07-20 09:58:01 +08:00
if (myType == ItemType.small)
{
2021-07-27 10:32:39 +08:00
transform.Find("DataContainer").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
2021-07-20 09:58:01 +08:00
}
else
{
2021-07-23 15:04:29 +08:00
transform.Find("InfoContainer/Line").GetComponent<Image>().DOFade(1,0.5f);
2021-07-20 09:58:01 +08:00
transform.Find("InfoContainer").DOLocalMoveY(-246, 0.3f);
}
if (this.parent != null)
{
this.parent.GetComponent<RaceScript>().SetCurrentItem(null);
//this.parent.SendMessage("SetCurrentItem", null, SendMessageOptions.RequireReceiver);
}
}
public void OnPointerEnter(PointerEventData eventData)
{
2021-08-09 16:32:47 +08:00
if (localY == null)
{
localY = transform.localPosition.y;
}
transform.DOLocalMoveY(localY.Value + 5, 0.3f);
Cursor.SetCursor(Resources.Load<Texture2D>("Images/PointerButtonHover"), Vector2.zero, CursorMode.Auto);
2021-07-20 09:58:01 +08:00
transform.Find("Masking").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
2021-07-27 10:32:39 +08:00
transform.Find("AltitudeCurve").GetComponent<RawImage>().DOFade(1, 0.5f);
transform.Find("InfoContainer/Text").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
2021-07-20 09:58:01 +08:00
transform.Find("BtnContainer").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
transform.Find("CountContainer").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
transform.Find("GetReadyContainer").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
2021-07-27 10:32:39 +08:00
transform.Find("GetReadyContainer-2").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
transform.Find("InfoContainer").GetComponent<CanvasGroup>().DOFade(0, 0.2f);
2021-07-20 09:58:01 +08:00
if (myType == ItemType.small)
{
2021-07-27 10:32:39 +08:00
transform.Find("DataContainer").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
2021-07-20 09:58:01 +08:00
}
else
{
2021-07-23 15:04:29 +08:00
transform.Find("InfoContainer/Line").GetComponent<Image>().DOFade(0, 0.5f);
2021-07-20 09:58:01 +08:00
transform.Find("InfoContainer").DOLocalMoveY(-155, 0.3f);
}
if (this.parent != null)
{
this.parent.GetComponent<RaceScript>().SetCurrentItem(transform);
//this.parent.SendMessage("SetCurrentItem", transform);
}
}
public void OnPointerUp(PointerEventData eventData)
{
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
#else
public void OnPointerExit(PointerEventData eventData)
{
//if (_from == "list")
//{
// transform.Find("Masking").GetComponent<CanvasGroup>().DOFade(0, 0.3f);
//}
}
public void OnPointerEnter(PointerEventData eventData)
{
//if (_from == "list")
//{
// transform.Find("Masking").GetComponent<CanvasGroup>().DOFade(1, 0.3f);
//}
}
public void OnPointerUp(PointerEventData eventData)
{
}
#endif
2021-07-26 14:09:21 +08:00
public void Join()
2021-07-23 15:04:29 +08:00
{
2021-07-26 14:09:21 +08:00
var res = ConfigHelper.mapCompetitionApi.ApplyMapCompetition(mapCompetition.Id);
2021-07-23 15:04:29 +08:00
if (res.result)
{
2021-07-28 14:59:21 +08:00
//mapCompetition.HasJoin = true;
//mapCompetition.CanJoin = false;
//transform.Find("CountContainer/Text").GetComponent<Text>().text = (++mapCompetition.ApplyCount).ToString();
//SetStatus(mapCompetition);
//transform.Find("BtnContainer").gameObject.Destroy();
//SetButtonGroup(mapCompetition);
2021-09-02 14:18:26 +08:00
Refresh();
2021-07-28 21:11:46 +08:00
Utils.showToast(null, "success", type: 1);
2021-07-23 15:04:29 +08:00
}
2021-07-26 14:08:43 +08:00
else
{
Utils.showToast(null, res.errMsg, type: 0);
}
2021-07-23 15:04:29 +08:00
}
2021-07-26 14:09:21 +08:00
public void CancelJoin()
2021-07-23 15:04:29 +08:00
{
2021-07-26 14:09:21 +08:00
var res = ConfigHelper.mapCompetitionApi.CancelMapCompetition(mapCompetition.Id);
2021-07-23 15:04:29 +08:00
if (res.result)
{
2021-07-28 14:59:21 +08:00
//mapCompetition.HasJoin = false;
//mapCompetition.CanJoin = true;
//transform.Find("CountContainer/Text").GetComponent<Text>().text = (--mapCompetition.ApplyCount).ToString();
//SetStatus(mapCompetition);
//transform.Find("BtnContainer").gameObject.Destroy();
//SetButtonGroup(mapCompetition);
2021-08-10 16:36:13 +08:00
App.CompetitionIdList.Remove(mapCompetition.Id);
2021-09-02 14:18:26 +08:00
Refresh();
2021-07-28 21:11:46 +08:00
Utils.showToast(null, "success", type: 1);
2021-07-23 15:04:29 +08:00
}
2021-07-26 14:08:43 +08:00
else
{
Utils.showToast(null, res.errMsg, type: 0);
}
2021-07-23 15:04:29 +08:00
}
2021-07-20 09:58:01 +08:00
}