using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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;
using Tango.Visuals.Components;
namespace Tango.Visuals
{
///
/// Interaction logic for VUMeter.xaml
///
public partial class VUMeter : UserControl
{
private const double RED_PRECENTAGE = 0.1;
private const double YELLOW_PRECENTAGE = 0.2;
public VUMeter()
{
Leds = new ObservableCollection();
CreateLeds();
InitializeComponent();
}
internal ObservableCollection Leds
{
get { return (ObservableCollection)GetValue(LedsProperty); }
set { SetValue(LedsProperty, value); }
}
internal static readonly DependencyProperty LedsProperty =
DependencyProperty.Register("Leds", typeof(ObservableCollection), typeof(VUMeter), new PropertyMetadata(null));
public int LedCount
{
get { return (int)GetValue(LedCountProperty); }
set { SetValue(LedCountProperty, value); }
}
public static readonly DependencyProperty LedCountProperty =
DependencyProperty.Register("LedCount", typeof(int), typeof(VUMeter), new PropertyMetadata(14, (d, e) => (d as VUMeter).CreateLeds()));
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
public static readonly DependencyProperty MaximumProperty =
DependencyProperty.Register("Maximum", typeof(double), typeof(VUMeter), new PropertyMetadata(1.0, (d, e) => (d as VUMeter).RenderCurrentValue()));
public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(double), typeof(VUMeter), new PropertyMetadata(0.0, (d, e) => (d as VUMeter).RenderCurrentValue()));
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double), typeof(VUMeter), new PropertyMetadata(0.0, (d, e) => (d as VUMeter).RenderCurrentValue()));
public Brush HighBrush
{
get { return (Brush)GetValue(HighBrushProperty); }
set { SetValue(HighBrushProperty, value); }
}
public static readonly DependencyProperty HighBrushProperty =
DependencyProperty.Register("HighBrush", typeof(Brush), typeof(VUMeter), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(255, 81, 3)), (d, e) => (d as VUMeter).CreateLeds()));
public Brush MidBrush
{
get { return (Brush)GetValue(MidBrushProperty); }
set { SetValue(MidBrushProperty, value); }
}
public static readonly DependencyProperty MidBrushProperty =
DependencyProperty.Register("MidBrush", typeof(Brush), typeof(VUMeter), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(255, 254, 82)), (d, e) => (d as VUMeter).CreateLeds()));
public Brush LowBrush
{
get { return (Brush)GetValue(LowBrushProperty); }
set { SetValue(LowBrushProperty, value); }
}
public static readonly DependencyProperty LowBrushProperty =
DependencyProperty.Register("LowBrush", typeof(Brush), typeof(VUMeter), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(80, 254, 0)), (d, e) => (d as VUMeter).CreateLeds()));
public bool UseTexture
{
get { return (bool)GetValue(UseTextureProperty); }
set { SetValue(UseTextureProperty, value); }
}
public static readonly DependencyProperty UseTextureProperty =
DependencyProperty.Register("UseTexture", typeof(bool), typeof(VUMeter), new PropertyMetadata(true));
public bool SunkenLeds
{
get { return (bool)GetValue(SunkenLedsProperty); }
set { SetValue(SunkenLedsProperty, value); }
}
public static readonly DependencyProperty SunkenLedsProperty =
DependencyProperty.Register("SunkenLeds", typeof(bool), typeof(VUMeter), new PropertyMetadata(true));
public bool EmulateLight
{
get { return (bool)GetValue(EmulateLightProperty); }
set { SetValue(EmulateLightProperty, value); }
}
public static readonly DependencyProperty EmulateLightProperty =
DependencyProperty.Register("EmulateLight", typeof(bool), typeof(VUMeter), new PropertyMetadata(true));
public bool EmulateOuterLight
{
get { return (bool)GetValue(EmulateOuterLightProperty); }
set { SetValue(EmulateOuterLightProperty, value); }
}
public static readonly DependencyProperty EmulateOuterLightProperty =
DependencyProperty.Register("EmulateOuterLight", typeof(bool), typeof(VUMeter), new PropertyMetadata(true));
public Thickness LedsMargin
{
get { return (Thickness)GetValue(LedsMarginProperty); }
set { SetValue(LedsMarginProperty, value); }
}
public static readonly DependencyProperty LedsMarginProperty =
DependencyProperty.Register("LedsMargin", typeof(Thickness), typeof(VUMeter), new PropertyMetadata(new Thickness(2, 2, 2, 1)));
public TickPlacement TicksPlacement
{
get { return (TickPlacement)GetValue(TicksPlacementProperty); }
set { SetValue(TicksPlacementProperty, value); }
}
public static readonly DependencyProperty TicksPlacementProperty =
DependencyProperty.Register("TicksPlacement", typeof(TickPlacement), typeof(VUMeter), new PropertyMetadata(TickPlacement.TopLeft));
public double TicksMinimum
{
get { return (double)GetValue(TicksMinimumProperty); }
set { SetValue(TicksMinimumProperty, value); }
}
public static readonly DependencyProperty TicksMinimumProperty =
DependencyProperty.Register("TicksMinimum", typeof(double), typeof(VUMeter), new PropertyMetadata(-60.0));
public double TicksMaximum
{
get { return (double)GetValue(TicksMaximumProperty); }
set { SetValue(TicksMaximumProperty, value); }
}
public static readonly DependencyProperty TicksMaximumProperty =
DependencyProperty.Register("TicksMaximum", typeof(double), typeof(VUMeter), new PropertyMetadata(8.0));
public int TicksCount
{
get { return (int)GetValue(TicksCountProperty); }
set { SetValue(TicksCountProperty, value); }
}
public static readonly DependencyProperty TicksCountProperty =
DependencyProperty.Register("TicksCount", typeof(int), typeof(VUMeter), new PropertyMetadata(10));
public String TicksFormat
{
get { return (String)GetValue(TicksFormatProperty); }
set { SetValue(TicksFormatProperty, value); }
}
public static readonly DependencyProperty TicksFormatProperty =
DependencyProperty.Register("TicksFormat", typeof(String), typeof(VUMeter), new PropertyMetadata("0"));
public bool ShowTicksLines
{
get { return (bool)GetValue(ShowTicksLinesProperty); }
set { SetValue(ShowTicksLinesProperty, value); }
}
public static readonly DependencyProperty ShowTicksLinesProperty =
DependencyProperty.Register("ShowTicksLines", typeof(bool), typeof(VUMeter), new PropertyMetadata(false));
public bool ShowTicksLabels
{
get { return (bool)GetValue(ShowTicksLabelsProperty); }
set { SetValue(ShowTicksLabelsProperty, value); }
}
public static readonly DependencyProperty ShowTicksLabelsProperty =
DependencyProperty.Register("ShowTicksLabels", typeof(bool), typeof(VUMeter), new PropertyMetadata(false));
public double LedCornerRadius
{
get { return (double)GetValue(LedCornerRadiusProperty); }
set { SetValue(LedCornerRadiusProperty, value); }
}
public static readonly DependencyProperty LedCornerRadiusProperty =
DependencyProperty.Register("LedCornerRadius", typeof(double), typeof(VUMeter), new PropertyMetadata(2.0));
public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register("Orientation", typeof(Orientation), typeof(VUMeter), new PropertyMetadata(Orientation.Vertical));
public Brush LedBackground
{
get { return (Brush)GetValue(LedBackgroundProperty); }
set { SetValue(LedBackgroundProperty, value); }
}
public static readonly DependencyProperty LedBackgroundProperty =
DependencyProperty.Register("LedBackground", typeof(Brush), typeof(VUMeter), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(15, 15, 15))));
private void RenderCurrentValue()
{
double percentage = (Value - Minimum) / (Maximum - Minimum);
int ledsToTurn = (int)((double)LedCount - (LedCount * percentage));
for (int i = LedCount - 1; i >= 0; i--)
{
Leds[i].On = i >= ledsToTurn;
}
}
private void CreateLeds()
{
Leds.Clear();
for (int i = 0; i < LedCount * RED_PRECENTAGE; i++)
{
Leds.Add(new VULed() { Brush = HighBrush });
}
for (int i = 0; i < LedCount * YELLOW_PRECENTAGE; i++)
{
Leds.Add(new VULed() { Brush = MidBrush });
}
int remaining = LedCount - Leds.Count;
for (int i = 0; i < remaining; i++)
{
Leds.Add(new VULed() { Brush = LowBrush });
}
RenderCurrentValue();
}
}
}