powerfun-unity/Assets/Scripts/UI/Prefab/Panel/MapListController.cs
2021-12-30 16:51:04 +08:00

556 lines
19 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using Assets.Scripts.UI.Prefab.MapList;
using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MapListController : PFUIPanel
{
// Start is called before the first frame update
[SerializeField] GameObject map;
[SerializeField] GameObject content;
[SerializeField] GameObject scroll;
[HideInInspector] public int pageIndex = 0;
#if UNITY_ANDROID || UNITY_IOS
[HideInInspector] public int pageSize = 21;
#else
[HideInInspector] public int pageSize = 20;
#endif
[HideInInspector] public string hard = "全部";
private Transform distanceOptions;
private Transform diffOptions;
private Transform tdContainer;
private Transform favContainer;
private Transform sorts;
private Transform sortDir;
private Transform searchInput;
//private Transform distanceOption;
//private Transform distanceOption;
private Button returnBtn;
GameObject btnMapMode;
public GameObject Content {
get
{
return content;
}
}
private bool isEnd = false;
private Transform topContainer;
void ResetPage()
{
var rect = transform.GetComponent<RectTransform>();
rect.offsetMax = new Vector2(rect.offsetMax.x, 0);
rect.offsetMin = new Vector2(rect.offsetMin.x, 0);
rect.localScale = Vector3.one;
rect.localPosition = Vector3.zero;
}
protected override void Start()
{
#if (UNITY_ANDROID || UNITY_IOS)
topContainer = transform.Find("Top");
ResetPage();
//var rectTransform = transform.GetComponent<RectTransform>();
//var offsetMax = rectTransform.offsetMax;
//rectTransform.offsetMax = new Vector2(0, offsetMax.y);
//var offsetMin = rectTransform.offsetMin;
//rectTransform.offsetMin = new Vector2(0, offsetMin.y);
var widthDelta = (transform.parent.parent.GetComponent<RectTransform>().sizeDelta.x -
transform.GetComponent<RectTransform>().sizeDelta.x)/2;
//筛选按钮
var topRect = topContainer.GetComponent<RectTransform>();
topRect.localPosition = new Vector3(topRect.localPosition.x - widthDelta
, topRect.localPosition.y, topRect.localPosition.z);
if (!App.topRectStartX.HasValue)
{
App.topRectStartX = topRect.localPosition.x;
}
if (App.isFullScreen)
{
topRect.sizeDelta = new Vector2(topRect.sizeDelta.x + 20, topRect.sizeDelta.y);
}
var mc = transform.Find("MC");
UIManager.AddEvent(transform.Find("SwitchMode/BtnFilter").gameObject, EventTriggerType.PointerClick, b =>
{
OpenFilter();
});
UIManager.AddEvent(mc.gameObject, EventTriggerType.PointerClick, b =>
{
topContainer.DOLocalMoveX(App.topRectStartX.Value, 0.3f);
mc.GetComponent<CanvasGroup>().DOFade(0, 0.3f).onComplete = () =>
{
mc.gameObject.SetActive(false);
};
});
map = Resources.Load<GameObject>("UI/Prefab/MapList/MapItem-Mobile");
Debug.Log(98);
var nav = transform.Find("Tmp/MainNav-mobile").GetComponent<NewMainNav>();
nav.SetButtonActive(new List<int> { 0, 3, 4, 6, 7 }, 0,
new NewMainNav.CustomButton(Resources.Load<Sprite>("Images/Mobile/map_筛选"), () =>
{
OpenFilter(false);
}));
newNav = nav;
#else
topContainer = transform.Find("Panel").Find("Top");
#endif
#if UNITY_STANDALONE_WIN
btnMapMode = this.transform.Find("SwitchMode").gameObject;
SetRounded(btnMapMode.transform, 64);
UIManager.AddEvent(btnMapMode.transform.Find("GoList").gameObject, EventTriggerType.PointerClick, (e) =>
{
Application.OpenURL("http://192.168.0.101:3081/Mine/Upload?token="+ App.CurrentUser.cookie);
});
transform.Find("MainNav").GetComponent<MainNav>().ShowBack();
#endif
sort = "Near";
sortDire = "asc";
//if (hardSelector != null)
//{
// hardSelector.onValueChanged.AddListener(ChangeHard);
//}
//ApiBase.SetCookie("15A37DF28842CBA8E4E550BFA24186C8222D392F3EE18D9E95A49CC32796D2E3F6E40385C6A114942546EE1DD793DF34880DE48090B91AD2460EB887B59664BB3965F3371DDA1F1401EC7F27C5A1084566E47069F12CB6BBC0BC7A84ADC5582B");
if (content!=null)
{
//var transform = content.transform.GetComponent<RectTransform>();
//var layout = content.GetComponent<GridLayoutGroup>();
//if (layout != null)
//{
// var width = transform.rect.width;
// layout.cellSize = new Vector2((width - 120) / 5, (width - 120) / 5);
//}
}
distanceOptions = topContainer.Find("distanceOptions");
if (distanceOptions != null)
{
var dDropdown = distanceOptions.GetComponent<Dropdown>();
dDropdown.options = MapFilterOptions.distances;
dDropdown.onValueChanged.AddListener(ChangeDistance);
}
diffOptions = topContainer.Find("DifficultyContainer");
if (diffOptions != null)
{
foreach (Transform t in diffOptions)
{
var button = t.GetComponent<Button>();
var image = t.GetComponent<Image>();
var text = t.Find("Text").GetComponent<Text>();
UIManager.AddEvent(button.gameObject, EventTriggerType.PointerClick, (b) =>
{
ColorUtility.TryParseHtmlString("#23232D", out Color c1);
if (image.color.r == c1.r && image.color.g == c1.g && image.color.b == c1.b)
{
ColorUtility.TryParseHtmlString("#F93086", out Color c);
image.color = c;
text.color = new Color(1, 1, 1, 1);
hands.Add(MapFilterOptions.diffDict[text.text]);
}
else
{
ColorUtility.TryParseHtmlString("#6E6E7D", out Color c);
image.color = c1;
text.color = new Color(1, 1, 1, 1);
hands.Remove(MapFilterOptions.diffDict[text.text]);
}
scroll.GetComponent<ScrollRect>().verticalNormalizedPosition = 1;
Refresh();
});
//button.onClick.AddListener();
}
}
BindHeadImage();
Refresh();
if (scroll != null)
{
//scroll.GetComponent<ScrollRect>().onValueChanged.AddListener((v) =>
//{
// OnEndDrag(null);
//});
UIManager.AddEvent(scroll, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
}
//UIManager.AddEvent(transform.Find("Panel").Find("Top"))
//var obj = Instantiate(Resources.Load<GameObject>("Canvas"));
//obj.transform.parent = transform.parent;
var btnQuit = this.transform.Find("BtnQuit");
if(btnQuit != null)
{
returnBtn = btnQuit.GetComponent<Button>();
UIManager.AddEvent(returnBtn.gameObject, EventTriggerType.PointerClick, (e) =>
{
UIManager.ShowHomePanel();
});
}
tdContainer = topContainer.Find("3dContainer");
favContainer = topContainer.Find("FavContainer");
if (tdContainer != null)
{
UIManager.AddEvent(tdContainer.gameObject, EventTriggerType.PointerClick, (b) =>
{
var gou = tdContainer.Find("Gou").gameObject;
bool v = !gou.activeSelf;
gou.SetActive(v);
is3d = v;
Refresh();
});
}
if (favContainer != null)
{
UIManager.AddEvent(favContainer.gameObject, EventTriggerType.PointerClick, (b) =>
{
var gou = favContainer.Find("Gou").gameObject;
bool v = !gou.activeSelf;
gou.SetActive(v);
isFav = v;
Refresh();
});
}
searchInput = topContainer.Find("SearchInput");
if (searchInput != null)
{
searchInput.GetComponent<InputField>().onEndEdit.AddListener((s) =>
{
var t = searchInput.GetComponent<InputField>().text;
ftname = t;
});
UIManager.AddEvent(searchInput.Find("Button").gameObject, EventTriggerType.PointerClick, (b) =>
{
onEndEdit();
});
}
sorts = topContainer.Find("FilterOptions");
if (sorts != null)
{
var drop = sorts.GetComponent<Dropdown>();
drop.options = MapFilterOptions.sorts;
drop.value = 0;
drop.onValueChanged.AddListener((int index)=>
{
var text = drop.options[index].text;
sort = MapFilterOptions.sortsDict[text];
if (sort == "Trend")
{
sort = "Hot";
}
var image = sortDir.Find("Image").GetComponent<Image>();
if (sort == "Near")
{
image.sprite = Resources.Load<Sprite>("Images/UP");
sortDire = "asc";
}
else
{
image.sprite = Resources.Load<Sprite>("Images/DOWN");
sortDire = "desc";
}
Refresh();
});
}
sortDir = topContainer.Find("Dir");
if (sortDir)
{
var image = sortDir.Find("Image").GetComponent<Image>();
UIManager.AddEvent(sortDir.gameObject, EventTriggerType.PointerClick, (b) =>
{
if (image.sprite.name == "DOWN")
{
image.sprite = Resources.Load<Sprite>("Images/UP");
sortDire = "asc";
}
else
{
image.sprite = Resources.Load<Sprite>("Images/DOWN");
sortDire = "desc";
}
Refresh();
});
//sortDir.GetComponent<Button>().onClick.AddListener();
}
caches = new Dictionary<string, Texture>();
}
public void OpenFilter(bool isFocusSearch = true)
{
#if UNITY_ANDROID || UNITY_IOS
var topRect = transform.Find("Top").GetComponent<RectTransform>();
if (!App.topRectStartX.HasValue)
{
App.topRectStartX = topRect.localPosition.x;
}
topRect.DOLocalMoveX(App.topRectStartX.Value + topRect.rect.width, 0.3f);
var mc = transform.Find("MC");
mc.gameObject.SetActive(true);
mc.GetComponent<CanvasGroup>().alpha = 0;
mc.GetComponent<CanvasGroup>().DOFade(1, 0.3f);
#endif
if (isFocusSearch)
{
topRect.Find("SearchInput").GetComponent<InputField>().OnPointerClick(new PointerEventData(EventSystem.current));
}
}
void onEndEdit()
{
var t = searchInput.GetComponent<InputField>().text;
ftname = t;
Refresh();
}
/// <summary>
/// 绑定头像
/// </summary>
void BindHeadImage()
{
var headImage = this.transform.Find("HeadImage").GetComponent<RawImage>();
var material = Instantiate(Resources.Load<Material>("UI/Material/RoundedCornersTextureMaterial"));
var rect = ((RectTransform)headImage.transform).rect;
material.SetVector(Shader.PropertyToID("_WidthHeightRadius"), new Vector4(rect.width, rect.height, rect.height, 0));
headImage.material = material;
//if (App.FromLogin)
//{
Utils.DisplayHead(headImage, App.CurrentUser.WxHeadImg);
//}
UIManager.AddEvent(headImage.gameObject, EventTriggerType.PointerClick, (e) =>
{
UIManager.ShowUserInfoPanel();
});
}
private void OnEndDrag(BaseEventData arg0)
{
var scrollrect = scroll.GetComponent<ScrollRect>();
Debug.Log(scrollrect.verticalNormalizedPosition);
if (scrollrect.verticalNormalizedPosition <= 0)
{
pageIndex++;
GetList();
}
if (scrollrect.verticalNormalizedPosition >= (pageIndex == 0 ? 1.2:1))
{
Refresh();
}
}
private void ChangeDistance(int index)
{
var text = distanceOptions.GetComponent<Dropdown>().options[index].text;
distance = MapFilterOptions.distanceDict[text];
scroll.GetComponent<ScrollRect>().verticalNormalizedPosition = 1;
Refresh();
//var text = hardSelector.options[index].text;
//if (text == "全部难度")
//{
//}
}
//查询条件
string ftname = "";
string distance = "";
List<string> hands = new List<string>();
bool is3d = false,isFav = false,isMine = false,isRecent = false;
string sort = "",sortDire = "";
//string name = "";
//string name = "";
async void GetList()
{
if (isEnd) return;
var res = await ConfigHelper.mapApi.GetList(pageIndex, pageSize, ftname,distance,string.Join(",",hands),is3d,sort,sortDire,isFav,isMine,isRecent);
if (res.result)
{
if (res.data.Count == 0)
{
if (pageIndex != 0)
{
isEnd = true;
scroll.transform.Find("Error").gameObject.SetActive(false);
}
else
{
Debug.Log("当前没查到内容");
scroll.transform.Find("Error").gameObject.SetActive(true);
}
}
else
{
isEnd = false;
DisplayMaps(res.data);
scroll.transform.Find("Error").gameObject.SetActive(false);
}
}
}
public Dictionary<string, Texture> caches = null;
void DisplayMaps(List<MapRoute> list)
{
if (map != null)
{
foreach (var item in list)
{
var obj = Instantiate(map);
obj.GetComponent<MapItem>().Initial(item, caches);
//obj.SendMessage("Initial", );
obj.transform.SetParent(content.transform);
obj.transform.localScale = new Vector3(1, 1, 1);
}
}
}
public void Refresh()
{
content.transform.DestroyChildren();
HandleType();
pageIndex = 0;
isEnd = false;
GetList();
}
public void ResetPanel()
{
#if !(UNITY_ANDROID || UNITY_IOS)
var routeTypeName = transform.Find("Panel/Top/RouteTypeName");
#else
var routeTypeName = transform.Find("RouteTypeName");
#endif
routeTypeName.GetComponent<Text>().text = App.GetLocalString(App.CurrentRouteType);
if (searchInput != null)
{
//empty Data
ftname = string.Empty;
hands = new List<string>();
var text = distanceOptions.GetComponent<Dropdown>().options[0].text;
distance = MapFilterOptions.distanceDict[text];
is3d = false;
isFav = false;
//empty ui
searchInput.GetComponent<InputField>().text = string.Empty;
var dDropdown = distanceOptions.GetComponent<Dropdown>();
dDropdown.value = 0;
if (diffOptions != null)
{
foreach (Transform t in diffOptions)
{
var button = t.GetComponent<Button>();
var image = t.GetComponent<Image>();
var buttonText = t.Find("Text").GetComponent<Text>();
ColorUtility.TryParseHtmlString("#23232D", out Color c);
//ColorUtility.TryParseHtmlString("#F93086", out Color c);
image.color = c;
buttonText.color = new Color(1, 1, 1, 1);
}
}
var isfav = favContainer.Find("Gou").gameObject;
isfav.SetActive(false);
var is3dUI = tdContainer.Find("Gou").gameObject;
is3dUI.SetActive(false);
if (sorts != null)
{
var drop = sorts.GetComponent<Dropdown>();
drop.value = 0;
}
Refresh();
}
}
void HandleType()
{
isRecent = false;
isFav = false;
isMine = false;
if (App.CurrentRouteType == "My Upload")
{
isMine = true;
}
else if (App.CurrentRouteType == "My Collection" || topContainer.Find("FavContainer/Gou").gameObject.activeInHierarchy)
{
isFav = true;
}
else if (App.CurrentRouteType == "Recent Routes")
{
isRecent = true;
}
#if UNITY_STANDALONE_WIN
btnMapMode.SetActive(App.CurrentRouteType == "My Upload");
#endif
topContainer.Find("FavContainer").gameObject.SetActive(App.CurrentRouteType != "My Collection");
}
void ResetList()
{
distance = "";
hands = new List<string>();
name = "";
Refresh();
}
// Update is called once per frame
bool startMouse = false;
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
if (scroll.GetComponent<ScrollRect>().verticalNormalizedPosition <= 0 ||
scroll.GetComponent<ScrollRect>().verticalNormalizedPosition >= (pageIndex == 0 ? 1.2 : 1))
{
startMouse = true;
}
}
else
{
if (startMouse)
{
startMouse = false;
OnEndDrag(null);
}
}
if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
{
onEndEdit();
}
}
public override void Show()
{
base.Show();
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;
}
GetComponent<MapListExtraController>().HandleCollection();
#if UNITY_ANDROID || UNITY_IOS
ResetPage();
#endif
ResetPanel();
transform.MyDOFade();
}
protected override void OnDestroy()
{
caches = null;
Resources.UnloadUnusedAssets();
GC.Collect();
Debug.Log("list empty");
}
}