-
Notifications
You must be signed in to change notification settings - Fork 62
/
WireControl.cs
46 lines (39 loc) · 1.97 KB
/
WireControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Windows;
using System.Windows.Media;
namespace SchematicControls
{
public class WireControl : ElementControl
{
static WireControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WireControl), new FrameworkPropertyMetadata(typeof(WireControl)));
}
protected Circuit.Wire wire;
public WireControl(Circuit.Wire W) : base(W)
{
wire = W;
MouseMove += (s, e) => ToolTip = wire.Node != null ? wire.Node.ToString() : null;
}
// To avoid glitches, draw wire terminals a little smaller than regular terminals.
public static double WireTerminalSize = TerminalSize * 0.9;
protected override void OnRender(DrawingContext dc)
{
// This isn't pointless, it makes WPF mouse hit tests succeed near the wire instead of exactly on it.
dc.DrawRectangle(Brushes.Transparent, null, new Rect(-2, -2, ActualWidth + 4, ActualHeight + 4));
Pen pen;
if (Selected)
pen = SelectedWirePen;
else if (Highlighted)
pen = HighlightedWirePen;
else
pen = WirePen;
dc.DrawLine(pen, new Point(0, 0), new Point(ActualWidth, ActualHeight));
// Don't use the pen to draw the terminals, because the terminals tend to get overdrawn by other components.
Vector dx = new Vector(WireTerminalSize / 2, WireTerminalSize / 2);
foreach (Point x in new[] { ToPoint(wire.A - wire.LowerBound), ToPoint(wire.B - wire.LowerBound) })
dc.DrawRectangle(WirePen.Brush, WirePen, new Rect(x - dx, x + dx));
}
protected static Pen SelectedWirePen = new Pen(Brushes.DodgerBlue, EdgeThickness) { StartLineCap = PenLineCap.Round, EndLineCap = PenLineCap.Round };
protected static Pen HighlightedWirePen = new Pen(Brushes.Gray, EdgeThickness) { StartLineCap = PenLineCap.Round, EndLineCap = PenLineCap.Round };
}
}