You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
8.4 KiB

  1. using DamnOverSharp.Helpers;
  2. using DamnOverSharp.WPF.UiLibrary;
  3. using Gma.System.MouseKeyHook;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using System.Windows.Input;
  9. namespace DamnOverSharp.Renderers.WPF
  10. {
  11. internal class WpfInteractionManager
  12. {
  13. private WpfRenderer Owner;
  14. private IKeyboardMouseEvents GlobalMouseHook;
  15. public Point Offset = new Point(0, 0);
  16. public static bool IsShiftDown { get; private set; } = false;
  17. public static bool IsControlDown { get; private set; } = false;
  18. public static bool IsAltDown { get; private set; } = false;
  19. public VirtualControlBase FocusedElement { get; private set; } = null;
  20. internal WpfInteractionManager(WpfRenderer owner)
  21. {
  22. Owner = owner;
  23. GlobalMouseHook = Hook.GlobalEvents();
  24. GlobalMouseHook.MouseDownExt += GlobalMouseDown;
  25. GlobalMouseHook.MouseUpExt += GlobalMouseUp;
  26. GlobalMouseHook.MouseMoveExt += GlobalMouseMove;
  27. GlobalMouseHook.KeyDown += GlobalKeyDown;
  28. GlobalMouseHook.KeyUp += GlobalKeyUp;
  29. }
  30. private void GlobalKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  31. {
  32. Rect windowRect = WindowHelper.GetWindowRectangle(Owner.InternalBitmapRenderer.HookedProcess.MainWindowHandle);
  33. Point cursorPos = MouseHelper.GetCursorPosition();
  34. if (windowRect.Contains(cursorPos))
  35. {
  36. //e.Handled = true;
  37. switch (e.KeyCode)
  38. {
  39. case Keys.LShiftKey:
  40. case Keys.RShiftKey:
  41. IsShiftDown = true;
  42. break;
  43. case Keys.LControlKey:
  44. case Keys.RControlKey:
  45. IsControlDown = true;
  46. break;
  47. case Keys.Alt:
  48. IsAltDown = true;
  49. break;
  50. }
  51. FocusedElement.OnVirtualKeyDown(e.KeyCode);
  52. }
  53. }
  54. private void GlobalKeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  55. {
  56. Rect windowRect = WindowHelper.GetWindowRectangle(Owner.InternalBitmapRenderer.HookedProcess.MainWindowHandle);
  57. Point cursorPos = MouseHelper.GetCursorPosition();
  58. if (windowRect.Contains(cursorPos))
  59. {
  60. //e.Handled = true;
  61. switch (e.KeyCode)
  62. {
  63. case Keys.LShiftKey:
  64. case Keys.RShiftKey:
  65. IsShiftDown = false;
  66. break;
  67. case Keys.LControlKey:
  68. case Keys.RControlKey:
  69. IsControlDown = false;
  70. break;
  71. case Keys.Alt:
  72. IsAltDown = false;
  73. break;
  74. }
  75. FocusedElement.OnVirtualKeyUp(e.KeyCode);
  76. }
  77. }
  78. private void GlobalMouseMove(object sender, MouseEventExtArgs e)
  79. {
  80. Rect windowRect = WindowHelper.GetWindowRectangle(Owner.InternalBitmapRenderer.HookedProcess.MainWindowHandle);
  81. Point cursorPos = MouseHelper.GetCursorPosition();
  82. if (windowRect.Contains(MouseHelper.GetCursorPosition()))
  83. {
  84. Point relativeCursorPos = new Point(cursorPos.X - windowRect.X - Offset.X, cursorPos.Y - windowRect.Y - Offset.Y);
  85. foreach (VirtualControlBase child in WPF_VisualHelper.GetAllChildren(Owner.MainContentGrid).OfType<VirtualControlBase>())
  86. {
  87. if (!child.IsHitTestVisible || child.Visibility != Visibility.Visible || child.Opacity <= 0)
  88. {
  89. continue;
  90. }
  91. if (WPF_VisualHelper.GetVisualRect(child, Owner.ViewBox).Contains(relativeCursorPos))
  92. {
  93. if (!child.VirtualMouseOver)
  94. {
  95. child.VirtualMouseOver = true;
  96. child.OnVirtualMouseEnter();
  97. }
  98. }
  99. else
  100. {
  101. if (child.VirtualMouseOver)
  102. {
  103. child.VirtualMouseOver = false;
  104. child.OnVirtualMouseLeave();
  105. }
  106. }
  107. child.OnVirtualMouseMove(relativeCursorPos);
  108. }
  109. }
  110. }
  111. private void GlobalMouseDown(object sender, MouseEventExtArgs e)
  112. {
  113. Rect windowRect = WindowHelper.GetWindowRectangle(Owner.InternalBitmapRenderer.HookedProcess.MainWindowHandle);
  114. Point cursorPos = MouseHelper.GetCursorPosition();
  115. if (windowRect.Contains(cursorPos))
  116. {
  117. Point relativeCursorPos = new Point(cursorPos.X - windowRect.X - Offset.X, cursorPos.Y - windowRect.Y - Offset.Y);
  118. foreach (FrameworkElement child in WPF_VisualHelper.GetAllChildren(Owner.MainContentGrid).OfType<FrameworkElement>())
  119. {
  120. if (!child.IsHitTestVisible || child.Visibility != Visibility.Visible || child.Opacity <= 0)
  121. {
  122. continue;
  123. }
  124. if (WPF_VisualHelper.GetVisualRect(child, Owner.ViewBox).Contains(relativeCursorPos))
  125. {
  126. e.Handled = true;
  127. if (child is VirtualControlBase)
  128. {
  129. if((child as VirtualControlBase).OnVirtualMouseDown())
  130. {
  131. if(FocusedElement != (child as VirtualControlBase))
  132. {
  133. if (FocusedElement != null)
  134. {
  135. FocusedElement.VirtualFocused = false;
  136. FocusedElement.OnLostVirtualFocus();
  137. }
  138. FocusedElement = (child as VirtualControlBase);
  139. FocusedElement.VirtualFocused = true;
  140. FocusedElement.OnGotVirtualFocus();
  141. Owner.UpdateVisual();
  142. }
  143. (child as VirtualControlBase).VirtualMouseDown = true;
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. private void GlobalMouseUp(object sender, MouseEventExtArgs e)
  152. {
  153. Rect windowRect = WindowHelper.GetWindowRectangle(Owner.InternalBitmapRenderer.HookedProcess.MainWindowHandle);
  154. Point cursorPos = MouseHelper.GetCursorPosition();
  155. if (windowRect.Contains(cursorPos))
  156. {
  157. Point relativeCursorPos = new Point(cursorPos.X - windowRect.X - Offset.X, cursorPos.Y - windowRect.Y - Offset.Y);
  158. foreach (FrameworkElement child in WPF_VisualHelper.GetAllChildren(Owner.MainContentGrid).OfType<FrameworkElement>())
  159. {
  160. if (!child.IsHitTestVisible || child.Visibility != Visibility.Visible || child.Opacity <= 0)
  161. {
  162. continue;
  163. }
  164. if (WPF_VisualHelper.GetVisualRect(child, Owner.ViewBox).Contains(relativeCursorPos))
  165. {
  166. e.Handled = true;
  167. if (child is VirtualControlBase)
  168. {
  169. if ((child as VirtualControlBase).OnVirtualMouseUp())
  170. {
  171. (child as VirtualControlBase).VirtualMouseDown = false;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. public void Destroy()
  180. {
  181. GlobalMouseHook.MouseDownExt -= GlobalMouseDown;
  182. GlobalMouseHook.MouseUpExt -= GlobalMouseUp;
  183. GlobalMouseHook.MouseMoveExt -= GlobalMouseMove;
  184. GlobalMouseHook.Dispose();
  185. }
  186. }
  187. }