ProjectDDD/Assets/_DDD/_Scripts/GameUi/New/Converters/CommonConverters.cs
2025-08-20 15:22:08 +09:00

223 lines
6.7 KiB
C#

using System.Linq;
using UnityEngine;
namespace DDD.MVVM
{
/// <summary>
/// 불린 값을 반전시키는 컨버터
/// 예: true → false, false → true
/// </summary>
public class InvertBoolConverter : IValueConverter
{
public object Convert(object value)
{
return value is bool boolValue ? !boolValue : false;
}
public object ConvertBack(object value)
{
return Convert(value); // 반전은 양방향 동일
}
}
/// <summary>
/// 아이템 개수를 텍스트로 변환하는 컨버터
/// 예: 5 → "아이템 수: 5"
/// </summary>
public class ItemCountConverter : IValueConverter
{
public object Convert(object value)
{
if (value is int count)
return $"아이템 수: {count}";
return "아이템 수: 0";
}
public object ConvertBack(object value)
{
if (value is string str && str.StartsWith("아이템 수: "))
{
var countStr = str.Substring("아이템 수: ".Length);
return int.TryParse(countStr, out var count) ? count : 0;
}
return 0;
}
}
/// <summary>
/// null 또는 빈 값을 불린으로 변환하는 컨버터
/// 예: null → false, "" → false, "text" → true
/// </summary>
public class IsNullOrEmptyConverter : IValueConverter
{
public object Convert(object value)
{
if (value == null) return true;
if (value is string str) return string.IsNullOrEmpty(str);
return false;
}
public object ConvertBack(object value)
{
return value is bool boolValue && !boolValue ? "" : null;
}
}
/// <summary>
/// 숫자를 백분율 텍스트로 변환하는 컨버터
/// 예: 0.75f → "75%"
/// </summary>
public class PercentageConverter : IValueConverter
{
public object Convert(object value)
{
if (value is float floatValue)
return $"{(floatValue * 100):F0}%";
if (value is double doubleValue)
return $"{(doubleValue * 100):F0}%";
return "0%";
}
public object ConvertBack(object value)
{
if (value is string str && str.EndsWith("%"))
{
var percentStr = str.Substring(0, str.Length - 1);
if (float.TryParse(percentStr, out var percent))
return percent / 100f;
}
return 0f;
}
}
/// <summary>
/// 열거형을 문자열로 변환하는 컨버터
/// </summary>
public class EnumToStringConverter : IValueConverter
{
public object Convert(object value)
{
return value?.ToString() ?? string.Empty;
}
public object ConvertBack(object value)
{
// 역변환은 타입 정보가 필요하므로 기본 구현만 제공
return value;
}
}
/// <summary>
/// 컬렉션의 개수를 확인하는 컨버터
/// 예: List<T>(5개) → true, List<T>(0개) → false
/// </summary>
public class CollectionHasItemsConverter : IValueConverter
{
public object Convert(object value)
{
if (value is System.Collections.ICollection collection)
return collection.Count > 0;
if (value is System.Collections.IEnumerable enumerable)
return enumerable.Cast<object>().Any();
return false;
}
public object ConvertBack(object value)
{
return value; // 역변환 불가
}
}
/// <summary>
/// 색상 투명도 조절 컨버터
/// 불린 값에 따라 알파값을 조절
/// </summary>
public class AlphaConverter : IValueConverter
{
public float EnabledAlpha { get; set; } = 1.0f;
public float DisabledAlpha { get; set; } = 0.5f;
public object Convert(object value)
{
if (value is bool boolValue)
return boolValue ? EnabledAlpha : DisabledAlpha;
return EnabledAlpha;
}
public object ConvertBack(object value)
{
if (value is float alpha)
return Mathf.Approximately(alpha, EnabledAlpha);
return true;
}
}
/// <summary>
/// InventoryCategoryType을 한국어로 변환하는 컨버터
/// </summary>
public class InventoryCategoryConverter : IValueConverter
{
public object Convert(object value)
{
if (value is InventoryCategoryType category)
{
return category switch
{
InventoryCategoryType.Food => "음식",
InventoryCategoryType.Drink => "음료",
InventoryCategoryType.Ingredient => "재료",
InventoryCategoryType.Cookware => "조리도구",
InventoryCategoryType.Special => "특수",
_ => "전체"
};
}
return "전체";
}
public object ConvertBack(object value)
{
if (value is string str)
{
return str switch
{
"음식" => InventoryCategoryType.Food,
"음료" => InventoryCategoryType.Drink,
"재료" => InventoryCategoryType.Ingredient,
"조리도구" => InventoryCategoryType.Cookware,
"특수" => InventoryCategoryType.Special,
_ => InventoryCategoryType.Food
};
}
return InventoryCategoryType.Food;
}
}
/// <summary>
/// 가격을 통화 형식으로 변환하는 컨버터
/// 예: 1000 → "1,000원"
/// </summary>
public class CurrencyConverter : IValueConverter
{
public string CurrencySymbol { get; set; } = "원";
public object Convert(object value)
{
if (value is int intValue)
return $"{intValue:N0}{CurrencySymbol}";
if (value is float floatValue)
return $"{floatValue:N0}{CurrencySymbol}";
return $"0{CurrencySymbol}";
}
public object ConvertBack(object value)
{
if (value is string str && str.EndsWith(CurrencySymbol))
{
var numberStr = str.Substring(0, str.Length - CurrencySymbol.Length).Replace(",", "");
if (int.TryParse(numberStr, out var number))
return number;
}
return 0;
}
}
}