// Copyright (C) 2023 Nicholas Maltbie
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using nickmaltbie.OpenKCC.Character;
using nickmaltbie.ScreenManager.Actions;
using UnityEngine;
using UnityEngine.UI;
namespace nickmaltbie.OpenKCC.UI.Actions
{
///
/// Actions to adjust mouse Sensitivity via sliders
///
public class ChangeMouseSensitivity : MonoBehaviour, IBindingControl
{
///
/// String key for saving mouse sensitivity to player preferences.
///
public const string mouseSensitivityPlayerPref = "MouseSensitivity";
///
/// Power value for scaling mouse sensitivity between minimum and maximum
///
public const float powerValue = 0.75f;
///
/// Slider to control mouse sensitivity.
///
[SerializeField]
[Tooltip("Slider to control mouse sensitivity.")]
internal Slider slider;
///
/// Get the value on a slider from a given mouse sensitivity level
///
/// Currently selected mouse sensitivity
/// The float position of a slider based on the mouse sensitivity
public static float GetSliderValue(float sensitivity)
{
if (sensitivity < PlayerInputUtils.minimumMouseSensitivity)
{
return 0.0f;
}
if (sensitivity > PlayerInputUtils.maximumMouseSensitivity)
{
return 1.0f;
}
return Mathf.Pow((PlayerInputUtils.mouseSensitivity - PlayerInputUtils.minimumMouseSensitivity) /
(PlayerInputUtils.maximumMouseSensitivity - PlayerInputUtils.minimumMouseSensitivity), powerValue);
}
///
/// Gets the mouse sensitivity from a given slider position
///
/// Slider position between 0 and 1
/// The mouse sensitivity between min sensitivity and max sensitivity
public static float GetMouseSensitivity(float sliderPosition)
{
return Mathf.Pow(sliderPosition, 1 / powerValue) *
(PlayerInputUtils.maximumMouseSensitivity - PlayerInputUtils.minimumMouseSensitivity) + PlayerInputUtils.minimumMouseSensitivity;
}
///
/// Load saved values during startup
///
public void Awake()
{
PlayerInputUtils.mouseSensitivity = PlayerPrefs.GetFloat(
mouseSensitivityPlayerPref, PlayerInputUtils.DefaultMouseSensitivity);
slider.SetValueWithoutNotify(GetSliderValue(PlayerInputUtils.mouseSensitivity));
// Update saved and current value on player input
slider.onValueChanged.AddListener(value =>
{
PlayerInputUtils.mouseSensitivity = GetMouseSensitivity(value);
PlayerPrefs.SetFloat(mouseSensitivityPlayerPref, PlayerInputUtils.mouseSensitivity);
});
}
///
public void ResetBinding()
{
PlayerPrefs.DeleteKey(mouseSensitivityPlayerPref);
PlayerInputUtils.mouseSensitivity = PlayerInputUtils.DefaultMouseSensitivity;
}
///
public void UpdateDisplay()
{
slider.SetValueWithoutNotify(GetSliderValue(PlayerInputUtils.mouseSensitivity));
}
}
}