Advertisement
Guest User

Untitled

a guest
May 9th, 2018
2,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.20 KB | None | 0 0
  1. --[[
  2.     // FileName: ShiftLockController
  3.     // Written by: jmargh
  4.     // Version 1.2
  5.     // Description: Manages the state of shift lock mode
  6.  
  7.     // Required by:
  8.         RootCamera
  9.  
  10.     // Note: ContextActionService sinks keys, so until we allow binding to ContextActionService without sinking
  11.     // keys, this module will use UserInputService.
  12. --]]
  13.  
  14. local Players = game:GetService('Players')
  15. local UserInputService = game:GetService('UserInputService')
  16. -- Settings and GameSettings are read only
  17. local Settings = UserSettings() -- ignore warning
  18. local GameSettings = Settings.GameSettings
  19.  
  20. local ShiftLockController = {}
  21.  
  22. --[[ Script Variables ]]--
  23. while not Players.LocalPlayer do
  24.     Players.PlayerAdded:wait()
  25. end
  26.  
  27. local LocalPlayer = Players.LocalPlayer
  28. local Mouse = LocalPlayer:GetMouse()
  29. local PlayerGui = LocalPlayer:WaitForChild('PlayerGui')
  30. local ScreenGui = nil
  31. local ShiftLockIcon = nil
  32. local InputCn = nil
  33. local IsShiftLockMode = false
  34. local IsShiftLocked = false
  35. local IsActionBound = false
  36. local IsInFirstPerson = false
  37. local IsSprinting = false
  38.  
  39. -- Toggle Event
  40. ShiftLockController.OnShiftLockToggled = Instance.new('BindableEvent')
  41.  
  42. -- wrapping long conditional in function
  43. local function isShiftLockMode()
  44.     --[[return LocalPlayer.DevEnableMouseLock and GameSettings.ControlMode == Enum.ControlMode.MouseLockSwitch and
  45.             LocalPlayer.DevComputerMovementMode ~= Enum.DevComputerMovementMode.ClickToMove and
  46.             GameSettings.ComputerMovementMode ~= Enum.ComputerMovementMode.ClickToMove and
  47.     LocalPlayer.DevComputerMovementMode ~= Enum.DevComputerMovementMode.Scriptable]]
  48.     if not LocalPlayer.Character or not LocalPlayer.Character:FindFirstChild("Gun") or not LocalPlayer.Character.Gun.Value then
  49.         return false
  50.     else
  51.         return true
  52.     end
  53.    
  54.     return true
  55. end
  56.  
  57. if not UserInputService.TouchEnabled then   -- TODO: Remove when safe on mobile
  58.     IsShiftLockMode = isShiftLockMode()
  59. end
  60.  
  61. --[[ Constants ]]--
  62. local SHIFT_LOCK_OFF = 'rbxasset://textures/ui/mouseLock_off.png'
  63. local SHIFT_LOCK_ON = 'rbxasset://textures/ui/mouseLock_on.png'
  64. local SHIFT_LOCK_CURSOR = 'rbxasset://textures/MouseLockedCursor.png'
  65.  
  66. --[[ Local Functions ]]--
  67. local function onShiftLockToggled()
  68.     IsShiftLocked = true--not IsShiftLocked
  69.     if IsShiftLocked then
  70.         ShiftLockIcon.Image = SHIFT_LOCK_ON
  71.         --Mouse.Icon = SHIFT_LOCK_CURSOR
  72.     else
  73.         ShiftLockIcon.Image = SHIFT_LOCK_OFF
  74.         --Mouse.Icon = ""
  75.     end
  76.     ShiftLockController.OnShiftLockToggled:Fire()
  77. end
  78.  
  79. local function initialize()
  80.     if ScreenGui then
  81.         ScreenGui:Destroy()
  82.         ScreenGui = nil
  83.     end
  84.     ScreenGui = Instance.new('ScreenGui')
  85.     ScreenGui.Name = "ControlGui"
  86.    
  87.     local frame = Instance.new('Frame')
  88.     frame.Name = "BottomLeftControl"
  89.     frame.Size = UDim2.new(0, 130, 0, 46)
  90.     frame.Position = UDim2.new(0, 0, 1, -46)
  91.     frame.BackgroundTransparency = 1
  92.     frame.Parent = ScreenGui
  93.    
  94.     ShiftLockIcon = Instance.new('ImageButton')
  95.     ShiftLockIcon.Name = "MouseLockLabel"
  96.     ShiftLockIcon.Size = UDim2.new(0, 31, 0, 31)
  97.     ShiftLockIcon.Position = UDim2.new(0, 12, 0, 2)
  98.     ShiftLockIcon.BackgroundTransparency = 1
  99.     ShiftLockIcon.Image = IsShiftLocked and SHIFT_LOCK_ON or SHIFT_LOCK_OFF
  100.     ShiftLockIcon.Visible = true
  101.     ShiftLockIcon.Parent = frame
  102.    
  103.     ShiftLockIcon.MouseButton1Click:connect(onShiftLockToggled)
  104.    
  105.     --ScreenGui.Parent = IsShiftLockMode and PlayerGui or nil
  106. end
  107.  
  108. --[[ Public API ]]--
  109. function ShiftLockController:IsShiftLocked()
  110.     return IsShiftLockMode and IsShiftLocked and not IsSprinting
  111. end
  112.  
  113. function ShiftLockController:SetIsInFirstPerson(isInFirstPerson)
  114.     IsInFirstPerson = isInFirstPerson
  115. end
  116.  
  117. --[[ Input/Settings Changed Events ]]--
  118. local mouseLockSwitchFunc = function(actionName, inputState, inputObject)
  119.     if IsShiftLockMode then
  120.         onShiftLockToggled()
  121.     end
  122. end
  123.  
  124. -- TODO: Remove when we figure out ContextActionService without sinking keys
  125. local function onShiftInputBegan(inputObject, isProcessed)
  126.     if isProcessed then return end
  127.     if (inputObject.UserInputType == Enum.UserInputType.Keyboard and inputObject.KeyCode == Enum.KeyCode.LeftShift) then
  128.         --
  129.         IsSprinting = true
  130.         --mouseLockSwitchFunc()
  131.         ShiftLockController.OnShiftLockToggled:Fire()
  132.     end
  133. end
  134.  
  135. local function onShiftInputEnded(inputObject, isProcessed)
  136.     if isProcessed then return end
  137.     if (inputObject.UserInputType == Enum.UserInputType.Keyboard and inputObject.KeyCode == Enum.KeyCode.LeftShift) then
  138.         --
  139.         IsSprinting = false
  140.         --mouseLockSwitchFunc()
  141.         ShiftLockController.OnShiftLockToggled:Fire()
  142.     end
  143. end
  144.  
  145. local function enableShiftLock()
  146.     IsShiftLockMode = isShiftLockMode()
  147.     if IsShiftLockMode then
  148.         if ScreenGui then
  149.             --ScreenGui.Parent = PlayerGui
  150.         end
  151.         if IsShiftLocked then
  152.             --Mouse.Icon = SHIFT_LOCK_CURSOR
  153.             ShiftLockController.OnShiftLockToggled:Fire()
  154.         end
  155.         if not IsActionBound then
  156.             InputCn = UserInputService.InputBegan:connect(onShiftInputBegan)
  157.             UserInputService.InputEnded:connect(onShiftInputEnded)
  158.             IsActionBound = true
  159.         end
  160.     end
  161. end
  162.  
  163. local function disableShiftLock()
  164.     if ScreenGui then ScreenGui.Parent = nil end
  165.     IsShiftLockMode = false
  166.     --Mouse.Icon = ""
  167.     if InputCn then
  168.         InputCn:disconnect()
  169.         InputCn = nil
  170.     end
  171.     IsActionBound = false
  172.     ShiftLockController.OnShiftLockToggled:Fire()
  173. end
  174.  
  175. function CharacterAdded(char)
  176.    
  177.     char:WaitForChild("Gun").Changed:connect(function()
  178.         if char.Gun.Value and not IsSprinting then
  179.             enableShiftLock()
  180.         else
  181.             disableShiftLock()
  182.         end
  183.     end)
  184.    
  185.     if char.Gun.Value and not IsSprinting then
  186.         enableShiftLock()
  187.     else
  188.         disableShiftLock()
  189.     end
  190. end
  191.  
  192. LocalPlayer.CharacterAdded:connect(CharacterAdded)
  193.  
  194. if LocalPlayer.Character then
  195.     CharacterAdded(LocalPlayer.Character)
  196. end
  197.  
  198. GameSettings.Changed:connect(function(property)
  199.             enableShiftLock() --Enum.ControlMode.MouseLockSwitch
  200.     --[[if property == 'ControlMode' then
  201.         if GameSettings.ControlMode == Enum.ControlMode.MouseLockSwitch then
  202.             enableShiftLock()
  203.         else
  204.             disableShiftLock()
  205.         end
  206.     elseif property == 'ComputerMovementMode' then
  207.         if GameSettings.ComputerMovementMode == Enum.ComputerMovementMode.ClickToMove then
  208.             disableShiftLock()
  209.         else
  210.             enableShiftLock()
  211.         end
  212.     end]]
  213. end)
  214.  
  215. LocalPlayer.Changed:connect(function(property)
  216.     if property == 'DevEnableMouseLock' then
  217.         if LocalPlayer.DevEnableMouseLock then
  218.             enableShiftLock()
  219.         else
  220.             disableShiftLock()
  221.         end
  222.     elseif property == 'DevComputerMovementMode' then
  223.         if LocalPlayer.DevComputerMovementMode == Enum.DevComputerMovementMode.ClickToMove or
  224.             LocalPlayer.DevComputerMovementMode == Enum.DevComputerMovementMode.Scriptable then
  225.             --
  226.             disableShiftLock()
  227.         else
  228.             enableShiftLock()
  229.         end
  230.     end
  231. end)
  232.  
  233. LocalPlayer.CharacterAdded:connect(function(character)
  234.     -- we need to recreate guis on character load
  235.     if not UserInputService.TouchEnabled then
  236.         initialize()
  237.     end
  238. end)
  239.  
  240. --[[ Initialization ]]--
  241.  -- TODO: Remove when safe! ContextActionService crashes touch clients with tupele is 2 or more
  242. if not UserInputService.TouchEnabled then
  243.     initialize()
  244.     if isShiftLockMode() then
  245.         InputCn = UserInputService.InputBegan:connect(onShiftInputBegan)
  246.             UserInputService.InputEnded:connect(onShiftInputEnded)
  247.         IsActionBound = true
  248.     end
  249. end
  250.  
  251. function ShiftLockController:Lock()
  252.     onShiftLockToggled()
  253. end
  254.  
  255. return ShiftLockController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement