|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Windows; |
| 6 | +using System.Windows.Controls; |
| 7 | +using System.Windows.Data; |
| 8 | +using System.Windows.Documents; |
| 9 | +using System.Windows.Input; |
| 10 | +using System.Windows.Media; |
| 11 | +using System.Windows.Media.Imaging; |
| 12 | +using System.Windows.Navigation; |
| 13 | +using System.Windows.Shapes; |
| 14 | + |
| 15 | +namespace VidCoder.Controls |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Interaction logic for SeekBar.xaml |
| 19 | + /// </summary> |
| 20 | + public partial class SeekBar : UserControl |
| 21 | + { |
| 22 | + public SeekBar() |
| 23 | + { |
| 24 | + InitializeComponent(); |
| 25 | + |
| 26 | + this.UpdateSeekBarUI(); |
| 27 | + this.UpdateMarkers(); |
| 28 | + } |
| 29 | + |
| 30 | + public static readonly DependencyProperty SlotProperty = DependencyProperty.Register( |
| 31 | + "Slot", |
| 32 | + typeof(int), |
| 33 | + typeof(SeekBar), |
| 34 | + new PropertyMetadata(new PropertyChangedCallback(OnSlotChanged))); |
| 35 | + public int Slot |
| 36 | + { |
| 37 | + get |
| 38 | + { |
| 39 | + return (int)GetValue(SlotProperty); |
| 40 | + } |
| 41 | + |
| 42 | + set |
| 43 | + { |
| 44 | + SetValue(SlotProperty, value); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static readonly DependencyProperty NumSlotsProperty = DependencyProperty.Register( |
| 49 | + "NumSlots", |
| 50 | + typeof(int), |
| 51 | + typeof(SeekBar), |
| 52 | + new PropertyMetadata(new PropertyChangedCallback(OnNumSlotsChanged))); |
| 53 | + public int NumSlots |
| 54 | + { |
| 55 | + get |
| 56 | + { |
| 57 | + return (int)GetValue(NumSlotsProperty); |
| 58 | + } |
| 59 | + |
| 60 | + set |
| 61 | + { |
| 62 | + SetValue(NumSlotsProperty, value); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void OnSlotChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs) |
| 67 | + { |
| 68 | + if (eventArgs.NewValue != eventArgs.OldValue) |
| 69 | + { |
| 70 | + var seekBar = dependencyObject as SeekBar; |
| 71 | + |
| 72 | + seekBar.UpdateSeekBarUI(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static void OnNumSlotsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs) |
| 77 | + { |
| 78 | + if (eventArgs.NewValue != eventArgs.OldValue) |
| 79 | + { |
| 80 | + var seekBar = dependencyObject as SeekBar; |
| 81 | + |
| 82 | + seekBar.UpdateSeekBarUI(); |
| 83 | + seekBar.UpdateMarkers(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
| 88 | + { |
| 89 | + this.HandleMouseEvent(e); |
| 90 | + this.CaptureMouse(); |
| 91 | + } |
| 92 | + |
| 93 | + private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) |
| 94 | + { |
| 95 | + this.ReleaseMouseCapture(); |
| 96 | + } |
| 97 | + |
| 98 | + private void UserControl_MouseMove(object sender, MouseEventArgs e) |
| 99 | + { |
| 100 | + if (e.LeftButton == MouseButtonState.Pressed) |
| 101 | + { |
| 102 | + this.HandleMouseEvent(e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void HandleMouseEvent(MouseEventArgs e) |
| 107 | + { |
| 108 | + if (!this.IsEnabled) |
| 109 | + { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + Point location = e.GetPosition(this); |
| 114 | + |
| 115 | + double width = this.ActualWidth; |
| 116 | + |
| 117 | + if (location.X < 0 || location.X > this.ActualWidth) |
| 118 | + { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + double slotWidth = width / (this.NumSlots - 1); |
| 123 | + |
| 124 | + int newSlot = (int)(location.X / slotWidth + 0.5); |
| 125 | + if (newSlot >= 0 && newSlot < this.NumSlots && newSlot != this.Slot) |
| 126 | + { |
| 127 | + this.Slot = newSlot; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void UpdateSeekBarUI() |
| 132 | + { |
| 133 | + if (this.NumSlots <= 0) |
| 134 | + { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + this.filledColumn.Width = new GridLength(this.Slot, GridUnitType.Star); |
| 139 | + this.emptyColumn.Width = new GridLength(this.NumSlots - this.Slot - 1, GridUnitType.Star); |
| 140 | + |
| 141 | + if (this.Slot == this.NumSlots - 1) |
| 142 | + { |
| 143 | + this.seekBarFilledBorder.CornerRadius = new CornerRadius(5); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + this.seekBarFilledBorder.CornerRadius = new CornerRadius(5,0,0,5); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void UpdateMarkers() |
| 152 | + { |
| 153 | + if (this.NumSlots <= 0) |
| 154 | + { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + this.markersGrid.ColumnDefinitions.Clear(); |
| 159 | + this.markersGrid.Children.Clear(); |
| 160 | + |
| 161 | + this.markersGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.5, GridUnitType.Star) }); |
| 162 | + |
| 163 | + for (int i = 0; i < this.NumSlots - 2; i++) |
| 164 | + { |
| 165 | + this.markersGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0, GridUnitType.Star) }); |
| 166 | + var marker = new Polygon |
| 167 | + { |
| 168 | + Style = this.FindResource("SeekBarMarker") as Style |
| 169 | + }; |
| 170 | + |
| 171 | + Grid.SetColumn(marker, i + 1); |
| 172 | + |
| 173 | + this.markersGrid.Children.Add(marker); |
| 174 | + } |
| 175 | + |
| 176 | + this.markersGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.5, GridUnitType.Star) }); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments