Feature: Added a Range selection mode by aspect ratio value.

This commit is contained in:
望尘空忧
2024-11-13 19:14:12 +08:00
parent 60ba488219
commit 4bfaab3f3d
3 changed files with 134 additions and 7 deletions

View File

@@ -8,12 +8,18 @@
<Grid
RowDefinitions="Auto,*">
<StackPanel
Grid.Row="0"
Orientation="Horizontal">
<u:NumericDoubleUpDown InnerLeftContent="AspectRatioChangeAmbiguity" Value="{Binding #AspectRatioLayout.AspectRatioChangeAmbiguity}"></u:NumericDoubleUpDown>
Grid.Row="0">
<u:NumericDoubleUpDown InnerLeftContent="AspectRatioChangeAmbiguity"
Value="{Binding #AspectRatioLayout.AspectRatioChangeAmbiguity}">
</u:NumericDoubleUpDown>
<TextBlock Text="{Binding #AspectRatioLayout.AspectRatioValue,StringFormat='AspectRatioValue: {0}'}"></TextBlock>
</StackPanel>
<u:AspectRatioLayout Name="AspectRatioLayout" Grid.Row="1">
<u:AspectRatioLayout Name="AspectRatioLayout" Grid.Row="1"
BorderThickness="1"
BorderBrush="Red"
Margin="2"
CornerRadius="10">
<u:AspectRatioLayoutItem AcceptAspectRatioMode="HorizontalRectangle">
<Button>HorizontalRectangle ControlLayout</Button>
</u:AspectRatioLayoutItem>
@@ -23,6 +29,51 @@
<u:AspectRatioLayoutItem AcceptAspectRatioMode="Square">
<Button>Square ControlLayout</Button>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.2">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.4">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.6">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="2" EndAspectRatioValue="2.8">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
<u:AspectRatioLayoutItem StartAspectRatioValue="1.3" EndAspectRatioValue="1.5">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].StartAspectRatioValue,StringFormat='StartAspectRatioValue {0}'}"></Run>
<LineBreak></LineBreak>
<Run Text="{Binding $parent[u:AspectRatioLayoutItem].EndAspectRatioValue,StringFormat='EndAspectRatioValue {0}'}"></Run>
</TextBlock>
</u:AspectRatioLayoutItem>
</u:AspectRatioLayout>
</Grid>
</UserControl>

View File

@@ -45,6 +45,16 @@ public class AspectRatioLayout : TransitioningContentControl
set => SetValue(CurrentAspectRatioModeProperty, value);
}
public static readonly StyledProperty<double> AspectRatioValueProperty =
AvaloniaProperty.Register<AspectRatioLayout, double>(
nameof(AspectRatioValue));
public double AspectRatioValue
{
get => GetValue(AspectRatioValueProperty);
set => SetValue(AspectRatioValueProperty, value);
}
protected override Type StyleKeyOverride => typeof(TransitioningContentControl);
[Content]
@@ -73,9 +83,14 @@ public class AspectRatioLayout : TransitioningContentControl
return _history.All(x => x) || _history.All(x => !x);
}
private double GetAspectRatio(Rect rect)
{
return Math.Round(Math.Truncate(Math.Abs(rect.Width)) / Math.Truncate(Math.Abs(rect.Height)), 3);
}
private AspectRatioMode GetScaleMode(Rect rect)
{
var scale = Math.Round(Math.Truncate(Math.Abs(rect.Width)) / Math.Truncate(Math.Abs(rect.Height)), 3);
var scale = GetAspectRatio(rect);
var absA = Math.Abs(AspectRatioChangeAmbiguity);
var h = 1d + absA;
var v = 1d - absA;
@@ -96,12 +111,20 @@ public class AspectRatioLayout : TransitioningContentControl
{
var o = (Rect)change.OldValue!;
var n = (Rect)change.NewValue!;
UpdataHistory(GetScaleMode(o) == GetScaleMode(n));
UpdataHistory(GetAspectRatio(o) <= GetAspectRatio(n));
if (!IsRightChanges()) return;
CurrentAspectRatioMode = GetScaleMode(n);
}
var c = Items.FirstOrDefault(x => x.AcceptAspectRatioMode == GetScaleMode(Bounds));
AspectRatioValue = GetAspectRatio(Bounds);
var c =
Items
.Where(x => x.IsUseAspectRatioRange)
.FirstOrDefault(x =>
x.StartAspectRatioValue <= AspectRatioValue
&& AspectRatioValue <= x.EndAspectRatioValue);
c ??= Items.FirstOrDefault(x => x.AcceptAspectRatioMode == GetScaleMode(Bounds));
if (c == null)
{
if (Items.Count == 0) return;

View File

@@ -9,9 +9,62 @@ public class AspectRatioLayoutItem : ContentControl
AvaloniaProperty.Register<AspectRatioLayoutItem, AspectRatioMode>(
nameof(AcceptAspectRatioMode));
public static readonly StyledProperty<double> StartAspectRatioValueProperty =
AvaloniaProperty.Register<AspectRatioLayoutItem, double>(
nameof(StartAspectRatioValue), defaultValue: double.NaN);
public double StartAspectRatioValue
{
get => GetValue(StartAspectRatioValueProperty);
set => SetValue(StartAspectRatioValueProperty, value);
}
public static readonly StyledProperty<double> EndAspectRatioValueProperty =
AvaloniaProperty.Register<AspectRatioLayoutItem, double>(
nameof(EndAspectRatioValue), defaultValue: double.NaN);
public double EndAspectRatioValue
{
get => GetValue(EndAspectRatioValueProperty);
set => SetValue(EndAspectRatioValueProperty, value);
}
private bool _isUseAspectRatioRange;
public static readonly DirectProperty<AspectRatioLayoutItem, bool> IsUseAspectRatioRangeProperty =
AvaloniaProperty.RegisterDirect<AspectRatioLayoutItem, bool>(
nameof(IsUseAspectRatioRange), o => o.IsUseAspectRatioRange);
public bool IsUseAspectRatioRange
{
get => _isUseAspectRatioRange;
private set => SetAndRaise(IsUseAspectRatioRangeProperty, ref _isUseAspectRatioRange, value);
}
public AspectRatioMode AcceptAspectRatioMode
{
get => GetValue(AcceptScaleModeProperty);
set => SetValue(AcceptScaleModeProperty, value);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == StartAspectRatioValueProperty ||
change.Property == EndAspectRatioValueProperty)
{
UpdataIsUseAspectRatioRange();
}
}
private void UpdataIsUseAspectRatioRange()
{
if (double.IsNaN(StartAspectRatioValue)
|| double.IsNaN(EndAspectRatioValue)
|| StartAspectRatioValue > EndAspectRatioValue)
IsUseAspectRatioRange = false;
else
IsUseAspectRatioRange = true;
}
}