Skip to content

Commit

Permalink
Add ClickControl and Connection classes, update Node and Styles
Browse files Browse the repository at this point in the history
The ClickControl and Connection classes have been added. Changes were made in Node.xaml for visual improvements and to support the new classes. Some color codes and styles have been added to Styles.xaml for the same reason. Additionally, the .NET
  • Loading branch information
xorza committed May 18, 2024
1 parent 1d2a7c4 commit fd54069
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 72 deletions.
40 changes: 40 additions & 0 deletions Editor.NET/Editor.NET/ClickControl.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
119 changes: 119 additions & 0 deletions Editor.NET/Editor.NET/Connection.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion Editor.NET/Editor.NET/Editor.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
29 changes: 14 additions & 15 deletions Editor.NET/Editor.NET/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -33,26 +33,25 @@ 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) {
_viewModel.Nodes.Add(new DesignNode());
}

private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) {

}
}
}
66 changes: 33 additions & 33 deletions Editor.NET/Editor.NET/Node.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
Width="Auto"
Height="Auto"
Background="{x:Null}"

Loaded="Node_OnLoaded">
<UserControl.Resources>
<ResourceDictionary>
<Brush x:Key="EventPinColor">#DD423A</Brush>
<sys:Double x:Key="PinSize">8</sys:Double>
<sys:Double x:Key="PinSize">10</sys:Double>

<Style x:Key="PinButton"
TargetType="Button"
Expand All @@ -42,11 +41,16 @@
</ResourceDictionary>
</UserControl.Resources>

<Border BorderBrush="#333" Margin="4" Padding="0" BorderThickness="1"
Background="#444" CornerRadius="1">
<Border BorderBrush="#333"
Margin="4" Padding="0"
BorderThickness="1"
Background="#222"
CornerRadius="2">

<StackPanel Orientation="Vertical" Margin="0">
<Border Background="#555"
<Border Background="#333"
CornerRadius="2 2 0 0"
Padding="0"
MouseLeftButtonDown="Title_OnMouseLeftButtonDown"
MouseLeftButtonUp="Title_OnMouseLeftButtonUp">
<Grid>
Expand All @@ -58,43 +62,42 @@
<Button Style="{StaticResource PinButton}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="-4 -4 0 0"
Background="{StaticResource EventPinColor}" />
Margin="-5 -5 0 0"
Background="{StaticResource Red}" />

<Label Grid.Column="0" Content="{Binding Name}"
VerticalAlignment="Center" Margin="8 3" FontWeight="Bold">
VerticalAlignment="Center" Margin="8 0" FontWeight="Bold">
</Label>

<Button Grid.Column="1"
Margin="1"
Padding="0"
Margin="5"
Padding="2"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Width="15"
Height="15"
Background="Transparent"
>
Height="15"
Background="Transparent">
<Grid>
<Border Background="Transparent"
VerticalAlignment="Center" />

<Grid Margin="2">
<Path Data="M 0,0 L 1,1" Stretch="Fill" Stroke="#222" StrokeThickness="2"
<Grid Margin="0">
<Path Data="M 0,0 L 1,1" Stretch="Fill" Stroke="#555" StrokeThickness="2"
HorizontalAlignment="Center" VerticalAlignment="Center" />
<Path Data="M 0,1 L 1,0" Stretch="Fill" Stroke="#222" StrokeThickness="2"
<Path Data="M 0,1 L 1,0" Stretch="Fill" Stroke="#555" StrokeThickness="2"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>

</Grid>
</Button>

</Grid>
</Border>

<Grid Margin="0 3">
<Grid Margin="0 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="5"></ColumnDefinition>
<ColumnDefinition Width="15"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>

Expand All @@ -107,11 +110,10 @@

<StackPanel Orientation="Horizontal">
<Button Style="{StaticResource PinButton}"
Margin="-4 0 0 0"
Background="Coral"
Margin="-5 0 0 0"
Background="{StaticResource Yellow}"
DataContext="{Binding}"
Loaded="Input_OnLoaded"
/>
Loaded="Input_OnLoaded" />
<Label
VerticalAlignment="Center"
Content="{Binding Name}">
Expand All @@ -134,10 +136,9 @@
<Label Content="{Binding Name}" VerticalAlignment="Center"></Label>

<Button Style="{StaticResource PinButton}"
Margin="0 0 -4 0"
Background="{StaticResource EventPinColor}"
Loaded="Output_OnLoaded"
/>
Margin="0 0 -5 0"
Background="{StaticResource Red}"
Loaded="Output_OnLoaded" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
Expand All @@ -154,10 +155,9 @@
<Label Content="{Binding Name}" VerticalAlignment="Center"></Label>

<Button Style="{StaticResource PinButton}"
Margin="0 0 -4 0"
Background="YellowGreen"
Loaded="Output_OnLoaded"
/>
Margin="0 0 -5 0"
Background="{StaticResource Green}"
Loaded="Output_OnLoaded" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
Expand Down
Loading

0 comments on commit fd54069

Please sign in to comment.