Files
AZTECH_WB/Assets/Scripts/UI/Library/LibraryPanel.cs
정영민 18bc516f1d [정영민] 아즈텍 UI 디자인 변경 및 기능 수정 1차
26.04.03
가상공장 화면 디자인 반영
  • 좌측 도구 창, 상단 UI, 하단 UI
  • 생산 현황판, 알림 현황판
  • 설비 라벨, 요약 팝업, 대시보드, 데이터 보드
  • 설정창, 미니맵, AI 시뮬레이션 결과창, 나가기 팝업
가상 공장 화면 디자인 변경에 따른 기능 추가
  • 미니맵 드래그 및 위치 조정 기능 추가
  • UI 상호 작용(호버링, 선택, 위치 이동, 재설정)
  • UI 기능 추가(상태 표시, 색상)
2026-04-03 11:50:05 +09:00

325 lines
10 KiB
C#

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;
namespace AZTECHWB.UI
{
public class LibraryPanel : UIPanel
{
private RectTransform rectTransform;
private Button Button_Active;
private RectTransform Hide;
private RectTransform Mark;
private Image Image_SearchIcon;
private TMP_InputField InputField_MachineSearch;
private Button Button_Reset;
public Button Button_Left;
public Button Button_Right;
private ScrollRect ScrollView_MachineList;
float moveAmount = 200f;
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;
private FilterButton[] filterButtons;
private FilterButton currentLabelButton;
public override async UniTask Init()
{
rectTransform = GetComponent<RectTransform>();
Button_Active = transform.GetComponentInChildren<Button>();
Hide = GetElement<RectTransform>(nameof(Hide));
Mark = GetElement<RectTransform>(nameof(Mark));
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);
Button_Left.onClick.AddListener(MoveLeft);
Button_Right.onClick.AddListener(MoveRight);
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]);
}
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);
}
private FilterButton FindFilterButton(string label)
{
FilterButton button = filterButtons.ToList().Find(a => a.label == label);
return button;
}
private void OnClickFilterButton(FilterButton filterLabel)
{
if (currentLabelButton != null)
{
currentLabelButton.OnDeselected();
}
currentLabelButton = filterLabel;
currentLabelButton.OnSelected();
var libraryManager = AZTECHSceneMain.Instance.GetManager<LibraryManager>();
libraryManager.LibraryFiltering(currentLabelButton.label);
}
public void OnClickActiveButton()
{
if (!isActive)
{
Open();
}
else
{
Close();
}
EventSystem.current.SetSelectedGameObject(null);
}
public override void Open()
{
isActive = true;
Mark.gameObject.SetActive(false);
Hide.gameObject.SetActive(true);
StopAllCoroutines();
StartCoroutine(MoveAnimation(originPos));
new MoveLibraryPanelCommand(false).Execute();
}
public override void Close()
{
isActive = false;
Mark.gameObject.SetActive(true);
Hide.gameObject.SetActive(false);
StopAllCoroutines();
StartCoroutine(MoveAnimation(downPos));
new MoveLibraryPanelCommand(true).Execute();
}
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);
}
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;
}
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);
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)
{
if (currentLibraryButton != null)
{
currentLibraryButton.OnDeselected();
}
currentLibraryButton = machineButton;
currentLibraryButton.OnSelected();
new SelectedMachineCommand(machineButton.machine, false).Execute();
}
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;
}
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;
}
}
}
}