JD2022-TU1/main/tools/framework/JD.Timeline/Karaoke/LyricsControl.xaml.cs

112 lines
3.5 KiB
C#

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using JD.Collections.Extensions;
using JD.Timeline.Model;
using JD.Timeline.View.ClipView;
using TeaBox;
namespace JD.Timeline.View.Pads
{
/// <summary>
/// Interaction logic for LyricsControl.xaml
/// </summary>
public partial class LyricsControl
{
private ClipViewControl _clipViewControl;
private bool _mouseEnteredPressed;
public LyricsControl()
{
InitializeComponent();
}
public ClipViewControl ClipViewControl
{
get { return _clipViewControl; }
set
{
if (_clipViewControl != null)
{
_clipViewControl.Timeline.CurrentTimeChanged -= Timeline_CurrentTimeChanged;
}
_clipViewControl = value;
if (value != null)
{
_lyrics.DataContext = _clipViewControl.Tape.Lyrics();
_clipViewControl.Timeline.CurrentTimeChanged += Timeline_CurrentTimeChanged;
}
else
{
_lyrics.DataContext = null;
}
}
}
void Timeline_CurrentTimeChanged(object sender, EventArgs e)
{
var lyrics = _clipViewControl.Tape.Lyrics();
var time = _clipViewControl.Timeline.TimeCursor.Time;
var currentWord = lyrics.AllWords().Where(x => x.AssociatedClip != null).LastOrDefault(x => x.TimeRange().Min <= time);
lyrics.AllWords().ForEach(x => x.IsCurrentWord = (x == currentWord));
if (currentWord != null)
{
ScrollToWord(currentWord);
}
}
public void ScrollToWord(LyricWord word)
{
var itemcontainer = _itemsControl.ItemContainerGenerator.ContainerFromItem(word.Parent.Parent) as ContentPresenter;
if (itemcontainer != null)
{
itemcontainer.BringIntoView();
}
}
// Avoid conflicting with dragged Avalon pad
private void Paragraph_MouseEnter(object sender, MouseEventArgs e)
{
this._mouseEnteredPressed = e.LeftButton == MouseButtonState.Pressed;
}
private void Paragraph_MouseMove(object sender, MouseEventArgs e)
{
var control = (ItemsControl)sender;
var paragraph = (LyricParagraph)control.DataContext;
if (e.LeftButton == MouseButtonState.Pressed
&& !_mouseEnteredPressed
&& paragraph != null)
{
TeaBox.ObjectModel.Application.Main.Dispatcher.DispatchAsync(() =>
{
DragDrop.DoDragDrop(control, new DataObject(DataFormats.Serializable, paragraph), DragDropEffects.Copy);
}, true);
}
}
private void Line_MouseMove(object sender, MouseEventArgs e)
{
}
private void Word_MouseDown(object sender, MouseButtonEventArgs e)
{
var control = (StackPanel)sender;
var word = (LyricWord)control.DataContext;
if (word != null && word.AssociatedClip != null)
{
ClipViewControl.ClearSelection();
ClipViewControl.AddToSelection(word.AssociatedClip);
ClipViewControl.CenterView(word.AssociatedClip);
}
}
}
}