asmrobot
2024-12-31 a8536c3c73a4445b1f1252a1d3271e0c73b35613
src/RichCreator/MainWindow.xaml.cs
@@ -16,6 +16,13 @@
using System.Windows.Controls;
using ZTImage.Configuration;
using ZTImage.Log;
using RichCreator.Utility.Maps;
using RichCreator.Dnf;
using System.Windows.Interop;
using RichCreator.Maps.Kalete;
using ZTImage.Collections;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
namespace RichCreator
{
@@ -28,7 +35,7 @@
        public MainWindow()
        {
            InitializeComponent();
            G.Instance.EnableWriter(this.writeDebug, this.writeInfo, this.writeRemainTime, this.CancelConfirm,this.stopTaskUI);
            G.Instance.EnableWriter(this.writeDebug, this.writeInfo, this.writeRemainTime, this.CancelConfirm,this.stopTaskUI,this.updateState);
            viewModel = new ConfigViewModel();
            this.DataContext = viewModel;
        }
@@ -45,7 +52,36 @@
        /// <param name="e"></param>
        private void Run_Click(object sender, RoutedEventArgs e)
        {
            Int32 taskIndex = this.taskName.SelectedIndex;
            MapType mapType = (MapType)this.taskName.SelectedIndex;
            if (mapType == MapType.TestSpeed)
            {
                //测速
                G.Instance.InfoWriter("开始测速");
                TestSpeed();
                return;
            }
            else if (mapType == MapType.TestSkill)
            {
                //测技能
                G.Instance.InfoWriter("开始测技能");
                TestSkill();
                return;
            }
            else if (mapType == MapType.ReadRolePosition)
            {
                //读取角色位置
                G.Instance.InfoWriter("开始测角色位置");
                ReadRolePosition();
                return;
            }
            else if (mapType == MapType.TestToNextDoor)
            {
                //测试进入下一关的门
                G.Instance.InfoWriter("开始测进门");
                TestToNextDoor();
                return;
            }
            if (isRunning)
            {
                monitor.Stop();
@@ -68,7 +104,7 @@
                }
                G.Instance.DebugWriter("硬件检测通过");
                
                this.monitor = new JobMonitor(taskIndex);
                this.monitor = new JobMonitor(mapType);
                monitor.Start();
                startTaskUI();
@@ -86,6 +122,36 @@
        private void ClearLog_Click(object sender, RoutedEventArgs e)
        {
            this.LogTB.Text = string.Empty;
        }
        /// <summary>
        /// 折叠日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LogTBExpand_Click(object sender, RoutedEventArgs e)
        {
            var btn = sender as Button;
            Image img = btn.Content as Image;
            BitmapImage bitmap = null;
            if (this.LogTB.Visibility == Visibility.Collapsed)
            {
                //当前是隐藏状态
                this.LogTB.Visibility = Visibility.Visible;
                this.LogTBClearBtn.Visibility = Visibility.Visible;
                //todo:让日志图像设为up.png
                bitmap = new BitmapImage(new Uri("pack://application:,,,/Images/up.png"));
            }
            else
            {
                this.LogTB.Visibility = Visibility.Collapsed;
                this.LogTBClearBtn.Visibility = Visibility.Collapsed;
                //todo:让日志图像设为down.png
                bitmap = new BitmapImage(new Uri("pack://application:,,,/Images/down.png"));
            }
            img.Source = bitmap;
        }
        /// <summary>
@@ -156,22 +222,146 @@
            MessageBox.Show("配置更新成功");
        }
        #endregion
        private ZTPoint GetRolePosition()
        /// <summary>
        /// 测速
        /// </summary>
        private void TestSpeed()
        {
            System.Drawing.Bitmap bitmap = ScreenCapture.Instance.CaptureScreen();
            //原图
            Image<Rgb, byte> image = new Image<Rgb, byte>(bitmap);
            //色彩hsv
            Image<Hsv, byte> hsvImage = new Image<Hsv, byte>(image.Width, image.Height);
            ZTRectangle gameRect = ZTRectangle.Empty;
            WindowUtils.GetDnfRect(out gameRect);
            Image<Hsv, byte> hsvImage = new Image<Hsv, byte>(gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1);
            Task.Run(() =>
            {
                WindowUtils.SetDnfToTop();
                Thread.Sleep(1000);
                Image<Rgb, byte> image = ScreenCapture.Instance.CaptureScreenReturnImage();
                CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
                ZTPoint start = DnfCVHelper.FindRole(hsvImage, gameRect);
                G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, HIDCode.LeftArrow);
                Thread.Sleep(30);
                G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
                Thread.Sleep(30);
                G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, HIDCode.LeftArrow);
                Thread.Sleep(800);
                G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
                //G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, HIDCode.DownArrow);
                //Thread.Sleep(1000);
                //G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
            CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
                //G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, HIDCode.LeftArrow);
                //Thread.Sleep(1000);
                //G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
            hsvImage = hsvImage.GetSubRect(new System.Drawing.Rectangle(0, 0, 1280, 720));
            return DnfCVHelper.FindRole(hsvImage, new ZTRectangle(0, 0, 1280, 720));
                image = ScreenCapture.Instance.CaptureScreenReturnImage();
                CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
                ZTPoint end = DnfCVHelper.FindRole(hsvImage, gameRect);
                G.Instance.InfoWriter($"from:{start},to:{end},distanceX:{end.X - start.X},distanceY:{end.Y - start.Y}");
            });
        }
        /// <summary>
        /// 测技能
        /// </summary>
        private void TestSkill()
        {
            ZTRectangle gameRect = ZTRectangle.Empty;
            WindowUtils.GetDnfRect(out gameRect);
            Image<Hsv, byte> hsvImage = new Image<Hsv, byte>(gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1);
            Task.Run(() => {
                WindowUtils.SetDnfToTop();
                Thread.Sleep(10);
                KaleteMap map = new KaleteMap(gameRect, CancellationToken.None);
                Int32 lastHouseIndex = -1;
                Int32 houseIndex = -1;
                while (true)
                {
                    Image<Rgb, byte> image = ScreenCapture.Instance.CaptureScreenReturnImage();
                    if (map.MiniMap.GetCurrentHouseIndex(out houseIndex, image))
                    {
                        if (houseIndex != lastHouseIndex)
                        {
                            G.Instance.InfoWriter($"entry house:{houseIndex}");
                            lastHouseIndex = houseIndex;
                            //发技能
                            map.EntryHousePrework(houseIndex, lastHouseIndex);
                        }
                    }
                    Thread.Sleep(500);
                }
            });
        }
        /// <summary>
        /// 读取角色位置
        /// </summary>
        private void ReadRolePosition()
        {
            ZTRectangle gameRect = ZTRectangle.Empty;
            WindowUtils.GetDnfRect(out gameRect);
            Image<Hsv, byte> hsvImage = new Image<Hsv, byte>(gameRect.End.X - gameRect.Start.X + 1, gameRect.End.Y - gameRect.Start.Y + 1);
            Task.Run(() => {
                WindowUtils.SetDnfToTop();
                Thread.Sleep(1000);
                Image<Rgb, byte> image = ScreenCapture.Instance.CaptureScreenReturnImage();
                CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
                ZTPoint rolePosition = DnfCVHelper.FindRole(hsvImage, gameRect);
                G.Instance.InfoWriter($"角色位置读取成功,CBPosition:{rolePosition},Half Position:{DnfRole.CBToHalfPosition(rolePosition)},Foot Position:{DnfRole.CBToFootPosition(rolePosition)}");
            });
        }
        /// <summary>
        /// 测试进入下一关的门
        /// </summary>
        private void TestToNextDoor()
        {
            //ZTRectangle gameRect = ZTRectangle.Empty;
            //WindowUtils.GetDnfRect(out gameRect);
            //HouseInfo house = new HouseInfo(1, MapType.Kalete, false, null,null);
            //DnfRole role = new DnfRole(gameRect);
            //role.SetHouse(house);
            //Image<Hsv, byte> hsvImage = new Image<Hsv, byte>(gameRect.End.X - gameRect.Start.X, gameRect.End.Y - gameRect.Start.Y );
            //Task.Run(() => {
            //    WindowUtils.SetDnfToTop();
            //    Thread.Sleep(10);
            //    Image<Rgb, byte> image = ScreenCapture.Instance.CaptureScreenReturnImage();
            //    image.ROI = new System.Drawing.Rectangle(gameRect.Start.X, gameRect.Start.Y, gameRect.End.X - gameRect.Start.X, gameRect.End.Y - gameRect.Start.Y);
            //    CvInvoke.CvtColor(image, hsvImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Hsv);
            //    image.ROI = System.Drawing.Rectangle.Empty;
            //    //定位点
            //    ParametersPoint locationCoor = DnfCVHelper.GetLocationPoint(image, gameRect);
            //    if (locationCoor.Equals(ParametersPoint.Empty))
            //    {
            //        //找不到定位点
            //        G.Instance.InfoWriter("找不到定位点");
            //        return;
            //    }
            //    ZTPoint rolePosition = DnfCVHelper.FindRole(hsvImage, gameRect);
            //    role.UpdatePosition(rolePosition);
            //    role.ToNextGateMove(locationCoor, house.HousePathInfo.NextGates[0].Point);
            //    G.Instance.InfoWriter($"测试进入下一关的门完成");
            //});
        }
@@ -356,6 +546,19 @@
        }
        /// <summary>
        /// 更新指定状态
        /// </summary>
        /// <param name="index"></param>
        /// <param name="text"></param>
        private void updateState(Int32 index, string text)
        {
            this.Dispatcher.BeginInvoke((Action)(() =>
            {
                this.StateList.Items[index] = text;
            }));
        }
        /// <summary>
        /// 开始任务UI设置
        /// </summary>
        private void startTaskUI()
@@ -380,8 +583,61 @@
                isRunning = false;
            }));
        }
        #endregion
        private int hotKeyID = 2839;
        /// <summary>
        ///     热键消息
        /// </summary>
        const int WindowsMessageHotkey = 786;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //注册热键
            IntPtr intPtr = (new WindowInteropHelper(this)).Handle;
            if (!SystemHotKey.RegHotKey(intPtr, hotKeyID, SystemHotKey.KeyModifiers.None, System.Windows.Forms.Keys.Home))
            {
                MessageBox.Show("热键不可用,其它应用已经占用热键");
            }
            else
            {
                ComponentDispatcher.ThreadPreprocessMessage += (ref MSG Message, ref bool Handled) =>
                {
                    //  判断是否热键消息
                    if (Message.message == WindowsMessageHotkey)
                    {
                        //  获取热键id
                        var id = Message.wParam.ToInt32();
                        //  执行热键回调方法(执行时需要判断是否与注册的热键匹配)
                        if (id == hotKeyID)
                        {
                            Run_Click(this, new RoutedEventArgs());
                        }
                    }
                };
            }
            //初始状态
            for (Int32 i = 0; i < StateProvider.States.Length; i++)
            {
                this.StateList.Items.Add(StateProvider.States[i].HeaderText+":"+ StateProvider.States[i].Value);
            }
        }
        
        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            //注销热键
            IntPtr intPtr = (new WindowInteropHelper(this)).Handle;
            SystemHotKey.UnRegHotKey(intPtr, hotKeyID);
        }
    }
}