From 82cc320c32ca54bcd507e8f57d9f651b263233a4 Mon Sep 17 00:00:00 2001 From: c0nstexpr <976999484@qq.com> Date: Fri, 2 Aug 2024 21:08:18 +0800 Subject: [PATCH] feat(turn-action): add wheel adjust --- Turnbind/Action/BindControl.cs | 4 +- Turnbind/Action/TurnAction.cs | 8 +-- Turnbind/Action/TurnTickAction.cs | 81 +++++--------------------- Turnbind/Model/TurnSetting.cs | 2 +- Turnbind/NativeMethods.txt | 4 +- Turnbind/View/KeyBindEditControl.xaml | 4 +- Turnbind/View/KeyBindListControl.xaml | 2 +- Turnbind/ViewModel/KeyBindViewModel.cs | 6 +- 8 files changed, 29 insertions(+), 82 deletions(-) diff --git a/Turnbind/Action/BindControl.cs b/Turnbind/Action/BindControl.cs index 0a432df..8e12b8b 100644 --- a/Turnbind/Action/BindControl.cs +++ b/Turnbind/Action/BindControl.cs @@ -99,7 +99,7 @@ void OnActive(bool active) m_keysStr, m_dir, Setting.PixelPerMs, - Setting.MouseMoveFactor + Setting.WheelFactor ); if (active) Activate(); @@ -117,7 +117,7 @@ void Deactivate() void Activate() { m_turnAction.PixelPerMs = Setting.PixelPerMs; - m_turnAction.MouseFactor = Setting.MouseMoveFactor; + m_turnAction.WheelFactor = Setting.WheelFactor; m_instructionIndex = m_turnAction.InputDirection(m_dir); } diff --git a/Turnbind/Action/TurnAction.cs b/Turnbind/Action/TurnAction.cs index 585163c..7b0eff1 100644 --- a/Turnbind/Action/TurnAction.cs +++ b/Turnbind/Action/TurnAction.cs @@ -49,14 +49,14 @@ private set } } - public double MouseFactor + public double WheelFactor { - get => action.MouseFactor; + get => action.WheelFactor; set { - action.MouseFactor = value; + action.WheelFactor = value; OnPropertyChanged(); - log.LogInformation("Set MouseFactor {factor}", value); + log.LogInformation("Set WheelFactor {factor}", value); } } diff --git a/Turnbind/Action/TurnTickAction.cs b/Turnbind/Action/TurnTickAction.cs index e04920c..88837b1 100644 --- a/Turnbind/Action/TurnTickAction.cs +++ b/Turnbind/Action/TurnTickAction.cs @@ -3,17 +3,15 @@ using System.Reactive.Disposables; using System.Reactive; using System.Reactive.Linq; -using System.Drawing; using System.Runtime.InteropServices; using Windows.Win32.UI.Input.KeyboardAndMouse; -using Windows.Win32.UI.WindowsAndMessaging; -using Turnbind.Helper; +using System.Reactive.Concurrency; namespace Turnbind.Action; sealed class TurnTickAction : IDisposable { - readonly System.Reactive.Concurrency.EventLoopScheduler m_scheduler = new(); + readonly EventLoopScheduler m_scheduler = new(); readonly SerialDisposable m_tick = new(); @@ -78,17 +76,17 @@ public TurnInstruction Instruction public TimeSpan Interval { get; set; } - double m_currentMouseFactor; + double m_currentWheelFactor; - double m_mouseFactor; + double m_wheelFactor; - public double MouseFactor + public double WheelFactor { - get => m_currentMouseFactor; + get => m_currentWheelFactor; set { - m_currentMouseFactor = value; - Schedule(() => m_mouseFactor = value); + m_currentWheelFactor = value; + Schedule(() => m_wheelFactor = value); } } @@ -107,65 +105,19 @@ public double PixelSpeed } } - readonly IDisposable m_mouseMoveDisposable; + readonly IDisposable m_mouseWheelDisposable; readonly ILogger m_log; - Point m_mousePos; - public TurnTickAction(ILogger log, InputAction inputAction) { m_log = log; - m_mouseMoveDisposable = inputAction.MouseMoveRaw - .Select(p => Tuple.Create(p, Instruction != TurnInstruction.Stop)) + m_mouseWheelDisposable = inputAction.MouseRaw + .Where(e => e.Event == PInvoke.WM_MOUSEWHEEL && Instruction != TurnInstruction.Stop) + .Select(e => (short)(e.Data.mouseData >> 16)) .SubscribeOn(m_scheduler) - .Select( - t => - { - var (mouse, isTurning) = t; - var p = mouse.pt; - long delta = 0; - - if (isTurning && !IsInjected(mouse.flags)) - delta += p.X - m_mousePos.X; - - m_mousePos = p; - - return delta; - } - ) - .Buffer(3) - .Subscribe( - d => - { - long plusSum = 0; - long minusSum = 0; - - byte plusCount = 0; - byte minusCount = 0; - - foreach (var i in d) - if (i > 0) - { - plusSum += i; - ++plusCount; - } - else - { - minusSum += i; - ++minusCount; - } - - m_delta += plusCount >= minusCount ? plusSum : minusSum; - } - ); - } - - static bool IsInjected(uint flags) - { - const uint test = PInvoke.LLMHF_INJECTED | PInvoke.LLMHF_LOWER_IL_INJECTED; - return (flags & test) != 0; + .Subscribe(d => m_delta += d); } #region Tick @@ -178,9 +130,7 @@ static bool IsInjected(uint flags) Unit Tick(Unit u) { - m_log.LogDebug("Current delta:{delta}", m_delta); - - var x = m_speed + m_delta * m_mouseFactor + m_remain; + var x = m_speed + m_delta * m_wheelFactor + m_remain; var round_x = (int)Math.Round(x); m_mouseInput.dx = round_x; @@ -191,7 +141,6 @@ Unit Tick(Unit u) return u; } - m_log.LogDebug("Sended mouse move, x:{x}", round_x); m_remain = x - round_x; return u; @@ -214,7 +163,7 @@ void Schedule(System.Action action) => m_scheduler.Schedule( public void Dispose() { m_tick.Dispose(); - m_mouseMoveDisposable.Dispose(); + m_mouseWheelDisposable.Dispose(); m_scheduler.Dispose(); } } \ No newline at end of file diff --git a/Turnbind/Model/TurnSetting.cs b/Turnbind/Model/TurnSetting.cs index c8b6fd1..343a882 100644 --- a/Turnbind/Model/TurnSetting.cs +++ b/Turnbind/Model/TurnSetting.cs @@ -6,5 +6,5 @@ public class TurnSetting public double PixelPerMs { get; set; } - public double MouseMoveFactor { get; set; } + public double WheelFactor { get; set; } } diff --git a/Turnbind/NativeMethods.txt b/Turnbind/NativeMethods.txt index 1b0063f..b678c7e 100644 --- a/Turnbind/NativeMethods.txt +++ b/Turnbind/NativeMethods.txt @@ -14,6 +14,4 @@ SetWinEventHook EVENT_* -SendInput -LLMHF_* -GetSystemMetrics \ No newline at end of file +SendInput \ No newline at end of file diff --git a/Turnbind/View/KeyBindEditControl.xaml b/Turnbind/View/KeyBindEditControl.xaml index 176ac82..996e821 100644 --- a/Turnbind/View/KeyBindEditControl.xaml +++ b/Turnbind/View/KeyBindEditControl.xaml @@ -122,7 +122,7 @@ Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center" - Text="Mouse Move Factor:" /> + Text="Wheel Factor:" /> + Text="{Binding Path=KeyBind.WheelFactor}" /> diff --git a/Turnbind/View/KeyBindListControl.xaml b/Turnbind/View/KeyBindListControl.xaml index ab1d073..c3ca77e 100644 --- a/Turnbind/View/KeyBindListControl.xaml +++ b/Turnbind/View/KeyBindListControl.xaml @@ -44,7 +44,7 @@ Header="dir" /> diff --git a/Turnbind/ViewModel/KeyBindViewModel.cs b/Turnbind/ViewModel/KeyBindViewModel.cs index 75d6858..7da55a1 100644 --- a/Turnbind/ViewModel/KeyBindViewModel.cs +++ b/Turnbind/ViewModel/KeyBindViewModel.cs @@ -32,7 +32,7 @@ public InputKeys Keys [ObservableProperty] [Range(double.Epsilon, double.MaxValue)] - double m_mouseMoveFactor; + double m_wheelFactor; public TurnSetting TurnSetting { @@ -40,14 +40,14 @@ public TurnSetting TurnSetting { Dir = value.Dir; PixelPerMs = value.PixelPerMs; - MouseMoveFactor = value.MouseMoveFactor; + WheelFactor = value.WheelFactor; } get => new() { Dir = Dir, PixelPerMs = PixelPerMs, - MouseMoveFactor = MouseMoveFactor + WheelFactor = WheelFactor }; } }