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 Tango.Visuals { /// /// Interaction logic for Fader.xaml /// public partial class Fader : UserControl { public Fader() { InitializeComponent(); this.Loaded += Fader_Loaded; this.SizeChanged += Fader_SizeChanged; } private void Fader_SizeChanged(object sender, SizeChangedEventArgs e) { SetSize(); } private void SetSize() { border.Margin = new Thickness(0, gridThumb.ActualHeight / 2, 0, gridThumb.ActualHeight / 2); } private void Fader_Loaded(object sender, RoutedEventArgs e) { SetSize(); OnValueChanged(); } public Brush TrackBrush { get { return (Brush)GetValue(TrackBrushProperty); } set { SetValue(TrackBrushProperty, value); } } public static readonly DependencyProperty TrackBrushProperty = DependencyProperty.Register("TrackBrush", typeof(Brush), typeof(Fader), new PropertyMetadata(null)); public Brush TrackHighlighBrush { get { return (Brush)GetValue(TrackHighlighBrushProperty); } set { SetValue(TrackHighlighBrushProperty, value); } } public static readonly DependencyProperty TrackHighlighBrushProperty = DependencyProperty.Register("TrackHighlighBrush", typeof(Brush), typeof(Fader), new PropertyMetadata(null)); public bool HighlightTrack { get { return (bool)GetValue(HighlightTrackProperty); } set { SetValue(HighlightTrackProperty, value); } } public static readonly DependencyProperty HighlightTrackProperty = DependencyProperty.Register("HighlightTrack", typeof(bool), typeof(Fader), new PropertyMetadata(false)); public double Minimum { get { return (double)GetValue(MinimumProperty); } set { SetValue(MinimumProperty, value); } } public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(Fader), new PropertyMetadata(0.0, (d, e) => (d as Fader).OnValueChanged())); public double Maximum { get { return (double)GetValue(MaximumProperty); } set { SetValue(MaximumProperty, value); } } public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(Fader), new PropertyMetadata(100.0, (d, e) => (d as Fader).OnValueChanged())); public double Value { get { return (double)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(Fader), new FrameworkPropertyMetadata(0.0, (d, e) => (d as Fader).OnValueChanged(), (d, baseValue) => (d as Fader).OnCoerceValue((double)baseValue)) { BindsTwoWayByDefault = true }); public bool SnapToValues { get { return (bool)GetValue(SnapToValuesProperty); } set { SetValue(SnapToValuesProperty, value); } } public static readonly DependencyProperty SnapToValuesProperty = DependencyProperty.Register("SnapToValues", typeof(bool), typeof(Fader), new PropertyMetadata(false)); public int SnapPercision { get { return (int)GetValue(SnapPercisionProperty); } set { SetValue(SnapPercisionProperty, value); } } public static readonly DependencyProperty SnapPercisionProperty = DependencyProperty.Register("SnapPercision", typeof(int), typeof(Fader), new PropertyMetadata(1)); public int Ticks { get { return (int)GetValue(TicksProperty); } set { SetValue(TicksProperty, value); } } public static readonly DependencyProperty TicksProperty = DependencyProperty.Register("Ticks", typeof(int), typeof(Fader), new PropertyMetadata(50)); public int? BigTicksInterval { get { return (int?)GetValue(BigTicksIntervalProperty); } set { SetValue(BigTicksIntervalProperty, value); } } public static readonly DependencyProperty BigTicksIntervalProperty = DependencyProperty.Register("BigTicksInterval", typeof(int?), typeof(Fader), new PropertyMetadata(5)); public Brush TicksBrush { get { return (Brush)GetValue(TicksBrushProperty); } set { SetValue(TicksBrushProperty, value); } } public static readonly DependencyProperty TicksBrushProperty = DependencyProperty.Register("TicksBrush", typeof(Brush), typeof(Fader), new PropertyMetadata(null)); public Brush BigTicksBrush { get { return (Brush)GetValue(BigTicksBrushProperty); } set { SetValue(BigTicksBrushProperty, value); } } public static readonly DependencyProperty BigTicksBrushProperty = DependencyProperty.Register("BigTicksBrush", typeof(Brush), typeof(Fader), new PropertyMetadata(null)); public double FaderWidth { get { return (double)GetValue(FaderWidthProperty); } set { SetValue(FaderWidthProperty, value); } } public static readonly DependencyProperty FaderWidthProperty = DependencyProperty.Register("FaderWidth", typeof(double), typeof(Fader), new PropertyMetadata(40.0)); public double TrackWidth { get { return (double)GetValue(TrackWidthProperty); } set { SetValue(TrackWidthProperty, value); } } public static readonly DependencyProperty TrackWidthProperty = DependencyProperty.Register("TrackWidth", typeof(double), typeof(Fader), new PropertyMetadata(10.0)); public bool ShowTicks { get { return (bool)GetValue(ShowTicksProperty); } set { SetValue(ShowTicksProperty, value); } } public static readonly DependencyProperty ShowTicksProperty = DependencyProperty.Register("ShowTicks", typeof(bool), typeof(Fader), new PropertyMetadata(true)); public Color? FaderColor { get { return (Color?)GetValue(FaderColorProperty); } set { SetValue(FaderColorProperty, value); } } public static readonly DependencyProperty FaderColorProperty = DependencyProperty.Register("FaderColor", typeof(Color?), typeof(Fader), new PropertyMetadata(null)); public double TicksWidth { get { return (double)GetValue(TicksWidthProperty); } set { SetValue(TicksWidthProperty, value); } } public static readonly DependencyProperty TicksWidthProperty = DependencyProperty.Register("TicksWidth", typeof(double), typeof(Fader), new PropertyMetadata(8.0)); public double BigTicksWidth { get { return (double)GetValue(BigTicksWidthProperty); } set { SetValue(BigTicksWidthProperty, value); } } public static readonly DependencyProperty BigTicksWidthProperty = DependencyProperty.Register("BigTicksWidth", typeof(double), typeof(Fader), new PropertyMetadata(20.0)); public Brush FaderLightBrush { get { return (Brush)GetValue(FaderLightBrushProperty); } set { SetValue(FaderLightBrushProperty, value); } } public static readonly DependencyProperty FaderLightBrushProperty = DependencyProperty.Register("FaderLightBrush", typeof(Brush), typeof(Fader), new PropertyMetadata(null)); public double Change { get { return (double)GetValue(ChangeProperty); } set { SetValue(ChangeProperty, value); } } public static readonly DependencyProperty ChangeProperty = DependencyProperty.Register("Change", typeof(double), typeof(Fader), new PropertyMetadata(0.01)); internal bool IsKeyDown { get { return (bool)GetValue(IsKeyDownProperty); } set { SetValue(IsKeyDownProperty, value); } } internal static readonly DependencyProperty IsKeyDownProperty = DependencyProperty.Register("IsKeyDown", typeof(bool), typeof(Fader), new PropertyMetadata(false)); private object OnCoerceValue(double value) { if (value > Maximum) return Maximum; if (value < Minimum) return Minimum; return SnapToValues ? Math.Round(value, SnapPercision) : value; } private void OnValueChanged() { double percentage = (Value - Minimum) / (Maximum - Minimum); double faderHeight = gridThumb.ActualHeight; faderTranslate.Y = -((this.ActualHeight - faderHeight) * percentage); borderHighLight.Height = Math.Abs(faderTranslate.Y); } private void OnThumbDrag(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { double value = (faderTranslate.Y + e.VerticalChange); double faderHeight = gridThumb.ActualHeight; if (value > 0) value = 0; if (value < -(this.ActualHeight - faderHeight)) { value = -(this.ActualHeight - faderHeight); } faderTranslate.Y = value; borderHighLight.Height = Math.Abs(faderTranslate.Y); double percentage = Math.Abs(value / (this.ActualHeight - faderHeight)); Value = (Minimum + (Maximum - Minimum) * percentage); } private void OnKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Down) { Value -= Change; } else if (e.Key == Key.Up) { Value += Change; } Focus(); IsKeyDown = true; e.Handled = true; } private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { Focus(); } private void OnKeyUp(object sender, KeyEventArgs e) { IsKeyDown = false; } } }