Files
GSS2Rework/GSS2.UI.Core/SectionPanel.cs
T
amkovkov 40aa38d353 feat: SectionPanel
Add scroll flugs
Add carusel scroll
Add animation
2026-02-10 15:03:23 +03:00

217 lines
8.1 KiB
C#

using Avalonia;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.Threading;
using GSS2.Core.Extentions;
namespace GSS2.UI.Core;
public class SectionPanel : Panel
{
public static readonly StyledProperty<int> CurrentIndexProperty = AvaloniaProperty.Register<SectionPanel, int>(
name: nameof(CurrentIndex),
defaultValue: 0,
coerce: (obj, value) =>
{
if (obj is not SectionPanel sectionPanel)
return value;
if (sectionPanel.Cyclic)
return value;
return Math.Clamp(value, 0, sectionPanel.Children.Count() - 1);
});
public int CurrentIndex
{
get => GetValue(CurrentIndexProperty);
set => SetValue(CurrentIndexProperty, value);
}
public static readonly StyledProperty<bool> EnableAnimationProperty = AvaloniaProperty.Register<SectionPanel, bool>(nameof(EnableAnimation), true);
public bool EnableAnimation
{
get => GetValue(EnableAnimationProperty);
set => SetValue(EnableAnimationProperty, value);
}
public static readonly StyledProperty<double> AnimationSpeedProperty = AvaloniaProperty.Register<SectionPanel, double>(nameof(AnimationSpeed), 500);
public double AnimationSpeed
{
get => GetValue(AnimationSpeedProperty);
set => SetValue(AnimationSpeedProperty, value);
}
public static readonly StyledProperty<Orientation> OrientationProperty = AvaloniaProperty.Register<SectionPanel, Orientation>(nameof(Orientation), Orientation.Vertical);
public Orientation Orientation
{
get => GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
public static readonly StyledProperty<bool> CyclicProperty = AvaloniaProperty.Register<SectionPanel, bool>(nameof(Cyclic), false);
public bool Cyclic
{
get => GetValue(CyclicProperty);
set => SetValue(CyclicProperty, value);
}
public static readonly StyledProperty<bool> CanScrollForwardProperty = AvaloniaProperty.Register<SectionPanel, bool>(nameof(CanScrollForward), false, defaultBindingMode: Avalonia.Data.BindingMode.OneWayToSource);
public bool CanScrollForward
{
get => GetValue(CanScrollForwardProperty);
set => SetValue(CanScrollForwardProperty, value);
}
public static readonly StyledProperty<bool> CanScrollBackwardProperty = AvaloniaProperty.Register<SectionPanel, bool>(nameof(CanScrollBackward), false, defaultBindingMode: Avalonia.Data.BindingMode.OneWayToSource);
public bool CanScrollBackward
{
get => GetValue(CanScrollBackwardProperty);
set => SetValue(CanScrollBackwardProperty, value);
}
private readonly Dictionary<Control, int> _childIndeces = new Dictionary<Control, int>();
private bool _initialized = false;
private int _previousIndex = 0;
public SectionPanel()
: base()
{
CurrentIndexProperty.Changed.AddClassHandler<SectionPanel>((panel, ea) =>
{
CanScrollForward = Cyclic || CurrentIndex != Children.Count() - 1;
CanScrollBackward = Cyclic || CurrentIndex != 0;
panel.InvalidateArrange();
});
Children.CollectionChanged += (_, ea) =>
{
CanScrollForward = Cyclic || CurrentIndex != Children.Count() - 1;
CanScrollBackward = Cyclic || CurrentIndex != 0;
};
}
protected override Size MeasureOverride(Size availableSize)
{
foreach (var child in Children)
child.Measure(availableSize);
return availableSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
if (!_initialized)
{
foreach (var (i, child) in Children.Enumerate())
{
var rect = Orientation switch
{
Orientation.Vertical => new Rect(0, i * finalSize.Height, finalSize.Width, finalSize.Height),
Orientation.Horizontal => new Rect(i * finalSize.Width, 0, finalSize.Width, finalSize.Height),
_ => default
};
_childIndeces.Add(child, i);
child.Arrange(rect);
}
_initialized = true;
}
else if (Children.Count() > 1)
Dispatcher.UIThread.Post(async () => _ = ApplyOffset(finalSize, true), DispatcherPriority.Render);
return finalSize;
}
private async Task ApplyOffset(Size size, bool animate)
{
int indexShift = CurrentIndex - _previousIndex;
while (indexShift != 0)
{
int indexSingleShift = indexShift / Math.Abs(indexShift);
var firstIndex = _childIndeces.Min(ch => ch.Value);
var lastIndex = _childIndeces.Max(ch => ch.Value);
if (indexSingleShift == -1 &&
_previousIndex - 1 < firstIndex)
{
var newIndex = firstIndex - 1;
var (child, _) = _childIndeces.MaxBy(ch => ch.Value);
InstantTransform(child, -Children.Count(), size);
_childIndeces[child] = newIndex;
}
if (indexSingleShift == 1 &&
_previousIndex + 1 > lastIndex)
{
var newIndex = lastIndex + 1;
var (child, _) = _childIndeces.MinBy(ch => ch.Value);
InstantTransform(child, Children.Count(), size);
_childIndeces[child] = newIndex;
}
List<Task> animations = new List<Task>();
foreach (var (i, child) in Children.Enumerate())
{
double offset = Orientation switch
{
Orientation.Vertical => -indexSingleShift * size.Height,
Orientation.Horizontal => -indexSingleShift * size.Width,
_ => 0
};
if (!animate || !EnableAnimation)
InstantTransform(child, offset);
else
animations.Add(AnimationTransform(child, offset, Math.Abs(indexShift)));
}
if (animations.Count() > 0)
await Task.WhenAll(animations);
indexShift -= indexSingleShift;
_previousIndex += indexSingleShift;
}
}
private void InstantTransform(Control control, double offset, Size? size = null)
{
var transform = control.RenderTransform as TranslateTransform;
if (transform is null)
{
transform = new TranslateTransform();
control.RenderTransform = transform;
}
if (control.RenderTransformOrigin != RelativePoint.TopLeft)
control.RenderTransformOrigin = RelativePoint.TopLeft;
if (Orientation is Orientation.Vertical)
transform.Y += offset * (size is null ? 1 : size.Value.Height);
else
transform.X += offset * (size is null ? 1 : size.Value.Width);
}
private Task AnimationTransform(Control control, double offset, double speedRatio)
{
var transform = control.RenderTransform as TranslateTransform;
if (transform is null)
{
transform = new TranslateTransform();
control.RenderTransform = transform;
}
if (control.RenderTransformOrigin != RelativePoint.TopLeft)
control.RenderTransformOrigin = RelativePoint.TopLeft;
var animation = new Animation
{
Duration = TimeSpan.FromMilliseconds(Math.Clamp(AnimationSpeed / speedRatio, 120, 1000)),
FillMode = FillMode.Forward,
Children =
{
new KeyFrame
{
Cue = new Cue(1),
Setters =
{
Orientation is Orientation.Vertical ?
new Setter(TranslateTransform.YProperty, transform.Y + offset) :
new Setter(TranslateTransform.XProperty, transform.X + offset)
}
}
}
};
return animation.RunAsync(control);
}
}