407 lines
14 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PropNames
{
public static List<string> icons = new List<string> { "icon1", "icon2", "icon3" };
public static List<string> texts = new List<string> { "DistanceText", "EleText", "SlopeText" };
public static Dictionary<bool, string> colorDict = new Dictionary<bool, string>
{
{ true,"#ffffff"},{false,"#5c5c6e" }
};
}
public class MapItem : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler,IPointerUpHandler
{
public bool isModal = false;
float width;
float height;
void Awake()
{
width = this.GetComponent<RectTransform>().rect.width;
height = this.GetComponent<RectTransform>().rect.height;
}
// Start is called before the first frame update
//[SerializeField] Text text;
//[SerializeField] GameObject panel;
//[SerializeField] RawImage rawImage;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
float? localY = null;
MapRoute map;
Dictionary<string, Texture> caches;
private MapRouteAreaItem area = null;
public void Initial(MapRoute myMap, Dictionary<string, Texture> caches, MapRouteAreaItem area = null)
{
if (caches != null)
{
this.caches = caches;
}
this.area = area;
map = myMap;
//localY = transform.GetComponent<RectTransform>().rect.height;
//Debug.Log(localY);
//UIManager.AddEvent(gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (Base) =>
//{
// UIManager.ShowMapDetailPanel(myMap.Id);
//});
//UIManager.AddEvent(gameObject, UnityEngine.EventSystems.EventTriggerType.PointerEnter, (Base) =>
//{
// if (localY == null)
// {
// localY = transform.localPosition.y;
// }
// //transform.localPosition.Set(transform.localPosition.x,localY.Value+8, transform.localPosition.z);
// //transform.DOLocalMoveY(localY.Value + 8, 0.5f);
// //transform.domov(localY+8, 0.5f);
//});
//UIManager.AddEvent(gameObject, UnityEngine.EventSystems.EventTriggerType.PointerExit, (Base) =>
//{
// //transform.localPosition.Set(transform.localPosition.x, localY.Value, transform.localPosition.z);
// //transform.DOLocalMoveY(localY.Value, 0.5f);
//});
//UIManager.AddEvent(transform.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (Base) =>
//{
// App.RouteIdParam = myMap.Id;
// if (!App.MainSceneParam.ContainsKey("Name"))
// {
// App.MainSceneParam.Add("Name", "MapListPanel");
// }
// else
// {
// App.MainSceneParam["Name"] = "MapListPanel";
// }
// SceneManager.LoadScene("Ride");
//});
if (!isModal)
{
transform.GetComponent<Button>().onClick.RemoveAllListeners();
transform.GetComponent<Button>().onClick.AddListener(Ride);
}
transform.Find("Name").GetComponent<Text>().text = myMap.Name;
transform.Find("IdContainer/Text").GetComponent<Text>().text = $"#{myMap.Id}";
transform.Find("TimesContainer").gameObject.SetActive(myMap.MineCount > 0);
transform.Find("TimesContainer/Text").GetComponent<Text>().text = myMap.MineCountString;
transform.Find("Hot").gameObject.SetActive(myMap.IsFire);
var props = transform.Find("Props");
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")}%";
props.Find("rideText").GetComponent<Text>().text = myMap.TheHeat.ToString();
var tabContainer = transform.Find("TabContainer");
var diff = tabContainer.Find("Diff");
diff.Find("Text").GetComponent<Text>().text = myMap.Hard;
tabContainer.Find("3d").gameObject.SetActive(myMap.Enable3D);
tabContainer.Find("Country").GetComponent<RawImage>().texture = UIManager.Instance.loginRegOptions.GetCountryImage(myMap.CountryCode);
if (!isModal)
{
transform.Find("CollectImg").GetComponent<Button>().onClick.RemoveAllListeners();
if (App.CurrentRouteType == "My Upload")
{
transform.Find("CollectImg").GetComponent<Button>().onClick.AddListener(ApplyOpenRoute);
}
else
{
transform.Find("CollectImg").GetComponent<Button>().onClick.AddListener(Collect);
}
}
transform.Find("CollectImg").Find("Image").GetComponent<Image>().sprite =
App.CurrentRouteType != "My Upload"?UIManager.Instance.collectDict[myMap.IsFavorite]: UIManager.Instance.uploadDict[myMap.IsEnabled];
if (App.CurrentRouteType == "My Upload")
{
transform.Find("CollectImg").Find("Image").GetComponent<RectTransform>().sizeDelta = new Vector2(25, 20f);
}
#if !(UNITY_ANDROID || UNITY_IOS)
transform.Find("CollectImg").gameObject.SetActive(false);
#endif
transform.Find("BtnInfo").GetComponent<Button>().onClick.AddListener(Info);
transform.Find("BtnRide").GetComponent<Button>().onClick.AddListener(Ride);
Utils.DisplayImageTempDict(transform.Find("MapTitleImg").GetComponent<RawImage>(), myMap.CoverImage, this.caches);
Utils.DisplayImageTempDict(transform.Find("MapHBImg").GetComponent<RawImage>(), myMap.AltitudeGraph, this.caches);
SetActive4Button(false);
var isRecent = App.CurrentRouteType == "Recent Routes" && myMap.RecentRecord != null && myMap.RecentRecord.ContinueCyclingParam != null && !isModal;
transform.Find("BtnContinue").gameObject.SetActive(isRecent);
transform.Find("BtnReride").gameObject.SetActive(isRecent);
transform.Find("MapHBImg").gameObject.SetActive(!isRecent);
if (isRecent)
{
transform.Find("BtnContinue").GetComponent<Button>().onClick.RemoveAllListeners();
transform.Find("BtnReride").GetComponent<Button>().onClick.RemoveAllListeners();
transform.Find("BtnContinue").GetComponent<Button>().onClick.AddListener(() =>
{
if (myMap.RecentRecord != null && myMap.RecentRecord.ContinueCyclingParam != null)
{
App.RouteIdParam = myMap.RecentRecord.RouteId;
App.routeResult = myMap.RecentRecord;
if (area != null)
{
App.MainSceneParam["Name"] = "NewRouteDetail";
}
else
{
if (!App.MainSceneParam.ContainsKey("Name"))
{
App.MainSceneParam.Add("Name", "MapListPanel");
}
else
{
App.MainSceneParam["Name"] = "MapListPanel";
}
}
SceneManager.LoadScene("Ride");
}
});
transform.Find("BtnReride").GetComponent<Button>().onClick.AddListener(Ride);
}
}
void ApplyOpenRoute()
{
if (!map.IsEnabled)
{
UIManager.ShowConfirm(App.GetLocalString("Request to make the route public"), App.GetLocalString("Do you want to make the route public?"), async () =>
{
var res = await ConfigHelper.mapApi.ApplyOpenRoute(map.Id);
if (res.result)
{
Utils.showToast(null, App.GetLocalString("Submitted, please wait for background review."), type: 1);
UIManager.CloseConfirm();
}
else
{
Utils.showToast(null, res.errMsg);
}
});
}
else
{
Utils.showToast(null, App.GetLocalString("Route is public."), type: 1);
}
}
private void Ride()
{
App.RouteIdParam = map.Id;
App.currentArea = area;
if (area != null)
{
App.MainSceneParam["Name"] = "NewRouteDetail";
}
else
{
if (!App.MainSceneParam.ContainsKey("Name"))
{
App.MainSceneParam.Add("Name", "MapListPanel");
}
else
{
App.MainSceneParam["Name"] = "MapListPanel";
}
}
SceneManager.LoadScene("Ride");
//foreach (var item in Utils.propTextureCache)
//{
// DestroyImmediate(item.Value);
//}
//Utils.propTextureCache.Clear();
}
private void Info()
{
UIManager.ShowMapDetailPanel(map.Id);
}
public void OnPointerExit(PointerEventData eventData)
{
if (isModal)
{
return;
}
#if !(UNITY_ANDROID || UNITY_IOS)
if (localY != null)
{
transform.DOLocalMoveY(localY.Value, 0.3f);
}
transform.Find("CollectImg").gameObject.SetActive(false);
transform.Find("Shadow").gameObject.SetActive(false);
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
//SetActive4Button(false);
SetColor(false);
#else
transform.Find("Shadow").GetComponent<Image>().DOFade(0, 0.3f);
#endif
}
public void OnPointerEnter(PointerEventData eventData)
{
if (isModal)
{
return;
}
#if !(UNITY_ANDROID || UNITY_IOS)
if (localY == null)
{
localY = transform.localPosition.y;
}
transform.Find("CollectImg").gameObject.SetActive(true);
transform.DOLocalMoveY(localY.Value + 5, 0.3f);
transform.Find("Shadow").gameObject.SetActive(true);
Cursor.SetCursor(Resources.Load<Texture2D>("Images/PointerButtonHover"), Vector2.zero, CursorMode.Auto);
//SetActive4Button(true);
SetColor(true);
#else
transform.Find("Shadow").GetComponent<Image>().DOFade(1, 0.3f);
#endif
}
void SetActive4Button(bool b)
{
transform.Find("MapHBImg").gameObject.SetActive(!b);
transform.Find("BtnInfo").gameObject.SetActive(b);
transform.Find("BtnRide").gameObject.SetActive(b);
}
private void SetColor(bool isFav)
{
PropNames.icons.ForEach((v) =>
{
transform.Find("Props").Find(v).GetComponent<Image>().color = Utils.HexToColorHtml(
PropNames.colorDict[isFav]);
});
PropNames.texts.ForEach((v) =>
{
transform.Find("Props").Find(v).GetComponent<Text>().color = Utils.HexToColorHtml(
PropNames.colorDict[isFav]);
});
transform.Find("MapHBImg").GetComponent<RawImage>().color = Utils.HexToColorHtml(
PropNames.colorDict[isFav]);
}
async void Collect()
{
JsonResult<object> r;
transform.Find("CollectImg").GetComponent<Button>().enabled = false;
if (map.IsFavorite)
{
r = await ConfigHelper.mapApi.CancelFav(map.Id.ToString());
}
else
{
r = await ConfigHelper.mapApi.AddFav(map.Id.ToString());
}
transform.Find("CollectImg").GetComponent<Button>().enabled = true;
if (r.result)
{
map.IsFavorite = !map.IsFavorite;
transform.Find("CollectImg").Find("Image").GetComponent<Image>().sprite =
UIManager.Instance.collectDict[map.IsFavorite];
if (this.model != null)
{
this.model.IsFavorite = map.IsFavorite;
}
//SetColor(map.IsFavorite);
}
else
{
UIManager.ShowAlert("WARNING", r.errMsg);
}
}
NearRouteModel model { get; set; }
public void Show(Vector3 position, NearRouteModel model)
{
this.model = model;
//if(position.y > position.z)
//{
// position.y = position.z;
//}
#if !(UNITY_ANDROID || UNITY_IOS)
this.Move(position);
#endif
this.gameObject.SetActive(true);
//this.SetModel(model);
Initial(new MapRoute
{
Id = model.Id,
Name = model.Name,
Hard = model.Hard,
CoverImage = model.CoverImage,
Enable3D = model.Enable3D,
TheHeat = model.TheHeat,
TotalClimb = model.TotalClimb,
AverageGrade = model.AverageGrade,
CountryCode = model.CountryCode,
AltitudeGraph = model.AltitudeGraph,
Distance = model.Distance,
IsFavorite = model.IsFavorite,
},null);
}
public void Move(Vector3 position)
{
#if !(UNITY_ANDROID || UNITY_IOS)
this.transform.position = position;
var localPosition = this.transform.localPosition;
Debug.Log(localPosition.y);
//localPosition.y = localPosition.y + (287 * 2);
//localPosition.x = localPosition.x - 280;
localPosition.y = localPosition.y + (width * 2);
localPosition.x = localPosition.x - height;
//if (localPosition.y > 128)
//{
// localPosition.y = 128;
//}
//if (localPosition.x < -465)
//{
// localPosition.x = -465f;
//}
//if (localPosition.x > 1080)
//{
// localPosition.x = 1080;
//}
this.transform.localPosition = localPosition;
this.transform.localScale = new Vector2(2, 2);
Debug.Log($"{ this.transform.localPosition.y }, { this.transform.localPosition.y }");
#endif
}
public void Hide()
{
#if UNITY_ANDROID || UNITY_IOS
this.gameObject.SetActive(false);
#else
DestroyImmediate(this.gameObject);
#endif
}
public void OnPointerUp(PointerEventData eventData)
{
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
}