-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.luau
510 lines (428 loc) · 15.2 KB
/
init.luau
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
--!strict
local types = require(script.Parent.types)
local Encoder = require(script.Parent.Encoder)
local Hook = require(script.Parent.Hook)
local RunService = game:GetService("RunService")
local IsServer = RunService:IsServer()
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")
local GamepadService = game:GetService("GamepadService")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = nil :: PlayerGui?
local Camera = workspace.CurrentCamera
local Keycodes = Enum.KeyCode:GetEnumItems()
local function searchkeycodes(A: {Enum.KeyCode}, enumvalue: number): Enum.KeyCode?
local L = 1
local R = #A
if L > R then
return nil
else
local idx = (L + R) // 2
local value = A[idx]
local result = value.Value == enumvalue and 0 or value.Value < enumvalue and 1 or 2
if result == 0 then
return value
else
if result == 1 then
L = idx + 1
elseif result == 2 then
R = idx - 1
end
return searchkeycodes(table.move(A, L, R, 1, table.create(L + R)), enumvalue)
end
end
end
--- ### Inputs.luau
---
--- `UserInputService` wrapper \
--- compatible with pc, mobile, gamepad \
--- \
--- PlatformId `0`: unknown \
--- PlatformId `1`: pc \
--- PlatformId `2`: mobile \
--- PlatformId `3`: gamepad
local Inputs = {
TouchEnabled = UIS.TouchEnabled;
PlatformId = 0;
InputTimes = {};
DoubleTapTime = 0.2;
MobileScreenCenter = nil :: (() -> Vector2)?;
--- write externally
MoveVector = Vector3.zero;
CursorBlacklist = table.create(16);
GuiInset = Vector2.zero;
Aliases = require(script.Aliases);
Platforms = require(script.Platforms);
Gamepad2D = script:WaitForChild("Gamepad2D");
Gamepad2DMap = require(script.Gamepad2DMap);
DPad2D = script:WaitForChild("DPad2D");
DPad2DMap = require(script.DPad2DMap);
Select2DColor = Color3.fromRGB(255, 255, 255);
Deselect2DColor = Color3.fromRGB(0, 0, 0);
}
local BlacklistParams = RaycastParams.new()
BlacklistParams.FilterType = Enum.RaycastFilterType.Exclude
BlacklistParams.IgnoreWater = true
BlacklistParams.FilterDescendantsInstances = Inputs.CursorBlacklist
local Cursor: types.Cursor = {
Position = Vector3.zero;
Normal = Vector3.yAxis;
Camera = -Vector3.zAxis;
Instance = nil;
}
local function rayblacklist(origin: Vector3, direction: Vector3)
return workspace:Raycast(origin, direction, BlacklistParams)
end
--- returns mutable input-cursor, describing client input
function Inputs.cursor(): types.Cursor
return Cursor
end
--- adds instance to cursor blacklist
function Inputs.cursorblacklistadd(thing: Instance)
if not table.find(Inputs.CursorBlacklist, thing) then
table.insert(Inputs.CursorBlacklist, thing)
end
BlacklistParams.FilterDescendantsInstances = Inputs.CursorBlacklist
end
--- removes instance from cursor blacklist
function Inputs.cursorblacklistremove(thing: Instance)
table.remove(Inputs.CursorBlacklist, table.find(Inputs.CursorBlacklist, thing) or 0)
BlacklistParams.FilterDescendantsInstances = Inputs.CursorBlacklist
end
--- packs cursor vectors into a buffer
function Inputs.cursorpack()
local c = buffer.create(12+3+3) --- totally folded to 18
Encoder.writevector3(c, 0, Cursor.Position)
Encoder.writeunitvector(c, 12, Cursor.Normal)
Encoder.writeunitvector(c, 15, Cursor.Camera)
return c
end
--- unpacks cursor-buffer:
--- `Position`: cursor world position
--- `Normal`: cursor surface normal
--- `Camera`: camera normal
function Inputs.cursorunpack(c: buffer)
return
Encoder.readvector3(c, 0),
Encoder.readunitvector(c, 12),
Encoder.readunitvector(c, 15)
end
--- returns key-value pair bound to keycode with respect to internal platform id \
--- `Enum.KeyCode.Unknown (value 0)` immediately returns nil
function Inputs.keybind(keycode: Enum.KeyCode, k: {[string]: types.Keybind}): (string?, types.Keybind?)
local platformid = Inputs.PlatformId
if platformid == 0 then
return
elseif keycode == Enum.KeyCode.Unknown then
return nil, nil
else
for keyid, keybind in k do
local reals = k[keyid].Reals
local defaults = k[keyid].Defaults
assert(type(reals) == "table")
assert(type(defaults) == "table")
local boundkey = reals[platformid] or defaults[platformid]
if boundkey == keycode.Value then
return keyid, keybind
end
end
end
return
end
--- folds saved keybinds `j` into read-write keybind table `k`
function Inputs.keybindfold(j: {[string]: {number}}, k: {[string]: types.Keybind})
for keyid, keybinds in j do
local reals = k[keyid].Reals
assert(type(reals) == "table")
local defaults = k[keyid].Defaults
assert(type(defaults) == "table")
table.move(defaults, 1, #defaults, 1, reals)
table.move(keybinds, 1, #keybinds, 1, reals)
end
end
--- resets keybind of `k`
function Inputs.keybindreset(keyid: string, k: {[string]: types.Keybind})
local reals = k[keyid].Reals
assert(type(reals) == "table")
local defaults = k[keyid].Defaults
assert(type(defaults) == "table")
table.move(defaults, 1, #defaults, 1, reals)
end
--- compiles read-write keybind table `k` into a savable format
function Inputs.keybindcompile(k: {[string]: types.Keybind}): {[string]: {number}}
local j = {}
for keyid, keybind in k do
local reals = k[keyid].Reals
assert(type(reals) == "table")
j[keyid] = table.clone(reals)
end
return j
end
--- asserts keybinds `j` are of read-write keybind table `k`
function Inputs.keybindassert(j: {[string]: {number}}, k: {[string]: types.Keybind})
assert(type(j) == "table")
for keyid, keybinds in j do
assert(k[keyid] ~= nil)
assert(type(keyid) == "string")
assert(type(keybinds) == "table")
assert(#keybinds <= #k[keyid].Defaults)
end
end
--- returns an enumeration of keybind aliases with respect to keybind table `k`
function Inputs.keybindenumerate(keyids: {string}, k: {[string]: types.Keybind})
local platformid = Inputs.PlatformId
if platformid ~= 0 then
local enumeration = table.create(#keyids)
for i, keyid in ipairs(keyids) do
local defaults = k[keyid].Defaults :: {number}
local reals = k[keyid].Reals :: {number}
local keycode = searchkeycodes(Keycodes, reals[platformid] or defaults[platformid])
table.insert(enumeration, i, Inputs.Aliases[keycode::any] or keycode and keycode.Name or keyid) --- remove ::any when solver grows a brain
end
return enumeration
end
return keyids
end
--- returns associated mouse button `Enum.KeyCode` with respect to inputobject
function Inputs.inputobjecttomousekeycode(inputobject: InputObject): Enum.KeyCode?
local inputtype = inputobject.UserInputType
if inputtype == Enum.UserInputType.MouseButton1 then
return Enum.KeyCode.MouseLeftButton
elseif inputtype == Enum.UserInputType.MouseButton3 then
return Enum.KeyCode.MouseMiddleButton
elseif inputtype == Enum.UserInputType.MouseButton2 then
return Enum.KeyCode.MouseRightButton
end
return
end
--- returns a platform id corresponding to `inputobject.UserInputType` \
--- `0`: unknown \
--- `1`: pc \
--- `2`: mobile \
--- `3`: gamepad
function Inputs.platform(inputobject: InputObject)
return Inputs.Platforms[inputobject.UserInputType] or 0
end
--- binds function to `UserInputService.InputBegan`
function Inputs.began(f: (inputobject: InputObject, sunkinput: boolean) -> ())
return Hook("k_InputBegan").Connect(f)
end
--- binds function to `UserInputService.InputEnded`
function Inputs.ended(f: (inputobject: InputObject, sunkinput: boolean) -> ())
return Hook("k_InputEnded").Connect(f)
end
--- binds function to `UserInputService.InputChanged`
function Inputs.changed(f: (inputobject: InputObject, sunkinput: boolean) -> ())
return Hook("k_InputChanged").Connect(f)
end
--- fires function when a key is double-tapped
function Inputs.doubletap(f: (keycode: Enum.KeyCode) -> ())
return Hook("k_DoubleTap").Connect(f)
end
--- fires function when the input platform is changed \
--- `0`: unknown \
--- `1`: pc \
--- `2`: mobile \
--- `3`: gamepad
function Inputs.platformchanged(f: (platformid: number) -> ())
return Hook("k_PlatformChanged").Connect(f)
end
--- fires function when the gamepad cursor is toggled on/off
function Inputs.gamepadcursorstatechanged(f: (gamepadcursorenabled: boolean) -> ())
return Hook("k_GamepadCursorStateChanged").Connect(f)
end
--- sets visibility of mouse icon
function Inputs.setmouseiconenabled(state: boolean)
UIS.MouseIconEnabled = state
end
--- returns `UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter`
function Inputs.ismouselockcenter()
return UIS.MouseBehavior == Enum.MouseBehavior.LockCenter
end
--- returns keycode enum from its integral value
function Inputs.findkeycodefromvalue(keycodevalue: number): Enum.KeyCode?
return searchkeycodes(Keycodes, keycodevalue)
end
--- returns true if the inputobject was *not* obscured by the on-screen thumbstick
function Inputs.sinkthumbstickcheck(inputobject: InputObject)
if not PlayerGui then return false end
if Inputs.PlatformId == 2 then
local screenposition = inputobject.Position
local array = PlayerGui:GetGuiObjectsAtPosition(screenposition.X, screenposition.Y)
if #array == 0 then
return true
else
for _, GuiObject in array do
if GuiObject.Name ~= "DynamicThumbstickFrame" then continue end
return false
end
end
end
return true
end
--- toggles (displaces) mobile gui elements off-screen \
--- setting `.Enabled` does not work as that property is constantly set by the control scripts
function Inputs.togglemobileinterface(thumbstickenabled: boolean?, jumpbuttonenabled: boolean?)
assert(PlayerGui)
local function t(TouchGui: Instance)
if TouchGui.Name ~= "TouchGui" then return end
local touchcontrolframe = TouchGui:WaitForChild("TouchControlFrame")
local jumpbutton = touchcontrolframe:WaitForChild("JumpButton") :: ImageButton
if not jumpbuttonenabled then
jumpbutton.Position = UDim2.fromScale(2, 2)
end
local thumbstick = touchcontrolframe:WaitForChild("DynamicThumbstickFrame") :: Frame
if not thumbstickenabled then
thumbstick.Position = UDim2.fromScale(2, 2)
end
end
for _, thing in PlayerGui:GetChildren() do
t(thing)
end
return PlayerGui.ChildAdded:Connect(function(thing)
t(thing)
end)
end
--- connects on-screen gamepad \
--- emits keycodes `ButtonY`, `ButtonX`, `ButtonB`, `ButtonA`, `ButtonL1`, `ButtonR1`, `ButtonL2`, `ButtonR2`, `ButtonR3`
--- ```
--- --- Gamepad2D: GuiObject
--- --- ┗┳ [KeyId]: GuiObject
--- --- ┣━ ...: any
--- --- ┗━ Button: TextButton
--- ```
function Inputs.connectgamepad2D(gamepad2d: typeof(Inputs.Gamepad2D), f: (keycode: Enum.KeyCode, userinputstate: Enum.UserInputState, inputframe: Frame) -> ())
for keyid, keyenum in Inputs.Gamepad2DMap do
local buttonframe = gamepad2d:FindFirstChild(keyid)
if not buttonframe then warn(gamepad2d, keyid); continue end
local uistroke = buttonframe:FindFirstChildOfClass("UIStroke")
local textbutton = buttonframe:FindFirstChildOfClass("TextButton")
if not textbutton then continue end
textbutton:GetPropertyChangedSignal("GuiState"):Connect(function()
if textbutton.GuiState == Enum.GuiState.Press then
f(keyenum, Enum.UserInputState.Begin, buttonframe)
if uistroke then
uistroke.Color = Inputs.Select2DColor
end
elseif textbutton.GuiState == Enum.GuiState.Idle then
f(keyenum, Enum.UserInputState.End, buttonframe)
if uistroke then
uistroke.Color = Inputs.Deselect2DColor
end
end
end)
end
end
--- connects on-screen dpad \
--- emits keycodes `DPadUp`, `DPadLeft`, `DPadRight`, `DPadDown`, `ButtonL3`
--- ```
--- --- DPad2D: GuiObject
--- --- ┗┳ [KeyId]: GuiObject
--- --- ┣━ ...: any
--- --- ┗━ Button: TextButton
--- ```
function Inputs.connectdpad2D(dpad2d: typeof(Inputs.DPad2D), f: (keycode: Enum.KeyCode, userinputstate: Enum.UserInputState, inputframe: Frame) -> ())
for keyid, keyenum in Inputs.DPad2DMap do
local buttonframe = dpad2d:FindFirstChild(keyid)
if not buttonframe then warn(dpad2d, keyid); continue end
local uistroke = buttonframe:FindFirstChildOfClass("UIStroke")
local textbutton = buttonframe:FindFirstChildOfClass("TextButton")
if not textbutton then continue end
textbutton:GetPropertyChangedSignal("GuiState"):Connect(function()
if textbutton.GuiState == Enum.GuiState.Press then
f(keyenum, Enum.UserInputState.Begin, buttonframe)
if uistroke then
uistroke.Color = Inputs.Select2DColor
end
elseif textbutton.GuiState == Enum.GuiState.Idle then
f(keyenum, Enum.UserInputState.End, buttonframe)
if uistroke then
uistroke.Color = Inputs.Deselect2DColor
end
end
end)
end
end
if IsServer then
else
Inputs.PlatformId = Inputs.Platforms[UIS:GetLastInputType()]
LocalPlayer.CharacterAdded:Connect(function(C: Model)
Inputs.cursorblacklistadd(C)
end)
LocalPlayer.CharacterRemoving:Connect(Inputs.cursorblacklistremove)
Inputs.GuiInset = GuiService:GetGuiInset()
GuiService:GetPropertyChangedSignal("TopbarInset"):Connect(function()
Inputs.GuiInset = GuiService:GetGuiInset()
end)
local function updatemouse(inputobject: InputObject)
local screenposition = Vector2.zero
if Inputs.PlatformId == 2 then
local inputposition: Vector2|Vector3 = inputobject.Position
if Inputs.MobileScreenCenter then
inputposition = Inputs.MobileScreenCenter() or inputposition
end
screenposition = Vector2.new(inputposition.X, inputposition.Y)
else
screenposition = UIS:GetMouseLocation() - Inputs.GuiInset
end
local unitray = Camera:ScreenPointToRay(screenposition.X, screenposition.Y)
local result = rayblacklist(unitray.Origin, unitray.Direction * 0x800) --- ray length 2048 units
if result then
Cursor.Position = result.Position
Cursor.Normal = result.Normal
Cursor.Instance = result.Instance
else
Cursor.Position = unitray.Origin + (unitray.Direction * 0x800)
Cursor.Normal = Vector3.yAxis
Cursor.Instance = nil
end
Cursor.Camera = Camera.CFrame.LookVector
end
--- input changes
UIS.InputBegan:Connect(function(inputobject: InputObject, ...)
if Inputs.sinkthumbstickcheck(inputobject) then return end
Hook("k_InputBegan").Fire(inputobject, ...)
local now, keycode = os.clock(), inputobject.KeyCode
local keycodetimetable = Inputs.InputTimes
do --- doubletap inputs
local later = keycodetimetable[keycode] or 0
keycodetimetable[keycode] = now
if now - later < Inputs.DoubleTapTime then
Hook("k_DoubleTap").Fire(keycode)
end
end
updatemouse(inputobject)
end)
UIS.InputEnded:Connect(function(...)
Hook("k_InputEnded").Fire(...)
updatemouse(...)
end)
UIS.InputChanged:Connect(function(...)
Hook("k_InputChanged").Fire(...)
updatemouse(...)
end)
UIS.TouchStarted:Connect(function(...)
if not Inputs.sinkthumbstickcheck(...) then return end
Hook("k_InputBegan").Fire(...)
end)
UIS.TouchEnded:Connect(function(...)
Hook("k_InputEnded").Fire(...)
end)
UIS.TouchMoved:Connect(function(...)
Hook("k_InputChanged").Fire(...)
end)
UIS.LastInputTypeChanged:Connect(function(userinputtype: Enum.UserInputType)
Inputs.TouchEnabled = UIS.TouchEnabled
local platformid = Inputs.Platforms[userinputtype] or Inputs.PlatformId
if Inputs.PlatformId ~= platformid then
Inputs.PlatformId = platformid
Hook("k_PlatformChanged").Fire(Inputs.PlatformId)
end
end)
GamepadService:GetPropertyChangedSignal("GamepadCursorEnabled"):Connect(function()
Inputs.GamepadCursorEnabled = GamepadService.GamepadCursorEnabled
Hook("k_GamepadCursorStateChanged").Fire(GamepadService.GamepadCursorEnabled)
end)
end
return Inputs