-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeedle.cs
165 lines (138 loc) · 5.95 KB
/
Needle.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace Rule
{
/// <summary>
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
///
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:Rule"
///
///
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:Rule;assembly=Rule"
///
/// You will also need to add a project reference from the project where the XAML file lives
/// to this project and Rebuild to avoid compilation errors:
///
/// Right click on the target project in the Solution Explorer and
/// "Add Reference"->"Projects"->[Browse to and select this project]
///
///
/// Step 2)
/// Go ahead and use your control in the XAML file.
///
/// <MyNamespace:Needle/>
///
/// </summary>
public class Needle : Control, INotifyPropertyChanged
{
#region Property Change Notification
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged( string propertyName )
{
var handler = PropertyChanged;
if( handler != null ) handler( this, new PropertyChangedEventArgs( propertyName ) );
}
#endregion // Property Change Notification
private int _mousePos;
private Typeface tooltipTypeface;
public Orientation Orientation
{
get { return (Orientation)this.GetValue( OrientationProperty ); }
set { this.SetValue( OrientationProperty, value ); }
}
public int ToothSize
{
get { return (int)this.GetValue( ToothSizeProperty ); }
set { this.SetValue( ToothSizeProperty, value ); }
}
public int NeedleSize
{
get { return (int)this.GetValue( NeedleSizeProperty ); }
set { this.SetValue( NeedleSizeProperty, value ); }
}
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
"Orientation", typeof( Orientation ), typeof( Needle ), new FrameworkPropertyMetadata( Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsRender ) );
public static readonly DependencyProperty ToothSizeProperty = DependencyProperty.Register(
"ToothSize", typeof( int ), typeof( Needle ), new FrameworkPropertyMetadata( 10, FrameworkPropertyMetadataOptions.AffectsRender ) );
public static readonly DependencyProperty NeedleSizeProperty = DependencyProperty.Register(
"NeedleSize", typeof( int ), typeof( Needle ), new FrameworkPropertyMetadata( 30, FrameworkPropertyMetadataOptions.AffectsRender ) );
public int MousePosition
{
get { return _mousePos; }
private set { _mousePos = value; OnPropertyChanged( "MousePosition" ); }
}
static Needle()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( Needle ), new FrameworkPropertyMetadata( typeof( Needle ) ) );
}
public Needle() : base()
{
if( DesignerProperties.GetIsInDesignMode( this ) )
{
MousePosition = 20;
}
Background = Brushes.Transparent;
tooltipTypeface = new Typeface( FontFamily, FontStyle, FontWeight, FontStretch );
this.PreviewMouseMove += ( s, e ) =>
{
var pos = e.GetPosition( this );
if( Orientation == Orientation.Horizontal ) MousePosition = (int)pos.X;
else MousePosition = (int)pos.Y;
InvalidateVisual();
e.Handled = false;
};
}
protected override void OnRender( DrawingContext ctx )
{
base.OnRender( ctx );
var thinPen = new Pen( this.Foreground, 1 );
var thickPen = new Pen( this.Foreground, 3 );
var halfPenWidth = thinPen.Thickness / 2;
var guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add( 0 + halfPenWidth );
guidelines.GuidelinesY.Add( 0 + halfPenWidth );
ctx.PushGuidelineSet( guidelines );
Point pt1, pt2, pt3;
if( Orientation == Orientation.Horizontal )
{
pt1 = new Point( MousePosition, RenderSize.Height );
pt2 = new Point( MousePosition, RenderSize.Height - ToothSize );
pt3 = new Point( MousePosition, RenderSize.Height - ToothSize - NeedleSize );
}
else
{
pt1 = new Point( 0, MousePosition );
pt2 = new Point( ToothSize, MousePosition );
pt3 = new Point( ToothSize + NeedleSize, MousePosition );
}
ctx.DrawLine( new Pen( Brushes.White, 3 ), pt1, pt2 );
ctx.DrawLine( thinPen, pt1, pt2 );
ctx.DrawLine( thickPen, pt2, pt3 );
// var axisLabel = new FormattedText( MousePosition.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
// tooltipTypeface, FontSize, Foreground );
//ctx.DrawText( axisLabel, new Point( ToothSize + MinorTickSize + 3, y - 2 ) );;
}
}
}