-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathRuler.cs
More file actions
143 lines (122 loc) · 5.24 KB
/
Ruler.cs
File metadata and controls
143 lines (122 loc) · 5.24 KB
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
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace NodeGraph.NET6.Controls
{
internal class Ruler : ContentControl
{
public double Scale
{
get => (double)GetValue(ScaleProperty);
set => SetValue(ScaleProperty, value);
}
public static readonly DependencyProperty ScaleProperty =
DependencyProperty.Register(nameof(Scale), typeof(double), typeof(Ruler), new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.AffectsRender));
public Point Offset
{
get => (Point)GetValue(OffsetProperty);
set => SetValue(OffsetProperty, value);
}
public static readonly DependencyProperty OffsetProperty =
DependencyProperty.Register(nameof(Offset), typeof(Point), typeof(Ruler), new FrameworkPropertyMetadata(new Point(0, 0), FrameworkPropertyMetadataOptions.AffectsRender));
public Brush Color
{
get => (Brush)GetValue(ColorProperty);
set => SetValue(ColorProperty, value);
}
public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register(nameof(Color), typeof(Brush), typeof(Ruler), new FrameworkPropertyMetadata(Brushes.Black, ColorPropertyChanged));
public Orientation Orientation
{
get => (Orientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register(nameof(Orientation), typeof(Orientation), typeof(Ruler), new FrameworkPropertyMetadata(Orientation.Horizontal));
Pen _Pen = null;
static Typeface Typeface = new Typeface("Verdana");
static double LineOffset = 3;
static double LineLength = 5 + LineOffset;
static double LineDistance = 100;
static double SubLineOffset = 1;
static double SubLineLength = 5 + SubLineOffset;
static double SubLineDistance = LineDistance / 10;
static void ColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as Ruler).UpdatePen();
}
static Ruler()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Ruler), new FrameworkPropertyMetadata(typeof(Ruler)));
}
public Ruler()
{
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (_Pen == null)
{
UpdatePen();
}
switch (Orientation)
{
case Orientation.Horizontal:
OnRenderHorizontal(drawingContext);
break;
case Orientation.Vertical:
OnRenderVertical(drawingContext);
break;
}
}
void OnRenderHorizontal(DrawingContext dc)
{
double s = Math.Max(Scale, 1);
double numScale = Math.Max(1.0 / Scale, 1);
int init = (int)(-Offset.X / LineDistance) - 1;
int count = (int)((-Offset.X + ActualWidth) / LineDistance) + 1;
for (int i = init; i < count; ++i)
{
double num = i * LineDistance;
double x = (num + Offset.X) * s;
dc.DrawLine(_Pen, new Point(x, LineOffset), new Point(x, LineLength));
for (int j = 1; j < 10; ++j)
{
double sub_x = x + j * SubLineDistance * s;
dc.DrawLine(_Pen, new Point(sub_x, SubLineOffset), new Point(sub_x, SubLineLength));
}
int numText = (int)(num * numScale);
var text = new FormattedText($"{numText}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, Typeface, 8, Color, 1.0);
dc.DrawText(text, new Point(x - text.Width * 0.5, LineLength));
}
}
void OnRenderVertical(DrawingContext dc)
{
double s = Math.Max(Scale, 1);
double numScale = Math.Max(1.0 / Scale, 1);
int init = (int)(-Offset.Y / LineDistance) - 1;
int count = (int)((-Offset.Y + ActualHeight) / LineDistance) + 1;
for (int i = init; i < count; ++i)
{
double num = i * LineDistance;
double y = (num + Offset.Y) * s;
dc.DrawLine(_Pen, new Point(LineOffset, y), new Point(LineLength, y));
for (int j = 1; j < 10; ++j)
{
double sub_y = y + j * SubLineDistance * s;
dc.DrawLine(_Pen, new Point(SubLineOffset, sub_y), new Point(SubLineLength, sub_y));
}
int numText = (int)(num * numScale);
var text = new FormattedText($"{numText}", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, Typeface, 8, Color, 1.0);
dc.DrawText(text, new Point(LineLength + LineOffset, y - text.Height * 0.5));
}
}
void UpdatePen()
{
_Pen = new Pen(Color, 1);
_Pen.Freeze();
}
}
}