123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using GUI.DataProvider;
- using GUI.GUIUtils;
- using GUI.Hook;
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.Windows.Forms;
- namespace GUI
- {
- public partial class ControllerGUI : Form
- {
- readonly EventPerformer<ISizeChangedListener> sizeEvent;
- readonly CheckGroup<BottomButton> bottomGroup;
- readonly KeyboardHook keyboard;
- readonly MouseHook mouse;
- public IGUIDataSource dataSource;
- ComAgent agent => Program.comAgent;
- Point screenCenter => new(Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2);
- Point screenRightDown => new(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
-
- public ControllerGUI()
- {
- ComConfig guiCfg = new ComConfig();
- guiCfg.ShowDialog();
- if (Program.comAgent == null)
- Process.GetCurrentProcess().Kill();
- MessageBox.Show(
- "按F12暂停和继续控制程序",
- "控制器",
- MessageBoxButtons.OK, MessageBoxIcon.Information);
- InitializeComponent();
- dataSource = agent;
- sizeEvent = new();
- sizeEvent.AddSafe(new GUIParagraph(BottomLayout));
- sizeEvent.AddSafe(new GUIParagraph(LayoutRight)
- {
- Target = GUIParagraph.TargetXY.Y,
- FixedPaneReferenceFunc = (c, f) => c.Size = new(f.Width - 3, c.Height)
- });
- bottomGroup = new();
- bottomGroup.AddSafe(bottomButton1, bottomButton2, bottomButton3);
- OnSizeChanged(null);
- keyboard = new();
- keyboard.SetHook();
- keyboard.OnKeyDownEvent += keyDown;
- keyboard.OnKeyUpEvent += keyUp;
- mouse = new();
- mouse.SetHook();
- mouse.OnMouseMoveEvent += mouseMove;
- mouse.OnMouseDownEvent += mouseDown;
- mouse.OnMouseUpEvent += mouseUp;
- }
- protected override void OnFormClosing(FormClosingEventArgs e)
- {
- base.OnFormClosing(e);
- keyboard.UnHook();
- }
- void keyDown(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.F12:
- Visible = !Visible;
- if (Visible)
- {
- mouseGotoCenter();
- Cursor.Hide();
- mouse.SetHook();
- }
- else
- {
- mouse.UnHook();
- Cursor.Show();
- }
- break;
- case Keys.Q: agent.Q = true; break;
- case Keys.W: agent.W = true; break;
- case Keys.E: agent.E = true; break;
- case Keys.A: agent.A = true; break;
- case Keys.S: agent.S = true; break;
- case Keys.D: agent.D = true; break;
- case Keys.LButton: agent.L = true; break;
- case Keys.RButton: agent.R = true; break;
- }
- }
- void keyUp(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.Q: agent.Q = false; break;
- case Keys.W: agent.W = false; break;
- case Keys.E: agent.E = false; break;
- case Keys.A: agent.A = false; break;
- case Keys.S: agent.S = false; break;
- case Keys.D: agent.D = false; break;
- case Keys.LButton: agent.L = false; break;
- case Keys.RButton: agent.R = false; break;
- }
- }
- void mouseDown(object sender, MouseEventArgs e)
- {
- switch (e.Button)
- {
- case MouseButtons.Left: agent.L = true; break;
- case MouseButtons.Right: agent.R = true; break;
- }
- }
- void mouseUp(object sender, MouseEventArgs e)
- {
- switch (e.Button)
- {
- case MouseButtons.Left: agent.L = false; break;
- case MouseButtons.Right: agent.R = false; break;
- }
- }
- int sx, sy;
- void mouseMove(object sender, MouseEventArgs e)
- {
- sx = e.X - screenCenter.X;
- sy = e.Y - screenCenter.Y;
- }
- void ControllerGUI_Resize(object sender, EventArgs e)
- {
- LayoutRight.Size = new(Width / 4, LayoutRight.Height);
- sizeEvent?.Perform(p => p.OnSizeChanged());
- }
- void mouseGotoCenter()
- {
- Win32Api.mouse_event(Win32Api.MOUSEEVENTF_MOVE | Win32Api.MOUSEEVENTF_ABSOLUTE,
- screenCenter.X * 65535 / screenRightDown.X,
- screenCenter.Y * 65535 / screenRightDown.Y, 0, 0);
- }
- private void mouseTimer_Tick(object sender, EventArgs e)
- {
- if (!Visible) return;
- agent.Move(++sx, ++sy);
- mouseGotoCenter();
- }
- void timer1_Tick(object sender, EventArgs e)
- {
- if (!Visible) return;
- DirectionRule.Degree = dataSource?.Yaw ?? 0;
- roulette.Degree = dataSource?.Roll ?? 0;
- agent.SendFrame();
- agent.ResetMove();
- }
- }
- }
|