Files
chameleon/Assets/Scripts/Components/UI/SliderDoubleClickReset.cs
T
adminandClaude Fable 5 14015d7134 Ignore drag-releases in slider double-click reset; tune HUD slider defaults
A pointer-up after a drag no longer counts as a click (dragging flag +
press-to-release distance within the existing tolerance), so drag ->
release -> click cannot trigger a reset anymore. PaintHud prefab:
pointer tolerance 4 px, HalfLambert default back to 0.5.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 22:37:03 +02:00

47 lines
2.0 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// Double-clicking a slider resets it to its default value. The default is the slider's authored
/// value, serialized when the component is added (runtime seeding from settings/persistence must not
/// overwrite it, so it is captured at edit time, not in Awake). The reset goes through slider.value,
/// so onValueChanged listeners (persistence, SliderCurve, value labels) fire as if the user dragged.
/// </summary>
[RequireComponent(typeof(Slider))]
public class SliderDoubleClickReset : MonoBehaviour, IPointerClickHandler
{
[SerializeField, Tooltip("Raw slider value restored on double-click. Seeded from the slider's authored value when the component is added.")]
private float _defaultValue;
[SerializeField, Tooltip("Max seconds between the two clicks.")]
private float _maxClickInterval = 0.25f;
[SerializeField, Tooltip("Max pointer movement (screen pixels) between the two clicks.")]
private float _maxPointerDistance = 8f;
private float _lastClickTime = float.NegativeInfinity;
private Vector2 _lastClickPosition;
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left) return;
if (eventData.dragging || (eventData.position - eventData.pressPosition).sqrMagnitude > _maxPointerDistance * _maxPointerDistance)
{
_lastClickTime = float.NegativeInfinity;
return;
}
bool isDouble = Time.unscaledTime - _lastClickTime <= _maxClickInterval
&& (eventData.position - _lastClickPosition).sqrMagnitude <= _maxPointerDistance * _maxPointerDistance;
_lastClickTime = isDouble ? float.NegativeInfinity : Time.unscaledTime;
_lastClickPosition = eventData.position;
if (isDouble) GetComponent<Slider>().value = _defaultValue;
}
private void Reset() => _defaultValue = GetComponent<Slider>().value;
}