A while ago, I started to work on a plug-in called Color+, then I ran into an issue where it would barely run due to a dependency on the mouse, and the mouse has to be active on a plugin to use. I pretty much gave up on it, then today I decided to try to use the UserInputService and the Camera to fix this issue. So, I present the MouseModule.
Note: This is more for plug-ins rather than LocalScripts due to the nature that the UserInputService ignores the TopBar for Mouse.X and Mouse.Y positions.
For using in a plug-in, you can just replace any code like this:
local Mouse = Plugin:GetMouse()
for…
local Mouse = require(script.MouseModule)
Included functionality:
Included:
-Mouse.X
-Mouse.Y
-Mouse.ViewSizeX
-Mouse.ViewSizeY
-Mouse.Hit
-Mouse.Target
-Mouse.TargetSurface
-Mouse.TargetFilter (Bonus: Accepts a single instance, nil, or a table of instances!)
-Mouse.Origin
-Mouse.UnitRay
-Mouse.Button1Down ()
-Mouse.Button1Up ()
-Mouse.Button2Down ()
-Mouse.Button2Up ()
-Mouse.WheelForward ()
-Mouse.WheelBackward ()
Not included functionality:
-Mouse.Icon
-Mouse.Idle ()
-Mouse.Move ()
-Mouse.KeyDown (Key) [Deprecated, use UserInputService]
-Mouse.KeyUp (Key) [Deprecated, use UserInputService]
Hopefully some people will find more uses for this, and (possibly) add this to qCmdUt, I know people are annoyed with the deactivation.
Module source with-out the info at the top:
[code]local NewMouse = {}
local CFNew,CFAng,RayNew,InsNew = CFrame.new,CFrame.Angles,Ray.new,Instance.new
local pi = math.pi
local NormalId,UserInputType = Enum.NormalId,Enum.UserInputType
local Bindable1 = InsNew(“BindableEvent”)
local Bindable2 = InsNew(“BindableEvent”)
local Bindable3 = InsNew(“BindableEvent”)
local Bindable4 = InsNew(“BindableEvent”)
local Bindable5 = InsNew(“BindableEvent”)
local Bindable6 = InsNew(“BindableEvent”)
NewMouse.X = 0
NewMouse.Y = 0
NewMouse.Hit = CFNew()
NewMouse.Button1Down = Bindable1.Event
NewMouse.Button1Up = Bindable2.Event
NewMouse.Button2Down = Bindable3.Event
NewMouse.Button2Up = Bindable4.Event
NewMouse.WheelForward = Bindable5.Event
NewMouse.WheelBackward = Bindable6.Event
local Camera = game.Workspace.CurrentCamera
local UserInputService = game:GetService(“UserInputService”)
local function VectorToFace(Cframe,Vector)
local X,Y,Z,XX,YX,ZX,XY,YY,ZY,XZ,YZ,ZZ = Cframe:components()
local VX,VY,VZ = Vector.X,Vector.Y,Vector.Z
if VX == YX and VY == YY and VZ == YZ then
return NormalId.Top
elseif VX == XX and VY == XY and VZ == XZ then
return NormalId.Right
elseif VX == ZX and VY == ZY and VZ == ZZ then
return NormalId.Back
elseif VX == -YX and VY == -YY and VZ == -YZ then
return NormalId.Bottom
elseif VX == -XX and VY == -XY and VZ == -XZ then
return NormalId.Left
elseif VX == -ZX and VY == -ZY and VZ == -ZZ then
return NormalId.Front
end
end
local function GetRayFromMousePoint(Distance)
local MouseRay = Camera:ScreenPointToRay(NewMouse.X,NewMouse.Y,Distance)
local EndPos = MouseRay.Origin + MouseRay.Direction
return RayNew(Camera.CoordinateFrame.p,EndPos - Camera.CoordinateFrame.p)
end
local function UpdateProps()
local MouseRay = GetRayFromMousePoint(999)
local MouseTarget,MouseHit,TargetSurface
if type(NewMouse.TargetFilter) == “table” then
MouseTarget,MouseHit,TargetSurface = game.Workspace:FindPartOnRayWithIgnoreList(MouseRay,NewMouse.TargetFilter)
else
MouseTarget,MouseHit,TargetSurface = game.Workspace:FindPartOnRay(MouseRay,NewMouse.TargetFilter)
end
NewMouse.Target = MouseTarget
if MouseTarget then
NewMouse.TargetSurface = VectorToFace(MouseTarget.CFrame,TargetSurface)
else
NewMouse.TargetSurface = nil
end
local CamCFp = Camera.CoordinateFrame.p
NewMouse.Origin = CFrame.new(CamCFp,MouseRay.Origin + MouseRay.Direction)
NewMouse.Hit = CFNew(MouseHit,CamCFp) * CFAng(0,pi,0)
NewMouse.UnitRay = GetRayFromMousePoint(1)
NewMouse.ViewSizeX = Camera.ViewportSize.X
NewMouse.ViewSizeY = Camera.ViewportSize.Y
end
local function OnKeyGeneral(Key)
NewMouse.X,NewMouse.Y = Key.Position.X,Key.Position.Y
if Key.UserInputType == UserInputType.MouseWheel then
if Key.Position.Z == 1 then
Bindable5:Fire()
elseif Key.Position.Z == -1 then
Bindable6:Fire()
end
end
UpdateProps()
end
local function OnKeyBegin(Key)
if Key.UserInputType == UserInputType.MouseButton1 then
Bindable1:Fire()
elseif Key.UserInputType == UserInputType.MouseButton2 then
Bindable3:Fire()
end
OnKeyGeneral(Key)
end
local function OnKeyEnd(Key)
if Key.UserInputType == UserInputType.MouseButton1 then
Bindable2:Fire()
elseif Key.UserInputType == UserInputType.MouseButton2 then
Bindable4:Fire()
end
OnKeyGeneral(Key)
end
UserInputService.InputBegan:connect(OnKeyBegin)
UserInputService.InputChanged:connect(OnKeyGeneral)
UserInputService.InputEnded:connect(OnKeyEnd)
return NewMouse[/code]