104 lines
3.6 KiB
C#
104 lines
3.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace KernelPanic.UI
|
|
{
|
|
public enum UITextSize { Standard, Large }
|
|
public enum UILayoutDensity { Compact, Comfortable }
|
|
|
|
/// <summary>Presentation preferences are device-local and independent of progression saves.</summary>
|
|
public static class UIPreferences
|
|
{
|
|
private const string StorageKey = "KernelPanic.UI.Preferences.v1";
|
|
private static bool _reducedMotion;
|
|
private static UITextSize _textSize;
|
|
private static UILayoutDensity _density;
|
|
|
|
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<StoredPreferences>(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;
|
|
bool changed = motion != _reducedMotion || size != _textSize || density != _density;
|
|
_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
|
|
}));
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
[Serializable]
|
|
private sealed class StoredPreferences
|
|
{
|
|
public int version = 1;
|
|
public bool reducedMotion;
|
|
public int textSize;
|
|
public int density;
|
|
}
|
|
}
|
|
}
|