using System.Globalization; namespace QuikDawEditor; public partial class PlayGridHeader : UserControl { public PlayGridHeader() { InitializeComponent(); } FormattedText fText = null; Typeface typefaceMeasure = new Typeface(new FontFamily("Verdana"), FontStyles.Normal, FontWeights.Normal, FontStretches.Condensed); Typeface typefaceBeat = new Typeface(new FontFamily("Verdana"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); Pen linePenMeasure = new Pen(Brushes.White, 1); Pen linePenBeat = new Pen(Brushes.White, 0.6); SolidColorBrush NumbersBrush = Brushes.White; private double ContinueTo = 100 + 20; private double ContinueToB = 100 + 20; double beatsPerSecond { get { return BeatsPerMinute / 60; } } public void UpdateGridVisual() { ContinueTo = Math.Max(editingProject.projectEndPointMs / 1000 + 20, PixelsToMsec(this.ActualWidth) / 1000 / editingProject.ViewXZoomFac); ContinueToB = Math.Max(editingProject.projectEndPointMs / 1000 + 20, PixelsToMsec(this.ActualWidth) / 1000 / editingProject.ViewXZoomFac) * beatsPerSecond; this.InvalidateVisual(); } protected override void OnRender(DrawingContext dc) { if (editingProject == null) return; switch (editingProject.IsTimePlayScale) { case true: //TimeScale double lineStart = this.ActualHeight / 2; double textY = this.ActualHeight / 2; int stride = editingProject.ViewXZoomFac < 0.6 ? 40 : (editingProject.ViewXZoomFac < 1 ? 20 : 10); int midStride = stride / 2; bool showMid = editingProject.ViewXZoomFac >= 1; bool showSecs = editingProject.ViewXZoomFac >= 3; bool showSecNos = editingProject.ViewXZoomFac >= 8; bool showSubSecs = editingProject.ViewXZoomFac > 30; for (int secNo = 1; secNo < ContinueTo; secNo++) { double x = secNo * PixelsPerSecond * editingProject.ViewXZoomFac; int oneBasedNo = secNo + 1; string numString = (secNo + 1).ToString(); numString = TimeSpan.FromSeconds(secNo + 1).ToString().Substring(3, 5); if (oneBasedNo % stride == 0) { textY = 0; dc.DrawLine(linePenMeasure, new Point(x, 0), new Point(x, this.ActualHeight)); fText = new FormattedText(numString, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typefaceMeasure, 9, NumbersBrush, VisualTreeHelper.GetDpi(this).PixelsPerDip); dc.DrawText(fText, new Point(x + 1, textY)); } else { if (showMid) { if (oneBasedNo % midStride == 0) { textY = this.ActualHeight / 2 - 1; fText = new FormattedText(numString, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typefaceBeat, 8, NumbersBrush, VisualTreeHelper.GetDpi(this).PixelsPerDip); dc.DrawText(fText, new Point(x + 1, textY)); lineStart = this.ActualHeight / 2.5; dc.DrawLine(linePenBeat, new Point(x, lineStart), new Point(x, this.ActualHeight)); } } if (showSecs) { lineStart = this.ActualHeight / 1.5; dc.DrawLine(linePenBeat, new Point(x, lineStart), new Point(x, this.ActualHeight)); } if (showSecNos) { textY = this.ActualHeight / 2 - 1; fText = new FormattedText(numString, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typefaceBeat, 8, NumbersBrush, VisualTreeHelper.GetDpi(this).PixelsPerDip); dc.DrawText(fText, new Point(x + 1, textY)); } } if (showSubSecs) { for (int i = 0; i < 10; i++) { lineStart = this.ActualHeight / 1.5; double subxunit = PixelsPerSecond * editingProject.ViewXZoomFac / 10; lineStart = this.ActualHeight / 1.5; dc.DrawLine(linePenBeat, new Point(x + subxunit * i, lineStart), new Point(x + subxunit * i, this.ActualHeight)); } } } break; case false: //BeatScale int measureStride = editingProject.ViewXZoomFac < 0.6 ? 16 : (editingProject.ViewXZoomFac < 1 ? 8 : editingProject.BeatsPerMeasure); double lineHeight = this.ActualHeight; double lineStartB = 0; double textYB = 0; bool showBeats = editingProject.ViewXZoomFac >= 4; string numStringB = ""; for (int beatNo = 1; beatNo < ContinueToB; beatNo++) { double x = MsecToPixelsZoomed((double)beatNo * MillisecondsPerBeat); if (beatNo % measureStride == 0) { lineHeight = this.ActualHeight; textYB = 0; numStringB = Math.Truncate(beatNo / (decimal)editingProject.BeatsPerMeasure + 1).ToString(); dc.DrawLine(linePenMeasure, new Point(x, 0), new Point(x, lineHeight)); fText = new FormattedText(numStringB, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typefaceMeasure, 9, NumbersBrush, VisualTreeHelper.GetDpi(this).PixelsPerDip); dc.DrawText(fText, new Point(x + 1, textYB)); } else { if (showBeats) { textYB = this.ActualHeight / 2 - 1; lineStartB = this.ActualHeight / 2; numStringB = ((beatNo % measureStride) + 1).ToString(); dc.DrawLine(linePenBeat, new Point(x, lineStartB), new Point(x, this.ActualHeight)); fText = new FormattedText(numStringB, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typefaceBeat, 8, NumbersBrush, VisualTreeHelper.GetDpi(this).PixelsPerDip); dc.DrawText(fText, new Point(x, textYB)); } } } break; } } }