diff --git a/Editor.NET/Editor.NET/ClickControl.cs b/Editor.NET/Editor.NET/ClickControl.cs new file mode 100644 index 0000000..66a144d --- /dev/null +++ b/Editor.NET/Editor.NET/ClickControl.cs @@ -0,0 +1,40 @@ +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; + +namespace Editor.NET; + +public class ClickControl : Control { + private bool _leftButtonDown; + + protected ClickControl() { + MouseEnter += MouseEnterHandler; + MouseLeave += MouseEnterHandler; + MouseLeftButtonDown += MouseLeftButtonDownHandler; + MouseLeftButtonUp += MouseLeftButtonUpHandler; + } + + public event MouseButtonEventHandler? LeftButtonClick; + + private void MouseEnterHandler(object? sender, RoutedEventArgs ea) { + _leftButtonDown = false; + + InvalidateVisual(); + } + + private void MouseLeftButtonDownHandler(object? sender, MouseButtonEventArgs ea) { + _leftButtonDown = true; + + InvalidateVisual(); + } + + private void MouseLeftButtonUpHandler(object? sender, MouseButtonEventArgs ea) { + if (_leftButtonDown) { + LeftButtonClick?.Invoke(sender, ea); + } + + _leftButtonDown = false; + + InvalidateVisual(); + } +} \ No newline at end of file diff --git a/Editor.NET/Editor.NET/Connection.cs b/Editor.NET/Editor.NET/Connection.cs new file mode 100644 index 0000000..ed37b0f --- /dev/null +++ b/Editor.NET/Editor.NET/Connection.cs @@ -0,0 +1,119 @@ +using System; +using System.Windows; +using System.Windows.Input; +using System.Windows.Media; + +namespace Editor.NET; + +public class ConnectionEventArgs : RoutedEventArgs { + public ConnectionEventArgs(Connection connection) { + Connection = connection; + } + + public Connection Connection { get; } +} + +public class Connection : ClickControl { + public static readonly DependencyProperty InputPositionDependencyProperty = DependencyProperty.Register( + nameof(InputPosition), + typeof(Point), + typeof(Connection), + new PropertyMetadata(Position_PropertyChangedCallback) + ); + + public static readonly DependencyProperty OutputPositionDependencyProperty = DependencyProperty.Register( + nameof(OutputPosition), + typeof(Point), + typeof(Connection), + new PropertyMetadata(Position_PropertyChangedCallback) + ); + + public static readonly DependencyProperty HoverBrushDependencyProperty = DependencyProperty.Register( + nameof(HoverBrush), + typeof(Brush), + typeof(Connection), + new PropertyMetadata(Brushes.Coral) + ); + + public static readonly DependencyProperty BrushProperty = DependencyProperty.Register( + nameof(Brush), typeof(Brush), + typeof(Connection), + new PropertyMetadata(Brushes.SlateGray) + ); + + public Brush Brush { + get { return (Brush)GetValue(BrushProperty); } + set { SetValue(BrushProperty, value); } + } + + public Connection() { + LeftButtonClick += LeftButtonClickHandler; + MouseDoubleClick += MouseButtonEventHandler; + + // Resources.Source = new Uri("/Styles.xaml", UriKind.Relative); + } + + public static readonly DependencyProperty ThicknessProperty = DependencyProperty.Register( + nameof(Thickness), typeof(double), typeof(Connection), new PropertyMetadata(default(double))); + + public double Thickness { + get { return (double)GetValue(ThicknessProperty); } + set { SetValue(ThicknessProperty, value); } + } + + public Point InputPosition { + get => (Point)GetValue(InputPositionDependencyProperty); + set => SetValue(InputPositionDependencyProperty, value); + } + + public Point OutputPosition { + get => (Point)GetValue(OutputPositionDependencyProperty); + set => SetValue(OutputPositionDependencyProperty, value); + } + + public Brush HoverBrush { + get => (Brush)GetValue(HoverBrushDependencyProperty); + set => SetValue(HoverBrushDependencyProperty, value); + } + + private static void Position_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { + ((UIElement)d).InvalidateVisual(); + } + + private void LeftButtonClickHandler(object? sender, MouseButtonEventArgs ea) { + } + + private void MouseButtonEventHandler(object sender, MouseButtonEventArgs e) { + } + + protected override void OnRender(DrawingContext drawingContext) { + base.OnRender(drawingContext); + + Pen pen; + if (IsMouseOver) { + pen = new Pen(HoverBrush, Thickness * 1.5); + } else { + pen = new Pen(Brush, Thickness); + } + + Point[] points = [ + new(InputPosition.X - 5, InputPosition.Y), + new(InputPosition.X - 50, InputPosition.Y), + new(OutputPosition.X + 50, OutputPosition.Y), + new(OutputPosition.X + 5, OutputPosition.Y) + ]; + + var pathFigure = new PathFigure { + StartPoint = points[0] + }; + pathFigure.Segments.Add( + new BezierSegment(points[1], points[2], points[3], true)); + pathFigure.IsClosed = false; + + PathGeometry path = new(); + path.Figures.Add(pathFigure); + + drawingContext.DrawGeometry(null, new Pen(Brushes.Transparent, 5), path); + drawingContext.DrawGeometry(null, pen, path); + } +} \ No newline at end of file diff --git a/Editor.NET/Editor.NET/Editor.NET.csproj b/Editor.NET/Editor.NET/Editor.NET.csproj index 02bf8e8..ca98ac2 100644 --- a/Editor.NET/Editor.NET/Editor.NET.csproj +++ b/Editor.NET/Editor.NET/Editor.NET.csproj @@ -2,9 +2,10 @@ WinExe - net7.0-windows + net8.0-windows enable true + enable diff --git a/Editor.NET/Editor.NET/MainWindow.xaml.cs b/Editor.NET/Editor.NET/MainWindow.xaml.cs index 6168b0d..a398840 100644 --- a/Editor.NET/Editor.NET/MainWindow.xaml.cs +++ b/Editor.NET/Editor.NET/MainWindow.xaml.cs @@ -18,13 +18,13 @@ namespace Editor.NET; public partial class MainWindow : Window { - private MainWindowViewModel _viewModel = new DesignMainWindowViewModel(); + private readonly MainWindowViewModel _viewModel = new DesignMainWindowViewModel(); public MainWindow() { InitializeComponent(); this.DataContext = _viewModel; - + _viewModel.Connections.CollectionChanged += (sender, args) => { RedrawConnections(); }; RedrawConnections(); } @@ -33,19 +33,19 @@ private void RedrawConnections() { ConnectionCanvas.Children.Clear(); foreach (var connection in _viewModel.Connections) { - var connectionView = new Connection { }; - - Binding outputBinding = new("Output.CanvasPosition"); - outputBinding.Source = connection; - connectionView.SetBinding(Connection.OutputPositionDependencyProperty, outputBinding); - - Binding inputBinding = new("Input.CanvasPosition"); - inputBinding.Source = connection; - connectionView.SetBinding(Connection.InputPositionDependencyProperty, inputBinding); - + var connectionView = new Connection { + Thickness = 2, + }; + + connectionView.SetBinding(Connection.OutputPositionDependencyProperty, + new Binding("Output.CanvasPosition") { Source = connection } + ); + connectionView.SetBinding(Connection.InputPositionDependencyProperty, + new Binding("Input.CanvasPosition") { Source = connection } + ); + ConnectionCanvas.Children.Add(connectionView); } - } private void AddDesignNodeButton_OnClick(object sender, RoutedEventArgs e) { @@ -53,6 +53,5 @@ private void AddDesignNodeButton_OnClick(object sender, RoutedEventArgs e) { } private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { - } -} +} \ No newline at end of file diff --git a/Editor.NET/Editor.NET/Node.xaml b/Editor.NET/Editor.NET/Node.xaml index 4c46a80..c091158 100644 --- a/Editor.NET/Editor.NET/Node.xaml +++ b/Editor.NET/Editor.NET/Node.xaml @@ -11,12 +11,11 @@ Width="Auto" Height="Auto" Background="{x:Null}" - + Loaded="Node_OnLoaded"> - #DD423A - 8 + 10 - -