Mouse module:
local mouse = require(mouseModule).new()
-- Methods:
local screenPos, delta = mouse:GetPosition()
local isDown = mouse:IsDown(UserInputType button)
local cframeHit, target, surfaceNormal = mouse:ProjectMouseRay(table ignoreList)
-- Events:
mouse.ButtonDown(UserInputType button)
mouse.ButtonUp(UserInputType button)
mouse.Moved(Vector2 position, Vector2 deltaPosition)
mouse.Scrolled(Integer delta)
Module code:
-- Mouse
-- Crazyman32
-- April 28, 2015
--[[
Usage:
local mouse = require(thisModule).new()
METHODS:
<Vector2 position, Vector2 delta> mouse:GetPosition()
<Boolean isDown> mouse:IsDown(UserInputType button)
<CFrame cframe, Object target, Vector3 normal> mouse:ProjectMouseRay(table ignoreList)
EVENTS:
mouse.ButtonDown(UserInputType button)
mouse.ButtonUp(UserInputType button)
mouse.Moved(Vector2 position, Vector2 deltaPosition)
mouse.Scrolled(Integer delta)
--]]
local Mouse = {}
Mouse.__index = Mouse
function Mouse.new()
local player = game.Players.LocalPlayer
assert(player, "Could not get player")
local cam = game.Workspace.CurrentCamera
local Ray = Ray.new
local input = game:GetService("UserInputService")
local button1 = Enum.UserInputType.MouseButton1
local button2 = Enum.UserInputType.MouseButton2
local button3 = Enum.UserInputType.MouseButton3
local mouseMovement = Enum.UserInputType.MouseMovement
local mouseWheel = Enum.UserInputType.MouseWheel
local mouse = {}
local mousePos, mouseDelta = Vector2.new(), Vector2.new()
local clicking = {
[button1] = false;
[button2] = false;
[button3] = false;
}
local function CreateEvent(eventName)
local e = Instance.new("BindableEvent")
mouse[eventName] = e.Event
return function(...)
e:Fire(...)
end
end
-- Events ---------------------------------------------------------------
local buttonDown = CreateEvent("ButtonDown")
local buttonUp = CreateEvent("ButtonUp")
local moved = CreateEvent("Moved")
local scrolled = CreateEvent("Scrolled")
-------------------------------------------------------------------------
-- API ------------------------------------------------------------------
function mouse:IsDown(inputType)
return (clicking[inputType] == true)
end
function mouse:GetPosition()
return mousePos, mouseDelta
end
function mouse:ProjectMouseRay(ignoreList)
local pos = self:GetPosition()
local ray = cam:ScreenPointToRay(pos.X, pos.Y, 0)
ray = Ray(ray.Origin, (ray.Unit.Direction * 999))
local hit, hitPos, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignoreList or {}, true, false)
local cframe = CFrame.new(hitPos, (hitPos + ray.Unit.Direction))
return cframe, hit, normal
end
-------------------------------------------------------------------------
-- UserInputService Setup -----------------------------------------------
local function InputBegan(inputObject, gameProcessed)
if (gameProcessed) then return end
local inputType = inputObject.UserInputType
if (inputType == button1 or inputType == button2 or inputType == button3) then
clicking[inputType] = true
buttonDown(inputType)
end
end
local function InputChanged(inputObject, gameProcessed)
--if (gameProcessed) then return end
local inputType = inputObject.UserInputType
if (inputType == mouseMovement) then
local newMousePos = Vector2.new(inputObject.Position.X, inputObject.Position.Y)
local delta = newMousePos - mousePos
mousePos = newMousePos
mouseDelta = Vector2.new(delta.X, delta.Y)
moved(mousePos, mouseDelta)
elseif (inputType == mouseWheel) then
local num = inputObject.Position.Z
scrolled(num < 0 and -1 or num > 0 and 1 or 0)
end
end
local function InputEnded(inputObject, gameProcessed)
--if (gameProcessed) then return end
local inputType = inputObject.UserInputType
if (inputType == button1 or inputType == button2 or inputType == button3) then
clicking[inputType] = false
buttonUp(inputType)
end
end
input.InputBegan:connect(InputBegan)
input.InputChanged:connect(InputChanged)
input.InputEnded:connect(InputEnded)
-------------------------------------------------------------------------
return setmetatable(mouse, Mouse)
end
function Mouse:__tostring()
local pos = self:GetPosition()
return ("Mouse <" .. tostring(pos) .. ">")
end
return Mouse
Note: This works, but it is still a work in progress. I have a Keyboard module that is similar like this, thus eliminating any need for the Mouse object. Let me know of any issues you find, so that I can continue to fine-tune it.