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

134 lines
3.9 KiB
C#
Raw Normal View History

2022-12-13 07:10:18 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
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
{
/// <summary>
/// MessageBar.xaml 的交互逻辑
/// </summary>
2023-01-20 17:01:10 +00:00
public partial class MessageBar : UserControl, IDisposable
2022-12-13 07:10:18 +00:00
{
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);
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-01-26 15:15:37 +00:00
Task.Run(() =>
{
Thread.Sleep(timeleft * 50);
2023-01-27 17:44:57 +00:00
if (m.DisplayType == GraphCore.GraphType.Default || m.DisplayType.ToString().Contains("Say"))
2023-01-28 06:12:01 +00:00
m.Display(GraphCore.GraphType.Say_C_End, m.DisplayNomal);
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
{
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-01-12 22:11:55 +00:00
public Timer EndTimer = new Timer() { Interval = 100 };
2023-01-26 15:15:37 +00:00
public Timer ShowTimer = new Timer() { Interval = 40 };
2023-01-20 12:42:00 +00:00
public Timer CloseTimer = new Timer() { Interval = 10 };
2023-01-11 16:46:28 +00:00
int timeleft;
2023-01-26 15:15:37 +00:00
/// <summary>
/// 显示消息
/// </summary>
/// <param name="name">名字</param>
/// <param name="text">内容</param>
2023-01-11 13:10:18 +00:00
public void Show(string name, string text)
{
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-01-11 13:10:18 +00:00
}
private void Border_MouseEnter(object sender, MouseEventArgs e)
{
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
}
private void Border_MouseLeave(object sender, MouseEventArgs e)
{
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;
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();
}
2022-12-13 07:10:18 +00:00
}
}