using System.Linq; using UnityEngine; namespace DDD.MVVM { /// /// 불린 값을 반전시키는 컨버터 /// 예: true → false, false → true /// public class InvertBoolConverter : IValueConverter { public object Convert(object value) { return value is bool boolValue ? !boolValue : false; } public object ConvertBack(object value) { return Convert(value); // 반전은 양방향 동일 } } /// /// 아이템 개수를 텍스트로 변환하는 컨버터 /// 예: 5 → "아이템 수: 5" /// 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; } } /// /// null 또는 빈 값을 불린으로 변환하는 컨버터 /// 예: null → false, "" → false, "text" → true /// 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; } } /// /// 숫자를 백분율 텍스트로 변환하는 컨버터 /// 예: 0.75f → "75%" /// 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; } } /// /// 열거형을 문자열로 변환하는 컨버터 /// public class EnumToStringConverter : IValueConverter { public object Convert(object value) { return value?.ToString() ?? string.Empty; } public object ConvertBack(object value) { // 역변환은 타입 정보가 필요하므로 기본 구현만 제공 return value; } } /// /// 컬렉션의 개수를 확인하는 컨버터 /// 예: List(5개) → true, List(0개) → false /// 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().Any(); return false; } public object ConvertBack(object value) { return value; // 역변환 불가 } } /// /// 색상 투명도 조절 컨버터 /// 불린 값에 따라 알파값을 조절 /// 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; } } /// /// InventoryCategoryType을 한국어로 변환하는 컨버터 /// 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; } } /// /// 가격을 통화 형식으로 변환하는 컨버터 /// 예: 1000 → "1,000원" /// 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; } } }