using System; using UnityEngine; namespace KernelPanic.UI { public enum UITextSize { Standard, Large } public enum UILayoutDensity { Compact, Comfortable } /// Presentation preferences are device-local and independent of progression saves. public static class UIPreferences { private const string StorageKey = "KernelPanic.UI.Preferences.v1"; private static bool _reducedMotion; private static UITextSize _textSize; private static UILayoutDensity _density; private static int _soundVolume = 75; public static int SoundVolume { get => _soundVolume; set { value = Mathf.Clamp(value, 0, 100); if (_soundVolume == value) return; _soundVolume = value; Changed?.Invoke(); } } public static event Action Changed; static UIPreferences() => Load(); public static bool ReducedMotion { get => _reducedMotion; set { if (_reducedMotion == value) return; _reducedMotion = value; Changed?.Invoke(); } } public static UITextSize TextSize { get => _textSize; set { value = value == UITextSize.Large ? UITextSize.Large : UITextSize.Standard; if (_textSize == value) return; _textSize = value; Changed?.Invoke(); } } public static UILayoutDensity Density { get => _density; set { value = value == UILayoutDensity.Comfortable ? UILayoutDensity.Comfortable : UILayoutDensity.Compact; if (_density == value) return; _density = value; Changed?.Invoke(); } } // Also reload when entering Play mode with domain reload disabled. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] public static void Load() => LoadFromKey(StorageKey); // Setters only affect the current presentation. User settings explicitly commit changes, // so combat fixtures and temporary presentation overrides never change saved preferences. public static void Save() => SaveToKey(StorageKey); internal static void LoadFromKey(string key) { StoredPreferences stored = null; if (PlayerPrefs.HasKey(key)) { try { stored = JsonUtility.FromJson(PlayerPrefs.GetString(key)); } catch (ArgumentException) { /* Invalid or incomplete preferences use safe defaults. */ } } if (stored == null || stored.version != 1) stored = new StoredPreferences(); bool motion = stored.reducedMotion; var size = stored.textSize == (int)UITextSize.Large ? UITextSize.Large : UITextSize.Standard; var density = stored.density == (int)UILayoutDensity.Comfortable ? UILayoutDensity.Comfortable : UILayoutDensity.Compact; int volume = Mathf.Clamp(stored.soundVolume, 0, 100); bool changed = motion != _reducedMotion || size != _textSize || density != _density || volume != _soundVolume; _soundVolume = volume; _reducedMotion = motion; _textSize = size; _density = density; if (changed) Changed?.Invoke(); } internal static void SaveToKey(string key) { PlayerPrefs.SetString(key, JsonUtility.ToJson(new StoredPreferences { reducedMotion = _reducedMotion, textSize = (int)_textSize, density = (int)_density, soundVolume = _soundVolume })); PlayerPrefs.Save(); } [Serializable] private sealed class StoredPreferences { public int version = 1; public bool reducedMotion; public int textSize; public int density; public int soundVolume = 75; } } }