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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
| public class Arrow : Shape
{
public static GridLength LengthDefault { get; }
= new GridLength(10);
protected override Geometry DefiningGeometry => ArrowGeometry;
/// <summary>Линия стрелки-указателя.
/// В <see cref="PathFigure.StartPoint"/> координата левого кончика.</summary>
protected readonly PathFigure ArrowFigure = new PathFigure() { IsClosed = true };
protected readonly PathGeometry ArrowGeometry;
/// <summary>Сегменты стрелки указателя:<br/>
/// 0 - верх левого наконечника;<br/>
/// 1 - верхний левый угол тела;<br/>
/// 2 - верхний правый угол тела;<br/>
/// 3 - вверх правого наконечника;<br/>
/// 4 - правый кончик;<br/>
/// 5 - низ правого наконечника;<br/>
/// 6 - нижний правый угол тела;<br/>
/// 7 - нижний левый угол тела;<br/>
/// 8 - низ левого наконечника.</summary>
protected readonly ReadOnlyCollection<LineSegment> ArrowSegments;
public Arrow()
{
LineSegment[] arrowSegments = new LineSegment[9];
for (int i = 0; i < 9; i++)
ArrowFigure.Segments.Add(arrowSegments[i] = new LineSegment());
ArrowSegments = Array.AsReadOnly(arrowSegments);
ArrowGeometry = new PathGeometry();
ArrowGeometry.Figures.Add(ArrowFigure);
}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.Property == StrokeThicknessProperty
|| e.Property == ActualWidthProperty
|| e.Property == ActualHeightProperty)
{
Rect rect = LayoutInformation.GetLayoutSlot(this);
RenderArrow(rect.Width, rect.Height);
}
}
public GridLength TipLength
{
get { return (GridLength)GetValue(TipLengthProperty); }
set { SetValue(TipLengthProperty, value); }
}
// Using a DependencyProperty as the backing store for TipLength. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TipLengthProperty =
DependencyProperty.Register(nameof(TipLength), typeof(GridLength), typeof(Arrow),
new FrameworkPropertyMetadata(LengthDefault) { CoerceValueCallback = LengthCoerce, AffectsMeasure = true });
private static object LengthCoerce(DependencyObject d, object baseValue)
{
Arrow arrow = (Arrow)d;
GridLength newLength = (GridLength)baseValue;
if (newLength.IsAuto)
return LengthDefault;
return newLength;
}
public GridLength BodyThickness
{
get { return (GridLength)GetValue(BodyThicknessProperty); }
set { SetValue(BodyThicknessProperty, value); }
}
// Using a DependencyProperty as the backing store for ThicknessBody. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BodyThicknessProperty =
DependencyProperty.Register(nameof(BodyThickness), typeof(GridLength), typeof(Arrow),
new FrameworkPropertyMetadata(LengthDefault) { CoerceValueCallback = LengthCoerce, AffectsMeasure = true });
public bool LeftTipVisibility
{
get { return (bool)GetValue(LeftTipVisibilityProperty); }
set { SetValue(LeftTipVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for LeftTipVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LeftTipVisibilityProperty =
DependencyProperty.Register(nameof(LeftTipVisibility), typeof(bool), typeof(Arrow),
new FrameworkPropertyMetadata(true, RenderArrow) { AffectsMeasure = true });
public bool RightTipVisibility
{
get { return (bool)GetValue(RightTipVisibilityProperty); }
set { SetValue(RightTipVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for RightTipVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty RightTipVisibilityProperty =
DependencyProperty.Register(nameof(RightTipVisibility), typeof(bool), typeof(Arrow),
new FrameworkPropertyMetadata(true, RenderArrow) { AffectsMeasure = true });
private static void RenderArrow(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Arrow arrow = (Arrow)d;
Rect rect = LayoutInformation.GetLayoutSlot(arrow);
arrow.RenderArrow(rect.Width, rect.Height);
}
private void RenderArrow(double actualWidth, double actualHeight)
{
// Определение размеров стрелки
if (actualHeight > ActualHeight)
actualHeight = ActualHeight;
if (actualWidth > ActualWidth)
actualWidth = ActualWidth;
double minWidth = 0;
if (TipLength.IsAbsolute && (LeftTipVisibility || RightTipVisibility))
{
minWidth = TipLength.Value + StrokeThickness;
if (LeftTipVisibility && RightTipVisibility)
minWidth *= 2;
}
double minHeight = 0;
if (BodyThickness.IsAbsolute)
minHeight = BodyThickness.Value + StrokeThickness;
if (actualHeight < minHeight)
actualHeight = minHeight;
if (actualWidth < minWidth)
actualWidth = minWidth;
// Размеры за вычетом границы
double left = StrokeThickness * 0.5;
double top = left;
double bottom = actualHeight - top;
double right = actualWidth - left;
double height = bottom - top;
double width = right - left;
if (width < 0)
{
}
// Вычисление толщины тела стрелки
double bodyHeight = BodyThickness.Value;
if (BodyThickness.IsStar)
bodyHeight *= height;
// Вычисление длины наконечников
double tipLength = TipLength.Value;
if (TipLength.IsStar)
tipLength *= width;
// Вычисление границ тела
double bodyLeft = left + (LeftTipVisibility ? tipLength : 0);
double bodyRight = right - (RightTipVisibility ? tipLength : 0);
double bodyTop = (height - bodyHeight) * 0.5;
double bodyBottom = bodyTop + bodyHeight;
// Центр высоты
double medHeight = top + height * 0.5;
// Кончики стрелки
ArrowFigure.StartPoint = new Point(left, medHeight);
ArrowSegments[4].Point = new Point(right, medHeight);
// Точки левого наконечника
if (LeftTipVisibility)
{
ArrowSegments[0].Point = new Point(bodyLeft, top);
ArrowSegments[8].Point = new Point(bodyLeft, bottom);
}
else
{
ArrowSegments[0].Point = new Point(bodyLeft, bodyTop);
ArrowSegments[8].Point = new Point(bodyLeft, bodyBottom);
}
// Точки правого наконечника
if (RightTipVisibility)
{
ArrowSegments[3].Point = new Point(bodyRight, top);
ArrowSegments[5].Point = new Point(bodyRight, bottom);
}
else
{
ArrowSegments[3].Point = new Point(bodyRight, bodyTop);
ArrowSegments[5].Point = new Point(bodyRight, bodyBottom);
}
// Точки тела
ArrowSegments[1].Point = new Point(bodyLeft, bodyTop);
ArrowSegments[2].Point = new Point(bodyRight, bodyTop);
ArrowSegments[7].Point = new Point(bodyLeft, bodyBottom);
ArrowSegments[6].Point = new Point(bodyRight, bodyBottom);
}
// Oпределение размеров стрелки указателя
protected override Size MeasureOverride(Size constraint)
{
double minWidth = 0;
if (TipLength.IsAbsolute && (LeftTipVisibility || RightTipVisibility))
{
minWidth = TipLength.Value + StrokeThickness;
if (LeftTipVisibility && RightTipVisibility)
minWidth *= 2;
}
double minHeight = 0;
if (BodyThickness.IsAbsolute)
minHeight = BodyThickness.Value + StrokeThickness;
if (constraint.Width < minWidth || constraint.Height < minHeight)
constraint = new Size
(
constraint.Width < minWidth ? minWidth : constraint.Width,
constraint.Height < minHeight ? minHeight : constraint.Height
);
RenderArrow(constraint.Width, constraint.Height);
return base.MeasureOverride(constraint);
}
} |