aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Visuals/VUMeter/VUMeter.xaml.cs
blob: a3a6c4e409b9aaab4ff157318f37e7caba6d284c (plain)
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
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
{
    /// <summary>
    /// Interaction logic for VUMeter.xaml
    /// </summary>
    public partial class VUMeter : UserControl
    {
        private const double RED_PRECENTAGE = 0.1;
        private const double YELLOW_PRECENTAGE = 0.2;

        public VUMeter()
        {
            Leds = new ObservableCollection<VULed>();
            CreateLeds();
            InitializeComponent();
        }

        internal ObservableCollection<VULed> Leds
        {
            get { return (ObservableCollection<VULed>)GetValue(LedsProperty); }
            set { SetValue(LedsProperty, value); }
        }
        internal static readonly DependencyProperty LedsProperty =
            DependencyProperty.Register("Leds", typeof(ObservableCollection<VULed>), 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();
        }
    }
}