Browse Source

Pointer Debug Window WIP

CPKreuz 1 year ago
parent
commit
c793117f60

+ 6 - 0
src/PixiEditor.AvaloniaUI/ViewModels/SubViewModels/DebugViewModel.cs

@@ -220,6 +220,12 @@ internal class DebugViewModel : SubViewModel<ViewModelMain>
         new CommandDebugPopup().Show();
     }
 
+    [Command.Debug("PixiEditor.Debug.OpenPointerDebugWindow", "Open pointer debug window", "Open pointer debug window")]
+    public void OpenPointerDebugWindow()
+    {
+        new PointerDebugPopup().Show();
+    }
+
     [Command.Debug("PixiEditor.Debug.OpenLocalizationDebugWindow", "OPEN_LOCALIZATION_DEBUG_WINDOW", "OPEN_LOCALIZATION_DEBUG_WINDOW")]
     public void OpenLocalizationDebugWindow()
     {

+ 72 - 0
src/PixiEditor.AvaloniaUI/Views/Dialogs/Debugging/PointerDebugField.cs

@@ -0,0 +1,72 @@
+using System.Collections.Generic;
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Input;
+using Avalonia.Media;
+
+namespace PixiEditor.AvaloniaUI.Views.Dialogs.Debugging;
+
+public class PointerDebugField : Control
+{
+    private List<PointerPoint> _lastPoints = new();
+    private Pen pen = new(Brushes.Aqua);
+
+    public static readonly StyledProperty<int> PointCountProperty =
+        AvaloniaProperty.Register<PointerDebugPopup, int>(nameof(PointCount));
+
+    public int PointCount
+    {
+        get => GetValue(PointCountProperty);
+        set => SetValue(PointCountProperty, value);
+    }
+    
+    public static readonly StyledProperty<IBrush> PointBrushProperty =
+        AvaloniaProperty.Register<PointerDebugPopup, IBrush>(nameof(PointBrush));
+
+    public IBrush PointBrush
+    {
+        get => GetValue(PointBrushProperty);
+        set => SetValue(PointBrushProperty, value);
+    }
+
+    public void ReportPoint(PointerPoint point)
+    {
+        // if (_lastPoints.Count > 5000) _lastPoints.Clear();
+        
+        _lastPoints.Add(point);
+        PointCount = _lastPoints.Count;
+        
+        InvalidateVisual();
+    }
+
+    public void ClearPoints()
+    {
+        _lastPoints.Clear();
+        PointCount = _lastPoints.Count;
+        
+        InvalidateVisual();
+    }
+
+    protected override Size MeasureOverride(Size availableSize) => availableSize;
+
+    public override void Render(DrawingContext context)
+    {
+        int xCount = (int)(DesiredSize.Width / 15);
+        int yCount = (int)(DesiredSize.Height / 15);
+        for (int y = 0; y < yCount; y++)
+        {
+            for (int x = 0; x < xCount; x++)
+            {
+                context.DrawEllipse(PointBrush, null, new Point(x * 15 + 15, y * 15 + 15), 2, 2);
+            }
+        }
+        
+        for (var i = 0; i < _lastPoints.Count - 1; i++)
+        {
+            var point1 = _lastPoints[i];
+            var point2 = _lastPoints[i + 1];
+            
+            context.DrawLine(pen, point1.Position, point2.Position);
+        }
+    }
+}

+ 33 - 0
src/PixiEditor.AvaloniaUI/Views/Dialogs/Debugging/PointerDebugPopup.axaml

@@ -0,0 +1,33 @@
+<dialogs:PixiEditorPopup
+    xmlns="https://github.com/avaloniaui"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    xmlns:dialogs="clr-namespace:PixiEditor.AvaloniaUI.Views.Dialogs"
+    xmlns:debugging="clr-namespace:PixiEditor.AvaloniaUI.Views.Dialogs.Debugging"
+    mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
+    Width="1000" Height="800"
+    x:Class="PixiEditor.AvaloniaUI.Views.Dialogs.Debugging.PointerDebugPopup"
+    Name="uc"
+    Title="Pointer Debug Window" Padding="10">
+    <Grid RowDefinitions="*,Auto">
+        <Border CornerRadius="10" BorderThickness="1"
+                BorderBrush="{DynamicResource ThemeBorderHighBrush}"
+                Background="{DynamicResource ThemeControlLowBrush}"
+                PointerMoved="OnPointerMoved"
+                PointerEntered="OnPointerEntered">
+            <debugging:PointerDebugField Name="DebugField" PointBrush="{DynamicResource ThemeBorderHighBrush}" />
+        </Border>
+        <StackPanel Grid.Row="1" Orientation="Vertical" Margin="0, 5, 0, 0">
+            <TextBlock HorizontalAlignment="Right" Text="{Binding #DebugField.PointCount, StringFormat='Points: {0}'}" />
+            <StackPanel Orientation="Vertical" Margin="5">
+                <TextBlock Text="{Binding #uc.PointerType, StringFormat='Pointer Type: {0}'}" />
+                <WrapPanel>
+                    <TextBlock Text="{Binding #uc.LastPoint.Pressure, StringFormat='Pressure: {0}'}" />
+                    <TextBlock Text="{Binding #uc.LastPoint.XTilt, StringFormat='X Tilt: {0}'}" />
+                    <TextBlock Text="{Binding #uc.LastPoint.YTilt, StringFormat='Y Tilt: {0}'}" />
+                </WrapPanel>
+            </StackPanel>
+        </StackPanel>
+    </Grid>
+</dialogs:PixiEditorPopup>

+ 43 - 0
src/PixiEditor.AvaloniaUI/Views/Dialogs/Debugging/PointerDebugPopup.axaml.cs

@@ -0,0 +1,43 @@
+using Avalonia;
+using Avalonia.Input;
+
+namespace PixiEditor.AvaloniaUI.Views.Dialogs.Debugging;
+
+public partial class PointerDebugPopup : PixiEditorPopup
+{
+    public static readonly StyledProperty<PointerPointProperties> LastPointProperty =
+        AvaloniaProperty.Register<PointerDebugPopup, PointerPointProperties>(nameof(LastPoint));
+
+    public PointerPointProperties LastPoint
+    {
+        get => GetValue(LastPointProperty);
+        set => SetValue(LastPointProperty, value);
+    }
+    
+    public static readonly StyledProperty<PointerType> PointerTypeProperty =
+        AvaloniaProperty.Register<PointerDebugPopup, PointerType>(nameof(LastPoint));
+
+    public PointerType PointerType
+    {
+        get => GetValue(PointerTypeProperty);
+        set => SetValue(PointerTypeProperty, value);
+    }
+    
+    public PointerDebugPopup()
+    {
+        InitializeComponent();
+    }
+
+    private void OnPointerMoved(object? sender, PointerEventArgs e)
+    {
+        var point = e.GetCurrentPoint(DebugField);
+        LastPoint = point.Properties;
+        
+        DebugField.ReportPoint(point);
+    }
+
+    private void OnPointerEntered(object? sender, PointerEventArgs e)
+    {
+        DebugField.ClearPoints();
+    }
+}