feat/estimate #50
2 changed files with 54 additions and 2 deletions
bug fix painting the signature
commit
e987f215bb
|
|
@ -68,21 +68,38 @@ public class SignaturePadControl : TemplatedControl
|
||||||
public event EventHandler? RedrawRequested;
|
public event EventHandler? RedrawRequested;
|
||||||
|
|
||||||
private readonly List<int> _strokes = new(capacity: 256);
|
private readonly List<int> _strokes = new(capacity: 256);
|
||||||
|
private InputElement? _wiredCaptureArea;
|
||||||
private int _pendingPoints; // number of (x, y) pairs awaiting a length prefix
|
private int _pendingPoints; // number of (x, y) pairs awaiting a length prefix
|
||||||
private bool _capturing;
|
private bool _capturing;
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
|
RewireCaptureArea();
|
||||||
|
}
|
||||||
|
|
||||||
if (CaptureArea is { } previous)
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
||||||
|
{
|
||||||
|
base.OnPropertyChanged(change);
|
||||||
|
|
||||||
|
if (change.Property == CaptureAreaProperty)
|
||||||
|
{
|
||||||
|
RewireCaptureArea();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RewireCaptureArea()
|
||||||
|
{
|
||||||
|
if (_wiredCaptureArea is { } previous)
|
||||||
{
|
{
|
||||||
previous.PointerPressed -= OnCapturePressed;
|
previous.PointerPressed -= OnCapturePressed;
|
||||||
previous.PointerMoved -= OnCaptureMoved;
|
previous.PointerMoved -= OnCaptureMoved;
|
||||||
previous.PointerReleased -= OnCaptureReleased;
|
previous.PointerReleased -= OnCaptureReleased;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CaptureArea is { } area)
|
_wiredCaptureArea = CaptureArea;
|
||||||
|
|
||||||
|
if (_wiredCaptureArea is { } area)
|
||||||
{
|
{
|
||||||
area.PointerPressed += OnCapturePressed;
|
area.PointerPressed += OnCapturePressed;
|
||||||
area.PointerMoved += OnCaptureMoved;
|
area.PointerMoved += OnCaptureMoved;
|
||||||
|
|
@ -97,12 +114,14 @@ public class SignaturePadControl : TemplatedControl
|
||||||
_capturing = true;
|
_capturing = true;
|
||||||
_pendingPoints = 0;
|
_pendingPoints = 0;
|
||||||
AppendPoint(e.GetPosition(CaptureArea));
|
AppendPoint(e.GetPosition(CaptureArea));
|
||||||
|
RedrawRequested?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnCaptureMoved(object? sender, PointerEventArgs e)
|
private void OnCaptureMoved(object? sender, PointerEventArgs e)
|
||||||
{
|
{
|
||||||
if (!_capturing) return;
|
if (!_capturing) return;
|
||||||
AppendPoint(e.GetPosition(CaptureArea));
|
AppendPoint(e.GetPosition(CaptureArea));
|
||||||
|
RedrawRequested?.Invoke(this, EventArgs.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnCaptureReleased(object? sender, PointerReleasedEventArgs e)
|
private void OnCaptureReleased(object? sender, PointerReleasedEventArgs e)
|
||||||
|
|
@ -169,6 +188,16 @@ public class SignaturePadControl : TemplatedControl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SignaturePadData Snapshot() => new(_strokes.ToArray());
|
public SignaturePadData Snapshot() => new(_strokes.ToArray());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Copy of the current in-progress stroke, without the length
|
||||||
|
/// prefix used for sealed strokes. The view can render this as a
|
||||||
|
/// live preview while the user is still drawing.
|
||||||
|
/// </summary>
|
||||||
|
internal IReadOnlyList<int> PendingStroke
|
||||||
|
=> _capturing && _pendingPoints > 0
|
||||||
|
? _strokes.GetRange(_strokes.Count - 2 * _pendingPoints, 2 * _pendingPoints)
|
||||||
|
: Array.Empty<int>();
|
||||||
|
|
||||||
// --- Test-only surface (visible to PostIt.Tests) -------------------
|
// --- Test-only surface (visible to PostIt.Tests) -------------------
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -87,5 +87,28 @@ public partial class SignaturePage : ContentPage
|
||||||
poly.Points = pts;
|
poly.Points = pts;
|
||||||
InkLayer.Children.Add(poly);
|
InkLayer.Children.Add(poly);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pending = _control.PendingStroke;
|
||||||
|
if (pending.Count > 0)
|
||||||
|
{
|
||||||
|
var poly = new Polyline
|
||||||
|
{
|
||||||
|
Stroke = StrokeBrush,
|
||||||
|
StrokeThickness = StrokeThickness,
|
||||||
|
StrokeLineCap = PenLineCap.Round,
|
||||||
|
StrokeJoin = PenLineJoin.Round,
|
||||||
|
};
|
||||||
|
|
||||||
|
var pts = new List<Point>(pending.Count / 2);
|
||||||
|
for (int p = 0; p < pending.Count; p += 2)
|
||||||
|
{
|
||||||
|
int nx = pending[p];
|
||||||
|
int ny = pending[p + 1];
|
||||||
|
pts.Add(new Point(nx / CoordinateMax * w, ny / CoordinateMax * h));
|
||||||
|
}
|
||||||
|
|
||||||
|
poly.Points = pts;
|
||||||
|
InkLayer.Children.Add(poly);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue