330 lines
8.8 KiB
C#
330 lines
8.8 KiB
C#
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Pool;
|
|
using Cysharp.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using System.Linq;
|
|
using System;
|
|
using BestHTTP;
|
|
|
|
public class SearchCtrl : SingletonMonoBehaviour<SearchCtrl>
|
|
{
|
|
//옵션 페이지 UI
|
|
[SerializeField] GameObject opsion;
|
|
[SerializeField] Image searchMenuButtonImage;
|
|
[SerializeField] Sprite[] searchMenuButton;
|
|
[SerializeField] TMP_Text searchMenuButtonText;
|
|
[SerializeField] TMP_InputField name;
|
|
[SerializeField] TMP_Dropdown suitStyle;
|
|
[SerializeField] TMP_Dropdown suitType;
|
|
[SerializeField] TMP_Dropdown region;
|
|
[SerializeField] TMP_Dropdown animalTypes;
|
|
[SerializeField] Toggle[] colors;
|
|
[SerializeField] Sprite[] isOnSprite;
|
|
|
|
// 검색 결과 UI
|
|
[SerializeField] ItemPrefab item;
|
|
[SerializeField] Transform results;
|
|
[SerializeField] GameObject listTop;
|
|
[SerializeField] GameObject bar;
|
|
[SerializeField] RectTransform content;
|
|
[SerializeField] Animator searchUI;
|
|
|
|
// 모달 UI
|
|
[SerializeField] ModalCtrl model;
|
|
|
|
// 검색 결과 데이터
|
|
private SearchRequest request;
|
|
private List<Library> librarys;
|
|
private bool hasMore;
|
|
|
|
// 검색결과 UI 프리팹
|
|
private List<ItemPrefab> itemPrefabs;
|
|
private ObjectPool<ItemPrefab> itemPrefabPool;
|
|
|
|
// ???
|
|
private bool isSearching = false;
|
|
private bool isRefreshing = false;
|
|
private Queue<Library> searchResults = new Queue<Library>();
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
librarys = new List<Library>();
|
|
itemPrefabPool = new ObjectPool<ItemPrefab>(itemPrefabs_OnCreate, itemPrefabs_OnGet, itemPrefabs_OnRelease, null, true, 100);
|
|
itemPrefabs = new List<ItemPrefab>();
|
|
}
|
|
|
|
#region ObjectPool
|
|
private ItemPrefab itemPrefabs_OnCreate()
|
|
{
|
|
return Instantiate<ItemPrefab>(item, results);
|
|
}
|
|
|
|
private void itemPrefabs_OnGet(ItemPrefab itemPrefab)
|
|
{
|
|
itemPrefabs.Add(itemPrefab);
|
|
itemPrefab.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void itemPrefabs_OnRelease(ItemPrefab itemPrefab)
|
|
{
|
|
itemPrefab.gameObject.SetActive(false);
|
|
itemPrefabs.Remove(itemPrefab);
|
|
}
|
|
#endregion
|
|
|
|
private void OnEnable()
|
|
{
|
|
hasMore = false;
|
|
model.gameObject.SetActive(false);
|
|
searchUI.SetTrigger("reset");
|
|
ResetButton();
|
|
ObjectClear();
|
|
}
|
|
|
|
private void ObjectClear()
|
|
{
|
|
results.gameObject.SetActive(false);
|
|
listTop.SetActive(false);
|
|
bar.SetActive(false);
|
|
librarys.Clear();
|
|
while (itemPrefabPool.CountActive > 0)
|
|
{
|
|
var item = itemPrefabs[itemPrefabs.Count - 1];
|
|
itemPrefabPool.Release(item);
|
|
}
|
|
}
|
|
|
|
|
|
public void OpsionButton()
|
|
{
|
|
//옵션창이 나오지 않았을때 250,100
|
|
//옵션창이 나왔을때 60 ,290
|
|
bool isSelf = !opsion.activeSelf;
|
|
searchMenuButtonImage.sprite = searchMenuButton[(isSelf ? 1 : 0)];
|
|
searchMenuButtonImage.pixelsPerUnitMultiplier = (isSelf ? 3.5f : 1.75f);
|
|
searchMenuButtonText.color = (isSelf ? Color.white : Color.black);
|
|
searchUI.SetBool("isSelf", isSelf);
|
|
ResetButton();
|
|
}
|
|
|
|
public void SearchButton()
|
|
{
|
|
ObjectClear();
|
|
request = new SearchRequest();
|
|
request.fursuitName = name.text;
|
|
if (suitStyle.value != 0)
|
|
request.suitStyle = GameManager.Instance.suitStyle[suitStyle.value - 1].code_id;
|
|
if (suitType.value != 0)
|
|
request.suitType = GameManager.Instance.suitType[suitType.value - 1].code_id;
|
|
if (region.value != 0)
|
|
request.suitRegion = GameManager.Instance.region[region.value - 1].code_id;
|
|
if (animalTypes.value != 0)
|
|
request.suitAnimalType = GameManager.Instance.animalTypes[animalTypes.value - 1].code_id;
|
|
|
|
results.gameObject.SetActive(true);
|
|
listTop.SetActive(true);
|
|
bar.SetActive(true);
|
|
SearchSuiters().Forget();
|
|
}
|
|
|
|
public async UniTask SearchSuiters()
|
|
{
|
|
if (isSearching) return;
|
|
isSearching = true;
|
|
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
HTTPRequest searchRequest = NetworkManager.Instance.CreateRequest<SearchResponse>(
|
|
"library/select/suiters",
|
|
request,
|
|
(data) =>
|
|
{
|
|
for (int n = 0; n < data.data.Count; n++)
|
|
{
|
|
searchResults.Enqueue(data.data[n]);
|
|
}
|
|
hasMore = data.hasMore;
|
|
tcs.SetResult(true); // 응답 완료 신호
|
|
}
|
|
);
|
|
|
|
await searchRequest.Send();
|
|
|
|
await tcs.Task; // 응답이 완료될 때까지 대기
|
|
await RefreshScroll();
|
|
isSearching = false;
|
|
|
|
|
|
}
|
|
|
|
public async UniTask RefreshScroll()
|
|
{
|
|
if (isRefreshing) return;
|
|
isRefreshing = true;
|
|
|
|
if (searchResults.Count > 0)
|
|
{
|
|
while (searchResults.TryDequeue(out var result))
|
|
{
|
|
librarys.Add(result);
|
|
}
|
|
|
|
// librarys.Sort((a,b) => a.owner_id.CompareTo(b.owner_id));
|
|
}
|
|
|
|
int n = 0;
|
|
for (; n < itemPrefabs.Count; n++)
|
|
{
|
|
var item = itemPrefabs[n];
|
|
item.Set(librarys[n]);
|
|
await UniTask.Yield();
|
|
}
|
|
|
|
|
|
for (; n < librarys.Count; n++)
|
|
{
|
|
var item = itemPrefabPool.Get();
|
|
item.Set(librarys[n]);
|
|
await UniTask.Yield();
|
|
}
|
|
|
|
for (; n > librarys.Count; n--)
|
|
{
|
|
var item = itemPrefabs.Last();
|
|
itemPrefabPool.Release(item);
|
|
await UniTask.Yield();
|
|
}
|
|
await ApplayImages();
|
|
// LayoutRebuilder.ForceRebuildLayoutImmediate(content);
|
|
isRefreshing = false;
|
|
}
|
|
|
|
private async UniTask ApplayImages()
|
|
{
|
|
for (int i = 0; itemPrefabs.Count > i; i++)
|
|
{
|
|
await GetImage(itemPrefabs[i].library);
|
|
itemPrefabs[i].ImageSet();
|
|
}
|
|
}
|
|
|
|
public async UniTask<Texture2D> GetImage(Library library)
|
|
{
|
|
if (library._suit_image != null)
|
|
{
|
|
return library._suit_image;
|
|
}
|
|
|
|
var tcs = new TaskCompletionSource<Texture2D>();
|
|
|
|
var request = NetworkManager.Instance.CreateImageRequest(
|
|
library.imageBase64.Substring(1),
|
|
new object(),
|
|
(data) =>
|
|
{
|
|
library._suit_image = data;
|
|
tcs.SetResult(data);
|
|
},
|
|
(fail) =>
|
|
{
|
|
Debug.LogError("GetImage Fail");
|
|
tcs.SetResult(null);
|
|
},
|
|
BestHTTP.HTTPMethods.Get
|
|
);
|
|
|
|
await request.Send();
|
|
|
|
await tcs.Task;
|
|
return library._suit_image;
|
|
}
|
|
|
|
public void NextSearch()
|
|
{
|
|
if (isSearching == false)
|
|
{
|
|
hasMore = false;
|
|
request.page++;
|
|
SearchSuiters().Forget();
|
|
}
|
|
}
|
|
|
|
public void ResetButton()
|
|
{
|
|
suitStyle.value = 0;
|
|
suitType.value = 0;
|
|
region.value = 0;
|
|
animalTypes.value = 0;
|
|
for(int n = 0; n < colors.Length; n++)
|
|
{
|
|
colors[n].isOn = false;
|
|
colors[n].image.sprite = isOnSprite[0];
|
|
}
|
|
}
|
|
|
|
public void OnToggle(int velue)
|
|
{
|
|
colors[velue].image.sprite = isOnSprite[(colors[velue].isOn ? 1 : 0)];
|
|
}
|
|
|
|
public void ModelSet(Library library)
|
|
{
|
|
model.Set(library);
|
|
}
|
|
|
|
public void ScrollValue(RectTransform Content) {
|
|
if(Content.anchoredPosition.y < 0.0f)
|
|
{
|
|
Content.anchoredPosition = Vector2.zero;
|
|
}
|
|
else if(hasMore && Content.sizeDelta.y != Screen.height && Content.sizeDelta.y - Screen.height < Content.anchoredPosition.y)
|
|
{
|
|
NextSearch();
|
|
}
|
|
}
|
|
}
|
|
|
|
public class SearchRequest
|
|
{
|
|
public int xid;
|
|
public string fursuitName = ""; /*오너,슈트 이름*/
|
|
public string suitMaker; /*사용안함*/
|
|
public string suitRegion = ""; /*활동 국가*/
|
|
public string suitStyle = ""; /*슈트 스타일*/
|
|
public string suitAnimalType = ""; /*슈트 동물 종*/
|
|
public string suitType = ""; /*슈트 형태*/
|
|
public List<string> colorList = new List<string>(); /*슈트 색상*/
|
|
public int page = 1;
|
|
public int size = 5;
|
|
public int offset = 0;
|
|
}
|
|
public class SearchResponse
|
|
{
|
|
public List<Library> data;
|
|
public bool hasMore;
|
|
}
|
|
public class ImageResponse
|
|
{
|
|
public byte[] imageBytes;
|
|
}
|
|
|
|
[Serializable]
|
|
public class Library
|
|
{
|
|
public string imageBase64;
|
|
public string suit_name;
|
|
public string owner_id;
|
|
public string owner_name;
|
|
public string production_date;
|
|
public string suit_image;//사용안함
|
|
public Texture2D _suit_image = null;
|
|
public string maker;
|
|
public string region;
|
|
public string animal_type;
|
|
public string suit_style;
|
|
public string suit_type;
|
|
public List<string> suit_color;
|
|
} |