217 lines
7.1 KiB
C#
217 lines
7.1 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
namespace AZTECHWB
|
|
{
|
|
public class Panel_Library : MonoBehaviour
|
|
{
|
|
private RectTransform rectTransform;
|
|
private Button Button_Active;
|
|
private TMP_InputField InputField_MachineSearch;
|
|
private GridLayoutGroup Panel_MachineFilter;
|
|
private ScrollRect ScrollView_MachineList;
|
|
|
|
private RectTransform Hide;
|
|
private RectTransform Mark;
|
|
|
|
private Vector2 originPos;
|
|
public Vector2 downPos;
|
|
|
|
public float fadeTime;
|
|
private bool isActive;
|
|
private string dataOrder;
|
|
|
|
private string currentSearchKeyword = "";
|
|
|
|
private HashSet<Machine> allMachines = new();
|
|
private HashSet<Machine> filteredMachines = new();
|
|
|
|
|
|
private UI_LibraryButton prf_LibraryButton;
|
|
private Dictionary<string, UI_LibraryButton> addLibraryButtons = new();
|
|
private UI_LibraryButton currentLibraryButton;
|
|
|
|
public UI_FilterButton[] filterButtons;
|
|
public UI_FilterButton pre_labelButton;
|
|
|
|
public Action<string> onClickLabelButton;
|
|
public Action<Machine> onClickLibraryButton;
|
|
|
|
public void Init()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
Button_Active = transform.GetComponentInChildren<Button>();
|
|
InputField_MachineSearch = transform.GetComponentInChildren<TMP_InputField>();
|
|
Panel_MachineFilter = transform.GetComponentInChildren<GridLayoutGroup>();
|
|
ScrollView_MachineList = transform.GetComponentInChildren<ScrollRect>();
|
|
|
|
Hide = Button_Active.transform.GetChild(0).GetComponent<RectTransform>();
|
|
Mark = Button_Active.transform.GetChild(1).GetComponent<RectTransform>();
|
|
|
|
dataOrder = Resources.Load<TextAsset>("DataOrder").text;
|
|
prf_LibraryButton = Resources.Load<UI_LibraryButton>("Prefabs/UI/PRF_UI_LibraryButton");
|
|
filterButtons = transform.GetComponentsInChildren<UI_FilterButton>(true);
|
|
|
|
Button_Active.onClick.AddListener(OnClickActiveButton);
|
|
InputField_MachineSearch.onValueChanged.AddListener(OnSearchKeywordChanged);
|
|
|
|
originPos = rectTransform.anchoredPosition;
|
|
isActive = true;
|
|
}
|
|
|
|
public void UpdateFileterList(string[] filters)
|
|
{
|
|
for (int i = 0; i < filters.Length; ++i)
|
|
{
|
|
var label = filters[i];
|
|
|
|
var btn = FindFilterButton(label);
|
|
btn.SettingButton(label);
|
|
btn.onClickButton += OnClickFilterButton;
|
|
}
|
|
OnClickFilterButton(filterButtons[0]);
|
|
}
|
|
private UI_FilterButton FindFilterButton(string label)
|
|
{
|
|
UI_FilterButton button = filterButtons.ToList().Find(a => a.label == label);
|
|
return button;
|
|
}
|
|
private void OnClickFilterButton(UI_FilterButton filterLabel)
|
|
{
|
|
if (pre_labelButton != null)
|
|
{
|
|
pre_labelButton.OnDeselected();
|
|
}
|
|
pre_labelButton = filterLabel;
|
|
pre_labelButton.OnSelected();
|
|
|
|
onClickLabelButton?.Invoke(pre_labelButton.label);
|
|
}
|
|
|
|
public void OnClickActiveButton()
|
|
{
|
|
if (!isActive)
|
|
{
|
|
Open();
|
|
}
|
|
else
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
public void Open()
|
|
{
|
|
isActive = true;
|
|
Mark.gameObject.SetActive(false);
|
|
Hide.gameObject.SetActive(true);
|
|
|
|
StopAllCoroutines();
|
|
StartCoroutine(MoveAnimation(originPos));
|
|
}
|
|
public void Close()
|
|
{
|
|
isActive = false;
|
|
Mark.gameObject.SetActive(true);
|
|
Hide.gameObject.SetActive(false);
|
|
|
|
StopAllCoroutines();
|
|
StartCoroutine(MoveAnimation(downPos));
|
|
}
|
|
public void SetAllMachines(HashSet<Machine> machines)
|
|
{
|
|
allMachines = machines;
|
|
}
|
|
private void OnSearchKeywordChanged(string keyword)
|
|
{
|
|
currentSearchKeyword = keyword;
|
|
|
|
if (string.IsNullOrWhiteSpace(currentSearchKeyword))
|
|
{
|
|
UpdateLibraryMachineList(filteredMachines);
|
|
}
|
|
else
|
|
{
|
|
var matched = allMachines
|
|
.Where(machine => machine.machineName.Contains(currentSearchKeyword, StringComparison.OrdinalIgnoreCase))
|
|
.ToHashSet();
|
|
|
|
UpdateLibraryMachineList(matched);
|
|
}
|
|
}
|
|
private void UpdateLibraryMachineList(HashSet<Machine> targetMachines)
|
|
{
|
|
var content = ScrollView_MachineList.content;
|
|
var sorted = SortListByWorkcd(targetMachines);
|
|
|
|
foreach (var item in addLibraryButtons.Values)
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
|
|
foreach (var machine in sorted)
|
|
{
|
|
if (!addLibraryButtons.ContainsKey(machine.machineName))
|
|
{
|
|
var btn = Instantiate(prf_LibraryButton, content);
|
|
btn.SettingButton(machine);
|
|
btn.onClickButton += OnClickLibraryButton;
|
|
addLibraryButtons.Add(machine.machineName, btn);
|
|
}
|
|
else
|
|
{
|
|
addLibraryButtons[machine.machineName].gameObject.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddLibrarayButtons(HashSet<Machine> machines)
|
|
{
|
|
filteredMachines = machines;
|
|
if (string.IsNullOrWhiteSpace(currentSearchKeyword))
|
|
{
|
|
UpdateLibraryMachineList(filteredMachines);
|
|
}
|
|
}
|
|
public List<Machine> SortListByWorkcd(HashSet<Machine> machines)
|
|
{
|
|
var workcdOrder = JsonConvert.DeserializeObject<List<string>>(dataOrder);
|
|
var orderMap = workcdOrder.Select((workcd, index) => new { workcd, index }).ToDictionary(x => x.workcd, x => x.index);
|
|
|
|
return machines.OrderBy(field => orderMap[field.machineName]).ToList();
|
|
}
|
|
|
|
|
|
private void OnClickLibraryButton(UI_LibraryButton machineButton)
|
|
{
|
|
if (currentLibraryButton != null)
|
|
{
|
|
currentLibraryButton.Image_Selected.gameObject.SetActive(false);
|
|
}
|
|
currentLibraryButton = machineButton;
|
|
currentLibraryButton.Image_Selected.gameObject.SetActive(true);
|
|
|
|
onClickLibraryButton?.Invoke(machineButton.machine);
|
|
}
|
|
|
|
IEnumerator MoveAnimation(Vector2 targetPos)
|
|
{
|
|
float timer = 0f;
|
|
float percent = 0f;
|
|
|
|
while (percent < 1)
|
|
{
|
|
timer += Time.deltaTime;
|
|
percent = timer / fadeTime;
|
|
rectTransform.anchoredPosition = Vector2.Lerp(rectTransform.anchoredPosition, targetPos, percent);
|
|
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
} |