From 576b92fd82f568572bc4beb125fa0ba0191a602f Mon Sep 17 00:00:00 2001
From: asmrobot <asmrobot@hotmail.com>
Date: Wed, 13 Nov 2019 14:59:52 +0000
Subject: [PATCH] add map editor

---
 src/RichCreator/MainWindow.xaml.cs |  240 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 238 insertions(+), 2 deletions(-)

diff --git a/src/RichCreator/MainWindow.xaml.cs b/src/RichCreator/MainWindow.xaml.cs
index b7f8521..69cb269 100644
--- a/src/RichCreator/MainWindow.xaml.cs
+++ b/src/RichCreator/MainWindow.xaml.cs
@@ -16,6 +16,12 @@
 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;
 
 namespace RichCreator
 {
@@ -45,7 +51,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 +103,7 @@
                 }
                 G.Instance.DebugWriter("硬件检测通过");
                 
-                this.monitor = new JobMonitor(taskIndex);
+                this.monitor = new JobMonitor(mapType);
                 monitor.Start();
 
                 startTaskUI();
@@ -172,6 +207,161 @@
             hsvImage = hsvImage.GetSubRect(new System.Drawing.Rectangle(0, 0, 1280, 720));
 
             return DnfCVHelper.FindRole(hsvImage, new ZTRectangle(0, 0, 1280, 720));
+        }
+
+        /// <summary>
+        /// 测速
+        /// </summary>
+        private void TestSpeed()
+        {
+            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);
+
+
+
+                //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);
+
+
+                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);
+                }
+                
+                G.Instance.InfoWriter($"测试技能发放完成");
+            });
+        }
+        
+        private ZTPoint rolePosition = ZTPoint.Empty;
+
+        /// <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);
+                rolePosition = DnfCVHelper.FindRole(hsvImage, gameRect);
+
+
+                G.Instance.InfoWriter($"角色位置读取成功:{rolePosition}");
+            });
+        }
+
+        /// <summary>
+        /// 测试进入下一关的门
+        /// </summary>
+        private void TestToNextDoor()
+        {
+            HouseInfo house = new HouseInfo(0, MapType.Kalete, false, null);
+
+            ZTRectangle gameRect = ZTRectangle.Empty;
+            WindowUtils.GetDnfRect(out gameRect);
+            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);
+                ZTPoint start=house.ScreenToMapCoordinate(rolePosition, locationCoor);
+
+
+
+                DnfRole role = new DnfRole(gameRect);
+
+                Int32 doorIndex = 0;
+                List<ZTPoint> paths=house.FindDoorPath(out doorIndex,start);
+                ZTPoint last = start;
+                for (int i = 0; i < paths.Count; i++)
+                {
+                    //移动
+                    ZTPoint distance = paths[i].Sub(last);
+                    last = paths[i];
+                    role.SyncMove(distance);
+                }
+
+                
+
+                G.Instance.InfoWriter($"测试进入下一关的门完成");
+            });
         }
 
 
@@ -380,8 +570,54 @@
                 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());
+                        }
+                        
+                    }
+                };
+
+            }
+
+        }
+
+
         
+        private void Window_Unloaded(object sender, RoutedEventArgs e)
+        {
+            //注销热键
+            IntPtr intPtr = (new WindowInteropHelper(this)).Handle;
+            SystemHotKey.UnRegHotKey(intPtr, hotKeyID);
+        }
     }
 }

--
Gitblit v1.9.3