powerfun-unity/Assets/Scripts/UI/Prefab/ResultList/ResultListController.cs
2021-04-15 15:58:37 +08:00

219 lines
7.0 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ResultListController : MonoBehaviour
{
[SerializeField]Transform routeResult;
Transform scrollContent;
Transform routeScroll, matchScroll;
Transform routeContent, matchContent;
Transform btnRoute, btnMatch,searchInput;
Color c1, c2;
// Start is called before the first frame update
int pageIndex = 0, pageSize = 20;
string name = "";
Transform[] contents;
void Start()
{
//ApiBase.SetCookie("73385F5F719B610D132C1ECF3E9143272BF15214D57ED91CD7A9DFD832407471535112AAEB8E9271F75D54FBBF2D99F18FA313C1EEA5676F5D722D7FBB07C926BEC5905591BF9AFDDC6336552DF273112C2DA1794E6FA2F465B11FECD2E82E52");
InitialColor();
scroll = transform.Find("ListPanel").Find("Scroll").GetComponent<ScrollRect>();
btnMatch = transform.Find("ListPanel").Find("BtnMatch");
btnRoute = transform.Find("ListPanel").Find("BtnRoute");
searchInput = transform.Find("ListPanel").Find("SearchInput");
scrollContent = scroll.transform.Find("Viewport").Find("Content");
routeScroll = scrollContent.Find("RouteList");
routeContent = routeScroll.Find("Viewport").Find("Content");
matchScroll = scrollContent.Find("MatchList");
matchContent = matchScroll.Find("Viewport").Find("Content");
contents = new Transform[] { routeContent, matchContent};
if (btnMatch != null)
{
btnMatch.GetComponent<Button>().onClick.AddListener(() => StartScroll(0));
}
if (btnRoute != null)
{
btnRoute.GetComponent<Button>().onClick.AddListener(() => StartScroll(1));
}
if (searchInput != null)
{
searchInput.Find("Button").GetComponent<Button>().onClick.AddListener(() =>
{
name = searchInput.GetComponent<InputField>().text;
Refresh(contents[scrollIndex]);
});
}
if (routeScroll != null)
{
UIManager.AddEvent(routeScroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
}
if (matchScroll)
{
UIManager.AddEvent(matchScroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
}
GetList();
}
private void OnEndDrag(BaseEventData e)
{
var pe = (PointerEventData)e;
var scrollrect = pe.pointerDrag.GetComponent<ScrollRect>();
if (scrollrect.verticalNormalizedPosition <= 0)
{
Debug.Log(scrollrect.verticalNormalizedPosition);
pageIndex++;
GetList();
}
if (scrollrect.verticalNormalizedPosition >= 1)
{
Refresh(contents[scrollIndex]);
}
}
private void Refresh(Transform content)
{
content.transform.DestroyChildren();
pageIndex = 0;
GetList();
}
bool isEnd = false;
void GetList()
{
var r =ConfigHelper.mapInterruptRecordApi.GetMapInterruptRecord(name, pageIndex, 20);
if (r.result)
{
if (r.data.Count == 0 && pageIndex != 0)
{
isEnd = true;
}
else
{
isEnd = false;
DisplayRouteResult(r.data,contents[scrollIndex]);
//DisplayRouteResult(r.data, matchContent);
}
}
else
{
Utils.showToast(gameObject, r.errMsg);
}
}
void DisplayRouteResult(List<RouteResult> list,Transform content)
{
if (routeResult != null)
{
foreach (var item in list)
{
var obj = Instantiate(routeResult);
obj.GetComponent<RouteItem>().Initial(item);
//obj.SendMessage("Initial", );
obj.transform.parent = content;
obj.transform.localScale = new Vector3(1, 1, 1);
}
}
}
void InitialColor()
{
ColorUtility.TryParseHtmlString("#FFFFFF", out c1);
ColorUtility.TryParseHtmlString("#5C5C6E", out c2);
}
// Update is called once per frame
void Update()
{
GoScroll();
}
void StartScroll(int index)
{
if (startScroll) return;
if (index == 0)
{
//翻到比赛页,比赛按钮变白,线路按钮变灰
scrollIndex = 1;
startScroll = true;
btnRoute.GetComponent<Text>().color = c2;
btnMatch.GetComponent<Text>().color = c1;
}
else if (index == 1)
{
//反过来
scrollIndex = 0;
startScroll = true;
btnRoute.GetComponent<Text>().color = c1;
btnMatch.GetComponent<Text>().color = c2;
}
}
#region
int scrollIndex = 0;
bool startScroll = false;
ScrollRect scroll;
//private float scrollValue = 0.5f;
int pageNums = 2;
void GoScroll()
{
var index = scrollIndex;
var scrollValue = 1f / (pageNums - 1);
var value = index * scrollValue;
if (scroll != null && startScroll)
{
if (scroll.horizontalNormalizedPosition >= value)
{
scroll.horizontalNormalizedPosition -= (scrollValue / 20);
if (scroll.horizontalNormalizedPosition <= value)
{
Debug.Log(index);
scroll.horizontalNormalizedPosition = value;
startScroll = false;
}
}
else
{
scroll.horizontalNormalizedPosition += (scrollValue / 20);
if (scroll.horizontalNormalizedPosition >= value)
{
Debug.Log(index);
scroll.horizontalNormalizedPosition = value;
startScroll = false;
}
}
}
}
#endregion
//float scrollIndex = 0;
//bool startScroll = false;
//ScrollRect scroll;
//void GoScroll()
//{
// float value = scrollIndex;
// if (scroll != null && startScroll)
// {
// if (scrollIndex == 0)
// {
// scroll.horizontalNormalizedPosition -= (1f / 20);
// if (scroll.horizontalNormalizedPosition <= 0)
// {
// scroll.horizontalNormalizedPosition = 0;
// startScroll = false;
// }
// }
// else if (scrollIndex == 1)
// {
// scroll.horizontalNormalizedPosition += (1f / 20);
// if (scroll.horizontalNormalizedPosition >= 1)
// {
// scroll.horizontalNormalizedPosition = 1;
// startScroll = false;
// }
// }
// }
//}
}