2023-05-19 08:17:51 +00:00
using LinePutScript ;
2023-07-23 22:57:06 +00:00
using LinePutScript.Localization.WPF ;
2023-05-19 08:17:51 +00:00
using Panuon.WPF.UI ;
2023-02-21 11:37:01 +00:00
using System ;
2022-12-13 07:10:18 +00:00
using System.Linq ;
2023-09-17 12:42:23 +00:00
using System.Media ;
2022-12-13 07:10:18 +00:00
using System.Threading ;
using System.Threading.Tasks ;
using System.Windows ;
using System.Windows.Input ;
using System.Windows.Media ;
2023-07-16 23:58:09 +00:00
using static VPet_Simulator . Core . GraphInfo ;
2022-12-13 07:10:18 +00:00
namespace VPet_Simulator.Core
{
/// <summary>
/// Main.xaml 的交互逻辑
/// </summary>
2023-02-21 11:37:01 +00:00
public partial class Main : ContentControlX , IDisposable
2022-12-13 07:10:18 +00:00
{
2023-01-08 16:57:10 +00:00
/// <summary>
/// 游戏核心
/// </summary>
2022-12-13 07:10:18 +00:00
public GameCore Core ;
2023-01-08 16:57:10 +00:00
/// <summary>
/// 菜单栏
/// </summary>
2023-01-11 13:10:18 +00:00
public ToolBar ToolBar ;
/// <summary>
2023-05-22 20:18:59 +00:00
/// 消息栏
2023-01-11 13:10:18 +00:00
/// </summary>
public MessageBar MsgBar ;
2023-01-08 16:57:10 +00:00
/// <summary>
2023-05-22 20:18:59 +00:00
/// 工作显示栏
/// </summary>
public WorkTimer WorkTimer ;
/// <summary>
2023-01-08 16:57:10 +00:00
/// 刷新时间时会调用该方法
/// </summary>
public event Action < Main > TimeHandle ;
/// <summary>
/// 刷新时间时会调用该方法,在所有任务处理完之后
/// </summary>
public event Action < Main > TimeUIHandle ;
2023-05-15 10:44:30 +00:00
/// <summary>
2023-06-05 09:18:27 +00:00
/// 如果不开启功能模式,默认状态设置
/// </summary>
2023-06-19 19:28:48 +00:00
public GameSave . ModeType NoFunctionMOD = GameSave . ModeType . Happy ;
2023-06-05 09:18:27 +00:00
/// <summary>
2023-05-15 10:44:30 +00:00
/// 是否开始运行
/// </summary>
public bool IsWorking { get ; private set ; } = false ;
2023-09-17 12:42:23 +00:00
public SoundPlayer soundPlayer = new SoundPlayer ( ) ;
public bool windowMediaPlayerAvailable = true ;
2023-02-20 08:47:44 +00:00
public Main ( GameCore core , bool loadtouchevent = true )
2022-12-13 07:10:18 +00:00
{
2023-08-14 06:37:35 +00:00
//Console.WriteLine(DateTime.Now.ToString("T:fff"));
2022-12-13 07:10:18 +00:00
InitializeComponent ( ) ;
Core = core ;
2023-05-22 20:18:59 +00:00
WorkTimer = new WorkTimer ( this ) ;
WorkTimer . Visibility = Visibility . Collapsed ;
UIGrid . Children . Add ( WorkTimer ) ;
2023-01-11 13:10:18 +00:00
ToolBar = new ToolBar ( this ) ;
ToolBar . Visibility = Visibility . Collapsed ;
UIGrid . Children . Add ( ToolBar ) ;
2023-01-26 15:15:37 +00:00
MsgBar = new MessageBar ( this ) ;
2023-01-11 13:10:18 +00:00
MsgBar . Visibility = Visibility . Collapsed ;
UIGrid . Children . Add ( MsgBar ) ;
2023-06-25 18:54:34 +00:00
labeldisplaytimer . Elapsed + = Labledisplaytimer_Elapsed ;
2023-05-22 20:18:59 +00:00
2023-01-27 17:44:57 +00:00
if ( loadtouchevent )
{
LoadTouchEvent ( ) ;
}
2023-06-05 09:18:27 +00:00
if ( ! core . Controller . EnableFunction )
Core . Save . Mode = NoFunctionMOD ;
2023-07-16 23:58:09 +00:00
var ig = Core . Graph . FindGraph ( Core . Graph . FindName ( GraphType . StartUP ) , AnimatType . Single , core . Save . Mode ) ;
2023-08-12 06:09:20 +00:00
ig ? ? = Core . Graph . FindGraph ( Core . Graph . FindName ( GraphType . StartUP ) , AnimatType . Single , GameSave . ModeType . PoorCondition ) ;
2023-07-16 23:58:09 +00:00
//var ig2 = Core.Graph.FindGraph(GraphType.Default, core.GameSave.Mode);
2023-02-13 09:48:59 +00:00
PetGrid2 . Visibility = Visibility . Collapsed ;
2023-05-31 17:47:23 +00:00
Task . Run ( ( ) = >
2023-04-04 09:32:53 +00:00
{
2023-08-15 05:09:47 +00:00
//while (!ig.IsReady)
//{
// Thread.Sleep(100);
//}//新功能:等待所有图像加载完成再跑
foreach ( var igs in Core . Graph . GraphsList . Values )
2023-05-31 17:47:23 +00:00
{
2023-08-15 05:09:47 +00:00
foreach ( var ig2 in igs . Values )
{
foreach ( var ig3 in ig2 )
{
while ( ! ig3 . IsReady )
{
Thread . Sleep ( 100 ) ;
}
}
}
2023-05-31 17:47:23 +00:00
}
2023-08-15 05:09:47 +00:00
2023-05-31 17:47:23 +00:00
ig . Run ( PetGrid , ( ) = >
2023-04-04 09:32:53 +00:00
{
2023-05-31 17:47:23 +00:00
IsWorking = true ;
Dispatcher . Invoke ( ( ) = >
{
PetGrid . Tag = ig ;
PetGrid2 . Tag = ig ;
} ) ;
DisplayNomal ( ) ;
2023-04-04 09:32:53 +00:00
} ) ;
2023-03-26 22:35:19 +00:00
} ) ;
2023-04-04 09:32:53 +00:00
2023-07-18 18:09:51 +00:00
EventTimer . Elapsed + = ( s , e ) = > EventTimer_Elapsed ( ) ;
2022-12-14 18:17:13 +00:00
MoveTimer . Elapsed + = MoveTimer_Elapsed ;
2023-01-24 06:56:16 +00:00
SmartMoveTimer . Elapsed + = SmartMoveTimer_Elapsed ;
2022-12-14 18:17:13 +00:00
}
2023-06-25 18:54:34 +00:00
private void Labledisplaytimer_Elapsed ( object sender , System . Timers . ElapsedEventArgs e )
{
if ( - - labeldisplaycount < = 0 )
{
labeldisplaytimer . Enabled = false ;
labeldisplaychangenum1 = 0 ;
labeldisplaychangenum2 = 0 ;
Dispatcher . Invoke ( ( ) = >
{
LabelDisplay . Visibility = Visibility . Collapsed ;
} ) ;
}
else if ( labeldisplaycount < 50 )
{
Dispatcher . Invoke ( ( ) = >
{
LabelDisplay . Opacity = labeldisplaycount / 50 ;
} ) ;
}
}
2023-01-27 17:44:57 +00:00
/// <summary>
/// 自动加载触摸事件
/// </summary>
public void LoadTouchEvent ( )
{
2023-06-11 09:10:09 +00:00
Core . TouchEvent . Add ( new TouchArea ( Core . Graph . GraphConfig . TouchHeadLocate , Core . Graph . GraphConfig . TouchHeadSize , ( ) = > { DisplayTouchHead ( ) ; return true ; } ) ) ;
for ( int i = 0 ; i < 4 ; i + + )
{
GameSave . ModeType m = ( GameSave . ModeType ) i ;
Core . TouchEvent . Add ( new TouchArea ( Core . Graph . GraphConfig . TouchRaisedLocate [ i ] , Core . Graph . GraphConfig . TouchRaisedSize [ i ] ,
( ) = >
{
if ( Core . Save . Mode = = m )
{
DisplayRaised ( ) ;
return true ;
}
else
return false ;
} , true ) ) ;
}
2023-01-27 17:44:57 +00:00
}
2023-05-25 11:02:13 +00:00
/// <summary>
2023-05-25 17:50:38 +00:00
/// 播放语音 语音播放时不会停止播放说话表情
2023-05-25 11:02:13 +00:00
/// </summary>
/// <param name="VoicePath">语音位置</param>
2023-05-25 17:50:38 +00:00
public void PlayVoice ( Uri VoicePath ) //, TimeSpan timediff = TimeSpan.Zero) TODO
2023-05-25 11:02:13 +00:00
{
2023-05-25 17:50:38 +00:00
PlayingVoice = true ;
2023-09-17 12:42:23 +00:00
if ( windowMediaPlayerAvailable )
2023-05-25 17:50:38 +00:00
{
2023-09-17 12:42:23 +00:00
Dispatcher . Invoke ( ( ) = >
{
VoicePlayer . Clock = new MediaTimeline ( VoicePath ) . CreateClock ( ) ;
VoicePlayer . Clock . Completed + = Clock_Completed ;
VoicePlayer . MediaFailed + = MediaPlayer_MediaFailed ;
VoicePlayer . Play ( ) ;
//Task.Run(() =>
//{
// Thread.Sleep(1000);
// Dispatcher.Invoke(() =>
// {
// if (VoicePlayer?.Clock?.NaturalDuration.HasTimeSpan == true)
// PlayEndTime += VoicePlayer.Clock.NaturalDuration.TimeSpan - TimeSpan.FromSeconds(0.8);
// });
//});
} ) ;
}
else
{
soundPlayer . SoundLocation = VoicePath . LocalPath ;
soundPlayer . LoadAsync ( ) ;
}
2023-05-25 17:50:38 +00:00
}
/// <summary>
/// 声音音量
/// </summary>
public double PlayVoiceVolume
{
get = > Dispatcher . Invoke ( ( ) = > VoicePlayer . Volume ) ;
set = > Dispatcher . Invoke ( ( ) = > VoicePlayer . Volume = value ) ;
}
/// <summary>
/// 当前是否正在播放
/// </summary>
public bool PlayingVoice = false ;
2023-09-17 12:42:23 +00:00
private void MediaPlayer_MediaFailed ( object sender , ExceptionRoutedEventArgs e )
{
windowMediaPlayerAvailable = false ;
PlayingVoice = false ;
MessageBoxX . Show ( "音频播放失败, 已尝试自动切换到备用播放器。如果问题持续, 请检查是否已安装WindowsMediaPlayer。\nAudio playback failed, attempted automatic switch to backup player. If the issue persists, please check if Windows Media Player is installed." , "音频错误 audio error" , Panuon . WPF . UI . MessageBoxIcon . Warning ) ;
}
2023-05-25 17:50:38 +00:00
private void Clock_Completed ( object sender , EventArgs e )
{
PlayingVoice = false ;
VoicePlayer . Clock = null ;
2023-05-25 11:02:13 +00:00
}
2023-07-23 22:57:06 +00:00
public bool MoveTimerSmartMove = false ;
2023-01-24 06:56:16 +00:00
private void SmartMoveTimer_Elapsed ( object sender , System . Timers . ElapsedEventArgs e )
{
2023-07-23 22:57:06 +00:00
MoveTimerSmartMove = false ;
2023-01-24 06:56:16 +00:00
}
2022-12-14 18:17:13 +00:00
private void MoveTimer_Elapsed ( object sender , System . Timers . ElapsedEventArgs e )
{
2023-07-23 22:57:06 +00:00
if ( DisplayType . Type ! = GraphType . Move | | ! MoveTimerSmartMove )
2023-01-25 04:49:18 +00:00
{
2023-04-04 09:32:53 +00:00
return ;
2023-01-25 04:49:18 +00:00
}
2023-04-04 09:32:53 +00:00
Core . Controller . MoveWindows ( MoveTimerPoint . X , MoveTimerPoint . Y ) ;
2023-07-23 22:57:06 +00:00
//if (Core.Controller.GetWindowsDistanceLeft() < -500)
//{
// MessageBox.Show("当前动画移动设计错误: 已到达边界 左侧\n动画名称: {0}\n距离: {1}".Translate(DisplayType.Name, Core.Controller.GetWindowsDistanceLeft()), "MOD移动设计错误".Translate());
//}
//else if (Core.Controller.GetWindowsDistanceRight() < -500)
//{
// MessageBox.Show("当前动画移动设计错误: 已到达边界 右侧\n动画名称: {0}\n距离: {1}".Translate(DisplayType.Name, Core.Controller.GetWindowsDistanceRight()), "MOD移动设计错误".Translate());
//}
//else if (Core.Controller.GetWindowsDistanceUp() < -500)
//{
// MessageBox.Show("当前动画移动设计错误: 已到达边界 上侧\n动画名称: {0}\n距离: {1}".Translate(DisplayType.Name, Core.Controller.GetWindowsDistanceUp()), "MOD移动设计错误".Translate());
//}
//else if (Core.Controller.GetWindowsDistanceDown() < -500)
//{
// MessageBox.Show("当前动画移动设计错误: 已到达边界 下侧\n动画名称: {0}\n距离: {1}".Translate(DisplayType.Name, Core.Controller.GetWindowsDistanceDown()), "MOD移动设计错误".Translate());
//}
MoveTimer . Start ( ) ;
2022-12-13 07:10:18 +00:00
}
2023-08-08 14:09:49 +00:00
/// <summary>
/// 默认点击事件
/// </summary>
2023-01-13 14:45:42 +00:00
public Action DefaultClickAction ;
2023-08-08 14:09:49 +00:00
/// <summary>
/// 默认长按事件
/// </summary>
public Action DefaultPressAction ;
2022-12-13 07:10:18 +00:00
bool isPress = false ;
2023-01-20 12:40:42 +00:00
long presstime ;
2022-12-13 07:10:18 +00:00
private void MainGrid_MouseLeftButtonDown ( object sender , MouseButtonEventArgs e )
{
isPress = true ;
2023-01-20 12:40:42 +00:00
CountNomal = 0 ;
2022-12-13 07:10:18 +00:00
Task . Run ( ( ) = >
{
2023-01-20 12:40:42 +00:00
var pth = DateTime . Now . Ticks ;
presstime = pth ;
2023-01-21 14:16:13 +00:00
Thread . Sleep ( Core . Controller . PressLength ) ;
2022-12-13 07:10:18 +00:00
Point mp = default ;
Dispatcher . BeginInvoke ( new Action ( ( ) = > mp = Mouse . GetPosition ( MainGrid ) ) ) . Wait ( ) ;
2023-01-24 06:56:16 +00:00
//mp = new Point(mp.X * Core.Controller.ZoomRatio, mp.Y * Core.Controller.ZoomRatio);
2023-01-20 12:40:42 +00:00
if ( isPress & & presstime = = pth )
2022-12-13 07:10:18 +00:00
{ //历遍长按事件
2023-06-14 11:13:52 +00:00
LastInteractionTime = DateTime . Now ;
2023-06-11 09:10:09 +00:00
foreach ( var x in Core . TouchEvent )
{
if ( x . IsPress = = true & & x . Touch ( mp ) & & x . DoAction ( ) )
return ;
}
2023-08-08 14:09:49 +00:00
DefaultPressAction ? . Invoke ( ) ;
2022-12-13 07:10:18 +00:00
}
else
{ //历遍点击事件
2023-06-14 11:13:52 +00:00
LastInteractionTime = DateTime . Now ;
2023-06-11 09:10:09 +00:00
foreach ( var x in Core . TouchEvent )
{
if ( x . IsPress = = false & & x . Touch ( mp ) & & x . DoAction ( ) )
return ;
}
2023-08-08 14:09:49 +00:00
//普通点击验证
if ( DisplayType . Type ! = GraphType . Default )
{ //不是nomal! 可能会卡timer,所有全部timer清空下
CleanState ( ) ;
2023-08-13 16:31:37 +00:00
if ( ! IsIdel & & DisplayStop ( DisplayToNomal ) )
2023-08-08 14:09:49 +00:00
return ;
}
2023-06-11 09:10:09 +00:00
DefaultClickAction ? . Invoke ( ) ;
2022-12-13 07:10:18 +00:00
}
} ) ;
}
private void MainGrid_MouseLeftButtonUp ( object sender , MouseButtonEventArgs e )
{
isPress = false ;
2023-07-17 15:38:06 +00:00
if ( DisplayType . Type . ToString ( ) . StartsWith ( "Raised" ) )
2022-12-13 07:10:18 +00:00
{
MainGrid . MouseMove - = MainGrid_MouseMove ;
2023-02-20 08:47:44 +00:00
MainGrid . MouseMove + = MainGrid_MouseWave ;
2022-12-13 07:10:18 +00:00
rasetype = - 1 ;
2023-01-18 18:00:30 +00:00
DisplayRaising ( ) ;
2022-12-13 07:10:18 +00:00
}
2023-01-26 15:15:37 +00:00
else
2023-01-24 06:56:16 +00:00
{
2023-01-26 15:15:37 +00:00
//if (MsgBar.Visibility == Visibility.Visible)
//{
// MsgBar.ForceClose();
//}
if ( SmartMove )
{
2023-07-23 22:57:06 +00:00
MoveTimerSmartMove = true ;
2023-02-03 13:36:41 +00:00
SmartMoveTimer . Enabled = false ;
2023-01-26 15:15:37 +00:00
SmartMoveTimer . Start ( ) ;
}
2023-01-24 06:56:16 +00:00
}
2023-03-09 05:34:16 +00:00
( ( UIElement ) e . Source ) . ReleaseMouseCapture ( ) ;
2022-12-13 07:10:18 +00:00
}
private void MainGrid_MouseMove ( object sender , MouseEventArgs e )
{
2023-08-14 19:07:02 +00:00
if ( ! ( ( UIElement ) e . Source ) . CaptureMouse ( ) | | ! isPress )
{
MainGrid . MouseMove - = MainGrid_MouseMove ;
MainGrid . MouseMove + = MainGrid_MouseWave ;
rasetype = - 1 ;
DisplayRaising ( ) ;
return ;
}
2022-12-13 07:10:18 +00:00
var mp = e . GetPosition ( MainGrid ) ;
2023-03-01 22:51:07 +00:00
var x = mp . X - Core . Graph . GraphConfig . RaisePoint [ ( int ) Core . Save . Mode ] . X ;
var y = mp . Y - Core . Graph . GraphConfig . RaisePoint [ ( int ) Core . Save . Mode ] . Y ;
2022-12-13 07:10:18 +00:00
Core . Controller . MoveWindows ( x , y ) ;
2023-07-17 15:38:06 +00:00
if ( Math . Abs ( x ) + Math . Abs ( y ) > 20 & & rasetype > = 1 )
2022-12-13 07:10:18 +00:00
rasetype = 0 ;
}
2023-01-11 13:44:16 +00:00
private void MainGrid_MouseRightButtonDown ( object sender , MouseButtonEventArgs e )
{
2023-06-14 11:13:52 +00:00
if ( ToolBar . Visibility = = Visibility . Visible )
{
ToolBar . CloseTimer . Enabled = false ;
ToolBar . Visibility = Visibility . Collapsed ;
}
else
ToolBar . Show ( ) ;
2023-01-11 13:44:16 +00:00
}
2023-01-19 15:26:58 +00:00
2023-01-12 22:11:55 +00:00
public void Dispose ( )
{
EventTimer . Stop ( ) ;
2023-05-27 10:37:15 +00:00
MoveTimer . Enabled = false ;
2023-01-12 22:11:55 +00:00
EventTimer . Dispose ( ) ;
MoveTimer . Dispose ( ) ;
2023-01-20 17:01:10 +00:00
MsgBar . Dispose ( ) ;
ToolBar . Dispose ( ) ;
2023-01-12 22:11:55 +00:00
if ( PetGrid . Child is IGraph g )
2023-01-20 17:01:10 +00:00
g . Stop ( true ) ;
2023-01-12 22:11:55 +00:00
if ( PetGrid2 . Child is IGraph g2 )
2023-01-20 17:01:10 +00:00
g2 . Stop ( true ) ;
}
/// <summary>
/// 清理所有状态
/// </summary>
public void CleanState ( )
{
2023-02-04 05:12:06 +00:00
MoveTimer . Enabled = false ;
2023-01-20 17:01:10 +00:00
MainGrid . MouseMove - = MainGrid_MouseMove ;
2023-02-20 08:47:44 +00:00
MainGrid . MouseMove + = MainGrid_MouseWave ;
}
private int wavetimes = 0 ;
private int switchcount = 0 ;
private bool? waveleft = null ;
private bool? wavetop = null ;
private DateTime wavespan ;
private void MainGrid_MouseWave ( object sender , MouseEventArgs e )
2023-05-22 20:18:59 +00:00
{
2023-05-19 08:17:51 +00:00
if ( rasetype > = 0 | | State ! = WorkingState . Nomal )
2023-03-09 05:34:16 +00:00
return ;
2023-02-21 11:37:01 +00:00
if ( ( DateTime . Now - wavespan ) . TotalSeconds > 2 )
2023-02-20 08:47:44 +00:00
{
wavetimes = 0 ;
switchcount = 0 ;
waveleft = null ;
wavetop = null ;
}
wavespan = DateTime . Now ;
bool active = false ;
var p = e . GetPosition ( MainGrid ) ;
if ( p . Y < 200 )
{
if ( wavetop ! = false )
wavetop = true ;
else
{
if ( switchcount + + > 150 )
wavespan = DateTime . MinValue ;
return ;
}
}
else
{
2023-02-21 11:37:01 +00:00
if ( wavetop ! = true )
2023-02-20 08:47:44 +00:00
wavetop = false ;
else
{
if ( switchcount + + > 150 )
wavespan = DateTime . MinValue ;
return ;
}
}
if ( p . X < 200 & & waveleft ! = true )
{
waveleft = true ;
active = true ;
}
if ( p . X > 300 & & waveleft ! = false )
{
active = true ;
waveleft = false ;
}
if ( active )
{
2023-03-07 08:13:14 +00:00
if ( wavetimes + + > 4 )
2023-02-20 08:47:44 +00:00
if ( wavetop = = true )
{
2023-08-10 11:34:11 +00:00
if ( wavetimes > = 10 | | IsIdel | | DisplayType . Type = = GraphType . Touch_Head )
2023-03-07 08:13:14 +00:00
DisplayTouchHead ( ) ;
2023-02-21 11:37:01 +00:00
//Console.WriteLine(wavetimes);
2023-06-18 18:11:40 +00:00
LastInteractionTime = DateTime . Now ;
2023-02-20 08:47:44 +00:00
}
else
{
2023-08-10 11:34:11 +00:00
if ( wavetimes > = 10 | | IsIdel | | DisplayType . Type = = GraphType . Touch_Body )
2023-03-07 08:13:14 +00:00
DisplayTouchBody ( ) ;
2023-06-18 18:11:40 +00:00
LastInteractionTime = DateTime . Now ;
2023-02-20 08:47:44 +00:00
}
}
2023-01-12 22:11:55 +00:00
}
2022-12-13 07:10:18 +00:00
}
}