feat: refactor length calculation.
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
</Style>
|
</Style>
|
||||||
</UserControl.Styles>
|
</UserControl.Styles>
|
||||||
<StackPanel>
|
<StackPanel>
|
||||||
<u:RangeSlider Name="range" Height="24" TickFrequency="5" IsSnapToTick="False"/>
|
<u:RangeSlider Name="range" Height="24" TickFrequency="5" IsSnapToTick="True"/>
|
||||||
<u:NumericDoubleUpDown InnerLeftContent="Minimum" Value="{Binding #range.Minimum, Mode=TwoWay}" />
|
<u:NumericDoubleUpDown InnerLeftContent="Minimum" Value="{Binding #range.Minimum, Mode=TwoWay}" />
|
||||||
<u:NumericDoubleUpDown InnerLeftContent="Maximum" Value="{Binding #range.Maximum, Mode=TwoWay}" />
|
<u:NumericDoubleUpDown InnerLeftContent="Maximum" Value="{Binding #range.Maximum, Mode=TwoWay}" />
|
||||||
<u:NumericDoubleUpDown InnerLeftContent="LowerValue" Value="{Binding #range.LowerValue, Mode=TwoWay}" />
|
<u:NumericDoubleUpDown InnerLeftContent="LowerValue" Value="{Binding #range.LowerValue, Mode=TwoWay}" />
|
||||||
|
|||||||
@@ -239,9 +239,8 @@ public class RangeSlider: TemplatedControl
|
|||||||
private Thumb? GetThumbByPoint(PointerPoint point)
|
private Thumb? GetThumbByPoint(PointerPoint point)
|
||||||
{
|
{
|
||||||
var isHorizontal = Orientation == Orientation.Horizontal;
|
var isHorizontal = Orientation == Orientation.Horizontal;
|
||||||
var lowerThumbPosition = isHorizontal? _track?.LowerThumb?.Bounds.Position.X : _track?.LowerThumb?.Bounds.Position.Y;
|
var lowerThumbPosition = isHorizontal? _track?.LowerThumb?.Bounds.Center.X : _track?.LowerThumb?.Bounds.Center.Y;
|
||||||
var upperThumbPosition = isHorizontal? _track?.UpperThumb?.Bounds.Position.X : _track?.UpperThumb?.Bounds.Position.Y;
|
var upperThumbPosition = isHorizontal? _track?.UpperThumb?.Bounds.Center.X : _track?.UpperThumb?.Bounds.Center.Y;
|
||||||
var thumbWidth = isHorizontal? _track?.LowerThumb?.Bounds.Width : _track?.LowerThumb?.Bounds.Height;
|
|
||||||
var pointerPosition = isHorizontal? point.Position.X : point.Position.Y;
|
var pointerPosition = isHorizontal? point.Position.X : point.Position.Y;
|
||||||
|
|
||||||
var lowerDistance = Math.Abs((lowerThumbPosition ?? 0) - pointerPosition);
|
var lowerDistance = Math.Abs((lowerThumbPosition ?? 0) - pointerPosition);
|
||||||
@@ -261,21 +260,28 @@ public class RangeSlider: TemplatedControl
|
|||||||
{
|
{
|
||||||
if (_track is null) return 0;
|
if (_track is null) return 0;
|
||||||
var isHorizontal = Orientation == Orientation.Horizontal;
|
var isHorizontal = Orientation == Orientation.Horizontal;
|
||||||
var trackLength = _track.GetTrackLength();
|
|
||||||
var pointPosition = isHorizontal ? point.Position.X : point.Position.Y;
|
var pointPosition = isHorizontal ? point.Position.X : point.Position.Y;
|
||||||
|
var ratio = _track.GetRatioByPoint(pointPosition);
|
||||||
|
var range = Maximum - Minimum;
|
||||||
|
var finalValue = ratio * range + Minimum;
|
||||||
|
return finalValue;
|
||||||
|
/*
|
||||||
|
var trackLength = _track.GetTrackLength();
|
||||||
var thumbLength = _track.GetThumbLength() * 0.5;
|
var thumbLength = _track.GetThumbLength() * 0.5;
|
||||||
if(pointPosition < thumbLength)
|
if(pointPosition < thumbLength * 0.5)
|
||||||
return isHorizontal? Minimum : Maximum;
|
return isHorizontal? Minimum : Maximum;
|
||||||
if (pointPosition > trackLength - thumbLength)
|
if (pointPosition > trackLength - thumbLength * 0.5)
|
||||||
return isHorizontal? Maximum : Minimum;
|
return isHorizontal? Maximum : Minimum;
|
||||||
trackLength -= thumbLength * 2;
|
trackLength -= thumbLength * 2;
|
||||||
pointPosition = MathUtilities.Clamp(pointPosition / trackLength, 0.0, 1.0);
|
pointPosition = MathUtilities.Clamp(pointPosition / trackLength, 0.0, 1.0);
|
||||||
var invert = isHorizontal
|
var invert = isHorizontal
|
||||||
? IsDirectionReversed ? 1.0 : 0
|
? IsDirectionReversed ? 1.0 : 0
|
||||||
: IsDirectionReversed ? 0 : 1.0;
|
: IsDirectionReversed ? 0 : 1.0;
|
||||||
var calValue = Math.Abs(invert - pointPosition);
|
var calValue = Math.Abs(invert - pointPosition);
|
||||||
var range = Maximum - Minimum;
|
var range = Maximum - Minimum;
|
||||||
var finalValue = calValue * range + Minimum;
|
var finalValue = calValue * range + Minimum;
|
||||||
return finalValue;
|
return finalValue;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -495,4 +495,53 @@ public class RangeTrack: Control
|
|||||||
? Bounds.Width
|
? Bounds.Width
|
||||||
: Bounds.Height;
|
: Bounds.Height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal double GetRatioByPoint(double position)
|
||||||
|
{
|
||||||
|
bool isHorizontal = Orientation == Orientation.Horizontal;
|
||||||
|
var range = LowerSection?.Bounds.Width + InnerSection?.Bounds.Width + UpperSection?.Bounds.Width ?? double.Epsilon;
|
||||||
|
if (isHorizontal)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (IsDirectionReversed)
|
||||||
|
{
|
||||||
|
double trackStart = UpperThumb?.Bounds.Width/2 ?? 0;
|
||||||
|
double trackEnd = trackStart + range;
|
||||||
|
if (position < trackStart) return 1.0;
|
||||||
|
if (position > trackEnd) return 0.0;
|
||||||
|
double diff = trackEnd - position;
|
||||||
|
return diff / range;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
double trackStart = LowerThumb?.Bounds.Width/2 ?? 0;
|
||||||
|
double trackEnd = trackStart + range;
|
||||||
|
if (position < trackStart) return 0.0;
|
||||||
|
if (position > trackEnd) return 1.0;
|
||||||
|
double diff = position - trackStart;
|
||||||
|
return diff / range;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsDirectionReversed)
|
||||||
|
{
|
||||||
|
double trackStart = UpperThumb?.Bounds.Height/2 ?? 0;
|
||||||
|
double trackEnd = trackStart + range;
|
||||||
|
if (position < trackStart) return 1.0;
|
||||||
|
if (position > trackEnd) return 0.0;
|
||||||
|
double diff = trackEnd - position;
|
||||||
|
return diff / range;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
double trackStart = LowerThumb?.Bounds.Height/2 ?? 0;
|
||||||
|
double trackEnd = trackStart + range;
|
||||||
|
if (position < trackStart) return 0.0;
|
||||||
|
if (position > trackEnd) return 1.0;
|
||||||
|
double diff = position - trackStart;
|
||||||
|
return diff / range;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user