469 lines
17 KiB
C#
469 lines
17 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
namespace UVC.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// UTKComponentList를 래핑하여 윈도우 형태로 제공하는 컴포넌트입니다.
|
|
///
|
|
/// <para><b>개요:</b></para>
|
|
/// <para>
|
|
/// UTKComponentListWindow는 UTKComponentList를 내부에 포함하고 헤더(타이틀, 닫기 버튼)를 추가한
|
|
/// 윈도우 형태의 컴포넌트입니다. 모든 트리 관련 기능은 내부 UTKComponentList에 위임됩니다.
|
|
/// </para>
|
|
///
|
|
/// <para><b>UXML 사용 예시:</b></para>
|
|
/// <code><![CDATA[
|
|
/// <!-- UXML 파일에서 UTKComponentListWindow 사용 -->
|
|
/// <ui:UXML xmlns:utk="UVC.UIToolkit">
|
|
/// <utk:UTKComponentListWindow name="component-window" />
|
|
/// </ui:UXML>
|
|
/// ]]></code>
|
|
///
|
|
/// <para><b>C# 사용 예시:</b></para>
|
|
/// <code><![CDATA[
|
|
/// // 1. 윈도우 참조 획득
|
|
/// var componentWindow = root.Q<UTKComponentListWindow>("component-window");
|
|
///
|
|
/// // 2. 윈도우 제목 및 닫기 버튼 설정
|
|
/// componentWindow.Title = "모델 리스트";
|
|
/// componentWindow.ShowCloseButton = true;
|
|
///
|
|
/// // 3. 데이터 구성 - 카테고리와 아이템
|
|
/// var data = new List<UTKComponentListItemDataBase>
|
|
/// {
|
|
/// new UTKComponentListCategoryData
|
|
/// {
|
|
/// name = "캐릭터",
|
|
/// isExpanded = true,
|
|
/// children = new List<UTKComponentListItemDataBase>
|
|
/// {
|
|
/// new UTKComponentListItemData
|
|
/// {
|
|
/// name = "플레이어",
|
|
/// ExternalKey = "player_001",
|
|
/// IsVisible = true
|
|
/// },
|
|
/// new UTKComponentListItemData
|
|
/// {
|
|
/// name = "NPC",
|
|
/// ExternalKey = "npc_001",
|
|
/// IsVisible = true
|
|
/// }
|
|
/// }
|
|
/// }
|
|
/// };
|
|
/// componentWindow.SetData(data);
|
|
///
|
|
/// // 4. 선택 이벤트 구독
|
|
/// componentWindow.OnItemSelected = (selectedItems) =>
|
|
/// {
|
|
/// foreach (var item in selectedItems)
|
|
/// {
|
|
/// Debug.Log($"선택됨: {item.name}");
|
|
/// // 3D 뷰에서 해당 모델 하이라이트
|
|
/// HighlightModel(item.ExternalKey);
|
|
/// }
|
|
/// };
|
|
///
|
|
/// // 5. 선택 해제 이벤트
|
|
/// componentWindow.OnItemDeselected = (deselectedItems) =>
|
|
/// {
|
|
/// foreach (var item in deselectedItems)
|
|
/// {
|
|
/// UnhighlightModel(item.ExternalKey);
|
|
/// }
|
|
/// };
|
|
///
|
|
/// // 6. 가시성 변경 이벤트 (눈 아이콘)
|
|
/// componentWindow.OnItemVisibilityChanged += (item, isVisible) =>
|
|
/// {
|
|
/// var gameObject = FindGameObjectByKey(item.ExternalKey);
|
|
/// if (gameObject != null)
|
|
/// {
|
|
/// gameObject.SetActive(isVisible);
|
|
/// }
|
|
/// };
|
|
///
|
|
/// // 7. 삭제 이벤트 (Delete/Backspace 키)
|
|
/// componentWindow.EnabledDeleteItem = true;
|
|
/// componentWindow.OnItemDeleted = (item) =>
|
|
/// {
|
|
/// Debug.Log($"삭제 요청: {item.name}");
|
|
/// componentWindow.DeleteItem(item);
|
|
/// };
|
|
///
|
|
/// // 8. 더블클릭 이벤트
|
|
/// componentWindow.OnItemDoubleClicked = (item) =>
|
|
/// {
|
|
/// // 카메라 포커스
|
|
/// FocusCameraOn(item.ExternalKey);
|
|
/// };
|
|
///
|
|
/// // 9. 윈도우 닫힘 이벤트
|
|
/// componentWindow.OnClosed += () =>
|
|
/// {
|
|
/// Debug.Log("윈도우가 닫혔습니다.");
|
|
/// };
|
|
///
|
|
/// // 10. 프로그래밍 방식 선택
|
|
/// componentWindow.SelectItem("플레이어", notify: true);
|
|
/// componentWindow.DeselectItem("플레이어", notify: false);
|
|
/// componentWindow.ClearSelection();
|
|
/// componentWindow.SelectByItemId(itemId);
|
|
///
|
|
/// // 11. 아이템 추가/삭제
|
|
/// var newItem = new UTKComponentListItemData { name = "새 캐릭터" };
|
|
/// componentWindow.AddItem(newItem);
|
|
/// componentWindow.AddItem(categoryData, newItem);
|
|
/// componentWindow.DeleteItem(newItem);
|
|
/// componentWindow.SetItemName(newItem, "수정된 이름");
|
|
///
|
|
/// // 12. 윈도우 표시
|
|
/// componentWindow.Show();
|
|
///
|
|
/// // 13. 리소스 해제 (OnDestroy에서 호출)
|
|
/// componentWindow.Dispose();
|
|
/// ]]></code>
|
|
/// </summary>
|
|
[UxmlElement]
|
|
public partial class UTKComponentListWindow : VisualElement, IDisposable
|
|
{
|
|
#region IDisposable
|
|
private bool _disposed = false;
|
|
#endregion
|
|
|
|
#region 상수 (Constants)
|
|
/// <summary>메인 UXML 파일 경로 (Resources 폴더 기준)</summary>
|
|
private const string UXML_PATH = "UIToolkit/Window/UTKComponentListWindow";
|
|
|
|
/// <summary>USS 파일 경로 (Resources 폴더 기준)</summary>
|
|
private const string USS_PATH = "UIToolkit/Window/UTKComponentListWindowUss";
|
|
#endregion
|
|
|
|
#region UI 컴포넌트 참조 (UI Component References)
|
|
/// <summary>내부 UTKComponentList 컴포넌트</summary>
|
|
private UTKComponentList? _componentList;
|
|
|
|
/// <summary>트리 리스트 닫기 버튼 (UTKButton)</summary>
|
|
private UTKButton? _closeButton;
|
|
|
|
/// <summary>윈도우 제목 라벨</summary>
|
|
private Label? _titleLabel;
|
|
#endregion
|
|
|
|
#region 공개 속성 (Public Properties)
|
|
/// <summary>
|
|
/// 항목 삭제 기능 활성화 여부입니다.
|
|
/// true일 때만 Delete/Backspace 키로 항목 삭제 이벤트가 발생합니다.
|
|
/// 기본값은 false입니다.
|
|
/// </summary>
|
|
public bool EnabledDeleteItem
|
|
{
|
|
get => _componentList?.EnabledDeleteItem ?? false;
|
|
set { if (_componentList != null) _componentList.EnabledDeleteItem = value; }
|
|
}
|
|
|
|
/// <summary>윈도우 제목을 가져오거나 설정합니다.</summary>
|
|
public string Title
|
|
{
|
|
get => _titleLabel?.text ?? string.Empty;
|
|
set { if (_titleLabel != null) _titleLabel.text = value; }
|
|
}
|
|
|
|
/// <summary>닫기 버튼 표시 여부를 설정합니다.</summary>
|
|
public bool ShowCloseButton
|
|
{
|
|
get => _closeButton?.style.display == DisplayStyle.Flex;
|
|
set { if (_closeButton != null) _closeButton.style.display = value ? DisplayStyle.Flex : DisplayStyle.None; }
|
|
}
|
|
#endregion
|
|
|
|
#region 외부 이벤트 (Public Events)
|
|
/// <summary>
|
|
/// 메인/검색 리스트에서 항목이 선택될 때 발생합니다.
|
|
/// </summary>
|
|
public Action<List<UTKComponentListItemDataBase>>? OnItemSelected
|
|
{
|
|
get => _componentList?.OnItemSelected;
|
|
set { if (_componentList != null) _componentList.OnItemSelected = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 메인/검색 리스트에서 항목이 선택 해제될 때 발생합니다.
|
|
/// </summary>
|
|
public Action<List<UTKComponentListItemDataBase>>? OnItemDeselected
|
|
{
|
|
get => _componentList?.OnItemDeselected;
|
|
set { if (_componentList != null) _componentList.OnItemDeselected = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 항목의 가시성(눈 아이콘)이 변경될 때 발생합니다.
|
|
/// </summary>
|
|
public event Action<UTKComponentListItemDataBase, bool>? OnItemVisibilityChanged
|
|
{
|
|
add { if (_componentList != null) _componentList.OnItemVisibilityChanged += value; }
|
|
remove { if (_componentList != null) _componentList.OnItemVisibilityChanged -= value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 메인/검색 리스트에서 항목이 삭제될 때 발생합니다 (Delete 키).
|
|
/// </summary>
|
|
public Action<UTKComponentListItemDataBase>? OnItemDeleted
|
|
{
|
|
get => _componentList?.OnItemDeleted;
|
|
set { if (_componentList != null) _componentList.OnItemDeleted = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 메인/검색 리스트에서 항목이 더블클릭될 때 발생합니다.
|
|
/// </summary>
|
|
public Action<UTKComponentListItemDataBase>? OnItemDoubleClicked
|
|
{
|
|
get => _componentList?.OnItemDoubleClicked;
|
|
set { if (_componentList != null) _componentList.OnItemDoubleClicked = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 아이콘을 클릭할 때 발생합니다.
|
|
/// </summary>
|
|
public Action<string, UTKComponentListItemDataBase>? OnItemIconClicked
|
|
{
|
|
get => _componentList?.OnItemIconClicked;
|
|
set { if (_componentList != null) _componentList.OnItemIconClicked = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 트리 리스트가 닫힐 때(숨겨질 때) 발생합니다.
|
|
/// 닫기 버튼 클릭 시 트리거됩니다.
|
|
/// </summary>
|
|
public event Action? OnClosed;
|
|
#endregion
|
|
|
|
#region 생성자 (Constructor)
|
|
/// <summary>
|
|
/// UTKComponentListWindow 컴포넌트를 초기화합니다.
|
|
/// UXML 템플릿을 로드하고 내부 UTKComponentList를 설정합니다.
|
|
/// </summary>
|
|
public UTKComponentListWindow()
|
|
{
|
|
// 1. 메인 UXML 로드 및 복제
|
|
var visualTree = Resources.Load<VisualTreeAsset>(UXML_PATH);
|
|
if (visualTree == null)
|
|
{
|
|
Debug.LogError($"[UTKComponentListWindow] UXML not found at: {UXML_PATH}");
|
|
return;
|
|
}
|
|
visualTree.CloneTree(this);
|
|
|
|
// 2. 내부 UTKComponentList 찾기 (UXML의 ui:Instance로 생성된 컴포넌트)
|
|
_componentList = this.Q<UTKComponentList>();
|
|
if (_componentList == null)
|
|
{
|
|
Debug.LogError("[UTKComponentListWindow] UTKComponentList not found in UXML");
|
|
return;
|
|
}
|
|
|
|
// 3. 테마 적용 및 변경 구독
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
SubscribeToThemeChanges();
|
|
|
|
// USS 로드 (테마 변수 스타일시트 이후에 로드되어야 변수가 해석됨)
|
|
var uss = Resources.Load<StyleSheet>(USS_PATH);
|
|
if (uss != null)
|
|
{
|
|
styleSheets.Add(uss);
|
|
}
|
|
|
|
// 4. 헤더 요소 참조
|
|
_titleLabel = this.Q<Label>("title");
|
|
_closeButton = this.Q<UTKButton>("close-btn");
|
|
|
|
// 5. 닫기 버튼 설정 및 이벤트 연결
|
|
if (_closeButton != null)
|
|
{
|
|
_closeButton.SetMaterialIcon(UTKMaterialIcons.Close, 16);
|
|
_closeButton.OnClicked += OnCloseButtonClicked;
|
|
}
|
|
}
|
|
|
|
/// <summary>닫기 버튼 클릭 이벤트 핸들러</summary>
|
|
private void OnCloseButtonClicked()
|
|
{
|
|
this.style.display = DisplayStyle.None;
|
|
OnClosed?.Invoke();
|
|
}
|
|
#endregion
|
|
|
|
#region 공개 메서드 (Public Methods)
|
|
/// <summary>
|
|
/// 트리 리스트를 화면에 표시합니다.
|
|
/// </summary>
|
|
public void Show()
|
|
{
|
|
this.style.display = DisplayStyle.Flex;
|
|
_componentList?.Show();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 트리 데이터를 설정합니다.
|
|
/// </summary>
|
|
/// <param name="roots">루트 항목들의 리스트</param>
|
|
public void SetData(List<UTKComponentListItemDataBase> roots)
|
|
{
|
|
_componentList?.SetData(roots);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 지정된 ID의 항목을 프로그래밍 방식으로 선택합니다.
|
|
/// </summary>
|
|
/// <param name="itemId">선택할 항목의 ID</param>
|
|
public void SelectByItemId(int itemId)
|
|
{
|
|
_componentList?.SelectByItemId(itemId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 지정된 이름 목록에 해당하는 항목만 표시하고 나머지는 숨깁니다.
|
|
/// </summary>
|
|
/// <param name="items">표시할 항목들의 이름 목록</param>
|
|
/// <param name="depth">검색 깊이</param>
|
|
public void ShowItems(List<string> items, int depth = 1)
|
|
{
|
|
_componentList?.ShowItems(items, depth);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 루트 레벨에 새 항목을 추가합니다.
|
|
/// </summary>
|
|
/// <param name="data">추가할 항목 데이터</param>
|
|
public void AddItem(UTKComponentListItemDataBase data)
|
|
{
|
|
_componentList?.AddItem(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 지정된 부모 카테고리의 자식으로 새 항목을 추가합니다.
|
|
/// </summary>
|
|
/// <param name="parent">부모 카테고리</param>
|
|
/// <param name="data">추가할 항목 데이터</param>
|
|
public void AddItem(UTKComponentListCategoryData? parent, UTKComponentListItemDataBase data)
|
|
{
|
|
_componentList?.AddItem(parent, data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 트리에서 항목을 완전히 삭제합니다.
|
|
/// </summary>
|
|
/// <param name="data">삭제할 항목 데이터</param>
|
|
public void DeleteItem(UTKComponentListItemDataBase data)
|
|
{
|
|
_componentList?.DeleteItem(data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 항목의 이름을 변경하고 UI를 갱신합니다.
|
|
/// </summary>
|
|
/// <param name="data">변경할 항목 데이터</param>
|
|
/// <param name="newName">새 이름</param>
|
|
public void SetItemName(UTKComponentListItemDataBase data, string newName)
|
|
{
|
|
_componentList?.SetItemName(data, newName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 이름으로 항목을 찾아 선택합니다.
|
|
/// </summary>
|
|
/// <param name="name">선택할 항목의 이름</param>
|
|
/// <param name="notify">선택 이벤트 발송 여부</param>
|
|
public void SelectItem(string name, bool notify = true)
|
|
{
|
|
_componentList?.SelectItem(name, notify);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 이름으로 항목을 찾아 선택을 해제합니다.
|
|
/// </summary>
|
|
/// <param name="name">선택 해제할 항목의 이름</param>
|
|
/// <param name="notify">선택 해제 이벤트 발송 여부</param>
|
|
public void DeselectItem(string name, bool notify = true)
|
|
{
|
|
_componentList?.DeselectItem(name, notify);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 모든 항목의 선택을 해제합니다.
|
|
/// </summary>
|
|
public void ClearSelection()
|
|
{
|
|
_componentList?.ClearSelection();
|
|
}
|
|
#endregion
|
|
|
|
#region 테마 (Theme)
|
|
|
|
private void SubscribeToThemeChanges()
|
|
{
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
|
RegisterCallback<AttachToPanelEvent>(OnAttachToPanelForTheme);
|
|
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanelForTheme);
|
|
}
|
|
|
|
private void OnAttachToPanelForTheme(AttachToPanelEvent evt)
|
|
{
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
UTKThemeManager.Instance.OnThemeChanged += OnThemeChanged;
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
}
|
|
|
|
private void OnDetachFromPanelForTheme(DetachFromPanelEvent evt)
|
|
{
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
}
|
|
|
|
private void OnThemeChanged(UTKTheme theme)
|
|
{
|
|
UTKThemeManager.Instance.ApplyThemeToElement(this);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDisposable
|
|
/// <summary>
|
|
/// 리소스를 해제하고 이벤트 핸들러를 정리합니다.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
|
|
// 테마 변경 이벤트 해제
|
|
UTKThemeManager.Instance.OnThemeChanged -= OnThemeChanged;
|
|
UnregisterCallback<AttachToPanelEvent>(OnAttachToPanelForTheme);
|
|
UnregisterCallback<DetachFromPanelEvent>(OnDetachFromPanelForTheme);
|
|
|
|
// 내부 UTKComponentList 정리
|
|
_componentList?.Dispose();
|
|
_componentList = null;
|
|
|
|
// 닫기 버튼 이벤트 해제 및 정리
|
|
if (_closeButton != null)
|
|
{
|
|
_closeButton.OnClicked -= OnCloseButtonClicked;
|
|
_closeButton.Dispose();
|
|
}
|
|
|
|
// 외부 이벤트 구독자 정리
|
|
OnClosed = null;
|
|
|
|
// UI 참조 정리
|
|
_closeButton = null;
|
|
_titleLabel = null;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|