using System;
using UnityEngine;
namespace Luneraria.UI
{
///
/// Player preferences of the HUD and options (port of scripts/autoload/preferences.gd as far as
/// the HUD uses it): UI size, reduced motion, volume, mute, full screen. Stored in PlayerPrefs.
/// Tests can set values without touching the stored ones (Persist = false).
///
public static class LunePreferences
{
public static event Action Changed;
/// False in tests: values change for the session only.
public static bool Persist = true;
static float _uiScale = -1f;
static int _reducedMotion = -1;
static float _volume = -1f;
static int _muted = -1;
static int _fullscreen = -1;
public static float UiScale
{
get
{
if (_uiScale < 0f)
_uiScale = PlayerPrefs.GetFloat("lune.ui_scale", 1f);
return _uiScale;
}
set { _uiScale = value; Save("lune.ui_scale", value); }
}
public static bool ReducedMotion
{
get
{
if (_reducedMotion < 0)
_reducedMotion = PlayerPrefs.GetInt("lune.reduced_motion", 0);
return _reducedMotion == 1;
}
set { _reducedMotion = value ? 1 : 0; Save("lune.reduced_motion", _reducedMotion); }
}
public static float Volume
{
get
{
if (_volume < 0f)
_volume = PlayerPrefs.GetFloat("lune.volume", .8f);
return _volume;
}
set { _volume = Mathf.Clamp01(value); Save("lune.volume", _volume); }
}
public static bool Muted
{
get
{
if (_muted < 0)
_muted = PlayerPrefs.GetInt("lune.muted", 0);
return _muted == 1;
}
set { _muted = value ? 1 : 0; Save("lune.muted", _muted); }
}
public static bool Fullscreen
{
get
{
if (_fullscreen < 0)
_fullscreen = PlayerPrefs.GetInt("lune.fullscreen", Screen.fullScreen ? 1 : 0);
return _fullscreen == 1;
}
set { _fullscreen = value ? 1 : 0; Save("lune.fullscreen", _fullscreen); }
}
static void Save(string key, float value)
{
if (Persist)
PlayerPrefs.SetFloat(key, value);
Changed?.Invoke();
}
static void Save(string key, int value)
{
if (Persist)
PlayerPrefs.SetInt(key, value);
Changed?.Invoke();
}
/// Writes pending values (Godot Preferences.commit).
public static void Commit()
{
if (Persist)
PlayerPrefs.Save();
}
/// Session values back to defaults (tests).
public static void ResetSession(float uiScale = 1f, bool reducedMotion = false)
{
Persist = false;
_uiScale = uiScale;
_reducedMotion = reducedMotion ? 1 : 0;
Changed?.Invoke();
}
}
}