180 lines
5.5 KiB
C#

using Assets.Scripts.Apis.Models;
using Assets.Scripts.UI.Control;
using Assets.Scripts.UI.Prefab.Login;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;
using System.Linq;
using Assets.Scripts;
public class Tips : MonoBehaviour
{
public NearRouteModel Model
{
get
{
return _model;
}
}
private Text Name;
private Text Count;
private Text Distance;
private Text TotalClimb;
private PfUIButton pFUIButton;
private RawImage countryIcon;
private Text averageGrade;
private List<CountryModel> countryList;
private GameObject level;
private GameObject m3DIcon;
private RawImage altitudeGraph;
private void Awake()
{
var materialSource = Resources.Load<Material>("UI/Material/RoundedCornersTextureMaterial");
var material = Instantiate(materialSource);
var rect = ((RectTransform)transform).rect;
material.SetVector(Shader.PropertyToID("_WidthHeightRadius"), new Vector4(rect.width, rect.height, 40f, 0));
this.GetComponent<Image>().material = material;
Name = this.transform.Find("Name").GetComponent<Text>();
Count = this.transform.Find("CountWrap").Find("Count").GetComponent<Text>();
Distance = this.transform.Find("Distance").GetComponent<Text>();
TotalClimb = this.transform.Find("TotalClimb").GetComponent<Text>();
pFUIButton = this.transform.Find("Ride").GetComponent<PfUIButton>();
UIManager.AddEvent(pFUIButton.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (e) => {
App.RouteIdParam = _model.Id;
SceneManager.LoadScene("Ride");
});
averageGrade = this.transform.Find("AverageGrade").GetComponent<Text>();
countryIcon = this.transform.Find("CountryIco").GetComponent<RawImage>();
var countryJson = Resources.Load<TextAsset>("UI/flags-mini").text;
countryList = JsonConvert.DeserializeObject<List<CountryModel>>(countryJson);
level = this.transform.Find("Level").gameObject;
var material1 = Instantiate(materialSource);
var rect1 = ((RectTransform)level.transform).rect;
material1.SetVector(Shader.PropertyToID("_WidthHeightRadius"), new Vector4(rect1.width, rect1.height, rect1.height, 0));
level.GetComponent<Image>().material = material1;
m3DIcon = this.transform.Find("3DIcon").gameObject;
altitudeGraph = this.transform.Find("AltitudeGraph").GetComponent<RawImage>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
NearRouteModel _model;
public void SetModel(NearRouteModel model)
{
if (this.isActiveAndEnabled)
{
if(_model != null && _model.Id == model.Id)
{
//this.gameObject.SetActive(false);
return;
}
}
//this.gameObject.SetActive(true);
_model = model;
Name.text = _model.Name;
Count.text = _model.TheHeat.ToString();
Distance.text = _model.Distance.ToString("0.0")+"KM";
TotalClimb.text = _model.TotalClimb.ToString("0") +"FT";
averageGrade.text = _model.AverageGrade.ToString("0.0") + "%";
//var country = countryList.SingleOrDefault(r => r.abbreviation.Equals(_model.CountryCode, StringComparison.InvariantCultureIgnoreCase));
//if(country )
var country = Resources.Load<Sprite>($"UI/flags-mini/{ _model.CountryCode.ToLower() }");
if(country != null)
{
countryIcon.texture = country.texture;
}
level.transform.Find("Text").GetComponent<Text>().text = _model.Hard;
m3DIcon.SetActive(_model.Enable3D);
//altitudeGraph
Utils.DisplayImage(altitudeGraph, _model.AltitudeGraph);
//Utils.DisplayImage(StartCoroutine, altitudeGraph, "http://192.168.0.97:5082/Map/AltitudeGraph?id=1215");
}
public void Show(Vector3 position, NearRouteModel model)
{
//if(position.y > position.z)
//{
// position.y = position.z;
//}
this.Move(position);
this.gameObject.SetActive(true);
this.SetModel(model);
}
public void Move(Vector3 position)
{
this.transform.position = position;
var localPosition = this.transform.localPosition;
Debug.Log(localPosition.y);
if(localPosition.y > 128)
{
localPosition.y = 128;
}
if(localPosition.x < -465)
{
localPosition.x = -465f;
}
if(localPosition.x > 1080)
{
localPosition.x = 1080;
}
//if (localPosition.y + 150 > localPosition.z)
//{
// localPosition.y = localPosition.z -150;
//}
//if(localPosition.x * -1f > localPosition.z)
//{
// localPosition.x = localPosition.z * -1f;
//}
//else if(localPosition.x > localPosition.z *2)
//{
// localPosition.x = localPosition.z * 2;
//}
this.transform.localPosition = localPosition;
this.transform.localScale = new Vector2(2, 2);
Debug.Log($"{ this.transform.localPosition.y }, { this.transform.localPosition.y }");
}
public void Hide()
{
//this.gameObject.SetActive(false);
DestroyImmediate(this.gameObject);
}
}