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

176 lines
5.8 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;
using System.Threading;
using System.Threading.Tasks;
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;
namespace VPet_Simulator.Core
{
/// <summary>
/// Main.xaml 的交互逻辑
/// </summary>
2023-01-12 22:11:55 +00:00
public partial class Main : UserControl, 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>
/// 菜单栏
/// </summary>
public MessageBar MsgBar;
2023-01-08 16:57:10 +00:00
/// <summary>
/// 刷新时间时会调用该方法
/// </summary>
public event Action<Main> TimeHandle;
/// <summary>
/// 刷新时间时会调用该方法,在所有任务处理完之后
/// </summary>
public event Action<Main> TimeUIHandle;
2022-12-13 07:10:18 +00:00
public Main(GameCore core)
{
InitializeComponent();
Core = core;
2023-01-11 13:10:18 +00:00
ToolBar = new ToolBar(this);
ToolBar.Visibility = Visibility.Collapsed;
UIGrid.Children.Add(ToolBar);
MsgBar = new MessageBar();
MsgBar.Visibility = Visibility.Collapsed;
UIGrid.Children.Add(MsgBar);
2023-01-20 07:08:28 +00:00
Core.TouchEvent.Add(new TouchArea(Core.Graph.GraphConfig.TouchHeadLocate, Core.Graph.GraphConfig.TouchHeadSize, DisplayTouchHead));
Core.TouchEvent.Add(new TouchArea(Core.Graph.GraphConfig.TouchRaisedLocate, Core.Graph.GraphConfig.TouchRaisedSize, DisplayRaised, true));
2023-01-21 14:16:13 +00:00
var ig = Core.Graph.FindGraph(GraphCore.GraphType.Default, core.Save.Mode);
2022-12-13 07:10:18 +00:00
PetGrid.Child = ig.This;
ig.Run(DisplayNomal);
2023-01-21 14:16:13 +00:00
2022-12-13 07:10:18 +00:00
EventTimer.Elapsed += 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-01-24 06:56:16 +00:00
private void SmartMoveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MoveTimer.AutoReset = false;
}
2023-01-11 13:10:18 +00:00
public void Say(string text)
{
MsgBar.Show(Core.Save.Name, text);
}
2022-12-14 18:17:13 +00:00
private void MoveTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Core.Controller.MoveWindows(MoveTimerPoint.X, MoveTimerPoint.Y);
2022-12-13 07:10:18 +00:00
}
2023-01-13 14:45:42 +00:00
public Action DefaultClickAction;
2022-12-13 07:10:18 +00:00
bool isPress = false;
long presstime;
2022-12-13 07:10:18 +00:00
private void MainGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isPress = true;
CountNomal = 0;
2023-01-19 15:26:58 +00:00
if (DisplayType != GraphCore.GraphType.Default)
2022-12-14 18:17:13 +00:00
{//不是nomal! 可能会卡timer,所有全部timer清空下
2023-01-20 17:01:10 +00:00
CleanState();
2023-01-19 15:26:58 +00:00
if (DisplayStopMove(DisplayNomal))
return;
2022-12-14 18:17:13 +00:00
}
2022-12-13 07:10:18 +00:00
Task.Run(() =>
{
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);
if (isPress && presstime == pth)
2022-12-13 07:10:18 +00:00
{//历遍长按事件
var act = Core.TouchEvent.FirstOrDefault(x => x.IsPress == true && x.Touch(mp));
if (act != null)
Dispatcher.Invoke(act.DoAction);
2022-12-13 07:10:18 +00:00
}
else
{//历遍点击事件
var act = Core.TouchEvent.FirstOrDefault(x => x.IsPress == false && x.Touch(mp));
if (act != null)
Dispatcher.Invoke(act.DoAction);
else
2023-01-13 14:45:42 +00:00
DefaultClickAction?.Invoke();
2022-12-13 07:10:18 +00:00
}
});
}
private void MainGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isPress = false;
if (DisplayType.ToString().StartsWith("Raised"))
2022-12-13 07:10:18 +00:00
{
MainGrid.MouseMove -= MainGrid_MouseMove;
rasetype = -1;
DisplayRaising();
2022-12-13 07:10:18 +00:00
}
2023-01-24 06:56:16 +00:00
else
{
if (SmartMove)
{
MoveTimer.AutoReset = true;
SmartMoveTimer.Stop();
SmartMoveTimer.Start();
}
}
2022-12-13 07:10:18 +00:00
}
private void MainGrid_MouseMove(object sender, MouseEventArgs e)
{
var mp = e.GetPosition(MainGrid);
2023-01-20 07:08:28 +00:00
var x = mp.X - Core.Graph.GraphConfig.RaisePoint.X;
var y = mp.Y - Core.Graph.GraphConfig.RaisePoint.Y;
2022-12-13 07:10:18 +00:00
Core.Controller.MoveWindows(x, y);
if (Math.Abs(x) + Math.Abs(y) > 10)
rasetype = 0;
}
2023-01-11 13:44:16 +00:00
private void MainGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
ToolBar.Show();
}
2023-01-19 15:26:58 +00:00
2023-01-12 22:11:55 +00:00
public void Dispose()
{
EventTimer.Stop();
MoveTimer.Stop();
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()
{
MoveTimer.Stop();
MainGrid.MouseMove -= MainGrid_MouseMove;
2023-01-12 22:11:55 +00:00
}
2022-12-13 07:10:18 +00:00
}
}