toolbar 코드 개선
This commit is contained in:
221
Assets/Scripts/UVC/UI/ToolBar/Toolbox.cs
Normal file
221
Assets/Scripts/UVC/UI/ToolBar/Toolbox.cs
Normal file
@@ -0,0 +1,221 @@
|
||||
using UnityEngine;
|
||||
using UVC.Locale;
|
||||
using UVC.UI.Commands;
|
||||
using UVC.UI.Toolbar;
|
||||
using UVC.UI.Toolbar.Model;
|
||||
using UVC.UI.Toolbar.View;
|
||||
|
||||
namespace UVC.UI.ToolBar
|
||||
{
|
||||
/// <summary>
|
||||
/// 툴바의 생성, 설정 및 관리를 담당하는 MonoBehaviour 컨트롤러 클래스입니다.
|
||||
/// ToolbarModel(데이터)과 ToolbarView(UI 표현) 사이의 중재자 역할을 합니다.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 주요 역할:
|
||||
/// - 초기화(Awake, Start): 필요한 컴포넌트(특히 ToolbarView)를 찾거나 확인합니다.
|
||||
/// - 모델 생성 및 설정(Start): ToolbarModel 인스턴스를 만들고,
|
||||
/// AddStandardButton, AddRadioButton 등의 메서드를 사용해 툴바에 표시될 항목들을 정의하고 추가합니다.
|
||||
/// - 뷰 초기화(Start): 설정된 ToolbarModel을 ToolbarView에 전달하여 UI를 렌더링하도록 합니다.
|
||||
///
|
||||
/// 이 클래스의 인스턴스는 Unity 씬 내의 GameObject에 컴포넌트로 추가되어야 합니다.
|
||||
/// ToolbarView 컴포넌트도 동일한 GameObject 또는 그 자식에 존재해야 합니다.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// // Unity 에디터에서 GameObject를 생성하고 이 Toolbox 스크립트를 추가합니다.
|
||||
/// // 또한, ToolbarView 스크립트도 해당 GameObject 또는 자식 GameObject에 추가하고,
|
||||
/// // ToolbarView의 프리팹 필드들(standardButtonPrefab 등)을 Inspector에서 할당해야 합니다.
|
||||
///
|
||||
/// // 이 스크립트의 Start 메서드 내에서 툴바 항목들이 정의됩니다.
|
||||
/// // 예:
|
||||
/// // mainToolbar.AddStandardButton("파일 열기", "icons/open", new ActionCommand(OpenFile), "파일을 엽니다.");
|
||||
/// // private void OpenFile() { Debug.Log("파일 열기 기능 실행"); }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public class Toolbox : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 툴바의 데이터 모델입니다. 툴바 항목들의 정보와 구조를 담고 있습니다.
|
||||
/// protected로 선언되어 파생 클래스에서 접근 가능합니다.
|
||||
/// </summary>
|
||||
protected ToolbarModel mainToolbar;
|
||||
|
||||
/// <summary>
|
||||
/// 툴바의 UI 표현을 담당하는 뷰 컴포넌트입니다.
|
||||
/// protected로 선언되어 파생 클래스에서 접근 가능합니다.
|
||||
/// </summary>
|
||||
protected ToolbarView mainToolbarView;
|
||||
|
||||
/// <summary>
|
||||
/// MonoBehaviour의 Awake 메서드입니다.
|
||||
/// 주로 현재 GameObject 또는 자식 GameObject에서 ToolbarView 컴포넌트를 찾아 mainToolbarView 필드에 할당합니다.
|
||||
/// </summary>
|
||||
protected virtual void Awake()
|
||||
{
|
||||
// 1. 이 GameObject에 연결된 ToolbarView 컴포넌트를 찾습니다.
|
||||
mainToolbarView = GetComponent<ToolbarView>();
|
||||
|
||||
// 2. 만약 현재 GameObject에 없다면, 자식 GameObject들 중에서 ToolbarView 컴포넌트를 찾습니다.
|
||||
if (mainToolbarView == null)
|
||||
{
|
||||
mainToolbarView = GetComponentInChildren<ToolbarView>();
|
||||
}
|
||||
|
||||
if (mainToolbarView == null)
|
||||
{
|
||||
Debug.LogError("Toolbox: ToolbarView 컴포넌트를 찾을 수 없습니다. GameObject에 ToolbarView를 추가하고 연결해주세요.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MonoBehaviour의 Start 메서드입니다. 첫 번째 프레임 업데이트 전에 호출됩니다.
|
||||
/// ToolbarModel을 생성하고, 다양한 툴바 항목들을 모델에 추가한 후,
|
||||
/// 설정된 모델을 사용하여 ToolbarView를 초기화(UI 렌더링)합니다.
|
||||
/// </summary>
|
||||
protected virtual void Start()
|
||||
{
|
||||
// ToolbarModel 인스턴스 생성
|
||||
mainToolbar = new ToolbarModel();
|
||||
|
||||
// ToolbarView가 제대로 할당되었는지 확인
|
||||
if (mainToolbarView == null)
|
||||
{
|
||||
// Awake에서 이미 로그를 남겼을 수 있지만, 한 번 더 확인하여 Start 로직 중단
|
||||
Debug.LogError("Toolbox: ToolbarView가 할당되지 않아 툴바를 초기화할 수 없습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
// --- 툴바 모델 구성 시작 ---
|
||||
// 여기에 다양한 툴바 항목(버튼, 구분선 등)을 mainToolbar 모델에 추가합니다.
|
||||
|
||||
// 예시 1: 카메라 조절 라디오 버튼 그룹
|
||||
// "CameraControlGroup"이라는 이름으로 라디오 버튼 그룹을 만듭니다.
|
||||
// AddRadioButton의 세 번째 파라미터(initialState)는 해당 버튼이 초기에 선택될지 여부입니다.
|
||||
// 각 버튼은 아이콘 경로(선택 시/해제 시), OnToggle 콜백, ClickCommand, 툴팁 키를 가질 수 있습니다.
|
||||
mainToolbar.AddRadioButton("CameraControlGroup", "Top View", true,
|
||||
"Prefabs/UI/Toolbar/images/ic_camera_top_on",
|
||||
"Prefabs/UI/Toolbar/images/ic_camera_top_off_white",
|
||||
(isSelected) => { if (isSelected) Debug.Log("탑뷰 카메라 선택됨"); },
|
||||
new ActionCommand(() => Debug.Log("탑뷰 카메라 Command 실행")),
|
||||
"Top View 시점으로 변경합니다.");
|
||||
|
||||
mainToolbar.AddRadioButton("CameraControlGroup", "Quarter View", false,
|
||||
"Prefabs/UI/Toolbar/images/ic_camera_quarter_on",
|
||||
"Prefabs/UI/Toolbar/images/ic_camera_quarter_off_white",
|
||||
(isSelected) => { if (isSelected) Debug.Log("쿼터뷰 카메라 선택됨"); },
|
||||
new ActionCommand(() => Debug.Log("쿼터뷰 카메라 Command 실행")),
|
||||
"Quarter View 시점으로 변경합니다.");
|
||||
|
||||
mainToolbar.AddRadioButton("CameraControlGroup", "Front View", false,
|
||||
"Prefabs/UI/Toolbar/images/ic_camera_top_on",
|
||||
"Prefabs/UI/Toolbar/images/ic_camera_top_off_white",
|
||||
(isSelected) => { if (isSelected) Debug.Log("프런트뷰 카메라 선택됨"); },
|
||||
new ActionCommand(() => Debug.Log("프런트뷰 카메라 Command 실행")),
|
||||
"Front View 시점으로 변경합니다.");
|
||||
|
||||
// 예시 2: 구분선 추가
|
||||
mainToolbar.AddSeparator();
|
||||
|
||||
// 예시 3: 표준 버튼 (객체 선택)
|
||||
// AddStandardButton은 텍스트, 아이콘 경로, 클릭 커맨드, 툴팁 키를 파라미터로 받습니다.
|
||||
mainToolbar.AddStandardButton("선택", // 버튼 텍스트 (또는 다국어 키)
|
||||
"Prefabs/UI/Toolbar/images/ic_select_white", // 아이콘 경로
|
||||
new ActionCommand(() => Debug.Log("객체 선택 버튼 클릭됨")), // 클릭 시 실행될 커맨드
|
||||
"객체를 선택합니다."); // 툴팁
|
||||
|
||||
// 객체 이동
|
||||
mainToolbar.AddStandardButton("이동",
|
||||
"Prefabs/UI/Toolbar/images/ic_move_white",
|
||||
new ActionCommand(() => Debug.Log("객체 이동 버튼 클릭됨")),
|
||||
"객체를 이동 시킵니다.");
|
||||
|
||||
// 객체 회전
|
||||
mainToolbar.AddStandardButton("회전",
|
||||
"Prefabs/UI/Toolbar/images/ic_rotation_white",
|
||||
new ActionCommand(() => Debug.Log("객체 회전 버튼 클릭됨")),
|
||||
"객체의 각도를 조절합니다.");
|
||||
|
||||
// 객체 크기조절
|
||||
mainToolbar.AddStandardButton("크기조절",
|
||||
"Prefabs/UI/Toolbar/images/ic_scale_white",
|
||||
new ActionCommand(() => Debug.Log("객체 크기조절 버튼 클릭됨")),
|
||||
"객체 크기를 조절합니다.");
|
||||
|
||||
// 객체 복제
|
||||
mainToolbar.AddStandardButton("복제",
|
||||
"Prefabs/UI/Toolbar/images/ic_copy_white",
|
||||
new ActionCommand(() => Debug.Log("객체 복제 버튼 클릭됨")),
|
||||
"객체를 복제 합니다.");
|
||||
|
||||
// 객체 삭제
|
||||
mainToolbar.AddStandardButton("삭제",
|
||||
"Prefabs/UI/Toolbar/images/ic_delete_white",
|
||||
new ActionCommand(() => Debug.Log("객체 삭제 버튼 클릭됨")),
|
||||
"객체를 삭제 합니다.");
|
||||
|
||||
mainToolbar.AddSeparator();
|
||||
|
||||
// 예시 4: 화면 캡처 버튼 (텍스트가 다국어 키일 수 있음)
|
||||
mainToolbar.AddStandardButton("button_capture_screen", // 다국어 키로 사용될 수 있는 텍스트
|
||||
"Prefabs/UI/Toolbar/images/ic_capture_white",
|
||||
new ActionCommand(() => Debug.Log("화면 캡처 버튼 클릭됨")),
|
||||
"tooltip_capture_screen"); // 툴팁도 다국어 키 사용 가능
|
||||
|
||||
// 예시 5: 화면 녹화 시작/중지 토글 버튼
|
||||
// AddToggleButton은 초기 상태, 선택/해제 아이콘, OnToggle 콜백 등을 설정합니다.
|
||||
// ClickCommand는 ActionCommand<bool>을 사용하여 현재 토글 상태를 파라미터로 받을 수 있습니다.
|
||||
mainToolbar.AddToggleButton("button_record_screen", false, // 초기 상태: 꺼짐(false)
|
||||
"Prefabs/UI/Toolbar/images/ic_record_on_white", // 켜짐(selected) 상태 아이콘
|
||||
"Prefabs/UI/Toolbar/images/ic_record_off_white", // 꺼짐(deselected) 상태 아이콘
|
||||
(isSelected) => Debug.Log($"화면 녹화 상태: {(isSelected ? "녹화 중" : "중지")} (OnToggle 콜백)"),
|
||||
new ActionCommand<bool>((isRecording) => Debug.Log($"화면 녹화 Command 실행: {(isRecording ? "녹화 시작" : "녹화 중지")}")),
|
||||
"tooltip_record_screen");
|
||||
|
||||
|
||||
// 예시 6: 확장 버튼 (브러시 크기 선택)
|
||||
// AddExpandableButton으로 주 버튼을 만들고, 반환된 객체의 SubButtons 리스트에 하위 버튼들을 추가합니다.
|
||||
var expandableBtnModel = mainToolbar.AddExpandableButton("button_brush_size", // 주 버튼 텍스트/키
|
||||
"Prefabs/UI/Toolbar/images/ic_brush_default_white", // 주 버튼 기본 아이콘
|
||||
new ActionCommand(() => Debug.Log("브러시 크기 주 버튼 클릭됨 (Command)")), // 주 버튼 자체의 커맨드
|
||||
"붓 사이즈 선택 합니다."); // 주 버튼 툴팁
|
||||
|
||||
// 하위 버튼1: 작은 브러시 (ToolbarStandardButton 사용)
|
||||
var smallBrushCmd = new ActionCommand(() => Debug.Log($"작은 브러시 선택됨"));
|
||||
var smallBrush = new ToolbarStandardButton
|
||||
{
|
||||
Text = "brush_size_small", // 하위 버튼 텍스트/키
|
||||
IconSpritePath = "Prefabs/UI/Toolbar/images/ic_brush_small_white", // 하위 버튼 아이콘
|
||||
Tooltip = "tooltip_brush_small", // 하위 버튼 툴팁
|
||||
ClickCommand = smallBrushCmd
|
||||
};
|
||||
expandableBtnModel.SubButtons.Add(smallBrush); // 확장 버튼 모델에 하위 버튼 추가
|
||||
|
||||
// 하위 버튼2: 중간 브러시
|
||||
var mediumBrush = new ToolbarStandardButton
|
||||
{
|
||||
Text = "brush_size_medium",
|
||||
IconSpritePath = "Prefabs/UI/Toolbar/images/ic_brush_medium_white",
|
||||
Tooltip = "tooltip_brush_medium",
|
||||
ClickCommand = new ActionCommand(() => Debug.Log("중간 브러시 선택됨 (Sub-Command 실행)"))
|
||||
};
|
||||
expandableBtnModel.SubButtons.Add(mediumBrush);
|
||||
|
||||
// 확장 버튼의 하위 버튼이 선택되었을 때 호출될 콜백 설정
|
||||
expandableBtnModel.OnSubButtonSelected = (selectedSubButtonModel) =>
|
||||
{
|
||||
// LocalizationManager를 사용하여 텍스트를 현재 언어에 맞게 가져올 수 있습니다.
|
||||
string localizedSubButtonText = LocalizationManager.Instance != null ? LocalizationManager.Instance.GetString(selectedSubButtonModel.Text) : selectedSubButtonModel.Text;
|
||||
Debug.Log($"브러시 크기 '{localizedSubButtonText}' 선택됨 (OnSubButtonSelected 콜백). 주 버튼 업데이트 로직 실행 가능.");
|
||||
};
|
||||
// --- 툴바 모델 구성 끝 ---
|
||||
|
||||
// 설정이 완료된 ToolbarModel을 ToolbarView에 전달하여 UI 렌더링 시작
|
||||
mainToolbarView.Initialize(mainToolbar);
|
||||
|
||||
// 예시: 모델 상태를 코드로 변경하고 UI가 업데이트되는지 테스트 (주석 처리된 기존 코드)
|
||||
// StartCoroutine(TestModelChange(saveBtnModel, muteToggleModel));
|
||||
// 이 테스트는 특정 버튼 모델의 상태를 시간차를 두고 변경하여 UI가 반응하는지 확인하는 용도입니다.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user