Commits

Mark Heath committed abd52f2

can now also draw waveforms with vertical bars

Comments (0)

Files changed (3)

WpfWaveform/MainWindow.xaml

         xmlns:local="clr-namespace:WpfWaveform"
         xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
         xmlns:toolkitx="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended"
-        Title="MainWindow" Height="350" Width="525">
+        Title="MainWindow" Height="345" Width="606">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="Auto" />
         <StackPanel Orientation="Horizontal" Margin="5">
             <Button Content="Load" Width="80" Click="Button_Click" />
             <CheckBox VerticalAlignment="Center" Margin="5,0" Content="Outline" Height="16" Name="checkBoxOutline" />
-            <CheckBox VerticalAlignment="Center" Margin="5,0" Content="Bezier" Height="16" Name="checkBoxBezier" />
+            <RadioButton VerticalAlignment="Center" Margin="5,0" Name="radioButtonBezier" Content="Bezier" />
+            <RadioButton VerticalAlignment="Center" Margin="5,0" Name="radioButtonLinear" Content="Linear" />
+            <RadioButton VerticalAlignment="Center" Margin="5,0" Name="radioButtonVertical" Content="Vertical" />
             <Label VerticalAlignment="Center">Fill:</Label>
             <toolkitx:ColorPicker Name="colorPickerFill" />
             <Label VerticalAlignment="Center">Outline:</Label>

WpfWaveform/MainWindow.xaml.cs

         public MainWindow()
         {
             InitializeComponent();
-            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
-            this.checkBoxBezier.IsChecked = true;
+            this.radioButtonBezier.IsChecked = true;
             this.colorPickerFill.SelectedColor = Colors.LightGray;
             this.colorPickerOutline.SelectedColor = Colors.SlateGray;
+
+            this.colorPickerFill.SelectedColorChanged += OnDrawingPropertyChanged;
+            this.colorPickerOutline.SelectedColorChanged += OnDrawingPropertyChanged;
+            this.radioButtonBezier.Click += OnDrawingPropertyChanged;
+            this.radioButtonLinear.Click += OnDrawingPropertyChanged;
+            this.radioButtonVertical.Click += OnDrawingPropertyChanged;
+            this.checkBoxOutline.Click += OnDrawingPropertyChanged;
         }
 
-        void MainWindow_Loaded(object sender, RoutedEventArgs e)
+        void OnDrawingPropertyChanged(object sender, RoutedEventArgs e)
         {
-
+            if (this.mipMap != null)
+            {
+                GenerateWaveform();
+            }
         }
 
         private void Button_Click(object sender, RoutedEventArgs e)
             bool? result = ofd.ShowDialog();
             if (result.HasValue && result.Value)
             {
-                GenerateWaveform(ofd.FileName);
+                var generator = new WaveFormPointsGenerator();
+                this.mipMap = generator.GetPeaks(ofd.FileName, 4100);
+                GenerateWaveform();
             }
         }
 
-        private void GenerateWaveform(string fileName)
+        private MipMap mipMap;
+
+        private void GenerateWaveform()
         {
             var generator = new WaveFormPointsGenerator();
-            var mipMap = generator.GetPeaks(fileName, 4100);
-            Brush strokeBrush = (checkBoxOutline.IsChecked.Value) ? new SolidColorBrush(colorPickerOutline.SelectedColor) : null;
+            Brush strokeBrush = new SolidColorBrush(colorPickerOutline.SelectedColor);
             Brush fillBrush = new SolidColorBrush(colorPickerFill.SelectedColor);
             canvas.Children.Clear();
-
             var topPoints = mipMap.Peaks.Select(p => p.Channels[0].Max / 32768.0);
             var bottomPoints = mipMap.Peaks.Select(p => p.Channels[0].Min / 32768.0);
 
-            var topPath = checkBoxBezier.IsChecked.Value ? generator.GetBezierPath(topPoints, 0, 2, 110, -100, strokeBrush, fillBrush) : generator.GetLinearPath(topPoints, 0, 2, 110, -100, strokeBrush, fillBrush);
-            var bottomPath = checkBoxBezier.IsChecked.Value ? generator.GetBezierPath(bottomPoints, 0, 2, 110, -100, strokeBrush, fillBrush) : generator.GetLinearPath(bottomPoints, 0, 2, 110, -100, strokeBrush, fillBrush);
-            canvas.Children.Add(topPath);
-            canvas.Children.Add(bottomPath);
+            if (radioButtonVertical.IsChecked.Value)
+            {
+                var path = generator.GetAsVerticalLines(topPoints, bottomPoints, 0, 110, -100, strokeBrush);
+                canvas.Children.Add(path);
+            }
+            else
+            {
+                if (!checkBoxOutline.IsChecked.Value) strokeBrush = null;
+                var topPath = radioButtonBezier.IsChecked.Value ? generator.GetBezierPath(topPoints, 0, 2, 110, -100, strokeBrush, fillBrush) : generator.GetLinearPath(topPoints, 0, 2, 110, -100, strokeBrush, fillBrush);
+                var bottomPath = radioButtonBezier.IsChecked.Value ? generator.GetBezierPath(bottomPoints, 0, 2, 110, -100, strokeBrush, fillBrush) : generator.GetLinearPath(bottomPoints, 0, 2, 110, -100, strokeBrush, fillBrush);
+                canvas.Children.Add(topPath);
+                canvas.Children.Add(bottomPath);
+            }
         }
-
-
     }
 }

WpfWaveform/WaveFormPointsGenerator.cs

             }
         }
 
+        public Path GetAsVerticalLines(IEnumerable<double> min, IEnumerable<double> max, double xOffset, double yOffset, double yMult, Brush stroke)
+        {
+            double xStep = 1.0; // force 1.0 for this
+            var minPoints = GetPoints(min, xOffset, xStep, yOffset, yMult).ToArray();
+            var maxPoints = GetPoints(max, xOffset, xStep, yOffset, yMult).ToArray();
+            List<PathFigure> lines = new List<PathFigure>(minPoints.Length);
+            for (int index = 0; index < minPoints.Length; index++)
+            {
+                var line = new LineSegment(maxPoints[index], true);
+                PathFigure figure = new PathFigure(minPoints[index], new PathSegment[] { line }, false);
+                lines.Add(figure);
+            }
+            
+            PathGeometry geometry = new PathGeometry(lines);
+            var path = new Path() { Stroke = stroke, StrokeThickness = 1, Data = geometry, SnapsToDevicePixels=true };
+            return path;
+        }
+
         public Path GetBezierPath(IEnumerable<double> magnitude, double xOffset, double xStep, double yOffset, double yMult, Brush stroke, Brush fill)
         {
-            var points = GetPoints(magnitude, 0, 2, 110, -100).ToArray();
+            var points = GetPoints(magnitude, xOffset, xStep, yOffset, yMult).ToArray();
             var geometry = GetBezierPathGeometry(points);
             var path = new Path() { Stroke = stroke, StrokeThickness = 1, Data = geometry, Fill = fill };
             return path;
 
         public Path GetLinearPath(IEnumerable<double> magnitude, double xOffset, double xStep, double yOffset, double yMult, Brush stroke, Brush fill)
         {
-            var points = GetPoints(magnitude, 0, 2, 110, -100).ToArray();
+            var points = GetPoints(magnitude, xOffset, xStep, yOffset, yMult).ToArray();
             var geometry = GetLinearPathGeometry(points);
             var path = new Path() { Stroke = stroke, StrokeThickness = 1, Data = geometry, Fill = fill };
             return path;