forked from amkovkov/GranuSightSoftware2
feat: SectionPanel
This commit is contained in:
@@ -13,7 +13,10 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
[ObservableProperty] private CameraViewModel _cameraViewModel;
|
[ObservableProperty] private CameraViewModel _cameraViewModel;
|
||||||
[ObservableProperty] private ResourcesViewModel _resourcesViewModel;
|
[ObservableProperty] private ResourcesViewModel _resourcesViewModel;
|
||||||
[ObservableProperty] private OpenCvSharp.Mat? _image;
|
[ObservableProperty] private OpenCvSharp.Mat? _image;
|
||||||
[ObservableProperty] private IRelayCommand _buttonClick;
|
[ObservableProperty] private IRelayCommand _buttonCaptureClick;
|
||||||
|
[ObservableProperty] private IRelayCommand _buttonUpClick;
|
||||||
|
[ObservableProperty] private IRelayCommand _buttonDownClick;
|
||||||
|
[ObservableProperty] private int _currentIndex;
|
||||||
|
|
||||||
public MainViewModel(ILogger<MainViewModel> logger, CameraViewModel cameraViewModel, ResourcesViewModel resourcesViewModel, CameraService cameraService)
|
public MainViewModel(ILogger<MainViewModel> logger, CameraViewModel cameraViewModel, ResourcesViewModel resourcesViewModel, CameraService cameraService)
|
||||||
{
|
{
|
||||||
@@ -21,10 +24,12 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
_cameraService = cameraService;
|
_cameraService = cameraService;
|
||||||
CameraViewModel = cameraViewModel;
|
CameraViewModel = cameraViewModel;
|
||||||
ResourcesViewModel = resourcesViewModel;
|
ResourcesViewModel = resourcesViewModel;
|
||||||
ButtonClick = new RelayCommand(() =>
|
ButtonCaptureClick = new RelayCommand(() =>
|
||||||
{
|
{
|
||||||
Task.Run(() => cameraService.CaptureImage(CancellationToken.None, 10).ContinueWith(image => Image = image.Result));
|
Task.Run(() => cameraService.CaptureImage(CancellationToken.None, 10).ContinueWith(image => Image = image.Result));
|
||||||
});
|
});
|
||||||
|
ButtonUpClick = new RelayCommand(() => CurrentIndex--);
|
||||||
|
ButtonDownClick = new RelayCommand(() => CurrentIndex++);
|
||||||
|
|
||||||
_logger.LogInformation("{} initialized", nameof(MainViewModel));
|
_logger.LogInformation("{} initialized", nameof(MainViewModel));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,16 @@
|
|||||||
xmlns:core="using:GSS2.UI.Core"
|
xmlns:core="using:GSS2.UI.Core"
|
||||||
x:DataType="local_vm:MainViewModel"
|
x:DataType="local_vm:MainViewModel"
|
||||||
x:Class="GSS2.Test.Views.MainView">
|
x:Class="GSS2.Test.Views.MainView">
|
||||||
<Grid RowDefinitions="*,*">
|
<Grid ColumnDefinitions="*,100" RowDefinitions="*,*">
|
||||||
<Grid Grid.Row="0" ColumnDefinitions="*,*,Auto">
|
<Button Grid.Row="0" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Command="{Binding ButtonUpClick}"/>
|
||||||
<ContentControl Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Content="{Binding CameraViewModel}" />
|
<Button Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Command="{Binding ButtonDownClick}"/>
|
||||||
<core:OpenCvImage Grid.Column="1" Image="{Binding Image}" />
|
<core:SectionPanel Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" CurrentIndex="{Binding CurrentIndex}">
|
||||||
<Button Grid.Column="2" Width="50" VerticalAlignment="Stretch" Command="{Binding ButtonClick}"/>
|
<ContentControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Content="{Binding ResourcesViewModel}" />
|
||||||
</Grid>
|
<ContentControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Content="{Binding CameraViewModel}" />
|
||||||
<ContentControl Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Content="{Binding ResourcesViewModel}" />
|
<Grid ColumnDefinitions="100, *">
|
||||||
|
<Button Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Command="{Binding ButtonCaptureClick}"/>
|
||||||
|
<core:OpenCvImage Grid.Column="1" Image="{Binding Image}" Background="Red" />
|
||||||
|
</Grid>
|
||||||
|
</core:SectionPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Media;
|
||||||
|
|
||||||
|
namespace GSS2.UI.Core;
|
||||||
|
|
||||||
|
public enum SectionDirection
|
||||||
|
{
|
||||||
|
UpDown,
|
||||||
|
DownUp,
|
||||||
|
LeftRight,
|
||||||
|
RightLeft
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SectionPanel : Panel
|
||||||
|
{
|
||||||
|
public static readonly StyledProperty<int> CurrentIndexProperty = AvaloniaProperty.Register<SectionPanel, int>(nameof(CurrentIndex), 0);
|
||||||
|
public int CurrentIndex
|
||||||
|
{
|
||||||
|
get => GetValue(CurrentIndexProperty);
|
||||||
|
set => SetValue(CurrentIndexProperty, value);
|
||||||
|
}
|
||||||
|
public static readonly StyledProperty<double> AnimationSpeedProperty = AvaloniaProperty.Register<SectionPanel, double>(nameof(AnimationSpeed), 300);
|
||||||
|
public double AnimationSpeed
|
||||||
|
{
|
||||||
|
get => GetValue(AnimationSpeedProperty);
|
||||||
|
set => SetValue(AnimationSpeedProperty, value);
|
||||||
|
}
|
||||||
|
public static readonly StyledProperty<SectionDirection> DirectionProperty = AvaloniaProperty.Register<SectionPanel, SectionDirection>(nameof(Direction), SectionDirection.UpDown);
|
||||||
|
public SectionDirection Direction
|
||||||
|
{
|
||||||
|
get => GetValue(DirectionProperty);
|
||||||
|
set => SetValue(DirectionProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SectionPanel()
|
||||||
|
: base()
|
||||||
|
{
|
||||||
|
CurrentIndexProperty.Changed.AddClassHandler<SectionPanel>((panel, _) => panel.InvalidateArrange());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Size MeasureOverride(Size availableSize)
|
||||||
|
{
|
||||||
|
foreach (var child in Children)
|
||||||
|
child.Measure(availableSize);
|
||||||
|
|
||||||
|
Console.WriteLine(availableSize);
|
||||||
|
return availableSize;
|
||||||
|
}
|
||||||
|
protected override Size ArrangeOverride(Size finalSize)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < Children.Count; i++)
|
||||||
|
{
|
||||||
|
var child = Children[i];
|
||||||
|
|
||||||
|
var rect = Direction switch
|
||||||
|
{
|
||||||
|
SectionDirection.UpDown => new Rect(0, i * finalSize.Height, finalSize.Width, finalSize.Height),
|
||||||
|
SectionDirection.DownUp => new Rect(0, -i * finalSize.Height, finalSize.Width, finalSize.Height),
|
||||||
|
SectionDirection.LeftRight => new Rect(i * finalSize.Width, 0, finalSize.Width, finalSize.Height),
|
||||||
|
SectionDirection.RightLeft => new Rect(-i * finalSize.Width, 0, finalSize.Width, finalSize.Height),
|
||||||
|
|
||||||
|
_ => default
|
||||||
|
};
|
||||||
|
|
||||||
|
child.Arrange(rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
ApplyOffset(finalSize);
|
||||||
|
|
||||||
|
return finalSize;
|
||||||
|
}
|
||||||
|
private void ApplyOffset(Size size)
|
||||||
|
{
|
||||||
|
double offset = Direction switch
|
||||||
|
{
|
||||||
|
SectionDirection.UpDown => -CurrentIndex * size.Height,
|
||||||
|
SectionDirection.DownUp => CurrentIndex * size.Height,
|
||||||
|
SectionDirection.LeftRight => -CurrentIndex * size.Width,
|
||||||
|
SectionDirection.RightLeft => CurrentIndex * size.Width,
|
||||||
|
_ => 0
|
||||||
|
};
|
||||||
|
|
||||||
|
var transform = RenderTransform as TranslateTransform;
|
||||||
|
if (transform is null)
|
||||||
|
{
|
||||||
|
transform = new TranslateTransform();
|
||||||
|
RenderTransform = transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Direction is SectionDirection.LeftRight or SectionDirection.RightLeft)
|
||||||
|
transform.X = offset;
|
||||||
|
else
|
||||||
|
transform.Y = offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user