320 lines
9.5 KiB
C#
Raw Normal View History

2021-04-27 19:47:02 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WPM;
using DG.Tweening;
using UnityEngine.UI;
using Assets.Scripts.Apis;
using Newtonsoft.Json.Linq;
2021-04-28 15:21:06 +08:00
using Assets.Scripts;
2021-04-30 23:58:11 +08:00
using UnityEngine.EventSystems;
2021-05-20 16:15:53 +08:00
using System;
2021-04-27 19:47:02 +08:00
public class EarthController : PFUIPanel
{
private WorldMapGlobe map;
private Text Province;
private Text Country;
private RawImage maskImage;
private MapApi mapApi;
private Text CountText;
private Text OfficialText;
private Text RiderText;
//private GameObject hotPanel;
//private GameObject content;
//private Object itemObject;
Transform rootPanel;
Canvas canvas;
float time = 0f;
bool showSummary = false;
Vector2 latlonCenter;
private GameObject panel;
2021-05-20 16:15:53 +08:00
private bool isTaiwanClick = false;
2021-04-27 19:47:02 +08:00
protected override void Awake()
{
base.Awake();
mapApi = new MapApi();
canvas = this.transform.Find("Canvas").GetComponent<Canvas>();
canvas.worldCamera = Camera.main;
rootPanel = this.transform.Find("Canvas").Find("Panel");
2021-04-27 19:47:02 +08:00
map = WorldMapGlobe.instance;
2021-09-16 19:49:45 +08:00
#if UNITY_IOS
if (Utils.isLowIPhone())
{
map.earthScenicGlowIntensity = 0;
}
#endif
2021-04-27 19:47:02 +08:00
map.showProvinces = true;
map.showCities = true;
//map.autoRotationSpeed = 0f;
2021-09-16 19:49:45 +08:00
2021-04-27 19:47:02 +08:00
//map.autoRotationSpeed = 0.1f;
//Camera.main.fieldOfView = 180;
//map.allowUserZoom = false;
map.showLatitudeLines = false;
map.showLongitudeLines = false;
map.showCursor = false;
2021-04-27 19:47:02 +08:00
}
// Start is called before the first frame update
protected override void Start()
{
//itemObject = Resources.Load("UI/Prefab/BigMap/Item");
panel = rootPanel.Find("Panel").gameObject;
#if UNITY_ANDROID || UNITY_IOS
SetRounded(panel.transform, 20);
#else
SetRounded(panel.transform, 60);
#endif
panel.gameObject.SetActive(false);
2021-04-30 23:58:11 +08:00
var goList = rootPanel.transform.Find("SwitchMode");
#if UNITY_ANDROID || UNITY_IOS
SetRounded(goList, 52);
#else
2021-04-30 23:58:11 +08:00
SetRounded(goList, 64);
#endif
2021-04-30 23:58:11 +08:00
UIManager.AddEvent(goList.Find("GoList").gameObject, EventTriggerType.PointerClick, (e) =>
{
//UIManager.PopStack();
UIManager.ShowMapListPanel();
//this.gameObject.SetActive(false);
//DestroyImmediate(this.gameObject);
});
2021-04-27 19:47:02 +08:00
maskImage = rootPanel.Find("MaskImage").GetComponent<RawImage>();
Country = panel.transform.Find("Country").GetComponent<Text>();
Province = panel.transform.Find("Province").GetComponent<Text>();
CountText = panel.transform.Find("CountPanel").Find("Count").GetComponent<Text>();
OfficialText = panel.transform.Find("OfficialPanel").Find("Count").GetComponent<Text>();
RiderText = panel.transform.Find("RiderPanel").Find("Count").GetComponent<Text>();
//hotPanel = rootPanel.Find("HotPanel").gameObject;
//SetRounded(hotPanel.transform, 60);
//content = hotPanel.transform.Find("Scroll View").Find("Viewport").Find("Content").gameObject;
2021-09-02 14:18:26 +08:00
var avatar = rootPanel.transform.Find("Avatar").GetComponent<RawImage>();
UIManager.AddEvent(avatar.gameObject, EventTriggerType.PointerClick, b =>
{
UIManager.ShowUserInfoPanel();
});
Utils.DisplayImage(avatar, App.CurrentUser.WxHeadImg, true);
2021-04-27 19:47:02 +08:00
//var mainCanvas = GameObject.Find("Canvas");
//mainCanvas.gameObject.SetActive(false);
UIManager.Instance.MainPanel.gameObject.SetActive(false);
Camera.main.transform.position = new Vector3(0, 0);
Camera.main.transform.rotation = Quaternion.Euler(0, 0, 0);
2021-04-30 23:58:11 +08:00
map.SetZoomLevel(2.6f);
//var zoom = map.GetZoomLevel();
//Debug.Log(zoom);
2021-04-27 19:47:02 +08:00
map.OnZoomStart += Map_OnZoomStart;
map.OnZoomEnd += Map_OnZoomEnd;
map.OnClick += Map_OnClick;
map.OnCountryEnter += (int countryIndex, int regionIndex) =>
{
map.autoRotationSpeed = 0f;
2021-04-27 19:47:02 +08:00
var country = map.countries[countryIndex];
if (country.name.ToLower().Contains("taiwan"))
{
Country.text = "China";
Province.text = "TaiWan";
ShowSummary(country.latlonCenter);
map.HighlightCountryRegion(173, 1, false, true, Color.white);
2021-04-27 19:47:02 +08:00
}
else {
Country.text = country.name;
2021-04-27 19:47:02 +08:00
}
};
map.OnCountryClick += (int countryIndex, int regionIndex) =>
{
2021-05-20 16:15:53 +08:00
2021-04-27 19:47:02 +08:00
var country = map.countries[countryIndex];
2021-05-20 16:15:53 +08:00
Debug.Log($"country click { country.name }, { countryIndex }, { regionIndex }");
if (country.name.ToLower().Contains("taiwan") || country.name.ToLower().Contains("china"))
2021-04-27 19:47:02 +08:00
{
2021-05-20 16:15:53 +08:00
//var latLon = map.countries[36].latlonCenter; //country.latlonCenter;
//EnterBigMap(latLon);
isTaiwanClick = true;
Invoke("DelayEnterBigMapForTaiwan", 0.01f);
2021-04-27 19:47:02 +08:00
}
};
map.OnProvinceEnter += (int provinceIndex, int regionIndex) =>
2021-04-27 19:47:02 +08:00
{
var province = map.provinces[provinceIndex];
Province.text = province.name;
latlonCenter = province.latlonCenter;
time = 0f;
showSummary = true;
//ShowSummary(province.latlonCenter);
2021-04-27 19:47:02 +08:00
};
map.OnProvinceExit += (int provinceIndex, int regionIndex) =>
{
showSummary = false;
2021-04-27 19:47:02 +08:00
};
map.OnProvinceClick += (int provinceIndex, int regionIndex) =>
{
2021-05-20 16:15:53 +08:00
isTaiwanClick = false;
Debug.Log("province click");
2021-04-27 19:47:02 +08:00
var province = map.provinces[provinceIndex];
var latLon = province.latlonCenter;
EnterBigMap(latLon);
};
//skybox = Camera.main.GetComponent<Skybox>();
2021-04-30 23:58:11 +08:00
}
2021-05-20 16:15:53 +08:00
private void DelayEnterBigMapForTaiwan()
{
if (!isTaiwanClick) return;
var latLon = map.countries[36].latlonCenter;
EnterBigMap(latLon);
}
//private float rot = 0;
//Skybox skybox;
2021-04-27 19:47:02 +08:00
/// <summary>
/// 显示概要信息
/// </summary>
/// <param name="latlonCenter"></param>
private async void ShowSummary(Vector2 latlonCenter)
{
if(panel.activeInHierarchy == false)
{
panel.SetActive(true);
panel.GetComponent<CanvasGroup>().alpha = 0;
panel.GetComponent<CanvasGroup>().DOFade(1f, 0.1f);
}
2021-04-27 19:47:02 +08:00
var result = await mapApi.GetEarthData(latlonCenter.x, latlonCenter.y);
if (result.result == false)
{
return;
}
var obj = JObject.FromObject(result.data);
CountText.text = obj.Value<string>("count");
OfficialText.text = obj.Value<string>("officialCount");
RiderText.text = obj.Value<string>("riderCount");
//foreach (Transform item in content.transform)
2021-04-27 19:47:02 +08:00
//{
// GameObject.Destroy(item.gameObject);
//}
2021-04-27 19:47:02 +08:00
//var list = JArray.FromObject(obj["list"]);
//foreach (var item in list)
//{
// var itemObject1 = (GameObject)Instantiate(itemObject, content.transform, false);
// var iii = itemObject1.GetComponent<Item>();
// iii.SetModel(new Assets.Scripts.Apis.Models.NearRouteModel
// {
// Id = item.Value<int>("Id"),
// Name = item.Value<string>("Name"),
// TheHeat = item.Value<int>("TheHat"),
// IsFire = item.Value<bool>("IsFire")
// });
//}
2021-04-27 19:47:02 +08:00
}
/// <summary>
/// 进入大地图
/// </summary>
/// <param name="latLon"></param>
private void EnterBigMap(Vector2 latLon)
{
map.FlyToLocation(latLon.x, latLon.y, 1f, 0.1f).Then(() => {
//Close();
//UIManager.ShowBigMapPanel();
});
maskImage.gameObject.SetActive(true);
maskImage.DOFade(1f, 0.9f).SetEase(Ease.InSine).OnComplete(() => {
Close();
UIManager.PopStack();
2021-04-27 19:47:02 +08:00
UIManager.ShowBigMapPanel(latLon.x, latLon.y);
});
}
private void Map_OnClick(Vector3 sphereLocation, int mouseButtonIndex)
{
//Debug.Log("click");
2021-04-27 19:47:02 +08:00
2021-06-07 17:05:49 +08:00
//Vector2 latLon = Conversion.GetLatLonFromSpherePoint(sphereLocation);
//Debug.Log("Clicked on Latitude: " + latLon.x + ", Longitude: " + latLon.y);
2021-04-27 19:47:02 +08:00
}
private void Map_OnZoomStart(float zoomLevel)
{
//throw new System.NotImplementedException();
Debug.Log(zoomLevel);
}
private void Map_OnZoomEnd(float zoomLevel)
{
Debug.Log(zoomLevel);
}
// Update is called once per frame
void Update()
{
var zoom = map.GetZoomLevel();
2021-04-27 19:47:02 +08:00
//Debug.Log(zoom);
if (showSummary)
{
time += Time.deltaTime;
if (time >= 0.2)
{
ShowSummary(latlonCenter);
showSummary = false;
}
}
//Debug.Log(showSummary + "," + time);
2021-04-30 23:58:11 +08:00
//rot += 0.1f * Time.deltaTime;
//rot %= 360;
//skybox.material.SetFloat("_Rotation", rot);
2021-04-27 19:47:02 +08:00
}
public void Show(double lat, double lon)
{
if (lat != 0 && lon != 0)
{
map.FlyToLocation(new Vector2((float)lat, (float)lon), 0.1f);
}
}
public override void Close()
{
//base.Close();
map.Destroy();
2021-04-27 19:47:02 +08:00
DestroyImmediate(this.gameObject);
2021-05-20 16:15:53 +08:00
GC.SuppressFinalize(this);
2021-04-27 19:47:02 +08:00
}
}