mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-26 04:21:10 +01:00
Added: Encoding quality slider user control.
This commit is contained in:
1 parent
d7f25f6ab2
commit
00d380f92e
2 files changed
+189
No files matched your search
@@ -0,0 +1,26 @@
|
||||
<UserControl
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:valueConverters="clr-namespace:FileConverter.ValueConverters" x:Name="userControl"
|
||||
x:Class="FileConverter.Controls.EncodingQualitySliderControl"
|
||||
mc:Ignorable="d" d:DesignWidth="300" Height="50">
|
||||
<UserControl.Resources>
|
||||
<valueConverters:BitrateToString x:Key="BitrateToString"/>
|
||||
</UserControl.Resources>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="65"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Slider Grid.Column="0" x:Name="slider" Margin="0,2,0,0" VerticalAlignment="Top" Maximum="245" AutoToolTipPlacement="TopLeft" SmallChange="8" IsSnapToTickEnabled="True" IsSelectionRangeEnabled="True" SelectionStart="115" SelectionEnd="245" Minimum="65" TickPlacement="BottomRight" LargeChange="32" TickFrequency="8" Ticks="65 85 100 115 130 165 175 190 225 245" Value="{Binding Bitrate, ElementName=userControl, Mode=TwoWay}" Foreground="#004B82"/>
|
||||
<Label Grid.Column="1" Content="{Binding Value, Converter={StaticResource BitrateToString}, ElementName=slider}" Width="65" />
|
||||
<Label Grid.Column="0" Grid.Row="1" Content="Recommended bitrate range in blue" Foreground="#E5004B82" FontSize="10.667" />
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,163 @@
|
||||
// <copyright file="EncodingQualitySliderControl.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
|
||||
|
||||
namespace FileConverter.Controls
|
||||
{
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for EncodingQualitySliderControl.xaml
|
||||
/// </summary>
|
||||
public partial class EncodingQualitySliderControl : UserControl, INotifyPropertyChanged
|
||||
{
|
||||
public static readonly DependencyProperty EncodingModeProperty = DependencyProperty.Register(
|
||||
"EncodingMode",
|
||||
typeof(EncodingMode),
|
||||
typeof(EncodingQualitySliderControl),
|
||||
null);
|
||||
|
||||
public static readonly DependencyProperty BitrateProperty = DependencyProperty.Register(
|
||||
"Bitrate",
|
||||
typeof(double),
|
||||
typeof(EncodingQualitySliderControl),
|
||||
null);
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
[Category("Behavior")]
|
||||
public event RoutedPropertyChangedEventHandler<double> ValueChanged;
|
||||
|
||||
public EncodingQualitySliderControl()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
|
||||
(this.Content as FrameworkElement).DataContext = this;
|
||||
|
||||
this.Bitrate = this.GetNearestTickValue(this.Bitrate);
|
||||
|
||||
this.slider.ValueChanged += Slider_ValueChanged;
|
||||
|
||||
DependencyPropertyDescriptor encodingModeDescriptor = DependencyPropertyDescriptor.FromProperty(
|
||||
EncodingQualitySliderControl.EncodingModeProperty,
|
||||
typeof(EncodingQualitySliderControl));
|
||||
|
||||
encodingModeDescriptor.AddValueChanged(this, this.EncodingModeValueChanged);
|
||||
}
|
||||
|
||||
public EncodingMode EncodingMode
|
||||
{
|
||||
get
|
||||
{
|
||||
return (EncodingMode)this.GetValue(EncodingQualitySliderControl.EncodingModeProperty);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
this.SetValueDependencyProperty(EncodingQualitySliderControl.EncodingModeProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
public double Bitrate
|
||||
{
|
||||
get
|
||||
{
|
||||
return (double)this.GetValue(EncodingQualitySliderControl.BitrateProperty);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
//this.SetValue(EncodingQualitySliderControl.BitrateProperty, value);
|
||||
this.SetValueDependencyProperty(EncodingQualitySliderControl.BitrateProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
|
||||
{
|
||||
this.ValueChanged?.Invoke(this, e);
|
||||
}
|
||||
|
||||
private void SetValueDependencyProperty(DependencyProperty dependencyProperty, object value, [CallerMemberName] string propertyName = null)
|
||||
{
|
||||
this.SetValue(dependencyProperty, value);
|
||||
|
||||
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
private void EncodingModeValueChanged(object sender, EventArgs eventArgs)
|
||||
{
|
||||
double previousValue = this.Bitrate;
|
||||
|
||||
switch (this.EncodingMode)
|
||||
{
|
||||
case EncodingMode.VBR:
|
||||
{
|
||||
this.slider.Minimum = 65;
|
||||
this.slider.Maximum = 245;
|
||||
this.slider.SelectionStart = 115;
|
||||
this.slider.SelectionEnd = 245;
|
||||
this.slider.Ticks.Clear();
|
||||
this.slider.Ticks.Add(65);
|
||||
this.slider.Ticks.Add(85);
|
||||
this.slider.Ticks.Add(100);
|
||||
this.slider.Ticks.Add(115);
|
||||
this.slider.Ticks.Add(130);
|
||||
this.slider.Ticks.Add(165);
|
||||
this.slider.Ticks.Add(175);
|
||||
this.slider.Ticks.Add(190);
|
||||
this.slider.Ticks.Add(225);
|
||||
this.slider.Ticks.Add(245);
|
||||
break;
|
||||
}
|
||||
|
||||
case EncodingMode.CBR:
|
||||
{
|
||||
this.slider.Minimum = 8;
|
||||
this.slider.Maximum = 320;
|
||||
this.slider.SelectionStart = 128;
|
||||
this.slider.SelectionEnd = 256;
|
||||
this.slider.Ticks.Clear();
|
||||
this.slider.Ticks.Add(8);
|
||||
this.slider.Ticks.Add(16);
|
||||
this.slider.Ticks.Add(24);
|
||||
this.slider.Ticks.Add(32);
|
||||
this.slider.Ticks.Add(40);
|
||||
this.slider.Ticks.Add(48);
|
||||
this.slider.Ticks.Add(64);
|
||||
this.slider.Ticks.Add(80);
|
||||
this.slider.Ticks.Add(96);
|
||||
this.slider.Ticks.Add(112);
|
||||
this.slider.Ticks.Add(128);
|
||||
this.slider.Ticks.Add(160);
|
||||
this.slider.Ticks.Add(192);
|
||||
this.slider.Ticks.Add(224);
|
||||
this.slider.Ticks.Add(256);
|
||||
this.slider.Ticks.Add(320);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.Bitrate = this.GetNearestTickValue(previousValue);
|
||||
}
|
||||
|
||||
private double GetNearestTickValue(double value)
|
||||
{
|
||||
double minimumDifference = float.PositiveInfinity;
|
||||
double nearestTick = value;
|
||||
for (int index = 0; index < this.slider.Ticks.Count; index++)
|
||||
{
|
||||
double tick = this.slider.Ticks[index];
|
||||
double difference = System.Math.Abs(value - tick);
|
||||
if (difference < minimumDifference)
|
||||
{
|
||||
minimumDifference = difference;
|
||||
nearestTick = tick;
|
||||
}
|
||||
}
|
||||
|
||||
return nearestTick;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user