Custom center-locked mouse camera control toggle

The purpose of having the if MouseLockController:GetIsMouseLocked() then block is to check whether the player is already using regular shift lock. MouseLockController:OnMouseLockToggled() takes them out of shift lock first before turning on the custom mouse lock. The mouse controller internally handles states and mouse icons, so I just tell it to turn itself off instead of hacking around it. If you were to remove this block and just set the mouse icon to "" then if you go into shift lock and press T to switch into this custom center lock state, when you press T again to turn off the custom center lock, if you try to press shift to enable regular mouse lock, you have to press it twice because it thinks it’s still on the first time and turns it off when it was already indirectly disabled. I wanted to support shift lock at the same time as this, and that’s what that’s for.

Here’s the OnMouseLockToggled function in the MouseLockController module:

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

The first line internally switches the isMouseLocked state. If this function weren’t called and you switched into the custom center lock while shift lock is on then it would think it’s still in shift lock mode, even when you switch out. The rest of the code handles restoring the original mouse icon. isMouseLocked could have been written to instead of calling the function but I found it cleaner to just have the module do its thing instead of doing it for it.

2 Likes