-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxControllerUtil.lua
362 lines (260 loc) · 8.5 KB
/
xControllerUtil.lua
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
-- ------------------------------------------
-- Controller Utilities
-- by: Xsear
-- ------------------------------------------
-- Lua
require "unicode"
require "math"
require "table"
-- Firefall
require "lib/lib_Debug"
require "lib/lib_Items"
require "lib/lib_Slash"
require "lib/lib_ChatLib"
require "lib/lib_Callback2"
require "lib/lib_Tabs"
require "lib/lib_MovablePanel"
require "lib/lib_PanelManager"
require "lib/lib_MultiArt"
require "lib/lib_RoundedPopupWindow"
require "lib/lib_UserKeybinds"
require "lib/lib_InputIcon"
-- Custom
require "./util/optionsPopupWindow"
-- Addon
require "./Pizza"
require "./OptionsUI"
require './DaisyWheel'
-- ------------------------------------------
-- CONSTANTS
-- ------------------------------------------
CVAR_ALLOW_GAMEPAD = "control.allowGamepad"
REDETECTION_DELAY_SECONDS = 1
SIN_ABILITY_ID = "43"
KEYCODE_GAMEPAD_START = 270
KEYCODE_GAMEPAD_BACK = 271
KEYCODE_GAMEPAD_LEFT_THUMBSTICK = 272
KEYCODE_GAMEPAD_RIGHT_THUMBSTICK = 273
KEYCODE_GAMEPAD_LEFT_BUMPER = 274
KEYCODE_GAMEPAD_RIGHT_BUMPER = 275
KEYCODE_GAMEPAD_LEFT_TRIGGER = 280
KEYCODE_GAMEPAD_RIGHT_TRIGGER = 281
KEYCODE_GAMEPAD_X = 278
KEYCODE_GAMEPAD_Y = 279
KEYCODE_GAMEPAD_B = 277
KEYCODE_GAMEPAD_A = 276
KEYCODE_GAMEPAD_DPAD_UP = 266
KEYCODE_GAMEPAD_DPAD_DOWN = 267
KEYCODE_GAMEPAD_DPAD_LEFT = 268
KEYCODE_GAMEPAD_DPAD_RIGHT = 269
ABILITY_PIZZA_KEYBINDINGS = {
[KEYCODE_GAMEPAD_X] = "SelectAbility1",
[KEYCODE_GAMEPAD_Y] = "SelectAbility2",
[KEYCODE_GAMEPAD_B] = "SelectAbility3",
[KEYCODE_GAMEPAD_A] = "SelectAbility4",
}
ABILITY_PIZZA_KEYBINDINGS_ORDER = {
[1] = KEYCODE_GAMEPAD_X,
[2] = KEYCODE_GAMEPAD_Y,
[3] = KEYCODE_GAMEPAD_B,
[4] = KEYCODE_GAMEPAD_A,
}
PIZZA_KEYBINDINGS_KEYCODE_INDEX = {
[KEYCODE_GAMEPAD_X] = 1,
[KEYCODE_GAMEPAD_Y] = 2,
[KEYCODE_GAMEPAD_B] = 3,
[KEYCODE_GAMEPAD_A] = 4,
}
ABILITY_PIZZA_KEYBINDING_INDEX = 3 -- So since each key action can have multiple binds, but the UI options only utilize the first 2, we put our stuff on 3. Putting it on higher litters the savefile with empty slots.
ABILITY_PIZZA_CANCELLATION_KEYS = {
KEYCODE_GAMEPAD_START, KEYCODE_GAMEPAD_BACK, KEYCODE_GAMEPAD_RIGHT_BUMPER
}
RT_SEG_WIDTH = 1024/3 -- Still used in pizza creation!
SUPER_CALLDOWN_PIZZA_KEYBINDINGS = {
[KEYCODE_GAMEPAD_X] = "SelectAbility5",
[KEYCODE_GAMEPAD_Y] = "SelectAbility6",
[KEYCODE_GAMEPAD_B] = "SelectAbility7",
[KEYCODE_GAMEPAD_A] = "SelectAbility8",
}
-- ------------------------------------------
-- GLOBALS
-- ------------------------------------------
-- SIN Notification Timestamp
g_NotificationsSINTriggerTimestamp = nil
-- ------------------------------------------
-- INTERFACE OPTIONS
-- ------------------------------------------
-- ------------------------------------------
-- LOAD
-- ------------------------------------------
function OnComponentLoad(args)
-- Debug
Debug.EnableLogging(true)
-- Pizza
Pizza_OnComponentLoad()
-- Options UI
OptionsUI_OnComponentLoad()
-- Daisy Wheel
DaisyWheel_OnComponentLoad()
-- Slash
LIB_SLASH.BindCallback({slash_list="xcontrollerutil,xconutil,xcu,cu", description="Controller Utilities", func=OnSlashGeneral})
LIB_SLASH.BindCallback({slash_list="redetect,gamepad", description="Attempt to detect active gamepad", func=OnSlashGamepad})
end
function OnPreReloadUI(args)
if DaisyWheel_IsActive() then
DaisyWheel_Deactivate()
end
Pizza_DeactivationTrigger(args)
end
-- ------------------------------------------
-- EVENTS
-- ------------------------------------------
function OnSlashGeneral(args)
Debug.Table("OnSlashGeneral", args)
if args[1] then
if args[1] == "daisy" then
OnSlashDaisy(args)
end
else
ToggleOptionsUI({show=true})
end
end
function OnSlashGamepad(args)
Debug.Table("OnSlashGamepad", args)
DetectActiveGamepad()
end
function OnClose(args)
ToggleOptionsUI({show=false})
end
function OnToggleDefaultUI(args)
Debug.Table("OnToggleDefaultUI", args)
-- Determine whether UI is being shown
local show = args.visible or args.show or false
-- If UI is being shown, we must disable ability pizzas
if show then
-- In case pizzas are shown, deactivate them
Pizza_DeactivationTrigger(args)
-- Disable activation keybinds
g_KeySet_PizzaActivators:Activate(false)
-- Workaround for keyset issues
KeySetActiveFix(g_KeySet_Daisy_DPAD)
KeySetActiveFix(g_KeySet_Daisy_XYAB)
--Output("Pizza Buttons Disabled")
-- If UI is being hidden, we should re-enable ability pizzas
else
-- If pizza action was disabled
if not g_KeySet_PizzaActivators:IsActive() then
-- Activate activaiton keybinds
g_KeySet_PizzaActivators:Activate(true)
--Output("Pizza Buttons Enabled")
end
end
end
-- If two keysets have the same key bound, then activating one of them unbinds the other keyset without "deactivating it", breaking it.
function KeySetActiveFix(KEYSET)
if KEYSET:IsActive() then
KEYSET:Activate(false)
KEYSET:Activate(true)
end
end
function OnPlayerReady(args)
Pizza_UpdateAll(args)
if not OptionsUI.haveSetupBars then
SetupOptionsUIBarList()
OptionsUI.haveSetupBars = true
end
end
function OnBattleframeChanged(args)
Pizza_UpdateAll(args)
end
function OnAbilitiesChanged(args) -- Fired when you swap abilities on the 3dactionbar (without changing slot)
Pizza_UpdateAll(args)
end
function OnAbilityUsed(args)
-- Catch SIN activation
if tostring(args.id) == SIN_ABILITY_ID then
-- Only with gamepad
if Player.IsUsingGamepad() then
-- If we have a timestamp
if g_NotificationsSINTriggerTimestamp ~= nil then
-- Compare the elapsed time to check for double tap
if System.GetElapsedUnixTime(g_NotificationsSINTriggerTimestamp) == 0 then
TriggerNotificationUI()
end
end
-- Set timestamp
g_NotificationsSINTriggerTimestamp = System.GetLocalUnixTime()
end
-- Normal logic
else
Pizza_DeactivationTrigger(args)
end
end
function OnAbilityFailed(args)
Pizza_DeactivationTrigger(args)
end
function OnPlaceCalldown(args)
Pizza_DeactivationTrigger(args)
end
-- ------------------------------------------
-- GENERAL FUNCTIONS
-- ------------------------------------------
function ToggleOptionsUI(args)
local show = args.show or false
OptionsUI.MAIN:Show(show)
Component.SetInputMode(show and "cursor" or "none");
if (show) then
PanelManager.OnShow(OptionsUI.MAIN)
else
PanelManager.OnHide(OptionsUI.MAIN)
end
end
function DetectActiveGamepad(args)
-- Get cvar value
local isGamepadEnabled = System.GetCvar(CVAR_ALLOW_GAMEPAD)
-- If gamepad is enabled, toggle in order to cause redetection
if isGamepadEnabled then
Output("Redetecting Gamepad")
System.SetCvar(CVAR_ALLOW_GAMEPAD, false)
Callback2.FireAndForget(function() System.SetCvar(CVAR_ALLOW_GAMEPAD, true) end, nil, REDETECTION_DELAY_SECONDS)
-- If gamepad was disabled, we just enable it right away
else
Output("Enabling Gamepad")
System.SetCvar(CVAR_ALLOW_GAMEPAD, true)
end
end
function TriggerNotificationUI(args)
Component.GenerateEvent("MY_NOTIFICATION_TOGGLE", {show=true})
end
-- Arkii did the maths <3
function GetPointOnCricle(orginX, orignY, radius, angle)
local res = { x = 0, y = 0 }
res.x = (radius * math.cos(angle * math.pi / 180)) + orginX
res.y = (radius * math.sin(angle * math.pi / 180)) + orignY
return res
end
function IsKeysetActionRegistered(KEYSET, action_name)
local export = KEYSET:ExportKeybinds()
return (export[action_name] ~= nil)
end
-- ------------------------------------------
-- UTILITY/RETURN FUNCTIONS
-- ------------------------------------------
function Output(text)
local args = {
text = "[xCU] " .. tostring(text),
}
ChatLib.SystemMessage(args);
end
function _table.empty(table)
if not table or next(table) == nil then
return true
end
return false
end
function widgetInfo(widget)
assert(widget)
Debug.Log("Widget ", (Component.IsWidget(widget) and "is a widget") or "is not a widget")
local name, kind = widget:GetInfo()
Debug.Log("WidgetGetInfo", "name", tostring(name), "type", tostring(kind))
end