ControllerGUI.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using GUI.DataProvider;
  2. using GUI.GUIUtils;
  3. using GUI.Hook;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace GUI
  9. {
  10. public partial class ControllerGUI : Form
  11. {
  12. readonly EventPerformer<ISizeChangedListener> sizeEvent;
  13. readonly CheckGroup<BottomButton> bottomGroup;
  14. readonly KeyboardHook keyboard;
  15. readonly MouseHook mouse;
  16. public IGUIDataSource dataSource;
  17. ComAgent agent => Program.comAgent;
  18. Point screenCenter => new(Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2);
  19. Point screenRightDown => new(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  20. public ControllerGUI()
  21. {
  22. ComConfig guiCfg = new ComConfig();
  23. guiCfg.ShowDialog();
  24. if (Program.comAgent == null)
  25. Process.GetCurrentProcess().Kill();
  26. MessageBox.Show(
  27. "按F12暂停和继续控制程序",
  28. "控制器",
  29. MessageBoxButtons.OK, MessageBoxIcon.Information);
  30. InitializeComponent();
  31. dataSource = agent;
  32. sizeEvent = new();
  33. sizeEvent.AddSafe(new GUIParagraph(BottomLayout));
  34. sizeEvent.AddSafe(new GUIParagraph(LayoutRight)
  35. {
  36. Target = GUIParagraph.TargetXY.Y,
  37. FixedPaneReferenceFunc = (c, f) => c.Size = new(f.Width - 3, c.Height)
  38. });
  39. bottomGroup = new();
  40. bottomGroup.AddSafe(bottomButton1, bottomButton2, bottomButton3);
  41. OnSizeChanged(null);
  42. keyboard = new();
  43. keyboard.SetHook();
  44. keyboard.OnKeyDownEvent += keyDown;
  45. keyboard.OnKeyUpEvent += keyUp;
  46. mouse = new();
  47. mouse.SetHook();
  48. mouse.OnMouseMoveEvent += mouseMove;
  49. mouse.OnMouseDownEvent += mouseDown;
  50. mouse.OnMouseUpEvent += mouseUp;
  51. }
  52. protected override void OnFormClosing(FormClosingEventArgs e)
  53. {
  54. base.OnFormClosing(e);
  55. keyboard.UnHook();
  56. }
  57. void keyDown(object sender, KeyEventArgs e)
  58. {
  59. switch (e.KeyCode)
  60. {
  61. case Keys.F12:
  62. Visible = !Visible;
  63. if (Visible)
  64. {
  65. mouseGotoCenter();
  66. Cursor.Hide();
  67. mouse.SetHook();
  68. }
  69. else
  70. {
  71. mouse.UnHook();
  72. Cursor.Show();
  73. }
  74. break;
  75. case Keys.Q: agent.Q = true; break;
  76. case Keys.W: agent.W = true; break;
  77. case Keys.E: agent.E = true; break;
  78. case Keys.A: agent.A = true; break;
  79. case Keys.S: agent.S = true; break;
  80. case Keys.D: agent.D = true; break;
  81. case Keys.LButton: agent.L = true; break;
  82. case Keys.RButton: agent.R = true; break;
  83. }
  84. }
  85. void keyUp(object sender, KeyEventArgs e)
  86. {
  87. switch (e.KeyCode)
  88. {
  89. case Keys.Q: agent.Q = false; break;
  90. case Keys.W: agent.W = false; break;
  91. case Keys.E: agent.E = false; break;
  92. case Keys.A: agent.A = false; break;
  93. case Keys.S: agent.S = false; break;
  94. case Keys.D: agent.D = false; break;
  95. case Keys.LButton: agent.L = false; break;
  96. case Keys.RButton: agent.R = false; break;
  97. }
  98. }
  99. void mouseDown(object sender, MouseEventArgs e)
  100. {
  101. switch (e.Button)
  102. {
  103. case MouseButtons.Left: agent.L = true; break;
  104. case MouseButtons.Right: agent.R = true; break;
  105. }
  106. }
  107. void mouseUp(object sender, MouseEventArgs e)
  108. {
  109. switch (e.Button)
  110. {
  111. case MouseButtons.Left: agent.L = false; break;
  112. case MouseButtons.Right: agent.R = false; break;
  113. }
  114. }
  115. int sx, sy;
  116. void mouseMove(object sender, MouseEventArgs e)
  117. {
  118. sx = e.X - screenCenter.X;
  119. sy = e.Y - screenCenter.Y;
  120. }
  121. void ControllerGUI_Resize(object sender, EventArgs e)
  122. {
  123. LayoutRight.Size = new(Width / 4, LayoutRight.Height);
  124. sizeEvent?.Perform(p => p.OnSizeChanged());
  125. }
  126. void mouseGotoCenter()
  127. {
  128. Win32Api.mouse_event(Win32Api.MOUSEEVENTF_MOVE | Win32Api.MOUSEEVENTF_ABSOLUTE,
  129. screenCenter.X * 65535 / screenRightDown.X,
  130. screenCenter.Y * 65535 / screenRightDown.Y, 0, 0);
  131. }
  132. private void mouseTimer_Tick(object sender, EventArgs e)
  133. {
  134. if (!Visible) return;
  135. agent.Move(++sx, ++sy);
  136. mouseGotoCenter();
  137. }
  138. void timer1_Tick(object sender, EventArgs e)
  139. {
  140. if (!Visible) return;
  141. DirectionRule.Degree = dataSource?.Yaw ?? 0;
  142. roulette.Degree = dataSource?.Roll ?? 0;
  143. agent.SendFrame();
  144. agent.ResetMove();
  145. }
  146. }
  147. }