Skip to content

Commit

Permalink
Added BindService
Browse files Browse the repository at this point in the history
  • Loading branch information
notqaltx committed Feb 17, 2024
1 parent 5fc4ad6 commit d9fe040
Show file tree
Hide file tree
Showing 11 changed files with 677 additions and 0 deletions.
File renamed without changes.
68 changes: 68 additions & 0 deletions Projects/BindService/Module/Features/Keystrokes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local KeyStrokes = {}

local services = {
ts = game:GetService("TweenService"),
uis = game:GetService("UserInputService"),
players = game:GetService("Players")
}
local activeKeyStrokes = {}

function KeyStrokes.CreateKeyStroke(key, frame)
local keyStroke = Instance.new("TextLabel", frame)
keyStroke.Name = "KeyStroke"
keyStroke.Size = UDim2.fromScale(1, 0.032)
keyStroke.BackgroundTransparency = 1
keyStroke.BorderSizePixel = 0
keyStroke.TextColor3 = Color3.fromRGB(255, 255, 255)
keyStroke.Font = Enum.Font.RobotoMono
keyStroke.TextScaled = true
keyStroke.Text = key
keyStroke.TextXAlignment = Enum.TextXAlignment.Right

return keyStroke
end
function KeyStrokes.FadeOut(keyStroke)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local fadeTween = services.ts:Create(keyStroke, tweenInfo, { TextTransparency = 1 })

fadeTween:Play()
fadeTween.Completed:Connect(function()
keyStroke:Destroy()
end)
end
function KeyStrokes.ListenForInput()
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "Keystrokes"
screenGui.Parent = services.players.LocalPlayer.PlayerGui

local frame = Instance.new("Frame", screenGui)
frame.Name = "MainFrame"
frame.BackgroundTransparency = 1
frame.Position = UDim2.fromScale(0.86, 0.266)
frame.Size = UDim2.fromScale(0.121, 0.699)

local listLayout = Instance.new("UIListLayout", frame)
listLayout.VerticalAlignment = Enum.VerticalAlignment.Bottom

local inputConnection = services.uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode.Name
local keyStroke = KeyStrokes.CreateKeyStroke(key, frame)
table.insert(activeKeyStrokes, keyStroke)
KeyStrokes.FadeOut(keyStroke)
end
end)
return inputConnection
end
--function KeyStrokes.StopListening(inputConnection)
-- if inputConnection then
-- inputConnection:Disconnect()
-- for _, keyStroke in ipairs(activeKeyStrokes) do
-- keyStroke:Destroy()
-- end
-- services.players:FindFirstChild("Keystrokes"):Destroy()
-- activeKeyStrokes = {}
-- end
--end

return KeyStrokes
85 changes: 85 additions & 0 deletions Projects/BindService/Module/Features/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
-- Features Module
local Features = {}
local uis = game:GetService("UserInputService")
local keystrokes = require(script.Keystrokes)

local inputFilters, inputMappings, inputProfiles = {}, {}, {}
local inputFeaturesMeta = {
inputFilters = {
__index = inputFilters,
__newindex = function(_, inputType, filterFunction)
inputFilters[inputType] = filterFunction
end
},
inputMappings = {
__index = inputMappings,
__newindex = function(_, inputType, mappedInputType)
inputMappings[inputType] = mappedInputType
end
},
inputProfiles = {
__index = inputProfiles,
__newindex = function(_, profileName, bindings)
inputProfiles[profileName] = bindings
end
},
ListenForInput = function() keystrokes.ListenForInput() end
--StopListening = function(inputConnection)
-- keystrokes.StopListening(inputConnection)
--end
}

-- Input Filtering
function Features.AddInputFilter(inputType, filterFunction)
rawset(inputFilters, inputType, filterFunction)
end

function Features.RemoveInputFilter(inputType)
inputFilters[inputType] = nil
end

function Features.IsInputAllowed(inputType)
local filter = inputFilters[inputType]
if filter then
return filter()
else
return true
end
end

-- Input Mapping
function Features.AddInputMapping(inputType, mappedInputType)
rawset(inputMappings, inputType, mappedInputType)
end

function Features.RemoveInputMapping(inputType)
inputMappings[inputType] = nil
end

function Features.GetMappedInput(inputType)
return inputMappings[inputType] or inputType
end

-- Input Profiles
function Features.SaveInputProfile(profileName, bindings)
rawset(inputProfiles, profileName, bindings)
end

function Features.LoadInputProfile(profileName)
return inputProfiles[profileName]
end

function Features.DeleteInputProfile(profileName)
inputProfiles[profileName] = nil
end

local featuresMeta = {
__index = function(self, key)
return inputFeaturesMeta[key]
end,
__newindex = function()
error("Attempt to modify read-only table")
end
}

return setmetatable(Features, featuresMeta)
8 changes: 8 additions & 0 deletions Projects/BindService/Module/Functions/Attributes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
return {
number = true,
string = true,
bool = true,
CFrame = true,
Color3 = true,
Instance = true
}
38 changes: 38 additions & 0 deletions Projects/BindService/Module/Functions/Errors.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local Errors = {}

Errors.INVALID_INPUT_ERROR = "Invalid input: %s is expected to be %s."
Errors.TYPE_ERROR = "Type error: %s is expected to be of type %s."
Errors.UNKNOWN_ERROR = "An unknown error occurred."

local Validators = require(script.Parent.Validators)
local function createErrorHandler(errorType)
return function(funcName, paramName, expectedType, minValue)
local errorMessage = errorType == "INVALID_INPUT_ERROR" and Errors.INVALID_INPUT_ERROR or Errors.TYPE_ERROR
local formattedMessage = string.format(errorMessage, paramName, expectedType)
error(string.format("%s in function %s", formattedMessage, funcName), 2)
end
end
local errorHandlers = {
handleInvalidInputError = createErrorHandler("INVALID_INPUT_ERROR"),
handleTypeError = createErrorHandler("TYPE_ERROR"),
handleUnknownError = function()
error(Errors.UNKNOWN_ERROR, 2)
end
}
local errorMeta = {
__index = function(_, key)
local handler = errorHandlers[key]
if handler then
return function(...)
handler(...)
end
else
return nil
end
end,
__metatable = "The metatable is locked",
__newindex = function()
error("Attempt to modify read-only table")
end
}
return setmetatable({}, errorMeta)
69 changes: 69 additions & 0 deletions Projects/BindService/Module/Functions/Validators.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
local Validators = {}

local function validateInputBinding(funcName, name, callback, debounce, keys)
local validation = {
valid = true,
paramName = "",
expectedType = "",
}

if type(name) ~= "string" then
validation.valid = false
validation.paramName = "name"
validation.expectedType = "string"
return validation
end

if type(callback) ~= "function" then
validation.valid = false
validation.paramName = "callback"
validation.expectedType = "function"
return validation
end

if type(debounce) ~= "number" or debounce < 0 then
validation.valid = false
validation.paramName = "debounce"
validation.expectedType = "number (>= 0)"
return validation
end

if type(keys) == "table" then
-- If keys is a table, validate each key code
for _, key in ipairs(keys) do
if typeof(key) ~= "EnumItem" or (key.EnumType ~= Enum.KeyCode and key.EnumType ~= Enum.UserInputType) then
validation.valid = false
validation.paramName = "keys"
validation.expectedType = "table of Enum.KeyCode or Enum.UserInputType values"
return validation
end
end
elseif typeof(keys) == "EnumItem" and (keys.EnumType == Enum.KeyCode or keys.EnumType == Enum.UserInputType) then
-- If keys is a single Enum.KeyCode or Enum.UserInputType value, convert it to a table
keys = {keys}
else
validation.valid = false
validation.paramName = "keys"
validation.expectedType = "table of Enum.KeyCode or Enum.UserInputType values"
return validation
end

return validation
end

local validatorsMeta = {
__index = function(_, key)
if key == "validateInputBinding" then
return function(...)
return validateInputBinding(...)
end
else
return nil
end
end,
__metatable = "The metatable is locked",
__newindex = function()
error("Attempt to modify read-only table")
end
}
return setmetatable({}, validatorsMeta)
Loading

0 comments on commit d9fe040

Please sign in to comment.