226 lines
9.3 KiB
C#
226 lines
9.3 KiB
C#
#nullable enable
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using UVC.UIToolkit;
|
|
using UVC.UI.Commands;
|
|
using UVC.Log;
|
|
|
|
namespace UVC.Sample.UIToolkit
|
|
{
|
|
/// <summary>
|
|
/// UTKToolBar 독립 실행 샘플 코드입니다.
|
|
/// 가로/세로 배치 전환, 4가지 버튼 타입, 구분선을 데모합니다.
|
|
/// </summary>
|
|
public class UTKToolBarSample : MonoBehaviour
|
|
{
|
|
[SerializeField] private UIDocument? _uiDocument;
|
|
|
|
[SerializeField]
|
|
[Tooltip("시작 시 적용할 테마")]
|
|
private UTKTheme initialTheme = UTKTheme.Dark;
|
|
|
|
private UTKToggle? _themeToggle;
|
|
private VisualElement? _root;
|
|
private UTKToolBar? _horizontalToolBar;
|
|
private UTKToolBar? _verticalToolBar;
|
|
private UTKToolBarModel? _horizontalModel;
|
|
private UTKToolBarModel? _verticalModel;
|
|
private Label? _eventLabel;
|
|
private readonly List<string> _eventLogs = new();
|
|
private const int MaxLogLines = 8;
|
|
|
|
private void Start()
|
|
{
|
|
// UIDocument 참조 확인
|
|
var doc = GetComponent<UIDocument>();
|
|
if (doc == null)
|
|
{
|
|
Debug.LogError("UIDocument가 할당되지 않았습니다.");
|
|
return;
|
|
}
|
|
_uiDocument = doc;
|
|
_root = _uiDocument.rootVisualElement;
|
|
|
|
UTKThemeManager.Instance.RegisterRoot(_root);
|
|
UTKThemeManager.Instance.SetTheme(initialTheme);
|
|
|
|
// 테마 토글
|
|
_themeToggle = _root.Q<UTKToggle>("toggle");
|
|
if (_themeToggle != null)
|
|
{
|
|
_themeToggle.OnValueChanged += (isOn) =>
|
|
{
|
|
UTKThemeManager.Instance.SetTheme(!isOn ? UTKTheme.Dark : UTKTheme.Light);
|
|
};
|
|
}
|
|
|
|
// 툴바 영역
|
|
var toolbarArea = _root.Q<VisualElement>("toolbar-area");
|
|
if (toolbarArea == null)
|
|
{
|
|
toolbarArea = new VisualElement();
|
|
toolbarArea.style.flexDirection = FlexDirection.Column;
|
|
toolbarArea.style.paddingTop = 8;
|
|
toolbarArea.style.paddingLeft = 8;
|
|
toolbarArea.style.paddingRight = 8;
|
|
_root.Add(toolbarArea);
|
|
}
|
|
|
|
// 이벤트 라벨
|
|
_eventLabel = new Label("(버튼을 클릭하면 여기에 이벤트가 표시됩니다)");
|
|
_eventLabel.style.fontSize = 11;
|
|
_eventLabel.style.whiteSpace = WhiteSpace.Normal;
|
|
_eventLabel.style.marginTop = 12;
|
|
|
|
CreateHorizontalToolBar(toolbarArea);
|
|
CreateVerticalToolBar(toolbarArea);
|
|
|
|
toolbarArea.Add(_eventLabel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 가로 툴바를 생성합니다.
|
|
/// </summary>
|
|
private void CreateHorizontalToolBar(VisualElement parent)
|
|
{
|
|
var label = new Label("Horizontal Toolbar");
|
|
label.style.fontSize = 14;
|
|
label.style.marginTop = 8;
|
|
label.style.marginBottom = 4;
|
|
parent.Add(label);
|
|
|
|
_horizontalModel = new UTKToolBarModel();
|
|
|
|
// Standard
|
|
_horizontalModel.AddStandardButton("", UTKMaterialIcons.Save, new DebugLogCommand("저장"), "파일 저장");
|
|
_horizontalModel.AddStandardButton("", UTKMaterialIcons.Undo, new DebugLogCommand("실행 취소"));
|
|
_horizontalModel.AddStandardButton("", UTKMaterialIcons.Redo, new DebugLogCommand("다시 실행"));
|
|
|
|
_horizontalModel.AddSeparator();
|
|
|
|
// Toggle
|
|
_horizontalModel.AddToggleButton("", false, UTKMaterialIcons.GridOn, UTKMaterialIcons.GridOff, tooltip: "그리드 표시/숨김");
|
|
_horizontalModel.AddToggleButton("", false, UTKMaterialIcons.FilterCenterFocus, UTKMaterialIcons.CenterFocusWeak, tooltip: "스냅 활성화");
|
|
|
|
_horizontalModel.AddSeparator();
|
|
|
|
// Radio
|
|
_horizontalModel.AddRadioButton("tool", "", true, UTKMaterialIcons.NearMe, tooltip: "선택 도구");
|
|
_horizontalModel.AddRadioButton("tool", "", false, UTKMaterialIcons.OpenWith, tooltip: "이동 도구");
|
|
_horizontalModel.AddRadioButton("tool", "", false, UTKMaterialIcons.Refresh, tooltip: "회전 도구");
|
|
|
|
_horizontalModel.AddSeparator();
|
|
|
|
// Expandable
|
|
var shapeBtn = _horizontalModel.AddExpandableButton("", UTKMaterialIcons.Category, tooltip: "도형 추가", updateIconOnSelection: true);
|
|
shapeBtn.SubButtons.Add(new UTKToolBarStandardButtonData { Text = "", IconPath = UTKMaterialIcons.CropSquare, UseMaterialIcon = true, Tooltip = "사각형 추가" });
|
|
shapeBtn.SubButtons.Add(new UTKToolBarStandardButtonData { Text = "", IconPath = UTKMaterialIcons.Circle, UseMaterialIcon = true, Tooltip = "원형 추가" });
|
|
shapeBtn.SubButtons.Add(new UTKToolBarStandardButtonData { Text = "", IconPath = UTKMaterialIcons.ChangeHistory, UseMaterialIcon = true, Tooltip = "삼각형 추가" });
|
|
|
|
_horizontalToolBar = new UTKToolBar();
|
|
_horizontalToolBar.Orientation = UTKToolBarOrientation.Horizontal;
|
|
_horizontalToolBar.OnAction += OnToolBarAction;
|
|
_horizontalToolBar.BuildToolBar(_horizontalModel);
|
|
|
|
parent.Add(_horizontalToolBar);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 세로 툴바를 생성합니다.
|
|
/// </summary>
|
|
private void CreateVerticalToolBar(VisualElement parent)
|
|
{
|
|
var label = new Label("Vertical Toolbar");
|
|
label.style.fontSize = 14;
|
|
label.style.marginTop = 12;
|
|
label.style.marginBottom = 4;
|
|
parent.Add(label);
|
|
|
|
_verticalModel = new UTKToolBarModel();
|
|
|
|
_verticalModel.AddStandardButton("", UTKMaterialIcons.Save, new DebugLogCommand("세로: 저장"), "저장");
|
|
_verticalModel.AddStandardButton("", UTKMaterialIcons.Undo, new DebugLogCommand("세로: 실행 취소"), "실행 취소");
|
|
|
|
_verticalModel.AddSeparator();
|
|
|
|
_verticalModel.AddToggleButton("", false, UTKMaterialIcons.Visibility, UTKMaterialIcons.VisibilityOff, tooltip: "표시/숨김");
|
|
_verticalModel.AddToggleButton("", false, UTKMaterialIcons.Lock, UTKMaterialIcons.LockOpen, tooltip: "잠금/해제");
|
|
|
|
_verticalModel.AddSeparator();
|
|
|
|
_verticalModel.AddRadioButton("vtool", "", true, UTKMaterialIcons.NearMe, tooltip: "선택 도구");
|
|
_verticalModel.AddRadioButton("vtool", "", false, UTKMaterialIcons.OpenWith, tooltip: "이동 도구");
|
|
_verticalModel.AddRadioButton("vtool", "", false, UTKMaterialIcons.Refresh, tooltip: "회전 도구");
|
|
|
|
_verticalModel.AddSeparator();
|
|
|
|
// Expandable
|
|
var shapeBtn = _verticalModel.AddExpandableButton("", UTKMaterialIcons.Category, tooltip: "도형 추가", updateIconOnSelection: true);
|
|
shapeBtn.SubButtons.Add(new UTKToolBarStandardButtonData { Text = "사각형", IconPath = UTKMaterialIcons.CropSquare, UseMaterialIcon = true });
|
|
shapeBtn.SubButtons.Add(new UTKToolBarStandardButtonData { Text = "원형", IconPath = UTKMaterialIcons.Circle, UseMaterialIcon = true });
|
|
shapeBtn.SubButtons.Add(new UTKToolBarStandardButtonData { Text = "삼각형", IconPath = UTKMaterialIcons.ChangeHistory, UseMaterialIcon = true });
|
|
|
|
_verticalToolBar = new UTKToolBar();
|
|
_verticalToolBar.Orientation = UTKToolBarOrientation.Vertical;
|
|
_verticalToolBar.OnAction += OnToolBarAction;
|
|
_verticalToolBar.BuildToolBar(_verticalModel);
|
|
|
|
parent.Add(_verticalToolBar);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 툴바 액션 이벤트 핸들러
|
|
/// </summary>
|
|
private void OnToolBarAction(UTKToolBarActionEventArgs args)
|
|
{
|
|
string valueStr = args.Value != null ? args.Value.ToString() ?? "" : "null";
|
|
string logEntry = $"[{args.ActionType}] {args.Text}: {valueStr}";
|
|
ULog.Debug($"[UTKToolBar Sample] {logEntry}");
|
|
AppendLog(logEntry);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 이벤트 로그에 항목을 추가합니다.
|
|
/// </summary>
|
|
private void AppendLog(string message)
|
|
{
|
|
_eventLogs.Add(message);
|
|
while (_eventLogs.Count > MaxLogLines)
|
|
{
|
|
_eventLogs.RemoveAt(0);
|
|
}
|
|
if (_eventLabel != null)
|
|
{
|
|
_eventLabel.text = string.Join("\n", _eventLogs);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_horizontalToolBar != null)
|
|
{
|
|
_horizontalToolBar.OnAction -= OnToolBarAction;
|
|
_horizontalToolBar.Dispose();
|
|
_horizontalToolBar = null;
|
|
}
|
|
|
|
if (_verticalToolBar != null)
|
|
{
|
|
_verticalToolBar.OnAction -= OnToolBarAction;
|
|
_verticalToolBar.Dispose();
|
|
_verticalToolBar = null;
|
|
}
|
|
|
|
_horizontalModel?.Dispose();
|
|
_horizontalModel = null;
|
|
_verticalModel?.Dispose();
|
|
_verticalModel = null;
|
|
|
|
_eventLogs.Clear();
|
|
|
|
ULog.Debug("UTKToolBarSample 정리 완료");
|
|
}
|
|
}
|
|
}
|