Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ControllerInput = { };
- local enumToString = function (e)
- return e.Name;
- end;
- local formatFloatTo = function (f, d)
- return ("%." .. d .. "f"):format(f);
- end;
- local vector3ToString = function (v3)
- local precision = 3;
- return "Vector3(" ..
- formatFloatTo(v3.x, precision) .. ", " ..
- formatFloatTo(v3.y, precision) .. ", " ..
- formatFloatTo(v3.z, precision) ..
- ")";
- end;
- local inputObjectToString = function (inputObject)
- local sep = " \t ";
- return "State: " .. enumToString(inputObject.UserInputState) .. sep ..
- "Type: " .. enumToString(inputObject.UserInputType) .. sep ..
- "KeyCode: " ..enumToString(inputObject.KeyCode) .. sep ..
- "Position: " .. vector3ToString(inputObject.Position) .. sep ..
- "Delta: " .. vector3ToString(inputObject.Delta);
- end;
- local isKeyCodeThumbstick;
- local printInputObject = function (inputObject)
- if (inputObject.UserInputType == Enum.UserInputType.MouseMovement) then
- return;
- end
- if (isKeyCodeThumbstick(inputObject.KeyCode)) then
- return;
- end
- print(inputObjectToString(inputObject));
- end;
- --[[
- float/Vector3 input:getStateFor(KeyCode keyCode)
- Returns the state as a float or Vector3
- If KeyCode is a button such as ButtonX, it's either 0 or 1
- If KeyCode is a trigger (LT/RT on 360 controller, L2/R2 on PS4)
- - Roblox treats it as a button
- - But returns an analogue input
- - So this is between 0 and 1
- If KeyCode is a thumbstick
- - Returns as a Vector3
- void input:setDeadzone(float deadzone)
- Threshold for the thumbsticks
- float input:getDeadzone()
- Returns the threshold
- void input:bindEvent(KeyCode keyCode, Function callback)
- void input:bindEvent(KeyCode keyCode, float threshold, Function callback)
- When the state for this keycode changes and becomes >= threshold, calls callback
- When threshold not passed, defaults to 0.5
- void input:bindReleasedEvent(KeyCode keyCode, Function callback)
- void input:bindReleasedEvent(KeyCode keyCode, float threshold, Function callback)
- When the state for this keycode changes and becomes <= threshold, calls callback
- When threshold not passed, defaults to 0.5
- --]]
- local buttonKeyCodes = {
- [Enum.KeyCode.ButtonA] = true;
- [Enum.KeyCode.ButtonB] = true;
- [Enum.KeyCode.ButtonX] = true;
- [Enum.KeyCode.ButtonY] = true;
- [Enum.KeyCode.ButtonL1] = true;
- [Enum.KeyCode.ButtonL2] = true;
- [Enum.KeyCode.ButtonL3] = true;
- [Enum.KeyCode.ButtonR1] = true;
- [Enum.KeyCode.ButtonR2] = true;
- [Enum.KeyCode.ButtonR3] = true;
- [Enum.KeyCode.ButtonStart] = true;
- [Enum.KeyCode.ButtonSelect] = true;
- };
- local dpadKeyCodes = {
- [Enum.KeyCode.DPadUp] = true;
- [Enum.KeyCode.DPadDown] = true;
- [Enum.KeyCode.DPadLeft] = true;
- [Enum.KeyCode.DPadRight] = true;
- };
- -- No local on this definition as it is declared earlier
- isKeyCodeThumbstick = function (keyCode)
- return ((keyCode == Enum.KeyCode.Thumbstick1) or (keyCode == Enum.KeyCode.Thumbstick2));
- end;
- local isKeyCodeGamepadButton = function (keyCode)
- return (buttonKeyCodes[keyCode] and true or false);
- end;
- local isKeyCodeDPadButton = function (keyCode)
- return (dpadKeyCodes[keyCode] and true or false);
- end;
- local isKeyCodeTriggerButton = function (keyCode)
- return ((keyCode == Enum.KeyCode.ButtonL2) or (keyCode == Enum.KeyCode.ButtonR2));
- end;
- local isKeyCodeOnGamepad = function (keyCode)
- return (isKeyCodeThumbstick(keyCode) or isKeyCodeGamepadButton(keyCode) or isKeyCodeDPadButton(keyCode));
- end;
- local updateState = function (self, keyCode, value)
- self.states[keyCode] = value;
- end;
- local getValueForInputObject = function (inputObject)
- if (isKeyCodeThumbstick(inputObject.KeyCode)) then
- return inputObject.Position;
- else
- return inputObject.Position.z;
- end
- end;
- local testKeyCodeAndUpdateState = function (self, inputObject)
- local keyCode = inputObject.KeyCode;
- if (isKeyCodeOnGamepad(keyCode)) then
- printInputObject(inputObject);
- local value = getValueForInputObject(inputObject);
- updateState(self, keyCode, value);
- end
- end;
- local inputBegan = function (self, inputObject, gameProcessedEvent)
- testKeyCodeAndUpdateState(self, inputObject);
- end;
- local inputChanged = function (self, inputObject, gameProcessedEvent)
- testKeyCodeAndUpdateState(self, inputObject);
- end;
- local inputEnded = function (self, inputObject, gameProcessedEvent)
- testKeyCodeAndUpdateState(self, inputObject);
- end;
- -- Initial deadzone is optional, defaults to 0.2
- local init = function (self, initialDeadzone)
- initialDeadzone = initialDeadzone or 0.2;
- local data = {
- thumbstickDeadzone = initialDeadzone;
- states = { };
- events = { };
- releasedEvents = { };
- };
- setmetatable(data, {__index = ControllerInput});
- local uis = game:GetService("UserInputService");
- uis.InputBegan:connect(function (inputObject, gameProcessedEvent)
- inputBegan(data, inputObject, gameProcessedEvent);
- end);
- uis.InputChanged:connect(function (inputObject, gameProcessedEvent)
- inputChanged(data, inputObject, gameProcessedEvent);
- end);
- uis.InputEnded:connect(function (inputObject, gameProcessedEvent)
- inputEnded(data, inputObject, gameProcessedEvent);
- end);
- return data;
- end;
- -- TODO: Should setting the deadzone update the states for thumbstick?
- -- I.e. if they were above the previous deadzone, but then it was raised, should they be set to 0 in this call?
- -- Or should the raw value be stored, but deadzone used in getStateFor() ?
- local setDeadzone = function (self, deadzone)
- self.thumbstickDeadzone = deadzone;
- end;
- local getDeadzone = function (self)
- return self.thumbstickDeadzone;
- end;
- -- Returns nil if the key code is not on the gamepad
- -- If it is a valid keycode but we don't have data for it yet
- -- (e.g. user hasn't pressed the button)
- -- Puts default value of 0 into the states dictionary and returns 0
- -- Else returns the current state for the key
- -- Thumbsticks return as a Vector3
- -- If their magnitude is < deadzone, then returns (0, 0, 0)
- local getStateFor = function (self, keyCode)
- if (not isKeyCodeOnGamepad(keyCode)) then
- return nil;
- end
- local state = self.states[keyCode];
- if (state == nil) then
- state = 0;
- self.states[keyCode] = state;
- end
- if (isKeyCodeThumbstick(keyCode)) then
- if (state.magnitude < self:getDeadzone()) then
- state = Vector3.new(0, 0, 0);
- end
- end
- return state;
- end;
- ControllerInput.init = init;
- ControllerInput.setDeadzone = setDeadzone;
- ControllerInput.setThumbstickThreashold = setDeadzone;
- ControllerInput.getDeadzone = getDeadzone;
- ControllerInput.getThumbstickThreashold = getDeadzone;
- ControllerInput.getStateFor = getStateFor;
- ControllerInput.isKeyCodeThumbstick = isKeyCodeThumbstick;
- ControllerInput.isKeyCodeGamepadButton = isKeyCodeGamepadButton;
- ControllerInput.isKeyCodeDPadButton = isKeyCodeDPadButton;
- ControllerInput.isKeyCodeTriggerButton = isKeyCodeTriggerButton;
- ControllerInput.isKeyCodeOnGamepad = isKeyCodeOnGamepad;
- return ControllerInput;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement