2021-07-28 14:59:21 +08:00

273 lines
12 KiB
C#

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;
using UnityEngine.UI;
public class RaceItemScript : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IPointerUpHandler
{
// Start is called before the first frame update
public enum ItemType { big, small }
private Transform parent;
[SerializeField] ItemType myType;
public MapCompetition mapCompetition { get; set; }
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Initial(MapCompetition mapCompetition,Transform parent)
{
Utils.DisplayImage(transform.GetComponent<RawImage>(), mapCompetition.RouteCover, true);
Utils.DisplayImage(transform.Find("AltitudeCurve").GetComponent<RawImage>(), mapCompetition.AltitudeGraph, true);
transform.Find("InfoContainer/Text").GetComponent<Text>().SetTextWithEllipsis(mapCompetition.Title);
transform.Find("GetReadyContainer").gameObject.SetActive(mapCompetition.Status != 3);
transform.Find("GetReadyContainer-2").gameObject.SetActive(mapCompetition.Status == 1);
//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")}%";
transform.Find("DataContainer/Distance/Text").GetComponent<Text>().text
= $"{mapCompetition.Distance.ToString(mapCompetition.Distance > 1000 ? "#0" : "#0.00")}KM";
transform.Find("DataContainer/Altitude/Text").GetComponent<Text>().text
= $"{(mapCompetition.TotalClimb ?? 0.0).ToString(mapCompetition.TotalClimb > 1000 ? "#0" : "#0.00")}M";
transform.Find("DataContainer/Slope/Text").GetComponent<Text>().text
= $"{mapCompetition.AverageGrade.ToString("#0.00")}%";
transform.Find("InfoContainer/StartTime/Text").GetComponent<Text>().text = mapCompetition.StartTime.ToString();
transform.Find("CountContainer/Text").GetComponent<Text>().text = mapCompetition.ApplyCount.ToString();
SetStatus(mapCompetition);
SetButtonGroup(mapCompetition);
if (parent != null)
{
this.parent = parent;
}
this.mapCompetition = mapCompetition;
}
private void SetButtonGroup(MapCompetition mapCompetition)
{
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);
container.Find("BtnEnter").gameObject.SetActive(mapCompetition.Status != 4);
container.Find("BtnWatch").gameObject.SetActive(mapCompetition.Status == 4);
//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;
//}
}
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");
if (mapCompetition.Status == 1)
{
status.gameObject.SetActive(false);
}
else
{
status.GetComponent<RawImage>().color = Utils.HexToColorHtml(statusColorDict[mapCompetition.Status]);
if (mapCompetition.Status == 2 && !mapCompetition.HasJoin)
{
status.gameObject.SetActive(false);
}
else
{
status.Find("Text").GetComponent<Text>().text = mapCompetition.StatusVlaue;
}
}
}
public void OnPointerExit(PointerEventData eventData)
{
transform.Find("Masking").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
transform.Find("AltitudeCurve").GetComponent<RawImage>().DOFade(0, 0.5f);
transform.Find("InfoContainer/Text").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
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);
transform.Find("GetReadyContainer-2").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
transform.Find("InfoContainer").GetComponent<CanvasGroup>().DOFade(1, 0.2f);
if (myType == ItemType.small)
{
transform.Find("DataContainer").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
}
else
{
transform.Find("InfoContainer/Line").GetComponent<Image>().DOFade(1,0.5f);
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)
{
transform.Find("Masking").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
transform.Find("AltitudeCurve").GetComponent<RawImage>().DOFade(1, 0.5f);
transform.Find("InfoContainer/Text").GetComponent<CanvasGroup>().DOFade(0, 0.5f);
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);
transform.Find("GetReadyContainer-2").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
transform.Find("InfoContainer").GetComponent<CanvasGroup>().DOFade(0, 0.2f);
if (myType == ItemType.small)
{
transform.Find("DataContainer").GetComponent<CanvasGroup>().DOFade(1, 0.5f);
}
else
{
transform.Find("InfoContainer/Line").GetComponent<Image>().DOFade(0, 0.5f);
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);
}
public void Join()
{
var res = ConfigHelper.mapCompetitionApi.ApplyMapCompetition(mapCompetition.Id);
if (res.result)
{
//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);
var res1 = ConfigHelper.mapCompetitionApi.GetById(mapCompetition.Id);
if (res1.result)
{
Initial(res1.data, null);
}
Utils.showToast(null, "Successful application!", type: 1);
}
else
{
Utils.showToast(null, res.errMsg, type: 0);
}
}
public void CancelJoin()
{
var res = ConfigHelper.mapCompetitionApi.CancelMapCompetition(mapCompetition.Id);
if (res.result)
{
//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);
var res1 = ConfigHelper.mapCompetitionApi.GetById(mapCompetition.Id);
if (res1.result)
{
Initial(res1.data, null);
}
Utils.showToast(null, "Successful cancellation application!", type: 1);
}
else
{
Utils.showToast(null, res.errMsg, type: 0);
}
}
}