Files
EnglewoodLAB/Assets/Sample/UIToolkit/UTKComponentListWindowSample.cs
2026-03-10 11:35:30 +09:00

263 lines
9.4 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UVC.UIToolkit;
/// <summary>
/// UTKComponentListWindow와 UTKComponentTabListWindow의 기능을 테스트하기 위한 샘플 MonoBehaviour입니다.
/// 계층적 트리 데이터를 생성하고 다양한 이벤트 핸들러를 등록하여 동작을 확인합니다.
/// </summary>
public class UTKComponentListWindowSample : MonoBehaviour
{
[SerializeField]
public UIDocument uiDocument;
[SerializeField]
[Tooltip("시작 시 적용할 테마")]
private UTKTheme initialTheme = UTKTheme.Dark;
private UTKToggle _themeToggle;
private UTKComponentListWindow listWindow;
private UTKComponentTabListWindow tabListWindow;
void Start()
{
// UIDocument 참조 확인
var doc = GetComponent<UIDocument>();
if (doc == null)
{
Debug.LogError("UIDocument가 할당되지 않았습니다.");
return;
}
uiDocument = doc;
var toggle = uiDocument.rootVisualElement.Q<UTKToggle>("toggle");
if (toggle == null)
{
Debug.LogError("UXML에서 UTKToggle을 찾을 수 없습니다.");
return;
}
_themeToggle = toggle;
var window = uiDocument.rootVisualElement.Q<UTKComponentListWindow>("window");
if (window == null)
{
Debug.LogError("UXML에서 UTKComponentListWindow를 찾을 수 없습니다.");
return;
}
listWindow = window;
var tabWindow = uiDocument.rootVisualElement.Q<UTKComponentTabListWindow>("tabWindow");
if (tabWindow == null)
{
Debug.LogError("UXML에서 UTKComponentTabListWindow를 찾을 수 없습니다.");
return;
}
tabListWindow = tabWindow;
UTKThemeManager.Instance.RegisterRoot(uiDocument.rootVisualElement);
UTKThemeManager.Instance.SetTheme(initialTheme);
_themeToggle.OnValueChanged += (isOn) =>
{
UTKThemeManager.Instance.SetTheme(!isOn ? UTKTheme.Dark : UTKTheme.Light);
};
// 테스트용 계층적 트리 데이터 생성
CreateTabListTestData();
// 이벤트 핸들러 등록
RegisterTabListEventHandlers();
// 테스트용 계층적 트리 데이터 생성
CreateTestData();
// 이벤트 핸들러 등록
RegisterEventHandlers();
}
/// <summary>
/// 테스트용 계층적 트리 데이터를 생성합니다.
/// 5개의 카테고리를 생성하고, 각 카테고리에 20개의 항목을 추가합니다.
/// </summary>
private void CreateTestData()
{
var data = new List<UTKComponentListItemDataBase>();
for (int i = 1; i <= 5; i++)
{
string categoryName = $"Category {i}";
// 카테고리 데이터 생성 (UTKComponentListCategoryData 사용)
var categoryData = new UTKComponentListCategoryData { name = categoryName };
for (int j = 1; j <= 20; j++)
{
// 일반 항목 데이터 생성 (UTKComponentListItemData 사용)
var itemData = new UTKComponentListItemData
{
name = $"Item {i}-{j}",
option = $"Option {j}"
};
categoryData.Add(itemData);
}
data.Add(categoryData);
}
listWindow.SetData(data);
Debug.Log("[UTKComponentListWindowSample] 테스트 데이터 생성 완료: 5개 카테고리");
}
/// <summary>
/// UTKComponentListWindow의 이벤트 핸들러들을 등록합니다.
/// 선택, 선택 해제, 삭제, 더블클릭, 가시성 변경 이벤트를 처리합니다.
/// </summary>
private void RegisterEventHandlers()
{
// 항목 선택 이벤트 (다중 선택 지원)
// 사용자가 항목을 클릭하거나 SelectItem() 호출 시 발생
listWindow.OnItemSelected += (List<UTKComponentListItemDataBase> selectedItems) =>
{
foreach (var item in selectedItems)
{
Debug.Log($"[선택됨] {item.name}");
}
};
// 항목 선택 해제 이벤트 (다중 선택 지원)
// 사용자가 다른 항목을 선택하거나 DeselectItem() 호출 시 발생
listWindow.OnItemDeselected += (List<UTKComponentListItemDataBase> deselectedItems) =>
{
foreach (var item in deselectedItems)
{
Debug.Log($"[선택 해제됨] {item.name}");
}
};
// 항목 삭제 이벤트
// 사용자가 Delete 또는 Backspace 키를 누를 때 발생
// 실제 삭제는 이 핸들러에서 DeleteItem()을 호출하여 수행
listWindow.OnItemDeleted += (UTKComponentListItemDataBase deletedItem) =>
{
Debug.Log($"[삭제 요청] {deletedItem.name}");
// 실제로 항목을 삭제하려면 아래 주석을 해제하세요:
// treeListWindow.DeleteItem(deletedItem);
};
// 항목 더블클릭 이벤트
// 사용자가 항목을 더블클릭하거나 Enter 키를 누를 때 발생
listWindow.OnItemDoubleClicked += (UTKComponentListItemDataBase doubleClickedItem) =>
{
Debug.Log($"[더블클릭] {doubleClickedItem.name}");
};
// 항목 가시성 변경 이벤트
// 사용자가 눈 아이콘 버튼을 클릭하여 가시성을 토글할 때 발생
listWindow.OnItemVisibilityChanged += (UTKComponentListItemDataBase item, bool isVisible) =>
{
Debug.Log($"[가시성 변경] {item.name}, IsVisible: {isVisible}");
};
// 아이콘 클릭 이벤트
// 그룹 항목의 setting-btn 등 아이콘 버튼 클릭 시 발생
listWindow.OnItemIconClicked += (string iconName, UTKComponentListItemDataBase item) =>
{
Debug.Log($"[아이콘 클릭] {iconName}, Item: {item.name}");
};
Debug.Log("[UTKComponentListWindowSample] 이벤트 핸들러 등록 완료");
}
/// <summary>
/// UTKComponentTabListWindow용 테스트 데이터를 생성합니다.
/// 5개의 카테고리(탭)를 생성하고, 각 카테고리에 20개의 자식 항목을 추가합니다.
/// </summary>
private void CreateTabListTestData()
{
var data = new List<UTKComponentListItemDataBase>();
for (int i = 1; i <= 5; i++)
{
string categoryName = $"Category {i}";
// 카테고리 데이터 생성 (UTKComponentListCategoryData 사용)
var categoryData = new UTKComponentListCategoryData { name = categoryName };
for (int j = 1; j <= 20; j++)
{
// 일반 항목 데이터 생성 (UTKComponentListItemData 사용)
var itemData = new UTKComponentListItemData
{
name = $"Item {i}-{j}",
option = $"Option {j}"
};
categoryData.Add(itemData);
}
data.Add(categoryData);
}
tabListWindow.SetData(data);
Debug.Log("[UTKComponentListWindowSample] TabList 테스트 데이터 생성 완료: 5개 카테고리 (탭으로 표시됨)");
}
/// <summary>
/// UTKComponentTabListWindow의 이벤트 핸들러들을 등록합니다.
/// </summary>
private void RegisterTabListEventHandlers()
{
// 항목 선택 이벤트 (다중 선택 지원)
tabListWindow.OnItemSelected += (List<UTKComponentListItemDataBase> selectedItems) =>
{
foreach (var item in selectedItems)
{
Debug.Log($"[TabList 선택됨] {item.name}");
}
};
// 항목 선택 해제 이벤트 (다중 선택 지원)
tabListWindow.OnItemDeselected += (List<UTKComponentListItemDataBase> deselectedItems) =>
{
foreach (var item in deselectedItems)
{
Debug.Log($"[TabList 선택 해제됨] {item.name}");
}
};
// 항목 삭제 이벤트
tabListWindow.OnItemDeleted += (UTKComponentListItemDataBase deletedItem) =>
{
Debug.Log($"[TabList 삭제 요청] {deletedItem.name}");
};
// 항목 더블클릭 이벤트
tabListWindow.OnItemDoubleClicked += (UTKComponentListItemDataBase doubleClickedItem) =>
{
Debug.Log($"[TabList 더블클릭] {doubleClickedItem.name}");
};
// 항목 가시성 변경 이벤트
tabListWindow.OnItemVisibilityChanged += (UTKComponentListItemDataBase item, bool isVisible) =>
{
Debug.Log($"[TabList 가시성 변경] {item.name}, IsVisible: {isVisible}");
};
// 아이콘 클릭 이벤트
tabListWindow.OnItemIconClicked += (string iconName, UTKComponentListItemDataBase item) =>
{
Debug.Log($"[TabList 아이콘 클릭] {iconName}, Item: {item.name}");
};
Debug.Log("[UTKComponentListWindowSample] TabList 이벤트 핸들러 등록 완료");
}
/// <summary>
/// 컴포넌트가 파괴될 때 리소스를 정리합니다.
/// </summary>
private void OnDestroy()
{
// UTKComponentListWindow의 Dispose 호출로 이벤트 핸들러 정리
listWindow?.Dispose();
tabListWindow?.Dispose();
}
}