using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System; namespace UIWidgets { /// /// Centered slider (zero at center, positive and negative parts have different scales). /// [AddComponentMenu("UI/UIWidgets/CenteredSlider")] public class CenteredSlider : CenteredSliderBase { /// /// Value to position. /// /// Position. /// Value. protected override float ValueToPosition(int value) { value = InBounds(value); var center = RangeSize() / 2f; if (value==0) { return center + GetStartPoint(); } if (value > 0) { var points_per_unit = center / limitMax; return (points_per_unit * value) + GetStartPoint() + center; } else { var points_per_unit = center / limitMin; return (points_per_unit * (limitMin - value)) + GetStartPoint(); } } /// /// Position to value. /// /// Value. /// Position. protected override int PositionToValue(float position) { var center = RangeSize() / 2f; if (position==center) { return 0; } float value = 0f; if (position > center) { var points_per_unit = (center) / limitMax; value = (position - center) / points_per_unit; } else { var points_per_unit = center / limitMin; value = - position / points_per_unit + LimitMin; } if (WholeNumberOfSteps) { return InBounds(Mathf.RoundToInt(value / step) * step); } else { return InBounds(Mathf.RoundToInt(value)); } } /// /// Position range for handle. /// /// The position limits. protected override Vector2 PositionLimits() { return new Vector2(ValueToPosition(LimitMin), ValueToPosition(LimitMax)); } /// /// Fit value to bounds. /// /// Value. /// Value. protected override int InBounds(int value) { var v = Mathf.Clamp(value, LimitMin, LimitMax); return (UseValueLimits) ? Mathf.Clamp(v, ValueMin, ValueMax) : v; } /// /// Increases the value. /// protected override void Increase() { Value += step; } /// /// Decreases the value. /// protected override void Decrease() { Value -= step; } /// /// Determines whether this instance is positive value. /// /// true if this instance is positive value; otherwise, false. protected override bool IsPositiveValue() { return Value > 0; } } }