Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.99 KB | None | 0 0
  1. local ControllerInput = { };
  2.  
  3.  
  4. local enumToString = function (e)
  5.     return e.Name;
  6. end;
  7.  
  8.  
  9. local formatFloatTo = function (f, d)
  10.     return ("%." .. d .. "f"):format(f);
  11. end;
  12.  
  13.  
  14. local vector3ToString = function (v3)
  15.     local precision = 3;
  16.     return "Vector3(" ..
  17.         formatFloatTo(v3.x, precision) .. ", " ..
  18.         formatFloatTo(v3.y, precision) .. ", " ..
  19.         formatFloatTo(v3.z, precision) ..
  20.         ")";
  21. end;
  22.  
  23.  
  24. local inputObjectToString = function (inputObject)
  25.     local sep = " \t ";
  26.    
  27.     return "State: " .. enumToString(inputObject.UserInputState) .. sep ..
  28.         "Type: " .. enumToString(inputObject.UserInputType) .. sep ..
  29.         "KeyCode: " ..enumToString(inputObject.KeyCode) .. sep ..
  30.         "Position: " .. vector3ToString(inputObject.Position) .. sep ..
  31.         "Delta: " .. vector3ToString(inputObject.Delta);
  32. end;
  33.  
  34.  
  35. local isKeyCodeThumbstick;
  36.  
  37.  
  38. local printInputObject = function (inputObject)
  39.     if (inputObject.UserInputType == Enum.UserInputType.MouseMovement) then
  40.         return;
  41.     end
  42.    
  43.     if (isKeyCodeThumbstick(inputObject.KeyCode)) then
  44.         return;
  45.     end
  46.    
  47.     print(inputObjectToString(inputObject));
  48. end;
  49.  
  50.  
  51. --[[
  52.     float/Vector3 input:getStateFor(KeyCode keyCode)
  53.         Returns the state as a float or Vector3
  54.         If KeyCode is a button such as ButtonX, it's either 0 or 1
  55.         If KeyCode is a trigger (LT/RT on 360 controller, L2/R2 on PS4)
  56.             - Roblox treats it as a button
  57.             - But returns an analogue input
  58.             - So this is between 0 and 1
  59.         If KeyCode is a thumbstick
  60.             - Returns as a Vector3
  61.            
  62.            
  63.     void input:setDeadzone(float deadzone)
  64.         Threshold for the thumbsticks
  65.  
  66.  
  67.     float input:getDeadzone()
  68.         Returns the threshold
  69.        
  70.        
  71.     void input:bindEvent(KeyCode keyCode, Function callback)
  72.     void input:bindEvent(KeyCode keyCode, float threshold, Function callback)
  73.         When the state for this keycode changes and becomes >= threshold, calls callback
  74.         When threshold not passed, defaults to 0.5
  75.    
  76.     void input:bindReleasedEvent(KeyCode keyCode, Function callback)
  77.     void input:bindReleasedEvent(KeyCode keyCode, float threshold, Function callback)
  78.         When the state for this keycode changes and becomes <= threshold, calls callback
  79.         When threshold not passed, defaults to 0.5
  80.    
  81. --]]
  82.  
  83.  
  84. local buttonKeyCodes = {
  85.     [Enum.KeyCode.ButtonA] = true;
  86.     [Enum.KeyCode.ButtonB] = true;
  87.     [Enum.KeyCode.ButtonX] = true;
  88.     [Enum.KeyCode.ButtonY] = true;
  89.     [Enum.KeyCode.ButtonL1] = true;
  90.     [Enum.KeyCode.ButtonL2] = true;
  91.     [Enum.KeyCode.ButtonL3] = true;
  92.     [Enum.KeyCode.ButtonR1] = true;
  93.     [Enum.KeyCode.ButtonR2] = true;
  94.     [Enum.KeyCode.ButtonR3] = true;
  95.     [Enum.KeyCode.ButtonStart] = true;
  96.     [Enum.KeyCode.ButtonSelect] = true;
  97. };
  98.  
  99.  
  100. local dpadKeyCodes = {
  101.     [Enum.KeyCode.DPadUp] = true;
  102.     [Enum.KeyCode.DPadDown] = true;
  103.     [Enum.KeyCode.DPadLeft] = true;
  104.     [Enum.KeyCode.DPadRight] = true;
  105. };
  106.  
  107.  
  108. -- No local on this definition as it is declared earlier
  109. isKeyCodeThumbstick = function (keyCode)
  110.     return ((keyCode == Enum.KeyCode.Thumbstick1) or (keyCode == Enum.KeyCode.Thumbstick2));
  111. end;
  112.  
  113.  
  114. local isKeyCodeGamepadButton = function (keyCode)
  115.     return (buttonKeyCodes[keyCode] and true or false);
  116. end;
  117.  
  118.  
  119. local isKeyCodeDPadButton = function (keyCode)
  120.     return (dpadKeyCodes[keyCode] and true or false);
  121. end;
  122.  
  123.  
  124. local isKeyCodeTriggerButton = function (keyCode)
  125.     return ((keyCode == Enum.KeyCode.ButtonL2) or (keyCode == Enum.KeyCode.ButtonR2));
  126. end;
  127.  
  128.  
  129. local isKeyCodeOnGamepad = function (keyCode)
  130.     return (isKeyCodeThumbstick(keyCode) or isKeyCodeGamepadButton(keyCode) or isKeyCodeDPadButton(keyCode));
  131. end;
  132.  
  133.  
  134. local updateState = function (self, keyCode, value)
  135.     self.states[keyCode] = value;
  136. end;
  137.  
  138.  
  139. local getValueForInputObject = function (inputObject)
  140.     if (isKeyCodeThumbstick(inputObject.KeyCode)) then
  141.         return inputObject.Position;
  142.     else
  143.         return inputObject.Position.z;
  144.     end
  145. end;
  146.  
  147.  
  148. local testKeyCodeAndUpdateState = function (self, inputObject)
  149.     local keyCode = inputObject.KeyCode;
  150.     if (isKeyCodeOnGamepad(keyCode)) then
  151.         printInputObject(inputObject);
  152.        
  153.         local value = getValueForInputObject(inputObject);
  154.         updateState(self, keyCode, value);
  155.     end
  156. end;
  157.  
  158.  
  159. local inputBegan = function (self, inputObject, gameProcessedEvent)
  160.     testKeyCodeAndUpdateState(self, inputObject);
  161. end;
  162.  
  163.  
  164. local inputChanged = function (self, inputObject, gameProcessedEvent)
  165.     testKeyCodeAndUpdateState(self, inputObject);
  166. end;
  167.  
  168.  
  169. local inputEnded = function (self, inputObject, gameProcessedEvent)
  170.     testKeyCodeAndUpdateState(self, inputObject);
  171. end;
  172.  
  173.  
  174. -- Initial deadzone is optional, defaults to 0.2
  175. local init = function (self, initialDeadzone)
  176.     initialDeadzone = initialDeadzone or 0.2;
  177.     local data = {
  178.         thumbstickDeadzone = initialDeadzone;
  179.         states = { };
  180.         events = { };
  181.         releasedEvents = { };
  182.     };
  183.     setmetatable(data, {__index = ControllerInput});
  184.    
  185.     local uis = game:GetService("UserInputService");
  186.    
  187.     uis.InputBegan:connect(function (inputObject, gameProcessedEvent)
  188.         inputBegan(data, inputObject, gameProcessedEvent);
  189.     end);
  190.    
  191.     uis.InputChanged:connect(function (inputObject, gameProcessedEvent)
  192.         inputChanged(data, inputObject, gameProcessedEvent);
  193.     end);
  194.    
  195.     uis.InputEnded:connect(function (inputObject, gameProcessedEvent)
  196.         inputEnded(data, inputObject, gameProcessedEvent);
  197.     end);
  198.    
  199.     return data;
  200. end;
  201.  
  202.  
  203. -- TODO: Should setting the deadzone update the states for thumbstick?
  204. -- I.e. if they were above the previous deadzone, but then it was raised, should they be set to 0 in this call?
  205. -- Or should the raw value be stored, but deadzone used in getStateFor() ?
  206. local setDeadzone = function (self, deadzone)
  207.     self.thumbstickDeadzone = deadzone;
  208. end;
  209.  
  210.  
  211. local getDeadzone = function (self)
  212.     return self.thumbstickDeadzone;
  213. end;
  214.  
  215.  
  216. -- Returns nil if the key code is not on the gamepad
  217. -- If it is a valid keycode but we don't have data for it yet
  218. -- (e.g. user hasn't pressed the button)
  219. -- Puts default value of 0 into the states dictionary and returns 0
  220. -- Else returns the current state for the key
  221. -- Thumbsticks return as a Vector3
  222. -- If their magnitude is < deadzone, then returns (0, 0, 0)
  223. local getStateFor = function (self, keyCode)
  224.     if (not isKeyCodeOnGamepad(keyCode)) then
  225.         return nil;
  226.     end
  227.    
  228.     local state = self.states[keyCode];
  229.     if (state == nil) then
  230.         state = 0;
  231.         self.states[keyCode] = state;
  232.     end
  233.  
  234.     if (isKeyCodeThumbstick(keyCode)) then
  235.         if (state.magnitude < self:getDeadzone()) then
  236.             state = Vector3.new(0, 0, 0);
  237.         end
  238.     end
  239.    
  240.     return state;
  241. end;
  242.  
  243.  
  244. ControllerInput.init = init;
  245. ControllerInput.setDeadzone = setDeadzone;
  246. ControllerInput.setThumbstickThreashold = setDeadzone;
  247. ControllerInput.getDeadzone = getDeadzone;
  248. ControllerInput.getThumbstickThreashold = getDeadzone;
  249. ControllerInput.getStateFor = getStateFor;
  250.  
  251. ControllerInput.isKeyCodeThumbstick = isKeyCodeThumbstick;
  252. ControllerInput.isKeyCodeGamepadButton = isKeyCodeGamepadButton;
  253. ControllerInput.isKeyCodeDPadButton = isKeyCodeDPadButton;
  254. ControllerInput.isKeyCodeTriggerButton = isKeyCodeTriggerButton;
  255. ControllerInput.isKeyCodeOnGamepad = isKeyCodeOnGamepad;
  256. return ControllerInput;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement