toolbar 개발 중
This commit is contained in:
104
Assets/Scripts/UVC/UI/Commands/ActionCommand.cs
Normal file
104
Assets/Scripts/UVC/UI/Commands/ActionCommand.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UVC.UI.Commands
|
||||
{
|
||||
public class ActionCommand : ICommand
|
||||
{
|
||||
private readonly Action _action;
|
||||
private readonly Action<object> _actionWithParam;
|
||||
|
||||
public ActionCommand(Action action)
|
||||
{
|
||||
_action = action ?? throw new ArgumentNullException(nameof(action));
|
||||
}
|
||||
|
||||
public ActionCommand(Action<object> actionWithParam)
|
||||
{
|
||||
_actionWithParam = actionWithParam ?? throw new ArgumentNullException(nameof(actionWithParam));
|
||||
}
|
||||
|
||||
public void Execute(object parameter = null)
|
||||
{
|
||||
_action?.Invoke();
|
||||
_actionWithParam?.Invoke(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
// 제네릭 ActionCommand<T>는 이미 파라미터를 생성자에서 받으므로,
|
||||
// ICommand.Execute(object parameter)를 구현할 때 해당 파라미터를 사용하지 않거나,
|
||||
// 혹은 Execute(object) 호출 시 전달된 파라미터로 내부 _parameter를 덮어쓰는 등의 정책을 정해야 합니다.
|
||||
// 또는, ICommand<T> 인터페이스를 고려할 수도 있습니다 (아래 2번 방법).
|
||||
public class ActionCommand<T> : ICommand<T> // ICommand<T>를 구현
|
||||
{
|
||||
private readonly Action<T> _action;
|
||||
private readonly T _defaultParameter;
|
||||
private bool _useDefaultParameterForParameterlessExecute;
|
||||
|
||||
public ActionCommand(Action<T> action)
|
||||
{
|
||||
_action = action ?? throw new ArgumentNullException(nameof(action));
|
||||
_useDefaultParameterForParameterlessExecute = true; // 기본적으로 default(T) 사용
|
||||
_defaultParameter = default(T);
|
||||
}
|
||||
|
||||
public ActionCommand(Action<T> action, T defaultParameter, bool useDefaultForParameterless = true)
|
||||
{
|
||||
_action = action ?? throw new ArgumentNullException(nameof(action));
|
||||
_defaultParameter = defaultParameter;
|
||||
_useDefaultParameterForParameterlessExecute = useDefaultForParameterless;
|
||||
}
|
||||
|
||||
// ICommand<T>의 Execute(T parameter) 구현
|
||||
public void Execute(T parameter)
|
||||
{
|
||||
_action.Invoke(parameter);
|
||||
}
|
||||
|
||||
// ICommand<T> 인터페이스에 의해 추가된 파라미터 없는 Execute()
|
||||
// 기본 구현은 Execute(default(T))를 호출합니다.
|
||||
// 이 메서드는 ICommand<T>의 기본 인터페이스 메서드에 의해 제공되므로,
|
||||
// 여기서 명시적으로 재정의할 필요는 없습니다. (void ICommand<T>.Execute() => Execute(default(T));)
|
||||
// 만약 다른 동작을 원한다면 여기서 재정의할 수 있습니다.
|
||||
// public new void Execute() // 'new'는 인터페이스의 기본 구현을 숨기기 위함이 아님.
|
||||
// {
|
||||
// if (_useDefaultParameterForParameterlessExecute)
|
||||
// {
|
||||
// _action.Invoke(_defaultParameter);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // 또는 예외를 발생시키거나, 로깅 후 아무것도 하지 않음
|
||||
// Debug.LogWarning($"ActionCommand<{typeof(T).Name}>의 파라미터 없는 Execute()가 호출되었으나, 기본 파라미터 사용이 설정되지 않았습니다.");
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// ICommand의 Execute(object parameter = null) 구현
|
||||
void ICommand.Execute(object parameter) // 명시적 인터페이스 구현
|
||||
{
|
||||
if (parameter is T typedParameter)
|
||||
{
|
||||
Execute(typedParameter);
|
||||
}
|
||||
else if (parameter == null)
|
||||
{
|
||||
// 파라미터가 null로 전달된 경우, T의 기본값 또는 생성자에서 설정된 기본값을 사용할지 결정
|
||||
if (_useDefaultParameterForParameterlessExecute)
|
||||
{
|
||||
Execute(_defaultParameter);
|
||||
}
|
||||
else
|
||||
{
|
||||
// T가 참조 타입이면 default(T)는 null. 값 타입이면 0, false 등.
|
||||
Execute(default(T));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"ActionCommand<{typeof(T).Name}>.Execute(object): 잘못된 파라미터 타입입니다. 기대: {typeof(T).Name}, 실제: {parameter.GetType().Name}");
|
||||
// 예외를 발생시킬 수도 있습니다: throw new ArgumentException("잘못된 파라미터 타입입니다.", nameof(parameter));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UVC/UI/Commands/ActionCommand.cs.meta
Normal file
2
Assets/Scripts/UVC/UI/Commands/ActionCommand.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f662cce09b4b4d48999758210c0ad4a
|
||||
@@ -1,4 +1,5 @@
|
||||
using UVC.Locale;
|
||||
using UnityEngine;
|
||||
using UVC.Locale;
|
||||
using UVC.Log;
|
||||
|
||||
namespace UVC.UI.Commands
|
||||
@@ -14,17 +15,33 @@ namespace UVC.UI.Commands
|
||||
_languageCode = languageCode;
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
public void Execute(object parameter = null)
|
||||
{
|
||||
LocalizationManager _localizationManager = LocalizationManager.Instance;
|
||||
if (_localizationManager != null)
|
||||
string targetLanguage = _languageCode;
|
||||
|
||||
// Execute 호출 시 전달된 파라미터가 있다면 그것을 우선 사용
|
||||
if (parameter is string langCodeFromParam && !string.IsNullOrEmpty(langCodeFromParam))
|
||||
{
|
||||
_localizationManager.SetCurrentLanguage(_languageCode);
|
||||
ULog.Debug($"언어가 {_languageCode}(으)로 변경되었습니다. (Command)");
|
||||
targetLanguage = langCodeFromParam;
|
||||
Debug.Log($"ChangeLanguageCommand: 파라미터로 언어 코드 '{targetLanguage}' 사용.");
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_languageCode))
|
||||
{
|
||||
Debug.Log($"ChangeLanguageCommand: 생성자에서 설정된 언어 코드 '{targetLanguage}' 사용.");
|
||||
}
|
||||
else
|
||||
{
|
||||
ULog.Error("LocalizationManager가 ChangeLanguageCommand에 전달되지 않았습니다.");
|
||||
Debug.LogError("ChangeLanguageCommand: 변경할 언어 코드가 지정되지 않았습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (LocalizationManager.Instance != null)
|
||||
{
|
||||
LocalizationManager.Instance.SetCurrentLanguage(targetLanguage);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("LocalizationManager 인스턴스를 찾을 수 없습니다.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,9 +12,14 @@ namespace UVC.UI.Commands
|
||||
_message = message;
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
public void Execute(object parameter = null)
|
||||
{
|
||||
ULog.Debug(_message);
|
||||
string finalMessage = _message;
|
||||
if (parameter != null)
|
||||
{
|
||||
finalMessage += $", parameter: {parameter}";
|
||||
}
|
||||
ULog.Debug(finalMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,13 @@
|
||||
{
|
||||
public interface ICommand
|
||||
{
|
||||
void Execute();
|
||||
void Execute(object parameter = null);
|
||||
}
|
||||
|
||||
public interface ICommand<T> : ICommand
|
||||
{
|
||||
void Execute(T parameter);
|
||||
|
||||
void Execute() => Execute(default(T)); // 기본 Execute 구현 제공 가능
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace UVC.UI.Commands.Mono
|
||||
{
|
||||
public class ChangeLanguageCommandMono : MonoBehaviourCommand
|
||||
{
|
||||
public override void Execute()
|
||||
public override void Execute(object parameter = null)
|
||||
{
|
||||
//언어가 2개 인경우 switch 시킴
|
||||
LocalizationManager _localizationManager = LocalizationManager.Instance;
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
|
||||
namespace UVC.UI.Commands.Mono
|
||||
{
|
||||
/// <summary>
|
||||
/// Unity Inspector에서 버튼에 직접 할당할 수 있는 MonoBehaviour 기반의 명령 클래스입니다.
|
||||
/// </summary>
|
||||
public class MonoBehaviourCommand : MonoBehaviour, ICommand
|
||||
{
|
||||
// MonoCommand는 MonoBehaviour를 상속받아 Unity의 생명주기를 활용할 수 있습니다.
|
||||
// ICommand 인터페이스를 구현하여 명령 패턴을 따릅니다.
|
||||
public virtual void Execute()
|
||||
public virtual void Execute(object parameter = null)
|
||||
{
|
||||
// 기본 실행 로직 (필요시 override 가능)
|
||||
Debug.Log("MonoCommand executed.");
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace UVC.UI.Commands.Mono
|
||||
{
|
||||
public class QuitApplicationCommandMono : MonoBehaviourCommand
|
||||
{
|
||||
public override void Execute()
|
||||
public override void Execute(object parameter = null)
|
||||
{
|
||||
new QuitApplicationCommand().Execute();
|
||||
}
|
||||
|
||||
@@ -1,17 +1,26 @@
|
||||
using UnityEngine;
|
||||
using UVC.Log;
|
||||
|
||||
namespace UVC.UI.Commands
|
||||
{
|
||||
// 애플리케이션 종료 커맨드
|
||||
public class QuitApplicationCommand : ICommand
|
||||
{
|
||||
public void Execute()
|
||||
public void Execute(object parameter = null)
|
||||
{
|
||||
ULog.Debug("애플리케이션을 종료합니다.");
|
||||
Application.Quit();
|
||||
// 파라미터는 여기서는 사용되지 않을 수 있음
|
||||
if (parameter != null)
|
||||
{
|
||||
Debug.Log($"QuitApplicationCommand 실행됨 (파라미터 무시됨: {parameter})");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("QuitApplicationCommand 실행됨");
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false; // 에디터에서 실행 중일 경우 플레이 모드 종료
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#else
|
||||
Application.Quit();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user