using UnityEngine; using UnityEngine.UI; using UVC.UI.Toolbar.Model; namespace UVC.UI.Toolbar.View { /// /// 확장 가능한 툴바 버튼(ToolbarExpandableButton)의 UI 생성, 이벤트 연결, 시각적 업데이트를 처리하는 클래스입니다. /// IButtonViewProcessor 인터페이스를 구현하여 ToolbarView가 확장 버튼을 일관된 방식으로 다룰 수 있도록 합니다. /// 확장 버튼은 클릭 시 하위 메뉴(SubButtons)를 표시하거나 숨기는 기능을 가집니다. /// /// /// 이 클래스의 주요 역할: /// 1. UI 생성: ToolbarView에 설정된 '확장 버튼 프리팹'을 사용하여 주 버튼의 GameObject를 만듭니다. /// 2. 상호작용 설정: 생성된 주 버튼 UI(UnityEngine.UI.Button)가 클릭되었을 때, /// - 연결된 ToolbarExpandableButton 모델의 ExecuteClick 메서드가 호출되도록 설정합니다. /// - ToolbarView의 ToggleSubMenu 메서드를 호출하여 하위 메뉴의 표시/숨김을 처리하도록 합니다. /// 3. 시각적 업데이트: 주 버튼 모델의 상태(텍스트, 아이콘, 활성화 상태 등)가 변경되면, /// 화면에 보이는 주 버튼의 모습도 그에 맞게 업데이트합니다. 하위 메뉴 자체의 렌더링은 ToolbarView가 담당합니다. /// /// /// /// // ToolbarView 내에서 이 프로세서가 사용되는 방식 (간략화된 예시): /// // 1. ToolbarModel로부터 ToolbarExpandableButton 객체를 가져옵니다. /// // ToolbarExpandableButton expandableModel = new ToolbarExpandableButton { Text = "Brush", ... }; /// // expandableModel.SubButtons.AddChild(new ToolbarStandardButton { Text = "Small Brush", ... }); /// /// // 2. ToolbarView는 해당 모델 타입에 맞는 Processor를 찾습니다. /// // IButtonViewProcessor processor = GetButtonViewProcessor(typeof(ToolbarExpandableButton)); /// /// // 3. UI 생성 요청 (주 버튼만 생성) /// // GameObject buttonUI = processor.CreateButtonUI(expandableModel, toolbarContainer, this); /// /// // 4. 상호작용 및 초기 시각적 요소 설정 요청 /// // processor.SetupButtonInteractions(expandableModel, buttonUI, this); /// // 이 과정에서 주 버튼 클릭 시 expandableModel.ExecuteClick()과 viewContext.ToggleSubMenu()가 연결됩니다. /// /// // 5. 주 버튼 모델의 상태가 변경되면(예: 하위 버튼 선택으로 아이콘 변경 시), /// // OnStateChanged 이벤트가 발생하고, ToolbarView는 processor.UpdateCommonButtonVisuals()를 호출합니다. /// /// public class ToolbarExpandableButtonViewProcessor : IButtonViewProcessor { /// /// ToolbarExpandableButton 모델에 해당하는 주 버튼 UI GameObject를 생성합니다. /// ToolbarView에 있는 expandableButtonPrefab을 복제하여 사용합니다. /// /// UI를 생성할 기반이 되는 버튼 데이터 모델 (ToolbarExpandableButton으로 간주됨). /// 생성된 버튼 UI가 자식으로 추가될 부모 Transform 객체입니다. /// 현재 ToolbarView의 인스턴스. 프리팹 참조 및 하위 메뉴 토글 기능에 접근할 때 사용됩니다. /// 성공적으로 생성된 주 버튼의 GameObject. 프리팹이 없으면 null을 반환합니다. public GameObject CreateButtonUI(ToolbarButtonBase buttonModel, Transform parentContainer, ToolbarView viewContext) { if (viewContext.expandableButtonPrefab == null) { Debug.LogError("ExpandableButtonViewProcessor: expandableButtonPrefab이 ToolbarView에 할당되지 않았습니다.", viewContext); return null; } return Object.Instantiate(viewContext.expandableButtonPrefab, parentContainer); } /// /// 생성된 확장 버튼(주 버튼) UI GameObject에 필요한 상호작용을 설정하고 초기 시각적 상태를 업데이트합니다. /// - 주 버튼 UI 클릭 시 모델의 ExecuteClick 메서드 호출 및 ToolbarView의 ToggleSubMenu 메서드 호출 설정. /// - 모델의 초기 텍스트, 아이콘, 활성화 상태를 주 버튼 UI에 반영. /// /// 설정 대상 버튼의 데이터 모델 (ToolbarExpandableButton으로 캐스팅하여 사용). /// 화면에 표시된, 설정할 주 버튼의 UI GameObject. /// 현재 ToolbarView의 인스턴스. public void SetupButtonInteractions(ToolbarButtonBase buttonModel, GameObject buttonUIObject, ToolbarView viewContext) { ToolbarExpandableButton expandableModel = buttonModel as ToolbarExpandableButton; if (expandableModel == null) return; Button uiButton = buttonUIObject.GetComponent