VPet/VPet-Simulator.Core/Display/MessageBar.xaml.cs

231 lines
7.2 KiB
C#
Raw Normal View History

2022-12-13 07:10:18 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2023-01-26 15:15:37 +00:00
using System.Threading;
2022-12-13 07:10:18 +00:00
using System.Threading.Tasks;
2023-01-11 13:10:18 +00:00
using System.Timers;
2022-12-13 07:10:18 +00:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
2023-01-26 15:15:37 +00:00
using Timer = System.Timers.Timer;
2022-12-13 07:10:18 +00:00
namespace VPet_Simulator.Core
{
2024-03-11 08:01:33 +00:00
public interface IMassageBar : IDisposable
{
/// <summary>
/// 显示消息
/// </summary>
/// <param name="name">名字</param>
/// <param name="text">内容</param>
/// <param name="graphname">图像名</param>
/// <param name="msgcontent">消息框内容</param>
void Show(string name, string text, string graphname = null, UIElement msgcontent = null);
/// <summary>
/// 强制关闭
/// </summary>
void ForceClose();
/// <summary>
/// 设置位置在桌宠内
/// </summary>
void SetPlaceIN();
/// <summary>
/// 设置位置在桌宠外
/// </summary>
void SetPlaceOUT();
/// <summary>
/// 显示状态
/// </summary>
Visibility Visibility { get; set; }
/// <summary>
/// 该消息框的Control
2024-03-11 08:01:33 +00:00
/// </summary>
Control This { get; }
2024-03-11 08:01:33 +00:00
}
2022-12-13 07:10:18 +00:00
/// <summary>
/// MessageBar.xaml 的交互逻辑
/// </summary>
2024-03-11 08:01:33 +00:00
public partial class MessageBar : UserControl, IDisposable, IMassageBar
2022-12-13 07:10:18 +00:00
{
public Control This => this;
2023-01-26 15:15:37 +00:00
Main m;
public MessageBar(Main m)
2022-12-13 07:10:18 +00:00
{
InitializeComponent();
2023-01-12 22:11:55 +00:00
EndTimer.Elapsed += EndTimer_Elapsed;
2023-01-11 13:10:18 +00:00
ShowTimer.Elapsed += ShowTimer_Elapsed;
2023-01-20 12:42:00 +00:00
CloseTimer.Elapsed += CloseTimer_Elapsed;
2023-01-26 15:15:37 +00:00
this.m = m;
2023-01-11 13:10:18 +00:00
}
2023-01-20 12:42:00 +00:00
private void CloseTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (Dispatcher.Invoke(() => Opacity) <= 0.05)
{
Dispatcher.Invoke(() =>
{
this.Visibility = Visibility.Collapsed;
MessageBoxContent.Children.Clear();
});
2023-01-20 12:42:00 +00:00
EndAction?.Invoke();
2023-01-26 15:15:37 +00:00
}
else
2023-01-20 12:42:00 +00:00
{
Dispatcher.Invoke(() => Opacity -= 0.02);
}
}
2023-01-12 22:11:55 +00:00
List<char> outputtext;
2023-01-11 13:10:18 +00:00
private void ShowTimer_Elapsed(object sender, ElapsedEventArgs e)
2023-01-12 22:11:55 +00:00
{
if (outputtext.Count > 0)
{
2023-01-28 06:12:01 +00:00
var str = outputtext[0];
outputtext.RemoveAt(0);
Dispatcher.Invoke(() => { TText.Text += str; });
2023-01-12 22:11:55 +00:00
}
else
{
2023-05-25 17:50:38 +00:00
if (m.PlayingVoice)
{
2023-09-17 12:42:23 +00:00
if (m.windowMediaPlayerAvailable)
2023-05-25 17:50:38 +00:00
{
2023-09-17 12:42:23 +00:00
TimeSpan ts = Dispatcher.Invoke(() => m.VoicePlayer?.Clock?.NaturalDuration.HasTimeSpan == true ? (m.VoicePlayer.Clock.NaturalDuration.TimeSpan - m.VoicePlayer.Clock.CurrentTime.Value) : TimeSpan.Zero);
if (ts.TotalSeconds > 2)
{
return;
}
else
{
Console.WriteLine(1);
}
2023-05-25 17:50:38 +00:00
}
else
{
2023-09-17 12:42:23 +00:00
if (m.soundPlayer.IsLoadCompleted)
{
m.PlayingVoice = false;
m.soundPlayer.PlaySync();
}
2023-05-25 17:50:38 +00:00
}
}
2023-01-26 15:15:37 +00:00
Task.Run(() =>
{
2023-02-17 05:33:46 +00:00
Thread.Sleep(timeleft * 50);
2023-07-16 23:58:09 +00:00
if (!string.IsNullOrEmpty(graphName) && m.DisplayType.Type == GraphInfo.GraphType.Say)
m.DisplayCEndtoNomal(graphName);
2023-01-26 15:15:37 +00:00
});
2023-01-12 22:11:55 +00:00
ShowTimer.Stop();
EndTimer.Start();
}
}
2023-01-13 14:45:42 +00:00
public Action EndAction;
2023-01-12 22:11:55 +00:00
private void EndTimer_Elapsed(object sender, ElapsedEventArgs e)
2023-01-11 13:10:18 +00:00
{
2024-03-11 08:01:33 +00:00
2023-01-11 16:46:28 +00:00
if (--timeleft <= 0)
2023-01-13 14:45:42 +00:00
{
2023-01-20 12:42:00 +00:00
EndTimer.Stop();
CloseTimer.Start();
2023-01-13 14:45:42 +00:00
}
2023-01-11 13:10:18 +00:00
}
2023-02-17 05:33:46 +00:00
public Timer EndTimer = new Timer() { Interval = 200 };
2023-05-25 17:50:38 +00:00
public Timer ShowTimer = new Timer() { Interval = 50 };
2023-02-17 05:33:46 +00:00
public Timer CloseTimer = new Timer() { Interval = 20 };
2023-01-11 16:46:28 +00:00
int timeleft;
2023-07-16 23:58:09 +00:00
string graphName;
2023-01-26 15:15:37 +00:00
/// <summary>
/// 显示消息
/// </summary>
/// <param name="name">名字</param>
/// <param name="text">内容</param>
2024-03-11 08:01:33 +00:00
public void Show(string name, string text, string graphname = null, UIElement msgcontent = null)
2023-01-11 13:10:18 +00:00
{
2023-02-01 14:16:43 +00:00
if (m.UIGrid.Children.IndexOf(this) != m.UIGrid.Children.Count - 1)
{
Panel.SetZIndex(this, m.UIGrid.Children.Count - 1);
2023-02-01 14:16:43 +00:00
}
2024-03-11 08:01:33 +00:00
MessageBoxContent.Children.Clear();
2023-01-12 22:11:55 +00:00
TText.Text = "";
outputtext = text.ToList();
2023-01-11 13:10:18 +00:00
LName.Content = name;
2023-01-12 22:11:55 +00:00
timeleft = text.Length + 5;
2023-01-20 12:42:00 +00:00
ShowTimer.Start(); EndTimer.Stop(); CloseTimer.Stop();
2023-01-11 13:10:18 +00:00
this.Visibility = Visibility.Visible;
2023-01-28 06:12:01 +00:00
Opacity = .8;
2023-07-16 23:58:09 +00:00
graphName = graphname;
2024-03-11 08:01:33 +00:00
if (msgcontent != null)
{
MessageBoxContent.Children.Add(msgcontent);
}
2023-01-11 13:10:18 +00:00
}
2023-04-04 09:32:53 +00:00
public void Border_MouseEnter(object sender, MouseEventArgs e)
2023-01-11 13:10:18 +00:00
{
2023-01-12 22:11:55 +00:00
EndTimer.Stop();
2023-01-20 12:42:00 +00:00
CloseTimer.Stop();
2023-01-28 06:12:01 +00:00
this.Opacity = .8;
2023-01-11 13:10:18 +00:00
}
2023-04-04 09:32:53 +00:00
public void Border_MouseLeave(object sender, MouseEventArgs e)
2023-01-11 13:10:18 +00:00
{
2023-01-12 22:11:55 +00:00
if (!ShowTimer.Enabled)
EndTimer.Start();
2023-01-11 13:10:18 +00:00
}
private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
2023-01-26 15:15:37 +00:00
ForceClose();
}
/// <summary>
/// 强制关闭
/// </summary>
public void ForceClose()
{
EndTimer.Stop(); ShowTimer.Stop(); CloseTimer.Close();
2023-01-11 13:10:18 +00:00
this.Visibility = Visibility.Collapsed;
MessageBoxContent.Children.Clear();
2023-01-13 14:45:42 +00:00
EndAction?.Invoke();
2022-12-13 07:10:18 +00:00
}
2023-01-20 17:01:10 +00:00
public void Dispose()
{
EndTimer.Dispose();
ShowTimer.Dispose();
CloseTimer.Dispose();
}
2023-05-30 16:51:17 +00:00
public void SetPlaceIN()
{
this.Height = 500;
BorderMain.VerticalAlignment = VerticalAlignment.Bottom;
Margin = new Thickness(0);
}
public void SetPlaceOUT()
{
this.Height = double.NaN;
BorderMain.VerticalAlignment = VerticalAlignment.Top;
Margin = new Thickness(0, 500, 0, 0);
}
2024-03-10 05:11:09 +00:00
private void MenuItemCopy_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(TText.Text);
}
private void MenuItemClose_Click(object sender, RoutedEventArgs e)
{
ForceClose();
}
private void ContextMenu_Opened(object sender, RoutedEventArgs e) => Border_MouseEnter(null, null);
private void ContextMenu_Closed(object sender, RoutedEventArgs e) => Border_MouseLeave(null, null);
private void TText_SizeChanged(object sender, SizeChangedEventArgs e)
{
sv.ScrollToEnd();
}
2022-12-13 07:10:18 +00:00
}
}