-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseToJoystickYuzu.ahk
216 lines (175 loc) · 4.1 KB
/
MouseToJoystickYuzu.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#MaxHotkeysPerInterval 500
#SingleInstance,Force
#NoEnv
#include <MouseDelta>
#Include <SystemCursor>
#Include <LogGui>
#Include <ClipCursor>
;Parameters
; key bindings
KEY_MAP := { RIGHT : "L", DOWN : "I", LEFT : "J", UP : "K"}
; pix/sec. larger value - slower movement
MOUSE_SPEED_CAP = 100
; sec. larger value - larger lag, but larger accuracy
SAMPLING_RATE = 0.2
;UI
VERBOSE_LOG := False
logGui := new LogGui("Mouse to joystick","Hit F12 to toggle on / off", VERBOSE_LOG)
; init private variables
maxPixelsPerSample := MOUSE_SPEED_CAP * SAMPLING_RATE
dutyCycleFull := SAMPLING_RATE * 1000
keyUpTasks := {}
bufX=0
bufY=0
; init private UI variables
MacroOn := 0
md := new MouseDelta("MouseEvent")
return
GuiClose:
md.Delete()
md := ""
ExitApp
F12::
MacroOn := !MacroOn
SystemCursor(!MacroOn)
md.SetState(MacroOn)
toggleTimer()
clipCursorInYuzu(MacroOn)
return
; Additional key bindings
; #if MacroOn
; LButton::v
; #If
; #if MacroOn
; RButton::Right
; #If
; Clip Cursor
clipCursorInYuzu(IsClipped){
; WinGetPos VarX, VarY, Width, Height, "yuzu "
; VarX2 := VarX + Width
; VarY2 := VarY + Height
; logGui.logV("clipCursor VarX: " VarX ", VarY: " VarY ", VarX2:" VarX2 ", VarY2:" VarY2)
; ClipCursor(IsClipped, VarX, VarY, VarX2, VarY2)
WinGetPos, X, Y, Width, Height, yuzu
SysGet, XFrame, 32
SysGet, YFrame, 33
SysGet, caption, 4
X2 := Width - XFrame
Y2 := Height - YFrame
X := X + XFrame
Y := Y + caption + YFrame
X := 10
logGui.logV("clipCursor X: " X ", Y: " Y ", X2:" X2 ", Y2:" Y2)
return ClipCursor(IsClipped, X, Y, X2, Y2)
}
; Gets called when mouse moves
; x and y are DELTA moves (Amount moved since last message), NOT coordinates.
MouseEvent(MouseID, x := 0, y := 0){
static LastTime := 0
t := A_TickCount
logGui.logV("x: " x ", y: " y (LastTime ? (", Delta Time: " t - LastTime " ms, MouseID: " MouseID) : ""))
LastTime := t
fillBuffer(x,y)
}
;TODO: refactor, create class and move it in separate file, remove global variables
;mouse to joystick
fillBuffer(x,y){
global bufX
global bufY
bufX += x
bufY += y
}
flushBuffer(){
global bufX
global bufY
bufX=0
bufY=0
}
; timer
toggle=0
toggleTimer(){
global dutyCycleFull
global toggle
If (toggle := !toggle){
SetTimer, timerTickLabel, %dutyCycleFull%
logGui.log("started")
} else{
SetTimer, timerTickLabel, Off
logGui.log("stopped")
}
}
timerTickLabel:
timerTick()
timerTick(){
global KEY_MAP
global bufX
global bufY
if (bufX != 0){
directionX := bufX > 0 ? "RIGHT" : "LEFT"
keyX := KEY_MAP[directionX]
dutyCycle := getDutyCycle(bufX)
pressAndReleaseKey(keyX,dutyCycle)
}
if (bufY != 0){
directionY := bufY > 0 ? "UP" : "DOWN"
keyY := KEY_MAP[directionY]
dutyCycle := getDutyCycle(bufY)
pressAndReleaseKey(keyY,dutyCycle)
}
flushBuffer()
}
getDutyCycle(buf){
global maxPixelsPerSample
global dutyCycleFull
dutyCycleMultiplier := Abs(buf / maxPixelsPerSample)
dutyCycleMultiplier := accelerateFunction(dutyCycleMultiplier)
if (dutyCycleMultiplier > 1.1)
dutyCycleMultiplier := 1.1
dutyCycle := dutyCycleMultiplier * dutyCycleFull
Return dutyCycle
}
; https://www.desmos.com/calculator/mjpywgnm6g
accelerateFunction(x){
ACCELERATE_POINT_X = 0.3
ACCELERATE_POINT_Y = 0.05
if (x < ACCELERATE_POINT_X)
y := (x - 1) * (1 - ACCELERATE_POINT_Y) / (1 - ACCELERATE_POINT_X) + 1
else
y := x * ACCELERATE_POINT_Y / ACCELERATE_POINT_X
Return y
}
; keys
pressAndReleaseKey(key,dutyCycle){
global keyUpTasks
if (dutyCycle is number && dutyCycle > 0){
prevousKeyUpTask := keyUpTasks[key]
if prevousKeyUpTask {
SetTimer, % prevousKeyUpTask, Off ; cancel previous up button task, and schelule new
} else {
pressKey(key)
}
; schedule key up task
keyUpTask := Func("releaseKey").bind(key)
dutyCycleOnse := dutyCycle * -1
SetTimer, % keyUpTask, %dutyCycleOnse%
; save key up task
keyUpTasks[key] := keyUpTask
}
}
pressKey(key){
Send, {%key% down}
logGui.logV(key " down")
}
releaseKey(key){
global keyUpTasks
Send, {%key% up}
keyUpTasks.Delete(Key)
logGui.logV(key " up")
}
; utils
join( strArray ){
s := ""
for i,v in strArray
s .= ", " . v
return substr(s, 3)
}