Files

325 lines
10 KiB
C#
Raw Permalink Normal View History

2026-01-16 09:23:25 +09:00
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Cysharp.Threading.Tasks;
using AZTECHWB.Constants;
using AZTECHWB.Management;
using AZTECHWB.Command;
using AZTECHWB.Core;
using AZTECHWB.Extensions;
using UnityEngine.EventSystems;
2026-01-16 09:23:25 +09:00
namespace AZTECHWB.UI
2026-01-16 09:23:25 +09:00
{
public class LibraryPanel : UIPanel
2026-01-16 09:23:25 +09:00
{
private RectTransform rectTransform;
2026-01-16 09:23:25 +09:00
private Button Button_Active;
private RectTransform Hide;
private RectTransform Mark;
private Image Image_SearchIcon;
2026-01-16 09:23:25 +09:00
private TMP_InputField InputField_MachineSearch;
private Button Button_Reset;
public Button Button_Left;
public Button Button_Right;
2026-01-16 09:23:25 +09:00
private ScrollRect ScrollView_MachineList;
float moveAmount = 200f;
2026-01-16 09:23:25 +09:00
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 LibraryButton libraryButton;
private Dictionary<string, LibraryButton> addLibraryButtons = new();
private LibraryButton currentLibraryButton;
2026-01-16 09:23:25 +09:00
private FilterButton[] filterButtons;
private FilterButton currentLabelButton;
2026-01-16 09:23:25 +09:00
public override async UniTask Init()
2026-01-16 09:23:25 +09:00
{
rectTransform = GetComponent<RectTransform>();
Button_Active = transform.GetComponentInChildren<Button>();
Hide = GetElement<RectTransform>(nameof(Hide));
Mark = GetElement<RectTransform>(nameof(Mark));
2026-01-16 09:23:25 +09:00
Image_SearchIcon = GetElement<Image>(nameof(Image_SearchIcon));
InputField_MachineSearch = transform.GetComponentInChildren<TMP_InputField>();
Button_Reset = GetElement<Button>(nameof(Button_Reset));
transform.TryGetComponentInChildren(nameof(Button_Left), out Button_Left);
transform.TryGetComponentInChildren(nameof(Button_Right), out Button_Right);
Button_Active.onClick.AddListener(OnClickActiveButton);
InputField_MachineSearch.onSelect.AddListener(OnSelectedInputField);
InputField_MachineSearch.onDeselect.AddListener(OnDeselectInputField);
InputField_MachineSearch.onValueChanged.AddListener(OnSearchKeywordChanged);
Button_Reset.onClick.AddListener(OnClickInputFieldResetButton);
ScrollView_MachineList = transform.GetComponentInChildren<ScrollRect>();
dataOrder = Resources.Load<TextAsset>($"{ResourceURL.dataFolderPath}DataOrder").text;
libraryButton = Resources.Load<LibraryButton>($"{ResourceURL.uiPrefabFolderPath}{nameof(LibraryButton)}");
filterButtons = transform.GetComponentsInChildren<FilterButton>(true);
2026-01-16 09:23:25 +09:00
Button_Left.onClick.AddListener(MoveLeft);
Button_Right.onClick.AddListener(MoveRight);
2026-01-16 09:23:25 +09:00
originPos = rectTransform.anchoredPosition;
isActive = true;
await UniTask.CompletedTask;
}
public void InitializedLibraryPanel()
{
InputField_MachineSearch.text = string.Empty;
InputField_MachineSearch.ForceLabelUpdate();
if (currentLibraryButton != null)
{
currentLibraryButton.OnDeselected();
currentLibraryButton = null;
}
SetFilterButton(0);
}
private void SetFilterButton(int index)
{
OnClickFilterButton(filterButtons[index]);
2026-01-16 09:23:25 +09:00
}
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;
}
SetFilterButton(0);
2026-01-16 09:23:25 +09:00
}
private FilterButton FindFilterButton(string label)
2026-01-16 09:23:25 +09:00
{
FilterButton button = filterButtons.ToList().Find(a => a.label == label);
2026-01-16 09:23:25 +09:00
return button;
}
private void OnClickFilterButton(FilterButton filterLabel)
2026-01-16 09:23:25 +09:00
{
if (currentLabelButton != null)
2026-01-16 09:23:25 +09:00
{
currentLabelButton.OnDeselected();
2026-01-16 09:23:25 +09:00
}
currentLabelButton = filterLabel;
currentLabelButton.OnSelected();
2026-01-16 09:23:25 +09:00
var libraryManager = AZTECHSceneMain.Instance.GetManager<LibraryManager>();
libraryManager.LibraryFiltering(currentLabelButton.label);
2026-01-16 09:23:25 +09:00
}
public void OnClickActiveButton()
{
if (!isActive)
{
Open();
}
else
{
Close();
}
EventSystem.current.SetSelectedGameObject(null);
2026-01-16 09:23:25 +09:00
}
public override void Open()
2026-01-16 09:23:25 +09:00
{
isActive = true;
Mark.gameObject.SetActive(false);
Hide.gameObject.SetActive(true);
StopAllCoroutines();
StartCoroutine(MoveAnimation(originPos));
new MoveLibraryPanelCommand(false).Execute();
2026-01-16 09:23:25 +09:00
}
public override void Close()
2026-01-16 09:23:25 +09:00
{
isActive = false;
Mark.gameObject.SetActive(true);
Hide.gameObject.SetActive(false);
StopAllCoroutines();
StartCoroutine(MoveAnimation(downPos));
new MoveLibraryPanelCommand(true).Execute();
2026-01-16 09:23:25 +09:00
}
public void SetAllMachines(HashSet<Machine> machines)
{
allMachines = machines;
}
private void OnSelectedInputField(string keyword)
{
ColorUtility.TryParseHtmlString("#0F1117",out var color);
Image_SearchIcon.color = color;
Button_Reset.gameObject.SetActive(true);
}
private void OnDeselectInputField(string keyword)
{
ColorUtility.TryParseHtmlString("#9B9B9B",out var color);
Image_SearchIcon.color = color;
Button_Reset.gameObject.SetActive(false);
}
2026-01-16 09:23:25 +09:00
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 OnClickInputFieldResetButton()
{
InputField_MachineSearch.text = string.Empty;
}
2026-01-16 09:23:25 +09:00
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(libraryButton, content);
2026-01-16 09:23:25 +09:00
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(LibraryButton machineButton)
2026-01-16 09:23:25 +09:00
{
if (currentLibraryButton != null)
{
currentLibraryButton.OnDeselected();
2026-01-16 09:23:25 +09:00
}
currentLibraryButton = machineButton;
currentLibraryButton.OnSelected();
2026-01-16 09:23:25 +09:00
new SelectedMachineCommand(machineButton.machine, false).Execute();
2026-01-16 09:23:25 +09:00
}
public void MoveLeft()
{
Move(moveAmount);
EventSystem.current.SetSelectedGameObject(null);
}
public void MoveRight()
{
Move(-moveAmount);
EventSystem.current.SetSelectedGameObject(null);
}
void Move(float delta)
{
RectTransform content = ScrollView_MachineList.content;
Vector2 pos = content.anchoredPosition;
pos.x += delta;
float minX = -(content.rect.width - ScrollView_MachineList.viewport.rect.width);
float maxX = 0f;
pos.x = Mathf.Clamp(pos.x, minX, maxX);
content.anchoredPosition = pos;
UpdateButtons();
}
void UpdateButtons()
{
RectTransform content = ScrollView_MachineList.content;
float minX = -(content.rect.width - ScrollView_MachineList.viewport.rect.width);
float maxX = 0f;
float x = content.anchoredPosition.x;
Button_Left.interactable = x < maxX;
Button_Right.interactable = x > minX;
}
2026-01-16 09:23:25 +09:00
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;
}
}
}
}