powerfun-unity/Assets/Scripts/UI/Prefab/Panel/NewRouteDetailController.cs
2022-07-06 17:48:55 +08:00

312 lines
13 KiB
C#

using Assets.Scenes.Ride.Scripts;
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Threading.Tasks;
public class NewRouteDetailController : PFUIPanel
{
// Start is called before the first frame update
Dictionary<string, Texture> caches;
Dictionary<string, byte[]> cacheByte;
GameObject map;
ScrollRect scroll;
Dictionary<bool, Sprite> FavDict;
List<int> routeIds { get; set; }
protected override void Awake()
{
FavDict = new Dictionary<bool, Sprite>()
{
{false,Resources.Load<Sprite>("Images/NewDesign/icon_heji_unlike") },
{true,Resources.Load<Sprite>("Images/NewDesign/icon_heji_like") },
};
caches = new Dictionary<string, Texture>();
cacheByte = new Dictionary<string, byte[]>();
scroll = transform.Find("Container/Right/Scroll View").GetComponent<ScrollRect>();
UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
#if UNITY_ANDROID || UNITY_IOS
map = Resources.Load<GameObject>("UI/Prefab/MapList/MapItem-Mobile");
#else
map = Resources.Load<GameObject>("UI/Prefab/MapList/MapItem");
#endif
}
protected override void OnDestroy()
{
caches = null;
cacheByte = null;
Resources.UnloadUnusedAssets();
GC.Collect();
Debug.Log("list empty");
}
public override void Show()
{
base.Show();
transform.MyDOFade();
if (App.TextureCache.ContainsKey("rotateImage"))
{
transform.Find("RawImage").GetComponent<RawImage>().texture = App.TextureCache["rotateImage"];
}
else if (App.DefaultRotateTexture != null)
{
transform.Find("RawImage").GetComponent<RawImage>().texture = App.DefaultRotateTexture;
}
}
void Start()
{
UIManager.AddEvent(transform.Find("Container/Left/Fav").gameObject, EventTriggerType.PointerClick, async b =>
{
Assets.Scripts.Apis.JsonResult<object> res = null;
var p = ((PointerEventData)b).pointerEnter;
var act = ((PointerEventData)b).pointerEnter.GetComponent<Image>().sprite.name == "icon_heji_like";
if (act)
{
res = await ConfigHelper.mapApi.CancelAreaFav(area.Id);
}
else
{
res = await ConfigHelper.mapApi.AddAreaFav(area.Id);
}
if (res.result)
{
p.GetComponent<Image>().sprite = FavDict[!act];
//Utils.showToast(null, App.GetLocalString("Success"), type: 1);
}
else
{
Utils.showToast(null, res.errMsg);
}
});
#if UNITY_ANDROID || UNITY_IOS
var nav = transform.Find("Tmp/MainNav-mobile").GetComponent<NewMainNav>();
nav.SetButtonActive(new List<int> { 0, 3, 6, 7 }, 0);
newNav = nav;
#else
transform.Find("MainNav").GetComponent<MainNav>().ShowBack();
#endif
var rect = transform.GetComponent<RectTransform>();
rect.offsetMax = new Vector2(rect.offsetMax.x, 0);
rect.offsetMin = new Vector2(rect.offsetMin.x, 0);
BindHeadImage();
}
float timer = 1f;
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
if (scroll.verticalNormalizedPosition <= 0)
{
startMouse = true;
}
}
else
{
if (startMouse)
{
startMouse = false;
OnEndDrag(null);
}
}
//timer -= Time.deltaTime;
//while (timer < 0)
//{
// var list = MapUDPService.GetAllOnlineUserList();
// if (list.Count > 0 && routeIds != null)
// {
// var onlineUserList = list.Where(c => routeIds.Contains(c.RouteId)).ToList();
// //当前集合骑行人数
// var currentUserCount = onlineUserList.Count();
// Debug.Log($"当前集合骑行人数{currentUserCount}");
// }
// timer += 1f;
//}
}
MapRouteAreaDetail area;
int pageIndex = 0, pageSize = 12;
bool isEnd = false, startMouse = false;
public async void Initial(int areaId,string coverImage)
{
isEnd = false;
pageIndex = 0;
var left = transform.Find("Container/Left");
Utils.DisplayImageTempDict(transform.Find("Container/Left/Icon").GetComponent<RawImage>(), coverImage, caches , b =>{ });
scroll.content.DestroyChildren();
transform.Find("Container/Left/RideContainer/Text").GetComponent<Text>()
.text = string.Empty;
transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent<Text>()
.text = string.Empty;
transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent<Text>()
.text = string.Empty;
transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent<Text>()
.text = string.Empty;
transform.Find("Container/Left/DescScroll/Viewport/Content/Desc").GetComponent<Text>()
.text = string.Empty;
await GetData(areaId);
GetList();
}
Texture2D ScaleTextureCutOut(Texture2D originalTexture, float startX, float startY, float originalWidth, float originalHeight)
{
originalWidth = Mathf.Clamp(originalWidth, 0, Mathf.Max(originalTexture.width - startX, 0));
originalHeight = Mathf.Clamp(originalHeight, 0, Mathf.Max(originalTexture.height - startY, 0));
Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight));
int maxX = originalTexture.width - 1;
int maxY = originalTexture.height - 1;
for (int y = 0; y < newTexture.height; y++)
{
for (int x = 0; x < newTexture.width; x++)
{
float targetX = x + startX;
float targetY = y + startY;
int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX));
int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY));
int x2 = Mathf.Min(maxX, x1 + 1);
int y2 = Mathf.Min(maxY, y1 + 1);
float u = targetX - x1;
float v = targetY - y1;
float w1 = (1 - u) * (1 - v);
float w2 = u * (1 - v);
float w3 = (1 - u) * v;
float w4 = u * v;
Color color1 = originalTexture.GetPixel(x1, y1);
Color color2 = originalTexture.GetPixel(x2, y1);
Color color3 = originalTexture.GetPixel(x1, y2);
Color color4 = originalTexture.GetPixel(x2, y2);
Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4),
Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4),
Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4),
Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4)
);
newTexture.SetPixel(x, y, color);
}
}
newTexture.anisoLevel = 2;
newTexture.Apply();
return newTexture;
}
private void OnEndDrag(BaseEventData arg0)
{
Debug.Log(scroll.verticalNormalizedPosition);
if (scroll.verticalNormalizedPosition <= 0 && !isEnd)
{
pageIndex++;
GetList();
}
}
private async Task GetData(int areaId)
{
var res = await ConfigHelper.mapApi.GetMapRouteAreaDetail(areaId);
if (res.result)
{
this.area = res.data;
transform.Find("Container/Left/Name").GetComponent<Text>().text = res.data.Name;
transform.Find("Container/Left/RideContainer/Text").GetComponent<Text>()
.text = res.data.RideCount.ToString();
transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent<Text>()
.text = $"{res.data.TotalDistance.ToString("#0")}KM";
transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent<Text>()
.text = $"{res.data.TotalClimb.ToString("#0")}M";
transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent<Text>()
.text = $"{res.data.AverageGrade.ToString("#0.00")}%";
transform.Find("Container/Left/DescScroll/Viewport/Content/Desc").GetComponent<Text>()
.text = res.data.Description;
Utils.DisplayImageTempDict(transform.Find("Container/Left/Create/Avatar").GetComponent<RawImage>(), res.data.CreateUserHead, caches);
transform.Find("Container/Left/Create/Name").GetComponent<Text>().text = res.data.CreateUserName;
LayoutRebuilder.ForceRebuildLayoutImmediate(transform.Find("Container/Left/DescScroll").GetComponent<ScrollRect>().content);
transform.Find("Container/Left/Fav").GetComponent<Image>().sprite = FavDict[res.data.IsFav];
LayoutRebuilder.ForceRebuildLayoutImmediate(transform.Find("Container/Left/RideContainer").GetComponent<RectTransform>());
var userinfo = Resources.Load<GameObject>("UI/Prefab/NewRoute/UserInfo");
if (res.data.CoverImage != null)
{
Utils.DisplayImageTempDict(transform.Find("Container/Left/Icon").GetComponent<RawImage>(), area.CoverImage, caches
, b =>
{
// if (b != null)
// {
// cacheByte.Add(area.CoverImage, b);
// }
// else if(cacheByte.ContainsKey(area.CoverImage))
// {
// b = cacheByte[area.CoverImage];
// }
//#if UNITY_EDITOR
// var _t = new Texture2D(4, 4, TextureFormat.DXT5, false);
//#else
//#if UNITY_IOS
// var _t = new Texture2D(4, 4, TextureFormat.ASTC_RGB_4x4,false);
// Debug.Log("使用了壓縮");
//#elif UNITY_ANDROID
// var _t = new Texture2D(4, 4, TextureFormat.ASTC_RGB_4x4,false);
// Debug.Log("使用了壓縮");
//#else //pc
// var _t = new Texture2D(4, 4, TextureFormat.DXT5,false);
//#endif
//#endif
// _t.LoadImage(b);
// //Graphics.CopyTexture(t, _t);
// //Debug.Log($"{t.width},{t.height}");
// left.Find("Thumbnail").GetComponent<RawImage>().texture =
// ScaleTextureCutOut(_t, 0, _t.height / 2, _t.width / 3, _t.height / 3);
});
//Utils.DisplayImageTempDict(left.Find("Thumbnail").GetComponent<RawImage>(), area.CoverImage, caches);
}
#if !(UNITY_ANDROID || UNITY_IOS)
transform.Find("Container/Left/NOData").gameObject.SetActive(res.data.UserList.Count == 0);
transform.Find("Container/Left/Users").DestroyChildren();
foreach (var item in res.data.UserList)
{
var info = Instantiate<GameObject>(userinfo);
Utils.DisplayImageTempDict(info.transform.Find("Avatar").GetComponent<Image>(), item.WxHeadImg, caches);
info.transform.Find("NickName").GetComponent<Text>().text = item.NickName;
info.transform.SetParent(transform.Find("Container/Left/Users"));
info.transform.localScale = Vector3.one;
}
#endif
}
}
private async void GetList()
{
var res = await ConfigHelper.mapApi.GetMapRouteAreaDetailList(area.Id, pageIndex, pageSize);
if (res.result)
{
if (res.data.RouteList.Count > 0)
{
DisplayMaps(res.data.RouteList);
routeIds = res.data.RouteIds;
}
else
{
isEnd = true;
}
}
}
void DisplayMaps(List<MapRoute> list)
{
if (map != null)
{
foreach (var item in list)
{
var obj = Instantiate(map);
obj.GetComponent<MapItem>().Initial(item, caches, area);
//obj.SendMessage("Initial", );
obj.transform.SetParent(scroll.content.transform);
obj.transform.localScale = new Vector3(1, 1, 1);
}
}
}
}