function MouseLockController.OnMouseLockToggled(n)n.isMouseLocked=not n.isMouseLocked if n.isMouseLocked then local t=script:FindFirstChild("\067\117\114\115\111\114\073\109\097\103\101")if t and(t:IsA("\083\116\114\105\110\103\086\097\108\117\101")and t.Value)then n.savedMouseCursor=Mouse.Icon Mouse.Icon=t.Value else if t then t:Destroy()end t=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")t.Name="\067\117\114\115\111\114\073\109\097\103\101"t.Value=DEFAULT_MOUSE_LOCK_CURSOR t.Parent=script n.savedMouseCursor=Mouse.Icon Mouse.Icon=DEFAULT_MOUSE_LOCK_CURSOR end else if n.savedMouseCursor then Mouse.Icon=n.savedMouseCursor n.savedMouseCursor=nil end end n.mouseLockToggledEvent:Fire()end function MouseLockController.DoMouseLockSwitch(B,n,t,H)if t==Enum.UserInputState.Begin then B:OnMouseLockToggled()return Enum.ContextActionResult.Sink end return Enum.ContextActionResult.Pass end function MouseLockController.BindContextActions(n)ContextActionService:BindActionAtPriority(CONTEXT_ACTION_NAME,function(t,H,B)return n:DoMouseLockSwitch(t,H,B)end,false,MOUSELOCK_ACTION_PRIORITY,unpack(n.boundKeys))end function MouseLockController.UnbindContextActions(n)ContextActionService:UnbindAction(CONTEXT_ACTION_NAME)end function MouseLockController.IsMouseLocked(n)return n.enabled and n.isMouseLocked end function MouseLockController.EnableMouseLock(t,n)if n~=t.enabled then t.enabled=n if t.enabled then t:BindContextActions()else if Mouse.Icon~=""then Mouse.Icon=""end t:UnbindContextActions()if t.isMouseLocked then t.mouseLockToggledEvent:Fire()end t.isMouseLocked=false end end end return MouseLockController
--
function MouseLockController:OnMouseLockToggled()
	self.isMouseLocked = not self.isMouseLocked

	if self.isMouseLocked then
		local cursorImageValueObj = script:FindFirstChild("CursorImage")
		if cursorImageValueObj and cursorImageValueObj:IsA("StringValue") and cursorImageValueObj.Value then
			self.savedMouseCursor = Mouse.Icon
			Mouse.Icon = cursorImageValueObj.Value
		else
			if cursorImageValueObj then
				cursorImageValueObj:Destroy()
			end
			cursorImageValueObj = Instance.new("StringValue")
			cursorImageValueObj.Name = "CursorImage"
			cursorImageValueObj.Value = DEFAULT_MOUSE_LOCK_CURSOR
			cursorImageValueObj.Parent = script
			self.savedMouseCursor = Mouse.Icon
			Mouse.Icon = DEFAULT_MOUSE_LOCK_CURSOR
		end
	else
		if self.savedMouseCursor then
			Mouse.Icon = self.savedMouseCursor
			self.savedMouseCursor = nil
		end
	end

	self.mouseLockToggledEvent:Fire()
end

function MouseLockController:DoMouseLockSwitch(name, state, input)
	if state == Enum.UserInputState.Begin then
		self:OnMouseLockToggled()
		return Enum.ContextActionResult.Sink
	end
	return Enum.ContextActionResult.Pass
end

function MouseLockController:BindContextActions()
	ContextActionService:BindActionAtPriority(CONTEXT_ACTION_NAME, function(name, state, input)
		return self:DoMouseLockSwitch(name, state, input)
	end, false, MOUSELOCK_ACTION_PRIORITY, unpack(self.boundKeys))
end

function MouseLockController:UnbindContextActions()
	ContextActionService:UnbindAction(CONTEXT_ACTION_NAME)
end

function MouseLockController:IsMouseLocked()
	return self.enabled and self.isMouseLocked
end

function MouseLockController:EnableMouseLock(enable)
	if enable ~= self.enabled then

		self.enabled = enable

		if self.enabled then
			-- Enabling the mode
			self:BindContextActions()
		else
			-- Disabling
			-- Restore mouse cursor
			if Mouse.Icon~="" then
				Mouse.Icon = ""
			end

			self:UnbindContextActions()

			-- If the mode is disabled while being used, fire the event to toggle it off
			if self.isMouseLocked then
				self.mouseLockToggledEvent:Fire()
			end

			self.isMouseLocked = false
		end

	end
end

return MouseLockController
