-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclass_Joystick.ahk
186 lines (156 loc) · 4.81 KB
/
class_Joystick.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
Class Joystick { ; example: https://gist.github.com/A-AhkUser/4627f2014352650115ad0cc4fa47ac51
; cf. https://autohotkey.com/docs/scripts/JoystickTest.htm
; cf. https://autohotkey.com/docs/scripts/JoystickMouse.htm
connect(_port:=0) {
Loop % !(_port) {
Loop 16 {
if (GetKeyState(a_index . "JoyX")) {
this.name := GetKeyState((_prefix:=this._prefix:=(this.port:=a_index) . "Joy") . "Name")
break, 2
}
}
return false
}
(_buttons:=this.buttons:=[])[ GetKeyState(_prefix . "Buttons") ] := ""
Loop % _buttons.length()
_buttons[ a_index ] := _prefix . a_index
this.thumbsticks := {}, this.thumbsticks.L := new Joystick.Thumbstick(this, "X", "Y")
_info := this.info := GetKeyState(_prefix . "Info")
if (this.hasPOV:=(InStr(_info, "P") && InStr(_info, "D")))
this.POV := _prefix . "POV", this.dPad := new Joystick.DPad(this)
if (this.hasZRAxis:=(InStr(_info, "Z") && InStr(_info, "R")))
this.thumbsticks.R := new Joystick.Thumbstick(this, "Z", "R")
return this.connected
}
_isConnected := false
connected {
set {
static _callbacks := {(true): "__connection", (false): "__disconnected"}
((_f:=this[ _callbacks[ this._isConnected:=value ] ]) && _f.call(this))
return value
}
get {
return ((_state:=(GetKeyState(this.X) <> "")) <> this._isConnected) ? this.connected:=_state : _state
}
}
onConnection(_callback) {
return Bound.Func._setCallback(this, "__connection", _callback)
}
onDisconnected(_callback) {
return Bound.Func._setCallback(this, "__disconnected", _callback)
}
dispose() {
for _axis, _thumbstick in this.thumbsticks
_thumbstick.dispose(), _thumbstick := ""
this.thumbsticks := ""
if (this.hasPOV)
this.dPad.dispose(), this.dPad := ""
this.__connection := this.__disconnected := ""
}
Class Axes {
eventMonitor := ""
__New(_device, _params*) {
this._device := _device, this._watcher := new Bound.Func.Iterator(this, "_spot", _params*)
}
setEventMonitor(_eventMonitor) {
return Bound.Func._setCallback(this, "eventMonitor", _eventMonitor)
}
watch(_period:="Off") {
if (this.eventMonitor)
return true, this._watcher.setPeriod(_period)
return false
}
dispose() {
this.watch(), this._watcher.delete(), this.eventMonitor := ""
}
}
Class DPad extends Joystick.Axes {
__New(_device) {
base.__New(_device, _device["POV"]:=_device._prefix . "POV")
this._keyToHoldDown := [], this.invertAxis := false
}
_spot(_POV) {
_listLines := A_ListLines
ListLines, Off
static _keyToHoldDown
if not (this._device.connected) {
this._watcher.setPeriod("Off")
return
}
if ((_POV:=GetKeyState(_POV)) < 0)
return
else if _POV between 22500 and 31500
_keyToHoldDown := this._keyToHoldDown.1
else if _POV between 4500 and 13500
_keyToHoldDown := this._keyToHoldDown.2
else if _POV between 13501 and 22500
_keyToHoldDown := this._keyToHoldDown.3
else _keyToHoldDown := this._keyToHoldDown.4
ListLines % _listLines ? "On" : "Off"
this.eventMonitor.call(this._device, _keyToHoldDown)
}
invertAxis {
set {
this._keyToHoldDown := (this._invertAxis:=value) ? StrSplit("Right|Left|Up|Down", "|") : StrSplit("Left|Right|Down|Up", "|")
return value
}
get {
return this._invertAxis
}
}
}
Class Thumbstick extends Joystick.Axes {
__New(_device, _axis1, _axis2, _threshold:=17, _multiplier:=0.30) {
this.axes := [_axis1, _axis2]
base.__New(_device, _device[_axis1]:=_device._prefix . _axis1, _device[_axis2]:=_device._prefix . _axis2)
this.threshold := _threshold, this.multiplier := _multiplier
this.invertAxis := false
}
_spot(_XOrZ, _YOrR) {
_listLines := A_ListLines
ListLines, Off
static _δX, _δY
if not (this._device.connected) {
this._watcher.setPeriod("Off")
return
}
_format := A_FormatFloat
SetFormat, float, 03
_XOrZ := GetKeyState(_XOrZ), _YOrR := GetKeyState(_YOrR)
_u := this._thresholdUpper, _l := this._thresholdLower
_boolean := false
if (_XOrZ > _u) {
_boolean := true, _δX := (_XOrZ - _u) * this.multiplier
} else if (_XOrZ < _l) {
_boolean := true, _δX := (_XOrZ - _l) * this.multiplier
} else _δX := 0
if (_YOrR > _u) {
_boolean := true, _δY := (_YOrR - _u) * this.multiplier
} else if (_YOrR < _l) {
_boolean := true, _δY := (_YOrR - _l) * this.multiplier
} else _δY := 0
ListLines % _listLines ? "On" : "Off"
if (_boolean) {
this.eventMonitor.call(this._device, this._δXm * _δX, this._δYm * _δY)
}
SetFormat, float, % _format
}
invertAxis {
set {
this._δXm := this._δYm := (this._invertAxis:=value) ? -1 : 1
return value
}
get {
return this._invertAxis
}
}
threshold {
set {
return this._threshold := value, this._thresholdLower := 50 - value, this._thresholdUpper := 50 + value
}
get {
return this._threshold
}
}
}
}