using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text.Json.Serialization; using System.Windows; using System.Windows.Documents; using System.Windows.Media; using static QuikDawEditor.EDITING.MiscMethods; using static QuikDawEditor.EDITING.StaticProperties; namespace QuikDawEditor.EditingClasses; public class LyricLine : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } [JsonIgnore(Condition = JsonIgnoreCondition.Always)] public ObservableCollection LineInlines { get; set; } = new ObservableCollection(); [JsonConstructor] public LyricLine(string LineXaml, bool IsShowLine) { this.LineXaml = LineXaml; CreateInlinesFromXaml(); this.IsShowLine = IsShowLine; } public LyricLine(Paragraph p) { myPar = p; LineXaml = GetMinimalXaml(new TextRange(p.ContentStart, p.ContentEnd)); IsShowLine = p.Tag == "ShowLine"; CreateInlinesFromPar(); } public Paragraph myPar = new Paragraph(); private double _timePosMs; public double timePosMs { get { return _timePosMs; } set { _timePosMs = value; UpdateLyricLine(); int thisIdx = editingProject.shownLyricLines.IndexOf(this); if (thisIdx > 0) editingProject.shownLyricLines[thisIdx - 1].UpdateLyricLine(); } } internal void UpdateLyricLine() { NotifyPropertyChanged(nameof(lyricLineMargin)); NotifyPropertyChanged(nameof(lyricLineWidth)); } internal void UpdateVisibility() { NotifyPropertyChanged(nameof(LyricLineVisibility)); NotifyPropertyChanged(nameof(lyricLineWidth)); } public Thickness lyricLineMargin { get { return new Thickness(MsecToPixelsZoomed(timePosMs), 0, 0, 0); } } public double lyricLineWidth { get { int thisIdx = editingProject.shownLyricLines.IndexOf(this); return thisIdx == editingProject.shownLyricLines.Count - 1 ? 10000D : Math.Max(editingProject.shownLyricLines[thisIdx + 1].lyricLineMargin.Left - lyricLineMargin.Left, 0); } } public string DisplayInfo { get { return "\"" + AllText + "\""; } } public string AllText { get { return new TextRange(myPar.ContentStart, myPar.ContentEnd).Text; } } internal bool ShowOnLyricLane { get { return LineInlines.Count > 0 && AllText != "" && IsShowLine; } } public Visibility LyricLineVisibility { get { return ShowOnLyricLane ? Visibility.Visible : Visibility.Hidden; } } public string LineXaml { get; set; } private bool _IsShowLine = true; public bool IsShowLine { get { return _IsShowLine; } set { _IsShowLine = value; editingProject.shownLyricLines = editingProject.LyricLines.Where(ll => ll.ShowOnLyricLane).ToList(); double fontSize = _IsShowLine ? 18 : 16; myPar.FontSize = fontSize; foreach (Inline iline in myPar.Inlines) { iline.Foreground = new SolidColorBrush(((SolidColorBrush)iline.Foreground).Color) { Opacity = _IsShowLine ? 1 : 0.5 }; iline.FontSize = fontSize; } myPar.Tag = _IsShowLine ? "ShowLine" : ""; UpdateVisibility(); } } public void CreateInlinesFromXaml() { LineInlines.Clear(); // This sets myPar content TextRange parRange = new TextRange(myPar.ContentStart, myPar.ContentEnd); SetRangeXaml(ref parRange, LineXaml); if (parRange.Text != "") GetInlines(); UpdateVisibility(); } public void CreateInlinesFromPar() { LineInlines.Clear(); GetInlines(); UpdateVisibility(); if (ShowOnLyricLane && !editingProject.shownLyricLines.Contains(this)) { //newly added text making it newly shown editingProject.shownLyricLines = editingProject.LyricLines.Where(ll => ll.ShowOnLyricLane).ToList(); int myidx = editingProject.shownLyricLines.IndexOf(this); if (myidx < editingProject.shownLyricLines.Count - 1) editingProject.shownLyricLines[myidx + 1].timePosMs += minLyricLineWidthMs; } } private void GetInlines() { foreach (Inline iline in myPar.Inlines) { if (iline.GetType() == typeof(Run)) { Run r = (Run)iline; LineInlines.Add(new Run(r.Text) { Background = r.Background, FontWeight = r.FontWeight, Foreground = r.Foreground, FontStyle = r.FontStyle, TextDecorations = r.TextDecorations }); } } } }