Skip to content

Commit

Permalink
feat(turn-action): add wheel adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
c0nstexpr committed Aug 2, 2024
1 parent 780f6fc commit 82cc320
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 82 deletions.
4 changes: 2 additions & 2 deletions Turnbind/Action/BindControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void OnActive(bool active)
m_keysStr,
m_dir,
Setting.PixelPerMs,
Setting.MouseMoveFactor
Setting.WheelFactor
);

if (active) Activate();
Expand All @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions Turnbind/Action/TurnAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
81 changes: 15 additions & 66 deletions Turnbind/Action/TurnTickAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -107,65 +105,19 @@ public double PixelSpeed
}
}

readonly IDisposable m_mouseMoveDisposable;
readonly IDisposable m_mouseWheelDisposable;

readonly ILogger<TurnTickAction> m_log;

Point m_mousePos;

public TurnTickAction(ILogger<TurnTickAction> 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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -214,7 +163,7 @@ void Schedule(System.Action action) => m_scheduler.Schedule<Unit>(
public void Dispose()
{
m_tick.Dispose();
m_mouseMoveDisposable.Dispose();
m_mouseWheelDisposable.Dispose();
m_scheduler.Dispose();
}
}
2 changes: 1 addition & 1 deletion Turnbind/Model/TurnSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class TurnSetting

public double PixelPerMs { get; set; }

public double MouseMoveFactor { get; set; }
public double WheelFactor { get; set; }
}
4 changes: 1 addition & 3 deletions Turnbind/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ SetWinEventHook

EVENT_*

SendInput
LLMHF_*
GetSystemMetrics
SendInput
4 changes: 2 additions & 2 deletions Turnbind/View/KeyBindEditControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Mouse Move Factor:" />
Text="Wheel Factor:" />

<ui:TextBox
Grid.Row="3"
Expand All @@ -131,7 +131,7 @@
VerticalContentAlignment="Center"
BorderBrush="Gray"
BorderThickness="0.5"
Text="{Binding Path=KeyBind.MouseMoveFactor}" />
Text="{Binding Path=KeyBind.WheelFactor}" />
</Grid>
</DockPanel>
</UserControl>
2 changes: 1 addition & 1 deletion Turnbind/View/KeyBindListControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
Header="dir" />
<ui:GridViewColumn
Width="Auto"
DisplayMemberBinding="{Binding MouseMoveFactor, Mode=OneWay}"
DisplayMemberBinding="{Binding WheelFactor, Mode=OneWay}"
Header="factor" />
</ui:GridView.Columns>
</ui:GridView>
Expand Down
6 changes: 3 additions & 3 deletions Turnbind/ViewModel/KeyBindViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public InputKeys Keys

[ObservableProperty]
[Range(double.Epsilon, double.MaxValue)]
double m_mouseMoveFactor;
double m_wheelFactor;

public TurnSetting TurnSetting
{
set
{
Dir = value.Dir;
PixelPerMs = value.PixelPerMs;
MouseMoveFactor = value.MouseMoveFactor;
WheelFactor = value.WheelFactor;
}

get => new()
{
Dir = Dir,
PixelPerMs = PixelPerMs,
MouseMoveFactor = MouseMoveFactor
WheelFactor = WheelFactor
};
}
}

0 comments on commit 82cc320

Please sign in to comment.