Skip to content

Commit

Permalink
Fix OnPointerPressed event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed May 6, 2024
1 parent 2b7e480 commit 5882fc2
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 110 deletions.
134 changes: 68 additions & 66 deletions NodifyM.Avalonia/Controls/BaseNode.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,97 @@
using Avalonia.VisualTree;
using NodifyM.Avalonia.Events;
using NodifyM.Avalonia.Helpers;
using NodifyM.Avalonia.ViewModelBase;

namespace NodifyM.Avalonia.Controls;

public class BaseNode : ContentControl
{
public static readonly AvaloniaProperty<Point> LocationProperty =
AvaloniaProperty.Register<BaseNode, Point>(nameof(Location));
public static readonly RoutedEvent LocationChangedEvent = RoutedEvent.Register<NodeLocationEventArgs>(nameof(LocationChanged), RoutingStrategies.Bubble, typeof(BaseNode));

public static readonly RoutedEvent LocationChangedEvent =
RoutedEvent.Register<NodeLocationEventArgs>(nameof(LocationChanged), RoutingStrategies.Bubble,
typeof(BaseNode));

public static readonly AvaloniaProperty<bool> IsSelectedProperty =
AvaloniaProperty.Register<BaseNode, bool>(nameof(IsSelected));

public bool IsSelected
{
get => (bool)GetValue(IsSelectedProperty);
set => SetValue(IsSelectedProperty, value);
}

public event NodeLocationEventHandler LocationChanged
{
add => AddHandler(LocationChangedEvent, value);
remove => RemoveHandler(LocationChangedEvent, value);
}

public Point Location
{
get => (Point)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}

public BaseNode()

protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
if (!isDragging) return;
// 停止拖动
isDragging = false;
e.Handled = true;
// 停止计时器
_editor.ClearAlignmentLine();

// var currentPoint = e.GetCurrentPoint(this);
// Debug.WriteLine($"停止拖动坐标X:{OffsetX} Y:{OffsetY}");
RaiseEvent(new NodeLocationEventArgs(Location, this, LocationChangedEvent, true));
}

protected override void OnPointerPressed(PointerPressedEventArgs e)
{
PointerPressed += OnPointerPressed;
PointerMoved += OnPointerMoved;
PointerReleased += OnPointerReleased;

base.OnPointerPressed(e);
_editor.SelectItem(this);
if (!e.GetCurrentPoint(this)
.Properties.IsLeftButtonPressed) return;
e.GetCurrentPoint(this)
.Pointer.Capture(this);
// 启动拖动
isDragging = true;
// 记录当前坐标
var relativeTo = ((Visual)this.GetLogicalParent()).GetVisualParent();
lastMousePosition = e.GetPosition((Visual)relativeTo);
// Debug.WriteLine($"记录当前坐标X:{lastMousePosition.X} Y:{lastMousePosition.Y}");
_startOffsetX = Location.X;
_startOffsetY = Location.Y;
e.Handled = true;
}

protected override void OnPointerMoved(PointerEventArgs e)
{
base.OnPointerMoved(e);
if (!e.GetCurrentPoint(((Visual)this.GetLogicalParent()).GetVisualParent())
.Properties
.IsLeftButtonPressed) return;

// 如果没有启动拖动,则不执行
if (!isDragging) return;

var currentMousePosition = e.GetPosition(((Visual)this.GetLogicalParent()).GetVisualParent());
var offset = currentMousePosition - lastMousePosition;

if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
{
_editor.ClearAlignmentLine();
Location = new Point((offset.X + _startOffsetX), offset.Y + _startOffsetY);
}
else
Location = _editor.TryAlignNode(this,
new Point((offset.X + _startOffsetX), offset.Y + _startOffsetY));

RaiseEvent(new NodeLocationEventArgs(Location, this, LocationChangedEvent));
}

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
Expand All @@ -55,12 +113,13 @@ private void NodifyAutoPanningEvent(object sender, NodifyAutoPanningEventArgs e)
{
return;
}
RaiseEvent(new NodeLocationEventArgs(Location,this,LocationChangedEvent));

RaiseEvent(new NodeLocationEventArgs(Location, this, LocationChangedEvent));
}


private NodifyEditor _editor;

/// <summary>
/// 记录上一次鼠标位置
/// </summary>
Expand All @@ -75,68 +134,11 @@ private void NodifyAutoPanningEvent(object sender, NodifyAutoPanningEventArgs e)
private double _startOffsetX;
private double _startOffsetY;

private void OnPointerPressed(object sender, PointerPressedEventArgs e)
{
e.GetCurrentPoint(this).Pointer.Capture(this);


_editor.SelectItem(this);
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
// 启动拖动
isDragging = true;
// 记录当前坐标
var relativeTo = ((Visual)this.GetLogicalParent()).GetVisualParent();
lastMousePosition = e.GetPosition((Visual)relativeTo);

// Debug.WriteLine($"记录当前坐标X:{lastMousePosition.X} Y:{lastMousePosition.Y}");
_startOffsetX = Location.X;
_startOffsetY = Location.Y;
e.Handled = true;
}

private void OnPointerReleased(object sender, PointerReleasedEventArgs e)
{


if (!isDragging) return;
// 停止拖动
isDragging = false;
e.Handled = true;
// 停止计时器
_editor.ClearAlignmentLine();

// var currentPoint = e.GetCurrentPoint(this);
// Debug.WriteLine($"停止拖动坐标X:{OffsetX} Y:{OffsetY}");
RaiseEvent(new NodeLocationEventArgs(Location,this,LocationChangedEvent,true));
}

protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
{
base.OnPointerCaptureLost(e);
RaiseEvent(new NodeLocationEventArgs(Location,this,LocationChangedEvent,true));
RaiseEvent(new NodeLocationEventArgs(Location, this, LocationChangedEvent, true));
_editor.ClearAlignmentLine();
}

private void OnPointerMoved(object sender, PointerEventArgs e)
{
if (!e.GetCurrentPoint(((Visual)this.GetLogicalParent()).GetVisualParent()).Properties
.IsLeftButtonPressed) return;

// 如果没有启动拖动,则不执行
if (!isDragging) return;

var currentMousePosition = e.GetPosition(((Visual)this.GetLogicalParent()).GetVisualParent());
var offset = currentMousePosition - lastMousePosition;

if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
{
_editor.ClearAlignmentLine();
Location = new Point((offset.X + _startOffsetX), offset.Y + _startOffsetY);
}
else
Location = _editor.TryAlignNode(this,
new Point((offset.X + _startOffsetX), offset.Y + _startOffsetY));

RaiseEvent(new NodeLocationEventArgs(Location,this,LocationChangedEvent));
}
}
Loading

0 comments on commit 5882fc2

Please sign in to comment.