-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathForegroundBackgroundPicker.xaml.cs
291 lines (262 loc) · 10.1 KB
/
ForegroundBackgroundPicker.xaml.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HighlightExplorer
{
/// <summary>
/// Interaction logic for ForegroundBackgroundPicker.xaml
/// </summary>
public partial class ForegroundBackgroundPicker : UserControl
{
public bool ShowForegroundControl
{
get
{
return ctlForeground.Visibility == Visibility.Visible;
}
set
{
if (value)
ctlForeground.Visibility = Visibility.Visible;
else
ctlForeground.Visibility = Visibility.Hidden;
}
}
public static readonly DependencyProperty InnerPaddingProperty = DependencyProperty.Register("InnerPadding", typeof(int), typeof(ForegroundBackgroundPicker), new PropertyMetadata(0, new PropertyChangedCallback(OnInnerPaddingChanged)));
private static void OnInnerPaddingChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ForegroundBackgroundPicker highlightTextPicker = o as ForegroundBackgroundPicker;
if (highlightTextPicker != null)
highlightTextPicker.OnInnerPaddingChanged((int)e.OldValue, (int)e.NewValue);
}
protected virtual void OnInnerPaddingChanged(int oldValue, int newValue)
{
SliderHelper.PadAbove(ctlBackground, InnerPadding);
SliderHelper.PadAbove(ctlForeground, InnerPadding);
}
public int InnerPadding
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (int)GetValue(InnerPaddingProperty);
}
set
{
SetValue(InnerPaddingProperty, value);
}
}
public static readonly DependencyProperty DraggableControlsProperty = DependencyProperty.Register("DraggableControls", typeof(bool), typeof(ForegroundBackgroundPicker), new PropertyMetadata(true, new PropertyChangedCallback(OnDraggableControlsChanged)));
private static void OnDraggableControlsChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ForegroundBackgroundPicker foregroundBackgroundPicker = o as ForegroundBackgroundPicker;
if (foregroundBackgroundPicker != null)
foregroundBackgroundPicker.OnDraggableControlsChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnDraggableControlsChanged(bool oldValue, bool newValue)
{
if (DraggableControls)
{
rctBackground.Visibility = Visibility.Hidden;
rctForeground.Visibility = Visibility.Hidden;
ArrowGray.Visibility = Visibility.Hidden;
SliderHelper.EnableDrag(ctlForeground);
SliderHelper.EnableDrag(ctlBackground);
ctlForeground.MouseUp += SliderMouseUp;
ctlBackground.MouseUp += SliderMouseUp;
ctlForeground.MouseDown += CtlForeground_MouseDown;
ctlBackground.MouseDown += CtlBackground_MouseDown;
SliderHelper.PositionChanged += SliderHelper_PositionChanged;
}
else
{
ArrowGray.Visibility = Visibility.Visible;
rctBackground.Visibility = Visibility.Visible;
rctForeground.Visibility = Visibility.Visible;
SliderHelper.DisableDrag(ctlForeground);
ctlForeground.MouseUp -= SliderMouseUp;
ctlForeground.MouseDown -= CtlForeground_MouseDown;
SliderHelper.DisableDrag(ctlBackground);
ctlBackground.MouseUp -= SliderMouseUp;
ctlBackground.MouseDown -= CtlForeground_MouseDown;
SliderHelper.PositionChanged -= SliderHelper_PositionChanged;
}
}
private void CtlBackground_MouseDown(object sender, MouseButtonEventArgs e)
{
OnBeforeBackgroundChanged(ctlBackground, (double)ctlBackground.Tag);
}
private void CtlForeground_MouseDown(object sender, MouseButtonEventArgs e)
{
OnBeforeForegroundChanged(ctlForeground, (double)ctlForeground.Tag);
}
void PositionBackgroundForegroundArrow()
{
SliderHelper.PositionArrow(ArrowGray, BackgroundValue, ForegroundValue, ArrowShaftGray, tbGrayPercentDistance);
}
public bool DraggableControls
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (bool)GetValue(DraggableControlsProperty);
}
set
{
SetValue(DraggableControlsProperty, value);
}
}
public event EventHandler<double> BeforeForegroundChanged;
public event EventHandler<double> ForegroundChanged;
public event EventHandler<double> AfterForegroundChanged;
public event EventHandler<double> BeforeBackgroundChanged;
public event EventHandler<double> BackgroundChanged;
public event EventHandler<double> AfterBackgroundChanged;
protected virtual void OnForegroundChanged(object sender, double e)
{
ForegroundChanged?.Invoke(sender, e);
}
protected virtual void OnBackgroundChanged(object sender, double e)
{
BackgroundChanged?.Invoke(sender, e);
}
protected virtual void OnBeforeForegroundChanged(object sender, double e)
{
BeforeForegroundChanged?.Invoke(sender, e);
}
protected virtual void OnBeforeBackgroundChanged(object sender, double e)
{
BeforeBackgroundChanged?.Invoke(sender, e);
}
protected virtual void OnAfterForegroundChanged(object sender, double e)
{
AfterForegroundChanged?.Invoke(sender, e);
}
protected virtual void OnAfterBackgroundChanged(object sender, double e)
{
AfterBackgroundChanged?.Invoke(sender, e);
}
#region Dependency Properties...
public static readonly DependencyProperty BackgroundValueProperty = DependencyProperty.Register("BackgroundValue", typeof(double), typeof(ForegroundBackgroundPicker), new PropertyMetadata(0.0, new PropertyChangedCallback(OnBackgroundValueChanged)));
private static void OnBackgroundValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ForegroundBackgroundPicker foregroundBackgroundPicker = o as ForegroundBackgroundPicker;
if (foregroundBackgroundPicker != null)
foregroundBackgroundPicker.OnBackgroundValueChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnBackgroundValueChanged(double oldValue, double newValue)
{
if (DraggableControls)
SliderHelper.SetPosition(ctlBackground, newValue);
else
SliderHelper.SetPosition(ctlBackground, rctBackground, newValue);
SliderHelper.MoveUpIfSpaceAvailable(ctlBackground, ctlForeground, InnerPadding);
if (!DraggableControls)
PositionBackgroundForegroundArrow();
}
public double BackgroundValue
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (double)GetValue(BackgroundValueProperty);
}
set
{
SetValue(BackgroundValueProperty, value);
}
}
public static readonly DependencyProperty ForegroundValueProperty = DependencyProperty.Register("ForegroundValue", typeof(double), typeof(ForegroundBackgroundPicker), new PropertyMetadata(0.0, new PropertyChangedCallback(OnForegroundValueChanged)));
private static void OnForegroundValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
ForegroundBackgroundPicker foregroundBackgroundPicker = o as ForegroundBackgroundPicker;
if (foregroundBackgroundPicker != null)
foregroundBackgroundPicker.OnForegroundValueChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnForegroundValueChanged(double oldValue, double newValue)
{
if (DraggableControls)
SliderHelper.SetPosition(ctlForeground, newValue);
else
SliderHelper.SetPosition(ctlForeground, rctForeground, newValue);
SliderHelper.MoveUpIfSpaceAvailable(ctlForeground, ctlBackground, InnerPadding);
if (!DraggableControls)
PositionBackgroundForegroundArrow();
}
public double ForegroundValue
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (double)GetValue(ForegroundValueProperty);
}
set
{
SetValue(ForegroundValueProperty, value);
}
}
#endregion
public ForegroundBackgroundPicker()
{
InitializeComponent();
if (DraggableControls)
{
SliderHelper.EnableDrag(ctlBackground);
ctlBackground.MouseUp += SliderMouseUp;
SliderHelper.EnableDrag(ctlForeground);
ctlForeground.MouseUp += SliderMouseUp;
SliderHelper.PositionChanged += SliderHelper_PositionChanged;
ctlForeground.MouseDown += CtlForeground_MouseDown;
ctlBackground.MouseDown += CtlBackground_MouseDown;
}
}
private void SliderMouseUp(object sender, MouseButtonEventArgs e)
{
var element = (UIElement)sender;
PopupIfNeeded(element);
}
void PopupIfNeeded(FrameworkElement element1, FrameworkElement element2)
{
if (!SliderHelper.DidPopup(element1, element2, InnerPadding))
SliderHelper.MoveUp(element1, InnerPadding);
SliderHelper.MoveUpIfSpaceAvailable(element2, element1, InnerPadding);
}
void PopupIfNeeded(UIElement uIElement)
{
if (uIElement == ctlBackground)
PopupIfNeeded(ctlBackground, ctlForeground);
else if (uIElement == ctlForeground)
PopupIfNeeded(ctlForeground, ctlBackground);
}
private void SliderHelper_PositionChanged(object sender, PositionChangedEventArgs ea)
{
SetPosition(ea.Element, ea.Position, "Moving");
}
void SetPosition(UIElement uIElement, double position, string logMessage)
{
if (uIElement == ctlBackground)
{
BackgroundValue = position;
ctlBackground.Tag = position;
OnBackgroundChanged(this, position);
}
else if (uIElement == ctlForeground)
{
ForegroundValue = position;
ctlForeground.Tag = position;
OnForegroundChanged(this, position);
}
}
}
}