Skip to content

Commit

Permalink
[Mouse] Allow customizing screen to slab transforms
Browse files Browse the repository at this point in the history
Fix flamendless#20.

Add function arg to customize screen to slab coordinates.

Improve interoperability with screen resizing libraries by allowing
users to specify a function to transform screen coordinates into game
coordinates.

For example, with Ulydev/push you could do:

    local function get_push_mouse(x,y)
        local new_x,new_y = push:toGame(x,y)
        if new_x and new_y then
            return new_x,new_y
        end
        return x,y
    end
    local game_width, game_height, window_width, window_height = 800, 600, 1920, 1080
    push:setupScreen(game_width, game_height, window_width, window_height)
    Slab.Initialize(get_push_mouse, args)

close flamendless#80
  • Loading branch information
idbrii authored and coding-jackalope committed Aug 5, 2021
1 parent c41d311 commit 5fb76d3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Internal/Input/Mouse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ local Events = {}
-- For more information, refer to the SetCustomCursor/ClearCustomCursor functions.
local CustomCursors = {}

local function TransformPoint(X,Y)
return X,Y
end

local function OnMouseMoved(X, Y, DX, DY, IsTouch)
X, Y = TransformPoint(X, Y)
State.X = X
State.Y = Y
State.AsyncDeltaX = State.AsyncDeltaX + DX
Expand All @@ -78,6 +83,7 @@ local function PushEvent(Type, X, Y, Button, IsTouch, Presses)
end

local function OnMousePressed(X, Y, Button, IsTouch, Presses)
X, Y = TransformPoint(X, Y)
PushEvent(Common.Event.Pressed, X, Y, Button, IsTouch, Presses)

if MousePressedFn ~= nil then
Expand All @@ -86,6 +92,7 @@ local function OnMousePressed(X, Y, Button, IsTouch, Presses)
end

local function OnMouseReleased(X, Y, Button, IsTouch, Presses)
X, Y = TransformPoint(X, Y)
PushEvent(Common.Event.Released, X, Y, Button, IsTouch, Presses)

if MouseReleasedFn ~= nil then
Expand All @@ -110,7 +117,9 @@ local function ProcessEvents()
Events = {}
end

function Mouse.Initialize(Args)
function Mouse.Initialize(Args, TransformPointToSlab)
TransformPoint = TransformPointToSlab or TransformPoint

MouseMovedFn = love.handlers['mousemoved']
MousePressedFn = love.handlers['mousepressed']
MouseReleasedFn = love.handlers['mousereleased']
Expand Down

0 comments on commit 5fb76d3

Please sign in to comment.